Settings.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. $settings = $this->database->query('SELECT * FROM lychee_settings;');
  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. $result = $this->database->query("UPDATE lychee_settings SET value = '$username' WHERE `key` = 'username';");
  51. if (!$result) {
  52. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  53. return false;
  54. }
  55. return true;
  56. }
  57. private function setPassword($password) {
  58. # Check dependencies
  59. self::dependencies(isset($this->database));
  60. $password = get_hashed_password($password);
  61. # Execute query
  62. $result = $this->database->query("UPDATE lychee_settings SET value = '$password' WHERE `key` = 'password';");
  63. if (!$result) {
  64. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  65. return false;
  66. }
  67. return true;
  68. }
  69. public function setDropboxKey($key) {
  70. # Check dependencies
  71. self::dependencies(isset($this->database, $key));
  72. if (strlen($key)<1||strlen($key)>50) {
  73. Log::notice($this->database, __METHOD__, __LINE__, 'Dropbox key is either too short or too long');
  74. return false;
  75. }
  76. # Execute query
  77. $result = $this->database->query("UPDATE lychee_settings SET value = '$key' WHERE `key` = 'dropboxKey';");
  78. if (!$result) {
  79. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  80. return false;
  81. }
  82. return true;
  83. }
  84. public function setSorting($type, $order) {
  85. # Check dependencies
  86. self::dependencies(isset($this->database, $type, $order));
  87. $sorting = 'ORDER BY ';
  88. # Set row
  89. switch ($type) {
  90. case 'id': $sorting .= 'id';
  91. break;
  92. case 'title': $sorting .= 'title';
  93. break;
  94. case 'description': $sorting .= 'description';
  95. break;
  96. case 'public': $sorting .= 'public';
  97. break;
  98. case 'type': $sorting .= 'type';
  99. break;
  100. case 'star': $sorting .= 'star';
  101. break;
  102. case 'takestamp': $sorting .= 'takestamp';
  103. break;
  104. default: exit('Error: Unknown type for sorting!');
  105. }
  106. $sorting .= ' ';
  107. # Set order
  108. switch ($order) {
  109. case 'ASC': $sorting .= 'ASC';
  110. break;
  111. case 'DESC': $sorting .= 'DESC';
  112. break;
  113. default: exit('Error: Unknown order for sorting!');
  114. }
  115. # Execute query
  116. $result = $this->database->query("UPDATE lychee_settings SET value = '$sorting' WHERE `key` = 'sorting';");
  117. if (!$result) {
  118. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  119. return false;
  120. }
  121. return true;
  122. }
  123. }
  124. ?>