[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/components/com_users/models/ -> registration.php (source)

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


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1