Session.php 2.6 KB

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