Session.php 2.8 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 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. # Path to Lychee for the server-import dialog
  32. $return['config']['location'] = LYCHEE;
  33. # No login
  34. if ($this->settings['username']===''&&$this->settings['password']==='') $return['config']['login'] = false;
  35. else $return['config']['login'] = true;
  36. if ($public===false) {
  37. # Logged in
  38. $return['loggedIn'] = true;
  39. } else {
  40. # Unset unused vars
  41. unset($return['config']['username']);
  42. unset($return['config']['thumbQuality']);
  43. unset($return['config']['sorting']);
  44. unset($return['config']['dropboxKey']);
  45. unset($return['config']['login']);
  46. unset($return['config']['location']);
  47. unset($return['config']['plugins']);
  48. # Logged out
  49. $return['loggedIn'] = false;
  50. }
  51. # Call plugins
  52. $this->plugins(__METHOD__, 1, func_get_args());
  53. return $return;
  54. }
  55. public function login($username, $password) {
  56. # Check dependencies
  57. self::dependencies(isset($this->settings, $username, $password));
  58. # Call plugins
  59. $this->plugins(__METHOD__, 0, func_get_args());
  60. # Check login with MD5 hash
  61. if ($username===$this->settings['username']&&$password===$this->settings['password']) {
  62. $_SESSION['login'] = true;
  63. return true;
  64. }
  65. # Check login with crypted hash
  66. if ($username===$this->settings['username']&&$this->settings['password']===crypt($password, $this->settings['password'])) {
  67. $_SESSION['login'] = true;
  68. return true;
  69. }
  70. # No login
  71. if ($this->settings['username']===''&&$this->settings['password']==='') {
  72. $_SESSION['login'] = true;
  73. return true;
  74. }
  75. # Call plugins
  76. $this->plugins(__METHOD__, 1, func_get_args());
  77. return false;
  78. }
  79. public function logout() {
  80. # Call plugins
  81. $this->plugins(__METHOD__, 0, func_get_args());
  82. session_destroy();
  83. # Call plugins
  84. $this->plugins(__METHOD__, 1, func_get_args());
  85. return true;
  86. }
  87. }
  88. ?>