Settings.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. if (!isset($this->database)) return false;
  17. # Execute query
  18. $settings = $this->database->query('SELECT * FROM lychee_settings;');
  19. # Add each to return
  20. while ($setting = $settings->fetch_object()) $return[$setting->key] = $setting->value;
  21. # Fallback for versions below v2.5
  22. if (!isset($return['plugins'])) $return['plugins'] = '';
  23. return $return;
  24. }
  25. public function setLogin($oldPassword = '', $username, $password) {
  26. if (!isset($this->database)) return false;
  27. # Load settings
  28. $settings = $this->get();
  29. if ($oldPassword==$settings['password']) {
  30. # Save username
  31. if (!$this->setUsername($username)) exit('Error: Updating username failed!');
  32. # Save password
  33. if (!$this->setPassword($password)) exit('Error: Updating password failed!');
  34. return true;
  35. }
  36. exit('Error: Current password entered incorrectly!');
  37. }
  38. private function setUsername($username) {
  39. if (!isset($this->database)) return false;
  40. # Parse
  41. $username = htmlentities($username);
  42. if (strlen($username)>50) return false;
  43. # Execute query
  44. $result = $this->database->query("UPDATE lychee_settings SET value = '$username' WHERE `key` = 'username';");
  45. if (!$result) return false;
  46. return true;
  47. }
  48. private function setPassword($password) {
  49. if (!isset($this->database)) return false;
  50. if (strlen($password)<1||strlen($password)>50) return false;
  51. # Execute query
  52. $result = $this->database->query("UPDATE lychee_settings SET value = '$password' WHERE `key` = 'password';");
  53. if (!$result) return false;
  54. return true;
  55. }
  56. public function setDropboxKey($key) {
  57. if (!isset($this->database, $key)) return false;
  58. if (strlen($key)<1||strlen($key)>50) return false;
  59. # Execute query
  60. $result = $this->database->query("UPDATE lychee_settings SET value = '$key' WHERE `key` = 'dropboxKey';");
  61. if (!$result) return false;
  62. return true;
  63. }
  64. public function setSorting($type, $order) {
  65. if (!isset($this->database, $type, $order)) return false;
  66. $sorting = 'ORDER BY ';
  67. # Set row
  68. switch ($type) {
  69. case 'id': $sorting .= 'id';
  70. break;
  71. case 'title': $sorting .= 'title';
  72. break;
  73. case 'description': $sorting .= 'description';
  74. break;
  75. case 'public': $sorting .= 'public';
  76. break;
  77. case 'type': $sorting .= 'type';
  78. break;
  79. case 'star': $sorting .= 'star';
  80. break;
  81. case 'take': $sorting .= 'UNIX_TIMESTAMP(STR_TO_DATE(CONCAT(takedate,"-",taketime),"%d.%m.%Y-%H:%i:%S"))';
  82. break;
  83. default: exit('Error: Unknown type for sorting!');
  84. }
  85. $sorting .= ' ';
  86. # Set order
  87. switch ($order) {
  88. case 'ASC': $sorting .= 'ASC';
  89. break;
  90. case 'DESC': $sorting .= 'DESC';
  91. break;
  92. default: exit('Error: Unknown order for sorting!');
  93. }
  94. # Execute query
  95. $result = $this->database->query("UPDATE lychee_settings SET value = '$sorting' WHERE `key` = 'sorting';");
  96. if (!$result) return false;
  97. return true;
  98. }
  99. }
  100. ?>