Session.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. # Check login with MD5 hash
  64. if ($username===$this->settings['username']&&$password===$this->settings['password']) {
  65. $_SESSION['login'] = true;
  66. return true;
  67. }
  68. # Check login with crypted hash
  69. if ($username===$this->settings['username']&&$this->settings['password']===crypt($password, $this->settings['password'])) {
  70. $_SESSION['login'] = true;
  71. return true;
  72. }
  73. # No login
  74. if ($this->settings['username']===''&&$this->settings['password']==='') {
  75. $_SESSION['login'] = true;
  76. return true;
  77. }
  78. # Call plugins
  79. $this->plugins(__METHOD__, 1, func_get_args());
  80. return false;
  81. }
  82. private function noLogin() {
  83. # Check dependencies
  84. self::dependencies(isset($this->settings));
  85. # Check if login credentials exist and login if they don't
  86. if ($this->settings['username']===''&&$this->settings['password']==='') {
  87. $_SESSION['login'] = true;
  88. return true;
  89. }
  90. return false;
  91. }
  92. public function logout() {
  93. # Call plugins
  94. $this->plugins(__METHOD__, 0, func_get_args());
  95. session_destroy();
  96. # Call plugins
  97. $this->plugins(__METHOD__, 1, func_get_args());
  98. return true;
  99. }
  100. }
  101. ?>