A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
9.9 KiB

13 years ago
  1. <?php
  2. if(!defined('QUASICMS') ) die("No quasi.");
  3. if (!defined("LOSTPASSWORDMODULE.CLASS.PHP")){
  4. define("LOSTPASSWORDMODULE.CLASS.PHP",1);
  5. /**
  6. * Class LostPasswordModule - provides a module to retrieve lost passwords
  7. * This module will prompt the user for a username or an email address and attempt
  8. * to retrieve the corresponding Account. If successful, it will create a onetime password
  9. * and send it to the email address for the Person (account.person_id). account.onetime_password
  10. * will be set true. The user is sent directly to change the password on login if onetime is true.
  11. * If onetime is true at logout, valid_password is set to false, which will trigger failure and a
  12. * redirect to this module if a second login is attempted - this is to enforce that the user reset
  13. * the password after retrieval.
  14. *
  15. *@author Erik Winn <erikwinnmail@yahoo.com>
  16. *
  17. *
  18. * $Id: LostPasswordModule.class.php 286 2008-10-10 23:33:36Z erikwinn $
  19. *@version 0.1
  20. *
  21. *@copyright (C) 2008 by Erik Winn
  22. *@license GPL v.2
  23. This program is free software; you can redistribute it and/or modify
  24. it under the terms of the GNU General Public License as published by
  25. the Free Software Foundation; either version 2 of the License, or
  26. (at your option) any later version.
  27. This program is distributed in the hope that it will be useful,
  28. but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. GNU General Public License for more details.
  31. You should have received a copy of the GNU General Public License
  32. along with this program; if not, write to the Free Software
  33. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  34. *
  35. *@package Quasi
  36. * @subpackage Modules
  37. */
  38. class LostPasswordModule extends QPanel
  39. {
  40. /**
  41. * @var ContentBlockView objContentBlock - the content block to which this module is assigned
  42. */
  43. protected $objContentBlock;
  44. /**
  45. * @var Account objAccount - local instance of the Account
  46. */
  47. protected $objAccount = null;
  48. /**
  49. * @var Person objPerson - local instance of the Person
  50. */
  51. protected $objPerson = null;
  52. /**
  53. * Note: this will accept a username or an email address, an account will be retrieved for either
  54. * if possible.
  55. * @var QTextBox txtUserName - input for password retrieval
  56. */
  57. public $txtUserName;
  58. public $lblInstructions;
  59. public $lblMessage;
  60. public $btnSubmit;
  61. /**
  62. * Module constructor
  63. * NOTE: This module ignores the required extra parameters ..
  64. *@param ContentBlock - parent controller object.
  65. *@param mixed - extra parameters, ignored
  66. */
  67. public function __construct( ContentBlockView $objContentBlock, $mixParameters=null)
  68. {
  69. //Parent should always be a ContentBlockView
  70. $this->objContentBlock =& $objContentBlock;
  71. try {
  72. parent::__construct($this->objContentBlock);
  73. } catch (QCallerException $objExc) {
  74. $objExc->IncrementOffset();
  75. throw $objExc;
  76. }
  77. $this->strTemplate = __QUASI_CORE_TEMPLATES__ . '/LostPasswordModule.tpl.php';
  78. $this->txtUserName = new QTextBox($this);
  79. $this->txtUserName->Name = Quasi::Translate('Username or Email');
  80. $this->txtUserName->Required = true;
  81. $this->lblMessage = new QLabel($this);
  82. $this->lblMessage->HtmlEntities = false;
  83. $this->lblInstructions = new QLabel($this);
  84. $this->lblInstructions->HtmlEntities = false;
  85. $this->lblInstructions->Text = Quasi::Translate('Please enter your username or primary email address') .':<br />';
  86. $this->btnSubmit = new QButton($this);
  87. $this->btnSubmit->Text = QApplication::Translate('Submit');
  88. if(IndexPage::$blnAjaxOk)
  89. $this->btnSubmit->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSubmit_Click'));
  90. else
  91. $this->btnSubmit->AddAction(new QClickEvent(), new QServerControlAction($this, 'btnSubmit_Click'));
  92. $this->btnSubmit->CausesValidation = $this;
  93. }
  94. /**
  95. * This Function is called when any input is sent - on failure the
  96. * fields are redrawn with optional error messages.
  97. */
  98. public function Validate()
  99. {
  100. $blnToReturn = true;
  101. // validate input here
  102. return $blnToReturn;
  103. }
  104. public function btnSubmit_Click($strFormId, $strControlId, $strParameter)
  105. {
  106. $strInput = $this->txtUserName->Text;
  107. $this->objAccount = Account::LoadByUsername($strInput);
  108. if( ! $this->objAccount instanceof Account )
  109. {
  110. $aryPersons = Person::LoadArrayByEmailAddress($strInput);
  111. foreach($aryPersons as $objPerson)
  112. {
  113. $this->objAccount = Account::LoadByPersonId($objPerson->Id);
  114. if( $this->objAccount instanceof Account )
  115. {
  116. $this->objPerson = $objPerson;
  117. break;
  118. }
  119. }
  120. }
  121. //Still no good? sorry ..
  122. if( ! $this->objAccount instanceof Account )
  123. {
  124. $strMessage = Quasi::Translate('I am sorry, I can not find an account for this username or email') . '! <br />'
  125. . Quasi::Translate('Please contact support at') . Quasi::$SupportEmailLink . Quasi::Translate('for further assistance') . '.' ;
  126. }
  127. else
  128. {
  129. $strMessage = Quasi::Translate('Thank You ') . $this->objAccount->Name . '! <br />'
  130. . Quasi::Translate('You will receive an email in a few minutes containing a onetime password to use to login and reset your password.');
  131. $this->lblInstructions->Visible = false;
  132. $this->txtUserName->Visible = false;
  133. $this->btnSubmit->Visible = false;
  134. $this->setRandomPassword();
  135. }
  136. $this->lblMessage->Text = $strMessage;
  137. }
  138. private function setRandomPassword()
  139. {
  140. $strPassword = self::CreatePassword();
  141. $this->objAccount->Password = sha1($strPassword);
  142. $this->objAccount->OnetimePassword = true;
  143. $this->objAccount->ValidPassword = true;
  144. $this->objAccount->Save();
  145. if(null == $this->objPerson)
  146. $this->objPerson = Person::LoadById( $this->objAccount->PersonId );
  147. $strEmailText = Quasi::Translate('Hi ') . $this->objAccount->Name . ", \n"
  148. . Quasi::Translate(' Here is a temporary password you can use to log in to your account ') . ". \n\n"
  149. . Quasi::Translate(' Username') . ': ' . $this->objAccount->Username . " \n"
  150. . Quasi::Translate(' Password') . ': ' . $strPassword . " \n\n"
  151. . Quasi::Translate('PLEASE NOTE: This password can only be used once. You MUST RESET YOUR PASSWORD after logging in!')
  152. . Quasi::Translate('Warm Regards') . ", \n\n" . STORE_NAME . Quasi::Translate('Support Team') . "\n"
  153. . STORE_EMAIL_ADDRESS . "\n .\n";
  154. $objEmail = new QEmailMessage();
  155. $objEmail->From = STORE_NAME . ' <' . STORE_EMAIL_ADDRESS . '>';
  156. $objEmail->Subject = STORE_NAME . ' Important Information ';
  157. $objEmail->To = $this->objPerson->FullName . ' <' . $this->objPerson->EmailAddress . '>';
  158. $objEmail->Body = $strEmailText;
  159. QEmailServer::Send($objEmail);
  160. }
  161. public static function CreatePassword($intLength = 8, $blnHard = true)
  162. {
  163. if($blnHard)
  164. $strChars = "0123456789_!@#$%&*()-=+/abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  165. else
  166. $strChars = "0123456789abcdfghjkmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  167. $strToReturn = '';
  168. $intCtr = 0;
  169. $intSelectionLength = strlen($strChars) - 1;
  170. while ($intCtr < $intLength)
  171. {
  172. $strChar = substr($strChars, rand(0, $intSelectionLength), 1);
  173. if (false === strpos($strToReturn, $strChar))
  174. {
  175. $strToReturn .= $strChar;
  176. $intCtr++;
  177. }
  178. }
  179. return $strToReturn;
  180. }
  181. public function __get($strName)
  182. {
  183. switch ($strName)
  184. {
  185. case 'Account':
  186. return $this->objAccount ;
  187. default:
  188. try {
  189. return parent::__get($strName);
  190. } catch (QCallerException $objExc) {
  191. $objExc->IncrementOffset();
  192. throw $objExc;
  193. }
  194. }
  195. }
  196. public function __set($strName, $mixValue)
  197. {
  198. switch ($strName)
  199. {
  200. case 'Account':
  201. try {
  202. return ($this->objAccount = QType::Cast($mixValue, 'Account' ));
  203. } catch (QInvalidCastException $objExc) {
  204. $objExc->IncrementOffset();
  205. throw $objExc;
  206. }
  207. default:
  208. try {
  209. return (parent::__set($strName, $mixValue));
  210. } catch (QCallerException $objExc) {
  211. $objExc->IncrementOffset();
  212. throw $objExc;
  213. }
  214. }
  215. }
  216. }//end class
  217. }//end define
  218. ?>