2014-04-04 21:17:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
###
|
2014-10-21 11:45:11 +00:00
|
|
|
# @name Settings Module
|
2014-04-04 21:17:54 +00:00
|
|
|
# @copyright 2014 by Tobias Reich
|
|
|
|
###
|
|
|
|
|
|
|
|
if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
|
|
|
|
|
|
|
|
class Settings extends Module {
|
|
|
|
|
|
|
|
private $database = null;
|
|
|
|
|
|
|
|
public function __construct($database) {
|
|
|
|
|
|
|
|
# Init vars
|
|
|
|
$this->database = $database;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get() {
|
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2014-06-25 12:50:49 +00:00
|
|
|
self::dependencies(isset($this->database));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Execute query
|
2014-08-30 05:05:26 +00:00
|
|
|
$query = Database::prepare($this->database, "SELECT * FROM ?", array(LYCHEE_TABLE_SETTINGS));
|
2014-08-29 19:38:40 +00:00
|
|
|
$settings = $this->database->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Add each to return
|
|
|
|
while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
|
|
|
|
|
2014-04-11 20:25:03 +00:00
|
|
|
# Fallback for versions below v2.5
|
|
|
|
if (!isset($return['plugins'])) $return['plugins'] = '';
|
|
|
|
|
2014-04-04 21:17:54 +00:00
|
|
|
return $return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLogin($oldPassword = '', $username, $password) {
|
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2014-06-25 12:50:49 +00:00
|
|
|
self::dependencies(isset($this->database));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Load settings
|
|
|
|
$settings = $this->get();
|
|
|
|
|
2014-04-21 00:19:23 +00:00
|
|
|
if ($oldPassword===$settings['password']||$settings['password']===crypt($oldPassword, $settings['password'])) {
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Save username
|
|
|
|
if (!$this->setUsername($username)) exit('Error: Updating username failed!');
|
|
|
|
|
|
|
|
# Save password
|
|
|
|
if (!$this->setPassword($password)) exit('Error: Updating password failed!');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
exit('Error: Current password entered incorrectly!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setUsername($username) {
|
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2014-06-25 12:50:49 +00:00
|
|
|
self::dependencies(isset($this->database));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Parse
|
|
|
|
$username = htmlentities($username);
|
2014-05-30 14:55:30 +00:00
|
|
|
if (strlen($username)>50) {
|
|
|
|
Log::notice($this->database, __METHOD__, __LINE__, 'Username is longer than 50 chars');
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Execute query
|
2014-08-30 05:05:26 +00:00
|
|
|
$query = Database::prepare($this->database, "UPDATE ? SET value = '?' WHERE `key` = 'username'", array(LYCHEE_TABLE_SETTINGS, $username));
|
2014-08-29 19:38:40 +00:00
|
|
|
$result = $this->database->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
|
|
|
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function setPassword($password) {
|
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2014-06-25 12:50:49 +00:00
|
|
|
self::dependencies(isset($this->database));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-04-21 00:19:23 +00:00
|
|
|
$password = get_hashed_password($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
|
2014-08-30 05:05:26 +00:00
|
|
|
$query = Database::prepare($this->database, "UPDATE ? SET value = '$password' WHERE `key` = 'password'", array(LYCHEE_TABLE_SETTINGS));
|
2014-08-29 19:38:40 +00:00
|
|
|
$result = $this->database->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
|
|
|
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setDropboxKey($key) {
|
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2014-06-25 12:50:49 +00:00
|
|
|
self::dependencies(isset($this->database, $key));
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (strlen($key)<1||strlen($key)>50) {
|
|
|
|
Log::notice($this->database, __METHOD__, __LINE__, 'Dropbox key is either too short or too long');
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
|
|
|
|
# Execute query
|
2014-08-30 05:05:26 +00:00
|
|
|
$query = Database::prepare($this->database, "UPDATE ? SET value = '?' WHERE `key` = 'dropboxKey'", array(LYCHEE_TABLE_SETTINGS, $key));
|
2014-08-29 19:38:40 +00:00
|
|
|
$result = $this->database->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
|
|
|
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setSorting($type, $order) {
|
|
|
|
|
2014-04-19 19:07:36 +00:00
|
|
|
# Check dependencies
|
2014-06-25 12:50:49 +00:00
|
|
|
self::dependencies(isset($this->database, $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
|
2014-08-30 05:05:26 +00:00
|
|
|
$query = Database::prepare($this->database, "UPDATE ? SET value = '$sorting' WHERE `key` = 'sorting'", array(LYCHEE_TABLE_SETTINGS));
|
2014-08-29 19:38:40 +00:00
|
|
|
$result = $this->database->query($query);
|
2014-04-04 21:17:54 +00:00
|
|
|
|
2014-05-30 14:55:30 +00:00
|
|
|
if (!$result) {
|
|
|
|
Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
|
|
|
|
return false;
|
|
|
|
}
|
2014-04-04 21:17:54 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|