PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/components/com_users/models/user.php

https://github.com/cosmocommerce/joomla
PHP | 511 lines | 280 code | 89 blank | 142 comment | 39 complexity | 678c9423abcc826dd097451257f13dc0 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id$
  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. jimport('joomla.plugin.helper');
  13. /**
  14. * User model class for Users.
  15. *
  16. * @package Joomla.Site
  17. * @subpackage com_users
  18. * @version 1.0
  19. */
  20. class UsersModelUser extends JModelForm
  21. {
  22. /**
  23. * Method to auto-populate the model state.
  24. *
  25. * @since 1.6
  26. */
  27. protected function _populateState($property = null, $default = null)
  28. {
  29. // Get the application object.
  30. $app = &JFactory::getApplication();
  31. $user = &JFactory::getUser();
  32. $params = &$app->getParams('com_users');
  33. // Get the member id.
  34. $memberId = JRequest::getInt('member_id', $app->getUserState('com_users.edit.profile.id'));
  35. $memberId = !empty($memberId) ? $memberId : (int)$user->get('id');
  36. // Set the member id.
  37. $this->setState('member.id', $memberId);
  38. // Load the parameters.
  39. $this->setState('params', $params);
  40. }
  41. /**
  42. * Method to get the login form.
  43. *
  44. * The base form is loaded from XML and then an event is fired
  45. * for users plugins to extend the form with extra fields.
  46. *
  47. * @access public
  48. * @param string $type The type of form to load (view, model);
  49. * @return mixed JForm object on success, false on failure.
  50. * @since 1.0
  51. */
  52. function &getLoginForm()
  53. {
  54. // Set the form loading options.
  55. $options = array(
  56. 'array' => false,
  57. 'event' => 'onPrepareUsersLoginForm',
  58. 'group' => 'users'
  59. );
  60. // Get the form.
  61. $form = $this->getForm('login', 'com_users.login', $options);
  62. // Check for an error.
  63. if (JError::isError($form)) {
  64. return $form;
  65. }
  66. // Check the session for previously entered login form data.
  67. $app = &JFactory::getApplication();
  68. $data = $app->getUserState('users.login.form.data', array());
  69. // check for return URL from the request first
  70. if ($return = JRequest::getVar('return', '', 'method', 'base64')) {
  71. $data['return'] = base64_decode($return);
  72. if (!JURI::isInternal($data['return'])) {
  73. $data['return'] = '';
  74. }
  75. }
  76. // Set the return URL if empty.
  77. if (!isset($data['return']) || empty($data['return'])) {
  78. $data['return'] = 'index.php?option=com_users&view=profile';
  79. }
  80. $app->setUserState('users.login.form.data', $data);
  81. // Bind the form data if present.
  82. if (!empty($data)) {
  83. $form->bind($data);
  84. }
  85. return $form;
  86. }
  87. /**
  88. * Method to get the username remind request form.
  89. *
  90. * @access public
  91. * @return object JForm object on success, JException on failure.
  92. * @since 1.0
  93. */
  94. function &getRemindForm()
  95. {
  96. // Set the form loading options.
  97. $options = array(
  98. 'array' => true,
  99. 'event' => 'onPrepareUsersRemindForm',
  100. 'group' => 'users'
  101. );
  102. // Get the form.
  103. return $this->getForm('remind', 'com_users.remind', $options);
  104. }
  105. /**
  106. * Method to get the password reset request form.
  107. *
  108. * @access public
  109. * @return object JForm object on success, JException on failure.
  110. * @since 1.0
  111. */
  112. function &getResetRequestForm()
  113. {
  114. // Set the form loading options.
  115. $options = array(
  116. 'array' => true,
  117. 'event' => 'onPrepareUsersResetRequestForm',
  118. 'group' => 'users'
  119. );
  120. // Get the form.
  121. return $this->getForm('reset_request', 'com_users.reset_request', $options);
  122. }
  123. /**
  124. * Method to get the password reset confirm form.
  125. *
  126. * @access public
  127. * @return object JForm object on success, JException on failure.
  128. * @since 1.0
  129. */
  130. function &getResetConfirmForm()
  131. {
  132. // Set the form loading options.
  133. $options = array(
  134. 'array' => true,
  135. 'event' => 'onPrepareUsersResetConfirmForm',
  136. 'group' => 'users'
  137. );
  138. // Get the form.
  139. return $this->getForm('reset_confirm', 'com_users.reset_confirm', $options);
  140. }
  141. /**
  142. * Method to get the password reset complete form.
  143. *
  144. * @access public
  145. * @return object JForm object on success, JException on failure.
  146. * @since 1.0
  147. */
  148. function &getResetCompleteForm()
  149. {
  150. // Set the form loading options.
  151. $options = array(
  152. 'array' => true,
  153. 'event' => 'onPrepareUsersResetCompleteForm',
  154. 'group' => 'users'
  155. );
  156. // Get the form.
  157. return $this->getForm('reset_complete', 'com_users.reset_complete', $options);
  158. }
  159. function processRemindRequest($data)
  160. {
  161. // Get the form.
  162. $form = &$this->getRemindForm();
  163. // Check for an error.
  164. if (JError::isError($form)) {
  165. return $form;
  166. }
  167. // Validate the data.
  168. $data = $this->validate($form, $data);
  169. // Check the validator results.
  170. if (JError::isError($data) || $data === false) {
  171. return $data;
  172. }
  173. // Find the user id for the given e-mail address.
  174. $db = $this->getDbo();
  175. $query = $db->getQuery(true);
  176. $query->select('*');
  177. $query->from('`#__users`');
  178. $query->where('`email` = '.$db->Quote($data['email']));
  179. // Get the user id.
  180. $db->setQuery((string) $query);
  181. $user = $db->loadObject();
  182. // Check for an error.
  183. if ($db->getErrorNum()) {
  184. return new JException(JText::sprintf('USERS_DATABASE_ERROR', $db->getErrorMsg()), 500);
  185. }
  186. // Check for a user.
  187. if (empty($user)) {
  188. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  189. return false;
  190. }
  191. // Make sure the user isn't blocked.
  192. if ($user->block) {
  193. $this->setError(JText::_('USERS_USER_BLOCKED'));
  194. return false;
  195. }
  196. $config = &JFactory::getConfig();
  197. // Assemble the login link.
  198. $itemid = UsersHelperRoute::getLoginRoute();
  199. $itemid = $itemid !== null ? '&Itemid='.$itemid : '';
  200. $link = 'index.php?option=com_users&view=login'.$itemid;
  201. $mode = $config->getValue('force_ssl', 0) == 2 ? 1 : -1;
  202. // Put together the e-mail template data.
  203. $data = JArrayHelper::fromObject($user);
  204. $data['fromname'] = $config->getValue('fromname');
  205. $data['mailfrom'] = $config->getValue('mailfrom');
  206. $data['sitename'] = $config->getValue('sitename');
  207. $data['link_text'] = JRoute::_($link, false, $mode);
  208. $data['link_html'] = JRoute::_($link, true, $mode);
  209. // Load the mail template.
  210. jimport('joomla.utilities.simpletemplate');
  211. $template = new JSimpleTemplate();
  212. if (!$template->load('users.username.remind.request')) {
  213. return new JException(JText::_('USERS_REMIND_MAIL_TEMPLATE_NOT_FOUND'), 500);
  214. }
  215. // Push in the email template variables.
  216. $template->bind($data);
  217. // Get the email information.
  218. $toEmail = $user->email;
  219. $subject = $template->getTitle();
  220. $message = $template->getHtml();
  221. // Send the password reset request e-mail.
  222. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $toEmail, $subject, $message);
  223. // Check for an error.
  224. if ($return !== true) {
  225. return new JException(JText::_('USERS_MAIL_FAILED'), 500);
  226. }
  227. return true;
  228. }
  229. /**
  230. * Method to start the password reset process.
  231. */
  232. function processResetRequest($data)
  233. {
  234. $config = &JFactory::getConfig();
  235. // Get the form.
  236. $form = &$this->getResetRequestForm();
  237. // Check for an error.
  238. if (JError::isError($form)) {
  239. return $form;
  240. }
  241. // Filter and validate the form data.
  242. $data = $form->filter($data);
  243. $return = $form->validate($data);
  244. // Check for an error.
  245. if (JError::isError($return)) {
  246. return $return;
  247. }
  248. // Check the validation results.
  249. if ($return === false) {
  250. // Get the validation messages from the form.
  251. foreach ($form->getErrors() as $message) {
  252. $this->setError($message);
  253. }
  254. return false;
  255. }
  256. // Get the user id.
  257. jimport('joomla.user.helper');
  258. $userId = JUserHelper::getUserId($data['username']);
  259. // Make sure the user exists.
  260. if (empty($userId)) {
  261. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  262. return false;
  263. }
  264. // Get the user object.
  265. $user = JUser::getInstance($userId);
  266. // Make sure the user isn't blocked.
  267. if ($user->block) {
  268. $this->setError(JText::_('USERS_USER_BLOCKED'));
  269. return false;
  270. }
  271. // Set the confirmation token.
  272. $token = JUtility::getHash(JUserHelper::genRandomPassword());
  273. $user->activation = $token;
  274. // Save the user to the database.
  275. if (!$user->save(true)) {
  276. return new JException(JText::sprintf('USERS_USER_SAVE_FAILED', $user->getError()), 500);
  277. }
  278. // Assemble the password reset confirmation link.
  279. $mode = $config->getValue('force_ssl', 0) == 2 ? 1 : -1;
  280. $link = 'index.php?option=com_users&task=reset.confirm&username='.$user->username.'&token='.$token.'&'.JUtility::getToken(true).'=1';
  281. // Put together the e-mail template data.
  282. $data = $user->getProperties();
  283. $data['fromname'] = $config->getValue('fromname');
  284. $data['mailfrom'] = $config->getValue('mailfrom');
  285. $data['sitename'] = $config->getValue('sitename');
  286. $data['link_text'] = JRoute::_($link, false, $mode);
  287. $data['link_html'] = JRoute::_($link, true, $mode);
  288. $data['token'] = $token;
  289. // Load the mail template.
  290. jimport('joomla.utilities.simpletemplate');
  291. $template = new JSimpleTemplate();
  292. if (!$template->load('users.password.reset.request')) {
  293. return new JException(JText::_('USERS_RESET_MAIL_TEMPLATE_NOT_FOUND'), 500);
  294. }
  295. // Push in the email template variables.
  296. $template->bind($data);
  297. // Get the email information.
  298. $toEmail = $user->email;
  299. $subject = $template->getTitle();
  300. $message = $template->getHtml();
  301. // Send the password reset request e-mail.
  302. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $toEmail, $subject, $message);
  303. // Check for an error.
  304. if ($return !== true) {
  305. return new JException(JText::_('USERS_MAIL_FAILED'), 500);
  306. }
  307. return true;
  308. }
  309. function processResetConfirm($data)
  310. {
  311. // Get the form.
  312. $form = &$this->getResetConfirmForm();
  313. // Check for an error.
  314. if (JError::isError($form)) {
  315. return $form;
  316. }
  317. // Filter and validate the form data.
  318. $data = $form->filter($data);
  319. $return = $form->validate($data);
  320. // Check for an error.
  321. if (JError::isError($return)) {
  322. return $return;
  323. }
  324. // Check the validation results.
  325. if ($return === false) {
  326. // Get the validation messages from the form.
  327. foreach ($form->getErrors() as $message) {
  328. $this->setError($message);
  329. }
  330. return false;
  331. }
  332. // Find the user id for the given token.
  333. $db = $this->getDbo();
  334. $query = $db->getQuery(true);
  335. $query->select('*');
  336. $query->from('`#__users`');
  337. $query->where('`activation` = '.$db->Quote($data['token']));
  338. // Get the user id.
  339. $db->setQuery((string) $query);
  340. $user = $db->loadObject();
  341. // Check for an error.
  342. if ($db->getErrorNum()) {
  343. return new JException(JText::sprintf('USERS_DATABASE_ERROR', $db->getErrorMsg()), 500);
  344. }
  345. // Check for a user.
  346. if (empty($user)) {
  347. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  348. return false;
  349. }
  350. // Make sure the user isn't blocked.
  351. if ($user->block) {
  352. $this->setError(JText::_('USERS_USER_BLOCKED'));
  353. return false;
  354. }
  355. // Push the user data into the session.
  356. $app = &JFactory::getApplication();
  357. $app->setUserState('com_users.reset.token', $data['token']);
  358. $app->setUserState('com_users.reset.user', $user->id);
  359. return true;
  360. }
  361. function processResetComplete($data)
  362. {
  363. // Get the form.
  364. $form = &$this->getResetCompleteForm();
  365. // Check for an error.
  366. if (JError::isError($form)) {
  367. return $form;
  368. }
  369. // Filter and validate the form data.
  370. $data = $form->filter($data);
  371. $return = $form->validate($data);
  372. // Check for an error.
  373. if (JError::isError($return)) {
  374. return $return;
  375. }
  376. // Check the validation results.
  377. if ($return === false) {
  378. // Get the validation messages from the form.
  379. foreach ($form->getErrors() as $message) {
  380. $this->setError($message);
  381. }
  382. return false;
  383. }
  384. // Get the token and user id from the confirmation process.
  385. $app = &JFactory::getApplication();
  386. $token = $app->getUserState('com_users.reset.token', null);
  387. $userId = $app->getUserState('com_users.reset.user', null);
  388. // Check the token and user id.
  389. if (empty($token) || empty($userId)) {
  390. return new JException(JText::_('USERS_RESET_COMPLETE_TOKENS_MISSING'), 403);
  391. }
  392. // Get the user object.
  393. $user = JUser::getInstance($userId);
  394. // Check for a user and that the tokens match.
  395. if (empty($user) || $user->activation !== $token) {
  396. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  397. return false;
  398. }
  399. // Make sure the user isn't blocked.
  400. if ($user->block) {
  401. $this->setError(JText::_('USERS_USER_BLOCKED'));
  402. return false;
  403. }
  404. // Generate the new password hash.
  405. jimport('joomla.user.helper');
  406. $salt = JUserHelper::genRandomPassword(32);
  407. $crypted = JUserHelper::getCryptedPassword($data['password1'], $salt);
  408. $password = $crypted.':'.$salt;
  409. // Update the user object.
  410. $user->password = $password;
  411. $user->activation = '';
  412. $user->password_clear = $data['password1'];
  413. // Save the user to the database.
  414. if (!$user->save(true)) {
  415. return new JException(JText::sprintf('USERS_USER_SAVE_FAILED', $user->getError()), 500);
  416. }
  417. // Flush the user data from the session.
  418. $app->setUserState('com_users.reset.token', null);
  419. $app->setUserState('com_users.reset.user', null);
  420. return true;
  421. }
  422. }