Session.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. ###
  3. # @name Session Module
  4. # @author Tobias Reich
  5. # @copyright 2014 by Tobias Reich
  6. ###
  7. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  8. class Session extends Module {
  9. private $settings = null;
  10. public function __construct($plugins, $settings) {
  11. # Init vars
  12. $this->plugins = $plugins;
  13. $this->settings = $settings;
  14. return true;
  15. }
  16. public function init($database, $dbName, $public, $version) {
  17. # Check dependencies
  18. $this->dependencies(isset($this->settings, $public, $version));
  19. # Call plugins
  20. $this->plugins(__METHOD__, 0, func_get_args());
  21. # Update
  22. if (!isset($this->settings['version'])||$this->settings['version']!==$version)
  23. if (!Database::update($database, $dbName, @$this->settings['version'])) exit('Error: Updating the database failed!');
  24. # Return settings
  25. $return['config'] = $this->settings;
  26. unset($return['config']['password']);
  27. # No login
  28. if ($this->settings['username']===''&&$this->settings['password']==='') $return['config']['login'] = false;
  29. else $return['config']['login'] = true;
  30. if ($public===false) {
  31. # Logged in
  32. $return['loggedIn'] = true;
  33. } else {
  34. # Unset unused vars
  35. unset($return['config']['username']);
  36. unset($return['config']['thumbQuality']);
  37. unset($return['config']['sorting']);
  38. unset($return['config']['dropboxKey']);
  39. unset($return['config']['login']);
  40. # Logged out
  41. $return['loggedIn'] = false;
  42. }
  43. # Call plugins
  44. $this->plugins(__METHOD__, 1, func_get_args());
  45. return $return;
  46. }
  47. public function login($username, $password) {
  48. # Check dependencies
  49. $this->dependencies(isset($this->settings, $username, $password));
  50. # Call plugins
  51. $this->plugins(__METHOD__, 0, func_get_args());
  52. # Check login with MD5 hash
  53. if ($username===$this->settings['username']&&$password===$this->settings['password']) {
  54. $_SESSION['login'] = true;
  55. return true;
  56. }
  57. # Check login with crypted hash
  58. if ($username===$this->settings['username']&&$this->settings['password']===crypt($password, $this->settings['password'])) {
  59. $_SESSION['login'] = true;
  60. return true;
  61. }
  62. # No login
  63. if ($this->settings['username']===''&&$this->settings['password']==='') {
  64. $_SESSION['login'] = true;
  65. return true;
  66. }
  67. # Call plugins
  68. $this->plugins(__METHOD__, 1, func_get_args());
  69. return false;
  70. }
  71. public function logout() {
  72. # Call plugins
  73. $this->plugins(__METHOD__, 0, func_get_args());
  74. session_destroy();
  75. # Call plugins
  76. $this->plugins(__METHOD__, 1, func_get_args());
  77. return true;
  78. }
  79. }
  80. ?>