Session.php 2.4 KB

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