Session.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. # Path to Lychee for the server-import dialog
  33. $return['config']['location'] = LYCHEE;
  34. # Check if login credentials exist and login if they don't
  35. if ($this->noLogin()===true) {
  36. $public = false;
  37. $return['config']['login'] = false;
  38. } else {
  39. $return['config']['login'] = true;
  40. }
  41. if ($public===false) {
  42. # Logged in
  43. $return['status'] = LYCHEE_STATUS_LOGGEDIN;
  44. } else {
  45. # Logged out
  46. $return['status'] = LYCHEE_STATUS_LOGGEDOUT;
  47. # Unset unused vars
  48. unset($return['config']['thumbQuality']);
  49. unset($return['config']['sorting']);
  50. unset($return['config']['sortingAlbums']);
  51. unset($return['config']['dropboxKey']);
  52. unset($return['config']['login']);
  53. unset($return['config']['location']);
  54. unset($return['config']['imagick']);
  55. unset($return['config']['medium']);
  56. unset($return['config']['plugins']);
  57. }
  58. # Call plugins
  59. $this->plugins(__METHOD__, 1, func_get_args());
  60. return $return;
  61. }
  62. public function login($username, $password) {
  63. # Check dependencies
  64. self::dependencies(isset($this->settings, $username, $password));
  65. # Call plugins
  66. $this->plugins(__METHOD__, 0, func_get_args());
  67. $username = crypt($username, $this->settings['username']);
  68. $password = crypt($password, $this->settings['password']);
  69. # Check login with crypted hash
  70. if ($this->settings['username']===$username&&
  71. $this->settings['password']===$password) {
  72. $_SESSION['login'] = true;
  73. return true;
  74. }
  75. # No login
  76. if ($this->noLogin()===true) {
  77. $_SESSION['login'] = true;
  78. return true;
  79. }
  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. return true;
  92. }
  93. return false;
  94. }
  95. public function logout() {
  96. # Call plugins
  97. $this->plugins(__METHOD__, 0, func_get_args());
  98. session_destroy();
  99. # Call plugins
  100. $this->plugins(__METHOD__, 1, func_get_args());
  101. return true;
  102. }
  103. }
  104. ?>