Settings.php 4.5 KB

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