Session.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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']['sortingAlbums']);
  32. unset($return['config']['sortingPhotos']);
  33. unset($return['config']['dropboxKey']);
  34. unset($return['config']['login']);
  35. unset($return['config']['location']);
  36. unset($return['config']['imagick']);
  37. unset($return['config']['medium']);
  38. unset($return['config']['plugins']);
  39. }
  40. // Call plugins
  41. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  42. return $return;
  43. }
  44. public function login($username, $password) {
  45. // Call plugins
  46. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  47. $username = crypt($username, Settings::get()['username']);
  48. $password = crypt($password, Settings::get()['password']);
  49. // Check login with crypted hash
  50. if (Settings::get()['username']===$username&&
  51. Settings::get()['password']===$password) {
  52. $_SESSION['login'] = true;
  53. $_SESSION['identifier'] = Settings::get()['identifier'];
  54. return true;
  55. }
  56. // No login
  57. if ($this->noLogin()===true) return true;
  58. // Call plugins
  59. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  60. return false;
  61. }
  62. private function noLogin() {
  63. // Check if login credentials exist and login if they don't
  64. if (Settings::get()['username']===''&&
  65. Settings::get()['password']==='') {
  66. $_SESSION['login'] = true;
  67. $_SESSION['identifier'] = Settings::get()['identifier'];
  68. return true;
  69. }
  70. return false;
  71. }
  72. public function logout() {
  73. // Call plugins
  74. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  75. $_SESSION['login'] = null;
  76. $_SESSION['identifier'] = null;
  77. session_destroy();
  78. // Call plugins
  79. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  80. return true;
  81. }
  82. }
  83. ?>