PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_users/models/user.php

https://github.com/joebushi/joomla
PHP | 512 lines | 281 code | 89 blank | 142 comment | 39 complexity | a32801fb141397e838518a67e09c2dff MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  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. $query = new JQuery();
  175. $query->select('*');
  176. $query->from('`#__users`');
  177. $query->where('`email` = '.$this->_db->Quote($data['email']));
  178. // Get the user id.
  179. $this->_db->setQuery((string) $query);
  180. $user = $this->_db->loadObject();
  181. // Check for an error.
  182. if ($this->_db->getErrorNum()) {
  183. return new JException(JText::sprintf('USERS_DATABASE_ERROR', $this->_db->getErrorMsg()), 500);
  184. }
  185. // Check for a user.
  186. if (empty($user)) {
  187. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  188. return false;
  189. }
  190. // Make sure the user isn't blocked.
  191. if ($user->block) {
  192. $this->setError(JText::_('USERS_USER_BLOCKED'));
  193. return false;
  194. }
  195. $config = &JFactory::getConfig();
  196. // Assemble the login link.
  197. $itemid = UsersHelperRoute::getLoginRoute();
  198. $itemid = $itemid !== null ? '&Itemid='.$itemid : '';
  199. $link = 'index.php?option=com_users&view=login'.$itemid;
  200. $mode = $config->getValue('force_ssl', 0) == 2 ? 1 : -1;
  201. // Put together the e-mail template data.
  202. $data = JArrayHelper::fromObject($user);
  203. $data['fromname'] = $config->getValue('fromname');
  204. $data['mailfrom'] = $config->getValue('mailfrom');
  205. $data['sitename'] = $config->getValue('sitename');
  206. $data['link_text'] = JRoute::_($link, false, $mode);
  207. $data['link_html'] = JRoute::_($link, true, $mode);
  208. // Load the mail template.
  209. jimport('joomla.utilities.simpletemplate');
  210. $template = new JSimpleTemplate();
  211. if (!$template->load('users.username.remind.request')) {
  212. return new JException(JText::_('USERS_REMIND_MAIL_TEMPLATE_NOT_FOUND'), 500);
  213. }
  214. // Push in the email template variables.
  215. $template->bind($data);
  216. // Get the email information.
  217. $toEmail = $user->email;
  218. $subject = $template->getTitle();
  219. $message = $template->getHtml();
  220. // Send the password reset request e-mail.
  221. $return = JUtility::sendMail($data['mailfrom'], $data['fromname'], $toEmail, $subject, $message);
  222. // Check for an error.
  223. if ($return !== true) {
  224. return new JException(JText::_('USERS_MAIL_FAILED'), 500);
  225. }
  226. return true;
  227. }
  228. /**
  229. * Method to start the password reset process.
  230. */
  231. function processResetRequest($data)
  232. {
  233. $config = &JFactory::getConfig();
  234. // Get the form.
  235. $form = &$this->getResetRequestForm();
  236. // Check for an error.
  237. if (JError::isError($form)) {
  238. return $form;
  239. }
  240. // Filter and validate the form data.
  241. $data = $form->filter($data);
  242. $return = $form->validate($data);
  243. // Check for an error.
  244. if (JError::isError($return)) {
  245. return $return;
  246. }
  247. // Check the validation results.
  248. if ($return === false)
  249. {
  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. {
  327. // Get the validation messages from the form.
  328. foreach ($form->getErrors() as $message) {
  329. $this->setError($message);
  330. }
  331. return false;
  332. }
  333. // Find the user id for the given token.
  334. $query = new JQuery();
  335. $query->select('*');
  336. $query->from('`#__users`');
  337. $query->where('`activation` = '.$this->_db->Quote($data['token']));
  338. // Get the user id.
  339. $this->_db->setQuery((string) $query);
  340. $user = $this->_db->loadObject();
  341. // Check for an error.
  342. if ($this->_db->getErrorNum()) {
  343. return new JException(JText::sprintf('USERS_DATABASE_ERROR', $this->_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. {
  379. // Get the validation messages from the form.
  380. foreach ($form->getErrors() as $message) {
  381. $this->setError($message);
  382. }
  383. return false;
  384. }
  385. // Get the token and user id from the confirmation process.
  386. $app = &JFactory::getApplication();
  387. $token = $app->getUserState('com_users.reset.token', null);
  388. $userId = $app->getUserState('com_users.reset.user', null);
  389. // Check the token and user id.
  390. if (empty($token) || empty($userId)) {
  391. return new JException(JText::_('USERS_RESET_COMPLETE_TOKENS_MISSING'), 403);
  392. }
  393. // Get the user object.
  394. $user = JUser::getInstance($userId);
  395. // Check for a user and that the tokens match.
  396. if (empty($user) || $user->activation !== $token) {
  397. $this->setError(JText::_('USERS_USER_NOT_FOUND'));
  398. return false;
  399. }
  400. // Make sure the user isn't blocked.
  401. if ($user->block) {
  402. $this->setError(JText::_('USERS_USER_BLOCKED'));
  403. return false;
  404. }
  405. // Generate the new password hash.
  406. jimport('joomla.user.helper');
  407. $salt = JUserHelper::genRandomPassword(32);
  408. $crypted = JUserHelper::getCryptedPassword($data['password1'], $salt);
  409. $password = $crypted.':'.$salt;
  410. // Update the user object.
  411. $user->password = $password;
  412. $user->activation = '';
  413. $user->password_clear = $data['password1'];
  414. // Save the user to the database.
  415. if (!$user->save(true)) {
  416. return new JException(JText::sprintf('USERS_USER_SAVE_FAILED', $user->getError()), 500);
  417. }
  418. // Flush the user data from the session.
  419. $app->setUserState('com_users.reset.token', null);
  420. $app->setUserState('com_users.reset.user', null);
  421. return true;
  422. }
  423. }