- md5
- a0aaa4de5188c4567889e69ebff20d54
- sha1
- 519992645094a8b2b915b43f6d87367df7ad7bbb
- sha256
- f8ba3a48aaaf6c30f026473a345c5ed045022a5f4d4e22b9a6881b53ec83c2e2
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
<?php
/**
* @package Joomla.Site
* @subpackage com_users
*
* @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\Component\Users\Site\Controller;
\defined('_JEXEC') or die;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Language\Multilanguage;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
use Joomla\Database\ParameterType;
/**
* Registration controller class for Users.
*
* @since 1.6
*/
class UserController extends BaseController
{
/**
* Method to log in a user.
*
* @return void
*
* @since 1.6
*/
public function login()
{
$this->checkToken('post');
$input = $this->input->getInputForRequestMethod();
// Populate the data array:
$data = array();
$data['return'] = base64_decode($input->get('return', '', 'BASE64'));
$data['username'] = $input->get('username', '', 'USERNAME');
$data['password'] = $input->get('password', '', 'RAW');
$data['secretkey'] = $input->get('secretkey', '', 'RAW');
// Check for a simple menu item id
if (is_numeric($data['return']))
{
if (Multilanguage::isEnabled())
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->from($db->quoteName('#__menu'))
->where($db->quoteName('client_id') . ' = 0')
->where($db->quoteName('id') . ' = :id')
->bind(':id', $data['return'], ParameterType::INTEGER);
$db->setQuery($query);
try
{
$language = $db->loadResult();
}
catch (\RuntimeException $e)
{
return;
}
if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
}
else
{
$lang = '';
}
$data['return'] = 'index.php?Itemid=' . $data['return'] . $lang;
}
else
{
// Don't redirect to an external URL.
if (!Uri::isInternal($data['return']))
{
$data['return'] = '';
}
}
// Set the return URL if empty.
if (empty($data['return']))
{
$data['return'] = 'index.php?option=com_users&view=profile';
}
// Set the return URL in the user state to allow modification by plugins
$this->app->setUserState('users.login.form.return', $data['return']);
// Get the log in options.
$options = array();
$options['remember'] = $this->input->getBool('remember', false);
$options['return'] = $data['return'];
// Get the log in credentials.
$credentials = array();
$credentials['username'] = $data['username'];
$credentials['password'] = $data['password'];
$credentials['secretkey'] = $data['secretkey'];
// Perform the log in.
if (true !== $this->app->login($credentials, $options))
{
// Login failed !
// Clear user name, password and secret key before sending the login form back to the user.
$data['remember'] = (int) $options['remember'];
$data['username'] = '';
$data['password'] = '';
$data['secretkey'] = '';
$this->app->setUserState('users.login.form.data', $data);
$this->app->redirect(Route::_('index.php?option=com_users&view=login', false));
}
// Success
if ($options['remember'] == true)
{
$this->app->setUserState('rememberLogin', true);
}
$this->app->setUserState('users.login.form.data', array());
$this->app->redirect(Route::_($this->app->getUserState('users.login.form.return'), false));
}
/**
* Method to log out a user.
*
* @return void
*
* @since 1.6
*/
public function logout()
{
$this->checkToken('request');
$app = $this->app;
// Prepare the logout options.
$options = array(
'clientid' => $app->get('shared_session', '0') ? null : 0,
);
// Perform the log out.
$error = $app->logout(null, $options);
$input = $app->input->getInputForRequestMethod();
// Check if the log out succeeded.
if ($error instanceof \Exception)
{
$app->redirect(Route::_('index.php?option=com_users&view=login', false));
}
// Get the return URL from the request and validate that it is internal.
$return = $input->get('return', '', 'BASE64');
$return = base64_decode($return);
// Check for a simple menu item id
if (is_numeric($return))
{
if (Multilanguage::isEnabled())
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->from($db->quoteName('#__menu'))
->where($db->quoteName('client_id') . ' = 0')
->where($db->quoteName('id') . ' = :id')
->bind(':id', $return, ParameterType::INTEGER);
$db->setQuery($query);
try
{
$language = $db->loadResult();
}
catch (\RuntimeException $e)
{
return;
}
if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
}
else
{
$lang = '';
}
$return = 'index.php?Itemid=' . $return . $lang;
}
else
{
// Don't redirect to an external URL.
if (!Uri::isInternal($return))
{
$return = '';
}
}
// In case redirect url is not set, redirect user to homepage
if (empty($return))
{
$return = Uri::root();
}
// Redirect the user.
$app->redirect(Route::_($return, false));
}
/**
* Method to logout directly and redirect to page.
*
* @return void
*
* @since 3.5
*/
public function menulogout()
{
// Get the ItemID of the page to redirect after logout
$app = $this->app;
$active = $app->getMenu()->getActive();
$itemid = $active ? $active->getParams()->get('logout') : 0;
// Get the language of the page when multilang is on
if (Multilanguage::isEnabled())
{
if ($itemid)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('language'))
->from($db->quoteName('#__menu'))
->where($db->quoteName('client_id') . ' = 0')
->where($db->quoteName('id') . ' = :id')
->bind(':id', $itemid, ParameterType::INTEGER);
$db->setQuery($query);
try
{
$language = $db->loadResult();
}
catch (\RuntimeException $e)
{
return;
}
if ($language !== '*')
{
$lang = '&lang=' . $language;
}
else
{
$lang = '';
}
// URL to redirect after logout
$url = 'index.php?Itemid=' . $itemid . $lang;
}
else
{
// Logout is set to default. Get the home page ItemID
$lang_code = $app->input->cookie->getString(ApplicationHelper::getHash('language'));
$item = $app->getMenu()->getDefault($lang_code);
$itemid = $item->id;
// Redirect to Home page after logout
$url = 'index.php?Itemid=' . $itemid;
}
}
else
{
// URL to redirect after logout, default page if no ItemID is set
$url = $itemid ? 'index.php?Itemid=' . $itemid : Uri::root();
}
// Logout and redirect
$this->setRedirect('index.php?option=com_users&task=user.logout&' . Session::getFormToken() . '=1&return=' . base64_encode($url));
}
/**
* Method to request a username reminder.
*
* @return boolean
*
* @since 1.6
*/
public function remind()
{
// Check the request token.
$this->checkToken('post');
$app = $this->app;
/** @var \Joomla\Component\Users\Site\Model\RemindModel $model */
$model = $this->getModel('Remind', 'Site');
$data = $this->input->post->get('jform', array(), 'array');
// Submit the username remind request.
$return = $model->processRemindRequest($data);
// Check for a hard error.
if ($return instanceof \Exception)
{
// Get the error message to display.
$message = $app->get('error_reporting')
? $return->getMessage()
: Text::_('COM_USERS_REMIND_REQUEST_ERROR');
// Go back to the complete form.
$this->setRedirect(Route::_('index.php?option=com_users&view=remind', false), $message, 'error');
return false;
}
if ($return === false)
{
// Go back to the complete form.
$message = Text::sprintf('COM_USERS_REMIND_REQUEST_FAILED', $model->getError());
$this->setRedirect(Route::_('index.php?option=com_users&view=remind', false), $message, 'notice');
return false;
}
// Proceed to the login form.
$message = Text::_('COM_USERS_REMIND_REQUEST_SUCCESS');
$this->setRedirect(Route::_('index.php?option=com_users&view=login', false), $message);
return true;
}
/**
* Method to resend a user.
*
* @return void
*
* @since 1.6
*/
public function resend()
{
// Check for request forgeries
// $this->checkToken('post');
}
}
The same content is in 48 releases, Joomla 4.0.0-rc1 to 4.2.0-beta1. See them all, or every version of this file.