defaultlogin.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php namespace HashOver;
  2. // Copyright (C) 2015-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class DefaultLogin
  27. {
  28. public $setup;
  29. public $cookies;
  30. public $locale;
  31. public $name;
  32. public $password;
  33. public $loginHash;
  34. public $email;
  35. public $website;
  36. public function __construct (Setup $setup, Cookies $cookies, Locale $locale)
  37. {
  38. $this->setup = $setup;
  39. $this->cookies = $cookies;
  40. $this->locale = $locale;
  41. // Disable login is cookies are disabled
  42. if ($setup->setsCookies === false) {
  43. $setup->allowsLogin = false;
  44. $setup->syncSettings ();
  45. }
  46. }
  47. // Set login credentials
  48. public function setCredentials ()
  49. {
  50. // Generate encrypted string / decryption key from e-mail
  51. $encryption_keys = $this->setup->encryption->encrypt ($this->email);
  52. // Set login cookies
  53. $this->cookies->set ('name', $this->name);
  54. $this->cookies->set ('password', $this->password);
  55. $this->cookies->set ('email', $encryption_keys['encrypted']);
  56. $this->cookies->set ('encryption', $encryption_keys['keys']);
  57. $this->cookies->set ('website', $this->website);
  58. }
  59. // Get login credentials
  60. public function getCredentials ()
  61. {
  62. // Set user name via cookie
  63. $this->name = trim ($this->cookies->getValue ('name'), " \r\n\t");
  64. // Set user password via cookie
  65. $this->password = trim ($this->cookies->getValue ('password'), " \r\n\t");
  66. // Decrypt email cookie
  67. $encrypted_email = trim ($this->cookies->getValue ('email'), " \r\n\t");
  68. $encryption_keys = trim ($this->cookies->getValue ('encryption'), " \r\n\t");
  69. $decrypted_email = $this->setup->encryption->decrypt ($encrypted_email, $encryption_keys);
  70. // Validate e-mail address
  71. if (filter_var ($decrypted_email, FILTER_VALIDATE_EMAIL)) {
  72. $this->email = trim ($decrypted_email, " \r\n\t");
  73. }
  74. // Set user website via cookie
  75. $this->website = trim ($this->cookies->getValue ('website'), " \r\n\t");
  76. // Set login hash via cookie
  77. $this->loginHash = trim ($this->cookies->getValue ('hashover-login'), " \r\n\t");
  78. }
  79. // Main login method
  80. public function setLogin ()
  81. {
  82. // Set login cookie
  83. $this->cookies->set ('hashover-login', $this->loginHash);
  84. }
  85. // Main logout method
  86. public function clearLogin ()
  87. {
  88. // Expire login cookie
  89. $this->cookies->expireCookie ('hashover-login');
  90. }
  91. }