Settings.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $this->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. $this->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. $this->dependencies(isset($this->database));
  43. # Parse
  44. $username = htmlentities($username);
  45. if (strlen($username)>50) return false;
  46. # Execute query
  47. $result = $this->database->query("UPDATE lychee_settings SET value = '$username' WHERE `key` = 'username';");
  48. if (!$result) return false;
  49. return true;
  50. }
  51. private function setPassword($password) {
  52. # Check dependencies
  53. $this->dependencies(isset($this->database));
  54. $password = get_hashed_password($password);
  55. # Execute query
  56. $result = $this->database->query("UPDATE lychee_settings SET value = '$password' WHERE `key` = 'password';");
  57. if (!$result) return false;
  58. return true;
  59. }
  60. public function setDropboxKey($key) {
  61. # Check dependencies
  62. $this->dependencies(isset($this->database, $key));
  63. if (strlen($key)<1||strlen($key)>50) return false;
  64. # Execute query
  65. $result = $this->database->query("UPDATE lychee_settings SET value = '$key' WHERE `key` = 'dropboxKey';");
  66. if (!$result) return false;
  67. return true;
  68. }
  69. public function setSorting($type, $order) {
  70. # Check dependencies
  71. $this->dependencies(isset($this->database, $type, $order));
  72. $sorting = 'ORDER BY ';
  73. # Set row
  74. switch ($type) {
  75. case 'id': $sorting .= 'id';
  76. break;
  77. case 'title': $sorting .= 'title';
  78. break;
  79. case 'description': $sorting .= 'description';
  80. break;
  81. case 'public': $sorting .= 'public';
  82. break;
  83. case 'type': $sorting .= 'type';
  84. break;
  85. case 'star': $sorting .= 'star';
  86. break;
  87. case 'take': $sorting .= 'UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(takedate,"-",taketime),"%d.%m.%Y-%H:%i:%S"))';
  88. break;
  89. default: exit('Error: Unknown type for sorting!');
  90. }
  91. $sorting .= ' ';
  92. # Set order
  93. switch ($order) {
  94. case 'ASC': $sorting .= 'ASC';
  95. break;
  96. case 'DESC': $sorting .= 'DESC';
  97. break;
  98. default: exit('Error: Unknown order for sorting!');
  99. }
  100. # Execute query
  101. $result = $this->database->query("UPDATE lychee_settings SET value = '$sorting' WHERE `key` = 'sorting';");
  102. if (!$result) return false;
  103. return true;
  104. }
  105. }
  106. ?>