Session.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Lychee\Modules;
  3. final class Session extends Module {
  4. public function init($public) {
  5. # Check dependencies
  6. self::dependencies(isset($public));
  7. # Call plugins
  8. $this->plugins(__METHOD__, 0, func_get_args());
  9. # Return settings
  10. $return['config'] = Settings::get();
  11. # Path to Lychee for the server-import dialog
  12. $return['config']['location'] = LYCHEE;
  13. # Remove username and password from response
  14. unset($return['config']['username']);
  15. unset($return['config']['password']);
  16. # Remove identifier from response
  17. unset($return['config']['identifier']);
  18. # Check if login credentials exist and login if they don't
  19. if ($this->noLogin()===true) {
  20. $public = false;
  21. $return['config']['login'] = false;
  22. } else {
  23. $return['config']['login'] = true;
  24. }
  25. if ($public===false) {
  26. # Logged in
  27. $return['status'] = LYCHEE_STATUS_LOGGEDIN;
  28. } else {
  29. # Logged out
  30. $return['status'] = LYCHEE_STATUS_LOGGEDOUT;
  31. # Unset unused vars
  32. unset($return['config']['thumbQuality']);
  33. unset($return['config']['sortingAlbums']);
  34. unset($return['config']['sortingPhotos']);
  35. unset($return['config']['dropboxKey']);
  36. unset($return['config']['login']);
  37. unset($return['config']['location']);
  38. unset($return['config']['imagick']);
  39. unset($return['config']['medium']);
  40. unset($return['config']['plugins']);
  41. }
  42. # Call plugins
  43. $this->plugins(__METHOD__, 1, func_get_args());
  44. return $return;
  45. }
  46. public function login($username, $password) {
  47. # Check dependencies
  48. self::dependencies(isset($username, $password));
  49. # Call plugins
  50. $this->plugins(__METHOD__, 0, func_get_args());
  51. $username = crypt($username, Settings::get()['username']);
  52. $password = crypt($password, Settings::get()['password']);
  53. # Check login with crypted hash
  54. if (Settings::get()['username']===$username&&
  55. Settings::get()['password']===$password) {
  56. $_SESSION['login'] = true;
  57. $_SESSION['identifier'] = Settings::get()['identifier'];
  58. return true;
  59. }
  60. # No login
  61. if ($this->noLogin()===true) return true;
  62. # Call plugins
  63. $this->plugins(__METHOD__, 1, func_get_args());
  64. return false;
  65. }
  66. private function noLogin() {
  67. # Check if login credentials exist and login if they don't
  68. if (Settings::get()['username']===''&&
  69. Settings::get()['password']==='') {
  70. $_SESSION['login'] = true;
  71. $_SESSION['identifier'] = Settings::get()['identifier'];
  72. return true;
  73. }
  74. return false;
  75. }
  76. public function logout() {
  77. # Call plugins
  78. $this->plugins(__METHOD__, 0, func_get_args());
  79. $_SESSION['login'] = null;
  80. $_SESSION['identifier'] = null;
  81. session_destroy();
  82. # Call plugins
  83. $this->plugins(__METHOD__, 1, func_get_args());
  84. return true;
  85. }
  86. }
  87. ?>