Settings.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Settings extends Module {
  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::get()->query($query);
  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::get()->query($query);
  27. if (!$result) return false;
  28. return true;
  29. }
  30. public static function setLogin($oldPassword = '', $username, $password) {
  31. # Check dependencies
  32. self::dependencies(isset($oldPassword, $username, $password));
  33. if ($oldPassword===self::get()['password']||self::get()['password']===crypt($oldPassword, self::get()['password'])) {
  34. # Save username
  35. if (self::setUsername($username)!==true) exit('Error: Updating username failed!');
  36. # Save password
  37. if (self::setPassword($password)!==true) exit('Error: Updating password failed!');
  38. return true;
  39. }
  40. exit('Error: Current password entered incorrectly!');
  41. }
  42. private static function setUsername($username) {
  43. # Check dependencies
  44. self::dependencies(isset($username));
  45. # Hash username
  46. $username = getHashedString($username);
  47. # Execute query
  48. # Do not prepare $username because it is hashed and save
  49. # Preparing (escaping) the username would destroy the hash
  50. if (self::set('username', $username, true)===false) {
  51. Log::error(__METHOD__, __LINE__, Database::get()->error);
  52. return false;
  53. }
  54. return true;
  55. }
  56. private static function setPassword($password) {
  57. # Check dependencies
  58. self::dependencies(isset($password));
  59. # Hash password
  60. $password = getHashedString($password);
  61. # Do not prepare $password because it is hashed and save
  62. # Preparing (escaping) the password would destroy the hash
  63. if (self::set('password', $password, true)===false) {
  64. Log::error(__METHOD__, __LINE__, Database::get()->error);
  65. return false;
  66. }
  67. return true;
  68. }
  69. public static function setDropboxKey($dropboxKey) {
  70. # Check dependencies
  71. self::dependencies(isset($dropboxKey));
  72. if (strlen($dropboxKey)<1||strlen($dropboxKey)>50) {
  73. Log::notice(__METHOD__, __LINE__, 'Dropbox key is either too short or too long');
  74. return false;
  75. }
  76. if (self::set('dropboxKey', $dropboxKey)===false) {
  77. Log::error(__METHOD__, __LINE__, Database::get()->error);
  78. return false;
  79. }
  80. return true;
  81. }
  82. public static function setSortingPhotos($type, $order) {
  83. # Check dependencies
  84. self::dependencies(isset($type, $order));
  85. $sorting = 'ORDER BY ';
  86. # Set row
  87. switch ($type) {
  88. case 'id': $sorting .= 'id';
  89. break;
  90. case 'title': $sorting .= 'title';
  91. break;
  92. case 'description': $sorting .= 'description';
  93. break;
  94. case 'public': $sorting .= 'public';
  95. break;
  96. case 'type': $sorting .= 'type';
  97. break;
  98. case 'star': $sorting .= 'star';
  99. break;
  100. case 'takestamp': $sorting .= 'takestamp';
  101. break;
  102. default: exit('Error: Unknown type for sorting!');
  103. }
  104. $sorting .= ' ';
  105. # Set order
  106. switch ($order) {
  107. case 'ASC': $sorting .= 'ASC';
  108. break;
  109. case 'DESC': $sorting .= 'DESC';
  110. break;
  111. default: exit('Error: Unknown order for sorting!');
  112. }
  113. # Do not prepare $sorting because it is a true statement
  114. # Preparing (escaping) the sorting would destroy it
  115. # $sorting is save and can't contain user-input
  116. if (self::set('sortingPhotos', $sorting, true)===false) {
  117. Log::error(__METHOD__, __LINE__, Database::get()->error);
  118. return false;
  119. }
  120. return true;
  121. }
  122. public static function setSortingAlbums($type, $order) {
  123. # Check dependencies
  124. self::dependencies(isset($type, $order));
  125. $sorting = 'ORDER BY ';
  126. # Set row
  127. switch ($type) {
  128. case 'id': $sorting .= 'id';
  129. break;
  130. case 'title': $sorting .= 'title';
  131. break;
  132. case 'description': $sorting .= 'description';
  133. break;
  134. case 'public': $sorting .= 'public';
  135. break;
  136. default: exit('Error: Unknown type for sorting!');
  137. }
  138. $sorting .= ' ';
  139. # Set order
  140. switch ($order) {
  141. case 'ASC': $sorting .= 'ASC';
  142. break;
  143. case 'DESC': $sorting .= 'DESC';
  144. break;
  145. default: exit('Error: Unknown order for sorting!');
  146. }
  147. # Do not prepare $sorting because it is a true statement
  148. # Preparing (escaping) the sorting would destroy it
  149. # $sorting is save and can't contain user-input
  150. if (self::set('sortingAlbums', $sorting, true)===false) {
  151. Log::error(__METHOD__, __LINE__, Database::get()->error);
  152. return false;
  153. }
  154. return true;
  155. }
  156. }
  157. ?>