Source for file jAuth.class.php

Documentation is available at jAuth.class.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage auth
  5. @author     Laurent Jouanneau
  6. @contributor Frédéric Guillot, Antoine Detante, Julien Issler, Dominique Papin, Tahina Ramaroson, Sylvain de Vathaire, Vincent Viaud
  7. @copyright  2001-2005 CopixTeam, 2005-2010 Laurent Jouanneau, 2007 Frédéric Guillot, 2007 Antoine Detante
  8. @copyright  2007-2008 Julien Issler, 2008 Dominique Papin, 2010 NEOV, 2010 BP2I
  9. *
  10. *  This classes were get originally from an experimental branch of the Copix project (Copix 2.3dev, http://www.copix.org)
  11. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  12. *  Initial author of this Copix classes is Laurent Jouanneau, and this classes were adapted for Jelix by him
  13. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  14. */
  15.  
  16. require(JELIX_LIB_PATH.'auth/jIAuthDriver.iface.php');
  17.  
  18. require(JELIX_LIB_PATH.'auth/jAuthDriverBase.class.php');
  19.  
  20. /**
  21.  * This is the main class for authentification process
  22.  * @package    jelix
  23.  * @subpackage auth
  24.  */
  25. class jAuth {
  26.  
  27.     /**
  28.      * Load the configuration of authentification, stored in the auth plugin config
  29.      * @return array 
  30.      */
  31.     protected static function  _getConfig(){
  32.         static $config null;
  33.         if($config == null){
  34.             global $gJCoord;
  35.             $plugin $gJCoord->getPlugin('auth');
  36.             if($plugin === null)
  37.                 throw new jException('jelix~auth.error.plugin.missing');
  38.             $config $plugin->config;
  39.  
  40.             if (!isset($config['session_name'])
  41.                 || $config['session_name'== '')
  42.                 $config['session_name''JELIX_USER';
  43.  
  44.             if (!isset$config['persistant_cookie_path'])
  45.                 ||  $config['persistant_cookie_path'== '')
  46.                 $config['persistant_cookie_path'$GLOBALS['gJConfig']->urlengine['basePath'];
  47.         }
  48.         return $config;
  49.     }
  50.  
  51.     /**
  52.      * return the auth driver
  53.      * @return jIAuthDriver 
  54.      */
  55.     protected static function _getDriver(){
  56.         static $driver null;
  57.         if($driver == null){
  58.             $config self::_getConfig();
  59.             $db strtolower($config['driver']);
  60.             $driver jApp::loadPlugin($db'auth''.auth.php'$config['driver'].'AuthDriver'$config[$config['driver']]);
  61.             if(is_null($driver))
  62.                 throw new jException('jelix~auth.error.driver.notfound',$db);
  63.         }
  64.         return $driver;
  65.     }
  66.  
  67.     /**
  68.      * return the value of a parameter of the configuration of the current driver
  69.      * @param string $paramName 
  70.      * @return string the value. null if it doesn't exist
  71.      */
  72.     public static function getDriverParam($paramName{
  73.         $config self::_getConfig();
  74.         $config $config[$config['driver']];
  75.         if(isset($config[$paramName]))
  76.             return $config[$paramName];
  77.         else
  78.             return null;
  79.     }
  80.  
  81.     /**
  82.      * load user data
  83.      *
  84.      * This method returns an object, generated by the driver, and which contains
  85.      * data corresponding to the given login. This method should be called if you want
  86.      * to update data of a user. see updateUser method.
  87.      *
  88.      * @param string $login 
  89.      * @return object the user
  90.      */
  91.     public static function getUser($login){
  92.         $dr self::_getDriver();
  93.         return $dr->getUser($login);
  94.     }
  95.  
  96.     /**
  97.      * Create a new user object
  98.      *
  99.      * You should call this method if you want to create a new user. It returns an object,
  100.      * representing a user. Then you should fill its properties and give it to the saveNewUser
  101.      * method.
  102.      *
  103.      * @param string $login the user login
  104.      * @param string $password the user password (not encrypted)
  105.      * @return object the returned object depends on the driver
  106.      * @since 1.0b2
  107.      */
  108.     public static function createUserObject($login,$password){
  109.         $dr self::_getDriver();
  110.         return $dr->createUserObject($login,$password);
  111.     }
  112.  
  113.     /**
  114.      * Save a new user
  115.      *
  116.      * if the saving has succeed, a AuthNewUser event is sent
  117.      * The given object should have been created by calling createUserObject method :
  118.      *
  119.      * example :
  120.      *  <pre>
  121.      *   $user = jAuth::createUserObject('login','password');
  122.      *   $user->email ='bla@foo.com';
  123.      *   jAuth::saveNewUser($user);
  124.      *  </pre>
  125.      *  the type of $user depends of the driver, so it can have other properties.
  126.      *
  127.      * @param  object $user the user data
  128.      * @return object the user (eventually, with additional data)
  129.      */
  130.     public static function saveNewUser($user){
  131.         $dr self::_getDriver();
  132.         if($dr->saveNewUser($user))
  133.             jEvent::notify ('AuthNewUser'array('user'=>$user));
  134.         return $user;
  135.     }
  136.  
  137.     /**
  138.      * update user data
  139.      *
  140.      * It send a AuthUpdateUser event if the saving has succeed. If you want
  141.      * to change the user password, you must use jAuth::changePassword method
  142.      * instead of jAuth::updateUser method.
  143.      *
  144.      * The given object should have been created by calling getUser method.
  145.      * Example :
  146.      *  <pre>
  147.      *   $user = jAuth::getUser('login');
  148.      *   $user->email ='bla@foo.com';
  149.      *   jAuth::updateUser($user);
  150.      *  </pre>
  151.      *  the type of $user depends of the driver, so it can have other properties.
  152.      *
  153.      * @param object $user  user data
  154.      */
  155.     public static function updateUser($user){
  156.         $dr self::_getDriver();
  157.         if($dr->updateUser($user=== false)
  158.             return false;
  159.  
  160.         if(self::isConnected(&& self::getUserSession()->login === $user->login){
  161.             $config self::_getConfig();
  162.             $_SESSION[$config['session_name']] $user;
  163.         }
  164.         jEvent::notify ('AuthUpdateUser'array('user'=>$user));
  165.         return true;
  166.     }
  167.  
  168.     /**
  169.      * remove a user
  170.      * send first AuthCanRemoveUser event, then if ok, send AuthRemoveUser
  171.      * and then remove the user.
  172.      * @param string $login the user login
  173.      * @return boolean true if ok
  174.      */
  175.     public static function removeUser($login){
  176.         $dr self::_getDriver();
  177.         $eventresp jEvent::notify ('AuthCanRemoveUser'array('login'=>$login));
  178.         foreach($eventresp->getResponse(as $rep){
  179.             if(!isset($rep['canremove']|| $rep['canremove'=== false)
  180.                 return false;
  181.         }
  182.         $user $dr->getUser($login);
  183.         if($dr->removeUser($login)===false)
  184.             return false;
  185.         jEvent::notify ('AuthRemoveUser'array('login'=>$login'user'=>$user));
  186.         if(self::isConnected(&& self::getUserSession()->login === $login)
  187.             self::logout();
  188.         return true;
  189.     }
  190.  
  191.     /**
  192.      * construct the user list
  193.      * @param string $pattern '' for all users
  194.      * @return array array of object
  195.      */
  196.     public static function getUserList($pattern '%'){
  197.         $dr self::_getDriver();
  198.         return $dr->getUserlist($pattern);
  199.     }
  200.  
  201.     /**
  202.      * change a user password
  203.      *
  204.      * @param string $login the login of the user
  205.      * @param string $newpassword the new password (not encrypted)
  206.      * @return boolean true if the change succeed
  207.      */
  208.     public static function changePassword($login$newpassword){
  209.         $dr self::_getDriver();
  210.         if($dr->changePassword($login$newpassword)===false)
  211.             return false;
  212.         if(self::isConnected(&& self::getUserSession()->login === $login){
  213.             $config self::_getConfig();
  214.             $_SESSION[$config['session_name']] self::getUser($login);
  215.         }
  216.         return true;
  217.     }
  218.  
  219.     /**
  220.      * verify that the password correspond to the login
  221.      * @param string $login the login of the user
  222.      * @param string $password the password to test (not encrypted)
  223.      * @return object|false if ok, returns the user as object
  224.      */
  225.     public static function verifyPassword($login$password){
  226.         $dr self::_getDriver();
  227.         return $dr->verifyPassword($login$password);
  228.     }
  229.  
  230.     /**
  231.      * authentificate a user, and create a user in the php session
  232.      * @param string $login the login of the user
  233.      * @param string $password the password to test (not encrypted)
  234.      * @param boolean $persistant (optional) the session must be persistant
  235.      * @return boolean true if authentification is ok
  236.      */
  237.     public static function login($login$password$persistant=false){
  238.  
  239.         $dr self::_getDriver();
  240.         $config self::_getConfig();
  241.  
  242.         $eventresp jEvent::notify ('AuthBeforeLogin'array('login'=>$login));
  243.         foreach($eventresp->getResponse(as $rep){
  244.             if(isset($rep['processlogin']&& $rep['processlogin'=== false)
  245.                 return false;
  246.         }
  247.  
  248.         if($user $dr->verifyPassword($login$password)){
  249.  
  250.             $eventresp jEvent::notify ('AuthCanLogin'array('login'=>$login'user'=>$user));
  251.             foreach($eventresp->getResponse(as $rep){
  252.                 if(!isset($rep['canlogin']|| $rep['canlogin'=== false)
  253.                     return false;
  254.             }
  255.  
  256.             $_SESSION[$config['session_name']] $user;
  257.             $persistence 0;
  258.  
  259.             // Add a cookie for session persistance, if enabled
  260.             if ($persistant && isset($config['persistant_enable']&& $config['persistant_enable']{
  261.                 if (!isset($config['persistant_crypt_key']|| !isset($config['persistant_cookie_name'])) {
  262.                     throw new jException('jelix~auth.error.persistant.incorrectconfig','persistant_cookie_name, persistant_crypt_key');
  263.                 }
  264.  
  265.                 if (isset($config['persistant_duration']))
  266.                     $persistence $config['persistant_duration']*86400;
  267.                 else
  268.                     $persistence 86400// 24h
  269.                 $persistence += time();
  270.                 $encrypted jCrypt::encrypt(serialize(array($login$password)),$config['persistant_crypt_key']);
  271.                 setcookie($config['persistant_cookie_name'].'[auth]'$encrypted$persistence$config['persistant_cookie_path']);
  272.             }
  273.  
  274.             jEvent::notify ('AuthLogin'array('login'=>$login'persistence'=>$persistence));
  275.             return true;
  276.         }else{
  277.             jEvent::notify ('AuthErrorLogin'array('login'=>$login));
  278.             return false;
  279.         }
  280.     }
  281.  
  282.     /**
  283.      * Check if persistant session is enabled in config
  284.      * @return boolean true if persistant session in enabled
  285.      */
  286.     public static function isPersistant(){
  287.         $config self::_getConfig();
  288.         if(!isset($config['persistant_enable']))
  289.             return false;
  290.         else
  291.             return $config['persistant_enable'];
  292.     }
  293.  
  294.     /**
  295.      * logout a user and delete the user in the php session
  296.      */
  297.     public static function logout(){
  298.  
  299.         $config self::_getConfig();
  300.         jEvent::notify ('AuthLogout'array('login'=>$_SESSION[$config['session_name']]->login));
  301.         $_SESSION[$config['session_name']] new jAuthDummyUser();
  302.  
  303.         if(isset($config['persistant_enable']&& $config['persistant_enable']){
  304.             if(!isset($config['persistant_cookie_name']))
  305.                 throw new jException('jelix~auth.error.persistant.incorrectconfig','persistant_cookie_name, persistant_crypt_key');
  306.             setcookie($config['persistant_cookie_name'].'[auth]'''time(3600$config['persistant_cookie_path']);
  307.         }
  308.     }
  309.  
  310.     /**
  311.      * Says if the user is connected
  312.      * @return boolean 
  313.      */
  314.     public static function isConnected(){
  315.         $config self::_getConfig();
  316.         return (isset($_SESSION[$config['session_name']]&& $_SESSION[$config['session_name']]->login != '');
  317.     }
  318.  
  319.    /**
  320.     * return the user stored in the php session
  321.     * @return object the user data
  322.     */
  323.     public static function getUserSession (){
  324.         $config self::_getConfig();
  325.         if (isset ($_SESSION[$config['session_name']]))
  326.             $_SESSION[$config['session_name']] new jAuthDummyUser();
  327.         return $_SESSION[$config['session_name']];
  328.     }
  329.  
  330.     /**
  331.      * generate a password with random letters, numbers and special characters
  332.      * @param int $length the length of the generated password
  333.      * @return string the generated password
  334.      */
  335.     public static function getRandomPassword($length 10$withoutSpecialChars false){
  336.         if ($length 10)
  337.             $length 10;
  338.         $nbNumber floor($length/4);
  339.         if ($nbNumber 2)
  340.             $nbNumber 2;
  341.         if ($withoutSpecialChars)
  342.             $nbSpec 0;
  343.         else {
  344.             $nbSpec floor($length/5);
  345.             if ($nbSpec 1)
  346.                 $nbSpec 1;
  347.         }
  348.  
  349.         $nbLower floor(($length-$nbNumber-$nbSpec)/2);
  350.         $nbUpper $length-$nbNumber-$nbLower-$nbSpec;
  351.  
  352.         $pass '';
  353.  
  354.         $letter "1234567890";
  355.         for($i=0;$i<$nbNumber;$i++)
  356.             $pass .= $letter[rand(0,9)];
  357.  
  358.         $letter '!@#$%^&*?_,~';
  359.         for($i=0;$i<$nbSpec;$i++)
  360.             $pass .= $letter[rand(0,11)];
  361.  
  362.         $letter "abcdefghijklmnopqrstuvwxyz";
  363.         for($i=0;$i<$nbLower;$i++)
  364.             $pass .= $letter[rand(0,25)];
  365.  
  366.         $letter "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  367.         for($i=0;$i<$nbUpper;$i++)
  368.             $pass .= $letter[rand(0,25)];
  369.  
  370.         return str_shuffle($pass);
  371.     }
  372. }

Documentation generated on Mon, 19 Sep 2011 14:11:53 +0200 by phpDocumentor 1.4.3