Session.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. ###
  3. # @name Session 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 Session extends Module {
  9. private $settings = null;
  10. public function __construct($plugins, $settings) {
  11. # Init vars
  12. $this->plugins = $plugins;
  13. $this->settings = $settings;
  14. return true;
  15. }
  16. public function init($public, $version) {
  17. if (!isset($this->settings, $public, $version)) return false;
  18. # Call plugins
  19. $this->plugins(__METHOD__, 0, func_get_args());
  20. global $configVersion;
  21. # Update
  22. if ($configVersion!==$version)
  23. if (!update($version)) exit('Error: Updating the database failed!');
  24. # Return settings
  25. $return['config'] = $this->settings;
  26. unset($return['config']['password']);
  27. # No login
  28. if ($this->settings['username']===''&&$this->settings['password']==='') $return['config']['login'] = false;
  29. else $return['config']['login'] = true;
  30. if ($public===false) {
  31. # Logged in
  32. $return['loggedIn'] = true;
  33. } else {
  34. # Unset unused vars
  35. unset($return['config']['username']);
  36. unset($return['config']['thumbQuality']);
  37. unset($return['config']['sorting']);
  38. unset($return['config']['dropboxKey']);
  39. unset($return['config']['login']);
  40. # Logged out
  41. $return['loggedIn'] = false;
  42. }
  43. # Call plugins
  44. $this->plugins(__METHOD__, 1, func_get_args());
  45. return $return;
  46. }
  47. public function login($username, $password) {
  48. if (!isset($this->settings, $username, $password)) return false;
  49. # Call plugins
  50. $this->plugins(__METHOD__, 0, func_get_args());
  51. # Check login
  52. if ($username===$this->settings['username']&&$password===$this->settings['password']) {
  53. $_SESSION['login'] = true;
  54. return true;
  55. }
  56. # No login
  57. if ($this->settings['username']===''&&$this->settings['password']==='') {
  58. $_SESSION['login'] = true;
  59. return true;
  60. }
  61. # Call plugins
  62. $this->plugins(__METHOD__, 1, func_get_args());
  63. return false;
  64. }
  65. public function logout() {
  66. # Call plugins
  67. $this->plugins(__METHOD__, 0, func_get_args());
  68. session_destroy();
  69. # Call plugins
  70. $this->plugins(__METHOD__, 1, func_get_args());
  71. return true;
  72. }
  73. }
  74. ?>