PageRenderTime 32ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_users/models/reset.php

https://github.com/sengann/iks-school.com
PHP | 393 lines | 216 code | 62 blank | 115 comment | 30 complexity | 166f3f12ec9553e330622b587d106d95 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: reset.php 18675 2010-08-27 04:53:20Z eddieajau $
  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. * @since 1.5
  18. */
  19. class UsersModelReset extends JModelForm
  20. {
  21. /**
  22. * Method to get the password reset request form.
  23. *
  24. * @param array $data Data for the form.
  25. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  26. * @return JForm A JForm object on success, false on failure
  27. * @since 1.6
  28. */
  29. public function getForm($data = array(), $loadData = true)
  30. {
  31. // Get the form.
  32. $form = $this->loadForm('com_users.reset_request', 'reset_request', array('control' => 'jform', 'load_data' => $loadData));
  33. if (empty($form)) {
  34. return false;
  35. }
  36. return $form;
  37. }
  38. /**
  39. * Method to get the password reset complete form.
  40. *
  41. * @param array $data Data for the form.
  42. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  43. * @return JForm A JForm object on success, false on failure
  44. * @since 1.6
  45. */
  46. public function getResetCompleteForm($data = array(), $loadData = true)
  47. {
  48. // Get the form.
  49. $form = $this->loadForm('com_users.reset_complete', 'reset_complete', $options = array('control' => 'jform'));
  50. if (empty($form)) {
  51. return false;
  52. }
  53. return $form;
  54. }
  55. /**
  56. * Method to get the password reset confirm form.
  57. *
  58. * @param array $data Data for the form.
  59. * @param boolean $loadData True if the form is to load its own data (default case), false if not.
  60. * @return JForm A JForm object on success, false on failure
  61. * @since 1.6
  62. */
  63. public function getResetConfirmForm($data = array(), $loadData = true)
  64. {
  65. // Get the form.
  66. $form = $this->loadForm('com_users.reset_confirm', 'reset_confirm', $options = array('control' => 'jform'));
  67. if (empty($form)) {
  68. return false;
  69. }
  70. return $form;
  71. }
  72. /**
  73. * Override preprocessForm to load the user plugin group instead of content.
  74. *
  75. * @param object A form object.
  76. * @param mixed The data expected for the form.
  77. * @throws Exception if there is an error in the form event.
  78. * @since 1.6
  79. */
  80. protected function preprocessForm(JForm $form, $data)
  81. {
  82. parent::preprocessForm($form, $data, 'user');
  83. }
  84. /**
  85. * Method to auto-populate the model state.
  86. *
  87. * Note. Calling getState in this method will result in recursion.
  88. *
  89. * @since 1.6
  90. */
  91. protected function populateState()
  92. {
  93. // Get the application object.
  94. $app = JFactory::getApplication();
  95. $params = $app->getParams('com_users');
  96. // Load the parameters.
  97. $this->setState('params', $params);
  98. }
  99. /**
  100. * @since 1.6
  101. */
  102. function processResetComplete($data)
  103. {
  104. // Get the form.
  105. $form = $this->getResetCompleteForm();
  106. // Check for an error.
  107. if (JError::isError($form)) {
  108. return $form;
  109. }
  110. // Filter and validate the form data.
  111. $data = $form->filter($data);
  112. $return = $form->validate($data);
  113. // Check for an error.
  114. if (JError::isError($return)) {
  115. return $return;
  116. }
  117. // Check the validation results.
  118. if ($return === false) {
  119. // Get the validation messages from the form.
  120. foreach ($form->getErrors() as $message) {
  121. $this->setError($message);
  122. }
  123. return false;
  124. }
  125. // Get the token and user id from the confirmation process.
  126. $app = JFactory::getApplication();
  127. $token = $app->getUserState('com_users.reset.token', null);
  128. $userId = $app->getUserState('com_users.reset.user', null);
  129. // Check the token and user id.
  130. if (empty($token) || empty($userId)) {
  131. return new JException(JText::_('COM_USERS_RESET_COMPLETE_TOKENS_MISSING'), 403);
  132. }
  133. // Get the user object.
  134. $user = JUser::getInstance($userId);
  135. // Check for a user and that the tokens match.
  136. if (empty($user) || $user->activation !== $token) {
  137. $this->setError(JText::_('COM_USERS_USER_NOT_FOUND'));
  138. return false;
  139. }
  140. // Make sure the user isn't blocked.
  141. if ($user->block) {
  142. $this->setError(JText::_('COM_USERS_USER_BLOCKED'));
  143. return false;
  144. }
  145. // Generate the new password hash.
  146. jimport('joomla.user.helper');
  147. $salt = JUserHelper::genRandomPassword(32);
  148. $crypted = JUserHelper::getCryptedPassword($data['password1'], $salt);
  149. $password = $crypted.':'.$salt;
  150. // Update the user object.
  151. $user->password = $password;
  152. $user->activation = '';
  153. $user->password_clear = $data['password1'];
  154. // Save the user to the database.
  155. if (!$user->save(true)) {
  156. return new JException(JText::sprintf('COM_USERS_USER_SAVE_FAILED', $user->getError()), 500);
  157. }
  158. // Flush the user data from the session.
  159. $app->setUserState('com_users.reset.token', null);
  160. $app->setUserState('com_users.reset.user', null);
  161. return true;
  162. }
  163. /**
  164. * @since 1.6
  165. */
  166. function processResetConfirm($data)
  167. {
  168. jimport('joomla.user.helper');
  169. // Get the form.
  170. $form = $this->getResetConfirmForm();
  171. // Check for an error.
  172. if (JError::isError($form)) {
  173. return $form;
  174. }
  175. // Filter and validate the form data.
  176. $data = $form->filter($data);
  177. $return = $form->validate($data);
  178. // Check for an error.
  179. if (JError::isError($return)) {
  180. return $return;
  181. }
  182. // Check the validation results.
  183. if ($return === false) {
  184. // Get the validation messages from the form.
  185. foreach ($form->getErrors() as $message) {
  186. $this->setError($message);
  187. }
  188. return false;
  189. }
  190. // Find the user id for the given token.
  191. $db = $this->getDbo();
  192. $query = $db->getQuery(true);
  193. $query->select('activation');
  194. $query->select('id');
  195. $query->select('block');
  196. $query->from('`#__users`');
  197. $query->where('`username` = '.$db->Quote($data['username']));
  198. // Get the user id.
  199. $db->setQuery((string) $query);
  200. $user = $db->loadObject();
  201. // Check for an error.
  202. if ($db->getErrorNum()) {
  203. return new JException(JText::sprintf('COM_USERS_DATABASE_ERROR', $db->getErrorMsg()), 500);
  204. }
  205. // Check for a user.
  206. if (empty($user)) {
  207. $this->setError(JText::_('COM_USERS_USER_NOT_FOUND'));
  208. return false;
  209. }
  210. $parts = explode( ':', $user->activation );
  211. $crypt = $parts[0];
  212. if (!isset($parts[1])) {
  213. $this->setError(JText::_('COM_USERS_USER_NOT_FOUND'));
  214. return false;
  215. }
  216. $salt = $parts[1];
  217. $testcrypt = JUserHelper::getCryptedPassword($data['token'], $salt);
  218. // Verify the token
  219. if (!($crypt == $testcrypt))
  220. {
  221. $this->setError(JText::_('COM_USERS_USER_NOT_FOUND'));
  222. return false;
  223. }
  224. // Make sure the user isn't blocked.
  225. if ($user->block) {
  226. $this->setError(JText::_('COM_USERS_USER_BLOCKED'));
  227. return false;
  228. }
  229. // Push the user data into the session.
  230. $app = JFactory::getApplication();
  231. $app->setUserState('com_users.reset.token', $crypt.':'.$salt);
  232. $app->setUserState('com_users.reset.user', $user->id);
  233. return true;
  234. }
  235. /**
  236. * Method to start the password reset process.
  237. *
  238. * @since 1.6
  239. */
  240. public function processResetRequest($data)
  241. {
  242. $config = JFactory::getConfig();
  243. // Get the form.
  244. $form = $this->getForm();
  245. // Check for an error.
  246. if (JError::isError($form)) {
  247. return $form;
  248. }
  249. // Filter and validate the form data.
  250. $data = $form->filter($data);
  251. $return = $form->validate($data);
  252. // Check for an error.
  253. if (JError::isError($return)) {
  254. return $return;
  255. }
  256. // Check the validation results.
  257. if ($return === false) {
  258. // Get the validation messages from the form.
  259. foreach ($form->getErrors() as $message) {
  260. $this->setError($message);
  261. }
  262. return false;
  263. }
  264. jimport('joomla.user.helper');
  265. // Find the user id for the given email address.
  266. $db = $this->getDbo();
  267. $query = $db->getQuery(true);
  268. $query->select('id');
  269. $query->from('`#__users`');
  270. $query->where('`email` = '.$db->Quote($data['email']));
  271. // Get the user object.
  272. $db->setQuery((string) $query);
  273. $userId = $db->loadResult();
  274. // Check for an error.
  275. if ($db->getErrorNum()) {
  276. $this->setError(JText::sprintf('COM_USERS_DATABASE_ERROR', $db->getErrorMsg()), 500);
  277. return false;
  278. }
  279. // Check for a user.
  280. if (empty($userId)) {
  281. $this->setError(JText::_('COM_USERS_INVALID_EMAIL'));
  282. return false;
  283. }
  284. // Get the user object.
  285. $user = JUser::getInstance($userId);
  286. // Make sure the user isn't blocked.
  287. if ($user->block) {
  288. $this->setError(JText::_('COM_USERS_USER_BLOCKED'));
  289. return false;
  290. }
  291. // Set the confirmation token.
  292. $token = JUtility::getHash(JUserHelper::genRandomPassword());
  293. $salt = JUserHelper::getSalt('crypt-md5');
  294. $hashedToken = md5($token.$salt).':'.$salt;
  295. $user->activation = $hashedToken;
  296. // Save the user to the database.
  297. if (!$user->save(true)) {
  298. return new JException(JText::sprintf('COM_USERS_USER_SAVE_FAILED', $user->getError()), 500);
  299. }
  300. // Assemble the password reset confirmation link.
  301. $mode = $config->get('force_ssl', 0) == 2 ? 1 : -1;
  302. $itemid = UsersHelperRoute::getLoginRoute();
  303. $itemid = $itemid !== null ? '&Itemid='.$itemid : '';
  304. $link = 'index.php?option=com_users&view=reset&layout=confirm'.$itemid;
  305. // Put together the email template data.
  306. $data = $user->getProperties();
  307. $data['fromname'] = $config->get('fromname');
  308. $data['mailfrom'] = $config->get('mailfrom');
  309. $data['sitename'] = $config->get('sitename');
  310. $data['link_text'] = JRoute::_($link, false, $mode);
  311. $data['link_html'] = JRoute::_($link, true, $mode);
  312. $data['token'] = $token;
  313. $subject = JText::sprintf(
  314. 'COM_USERS_EMAIL_PASSWORD_RESET_SUBJECT',
  315. $data['sitename']
  316. );
  317. $body = JText::sprintf(
  318. 'COM_USERS_EMAIL_PASSWORD_RESET_BODY',
  319. $data['sitename'],
  320. $data['token'],
  321. $data['link_text']
  322. );
  323. // Send the password reset request email.
  324. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $user->email, $subject, $body);
  325. // Check for an error.
  326. if ($return !== true) {
  327. return new JException(JText::_('COM_USERS_MAIL_FAILED'), 500);
  328. }
  329. return true;
  330. }
  331. }