Session.php 3.0 KB

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