Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/user/user.php

Documentation is available at user.php

  1. <?php
  2. /**
  3.  * @version        $Id: user.php 6760 2007-03-03 02:50:10Z Jinx $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    User
  6.  * @copyright    Copyright (C) 2005 - 2007 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.  * User class.  Handles all application interaction with a user
  22.  *
  23.  * @author         Louis Landry <[email protected]>
  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.      * Description
  62.      * @var string 
  63.      */
  64.     var $usertype        = null;
  65.  
  66.     /**
  67.      * Description
  68.      * @var int 
  69.      */
  70.     var $block            = null;
  71.  
  72.     /**
  73.      * Description
  74.      * @var int 
  75.      */
  76.     var $sendEmail        = null;
  77.  
  78.     /**
  79.      * The group id number
  80.      * @var int 
  81.      */
  82.     var $gid            = null;
  83.  
  84.     /**
  85.      * Description
  86.      * @var datetime 
  87.      */
  88.     var $registerDate    = null;
  89.  
  90.     /**
  91.      * Description
  92.      * @var datetime 
  93.      */
  94.     var $lastvisitDate    = null;
  95.  
  96.     /**
  97.      * Description
  98.      * @var string activation hash
  99.      */
  100.     var $activation        = null;
  101.  
  102.     /**
  103.      * Description
  104.      * @var string 
  105.      */
  106.     var $params            = null;
  107.     
  108.     /**
  109.      * Description
  110.      * @var string integer
  111.      */
  112.     var $aid         = null;
  113.     
  114.     /**
  115.      * Description
  116.      * @var boolean 
  117.      */
  118.     var $guest     = null;
  119.  
  120.     /**
  121.      * User parameters
  122.      * @var object 
  123.      */
  124.     var $_params     = null;
  125.  
  126.     /**
  127.      * Error message
  128.      * @var string 
  129.      */
  130.     var $_errorMsg    = null;
  131.  
  132.     /**
  133.      * Clear password, only available when a new password is set for a user
  134.      * @var string 
  135.      */
  136.     var $clearPW    = '';
  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.         else {
  153.             //initialise
  154.             $this->id        = 0;
  155.             $this->gid       = 0;
  156.             $this->sendEmail = 1;
  157.             $this->aid       = 0;
  158.             $this->guest     = 1;
  159.         }
  160.     }
  161.  
  162.     /**
  163.      * Returns a reference to the global User object, only creating it if it
  164.      * doesn't already exist.
  165.      *
  166.      * This method must be invoked as:
  167.      *         <pre>  $user =& JUser::getInstance($id);</pre>
  168.      *
  169.      * @access     public
  170.      * @param     int     $id     The user to load - Can be an integer or string - If string, it is converted to ID automatically.
  171.      * @return     JUser              The User object.
  172.      * @since     1.5
  173.      */
  174.     function &getInstance($id 0)
  175.     {
  176.         static $instances;
  177.  
  178.         if (!isset ($instances)) {
  179.             $instances array ();
  180.         }
  181.  
  182.         // Find the user id
  183.         if(!is_numeric($id))
  184.         {
  185.             jimport('joomla.user.helper');
  186.             if (!$id JUserHelper::getUserId($id)) {
  187.                 JError::raiseWarning'SOME_ERROR_CODE''JUser::_load: User '.$id.' does not exist' );
  188.                 return false;
  189.             }
  190.         }
  191.  
  192.         if (empty($instances[$id])) {
  193.             $user new JUser($id);
  194.             $instances[$id$user;
  195.         }
  196.  
  197.         return $instances[$id];
  198.     }
  199.  
  200.     /**
  201.      * Method to get a parameter value
  202.      *
  203.      * @access     public
  204.      * @param     string     $key         Parameter key
  205.      * @param     mixed    $default    Parameter default value
  206.      * @return    mixed                The value or the default if it did not exist
  207.      * @since    1.5
  208.      */
  209.     function getParam$key$default null )
  210.     {
  211.         return $this->_params->get$key$default );
  212.     }
  213.  
  214.     /**
  215.      * Method to set a parameter
  216.      *
  217.      * @access     public
  218.      * @param     string     $key     Parameter key
  219.      * @param     mixed    $value    Parameter value
  220.      * @return    mixed            Set parameter value
  221.      * @since    1.5
  222.      */
  223.     function setParam$key$value )
  224.     {
  225.         return $this->_params->set$key$value );
  226.     }
  227.  
  228.     /**
  229.      * Method to set a default parameter if it does not exist
  230.      *
  231.      * @access     public
  232.      * @param     string     $key     Parameter key
  233.      * @param     mixed    $value    Parameter value
  234.      * @return    mixed            Set parameter value
  235.      * @since    1.5
  236.      */
  237.     function defParam$key$value )
  238.     {
  239.         return $this->_params->def$key$value );
  240.     }
  241.  
  242.     /**
  243.      * Method to check JUser object authorization against an access control
  244.      * object and optionally an access extension object
  245.      *
  246.      * @access     public
  247.      * @param    string    $acoSection    The ACO section value
  248.      * @param    string    $aco        The ACO value
  249.      * @param    string    $axoSection    The AXO section value    [optional]
  250.      * @param    string    $axo        The AXO value            [optional]
  251.      * @return    boolean    True if authorized
  252.      * @since    1.5
  253.      */
  254.     function authorize$acoSection$aco$axoSection null$axo null )
  255.     {
  256.         $acl JFactory::getACL();
  257.         return $acl->acl_check$acoSection$aco,    'users'$this->usertype$axoSection$axo );
  258.     }
  259.  
  260.     /**
  261.      * Pass through method to the table for setting the last visit date
  262.      *
  263.      * @access     public
  264.      * @param    int        $timestamp    The timestamp, defaults to 'now'
  265.      * @return    boolean    True on success
  266.      * @since    1.5
  267.      */
  268.     function setLastVisit($timestamp=null)
  269.     {
  270.         // Create the user table object
  271.         $table     =JTable::getInstance'user');
  272.         $table->load($this->id);
  273.  
  274.         return $table->setLastVisit($timestamp);
  275.     }
  276.  
  277.     /**
  278.      * Method to get the user parameters
  279.      *
  280.      * @access     public
  281.      * @return    object    The user parameters object
  282.      * @since    1.5
  283.      */
  284.     function getParameters()
  285.     {
  286.         return $this->_params;
  287.     }
  288.  
  289.     /**
  290.      * Method to get the user table object
  291.      *
  292.      * @access     public
  293.      * @return    object    The user table object
  294.      * @since    1.5
  295.      */
  296.     function &getTable()
  297.     {
  298.         // Create the user table object
  299.         $table     =JTable::getInstance'user');
  300.         $table->load($this->id);
  301.  
  302.         return $table;
  303.     }
  304.  
  305.     /**
  306.      * Method to set the user parameters
  307.      *
  308.      *
  309.      * @access     public
  310.      * @param     string     $data     The paramters string in INI format
  311.      * @param     string     $path     Path to the parameters xml file [optional]
  312.      * @since     1.5
  313.      */
  314.     function setParameters($data$path null)
  315.     {
  316.         // Assume we are using the xml file from com_users if no other xml file has been set
  317.         if (is_null($path)) {
  318.             jimport'joomla.application.helper' );
  319.             $path     JApplicationHelper::getPath'com_xml''com_users' );
  320.         }
  321.  
  322.         $this->_params->loadSetupFile($path);
  323.         $this->_params->loadINI($data);
  324.     }
  325.  
  326.     /**
  327.      * Method to get JUser error message
  328.      *
  329.      * @access     public
  330.      * @return    string    The error message
  331.      * @since    1.5
  332.      */
  333.     function getError({
  334.         return $this->_errorMsg;
  335.     }
  336.  
  337.     /**
  338.      * Method to bind an associative array of data to a user object
  339.      *
  340.      * @access     private
  341.      * @param     array     $array     The associative array to bind to the object
  342.      * @return     boolean         True on success
  343.      * @since 1.5
  344.      */
  345.     function bind($array)
  346.     {
  347.         jimport('joomla.user.helper');
  348.         jimport'joomla.utilities.array' );
  349.  
  350.         // Lets check to see if the user is new or not
  351.         if (empty($this->id/*&& $array['id']*/)
  352.         {
  353.             /*
  354.              * Since we have a new user, and we are going to create it... we
  355.              * need to check a few things and set some defaults if we don't
  356.              * already have them.
  357.              */
  358.  
  359.             // First the password
  360.             if (empty($array['password']))
  361.             {
  362.                 $array['password'JUserHelper::genRandomPassword();
  363.             }
  364.             $this->clearPW = JArrayHelper::getValue$array'password''''string' );
  365.             $array['password'JUserHelper::getCryptedPassword($array['password']);
  366.  
  367.             // Next the registration timestamp
  368.             $this->set'registerDate'date'Y-m-d H:i:s' ) );
  369.  
  370.             // check that username is not greater than 25 characters
  371.             $username $this->get'username' );
  372.             if strlen($username150 )
  373.             {
  374.                 $username substr$username0150 );
  375.                 $this->set'username'$username );
  376.             }
  377.  
  378.             // check that password is not greater than 50 characters
  379.             $password $this->get'password' );
  380.             if strlen($password100 )
  381.             {
  382.                 $password substr$password0100 );
  383.                 $this->set'password'$password );
  384.             }
  385.         }
  386.         else
  387.         {
  388.             // We are updating an existing user.. so lets get down to it.
  389.             if (!empty($array['password']))
  390.             {
  391.                 $this->clearPW = JArrayHelper::getValue$array'password''''string' );
  392.                 $array['password'JUserHelper::getCryptedPassword($array['password']);
  393.             }
  394.             else
  395.             {
  396.                 $array['password'$this->password;
  397.             }
  398.         }
  399.  
  400.         /*
  401.          * NOTE
  402.          * TODO
  403.          * @todo: this will be deprecated as of the ACL implementation
  404.          */
  405.         $db =JFactory::getDBO();
  406.         
  407.         $gid    array_key_exists('gid'$array $array['gid'$this->get('gid');
  408.  
  409.         $query 'SELECT name'
  410.         . ' FROM #__core_acl_aro_groups'
  411.         . ' WHERE id = ' . (int) $gid
  412.         ;
  413.         $db->setQuery$query );
  414.         $this->set'usertype'$db->loadResult());
  415.  
  416.         if array_key_exists('params'$array) )
  417.         {
  418.             $params    '';
  419.             $this->_params->bind($array['params']);
  420.             if is_array($array['params']) ) {
  421.                 $params    $this->_params->toString();
  422.             else {
  423.                 $params $array['params'];
  424.             }
  425.             
  426.             $this->params = $params;
  427.         }
  428.         
  429.         /*
  430.          * Lets first try to bind the array to us... if that fails
  431.          * then we can certainly fail the whole method as we've done absolutely
  432.          * no good :)
  433.          */
  434.         if (!$this->_bind($array'aid guest')) {
  435.             $this->_setError("Unable to bind array to user object");
  436.             return false;
  437.         }
  438.  
  439.         // Make sure its an integer
  440.         $this->id = (int) $this->id;
  441.  
  442.         return true;
  443.     }
  444.  
  445.     /**
  446.      * Method to save the JUser object to the database
  447.      *
  448.      * @access     private
  449.      * @param     boolean $updateOnly Save the object only if not a new user
  450.      * @return     boolean             True on success
  451.      * @since 1.5
  452.      */
  453.     function save$updateOnly false )
  454.     {
  455.         jimport'joomla.utilities.array' );
  456.         
  457.         // Create the user table object
  458.         $table     =JTable::getInstance'user');
  459.         $table->bind(JArrayHelper::fromObject($thisfalse));
  460.  
  461.         /*
  462.          * We need to get the JUser object for the current installed user, but
  463.          * might very well be modifying that user... and isn't it ironic...
  464.          * don't ya think?
  465.          */
  466.         $me =JFactory::getUser();
  467.  
  468.         /*
  469.          * Now that we have gotten all the field handling out of the way, time
  470.          * to check and store the object.
  471.          */
  472.         if (!$table->check())
  473.         {
  474.             $this->_setError($table->getError());
  475.             return false;
  476.         }
  477.  
  478.         // if user is made a Super Admin group and user is NOT a Super Admin
  479.         if $this->get('gid'== 25 && $me->get('gid'!= 25 )
  480.         {
  481.             // disallow creation of Super Admin by non Super Admin users
  482.             $this->_setError(JText::_'WARNSUPERADMINCREATE' ));
  483.             return false;
  484.         }
  485.  
  486.         //are we creating a new user
  487.         $isnew !$this->id;
  488.  
  489.         // If we aren't allowed to create new and we are  about to... return true .. job done
  490.         if ($isnew && $updateOnly{
  491.             return true;
  492.         }
  493.  
  494.         /*
  495.          * Since we have passed all checks lets load the user plugin group and
  496.          * fire the onBeforeStoreUser event.
  497.          */
  498.         JPluginHelper::importPlugin'user' );
  499.         $dispatcher =JEventDispatcher::getInstance();
  500.         $dispatcher->trigger'onBeforeStoreUser'arrayget_object_vars$table )$isnew ) );
  501.  
  502.         /*
  503.          * Time for the real thing... are you ready for the real thing?  Store
  504.          * the JUserModel ... if a fail condition exists throw a warning
  505.          */
  506.         $result false;
  507.         if (!$result $table->store()) {
  508.             $this->_setError($table->getError());
  509.         }
  510.  
  511.         /*
  512.          * If the id is not set, lets set the id for the JUser object.  This
  513.          * might happen if we just inserted a new user... and need to update
  514.          * this objects id value with the inserted id.
  515.          */
  516.         if (empty($this->id)) {
  517.             $this->id = $table->get'id' );
  518.         }
  519.  
  520.         // We stored the user... lets tell everyone about it.
  521.         $dispatcher->trigger'onAfterStoreUser'arrayget_object_vars$table )$isnew$result$this->getError() ) );
  522.  
  523.         return $result;
  524.     }
  525.  
  526.     /**
  527.      * Method to delete the JUser object from the database
  528.      *
  529.      * @access     private
  530.      * @param     boolean $updateOnly Save the object only if not a new user
  531.      * @return     boolean             True on success
  532.      * @since 1.5
  533.      */
  534.     function delete)
  535.     {
  536.         JPluginHelper::importPlugin'user' );
  537.  
  538.         //trigger the onBeforeDeleteUser event
  539.         $dispatcher =JEventDispatcher::getInstance();
  540.         $dispatcher->trigger'onBeforeDeleteUser'arrayarray'id' => $this->id ) ) );
  541.  
  542.         // Create the user table object
  543.         $table     =JTable::getInstance'user');
  544.  
  545.         $result false;
  546.         if (!$result $table->delete($this->id)) {
  547.             $this->_setError($table->getError());
  548.         }
  549.  
  550.         //trigger the onAfterDeleteUser event
  551.         $dispatcher->trigger'onAfterDeleteUser'arrayarray('id' => $this->id)$result$this->getError()) );
  552.         return $result;
  553.  
  554.     }
  555.  
  556.     /**
  557.      * Method to load a JUser object by user id number
  558.      *
  559.      * @access     public
  560.      * @param     mixed     $identifier The user id of the user to load
  561.      * @param     string     $path         Path to a parameters xml file
  562.      * @return     boolean             True on success
  563.      * @since 1.5
  564.      */
  565.     function load($id)
  566.     {
  567.         // Create the user table object
  568.         $table     =JTable::getInstance'user');
  569.  
  570.          // Load the JUserModel object based on the user id or throw a warning.
  571.          if(!$table->load($id))
  572.          {
  573.             JError::raiseWarning'SOME_ERROR_CODE''JUser::_load: Unable to load user with id: '.$id );
  574.             return false;
  575.         }
  576.  
  577.         /*
  578.          * Set the user parameters using the default xml file.  We might want to
  579.          * extend this in the future to allow for the ability to have custom
  580.          * user parameters, but for right now we'll leave it how it is.
  581.          */
  582.         $this->_params->loadINI($table->params);
  583.  
  584.         // Assuming all is well at this point lets bind the data
  585.         $this->_bind(JArrayHelper::fromObject($tablefalse));
  586.  
  587.         return true;
  588.     }
  589.  
  590.     /**
  591.     * Binds a named array/hash to this object
  592.     *
  593.     * @access    protected
  594.     * @param    $array  mixed Either and associative array or another object
  595.     * @param    $ignore string    Space separated list of fields not to bind
  596.     * @return    boolean 
  597.     * @since    1.5
  598.     */
  599.     function _bind$from$ignore='' )
  600.     {
  601.         if (!is_array$from && !is_object$from )) {
  602.             $this->_setError(strtolower(get_class$this ))."::bind failed.");
  603.             return false;
  604.         }
  605.  
  606.         $fromArray  is_array$from );
  607.         $fromObject is_object$from );
  608.  
  609.         if ($fromArray || $fromObject)
  610.         {
  611.             foreach (get_object_vars($thisas $k => $v)
  612.             {
  613.                 // only bind to public variables
  614.                 ifsubstr$k0!= '_' )
  615.                 {
  616.                     // internal attributes of an object are ignored
  617.                     if (strpos$ignore$k=== false)
  618.                     {
  619.                         $ak $k;
  620.  
  621.                         if ($fromArray && isset$from[$ak)) {
  622.                             $this->$k $from[$ak];
  623.                         else if ($fromObject && isset$from->$ak )) {
  624.                             $this->$k $from->$ak;
  625.                         }
  626.                     }
  627.                 }
  628.             }
  629.         }
  630.         else
  631.         {
  632.             return false;
  633.         }
  634.  
  635.         return true;
  636.     }
  637.  
  638.     /**
  639.      * Method to set an error message
  640.      *
  641.      * @access    private
  642.      * @param    string    $msg    The message to append to the error message
  643.      * @return    void 
  644.      * @since    1.5
  645.      */
  646.     function _setError$msg )
  647.     {
  648.         $this->_errorMsg .= $msg."\n";
  649.     }
  650. }
  651.  
  652. ?>

Documentation generated on Mon, 05 Mar 2007 21:30:25 +0000 by phpDocumentor 1.3.1