Settings.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. ###
  3. # @name Settings Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Settings extends Module {
  9. private $database = null;
  10. public function __construct($database) {
  11. # Init vars
  12. $this->database = $database;
  13. return true;
  14. }
  15. public function get() {
  16. # Check dependencies
  17. self::dependencies(isset($this->database));
  18. # Execute query
  19. $query = Database::prepare($this->database, "SELECT * FROM ?", [LYCHEE_TABLE_SETTINGS]);
  20. $settings = $this->database->query($query);
  21. # Add each to return
  22. while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
  23. # Fallback for versions below v2.5
  24. if (!isset($return['plugins'])) $return['plugins'] = '';
  25. return $return;
  26. }
  27. public function setLogin($oldPassword = '', $username, $password) {
  28. # Check dependencies
  29. self::dependencies(isset($this->database));
  30. # Load settings
  31. $settings = $this->get();
  32. if ($oldPassword===$settings['password']||$settings['password']===crypt($oldPassword, $settings['password'])) {
  33. # Save username
  34. if (!$this->setUsername($username)) exit('Error: Updating username failed!');
  35. # Save password
  36. if (!$this->setPassword($password)) exit('Error: Updating password failed!');
  37. return true;
  38. }
  39. exit('Error: Current password entered incorrectly!');
  40. }
  41. private function setUsername($username) {
  42. # Check dependencies
  43. self::dependencies(isset($this->database));
  44. # Parse
  45. $username = htmlentities($username);
  46. if (strlen($username)>50) {
  47. Log::notice($this->database, __METHOD__, __LINE__, 'Username is longer than 50 chars');
  48. return false;
  49. }
  50. # Execute query
  51. $query = Database::prepare($this->database, "UPDATE ? SET value = '?' WHERE `key` = 'username'", [LYCHEE_TABLE_SETTINGS, $username]);
  52. $result = $this->database->query($query);
  53. if (!$result) {
  54. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  55. return false;
  56. }
  57. return true;
  58. }
  59. private function setPassword($password) {
  60. # Check dependencies
  61. self::dependencies(isset($this->database));
  62. $password = get_hashed_password($password);
  63. # Execute query
  64. # Do not prepare $password because it is hashed and save
  65. # Preparing (escaping) the password would destroy the hash
  66. $query = Database::prepare($this->database, "UPDATE ? SET value = '$password' WHERE `key` = 'password'", [LYCHEE_TABLE_SETTINGS]);
  67. $result = $this->database->query($query);
  68. if (!$result) {
  69. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  70. return false;
  71. }
  72. return true;
  73. }
  74. public function setDropboxKey($key) {
  75. # Check dependencies
  76. self::dependencies(isset($this->database, $key));
  77. if (strlen($key)<1||strlen($key)>50) {
  78. Log::notice($this->database, __METHOD__, __LINE__, 'Dropbox key is either too short or too long');
  79. return false;
  80. }
  81. # Execute query
  82. $query = Database::prepare($this->database, "UPDATE ? SET value = '?' WHERE `key` = 'dropboxKey'", [LYCHEE_TABLE_SETTINGS, $key]);
  83. $result = $this->database->query($query);
  84. if (!$result) {
  85. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  86. return false;
  87. }
  88. return true;
  89. }
  90. public function setSorting($type, $order) {
  91. # Check dependencies
  92. self::dependencies(isset($this->database, $type, $order));
  93. $sorting = 'ORDER BY ';
  94. # Set row
  95. switch ($type) {
  96. case 'id': $sorting .= 'id';
  97. break;
  98. case 'title': $sorting .= 'title';
  99. break;
  100. case 'description': $sorting .= 'description';
  101. break;
  102. case 'public': $sorting .= 'public';
  103. break;
  104. case 'type': $sorting .= 'type';
  105. break;
  106. case 'star': $sorting .= 'star';
  107. break;
  108. case 'takestamp': $sorting .= 'takestamp';
  109. break;
  110. default: exit('Error: Unknown type for sorting!');
  111. }
  112. $sorting .= ' ';
  113. # Set order
  114. switch ($order) {
  115. case 'ASC': $sorting .= 'ASC';
  116. break;
  117. case 'DESC': $sorting .= 'DESC';
  118. break;
  119. default: exit('Error: Unknown order for sorting!');
  120. }
  121. # Execute query
  122. # Do not prepare $sorting because it is a true statement
  123. # Preparing (escaping) the sorting would destroy it
  124. $query = Database::prepare($this->database, "UPDATE ? SET value = '$sorting' WHERE `key` = 'sorting'", [LYCHEE_TABLE_SETTINGS]);
  125. $result = $this->database->query($query);
  126. if (!$result) {
  127. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  128. return false;
  129. }
  130. return true;
  131. }
  132. }
  133. ?>