PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/packages/login-1.9.0-pl/modCategory/ea2ccf54911c1b480956f6980fa70563/0/login/controllers/web/ForgotPassword.php

https://gitlab.com/haque.mdmanzurul/nga-loyaltymatters
PHP | 250 lines | 149 code | 23 blank | 78 comment | 15 complexity | 3697076792f4bf30a90b65e0e84dee70 MD5 | raw file
  1. <?php
  2. /**
  3. * Login
  4. *
  5. * Copyright 2010-2012 by Shaun McCormick <shaun+login@modx.com>
  6. *
  7. * Login is free software; you can redistribute it and/or modify it under the
  8. * terms of the GNU General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option) any later
  10. * version.
  11. *
  12. * Login is distributed in the hope that it will be useful, but WITHOUT ANY
  13. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  14. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * Login; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * @package login
  21. */
  22. /**
  23. * Handles the Forgot Password form for users
  24. *
  25. * @package login
  26. * @subpackage controllers
  27. */
  28. class LoginForgotPasswordController extends LoginController {
  29. /** @var modUser $user */
  30. public $user;
  31. /** @var modUserProfile $profile */
  32. public $profile;
  33. /** @var string $templateToLoad */
  34. public $templateToLoad = 'lgnForgotPassTpl';
  35. /** @var string $templateTypeToLoad */
  36. public $templateTypeToLoad = 'modChunk';
  37. /** @var array $placeholders */
  38. public $placeholders = array();
  39. /** @var string $usernameField */
  40. public $usernameField = 'username';
  41. /** @var int $emailsSent */
  42. public $emailsSent = 0;
  43. public function initialize() {
  44. $this->modx->lexicon->load('login:forgotpassword');
  45. $this->setDefaultProperties(array(
  46. 'tpl' => 'lgnForgotPassTpl',
  47. 'tplType' => 'modChunk',
  48. 'sentTpl' => 'lgnForgotPassSentTpl',
  49. 'sentTplType' => 'modChunk',
  50. 'emailTpl' => 'lgnForgotPassEmail',
  51. 'emailTplAlt' => '',
  52. 'emailTplType' => 'modChunk',
  53. 'emailSubject' => '',
  54. 'preHooks' => '',
  55. 'resetResourceId' => 1,
  56. 'redirectTo' => false,
  57. 'redirectParams' => '',
  58. 'submitVar' => 'login_fp_service',
  59. ));
  60. }
  61. /**
  62. * Process the controller
  63. * @return string
  64. */
  65. public function process() {
  66. $this->templateToLoad = $this->getProperty('tpl');
  67. $this->templateTypeToLoad = $this->getProperty('tplType');
  68. /* get the request URI */
  69. $this->placeholders['loginfp.request_uri'] = empty($_POST['request_uri']) ? $this->login->getRequestURI() : $_POST['request_uri'];
  70. if ($this->hasPost()) {
  71. $this->handlePost();
  72. $fields = $this->dictionary->toArray();
  73. foreach ($fields as $k => $v) {
  74. $this->placeholders['loginfp.post.'.$k] = str_replace(array('[',']'),array('&#91;','&#93'),$v);
  75. }
  76. }
  77. return $this->login->getChunk($this->templateToLoad,$this->placeholders,$this->templateTypeToLoad);
  78. }
  79. /**
  80. * Handle the form submission
  81. * @return boolean
  82. */
  83. public function handlePost() {
  84. $this->loadDictionary();
  85. $success = false;
  86. $this->sanitizeFields();
  87. if ($this->runPreHooks()) {
  88. $this->fetchUser();
  89. if (empty($this->user)) {
  90. $this->placeholders['loginfp.errors'] = $this->formatError($this->modx->lexicon('login.user_err_nf_'.$this->usernameField));
  91. } else {
  92. $this->placeholders['email'] = $this->dictionary->get('email');
  93. $this->sendPasswordResetEmail();
  94. $this->templateToLoad = $this->getProperty('sentTpl');
  95. $this->templateTypeToLoad = $this->getProperty('sentTplType');
  96. $this->checkForRedirect();
  97. }
  98. }
  99. return $success;
  100. }
  101. /**
  102. * Wrap errors in an error tpl
  103. * @param string $message
  104. * @return string
  105. */
  106. public function formatError($message) {
  107. $errTpl = $this->getProperty('errTpl','lgnErrTpl');
  108. $errTplType = $this->getProperty('errTplType','modChunk');
  109. return $this->login->getChunk($errTpl, array('msg' => $message),$errTplType);
  110. }
  111. /**
  112. * Fetch the user to update, also allowing external user updating
  113. * @return modUser
  114. */
  115. public function fetchUser() {
  116. $fields = $this->dictionary->toArray();
  117. $this->usernameField = 'username';
  118. $alias = 'modUser';
  119. if (empty($fields['username']) && !empty($fields['email'])) {
  120. $this->usernameField = 'email';
  121. $alias = 'Profile';
  122. }
  123. /* if the preHook didn't set the user info, find it by email/username */
  124. if (empty($fields[Login::FORGOT_PASSWORD_EXTERNAL_USER])) {
  125. /* get the user dependent on the retrieval method */
  126. $this->user = $this->login->getUserByField($this->usernameField,$fields[$this->usernameField],$alias);
  127. if ($this->user) {
  128. $fields = array_merge($fields,$this->user->toArray());
  129. $this->profile = $this->user->getOne('Profile');
  130. if ($this->profile) { /* merge in profile */
  131. $fields = array_merge($this->profile->toArray(),$fields);
  132. }
  133. }
  134. }
  135. $this->dictionary->fromArray($fields);
  136. return $this->user;
  137. }
  138. /**
  139. * Check to see if the form has been submitted
  140. * @return boolean
  141. */
  142. public function hasPost() {
  143. $submitVar = $this->getProperty('submitVar','login_fp_service');
  144. return !empty($_POST) && !empty($_POST[$submitVar]);
  145. }
  146. /**
  147. * Sanitize the values sent on the form
  148. * @return void
  149. */
  150. public function sanitizeFields() {
  151. $fields = $this->dictionary->toArray();
  152. foreach ($fields as $k => $v) {
  153. $fields[$k] = str_replace(array('[',']'),array('&#91;','&#93'),$v);
  154. }
  155. $this->dictionary->fromArray($fields);
  156. }
  157. /**
  158. * Run any preHooks to process before submitting the form
  159. * @return boolean
  160. */
  161. public function runPreHooks() {
  162. $success = true;
  163. $preHooks = $this->getProperty('preHooks','');
  164. if (!empty($preHooks)) {
  165. $this->loadHooks('preHooks');
  166. $this->preHooks->loadMultiple($preHooks,$this->dictionary->toArray(),array(
  167. 'mode' => Login::MODE_FORGOT_PASSWORD,
  168. ));
  169. /* process preHooks */
  170. if ($this->preHooks->hasErrors()) {
  171. $success = false;
  172. $this->modx->toPlaceholders($this->preHooks->getErrors(),$this->getProperty('errorPrefix'));
  173. $errorMsg = $this->preHooks->getErrorMessage();
  174. $errorOutput = $this->formatError($errorMsg);
  175. $this->modx->setPlaceholder('errors',$errorOutput);
  176. }
  177. $values = $this->preHooks->getValues();
  178. if (!empty($values)) {
  179. $this->dictionary->fromArray($values);
  180. }
  181. }
  182. return $success;
  183. }
  184. /**
  185. * Send an email to the user with a confirmation URL to reset their password at
  186. * @return void
  187. */
  188. public function sendPasswordResetEmail() {
  189. $fields = $this->dictionary->toArray();
  190. /* generate a password and encode it and the username into the url */
  191. $password = $this->login->generatePassword();
  192. $confirmParams = array(
  193. 'lp' => urlencode(base64_encode($password)),
  194. 'lu' => urlencode(base64_encode($fields['username']))
  195. );
  196. $confirmUrl = $this->modx->makeUrl($this->getProperty('resetResourceId',1),'',$confirmParams,'full');
  197. /* set the email properties */
  198. $emailProperties = $fields;
  199. $emailProperties['confirmUrl'] = $confirmUrl;
  200. $emailProperties['password'] = $password;
  201. $emailProperties['tpl'] = $this->getProperty('emailTpl');
  202. $emailProperties['tplAlt'] = $this->getProperty('emailTplAlt','');
  203. $emailProperties['tplType'] = $this->getProperty('emailTplType');
  204. /* now set new password to cache to prevent middleman attacks */
  205. $this->modx->cacheManager->set('login/resetpassword/'.md5($fields['id'].':'.$fields['username']),$password);
  206. $emailSubject = $this->getProperty('emailSubject','');
  207. $subject = !empty($emailSubject) ? $emailSubject : $this->modx->getOption('login.forgot_password_email_subject',null,$this->modx->lexicon('login.forgot_password_email_subject'));
  208. $this->login->sendEmail($fields['email'],$fields['username'],$subject,$emailProperties);
  209. $this->emailsSent++;
  210. }
  211. /**
  212. * Redirect the user to another page after successful form submission, if desired
  213. * @return boolean
  214. */
  215. public function checkForRedirect() {
  216. $redirectTo = $this->getProperty('redirectTo',false,'isset');
  217. /* if redirecting, do so here */
  218. if (!empty($redirectTo)) {
  219. $redirectParams = $this->getProperty('redirectParams','');
  220. if (!empty($redirectParams)) $redirectParams = $this->modx->fromJSON($redirectParams);
  221. $url = $this->modx->makeUrl($redirectTo,'',$redirectParams,'full');
  222. $this->modx->sendRedirect($url);
  223. }
  224. return !empty($redirectTo);
  225. }
  226. }
  227. return 'LoginForgotPasswordController';