Source for file auth.coord.php

Documentation is available at auth.coord.php

  1. <?php
  2. /**
  3. @package    jelix
  4. @subpackage coord_plugin
  5. @author     Croes Gérald
  6. @contributor  Laurent Jouanneau, Frédéric Guillot, Antoine Detante, Julien Issler
  7. @copyright  2001-2005 CopixTeam, 2005-2007 Laurent Jouanneau, 2007 Frédéric Guillot, 2007 Antoine Detante
  8. @copyright  2007 Julien Issler
  9. *
  10. *  This class was get originally from an experimental branch of the Copix project
  11. *  (PluginAuth, Copix 2.3dev20050901, http://www.copix.org)
  12. *  Few lines of code are still copyrighted 2001-2005 CopixTeam (LGPL licence).
  13. *  Initial authors of this Copix classes are Gerald Croes and Laurent Jouanneau,
  14. *  and this class was adapted for Jelix by Laurent Jouanneau
  15. *
  16. @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  17. */
  18.  
  19. /**
  20.  *
  21.  */
  22. require(JELIX_LIB_AUTH_PATH.'jAuth.class.php');
  23. require(JELIX_LIB_AUTH_PATH.'jAuthDummyUser.class.php');
  24.  
  25. /**
  26.  * deprecated class. It is here only for a soft migrating from jelix 1.0b3 to 1.0
  27.  * when a jDummyAuthUser object is stored in a session
  28.  * @deprecated
  29.  */
  30. class jDummyAuthUser extends jAuthUser {
  31. }
  32.  
  33. /**
  34. @package    jelix
  35. @subpackage coord_plugin
  36. */
  37. class AuthCoordPlugin implements jICoordPlugin {
  38.     public $config;
  39.  
  40.     function __construct($conf){
  41.         $this->config = $conf;
  42.  
  43.         if (!isset($this->config['session_name'])
  44.             || $this->config['session_name'== ''){
  45.             $this->config['session_name''JELIX_USER';
  46.         }
  47.     }
  48.  
  49.     /**
  50.      * @param    array  $params   plugin parameters for the current action
  51.      * @return null or jSelectorAct  if action should change
  52.      */
  53.     public function beforeAction ($params){
  54.         $notLogged false;
  55.         $badip false;
  56.         $selector null;
  57.         // Check if auth cookie exist and user isn't logged on
  58.         if (isset($this->config['persistant_enable']&& $this->config['persistant_enable'&& !jAuth::isConnected()){
  59.             if(isset($this->config['persistant_cookie_name']&& isset($this->config['persistant_crypt_key'])){
  60.                 $cookieName=$this->config['persistant_cookie_name'];
  61.                 if(isset($_COOKIE[$cookieName]['login']&& isset($_COOKIE[$cookieName]['passwd']&& strlen($_COOKIE[$cookieName]['passwd'])>0){
  62.                     $login $_COOKIE[$cookieName]['login'];
  63.                     $encryptedPassword=$_COOKIE[$cookieName]['passwd'];
  64.                     jAuth::login($login,jCrypt::decrypt($encryptedPassword,$this->config['persistant_crypt_key']));
  65.                 }
  66.             }
  67.             else{
  68.                 throw new jException('jelix~auth.error.persistant.incorrectconfig','persistant_cookie_name, persistant_crypt_key');
  69.             }
  70.         }
  71.         //Do we check the ip ?
  72.         if ($this->config['secure_with_ip']){
  73.             if (isset ($_SESSION['JELIX_AUTH_SECURE_WITH_IP'])){
  74.                 $_SESSION['JELIX_AUTH_SECURE_WITH_IP'$this->_getIpForSecure ();
  75.             }else{
  76.                 if (($_SESSION['JELIX_AUTH_SECURE_WITH_IP'!= $this->_getIpForSecure ())){
  77.                     session_destroy ();
  78.                     $selector new jSelectorAct($this->config['bad_ip_action']);
  79.                     $notLogged true;
  80.                     $badip true;
  81.                 }
  82.             }
  83.         }
  84.  
  85.         //Creating the user's object if needed
  86.         if (isset ($_SESSION[$this->config['session_name']])){
  87.             $notLogged true;
  88.             $_SESSION[$this->config['session_name']] new jAuthDummyUser();
  89.         }else{
  90.             // This test is here only for a soft migrating from jelix 1.0b3 to 1.0
  91.             // it should be removed in futur version
  92.             if($_SESSION[$this->config['session_name']] instanceof jDummyAuthUser{
  93.                 $_SESSION[$this->config['session_name']] new jAuthDummyUser();
  94.             }
  95.  
  96.             $notLogged jAuth::isConnected();
  97.         }
  98.         if(!$notLogged && $this->config['timeout']){
  99.             if(isset($_SESSION['JELIX_AUTH_LASTTIME'])){
  100.                 if((time($_SESSION['JELIX_AUTH_LASTTIME')($this->config['timeout'*60)){
  101.                     $notLogged true;
  102.                     jAuth::logout();
  103.                     unset($_SESSION['JELIX_AUTH_LASTTIME']);
  104.                 }else{
  105.                     $_SESSION['JELIX_AUTH_LASTTIME'time();
  106.                 }
  107.             }else{
  108.                 $_SESSION['JELIX_AUTH_LASTTIME'time();
  109.             }
  110.         }
  111.         $needAuth = isset($params['auth.required']($params['auth.required']==true):$this->config['auth_required'];
  112.         $authok false;
  113.  
  114.         if($needAuth){
  115.             if($notLogged){
  116.                 if($this->config['on_error'== 
  117.                     || !$GLOBALS['gJCoord']->request->isAllowedResponse('jResponseRedirect')){
  118.                     throw new jException($this->config['error_message']);
  119.                 }else{
  120.                     if(!$badip){
  121.                         $selectornew jSelectorAct($this->config['on_error_action']);
  122.                     }
  123.                 }
  124.             }else{
  125.                 $authoktrue;
  126.             }
  127.         }else{
  128.           $authoktrue;
  129.         }
  130.  
  131.         return $selector;
  132.     }
  133.  
  134.  
  135.     public function beforeOutput(){}
  136.  
  137.     public function afterProcess (){}
  138.  
  139.     /**
  140.     * Getting IP adress of the user
  141.     * @return string 
  142.     * @access private
  143.     */
  144.     private function _getIpForSecure (){
  145.         //this method is heavily based on the article found on
  146.         // phpbuilder.com, and from the comments on the official phpdoc.
  147.         if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']&& $_SERVER['HTTP_X_FORWARDED_FOR']){
  148.             $IP_ADDR $_SERVER['HTTP_X_FORWARDED_FOR'];
  149.         }else if (isset ($_SERVER['HTTP_CLIENT_IP']&& $_SERVER['HTTP_CLIENT_IP']){
  150.             $IP_ADDR =  $_SERVER['HTTP_CLIENT_IP'];
  151.         }else{
  152.             $IP_ADDR $_SERVER['REMOTE_ADDR'];
  153.         }
  154.  
  155.         // get server ip and resolved it
  156.         $FIRE_IP_ADDR $_SERVER['REMOTE_ADDR'];
  157.         $ip_resolved gethostbyaddr($FIRE_IP_ADDR);
  158.         // builds server ip infos string
  159.         $FIRE_IP_LITT ($FIRE_IP_ADDR != $ip_resolved && $ip_resolved$FIRE_IP_ADDR." - "$ip_resolved $FIRE_IP_ADDR;
  160.         // builds client ip full infos string
  161.         $toReturn ($IP_ADDR != $FIRE_IP_ADDR"$IP_ADDR | $FIRE_IP_LITT$FIRE_IP_LITT;
  162.         return $toReturn;
  163.     }
  164. }
  165. ?>

Documentation generated on Wed, 07 Sep 2011 13:46:25 +0200 by phpDocumentor 1.4.3