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