Session.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. ###
  3. # @name Session Module
  4. # @copyright 2015 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. # Remove username and password from response
  30. unset($return['config']['username']);
  31. unset($return['config']['password']);
  32. # Remove identifier from response
  33. unset($return['config']['identifier']);
  34. # Path to Lychee for the server-import dialog
  35. $return['config']['location'] = LYCHEE;
  36. # Check if login credentials exist and login if they don't
  37. if ($this->noLogin()===true) {
  38. $public = false;
  39. $return['config']['login'] = false;
  40. } else {
  41. $return['config']['login'] = true;
  42. }
  43. if ($public===false) {
  44. # Logged in
  45. $return['status'] = LYCHEE_STATUS_LOGGEDIN;
  46. } else {
  47. # Logged out
  48. $return['status'] = LYCHEE_STATUS_LOGGEDOUT;
  49. # Unset unused vars
  50. unset($return['config']['thumbQuality']);
  51. unset($return['config']['sortingAlbums']);
  52. unset($return['config']['sortingPhotos']);
  53. unset($return['config']['dropboxKey']);
  54. unset($return['config']['login']);
  55. unset($return['config']['location']);
  56. unset($return['config']['imagick']);
  57. unset($return['config']['medium']);
  58. unset($return['config']['plugins']);
  59. }
  60. # Call plugins
  61. $this->plugins(__METHOD__, 1, func_get_args());
  62. return $return;
  63. }
  64. public function login($username, $password) {
  65. # Check dependencies
  66. self::dependencies(isset($this->settings, $username, $password));
  67. # Call plugins
  68. $this->plugins(__METHOD__, 0, func_get_args());
  69. $username = crypt($username, $this->settings['username']);
  70. $password = crypt($password, $this->settings['password']);
  71. # Check login with crypted hash
  72. if ($this->settings['username']===$username&&
  73. $this->settings['password']===$password) {
  74. $_SESSION['login'] = true;
  75. $_SESSION['identifier'] = $this->settings['identifier'];
  76. return true;
  77. }
  78. # No login
  79. if ($this->noLogin()===true) return true;
  80. # Call plugins
  81. $this->plugins(__METHOD__, 1, func_get_args());
  82. return false;
  83. }
  84. private function noLogin() {
  85. # Check dependencies
  86. self::dependencies(isset($this->settings));
  87. # Check if login credentials exist and login if they don't
  88. if ($this->settings['username']===''&&
  89. $this->settings['password']==='') {
  90. $_SESSION['login'] = true;
  91. $_SESSION['identifier'] = $this->settings['identifier'];
  92. return true;
  93. }
  94. return false;
  95. }
  96. public function logout() {
  97. # Call plugins
  98. $this->plugins(__METHOD__, 0, func_get_args());
  99. $_SESSION['login'] = null;
  100. $_SESSION['identifier'] = null;
  101. session_destroy();
  102. # Call plugins
  103. $this->plugins(__METHOD__, 1, func_get_args());
  104. return true;
  105. }
  106. }
  107. ?>