/components/com_users/controllers/user.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 246 lines · 134 code · 36 blank · 76 comment · 13 complexity · a797b003a0d43ae297c212b1e7e6ab39 MD5 · raw file

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