Session.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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']['dropboxKey']);
  51. unset($return['config']['login']);
  52. unset($return['config']['location']);
  53. unset($return['config']['plugins']);
  54. }
  55. # Call plugins
  56. $this->plugins(__METHOD__, 1, func_get_args());
  57. return $return;
  58. }
  59. public function login($username, $password) {
  60. # Check dependencies
  61. self::dependencies(isset($this->settings, $username, $password));
  62. # Call plugins
  63. $this->plugins(__METHOD__, 0, func_get_args());
  64. $username = crypt($username, $this->settings['username']);
  65. $password = crypt($password, $this->settings['password']);
  66. # Check login with crypted hash
  67. if ($this->settings['username']===$username&&
  68. $this->settings['password']===$password) {
  69. $_SESSION['login'] = true;
  70. return true;
  71. }
  72. # No login
  73. if ($this->noLogin()===true) {
  74. $_SESSION['login'] = true;
  75. return true;
  76. }
  77. # Call plugins
  78. $this->plugins(__METHOD__, 1, func_get_args());
  79. return false;
  80. }
  81. private function noLogin() {
  82. # Check dependencies
  83. self::dependencies(isset($this->settings));
  84. # Check if login credentials exist and login if they don't
  85. if ($this->settings['username']===''&&
  86. $this->settings['password']==='') {
  87. $_SESSION['login'] = true;
  88. return true;
  89. }
  90. return false;
  91. }
  92. public function logout() {
  93. # Call plugins
  94. $this->plugins(__METHOD__, 0, func_get_args());
  95. session_destroy();
  96. # Call plugins
  97. $this->plugins(__METHOD__, 1, func_get_args());
  98. return true;
  99. }
  100. }
  101. ?>