Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/user/user.php

Documentation is available at user.php

  1. <?php
  2. /**
  3.  * @version        $Id: user.php 11688 2009-03-13 02:05:56Z ian $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    User
  6.  * @copyright    Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant to the
  9.  *  GNU General Public License, and as distributed it includes or is derivative
  10.  *  of works licensed under the GNU General Public License or other free or open
  11.  *  source software licenses. See COPYRIGHT.php for copyright notices and
  12.  *  details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. jimport'joomla.html.parameter');
  19.  
  20.  
  21. /**
  22.  * User class.  Handles all application interaction with a user
  23.  *
  24.  * @package     Joomla.Framework
  25.  * @subpackage    User
  26.  * @since        1.5
  27.  */
  28. class JUser extends JObject
  29. {
  30.     /**
  31.      * Unique id
  32.      * @var int 
  33.      */
  34.     var $id                = null;
  35.  
  36.     /**
  37.      * The users real name (or nickname)
  38.      * @var string 
  39.      */
  40.     var $name            = null;
  41.  
  42.     /**
  43.      * The login name
  44.      * @var string 
  45.      */
  46.     var $username        = null;
  47.  
  48.     /**
  49.      * The email
  50.      * @var string 
  51.      */
  52.     var $email            = null;
  53.  
  54.     /**
  55.      * MD5 encrypted password
  56.      * @var string 
  57.      */
  58.     var $password        = null;
  59.  
  60.     /**
  61.      * Clear password, only available when a new password is set for a user
  62.      * @var string 
  63.      */
  64.     var $password_clear    = '';
  65.  
  66.     /**
  67.      * Description
  68.      * @var string 
  69.      */
  70.     var $usertype        = null;
  71.  
  72.     /**
  73.      * Description
  74.      * @var int 
  75.      */
  76.     var $block            = null;
  77.  
  78.     /**
  79.      * Description
  80.      * @var int 
  81.      */
  82.     var $sendEmail        = null;
  83.  
  84.     /**
  85.      * The group id number
  86.      * @var int 
  87.      */
  88.     var $gid            = null;
  89.  
  90.     /**
  91.      * Description
  92.      * @var datetime 
  93.      */
  94.     var $registerDate    = null;
  95.  
  96.     /**
  97.      * Description
  98.      * @var datetime 
  99.      */
  100.     var $lastvisitDate    = null;
  101.  
  102.     /**
  103.      * Description
  104.      * @var string activation hash
  105.      */
  106.     var $activation        = null;
  107.  
  108.     /**
  109.      * Description
  110.      * @var string 
  111.      */
  112.     var $params            = null;
  113.  
  114.     /**
  115.      * Description
  116.      * @var string integer
  117.      */
  118.     var $aid         = null;
  119.  
  120.     /**
  121.      * Description
  122.      * @var boolean 
  123.      */
  124.     var $guest     = null;
  125.  
  126.     /**
  127.      * User parameters
  128.      * @var object 
  129.      */
  130.     var $_params     = null;
  131.  
  132.     /**
  133.      * Error message
  134.      * @var string 
  135.      */
  136.     var $_errorMsg    = null;
  137.  
  138.  
  139.     /**
  140.     * Constructor activating the default information of the language
  141.     *
  142.     * @access     protected
  143.     */
  144.     function __construct($identifier 0)
  145.     {
  146.         // Create the user parameters object
  147.         $this->_params = new JParameter'' );
  148.  
  149.         // Load the user if it exists
  150.         if (!empty($identifier)) {
  151.             $this->load($identifier);
  152.         }
  153.         else
  154.         {
  155.             //initialise
  156.             $this->id        = 0;
  157.             $this->gid       = 0;
  158.             $this->sendEmail = 0;
  159.             $this->aid       = 0;
  160.             $this->guest     = 1;
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * Returns a reference to the global User object, only creating it if it
  166.      * doesn't already exist.
  167.      *
  168.      * This method must be invoked as:
  169.      *         <pre>  $user =& JUser::getInstance($id);</pre>
  170.      *
  171.      * @access     public
  172.      * @param     int     $id     The user to load - Can be an integer or string - If string, it is converted to ID automatically.
  173.      * @return     JUser              The User object.
  174.      * @since     1.5
  175.      */
  176.     function &getInstance($id 0)
  177.     {
  178.         static $instances;
  179.  
  180.         if (!isset ($instances)) {
  181.             $instances array ();
  182.         }
  183.  
  184.         // Find the user id
  185.         if(!is_numeric($id))
  186.         {
  187.             jimport('joomla.user.helper');
  188.             if (!$id JUserHelper::getUserId($id)) {
  189.                 JError::raiseWarning'SOME_ERROR_CODE''JUser::_load: User '.$id.' does not exist' );
  190.                 $retval false;
  191.                 return $retval;
  192.             }
  193.         }
  194.  
  195.         if (empty($instances[$id])) {
  196.             $user new JUser($id);
  197.             $instances[$id$user;
  198.         }
  199.  
  200.         return $instances[$id];
  201.     }
  202.  
  203.     /**
  204.      * Method to get a parameter value
  205.      *
  206.      * @access     public
  207.      * @param     string     $key         Parameter key
  208.      * @param     mixed    $default    Parameter default value
  209.      * @return    mixed                The value or the default if it did not exist
  210.      * @since    1.5
  211.      */
  212.     function getParam$key$default null )
  213.     {
  214.         return $this->_params->get$key$default );
  215.     }
  216.  
  217.     /**
  218.      * Method to set a parameter
  219.      *
  220.      * @access     public
  221.      * @param     string     $key     Parameter key
  222.      * @param     mixed    $value    Parameter value
  223.      * @return    mixed            Set parameter value
  224.      * @since    1.5
  225.      */
  226.     function setParam$key$value )
  227.     {
  228.         return $this->_params->set$key$value );
  229.     }
  230.  
  231.     /**
  232.      * Method to set a default parameter if it does not exist
  233.      *
  234.      * @access     public
  235.      * @param     string     $key     Parameter key
  236.      * @param     mixed    $value    Parameter value
  237.      * @return    mixed            Set parameter value
  238.      * @since    1.5
  239.      */
  240.     function defParam$key$value )
  241.     {
  242.         return $this->_params->def$key$value );
  243.     }
  244.  
  245.     /**
  246.      * Method to check JUser object authorization against an access control
  247.      * object and optionally an access extension object
  248.      *
  249.      * @access     public
  250.      * @param    string    $acoSection    The ACO section value
  251.      * @param    string    $aco        The ACO value
  252.      * @param    string    $axoSection    The AXO section value    [optional]
  253.      * @param    string    $axo        The AXO value            [optional]
  254.      * @return    boolean    True if authorized
  255.      * @since    1.5
  256.      */
  257.     function authorize$acoSection$aco$axoSection null$axo null )
  258.     {
  259.         // the native calls (Check Mode 1) work on the user id, not the user type
  260.         $acl    JFactory::getACL();
  261.         $value    $acl->getCheckMode(== $this->id : $this->usertype;
  262.  
  263.         return $acl->acl_check$acoSection$aco,    'users'$value$axoSection$axo );
  264.     }
  265.  
  266.     /**
  267.      * Pass through method to the table for setting the last visit date
  268.      *
  269.      * @access     public
  270.      * @param    int        $timestamp    The timestamp, defaults to 'now'
  271.      * @return    boolean    True on success
  272.      * @since    1.5
  273.      */
  274.     function setLastVisit($timestamp=null)
  275.     {
  276.         // Create the user table object
  277.         $table     =$this->getTable();
  278.         $table->load($this->id);
  279.  
  280.         return $table->setLastVisit($timestamp);
  281.     }
  282.  
  283.     /**
  284.      * Method to get the user parameters
  285.      *
  286.      * This function tries to load an xml file based on the users usertype. The filename of the xml
  287.      * file is the same as the usertype. The functionals has a static variable to store the parameters
  288.      * setup file base path. You can call this function statically to set the base path if needed.
  289.      *
  290.      * @access     public
  291.      * @param    boolean    If true, loads the parameters setup file. Default is false.
  292.      * @param    path    Set the parameters setup file base path to be used to load the user parameters.
  293.      * @return    object    The user parameters object
  294.      * @since    1.5
  295.      */
  296.     function &getParameters($loadsetupfile false$path null)
  297.     {
  298.         static $parampath;
  299.  
  300.         // Set a custom parampath if defined
  301.         ifisset($path) ) {
  302.             $parampath $path;
  303.         }
  304.  
  305.         // Set the default parampath if not set already
  306.         if!isset($parampath) ) {
  307.             $parampath JPATH_ADMINISTRATOR.DS.'components'.DS.'com_users'.DS.'models';
  308.         }
  309.  
  310.         if($loadsetupfile)
  311.         {
  312.             $type str_replace(' ''_'strtolower($this->usertype));
  313.  
  314.             $file $parampath.DS.$type.'.xml';
  315.             if(!file_exists($file)) {
  316.                 $file $parampath.DS.'user.xml';
  317.             }
  318.  
  319.             $this->_params->loadSetupFile($file);
  320.         }
  321.         return $this->_params;
  322.     }
  323.  
  324.     /**
  325.      * Method to get the user parameters
  326.      *
  327.      * @access     public
  328.      * @param    object    The user parameters object
  329.      * @since    1.5
  330.      */
  331.     function setParameters($params )
  332.     {
  333.         $this->_params = $params;
  334.     }
  335.  
  336.     /**
  337.      * Method to get the user table object
  338.      *
  339.      * This function uses a static variable to store the table name of the user table to
  340.      * it instantiates. You can call this function statically to set the table name if
  341.      * needed.
  342.      *
  343.      * @access     public
  344.      * @param    string    The user table name to be used
  345.      * @param    string    The user table prefix to be used
  346.      * @return    object    The user table object
  347.      * @since    1.5
  348.      */
  349.     function &getTable$type null$prefix 'JTable' )
  350.     {
  351.         static $tabletype;
  352.  
  353.         //Set the default tabletype;
  354.         if(!isset($tabletype)) {
  355.             $tabletype['name']         'user';
  356.             $tabletype['prefix']    'JTable';
  357.         }
  358.  
  359.         //Set a custom table type is defined
  360.         if(isset($type)) {
  361.             $tabletype['name']         $type;
  362.             $tabletype['prefix']    $prefix;
  363.         }
  364.  
  365.         // Create the user table object
  366.         $table     =JTable::getInstance$tabletype['name']$tabletype['prefix');
  367.         return $table;
  368.     }
  369.  
  370.     /**
  371.      * Method to bind an associative array of data to a user object
  372.      *
  373.      * @access     public
  374.      * @param     array     $array     The associative array to bind to the object
  375.      * @return     boolean         True on success
  376.      * @since 1.5
  377.      */
  378.     function bind($array)
  379.     {
  380.         jimport('joomla.user.helper');
  381.  
  382.         // Lets check to see if the user is new or not
  383.         if (empty($this->id))
  384.         {
  385.             // Check the password and create the crypted password
  386.             if (empty($array['password'])) {
  387.                 $array['password']  JUserHelper::genRandomPassword();
  388.                 $array['password2'$array['password'];
  389.             }
  390.  
  391.             if ($array['password'!= $array['password2']{
  392.                     $this->setErrorJText::_'PASSWORD DO NOT MATCH.' ) );
  393.                     return false;
  394.             }
  395.  
  396.             $this->password_clear = JArrayHelper::getValue$array'password''''string' );
  397.  
  398.             $salt  JUserHelper::genRandomPassword(32);
  399.             $crypt JUserHelper::getCryptedPassword($array['password']$salt);
  400.             $array['password'$crypt.':'.$salt;
  401.  
  402.             // Set the registration timestamp
  403.  
  404.             $now =JFactory::getDate();
  405.             $this->set'registerDate'$now->toMySQL() );
  406.  
  407.             // Check that username is not greater than 150 characters
  408.             $username $this->get'username' );
  409.             if strlen($username150 )
  410.             {
  411.                 $username substr$username0150 );
  412.                 $this->set'username'$username );
  413.             }
  414.  
  415.             // Check that password is not greater than 100 characters
  416.             $password $this->get'password' );
  417.             if strlen($password100 )
  418.             {
  419.                 $password substr$password0100 );
  420.                 $this->set'password'$password );
  421.             }
  422.         }
  423.         else
  424.         {
  425.             // Updating an existing user
  426.             if (!empty($array['password']))
  427.             {
  428.                 if $array['password'!= $array['password2'{
  429.                     $this->setErrorJText::_'PASSWORD DO NOT MATCH.' ) );
  430.                     return false;
  431.                 }
  432.  
  433.                 $this->password_clear = JArrayHelper::getValue$array'password''''string' );
  434.  
  435.                 $salt JUserHelper::genRandomPassword(32);
  436.                 $crypt JUserHelper::getCryptedPassword($array['password']$salt);
  437.                 $array['password'$crypt.':'.$salt;
  438.             }
  439.             else
  440.             {
  441.                 $array['password'$this->password;
  442.             }
  443.         }
  444.  
  445.         // TODO: this will be deprecated as of the ACL implementation
  446.         $db =JFactory::getDBO();
  447.  
  448.         $gid array_key_exists('gid'$array $array['gid'$this->get('gid');
  449.  
  450.         $query 'SELECT name'
  451.         . ' FROM #__core_acl_aro_groups'
  452.         . ' WHERE id = ' . (int) $gid
  453.         ;
  454.         $db->setQuery$query );
  455.         $this->set'usertype'$db->loadResult());
  456.  
  457.         if array_key_exists('params'$array) )
  458.         {
  459.             $params    '';
  460.             $this->_params->bind($array['params']);
  461.             if is_array($array['params']) ) {
  462.                 $params    $this->_params->toString();
  463.             else {
  464.                 $params $array['params'];
  465.             }
  466.  
  467.             $this->params = $params;
  468.         }
  469.  
  470.         // Bind the array
  471.         if (!$this->setProperties($array)) {
  472.             $this->setError("Unable to bind array to user object");
  473.             return false;
  474.         }
  475.  
  476.         // Make sure its an integer
  477.         $this->id = (int) $this->id;
  478.  
  479.         return true;
  480.     }
  481.  
  482.     /**
  483.      * Method to save the JUser object to the database
  484.      *
  485.      * @access     public
  486.      * @param     boolean $updateOnly Save the object only if not a new user
  487.      * @return     boolean             True on success
  488.      * @since 1.5
  489.      */
  490.     function save$updateOnly false )
  491.     {
  492.         // Create the user table object
  493.         $table     =$this->getTable();
  494.         $this->params = $this->_params->toString();
  495.         $table->bind($this->getProperties());
  496.  
  497.         // Check and store the object.
  498.         if (!$table->check()) {
  499.             $this->setError($table->getError());
  500.             return false;
  501.         }
  502.  
  503.         // If user is made a Super Admin group and user is NOT a Super Admin
  504.         $my =JFactory::getUser();
  505.         if $this->get('gid'== 25 && $my->get('gid'!= 25 )
  506.         {
  507.             // disallow creation of Super Admin by non Super Admin users
  508.             $this->setError(JText::_'WARNSUPERADMINCREATE' ));
  509.             return false;
  510.         }
  511.  
  512.         // If user is made an Admin group and user is NOT a Super Admin
  513.         if ($this->get('gid'== 24 && !($my->get('gid'== 25 || ($this->get('id'== $my->id && $my->get('gid'== 24)))
  514.         {
  515.             // disallow creation of Admin by non Super Admin users
  516.             $this->setError(JText::_'WARNSUPERADMINCREATE' ));
  517.             return false;
  518.         }
  519.  
  520.         //are we creating a new user
  521.         $isnew !$this->id;
  522.  
  523.         // If we aren't allowed to create new users return
  524.         if ($isnew && $updateOnly{
  525.             return true;
  526.         }
  527.  
  528.         // Get the old user
  529.         $old new JUser($this->id);
  530.  
  531.         // Fire the onBeforeStoreUser event.
  532.         JPluginHelper::importPlugin'user' );
  533.         $dispatcher =JDispatcher::getInstance();
  534.         $dispatcher->trigger'onBeforeStoreUser'array$old->getProperties()$isnew ) );
  535.  
  536.         //Store the user data in the database
  537.         if (!$result $table->store()) {
  538.             $this->setError($table->getError());
  539.         }
  540.  
  541.         // Set the id for the JUser object in case we created a new user.
  542.         if (empty($this->id)) {
  543.             $this->id = $table->get'id' );
  544.         }
  545.  
  546.         // Fire the onAftereStoreUser event
  547.         $dispatcher->trigger'onAfterStoreUser'array$this->getProperties()$isnew$result$this->getError() ) );
  548.  
  549.         return $result;
  550.     }
  551.  
  552.     /**
  553.      * Method to delete the JUser object from the database
  554.      *
  555.      * @access     public
  556.      * @param     boolean $updateOnly Save the object only if not a new user
  557.      * @return     boolean             True on success
  558.      * @since 1.5
  559.      */
  560.     function delete)
  561.     {
  562.         JPluginHelper::importPlugin'user' );
  563.  
  564.         //trigger the onBeforeDeleteUser event
  565.         $dispatcher =JDispatcher::getInstance();
  566.         $dispatcher->trigger'onBeforeDeleteUser'array$this->getProperties() ) );
  567.  
  568.         // Create the user table object
  569.         $table     =$this->getTable();
  570.  
  571.         $result false;
  572.         if (!$result $table->delete($this->id)) {
  573.             $this->setError($table->getError());
  574.         }
  575.  
  576.         //trigger the onAfterDeleteUser event
  577.         $dispatcher->trigger'onAfterDeleteUser'array$this->getProperties()$result$this->getError()) );
  578.         return $result;
  579.  
  580.     }
  581.  
  582.     /**
  583.      * Method to load a JUser object by user id number
  584.      *
  585.      * @access     public
  586.      * @param     mixed     $identifier The user id of the user to load
  587.      * @param     string     $path         Path to a parameters xml file
  588.      * @return     boolean             True on success
  589.      * @since 1.5
  590.      */
  591.     function load($id)
  592.     {
  593.         // Create the user table object
  594.         $table     =$this->getTable();
  595.  
  596.          // Load the JUserModel object based on the user id or throw a warning.
  597.          if(!$table->load($id)) {
  598.             JError::raiseWarning'SOME_ERROR_CODE''JUser::_load: Unable to load user with id: '.$id );
  599.             return false;
  600.         }
  601.  
  602.         /*
  603.          * Set the user parameters using the default xml file.  We might want to
  604.          * extend this in the future to allow for the ability to have custom
  605.          * user parameters, but for right now we'll leave it how it is.
  606.          */
  607.         $this->_params->loadINI($table->params);
  608.  
  609.         // Assuming all is well at this point lets bind the data
  610.         $this->setProperties($table->getProperties());
  611.  
  612.         return true;
  613.     }
  614. }

Documentation generated on Sat, 14 Nov 2009 11:23:01 +0000 by phpDocumentor 1.3.1