Settings.php 5.5 KB

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