Settings.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Settings {
  4. private static $cache = null;
  5. /**
  6. * @return array Returns the settings of Lychee.
  7. */
  8. public static function get() {
  9. if (self::$cache) return self::$cache;
  10. // Execute query
  11. $query = Database::prepare(Database::get(), "SELECT * FROM ?", array(LYCHEE_TABLE_SETTINGS));
  12. $settings = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  13. // Add each to return
  14. while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
  15. // Convert plugins to array
  16. $return['plugins'] = explode(';', $return['plugins']);
  17. self::$cache = $return;
  18. return $return;
  19. }
  20. /**
  21. * @return boolean Returns true when successful.
  22. */
  23. private static function set($key, $value, $row = false) {
  24. if ($row===false) {
  25. $query = Database::prepare(Database::get(), "UPDATE ? SET value = '?' WHERE `key` = '?'", array(LYCHEE_TABLE_SETTINGS, $value, $key));
  26. } elseif ($row===true) {
  27. // Do not prepare $value because it has already been escaped or is a true statement
  28. $query = Database::prepare(Database::get(), "UPDATE ? SET value = '$value' WHERE `key` = '?'", array(LYCHEE_TABLE_SETTINGS, $key));
  29. } else {
  30. return false;
  31. }
  32. $result = Database::execute(Database::get(), $query, __METHOD__, __LINE__);
  33. if ($result===false) return false;
  34. return true;
  35. }
  36. /**
  37. * Sets the username and password when current password is correct.
  38. * Exits on error.
  39. * @return true Returns true when successful.
  40. */
  41. public static function setLogin($oldPassword = '', $username, $password) {
  42. if ($oldPassword===self::get()['password']||self::get()['password']===crypt($oldPassword, self::get()['password'])) {
  43. // Save username
  44. if (self::setUsername($username)===false) Response::error('Updating username failed!');
  45. // Save password
  46. if (self::setPassword($password)===false) Response::error('Updating password failed!');
  47. return true;
  48. }
  49. Response::error('Current password entered incorrectly!');
  50. }
  51. /**
  52. * Sets a new username.
  53. * @return boolean Returns true when successful.
  54. */
  55. private static function setUsername($username) {
  56. // Check dependencies
  57. Validator::required(isset($username), __METHOD__);
  58. // Hash username
  59. $username = getHashedString($username);
  60. // Execute query
  61. // Do not prepare $username because it is hashed and save
  62. // Preparing (escaping) the username would destroy the hash
  63. if (self::set('username', $username, true)===false) return false;
  64. return true;
  65. }
  66. /**
  67. * Sets a new username.
  68. * @return boolean Returns true when successful.
  69. */
  70. private static function setPassword($password) {
  71. // Check dependencies
  72. Validator::required(isset($password), __METHOD__);
  73. // Hash password
  74. $password = getHashedString($password);
  75. // Do not prepare $password because it is hashed and save
  76. // Preparing (escaping) the password would destroy the hash
  77. if (self::set('password', $password, true)===false) return false;
  78. return true;
  79. }
  80. /**
  81. * Sets a new dropboxKey.
  82. * @return boolean Returns true when successful.
  83. */
  84. public static function setDropboxKey($dropboxKey) {
  85. if (strlen($dropboxKey)<1||strlen($dropboxKey)>50) {
  86. Log::notice(Database::get(), __METHOD__, __LINE__, 'Dropbox key is either too short or too long');
  87. return false;
  88. }
  89. if (self::set('dropboxKey', $dropboxKey)===false) return false;
  90. return true;
  91. }
  92. /**
  93. * Sets a new sorting for the photos.
  94. * @return boolean Returns true when successful.
  95. */
  96. public static function setSortingPhotos($type, $order) {
  97. $sorting = 'ORDER BY ';
  98. // Set row
  99. switch ($type) {
  100. case 'id': $sorting .= 'id'; break;
  101. case 'title': $sorting .= 'title'; break;
  102. case 'description': $sorting .= 'description'; break;
  103. case 'public': $sorting .= 'public'; break;
  104. case 'type': $sorting .= 'type'; break;
  105. case 'star': $sorting .= 'star'; break;
  106. case 'takestamp': $sorting .= 'takestamp'; break;
  107. default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown type for sorting.');
  108. return false;
  109. break;
  110. }
  111. $sorting .= ' ';
  112. // Set order
  113. switch ($order) {
  114. case 'ASC': $sorting .= 'ASC'; break;
  115. case 'DESC': $sorting .= 'DESC'; break;
  116. default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown order for sorting.');
  117. return false;
  118. break;
  119. }
  120. // Do not prepare $sorting because it is a true statement
  121. // Preparing (escaping) the sorting would destroy it
  122. // $sorting is save and can't contain user-input
  123. if (self::set('sortingPhotos', $sorting, true)===false) return false;
  124. return true;
  125. }
  126. /**
  127. * Sets a new sorting for the albums.
  128. * @return boolean Returns true when successful.
  129. */
  130. public static function setSortingAlbums($type, $order) {
  131. $sorting = 'ORDER BY ';
  132. // Set row
  133. switch ($type) {
  134. case 'id': $sorting .= 'id'; break;
  135. case 'title': $sorting .= 'title'; break;
  136. case 'description': $sorting .= 'description'; break;
  137. case 'public': $sorting .= 'public'; break;
  138. default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown type for sorting.');
  139. return false;
  140. break;
  141. }
  142. $sorting .= ' ';
  143. // Set order
  144. switch ($order) {
  145. case 'ASC': $sorting .= 'ASC'; break;
  146. case 'DESC': $sorting .= 'DESC'; break;
  147. default: Log::error(Database::get(), __METHOD__, __LINE__, 'Could not update settings. Unknown order for sorting.');
  148. return false;
  149. break;
  150. }
  151. // Do not prepare $sorting because it is a true statement
  152. // Preparing (escaping) the sorting would destroy it
  153. // $sorting is save and can't contain user-input
  154. if (self::set('sortingAlbums', $sorting, true)===false) return false;
  155. return true;
  156. }
  157. /**
  158. * @return array Returns the Imagick setting.
  159. */
  160. public static function hasImagick() {
  161. return (bool)(extension_loaded('imagick') && self::get()['imagick'] === '1');
  162. }
  163. }
  164. ?>