PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Layer-2__Business_logic/content/forms/Lost_Password_form.php

#
PHP | 161 lines | 98 code | 30 blank | 33 comment | 25 complexity | eeba2db9634e577492380ffdb677f2eb MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. // Authors: Davi Leal
  3. //
  4. // Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Davi Leal <davi at leals dot com>
  5. //
  6. // This program is free software: you can redistribute it and/or modify it under
  7. // the terms of the GNU Affero General Public License as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied
  12. // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  13. // General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License along with this
  16. // program in the COPYING file. If not, see <http://www.gnu.org/licenses/>.
  17. require_once "../Layer-4__DBManager_etc/DB_Manager.php";
  18. // Methods take the values from the global $_POST[] array.
  19. class LostPassword
  20. {
  21. private $manager;
  22. private $checks;
  23. private $processingResult;
  24. function __construct()
  25. {
  26. $this->manager = new DBManager();
  27. $this->processingResult = '';
  28. }
  29. public function processForm()
  30. {
  31. // Check the log in state
  32. if ( $_SESSION['Logged'] == '1' )
  33. {
  34. $error = "<p>".gettext("You are already logged!. So we suppose you know your password. Anyway, log out and ask for it again if you want we send it to you.")."</p>";
  35. throw new Exception($error,false);
  36. }
  37. // Process each button event
  38. if ( $_POST['send'] != '' )
  39. {
  40. // Checks
  41. $this->checkLostPasswordForm();
  42. if ($this->checks['result'] == "pass" )
  43. {
  44. if ( $this->manager->lookForEntity(trim($_POST['Email'])) == true )
  45. {
  46. // Check to avoid spam
  47. // If we do not send the email, to avoid possible spam, we do not add the 'magic' to the data base because of without that email the user will not be able to use the 'magic' to get a new password.
  48. if ( $this->manager->allowLostPasswordEmail(trim($_POST['Email'])) == true )
  49. {
  50. // Make the 'magic' flag
  51. $magic = md5( rand().rand().rand().rand().rand().rand().rand().rand().rand().rand().rand() );
  52. // Keep the 'magic' in the data base
  53. $this->manager->saveLostPasswordMagicForEntity($magic);
  54. // Send the email
  55. $message = gettext("For security reasons, GNU Herds does not send passwords by electronic mail.")."\n\n";
  56. $message .= gettext("To get your new password follow the below link.")." ".gettext("That link will expire in 2 hours:")."\n\n";
  57. $message .= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?email=".trim($_POST['Email'])."&magic=".$magic;
  58. $message .= "\n\n";
  59. $message .= gettext("If you have not asked for a new password, ignore it and your password will not be changed.")."\n\n";
  60. mb_language("uni");
  61. mb_send_mail(trim($_POST['Email']), "GNU Herds: ".gettext("Lost password?"), "$message", "From: association@gnuherds.org");
  62. }
  63. }
  64. // Report to the user
  65. $this->processingResult .= "<p>&nbsp;</p><p>".vsprintf(gettext('An email has been sent to %s with the instructions to change the password.'),"<span class='must'>{$_POST['Email']}</span>")."<p>\n";
  66. }
  67. }
  68. elseif ( isset($_GET['email']) and $_GET['email'] != '' )
  69. {
  70. // Get and set a new password for the Entity who has that email
  71. $new_password = $this->manager->setNewPasswordForEntity();
  72. if ( $new_password != false )
  73. {
  74. // Show the new password
  75. $this->processingResult .= "<p>&nbsp;</p>\n";
  76. $this->processingResult .= "<p>&nbsp; &nbsp; &nbsp; &nbsp; ".gettext("Your new password is:")." <strong>".$new_password."</strong></p>\n";
  77. $this->processingResult .= "<p>&nbsp; &nbsp; &nbsp; &nbsp; ".gettext("To improve your security, you should change your password after loging in.")."</p>";
  78. }
  79. }
  80. }
  81. public function printOutput()
  82. {
  83. if ( $_POST['send'] != '' )
  84. {
  85. // Show the form
  86. $this->printPersonForm();
  87. }
  88. elseif ( isset($_GET['email']) and $_GET['email'] != '' )
  89. {
  90. // Show just the processingResult
  91. }
  92. elseif( isset($_GET['language']) )
  93. {
  94. // GET request from the language change form.
  95. // Show the form
  96. $this->printPersonForm();
  97. }
  98. else
  99. {
  100. // Show the form
  101. $this->printPersonForm();
  102. }
  103. if ( ( $_POST['send'] != '' and $this->checks['result'] == "pass" ) or $_GET['email'] != '' )
  104. echo $this->processingResult;
  105. }
  106. private function printPersonForm()
  107. {
  108. $smarty = new Smarty;
  109. $smarty->assign('checks', $this->checks);
  110. $smarty->display("Lost_Password_form.tpl");
  111. }
  112. private function checkLostPasswordForm()
  113. {
  114. $this->checks['result'] = "pass"; // By default the checks pass
  115. if ( trim($_POST['Email'])=='' )
  116. {
  117. $this->checks['result'] = "fail";
  118. $this->checks['Email'] = gettext('Please fill in here');
  119. }
  120. else
  121. {
  122. // The Email field have to keep the right syntax
  123. if (!preg_match("/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/", trim($_POST["Email"])))
  124. {
  125. $this->checks['result'] = "fail";
  126. $this->checks['Email'] = gettext('Invalid email address');
  127. }
  128. }
  129. }
  130. }
  131. ?>