Joomla! PHP Cross Reference Web Portals

Source: /components/com_users/models/registration.php - 520 lines - 14968 bytes - Summary - Text - Print

   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  
  10  defined('_JEXEC') or die;
  11  
  12  /**
  13   * Registration model class for Users.
  14   *
  15   * @package     Joomla.Site
  16   * @subpackage  com_users
  17   * @since       1.6
  18   */
  19  class UsersModelRegistration extends JModelForm
  20  {
  21      /**
  22       * @var        object    The user registration data.
  23       * @since    1.6
  24       */
  25      protected $data;
  26  
  27      /**
  28       * Method to activate a user account.
  29       *
  30       * @param    string        The activation token.
  31       * @return    mixed        False on failure, user object on success.
  32       * @since    1.6
  33       */
  34  	public function activate($token)
  35      {
  36          $config    = JFactory::getConfig();
  37          $userParams    = JComponentHelper::getParams('com_users');
  38          $db        = $this->getDbo();
  39  
  40          // Get the user id based on the token.
  41          $db->setQuery(
  42              'SELECT '.$db->quoteName('id').' FROM '.$db->quoteName('#__users') .
  43              ' WHERE '.$db->quoteName('activation').' = '.$db->Quote($token) .
  44              ' AND '.$db->quoteName('block').' = 1' .
  45              ' AND '.$db->quoteName('lastvisitDate').' = '.$db->Quote($db->getNullDate())
  46          );
  47          $userId = (int) $db->loadResult();
  48  
  49          // Check for a valid user id.
  50          if (!$userId) {
  51              $this->setError(JText::_('COM_USERS_ACTIVATION_TOKEN_NOT_FOUND'));
  52              return false;
  53          }
  54  
  55          // Load the users plugin group.
  56          JPluginHelper::importPlugin('user');
  57  
  58          // Activate the user.
  59          $user = JFactory::getUser($userId);
  60  
  61          // Admin activation is on and user is verifying their email
  62          if (($userParams->get('useractivation') == 2) && !$user->getParam('activate', 0))
  63          {
  64              $uri = JURI::getInstance();
  65  
  66              // Compile the admin notification mail values.
  67              $data = $user->getProperties();
  68              $data['activation'] = JApplication::getHash(JUserHelper::genRandomPassword());
  69              $user->set('activation', $data['activation']);
  70              $data['siteurl']    = JUri::base();
  71              $base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
  72              $data['activate'] = $base.JRoute::_('index.php?option=com_users&task=registration.activate&token='.$data['activation'], false);
  73              $data['fromname'] = $config->get('fromname');
  74              $data['mailfrom'] = $config->get('mailfrom');
  75              $data['sitename'] = $config->get('sitename');
  76              $user->setParam('activate', 1);
  77              $emailSubject    = JText::sprintf(
  78                  'COM_USERS_EMAIL_ACTIVATE_WITH_ADMIN_ACTIVATION_SUBJECT',
  79                  $data['name'],
  80                  $data['sitename']
  81              );
  82  
  83              $emailBody = JText::sprintf(
  84                  'COM_USERS_EMAIL_ACTIVATE_WITH_ADMIN_ACTIVATION_BODY',
  85                  $data['sitename'],
  86                  $data['name'],
  87                  $data['email'],
  88                  $data['username'],
  89                  $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation']
  90              );
  91  
  92              // get all admin users
  93              $query = 'SELECT name, email, sendEmail, id' .
  94                          ' FROM #__users' .
  95                          ' WHERE sendEmail=1';
  96  
  97              $db->setQuery($query);
  98              $rows = $db->loadObjectList();
  99  
 100              // Send mail to all users with users creating permissions and receiving system emails
 101              foreach( $rows as $row )
 102              {
 103                  $usercreator = JFactory::getUser($row->id);
 104                  if ($usercreator->authorise('core.create', 'com_users'))
 105                  {
 106                      $return = JFactory::getMailer()->sendMail($data['mailfrom'], $data['fromname'], $row->email, $emailSubject, $emailBody);
 107  
 108                      // Check for an error.
 109                      if ($return !== true) {
 110                          $this->setError(JText::_('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_FAILED'));
 111                          return false;
 112                      }
 113                  }
 114              }
 115          }
 116  
 117          //Admin activation is on and admin is activating the account
 118          elseif (($userParams->get('useractivation') == 2) && $user->getParam('activate', 0))
 119          {
 120              $user->set('activation', '');
 121              $user->set('block', '0');
 122  
 123              $uri = JURI::getInstance();
 124  
 125              // Compile the user activated notification mail values.
 126              $data = $user->getProperties();
 127              $user->setParam('activate', 0);
 128              $data['fromname'] = $config->get('fromname');
 129              $data['mailfrom'] = $config->get('mailfrom');
 130              $data['sitename'] = $config->get('sitename');
 131              $data['siteurl']    = JUri::base();
 132              $emailSubject    = JText::sprintf(
 133                  'COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_SUBJECT',
 134                  $data['name'],
 135                  $data['sitename']
 136              );
 137  
 138              $emailBody = JText::sprintf(
 139                  'COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_BODY',
 140                  $data['name'],
 141                  $data['siteurl'],
 142                  $data['username']
 143              );
 144  
 145              $return = JFactory::getMailer()->sendMail($data['mailfrom'], $data['fromname'], $data['email'], $emailSubject, $emailBody);
 146  
 147              // Check for an error.
 148              if ($return !== true) {
 149                  $this->setError(JText::_('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_FAILED'));
 150                  return false;
 151              }
 152          }
 153          else
 154          {
 155              $user->set('activation', '');
 156              $user->set('block', '0');
 157          }
 158  
 159          // Store the user object.
 160          if (!$user->save()) {
 161              $this->setError(JText::sprintf('COM_USERS_REGISTRATION_ACTIVATION_SAVE_FAILED', $user->getError()));
 162              return false;
 163          }
 164  
 165          return $user;
 166      }
 167  
 168      /**
 169       * Method to get the registration form data.
 170       *
 171       * The base form data is loaded and then an event is fired
 172       * for users plugins to extend the data.
 173       *
 174       * @return    mixed        Data object on success, false on failure.
 175       * @since    1.6
 176       */
 177  	public function getData()
 178      {
 179          if ($this->data === null)
 180          {
 181              $this->data    = new stdClass;
 182              $app    = JFactory::getApplication();
 183              $params    = JComponentHelper::getParams('com_users');
 184  
 185              // Override the base user data with any data in the session.
 186              $temp = (array) $app->getUserState('com_users.registration.data', array());
 187              foreach ($temp as $k => $v) {
 188                  $this->data->$k = $v;
 189              }
 190  
 191              // Get the groups the user should be added to after registration.
 192              $this->data->groups = array();
 193  
 194              // Get the default new user group, Registered if not specified.
 195              $system    = $params->get('new_usertype', 2);
 196  
 197              $this->data->groups[] = $system;
 198  
 199              // Unset the passwords.
 200              unset($this->data->password1);
 201              unset($this->data->password2);
 202  
 203              // Get the dispatcher and load the users plugins.
 204              $dispatcher    = JEventDispatcher::getInstance();
 205              JPluginHelper::importPlugin('user');
 206  
 207              // Trigger the data preparation event.
 208              $results = $dispatcher->trigger('onContentPrepareData', array('com_users.registration', $this->data));
 209  
 210              // Check for errors encountered while preparing the data.
 211              if (count($results) && in_array(false, $results, true)) {
 212                  $this->setError($dispatcher->getError());
 213                  $this->data = false;
 214              }
 215          }
 216  
 217          return $this->data;
 218      }
 219  
 220      /**
 221       * Method to get the registration form.
 222       *
 223       * The base form is loaded from XML and then an event is fired
 224       * for users plugins to extend the form with extra fields.
 225       *
 226       * @param    array    $data        An optional array of data for the form to interogate.
 227       * @param    boolean    $loadData    True if the form is to load its own data (default case), false if not.
 228       * @return    JForm    A JForm object on success, false on failure
 229       * @since    1.6
 230       */
 231  	public function getForm($data = array(), $loadData = true)
 232      {
 233          // Get the form.
 234          $form = $this->loadForm('com_users.registration', 'registration', array('control' => 'jform', 'load_data' => $loadData));
 235          if (empty($form)) {
 236              return false;
 237          }
 238  
 239          return $form;
 240      }
 241  
 242      /**
 243       * Method to get the data that should be injected in the form.
 244       *
 245       * @return    mixed    The data for the form.
 246       * @since    1.6
 247       */
 248  	protected function loadFormData()
 249      {
 250          return $this->getData();
 251      }
 252  
 253      /**
 254       * Override preprocessForm to load the user plugin group instead of content.
 255       *
 256       * @param    object    A form object.
 257       * @param    mixed    The data expected for the form.
 258       * @throws    Exception if there is an error in the form event.
 259       * @since    1.6
 260       */
 261  	protected function preprocessForm(JForm $form, $data, $group = 'user')
 262      {
 263          $userParams    = JComponentHelper::getParams('com_users');
 264  
 265          //Add the choice for site language at registration time
 266          if ($userParams->get('site_language') == 1 && $userParams->get('frontend_userparams') == 1)
 267          {
 268              $form->loadFile('sitelang', false);
 269          }
 270  
 271          parent::preprocessForm($form, $data, $group);
 272      }
 273  
 274      /**
 275       * Method to auto-populate the model state.
 276       *
 277       * Note. Calling getState in this method will result in recursion.
 278       *
 279       * @since    1.6
 280       */
 281  	protected function populateState()
 282      {
 283          // Get the application object.
 284          $app    = JFactory::getApplication();
 285          $params    = $app->getParams('com_users');
 286  
 287          // Load the parameters.
 288          $this->setState('params', $params);
 289      }
 290  
 291      /**
 292       * Method to save the form data.
 293       *
 294       * @param    array        The form data.
 295       * @return    mixed        The user id on success, false on failure.
 296       * @since    1.6
 297       */
 298  	public function register($temp)
 299      {
 300          $config = JFactory::getConfig();
 301          $db        = $this->getDbo();
 302          $params = JComponentHelper::getParams('com_users');
 303  
 304          // Initialise the table with JUser.
 305          $user = new JUser;
 306          $data = (array) $this->getData();
 307  
 308          // Merge in the registration data.
 309          foreach ($temp as $k => $v) {
 310              $data[$k] = $v;
 311          }
 312  
 313          // Prepare the data for the user object.
 314          $data['email']        = $data['email1'];
 315          $data['password']    = $data['password1'];
 316          $useractivation = $params->get('useractivation');
 317          $sendpassword = $params->get('sendpassword', 1);
 318  
 319          // Check if the user needs to activate their account.
 320          if (($useractivation == 1) || ($useractivation == 2)) {
 321              $data['activation'] = JApplication::getHash(JUserHelper::genRandomPassword());
 322              $data['block'] = 1;
 323          }
 324  
 325          // Bind the data.
 326          if (!$user->bind($data)) {
 327              $this->setError(JText::sprintf('COM_USERS_REGISTRATION_BIND_FAILED', $user->getError()));
 328              return false;
 329          }
 330  
 331          // Load the users plugin group.
 332          JPluginHelper::importPlugin('user');
 333  
 334          // Store the data.
 335          if (!$user->save()) {
 336              $this->setError(JText::sprintf('COM_USERS_REGISTRATION_SAVE_FAILED', $user->getError()));
 337              return false;
 338          }
 339  
 340          // Compile the notification mail values.
 341          $data = $user->getProperties();
 342          $data['fromname']    = $config->get('fromname');
 343          $data['mailfrom']    = $config->get('mailfrom');
 344          $data['sitename']    = $config->get('sitename');
 345          $data['siteurl']    = JUri::root();
 346  
 347          // Handle account activation/confirmation emails.
 348          if ($useractivation == 2)
 349          {
 350              // Set the link to confirm the user email.
 351              $uri = JURI::getInstance();
 352              $base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
 353              $data['activate'] = $base.JRoute::_('index.php?option=com_users&task=registration.activate&token='.$data['activation'], false);
 354  
 355              $emailSubject    = JText::sprintf(
 356                  'COM_USERS_EMAIL_ACCOUNT_DETAILS',
 357                  $data['name'],
 358                  $data['sitename']
 359              );
 360  
 361              if ($sendpassword)
 362              {
 363                  $emailBody = JText::sprintf(
 364                      'COM_USERS_EMAIL_REGISTERED_WITH_ADMIN_ACTIVATION_BODY',
 365                      $data['name'],
 366                      $data['sitename'],
 367                      $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation'],
 368                      $data['siteurl'],
 369                      $data['username'],
 370                      $data['password_clear']
 371                  );
 372              }
 373              else
 374              {
 375                  $emailBody = JText::sprintf(
 376                      'COM_USERS_EMAIL_REGISTERED_WITH_ADMIN_ACTIVATION_BODY_NOPW',
 377                      $data['name'],
 378                      $data['sitename'],
 379                      $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation'],
 380                      $data['siteurl'],
 381                      $data['username']
 382                  );
 383              }
 384          }
 385          elseif ($useractivation == 1)
 386          {
 387              // Set the link to activate the user account.
 388              $uri = JURI::getInstance();
 389              $base = $uri->toString(array('scheme', 'user', 'pass', 'host', 'port'));
 390              $data['activate'] = $base.JRoute::_('index.php?option=com_users&task=registration.activate&token='.$data['activation'], false);
 391  
 392              $emailSubject    = JText::sprintf(
 393                  'COM_USERS_EMAIL_ACCOUNT_DETAILS',
 394                  $data['name'],
 395                  $data['sitename']
 396              );
 397  
 398              if ($sendpassword)
 399              {
 400                  $emailBody = JText::sprintf(
 401                      'COM_USERS_EMAIL_REGISTERED_WITH_ACTIVATION_BODY',
 402                      $data['name'],
 403                      $data['sitename'],
 404                      $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation'],
 405                      $data['siteurl'],
 406                      $data['username'],
 407                      $data['password_clear']
 408                  );
 409              }
 410              else
 411              {
 412                  $emailBody = JText::sprintf(
 413                      'COM_USERS_EMAIL_REGISTERED_WITH_ACTIVATION_BODY_NOPW',
 414                      $data['name'],
 415                      $data['sitename'],
 416                      $data['siteurl'].'index.php?option=com_users&task=registration.activate&token='.$data['activation'],
 417                      $data['siteurl'],
 418                      $data['username']
 419                  );
 420              }
 421          }
 422          else
 423          {
 424  
 425              $emailSubject    = JText::sprintf(
 426                  'COM_USERS_EMAIL_ACCOUNT_DETAILS',
 427                  $data['name'],
 428                  $data['sitename']
 429              );
 430  
 431              $emailBody = JText::sprintf(
 432                  'COM_USERS_EMAIL_REGISTERED_BODY',
 433                  $data['name'],
 434                  $data['sitename'],
 435                  $data['siteurl']
 436              );
 437          }
 438  
 439          // Send the registration email.
 440          $return = JFactory::getMailer()->sendMail($data['mailfrom'], $data['fromname'], $data['email'], $emailSubject, $emailBody);
 441  
 442          //Send Notification mail to administrators
 443          if (($params->get('useractivation') < 2) && ($params->get('mail_to_admin') == 1)) {
 444              $emailSubject = JText::sprintf(
 445                  'COM_USERS_EMAIL_ACCOUNT_DETAILS',
 446                  $data['name'],
 447                  $data['sitename']
 448              );
 449  
 450              $emailBodyAdmin = JText::sprintf(
 451                  'COM_USERS_EMAIL_REGISTERED_NOTIFICATION_TO_ADMIN_BODY',
 452                  $data['name'],
 453                  $data['username'],
 454                  $data['siteurl']
 455              );
 456  
 457              // get all admin users
 458              $query = 'SELECT name, email, sendEmail' .
 459                      ' FROM #__users' .
 460                      ' WHERE sendEmail=1';
 461  
 462              $db->setQuery($query);
 463              $rows = $db->loadObjectList();
 464  
 465              // Send mail to all superadministrators id
 466              foreach( $rows as $row )
 467              {
 468                  $return = JFactory::getMailer()->sendMail($data['mailfrom'], $data['fromname'], $row->email, $emailSubject, $emailBodyAdmin);
 469  
 470                  // Check for an error.
 471                  if ($return !== true) {
 472                      $this->setError(JText::_('COM_USERS_REGISTRATION_ACTIVATION_NOTIFY_SEND_MAIL_FAILED'));
 473                      return false;
 474                  }
 475              }
 476          }
 477          // Check for an error.
 478          if ($return !== true) {
 479              $this->setError(JText::_('COM_USERS_REGISTRATION_SEND_MAIL_FAILED'));
 480  
 481              // Send a system message to administrators receiving system mails
 482              $db = JFactory::getDBO();
 483              $q = "SELECT id
 484                  FROM #__users
 485                  WHERE block = 0
 486                  AND sendEmail = 1";
 487              $db->setQuery($q);
 488              $sendEmail = $db->loadColumn();
 489              if (count($sendEmail) > 0) {
 490                  $jdate = new JDate;
 491                  // Build the query to add the messages
 492                  $q = "INSERT INTO ".$db->quoteName('#__messages')." (".$db->quoteName('user_id_from').
 493                  ", ".$db->quoteName('user_id_to').", ".$db->quoteName('date_time').
 494                  ", ".$db->quoteName('subject').", ".$db->quoteName('message').") VALUES ";
 495                  $messages = array();
 496  
 497                  foreach ($sendEmail as $userid) {
 498                      $messages[] = "(".$userid.", ".$userid.", '".$jdate->toSql()."', '".JText::_('COM_USERS_MAIL_SEND_FAILURE_SUBJECT')."', '".JText::sprintf('COM_USERS_MAIL_SEND_FAILURE_BODY', $return, $data['username'])."')";
 499                  }
 500                  $q .= implode(',', $messages);
 501                  $db->setQuery($q);
 502                  $db->execute();
 503              }
 504              return false;
 505          }
 506  
 507          if ($useractivation == 1)
 508          {
 509              return "useractivate";
 510          }
 511          elseif ($useractivation == 2)
 512          {
 513              return "adminactivate";
 514          }
 515          else
 516          {
 517              return $user->id;
 518          }
 519      }
 520  }

title

Description

title

Description

title

Description

title

title

Body