/components/com_users/controllers/user.php

https://bitbucket.org/eternaware/joomus · PHP · 229 lines · 117 code · 36 blank · 76 comment · 16 complexity · b5dadb6cd7a2b459b699d0eb0bd77b3a MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_users
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 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. require_once JPATH_COMPONENT.'/controller.php';
  11. /**
  12. * Registration controller class for Users.
  13. *
  14. * @package Joomla.Site
  15. * @subpackage com_users
  16. * @since 1.6
  17. */
  18. class UsersControllerUser extends UsersController
  19. {
  20. /**
  21. * Method to log in a user.
  22. *
  23. * @since 1.6
  24. */
  25. public function login()
  26. {
  27. JSession::checkToken('post') or jexit(JText::_('JInvalid_Token'));
  28. $app = JFactory::getApplication();
  29. // Populate the data array:
  30. $data = array();
  31. $data['return'] = base64_decode($app->input->post->get('return', '', 'BASE64'));
  32. $data['username'] = JRequest::getVar('username', '', 'method', 'username');
  33. $data['password'] = JRequest::getString('password', '', 'post', JREQUEST_ALLOWRAW);
  34. // Set the return URL if empty.
  35. if (empty($data['return'])) {
  36. $data['return'] = 'index.php?option=com_users&view=profile';
  37. }
  38. // Set the return URL in the user state to allow modification by plugins
  39. $app->setUserState('users.login.form.return', $data['return']);
  40. // Get the log in options.
  41. $options = array();
  42. $options['remember'] = $this->input->getBool('remember', false);
  43. $options['return'] = $data['return'];
  44. // Get the log in credentials.
  45. $credentials = array();
  46. $credentials['username'] = $data['username'];
  47. $credentials['password'] = $data['password'];
  48. // Perform the log in.
  49. if (true === $app->login($credentials, $options)) {
  50. // Success
  51. $app->setUserState('users.login.form.data', array());
  52. $app->redirect(JRoute::_($app->getUserState('users.login.form.return'), false));
  53. } else {
  54. // Login failed !
  55. $data['remember'] = (int) $options['remember'];
  56. $app->setUserState('users.login.form.data', $data);
  57. $app->redirect(JRoute::_('index.php?option=com_users&view=login', false));
  58. }
  59. }
  60. /**
  61. * Method to log out a user.
  62. *
  63. * @since 1.6
  64. */
  65. public function logout()
  66. {
  67. JSession::checkToken('request') or jexit(JText::_('JInvalid_Token'));
  68. $app = JFactory::getApplication();
  69. // Perform the log in.
  70. $error = $app->logout();
  71. // Check if the log out succeeded.
  72. if (!($error instanceof Exception)) {
  73. // Get the return url from the request and validate that it is internal.
  74. $return = JRequest::getVar('return', '', 'method', 'base64');
  75. $return = base64_decode($return);
  76. if (!JURI::isInternal($return)) {
  77. $return = '';
  78. }
  79. // Redirect the user.
  80. $app->redirect(JRoute::_($return, false));
  81. } else {
  82. $app->redirect(JRoute::_('index.php?option=com_users&view=login', false));
  83. }
  84. }
  85. /**
  86. * Method to register a user.
  87. *
  88. * @since 1.6
  89. */
  90. public function register()
  91. {
  92. JSession::checkToken('post') or jexit(JText::_('JINVALID_TOKEN'));
  93. // Get the form data.
  94. $data = $this->input->post->get('user', array(), 'array');
  95. // Get the model and validate the data.
  96. $model = $this->getModel('Registration', 'UsersModel');
  97. $return = $model->validate($data);
  98. // Check for errors.
  99. if ($return === false) {
  100. // Get the validation messages.
  101. $app = &JFactory::getApplication();
  102. $errors = $model->getErrors();
  103. // Push up to three validation messages out to the user.
  104. for ($i = 0, $n = count($errors); $i < $n && $i < 3; $i++) {
  105. if ($errors[$i] instanceof Exception) {
  106. $app->enqueueMessage($errors[$i]->getMessage(), 'notice');
  107. } else {
  108. $app->enqueueMessage($errors[$i], 'notice');
  109. }
  110. }
  111. // Save the data in the session.
  112. $app->setUserState('users.registration.form.data', $data);
  113. // Redirect back to the registration form.
  114. $this->setRedirect('index.php?option=com_users&view=registration');
  115. return false;
  116. }
  117. // Finish the registration.
  118. $return = $model->register($data);
  119. // Check for errors.
  120. if ($return === false) {
  121. // Save the data in the session.
  122. $app->setUserState('users.registration.form.data', $data);
  123. // Redirect back to the registration form.
  124. $message = JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $model->getError());
  125. $this->setRedirect('index.php?option=com_users&view=registration', $message, 'error');
  126. return false;
  127. }
  128. // Flush the data from the session.
  129. $app->setUserState('users.registration.form.data', null);
  130. exit;
  131. }
  132. /**
  133. * Method to login a user.
  134. *
  135. * @since 1.6
  136. */
  137. public function remind()
  138. {
  139. // Check the request token.
  140. JSession::checkToken('post') or jexit(JText::_('JINVALID_TOKEN'));
  141. $app = JFactory::getApplication();
  142. $model = $this->getModel('User', 'UsersModel');
  143. $data = $this->input->post->get('jform', array(), 'array');
  144. // Submit the username remind request.
  145. $return = $model->processRemindRequest($data);
  146. // Check for a hard error.
  147. if ($return instanceof Exception) {
  148. // Get the error message to display.
  149. if ($app->getCfg('error_reporting')) {
  150. $message = $return->getMessage();
  151. } else {
  152. $message = JText::_('COM_USERS_REMIND_REQUEST_ERROR');
  153. }
  154. // Get the route to the next page.
  155. $itemid = UsersHelperRoute::getRemindRoute();
  156. $itemid = $itemid !== null ? '&Itemid='.$itemid : '';
  157. $route = 'index.php?option=com_users&view=remind'.$itemid;
  158. // Go back to the complete form.
  159. $this->setRedirect(JRoute::_($route, false), $message, 'error');
  160. return false;
  161. } elseif ($return === false) {
  162. // Complete failed.
  163. // Get the route to the next page.
  164. $itemid = UsersHelperRoute::getRemindRoute();
  165. $itemid = $itemid !== null ? '&Itemid='.$itemid : '';
  166. $route = 'index.php?option=com_users&view=remind'.$itemid;
  167. // Go back to the complete form.
  168. $message = JText::sprintf('COM_USERS_REMIND_REQUEST_FAILED', $model->getError());
  169. $this->setRedirect(JRoute::_($route, false), $message, 'notice');
  170. return false;
  171. } else {
  172. // Complete succeeded.
  173. // Get the route to the next page.
  174. $itemid = UsersHelperRoute::getLoginRoute();
  175. $itemid = $itemid !== null ? '&Itemid='.$itemid : '';
  176. $route = 'index.php?option=com_users&view=login'.$itemid;
  177. // Proceed to the login form.
  178. $message = JText::_('COM_USERS_REMIND_REQUEST_SUCCESS');
  179. $this->setRedirect(JRoute::_($route, false), $message);
  180. return true;
  181. }
  182. }
  183. /**
  184. * Method to login a user.
  185. *
  186. * @since 1.6
  187. */
  188. public function resend()
  189. {
  190. // Check for request forgeries
  191. JSession::checkToken('post') or jexit(JText::_('JINVALID_TOKEN'));
  192. }
  193. }