Source for file registration.php

Documentation is available at registration.php

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

Documentation generated on Tue, 19 Nov 2013 15:11:41 +0100 by phpDocumentor 1.4.3