PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_users/models/reset.php

https://github.com/gluckspilz/joomla
PHP | 346 lines | 191 code | 60 blank | 95 comment | 27 complexity | c66a6907a42db3f146f2a41e3f0b68af MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version
  4. * @package Joomla.Site
  5. * @subpackage com_users
  6. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. jimport('joomla.application.component.modelform');
  11. jimport('joomla.event.dispatcher');
  12. /**
  13. * Rest model class for Users.
  14. *
  15. * @package Joomla.Site
  16. * @subpackage com_users
  17. * @version 1.0
  18. */
  19. class UsersModelReset extends JModelForm
  20. {
  21. protected function _populateState()
  22. {
  23. // Get the application object.
  24. $app = &JFactory::getApplication();
  25. $params = &$app->getParams('com_users');
  26. // Load the parameters.
  27. $this->setState('params', $params);
  28. }
  29. /**
  30. * Method to get the password reset request form.
  31. *
  32. * @access public
  33. * @return object JForm object on success, JException on failure.
  34. * @since 1.0
  35. */
  36. function &getForm()
  37. {
  38. // Get the form.
  39. $form = parent::getForm('reset_request', 'com_users.reset_request', array('array' => 'jform', 'event' => 'onPrepareForm'));
  40. // Check for an error.
  41. if (JError::isError($form)) {
  42. $this->setError($form->getMessage());
  43. return false;
  44. }
  45. // Get the dispatcher and load the users plugins.
  46. $dispatcher = &JDispatcher::getInstance();
  47. JPluginHelper::importPlugin('users');
  48. // Trigger the form preparation event.
  49. $results = $dispatcher->trigger('onPrepareUserResetRequestForm', array(&$form));
  50. // Check for errors encountered while preparing the form.
  51. if (count($results) && in_array(false, $results, true)) {
  52. $this->setError($dispatcher->getError());
  53. return false;
  54. }
  55. return $form;
  56. }
  57. /**
  58. * Method to get the password reset confirm form.
  59. *
  60. * @access public
  61. * @return object JForm object on success, JException on failure.
  62. * @since 1.0
  63. */
  64. function &getResetConfirmForm()
  65. {
  66. // Set the form loading options.
  67. $options = array(
  68. 'array' => true,
  69. 'event' => 'onPrepareUsersResetConfirmForm',
  70. 'group' => 'users'
  71. );
  72. // Get the form.
  73. return $this->getForm('reset_confirm', 'com_users.reset_confirm', $options);
  74. }
  75. /**
  76. * Method to get the password reset complete form.
  77. *
  78. * @access public
  79. * @return object JForm object on success, JException on failure.
  80. * @since 1.0
  81. */
  82. function &getResetCompleteForm()
  83. {
  84. // Set the form loading options.
  85. $options = array(
  86. 'array' => true,
  87. 'event' => 'onPrepareUsersResetCompleteForm',
  88. 'group' => 'users'
  89. );
  90. // Get the form.
  91. return $this->getForm('reset_complete', 'com_users.reset_complete', $options);
  92. }
  93. /**
  94. * Method to start the password reset process.
  95. */
  96. function processResetRequest($data)
  97. {
  98. $config = &JFactory::getConfig();
  99. // Get the form.
  100. $form = &$this->getResetRequestForm();
  101. // Check for an error.
  102. if (JError::isError($form)) {
  103. return $form;
  104. }
  105. // Filter and validate the form data.
  106. $data = $form->filter($data);
  107. $return = $form->validate($data);
  108. // Check for an error.
  109. if (JError::isError($return)) {
  110. return $return;
  111. }
  112. // Check the validation results.
  113. if ($return === false) {
  114. // Get the validation messages from the form.
  115. foreach ($form->getErrors() as $message) {
  116. $this->setError($message);
  117. }
  118. return false;
  119. }
  120. // Get the user id.
  121. jimport('joomla.user.helper');
  122. $userId = JUserHelper::getUserId($data['username']);
  123. // Make sure the user exists.
  124. if (empty($userId)) {
  125. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  126. return false;
  127. }
  128. // Get the user object.
  129. $user = JUser::getInstance($userId);
  130. // Make sure the user isn't blocked.
  131. if ($user->block) {
  132. $this->setError(JText::_('USERS_USER_BLOCKED'));
  133. return false;
  134. }
  135. // Set the confirmation token.
  136. $token = JUtility::getHash(JUserHelper::genRandomPassword());
  137. $user->activation = $token;
  138. // Save the user to the database.
  139. if (!$user->save(true)) {
  140. return new JException(JText::sprintf('USERS_USER_SAVE_FAILED', $user->getError()), 500);
  141. }
  142. // Assemble the password reset confirmation link.
  143. $mode = $config->getValue('force_ssl', 0) == 2 ? 1 : -1;
  144. $link = 'index.php?option=com_users&task=reset.confirm&username='.$user->username.'&token='.$token.'&'.JUtility::getToken(true).'=1';
  145. // Put together the e-mail template data.
  146. $data = $user->getProperties();
  147. $data['fromname'] = $config->getValue('fromname');
  148. $data['mailfrom'] = $config->getValue('mailfrom');
  149. $data['sitename'] = $config->getValue('sitename');
  150. $data['link_text'] = JRoute::_($link, false, $mode);
  151. $data['link_html'] = JRoute::_($link, true, $mode);
  152. $data['token'] = $token;
  153. // Load the mail template.
  154. jimport('joomla.utilities.simpletemplate');
  155. $template = new JSimpleTemplate();
  156. if (!$template->load('users.password.reset.request')) {
  157. return new JException(JText::_('USERS_RESET_MAIL_TEMPLATE_NOT_FOUND'), 500);
  158. }
  159. // Push in the email template variables.
  160. $template->bind($data);
  161. // Get the email information.
  162. $toEmail = $user->email;
  163. $subject = $template->getTitle();
  164. $message = $template->getHtml();
  165. // Send the password reset request e-mail.
  166. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $toEmail, $subject, $message);
  167. // Check for an error.
  168. if ($return !== true) {
  169. return new JException(JText::_('USERS_MAIL_FAILED'), 500);
  170. }
  171. return true;
  172. }
  173. function processResetConfirm($data)
  174. {
  175. // Get the form.
  176. $form = &$this->getResetConfirmForm();
  177. // Check for an error.
  178. if (JError::isError($form)) {
  179. return $form;
  180. }
  181. // Filter and validate the form data.
  182. $data = $form->filter($data);
  183. $return = $form->validate($data);
  184. // Check for an error.
  185. if (JError::isError($return)) {
  186. return $return;
  187. }
  188. // Check the validation results.
  189. if ($return === false) {
  190. // Get the validation messages from the form.
  191. foreach ($form->getErrors() as $message) {
  192. $this->setError($message);
  193. }
  194. return false;
  195. }
  196. // Find the user id for the given token.
  197. $db = $this->getDbo();
  198. $query = $db->getQuery(true);
  199. $query->select('*');
  200. $query->from('`#__users`');
  201. $query->where('`activation` = '.$db->Quote($data['token']));
  202. // Get the user id.
  203. $db->setQuery((string) $query);
  204. $user = $db->loadObject();
  205. // Check for an error.
  206. if ($db->getErrorNum()) {
  207. return new JException(JText::sprintf('USERS_DATABASE_ERROR', $db->getErrorMsg()), 500);
  208. }
  209. // Check for a user.
  210. if (empty($user)) {
  211. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  212. return false;
  213. }
  214. // Make sure the user isn't blocked.
  215. if ($user->block) {
  216. $this->setError(JText::_('USERS_USER_BLOCKED'));
  217. return false;
  218. }
  219. // Push the user data into the session.
  220. $app = &JFactory::getApplication();
  221. $app->setUserState('com_users.reset.token', $data['token']);
  222. $app->setUserState('com_users.reset.user', $user->id);
  223. return true;
  224. }
  225. function processResetComplete($data)
  226. {
  227. // Get the form.
  228. $form = &$this->getResetCompleteForm();
  229. // Check for an error.
  230. if (JError::isError($form)) {
  231. return $form;
  232. }
  233. // Filter and validate the form data.
  234. $data = $form->filter($data);
  235. $return = $form->validate($data);
  236. // Check for an error.
  237. if (JError::isError($return)) {
  238. return $return;
  239. }
  240. // Check the validation results.
  241. if ($return === false) {
  242. // Get the validation messages from the form.
  243. foreach ($form->getErrors() as $message) {
  244. $this->setError($message);
  245. }
  246. return false;
  247. }
  248. // Get the token and user id from the confirmation process.
  249. $app = &JFactory::getApplication();
  250. $token = $app->getUserState('com_users.reset.token', null);
  251. $userId = $app->getUserState('com_users.reset.user', null);
  252. // Check the token and user id.
  253. if (empty($token) || empty($userId)) {
  254. return new JException(JText::_('USERS_RESET_COMPLETE_TOKENS_MISSING'), 403);
  255. }
  256. // Get the user object.
  257. $user = JUser::getInstance($userId);
  258. // Check for a user and that the tokens match.
  259. if (empty($user) || $user->activation !== $token) {
  260. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  261. return false;
  262. }
  263. // Make sure the user isn't blocked.
  264. if ($user->block) {
  265. $this->setError(JText::_('USERS_USER_BLOCKED'));
  266. return false;
  267. }
  268. // Generate the new password hash.
  269. jimport('joomla.user.helper');
  270. $salt = JUserHelper::genRandomPassword(32);
  271. $crypted = JUserHelper::getCryptedPassword($data['password1'], $salt);
  272. $password = $crypted.':'.$salt;
  273. // Update the user object.
  274. $user->password = $password;
  275. $user->activation = '';
  276. $user->password_clear = $data['password1'];
  277. // Save the user to the database.
  278. if (!$user->save(true)) {
  279. return new JException(JText::sprintf('USERS_USER_SAVE_FAILED', $user->getError()), 500);
  280. }
  281. // Flush the user data from the session.
  282. $app->setUserState('com_users.reset.token', null);
  283. $app->setUserState('com_users.reset.user', null);
  284. return true;
  285. }
  286. }