Session.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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']['medium']);
  41. unset($return['config']['plugins']);
  42. }
  43. // Call plugins
  44. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  45. return $return;
  46. }
  47. /**
  48. * Sets the session values when username and password correct.
  49. * @return boolean Returns true when login was successful.
  50. */
  51. public function login($username, $password) {
  52. // Call plugins
  53. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  54. $username = crypt($username, Settings::get()['username']);
  55. $password = crypt($password, Settings::get()['password']);
  56. // Check login with crypted hash
  57. if (Settings::get()['username']===$username&&
  58. Settings::get()['password']===$password) {
  59. $_SESSION['login'] = true;
  60. $_SESSION['identifier'] = Settings::get()['identifier'];
  61. return true;
  62. }
  63. // No login
  64. if ($this->noLogin()===true) return true;
  65. // Call plugins
  66. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  67. return false;
  68. }
  69. /**
  70. * Sets the session values when no there is no username and password in the database.
  71. * @return boolean Returns true when no login was found.
  72. */
  73. private function noLogin() {
  74. // Check if login credentials exist and login if they don't
  75. if (Settings::get()['username']===''&&
  76. Settings::get()['password']==='') {
  77. $_SESSION['login'] = true;
  78. $_SESSION['identifier'] = Settings::get()['identifier'];
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. * Unsets the session values.
  85. * @return boolean Returns true when logout was successful.
  86. */
  87. public function logout() {
  88. // Call plugins
  89. Plugins::get()->activate(__METHOD__, 0, func_get_args());
  90. session_unset();
  91. session_destroy();
  92. // Call plugins
  93. Plugins::get()->activate(__METHOD__, 1, func_get_args());
  94. return true;
  95. }
  96. }
  97. ?>