PageRenderTime 26ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 0ms app.codeStats 0ms

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