Settings.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Settings {
  4. private static $cache = null;
  5. public static function get() {
  6. if (self::$cache) return self::$cache;
  7. // Execute query
  8. $query = Database::prepare(Database::get(), "SELECT * FROM ?", array(LYCHEE_TABLE_SETTINGS));
  9. $settings = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  10. // Add each to return
  11. while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
  12. // Convert plugins to array
  13. $return['plugins'] = explode(';', $return['plugins']);
  14. self::$cache = $return;
  15. return $return;
  16. }
  17. private static function set($key, $value, $row = false) {
  18. if ($row===false) {
  19. $query = Database::prepare(Database::get(), "UPDATE ? SET value = '?' WHERE `key` = '?'", array(LYCHEE_TABLE_SETTINGS, $value, $key));
  20. } elseif ($row===true) {
  21. // Do not prepare $value because it has already been escaped or is a true statement
  22. $query = Database::prepare(Database::get(), "UPDATE ? SET value = '$value' WHERE `key` = '?'", array(LYCHEE_TABLE_SETTINGS, $key));
  23. } else {
  24. return false;
  25. }
  26. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  27. if ($result===false) return false;
  28. return true;
  29. }
  30. public static function setLogin($oldPassword = '', $username, $password) {
  31. if ($oldPassword===self::get()['password']||self::get()['password']===crypt($oldPassword, self::get()['password'])) {
  32. // Save username
  33. if (self::setUsername($username)!==true) Response::error('Updating username failed!');
  34. // Save password
  35. if (self::setPassword($password)!==true) Response::error('Updating password failed!');
  36. return true;
  37. }
  38. Response::error('Current password entered incorrectly!');
  39. }
  40. private static function setUsername($username) {
  41. // Check dependencies
  42. Validator::required(isset($username), __METHOD__);
  43. // Hash username
  44. $username = getHashedString($username);
  45. // Execute query
  46. // Do not prepare $username because it is hashed and save
  47. // Preparing (escaping) the username would destroy the hash
  48. if (self::set('username', $username, true)===false) return false;
  49. return true;
  50. }
  51. private static function setPassword($password) {
  52. // Check dependencies
  53. Validator::required(isset($password), __METHOD__);
  54. // Hash password
  55. $password = getHashedString($password);
  56. // Do not prepare $password because it is hashed and save
  57. // Preparing (escaping) the password would destroy the hash
  58. if (self::set('password', $password, true)===false) return false;
  59. return true;
  60. }
  61. public static function setDropboxKey($dropboxKey) {
  62. if (strlen($dropboxKey)<1||strlen($dropboxKey)>50) {
  63. Log::notice(Database::get(), __METHOD__, __LINE__, 'Dropbox key is either too short or too long');
  64. return false;
  65. }
  66. if (self::set('dropboxKey', $dropboxKey)===false) return false;
  67. return true;
  68. }
  69. public static function setSortingPhotos($type, $order) {
  70. $sorting = 'ORDER BY ';
  71. // Set row
  72. switch ($type) {
  73. case 'id': $sorting .= 'id'; break;
  74. case 'title': $sorting .= 'title'; break;
  75. case 'description': $sorting .= 'description'; break;
  76. case 'public': $sorting .= 'public'; break;
  77. case 'type': $sorting .= 'type'; break;
  78. case 'star': $sorting .= 'star'; break;
  79. case 'takestamp': $sorting .= 'takestamp'; break;
  80. default: Response::error('Unknown type for sorting!');
  81. }
  82. $sorting .= ' ';
  83. // Set order
  84. switch ($order) {
  85. case 'ASC': $sorting .= 'ASC'; break;
  86. case 'DESC': $sorting .= 'DESC'; break;
  87. default: Response::error('Unknown order for sorting!');
  88. }
  89. // Do not prepare $sorting because it is a true statement
  90. // Preparing (escaping) the sorting would destroy it
  91. // $sorting is save and can't contain user-input
  92. if (self::set('sortingPhotos', $sorting, true)===false) return false;
  93. return true;
  94. }
  95. public static function setSortingAlbums($type, $order) {
  96. $sorting = 'ORDER BY ';
  97. // Set row
  98. switch ($type) {
  99. case 'id': $sorting .= 'id'; break;
  100. case 'title': $sorting .= 'title'; break;
  101. case 'description': $sorting .= 'description'; break;
  102. case 'public': $sorting .= 'public'; break;
  103. default: Response::error('Unknown type for sorting!');
  104. }
  105. $sorting .= ' ';
  106. // Set order
  107. switch ($order) {
  108. case 'ASC': $sorting .= 'ASC'; break;
  109. case 'DESC': $sorting .= 'DESC'; break;
  110. default: Response::error('Unknown order for sorting!');
  111. }
  112. // Do not prepare $sorting because it is a true statement
  113. // Preparing (escaping) the sorting would destroy it
  114. // $sorting is save and can't contain user-input
  115. if (self::set('sortingAlbums', $sorting, true)===false) return false;
  116. return true;
  117. }
  118. }
  119. ?>