Session.php 2.7 KB

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