Session.php 2.2 KB

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