Session.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. ###
  3. # @name Session Module
  4. # @copyright 2014 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. class Session extends Module {
  8. private $settings = null;
  9. public function __construct($plugins, $settings) {
  10. # Init vars
  11. $this->plugins = $plugins;
  12. $this->settings = $settings;
  13. return true;
  14. }
  15. public function init($database, $dbName, $public, $version) {
  16. # Check dependencies
  17. self::dependencies(isset($this->settings, $public, $version));
  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, $dbName, @$this->settings['version'])) {
  23. Log::error($database, __METHOD__, __LINE__, 'Updating the database failed');
  24. exit('Error: Updating the database failed!');
  25. }
  26. }
  27. # Return settings
  28. $return['config'] = $this->settings;
  29. unset($return['config']['password']);
  30. # Path to Lychee for the server-import dialog
  31. $return['config']['location'] = LYCHEE;
  32. # No login
  33. if ($this->settings['username']===''&&$this->settings['password']==='') $return['config']['login'] = false;
  34. else $return['config']['login'] = true;
  35. if ($public===false) {
  36. # Logged in
  37. $return['loggedIn'] = true;
  38. } else {
  39. # Unset unused vars
  40. unset($return['config']['username']);
  41. unset($return['config']['thumbQuality']);
  42. unset($return['config']['sorting']);
  43. unset($return['config']['dropboxKey']);
  44. unset($return['config']['login']);
  45. unset($return['config']['location']);
  46. unset($return['config']['plugins']);
  47. # Logged out
  48. $return['loggedIn'] = false;
  49. }
  50. # Call plugins
  51. $this->plugins(__METHOD__, 1, func_get_args());
  52. return $return;
  53. }
  54. public function login($username, $password) {
  55. # Check dependencies
  56. self::dependencies(isset($this->settings, $username, $password));
  57. # Call plugins
  58. $this->plugins(__METHOD__, 0, func_get_args());
  59. # Check login with MD5 hash
  60. if ($username===$this->settings['username']&&$password===$this->settings['password']) {
  61. $_SESSION['login'] = true;
  62. return true;
  63. }
  64. # Check login with crypted hash
  65. if ($username===$this->settings['username']&&$this->settings['password']===crypt($password, $this->settings['password'])) {
  66. $_SESSION['login'] = true;
  67. return true;
  68. }
  69. # No login
  70. if ($this->settings['username']===''&&$this->settings['password']==='') {
  71. $_SESSION['login'] = true;
  72. return true;
  73. }
  74. # Call plugins
  75. $this->plugins(__METHOD__, 1, func_get_args());
  76. return false;
  77. }
  78. public function logout() {
  79. # Call plugins
  80. $this->plugins(__METHOD__, 0, func_get_args());
  81. session_destroy();
  82. # Call plugins
  83. $this->plugins(__METHOD__, 1, func_get_args());
  84. return true;
  85. }
  86. }
  87. ?>