[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_users/src/Controller/ -> UserController.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_users
   6   *
   7   * @copyright   (C) 2009 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Users\Site\Controller;
  12  
  13  use Joomla\CMS\Application\ApplicationHelper;
  14  use Joomla\CMS\Language\Multilanguage;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\BaseController;
  17  use Joomla\CMS\Router\Route;
  18  use Joomla\CMS\Session\Session;
  19  use Joomla\CMS\Uri\Uri;
  20  
  21  // phpcs:disable PSR1.Files.SideEffects
  22  \defined('_JEXEC') or die;
  23  // phpcs:enable PSR1.Files.SideEffects
  24  
  25  /**
  26   * Registration controller class for Users.
  27   *
  28   * @since  1.6
  29   */
  30  class UserController extends BaseController
  31  {
  32      /**
  33       * Method to log in a user.
  34       *
  35       * @return  void
  36       *
  37       * @since   1.6
  38       */
  39      public function login()
  40      {
  41          $this->checkToken('post');
  42  
  43          $input = $this->input->getInputForRequestMethod();
  44  
  45          // Populate the data array:
  46          $data = array();
  47  
  48          $data['return']    = base64_decode($input->get('return', '', 'BASE64'));
  49          $data['username']  = $input->get('username', '', 'USERNAME');
  50          $data['password']  = $input->get('password', '', 'RAW');
  51          $data['secretkey'] = $input->get('secretkey', '', 'RAW');
  52  
  53          // Check for a simple menu item id
  54          if (is_numeric($data['return'])) {
  55              $itemId = (int) $data['return'];
  56              $data['return'] = 'index.php?Itemid=' . $itemId;
  57  
  58              if (Multilanguage::isEnabled()) {
  59                  $language = $this->getModel('Login', 'Site')->getMenuLanguage($itemId);
  60  
  61                  if ($language !== '*') {
  62                      $data['return'] .= '&lang=' . $language;
  63                  }
  64              }
  65          } elseif (!Uri::isInternal($data['return'])) {
  66              // Don't redirect to an external URL.
  67              $data['return'] = '';
  68          }
  69  
  70          // Set the return URL if empty.
  71          if (empty($data['return'])) {
  72              $data['return'] = 'index.php?option=com_users&view=profile';
  73          }
  74  
  75          // Set the return URL in the user state to allow modification by plugins
  76          $this->app->setUserState('users.login.form.return', $data['return']);
  77  
  78          // Get the log in options.
  79          $options = array();
  80          $options['remember'] = $this->input->getBool('remember', false);
  81          $options['return']   = $data['return'];
  82  
  83          // Get the log in credentials.
  84          $credentials = array();
  85          $credentials['username']  = $data['username'];
  86          $credentials['password']  = $data['password'];
  87          $credentials['secretkey'] = $data['secretkey'];
  88  
  89          // Perform the log in.
  90          if (true !== $this->app->login($credentials, $options)) {
  91              // Login failed !
  92              // Clear user name, password and secret key before sending the login form back to the user.
  93              $data['remember'] = (int) $options['remember'];
  94              $data['username'] = '';
  95              $data['password'] = '';
  96              $data['secretkey'] = '';
  97              $this->app->setUserState('users.login.form.data', $data);
  98              $this->app->redirect(Route::_('index.php?option=com_users&view=login', false));
  99          }
 100  
 101          // Success
 102          if ($options['remember'] == true) {
 103              $this->app->setUserState('rememberLogin', true);
 104          }
 105  
 106          $this->app->setUserState('users.login.form.data', array());
 107          $this->app->redirect(Route::_($this->app->getUserState('users.login.form.return'), false));
 108      }
 109  
 110      /**
 111       * Method to log out a user.
 112       *
 113       * @return  void
 114       *
 115       * @since   1.6
 116       */
 117      public function logout()
 118      {
 119          $this->checkToken('request');
 120  
 121          $app = $this->app;
 122  
 123          // Prepare the logout options.
 124          $options = array(
 125              'clientid' => $app->get('shared_session', '0') ? null : 0,
 126          );
 127  
 128          // Perform the log out.
 129          $error = $app->logout(null, $options);
 130          $input = $app->input->getInputForRequestMethod();
 131  
 132          // Check if the log out succeeded.
 133          if ($error instanceof \Exception) {
 134              $app->redirect(Route::_('index.php?option=com_users&view=login', false));
 135          }
 136  
 137          // Get the return URL from the request and validate that it is internal.
 138          $return = $input->get('return', '', 'BASE64');
 139          $return = base64_decode($return);
 140  
 141          // Check for a simple menu item id
 142          if (is_numeric($return)) {
 143              $return = 'index.php?Itemid=' . $return;
 144  
 145              if (Multilanguage::isEnabled()) {
 146                  $language = $this->getModel('Login', 'Site')->getMenuLanguage($return);
 147  
 148                  if ($language !== '*') {
 149                      $return .= '&lang=' . $language;
 150                  }
 151              }
 152          } elseif (!Uri::isInternal($return)) {
 153              $return = '';
 154          }
 155  
 156          // In case redirect url is not set, redirect user to homepage
 157          if (empty($return)) {
 158              $return = Uri::root();
 159          }
 160  
 161          // Redirect the user.
 162          $app->redirect(Route::_($return, false));
 163      }
 164  
 165      /**
 166       * Method to logout directly and redirect to page.
 167       *
 168       * @return  void
 169       *
 170       * @since   3.5
 171       */
 172      public function menulogout()
 173      {
 174          // Get the ItemID of the page to redirect after logout
 175          $app    = $this->app;
 176          $active = $app->getMenu()->getActive();
 177          $itemid = $active ? $active->getParams()->get('logout') : 0;
 178  
 179          // Get the language of the page when multilang is on
 180          if (Multilanguage::isEnabled()) {
 181              if ($itemid) {
 182                  $language = $this->getModel('Login', 'Site')->getMenuLanguage($itemid);
 183  
 184                  // URL to redirect after logout
 185                  $url = 'index.php?Itemid=' . $itemid . ($language !== '*' ? '&lang=' . $language : '');
 186              } else {
 187                  // Logout is set to default. Get the home page ItemID
 188                  $lang_code = $app->input->cookie->getString(ApplicationHelper::getHash('language'));
 189                  $item      = $app->getMenu()->getDefault($lang_code);
 190                  $itemid    = $item->id;
 191  
 192                  // Redirect to Home page after logout
 193                  $url = 'index.php?Itemid=' . $itemid;
 194              }
 195          } else {
 196              // URL to redirect after logout, default page if no ItemID is set
 197              $url = $itemid ? 'index.php?Itemid=' . $itemid : Uri::root();
 198          }
 199  
 200          // Logout and redirect
 201          $this->setRedirect('index.php?option=com_users&task=user.logout&' . Session::getFormToken() . '=1&return=' . base64_encode($url));
 202      }
 203  
 204      /**
 205       * Method to request a username reminder.
 206       *
 207       * @return  boolean
 208       *
 209       * @since   1.6
 210       */
 211      public function remind()
 212      {
 213          // Check the request token.
 214          $this->checkToken('post');
 215  
 216          $app   = $this->app;
 217  
 218          /** @var \Joomla\Component\Users\Site\Model\RemindModel $model */
 219          $model = $this->getModel('Remind', 'Site');
 220          $data  = $this->input->post->get('jform', array(), 'array');
 221  
 222          // Submit the username remind request.
 223          $return = $model->processRemindRequest($data);
 224  
 225          // Check for a hard error.
 226          if ($return instanceof \Exception) {
 227              // Get the error message to display.
 228              $message = $app->get('error_reporting')
 229                  ? $return->getMessage()
 230                  : Text::_('COM_USERS_REMIND_REQUEST_ERROR');
 231  
 232              // Go back to the complete form.
 233              $this->setRedirect(Route::_('index.php?option=com_users&view=remind', false), $message, 'error');
 234  
 235              return false;
 236          }
 237  
 238          if ($return === false) {
 239              // Go back to the complete form.
 240              $message = Text::sprintf('COM_USERS_REMIND_REQUEST_FAILED', $model->getError());
 241              $this->setRedirect(Route::_('index.php?option=com_users&view=remind', false), $message, 'notice');
 242  
 243              return false;
 244          }
 245  
 246          // Proceed to the login form.
 247          $message = Text::_('COM_USERS_REMIND_REQUEST_SUCCESS');
 248          $this->setRedirect(Route::_('index.php?option=com_users&view=login', false), $message);
 249  
 250          return true;
 251      }
 252  
 253      /**
 254       * Method to resend a user.
 255       *
 256       * @return  void
 257       *
 258       * @since   1.6
 259       */
 260      public function resend()
 261      {
 262          // Check for request forgeries
 263          // $this->checkToken('post');
 264      }
 265  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer