Session.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. ###
  3. # @name Session Module
  4. # @copyright 2015 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. class Session extends Module {
  8. private $settings = null;
  9. public function __construct($plugins, $settings) {
  10. # Init vars
  11. $this->plugins = $plugins;
  12. $this->settings = $settings;
  13. return true;
  14. }
  15. public function init($database, $dbName, $public, $version) {
  16. # Check dependencies
  17. self::dependencies(isset($this->settings, $public, $version));
  18. # Call plugins
  19. $this->plugins(__METHOD__, 0, func_get_args());
  20. # Update
  21. if (!isset($this->settings['version'])||$this->settings['version']!==$version) {
  22. if (!Database::update($database, $dbName, @$this->settings['version'])) {
  23. Log::error($database, __METHOD__, __LINE__, 'Updating the database failed');
  24. exit('Error: Updating the database failed!');
  25. }
  26. }
  27. # Return settings
  28. $return['config'] = $this->settings;
  29. unset($return['config']['password']);
  30. # Path to Lychee for the server-import dialog
  31. $return['config']['location'] = LYCHEE;
  32. # Check if login credentials exist and login if they don't
  33. if ($this->noLogin()===true) {
  34. $public = false;
  35. $return['config']['login'] = false;
  36. } else {
  37. $return['config']['login'] = true;
  38. }
  39. if ($public===false) {
  40. # Logged in
  41. $return['loggedIn'] = true;
  42. } else {
  43. # Unset unused vars
  44. unset($return['config']['username']);
  45. unset($return['config']['thumbQuality']);
  46. unset($return['config']['sorting']);
  47. unset($return['config']['dropboxKey']);
  48. unset($return['config']['login']);
  49. unset($return['config']['location']);
  50. unset($return['config']['plugins']);
  51. # Logged out
  52. $return['loggedIn'] = false;
  53. }
  54. # Call plugins
  55. $this->plugins(__METHOD__, 1, func_get_args());
  56. return $return;
  57. }
  58. public function login($username, $password) {
  59. # Check dependencies
  60. self::dependencies(isset($this->settings, $username, $password));
  61. # Call plugins
  62. $this->plugins(__METHOD__, 0, func_get_args());
  63. $username = crypt($username, $this->settings['username']);
  64. $password = crypt($password, $this->settings['password']);
  65. # Check login with crypted hash
  66. if ($this->settings['username']===$username&&
  67. $this->settings['password']===$password) {
  68. $_SESSION['login'] = true;
  69. return true;
  70. }
  71. # No login
  72. if ($this->noLogin()===true) {
  73. $_SESSION['login'] = true;
  74. return true;
  75. }
  76. # Call plugins
  77. $this->plugins(__METHOD__, 1, func_get_args());
  78. return false;
  79. }
  80. private function noLogin() {
  81. # Check dependencies
  82. self::dependencies(isset($this->settings));
  83. # Check if login credentials exist and login if they don't
  84. if ($this->settings['username']===''&&
  85. $this->settings['password']==='') {
  86. $_SESSION['login'] = true;
  87. return true;
  88. }
  89. return false;
  90. }
  91. public function logout() {
  92. # Call plugins
  93. $this->plugins(__METHOD__, 0, func_get_args());
  94. session_destroy();
  95. # Call plugins
  96. $this->plugins(__METHOD__, 1, func_get_args());
  97. return true;
  98. }
  99. }
  100. ?>