2014-04-04 21:17:54 +00:00
|
|
|
<?php
|
|
|
|
|
2016-01-26 14:31:53 +00:00
|
|
|
namespace Lychee\Modules;
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:33:31 +00:00
|
|
|
final class Settings {
|
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-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* @return array Returns the settings of Lychee.
|
|
|
|
*/
|
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
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Execute query
|
|
|
|
$query = Database::prepare(Database::get(), "SELECT * FROM ?", array(LYCHEE_TABLE_SETTINGS));
|
2016-01-31 17:49:31 +00:00
|
|
|
$settings = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Add each to return
|
2014-04-04 21:17:54 +00:00
|
|
|
while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Convert plugins to array
|
2016-01-24 21:14:20 +00:00
|
|
|
$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-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* @return boolean Returns true when successful.
|
|
|
|
*/
|
2016-01-26 14:31:53 +00:00
|
|
|
private static function set($key, $value, $row = false) {
|
|
|
|
|
|
|
|
if ($row===false) {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
$query = Database::prepare(Database::get(), "UPDATE ? SET value = '?' WHERE `key` = '?'", array(LYCHEE_TABLE_SETTINGS, $value, $key));
|
2016-01-26 14:31:53 +00:00
|
|
|
|
|
|
|
} elseif ($row===true) {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Do not prepare $value because it has already been escaped or is a true statement
|
|
|
|
$query = Database::prepare(Database::get(), "UPDATE ? SET value = '$value' WHERE `key` = '?'", array(LYCHEE_TABLE_SETTINGS, $key));
|
2016-01-26 14:31:53 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-31 17:49:31 +00:00
|
|
|
$result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
|
2016-01-26 14:31:53 +00:00
|
|
|
|
2016-01-31 14:44:54 +00:00
|
|
|
if ($result===false) return false;
|
2016-01-26 14:31:53 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* Sets the username and password when current password is correct.
|
|
|
|
* Exits on error.
|
|
|
|
* @return true Returns true when successful.
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function setLogin($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
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Save username
|
2016-02-13 22:33:39 +00:00
|
|
|
if (self::setUsername($username)===false) Response::error('Updating username failed!');
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Save password
|
2016-02-13 22:33:39 +00:00
|
|
|
if (self::setPassword($password)===false) Response::error('Updating password failed!');
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-06 23:16:48 +00:00
|
|
|
Response::error('Current password entered incorrectly!');
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* Sets a new username.
|
|
|
|
* @return boolean Returns true when successful.
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
private static function setUsername($username) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Check dependencies
|
2016-01-30 20:33:31 +00:00
|
|
|
Validator::required(isset($username), __METHOD__);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Hash username
|
2015-02-08 14:36:13 +00:00
|
|
|
$username = getHashedString($username);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Execute query
|
|
|
|
// Do not prepare $username because it is hashed and save
|
|
|
|
// Preparing (escaping) the username would destroy the hash
|
2016-01-31 17:49:31 +00:00
|
|
|
if (self::set('username', $username, true)===false) return false;
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* Sets a new username.
|
|
|
|
* @return boolean Returns true when successful.
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
private static function setPassword($password) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Check dependencies
|
2016-01-30 20:33:31 +00:00
|
|
|
Validator::required(isset($password), __METHOD__);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Hash password
|
2015-02-08 14:36:13 +00:00
|
|
|
$password = getHashedString($password);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Do not prepare $password because it is hashed and save
|
|
|
|
// Preparing (escaping) the password would destroy the hash
|
2016-01-31 17:49:31 +00:00
|
|
|
if (self::set('password', $password, true)===false) return false;
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* Sets a new dropboxKey.
|
|
|
|
* @return boolean Returns true when successful.
|
|
|
|
*/
|
2016-01-26 14:31:53 +00:00
|
|
|
public static function setDropboxKey($dropboxKey) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2016-01-26 14:31:53 +00:00
|
|
|
if (strlen($dropboxKey)<1||strlen($dropboxKey)>50) {
|
2016-01-31 17:49:31 +00:00
|
|
|
Log::notice(Database::get(), __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
|
|
|
|
2016-01-31 17:49:31 +00:00
|
|
|
if (self::set('dropboxKey', $dropboxKey)===false) return false;
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* Sets a new sorting for the photos.
|
|
|
|
* @return boolean Returns true when successful.
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function setSortingPhotos($type, $order) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
$sorting = 'ORDER BY ';
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Set row
|
2014-04-04 21:17:54 +00:00
|
|
|
switch ($type) {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
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;
|
|
|
|
case 'takestamp': $sorting .= 'takestamp'; break;
|
2016-02-13 16:32:44 +00:00
|
|
|
default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown type for sorting.');
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$sorting .= ' ';
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Set order
|
2014-04-04 21:17:54 +00:00
|
|
|
switch ($order) {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
case 'ASC': $sorting .= 'ASC'; break;
|
|
|
|
case 'DESC': $sorting .= 'DESC'; break;
|
2016-02-13 16:32:44 +00:00
|
|
|
default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown order for sorting.');
|
|
|
|
return false;
|
|
|
|
break;
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// 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-31 17:49:31 +00:00
|
|
|
if (self::set('sortingPhotos', $sorting, true)===false) return false;
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-13 16:32:44 +00:00
|
|
|
/**
|
|
|
|
* Sets a new sorting for the albums.
|
|
|
|
* @return boolean Returns true when successful.
|
|
|
|
*/
|
2016-01-24 21:14:20 +00:00
|
|
|
public static function setSortingAlbums($type, $order) {
|
2015-05-14 15:20:33 +00:00
|
|
|
|
|
|
|
$sorting = 'ORDER BY ';
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Set row
|
2015-05-14 15:20:33 +00:00
|
|
|
switch ($type) {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
case 'id': $sorting .= 'id'; break;
|
|
|
|
case 'title': $sorting .= 'title'; break;
|
|
|
|
case 'description': $sorting .= 'description'; break;
|
|
|
|
case 'public': $sorting .= 'public'; break;
|
2016-02-13 16:32:44 +00:00
|
|
|
default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown type for sorting.');
|
|
|
|
return false;
|
|
|
|
break;
|
2015-05-14 15:20:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$sorting .= ' ';
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// Set order
|
2015-05-14 15:20:33 +00:00
|
|
|
switch ($order) {
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
case 'ASC': $sorting .= 'ASC'; break;
|
|
|
|
case 'DESC': $sorting .= 'DESC'; break;
|
2016-02-13 16:32:44 +00:00
|
|
|
default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown order for sorting.');
|
|
|
|
return false;
|
|
|
|
break;
|
2015-05-14 15:20:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:43:57 +00:00
|
|
|
// 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-31 17:49:31 +00:00
|
|
|
if (self::set('sortingAlbums', $sorting, true)===false) return false;
|
2015-05-14 15:20:33 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-04 21:17:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|