Source for file profile.php

Documentation is available at profile.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.  * Profile model class for Users.
  14.  *
  15.  * @package     Joomla.Site
  16.  * @subpackage  com_users
  17.  * @since       1.6
  18.  */
  19. class UsersModelProfile extends JModelForm
  20. {
  21.     /**
  22.      * @var        object    The user profile data.
  23.      * @since   1.6
  24.      */
  25.     protected $data;
  26.  
  27.     public function __construct($config array())
  28.     {
  29.         parent::__construct($config);
  30.  
  31.         // Load the Joomla! RAD layer
  32.         if (!defined('FOF_INCLUDED'))
  33.         {
  34.             include_once JPATH_LIBRARIES '/fof/include.php';
  35.         }
  36.  
  37.         // Load the helper and model used for two factor authentication
  38.         require_once JPATH_ADMINISTRATOR '/components/com_users/models/user.php';
  39.         require_once JPATH_ADMINISTRATOR '/components/com_users/helpers/users.php';
  40.     }
  41.  
  42.     /**
  43.      * Method to check in a user.
  44.      *
  45.      * @param   integer        The id of the row to check out.
  46.      * @return  boolean  True on success, false on failure.
  47.      * @since   1.6
  48.      */
  49.     public function checkin($userId null)
  50.     {
  51.         // Get the user id.
  52.         $userId (!empty($userId)) $userId : (int) $this->getState('user.id');
  53.  
  54.         if ($userId)
  55.         {
  56.             // Initialise the table with JUser.
  57.             $table JTable::getInstance('User');
  58.  
  59.             // Attempt to check the row in.
  60.             if (!$table->checkin($userId))
  61.             {
  62.                 $this->setError($table->getError());
  63.                 return false;
  64.             }
  65.         }
  66.  
  67.         return true;
  68.     }
  69.  
  70.     /**
  71.      * Method to check out a user for editing.
  72.      *
  73.      * @param   integer        The id of the row to check out.
  74.      * @return  boolean  True on success, false on failure.
  75.      * @since   1.6
  76.      */
  77.     public function checkout($userId null)
  78.     {
  79.         // Get the user id.
  80.         $userId (!empty($userId)) $userId : (int) $this->getState('user.id');
  81.  
  82.         if ($userId)
  83.         {
  84.             // Initialise the table with JUser.
  85.             $table JTable::getInstance('User');
  86.  
  87.             // Get the current user object.
  88.             $user JFactory::getUser();
  89.  
  90.             // Attempt to check the row out.
  91.             if (!$table->checkout($user->get('id')$userId))
  92.             {
  93.                 $this->setError($table->getError());
  94.                 return false;
  95.             }
  96.         }
  97.  
  98.         return true;
  99.     }
  100.  
  101.     /**
  102.      * Method to get the profile form data.
  103.      *
  104.      * The base form data is loaded and then an event is fired
  105.      * for users plugins to extend the data.
  106.      *
  107.      * @return  mixed      Data object on success, false on failure.
  108.      * @since   1.6
  109.      */
  110.     public function getData()
  111.     {
  112.         if ($this->data === null{
  113.  
  114.             $userId $this->getState('user.id');
  115.  
  116.             // Initialise the table with JUser.
  117.             $this->data    = new JUser($userId);
  118.  
  119.             // Set the base user data.
  120.             $this->data->email1 $this->data->get('email');
  121.             $this->data->email2 $this->data->get('email');
  122.  
  123.             // Override the base user data with any data in the session.
  124.             $temp = (array) JFactory::getApplication()->getUserState('com_users.edit.profile.data'array());
  125.             foreach ($temp as $k => $v)
  126.             {
  127.                 $this->data->$k $v;
  128.             }
  129.  
  130.             // Unset the passwords.
  131.             unset($this->data->password1);
  132.             unset($this->data->password2);
  133.  
  134.             $registry new JRegistry($this->data->params);
  135.             $this->data->params $registry->toArray();
  136.  
  137.             // Get the dispatcher and load the users plugins.
  138.             $dispatcher    JEventDispatcher::getInstance();
  139.             JPluginHelper::importPlugin('user');
  140.  
  141.             // Trigger the data preparation event.
  142.             $results $dispatcher->trigger('onContentPrepareData'array('com_users.profile'$this->data));
  143.  
  144.             // Check for errors encountered while preparing the data.
  145.             if (count($results&& in_array(false$resultstrue))
  146.             {
  147.                 $this->setError($dispatcher->getError());
  148.                 $this->data = false;
  149.             }
  150.         }
  151.  
  152.         return $this->data;
  153.     }
  154.  
  155.     /**
  156.      * Method to get the profile form.
  157.      *
  158.      * The base form is loaded from XML and then an event is fired
  159.      * for users plugins to extend the form with extra fields.
  160.      *
  161.      * @param   array  $data        An optional array of data for the form to interogate.
  162.      * @param   boolean    $loadData    True if the form is to load its own data (default case), false if not.
  163.      * @return  JForm    A JForm object on success, false on failure
  164.      * @since   1.6
  165.      */
  166.     public function getForm($data array()$loadData true)
  167.     {
  168.         // Get the form.
  169.         $form $this->loadForm('com_users.profile''profile'array('control' => 'jform''load_data' => $loadData));
  170.         if (empty($form))
  171.         {
  172.             return false;
  173.         }
  174.  
  175.         // Check for username compliance and parameter set
  176.         $isUsernameCompliant true;
  177.  
  178.         if ($this->loadFormData()->username)
  179.         {
  180.             $username $this->loadFormData()->username;
  181.             $isUsernameCompliant  !(preg_match('#[<>"\'%;()&\\s\\\\]|\\.\\./#'$username|| strlen(utf8_decode($username)) 2);
  182.         }
  183.  
  184.         $this->setState('user.username.compliant'$isUsernameCompliant);
  185.  
  186.         if (!JComponentHelper::getParams('com_users')->get('change_login_name'&& $isUsernameCompliant)
  187.         {
  188.             $form->setFieldAttribute('username''class''');
  189.             $form->setFieldAttribute('username''filter''');
  190.             $form->setFieldAttribute('username''description''COM_USERS_PROFILE_NOCHANGE_USERNAME_DESC');
  191.             $form->setFieldAttribute('username''validate''');
  192.             $form->setFieldAttribute('username''message''');
  193.             $form->setFieldAttribute('username''readonly''true');
  194.             $form->setFieldAttribute('username''required''false');
  195.         }
  196.  
  197.         return $form;
  198.     }
  199.  
  200.     /**
  201.      * Method to get the data that should be injected in the form.
  202.      *
  203.      * @return  mixed  The data for the form.
  204.      * @since   1.6
  205.      */
  206.     protected function loadFormData()
  207.     {
  208.         $data $this->getData();
  209.  
  210.         $this->preprocessData('com_users.profile'$data);
  211.  
  212.         return $data;
  213.     }
  214.  
  215.     /**
  216.      * Override preprocessForm to load the user plugin group instead of content.
  217.      *
  218.      * @param   object    form object.
  219.      * @param   mixed    The data expected for the form.
  220.      * @throws    Exception if there is an error in the form event.
  221.      * @since   1.6
  222.      */
  223.     protected function preprocessForm(JForm $form$data$group 'user')
  224.     {
  225.         if (JComponentHelper::getParams('com_users')->get('frontend_userparams'))
  226.         {
  227.             $form->loadFile('frontend'false);
  228.             if (JFactory::getUser()->authorise('core.login.admin'))
  229.             {
  230.                 $form->loadFile('frontend_admin'false);
  231.             }
  232.         }
  233.         parent::preprocessForm($form$data$group);
  234.     }
  235.  
  236.     /**
  237.      * Method to auto-populate the model state.
  238.      *
  239.      * Note. Calling getState in this method will result in recursion.
  240.      *
  241.      * @since   1.6
  242.      */
  243.     protected function populateState()
  244.     {
  245.         // Get the application object.
  246.         $params    JFactory::getApplication()->getParams('com_users');
  247.  
  248.         // Get the user id.
  249.         $userId JFactory::getApplication()->getUserState('com_users.edit.profile.id');
  250.         $userId !empty($userId$userId : (int) JFactory::getUser()->get('id');
  251.  
  252.         // Set the user id.
  253.         $this->setState('user.id'$userId);
  254.  
  255.         // Load the parameters.
  256.         $this->setState('params'$params);
  257.     }
  258.  
  259.     /**
  260.      * Method to save the form data.
  261.      *
  262.      * @param   array  The form data.
  263.      * @return  mixed      The user id on success, false on failure.
  264.      * @since   1.6
  265.      */
  266.     public function save($data)
  267.     {
  268.         $userId (!empty($data['id'])) $data['id': (int) $this->getState('user.id');
  269.  
  270.         $user new JUser($userId);
  271.  
  272.         // Prepare the data for the user object.
  273.         $data['email']        JStringPunycode::emailToPunycode($data['email1']);
  274.         $data['password']    $data['password1'];
  275.  
  276.         // Unset the username if it should not be overwritten
  277.         $username $data['username'];
  278.         $isUsernameCompliant $this->getState('user.username.compliant');
  279.  
  280.         if (!JComponentHelper::getParams('com_users')->get('change_login_name'&& $isUsernameCompliant)
  281.         {
  282.             unset($data['username']);
  283.         }
  284.  
  285.         // Unset the block so it does not get overwritten
  286.         unset($data['block']);
  287.  
  288.         // Unset the sendEmail so it does not get overwritten
  289.         unset($data['sendEmail']);
  290.  
  291.         // handle the two factor authentication setup
  292.         if (array_key_exists('twofactor'$data))
  293.         {
  294.             $model new UsersModelUser;
  295.  
  296.             $twoFactorMethod $data['twofactor']['method'];
  297.  
  298.             // Get the current One Time Password (two factor auth) configuration
  299.             $otpConfig $model->getOtpConfig($userId);
  300.  
  301.             if ($twoFactorMethod != 'none')
  302.             {
  303.                 // Run the plugins
  304.                 FOFPlatform::getInstance()->importPlugin('twofactorauth');
  305.                 $otpConfigReplies FOFPlatform::getInstance()->runPlugins('onUserTwofactorApplyConfiguration'array($twoFactorMethod));
  306.  
  307.                 // Look for a valid reply
  308.                 foreach ($otpConfigReplies as $reply)
  309.                 {
  310.                     if (!is_object($reply|| empty($reply->method|| ($reply->method != $twoFactorMethod))
  311.                     {
  312.                         continue;
  313.                     }
  314.  
  315.                     $otpConfig->method $reply->method;
  316.                     $otpConfig->config $reply->config;
  317.  
  318.                     break;
  319.                 }
  320.  
  321.                 // Save OTP configuration.
  322.                 $model->setOtpConfig($userId$otpConfig);
  323.  
  324.                 // Generate one time emergency passwords if required (depleted or not set)
  325.                 if (empty($otpConfig->otep))
  326.                 {
  327.                     $oteps $model->generateOteps($userId);
  328.                 }
  329.             }
  330.             else
  331.             {
  332.                 $otpConfig->method 'none';
  333.                 $otpConfig->config array();
  334.                 $model->setOtpConfig($userId$otpConfig);
  335.             }
  336.  
  337.             // Unset the raw data
  338.             unset($data['twofactor']);
  339.  
  340.             // Reload the user record with the updated OTP configuration
  341.             $user->load($userId);
  342.         }
  343.  
  344.         // Bind the data.
  345.         if (!$user->bind($data))
  346.         {
  347.             $this->setError(JText::sprintf('COM_USERS_PROFILE_BIND_FAILED'$user->getError()));
  348.             return false;
  349.         }
  350.  
  351.         // Load the users plugin group.
  352.         JPluginHelper::importPlugin('user');
  353.  
  354.         // Null the user groups so they don't get overwritten
  355.         $user->groups null;
  356.  
  357.         // Store the data.
  358.         if (!$user->save())
  359.         {
  360.             $this->setError($user->getError());
  361.             return false;
  362.         }
  363.  
  364.         $user->tags new JHelperTags;
  365.         $user->tags->getTagIds($user->id'com_users.user');
  366.  
  367.         return $user->id;
  368.     }
  369.  
  370.     /**
  371.      * Gets the configuration forms for all two-factor authentication methods
  372.      * in an array.
  373.      *
  374.      * @param   integer  $user_id  The user ID to load the forms for (optional)
  375.      *
  376.      * @return  array 
  377.      */
  378.     public function getTwofactorform($user_id null)
  379.     {
  380.         $user_id (!empty($user_id)) $user_id : (int) $this->getState('user.id');
  381.  
  382.         $model new UsersModelUser;
  383.  
  384.         $otpConfig $model->getOtpConfig($user_id);
  385.  
  386.         FOFPlatform::getInstance()->importPlugin('twofactorauth');
  387.         return FOFPlatform::getInstance()->runPlugins('onUserTwofactorShowConfiguration'array($otpConfig$user_id));
  388.     }
  389.  
  390.     public function getOtpConfig($user_id null)
  391.     {
  392.         $user_id (!empty($user_id)) $user_id : (int) $this->getState('user.id');
  393.  
  394.         $model new UsersModelUser;
  395.  
  396.         return $model->getOtpConfig($user_id);
  397.     }
  398. }

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