2014-04-04 21:17:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
###
|
2014-10-21 11:45:11 +00:00
|
|
|
# @name Settings Module
|
2015-02-01 21:08:37 +00:00
|
|
|
# @copyright 2015 by Tobias Reich
|
2014-04-04 21:17:54 +00:00
|
|
|
###
|
|
|
|
|
|
|
|
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|
|
|
|
2016-01-19 10:45:41 +00:00
|
|
|
final class Settings extends Module {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
private static $cache = null;
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function get() {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
if (self::$cache) return self::$cache;
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Execute query
|
2016-01-24 21:14:20 +00:00
|
|
|
$query = Database::prepare(Database::get(), "SELECT * FROM ?", array(LYCHEE_TABLE_SETTINGS));
|
|
|
|
$settings = Database::get()->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Add each to return
|
|
|
|
while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
# Convert plugins to array
|
|
|
|
$return['plugins'] = explode(';', $return['plugins']);
|
|
|
|
|
|
|
|
self::$cache = $return;
|
2014-04-11 20:25:03 +00:00
|
|
|
|
2014-04-04 21:17:54 +00:00
|
|
|
return $return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function setLogin($oldPassword = '', $username, $password) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2016-01-24 21:14:20 +00:00
|
|
|
self::dependencies(isset($oldPassword, $username, $password));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
if ($oldPassword===self::get()['password']||self::get()['password']===crypt($oldPassword, self::get()['password'])) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Save username
|
2016-01-24 21:14:20 +00:00
|
|
|
if (self::setUsername($username)!==true) exit('Error: Updating username failed!');
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Save password
|
2016-01-24 21:14:20 +00:00
|
|
|
if (self::setPassword($password)!==true) exit('Error: Updating password failed!');
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exit('Error: Current password entered incorrectly!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
private static function setUsername($username) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2016-01-24 21:14:20 +00:00
|
|
|
self::dependencies(isset($username));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2015-02-08 14:36:13 +00:00
|
|
|
# Hash username
|
|
|
|
$username = getHashedString($username);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Execute query
|
2015-02-08 14:36:13 +00:00
|
|
|
# Do not prepare $username because it is hashed and save
|
|
|
|
# Preparing (escaping) the username would destroy the hash
|
2016-01-24 21:14:20 +00:00
|
|
|
$query = Database::prepare(Database::get(), "UPDATE ? SET value = '$username' WHERE `key` = 'username'", array(LYCHEE_TABLE_SETTINGS));
|
|
|
|
$result = Database::get()->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
2016-01-24 21:14:20 +00:00
|
|
|
Log::error(__METHOD__, __LINE__, Database::get()->error);
|
2014-05-30 14:55:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
private static function setPassword($password) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2016-01-24 21:14:20 +00:00
|
|
|
self::dependencies(isset($password));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2015-02-08 14:36:13 +00:00
|
|
|
# Hash password
|
|
|
|
$password = getHashedString($password);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Execute query
|
2014-08-29 19:38:40 +00:00
|
|
|
# Do not prepare $password because it is hashed and save
|
|
|
|
# Preparing (escaping) the password would destroy the hash
|
2016-01-24 21:14:20 +00:00
|
|
|
$query = Database::prepare(Database::get(), "UPDATE ? SET value = '$password' WHERE `key` = 'password'", array(LYCHEE_TABLE_SETTINGS));
|
|
|
|
$result = Database::get()->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
2016-01-24 21:14:20 +00:00
|
|
|
Log::error(__METHOD__, __LINE__, Database::get()->error);
|
2014-05-30 14:55:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function setDropboxKey($key) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2016-01-24 21:14:20 +00:00
|
|
|
self::dependencies(isset($key));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (strlen($key)<1||strlen($key)>50) {
|
2016-01-24 21:14:20 +00:00
|
|
|
Log::notice(__METHOD__, __LINE__, 'Dropbox key is either too short or too long');
|
2014-05-30 14:55:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Execute query
|
2016-01-24 21:14:20 +00:00
|
|
|
$query = Database::prepare(Database::get(), "UPDATE ? SET value = '?' WHERE `key` = 'dropboxKey'", array(LYCHEE_TABLE_SETTINGS, $key));
|
|
|
|
$result = Database::get()->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
2016-01-24 21:14:20 +00:00
|
|
|
Log::error(__METHOD__, __LINE__, Database::get()->error);
|
2014-05-30 14:55:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function setSortingPhotos($type, $order) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2016-01-24 21:14:20 +00:00
|
|
|
self::dependencies(isset($type, $order));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
$sorting = 'ORDER BY ';
|
|
|
|
|
|
|
|
# Set row
|
|
|
|
switch ($type) {
|
|
|
|
|
|
|
|
case 'id': $sorting .= 'id';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'title': $sorting .= 'title';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'description': $sorting .= 'description';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'public': $sorting .= 'public';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'type': $sorting .= 'type';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'star': $sorting .= 'star';
|
|
|
|
break;
|
|
|
|
|
2014-05-06 17:57:59 +00:00
|
|
|
case 'takestamp': $sorting .= 'takestamp';
|
2014-04-04 21:17:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default: exit('Error: Unknown type for sorting!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$sorting .= ' ';
|
|
|
|
|
|
|
|
# Set order
|
|
|
|
switch ($order) {
|
|
|
|
|
|
|
|
case 'ASC': $sorting .= 'ASC';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'DESC': $sorting .= 'DESC';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: exit('Error: Unknown order for sorting!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Execute query
|
2014-08-29 19:38:40 +00:00
|
|
|
# Do not prepare $sorting because it is a true statement
|
|
|
|
# Preparing (escaping) the sorting would destroy it
|
2015-05-14 15:20:33 +00:00
|
|
|
# $sorting is save and can't contain user-input
|
2016-01-24 21:14:20 +00:00
|
|
|
$query = Database::prepare(Database::get(), "UPDATE ? SET value = '$sorting' WHERE `key` = 'sortingPhotos'", array(LYCHEE_TABLE_SETTINGS));
|
|
|
|
$result = Database::get()->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
2016-01-24 21:14:20 +00:00
|
|
|
Log::error(__METHOD__, __LINE__, Database::get()->error);
|
2014-05-30 14:55:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function setSortingAlbums($type, $order) {
|
2015-05-14 15:20:33 +00:00
|
|
|
|
|
|
|
# Check dependencies
|
2016-01-24 21:14:20 +00:00
|
|
|
self::dependencies(isset($type, $order));
|
2015-05-14 15:20:33 +00:00
|
|
|
|
|
|
|
$sorting = 'ORDER BY ';
|
|
|
|
|
|
|
|
# Set row
|
|
|
|
switch ($type) {
|
|
|
|
|
|
|
|
case 'id': $sorting .= 'id';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'title': $sorting .= 'title';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'description': $sorting .= 'description';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'public': $sorting .= 'public';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: exit('Error: Unknown type for sorting!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$sorting .= ' ';
|
|
|
|
|
|
|
|
# Set order
|
|
|
|
switch ($order) {
|
|
|
|
|
|
|
|
case 'ASC': $sorting .= 'ASC';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'DESC': $sorting .= 'DESC';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: exit('Error: Unknown order for sorting!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Execute query
|
|
|
|
# Do not prepare $sorting because it is a true statement
|
|
|
|
# Preparing (escaping) the sorting would destroy it
|
|
|
|
# $sorting is save and can't contain user-input
|
2016-01-24 21:14:20 +00:00
|
|
|
$query = Database::prepare(Database::get(), "UPDATE ? SET value = '$sorting' WHERE `key` = 'sortingAlbums'", array(LYCHEE_TABLE_SETTINGS));
|
|
|
|
$result = Database::get()->query($query);
|
2015-05-14 15:20:33 +00:00
|
|
|
|
|
|
|
if (!$result) {
|
2016-01-24 21:14:20 +00:00
|
|
|
Log::error(__METHOD__, __LINE__, Database::get()->error);
|
2015-05-14 15:20:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|