linea21-modules
[ class tree: linea21-modules ] [ index: linea21-modules ] [ all elements ]

Source for file class.auth.php

Documentation is available at class.auth.php

  1. <?php
  2. /**
  3.  * @package linea21.modules
  4.  * @subpackage auth
  5.  * @author Simon Georget <simon@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  Plugin Management
  10.  */
  11.  
  12. class auth {
  13.  
  14.  
  15.     protected $dispatcher = null;
  16.  
  17.     public function __construct({
  18.         $this->dispatcher = $GLOBALS['dispatcher'];
  19.     }
  20.  
  21.     public function __call($method$arguments{
  22.         $event $this->dispatcher->notifyUntil(new sfEvent($this'auth.extensible_function'array(
  23.                 'method'    => $method,
  24.                 'arguments' => $arguments
  25.         )));
  26.         if (!$event->isProcessed())
  27.         {
  28.             throw new Exception(sprintf('Call to undefined method %s::%s.'get_class($this)$method));
  29.         }
  30.  
  31.         return $event->getReturnValue();
  32.     }
  33.  
  34.     /**
  35.      * AuthenthificationProcess()
  36.      * Authentification d'un utilisateur
  37.      *
  38.      * @param  $login 
  39.      * @param  $pass 
  40.      * @param  $type ='PUBLIC' || 'ADMIN'
  41.      * @return boolean (true) ou message d'erreur
  42.      */
  43.     public function logIn($login$pass$type$rememberme)
  44.     {
  45.  
  46.         // Notify the beginning of the current method
  47.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.logIn'array('login' => $login'password' => $pass'type' => $type)));
  48.         
  49.         return $this->builtInAuth($login$pass$type$rememberme);
  50.             
  51.     }
  52.     
  53.     protected function builtInAuth($login$pass$type$rememberme{
  54.         
  55.         if($type == 'PUBLIC'$src LOG_PUBLIC_ACCESS;
  56.         if($type == 'ADMIN'$src LOG_ADMIN_ACCESS;
  57.  
  58.         $crypt_pass crypt($passSALT_CRYPT);
  59.         list($login$crypt_pass$GLOBALS['sql_object']->DBEscape(array($login$crypt_pass));
  60.         
  61.         $data $GLOBALS['sql_object']->DBSelect(SQL_Get_UserInfo4Auth($login$crypt_pass));
  62.  
  63.         if ($data != && count($data== 1{
  64.  
  65.             $this->initSession($data$login);
  66.  
  67.             if($rememberme{
  68.                 $this->setCookieRememberMe();
  69.             }
  70.  
  71.             if ($type == 'ADMIN'{
  72.                 // si est simple utilisateur
  73.                 if (!array_search ('A' $_SESSION['userright']&& !array_search ('O' $_SESSION['userright'])) {
  74.                     
  75.                     $this->logOut();
  76.                     
  77.                     logfile($srcarray($type$login'unauthorized'__METHOD__$_SERVER['HTTP_USER_AGENT']$_SERVER['HTTP_REFERER']$_SERVER['REQUEST_METHOD']$_SERVER['REQUEST_URI']$_SERVER['QUERY_STRING']$_SERVER['HTTP_COOKIE']i2c_realip()));
  78.                         
  79.                     return _t('divers','errorauth');
  80.                 }
  81.             }
  82.             
  83.             logfile($srcarray($type$login'success'__METHOD__$_SERVER['HTTP_USER_AGENT']$_SERVER['HTTP_REFERER']$_SERVER['REQUEST_METHOD']$_SERVER['REQUEST_URI']$_SERVER['QUERY_STRING']$_SERVER['HTTP_COOKIE']i2c_realip()));
  84.                 
  85.             
  86.             return true;
  87.             
  88.         else {
  89.             
  90.             logfile($srcarray($type$login'failed'__METHOD__$_SERVER['HTTP_USER_AGENT']$_SERVER['HTTP_REFERER']$_SERVER['REQUEST_METHOD']$_SERVER['REQUEST_URI']$_SERVER['QUERY_STRING']$_SERVER['HTTP_COOKIE']i2c_realip()));
  91.             
  92.             $error_msg _t('divers','errorauth');
  93.             return $error_msg;
  94.             
  95.         }
  96.  
  97.     }
  98.     
  99.     /**
  100.      * isSimpleUser()
  101.      *
  102.      * @return boolean 
  103.      */
  104.     public function isSimpleUser({
  105.         if(!isset($_SESSION['userright'])) return false;
  106.  
  107.         if(!in_array('A'$_SESSION['userright']&& !in_array('O'$_SESSION['userright'])) {
  108.             return true;
  109.         else {
  110.             return false;
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * isSuperAdmin()
  116.      *
  117.      * @return boolean 
  118.      */
  119.     public function isSuperAdmin({
  120.         return $this->hasRight('theme');
  121.     }
  122.  
  123.     /**
  124.      * logOut()
  125.      *
  126.      * @return boolean 
  127.      */
  128.     public function logOut({
  129.  
  130.         $_SESSION array();
  131.         session_unset();
  132.         session_destroy();
  133.         $this->destroyUserCookie();
  134.         
  135.         if(isset($_COOKIE['linea21_uname'])) {
  136.             
  137.             list($login$cookiehashexplode ('_' $_COOKIE['linea21_uname']);
  138.             
  139.             // we clean user_cookiehash field is set
  140.             $GLOBALS['sql_object']->DBQuery('UPDATE l21_user SET user_cookiehash="" WHERE user_cookiehash = "' $GLOBALS['sql_object']->DBEscape($cookiehash'" AND lower(user_login) = "'$GLOBALS['sql_object']->DBEscape($login'" ;');
  141.             setcookie('linea21_uname'''(time(3600)'/');
  142.         }
  143.         
  144.     }
  145.     /**
  146.      * hasRight()
  147.      *
  148.      * @param string $item 
  149.      * @return bool 
  150.      */
  151.     public function hasRight($item)
  152.     {
  153.         // Notify the beginning of the current method
  154.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.has_right'array('element' => $item)));
  155.  
  156.         switch ($item{
  157.             case 'category_user':
  158.                 if ($this->GetSessionElement('right_category_user'!= 'A'return false;
  159.                 else return true;
  160.             case 'theme':
  161.                 if ($this->GetSessionElement('right_theme'!= 'A'return false;
  162.                 else return true;
  163.             case 'level':
  164.                 if ($this->GetSessionElement('right_level'!= 'A'return false;
  165.                 else return true;
  166.             case 'scale':
  167.                 if ($this->GetSessionElement('right_scale'!= 'A'return false;
  168.                 else return true;
  169.             case 'yellowpages':
  170.                 if ($this->GetSessionElement('right_yellowpages'!= 'A'return false;
  171.                 else return true;
  172.             case 'news':
  173.                 if ($this->GetSessionElement('right_news'!= 'U'return true;
  174.                 else return false;
  175.             case 'project':
  176.                 if ($this->GetSessionElement('right_project'!= 'A'return false;
  177.                 else return true;
  178.             case 'publication':
  179.                 if ($this->GetSessionElement('right_publication'!= 'U'return true;
  180.                 else return false;
  181.             case 'workshop':
  182.                 if ($this->GetSessionElement('right_workshop'!= 'U'return true;
  183.                 else return false;
  184.             case 'workshoprep':
  185.                 if ($this->GetSessionElement('right_workshop'!= 'U'return true;
  186.                 else return false;
  187.             case 'dashboard':
  188.                 if ($this->GetSessionElement('right_dashboard'!= 'U'return true;
  189.                 else return false;
  190.             default:
  191.                 return false;
  192.         }
  193.     }
  194.     
  195.     
  196.      /**
  197.      * initSession()
  198.      * Init user session
  199.      *
  200.      * @param  $data 
  201.      * @return boolean (true) ou message d'erreur
  202.      */
  203.     public function initSession($data$login{
  204.         
  205.         // préparation du tableau de droits
  206.         $right['dashboard'$data[0]['rights_dashboard'];
  207.         $right['workshop'$data[0]['rights_workshop'];
  208.         $right['project'$data[0]['rights_project'];
  209.         $right['publication'$data[0]['rights_publication'];
  210.         $right['news'$data[0]['rights_news'];
  211.         $right['yellowpages'$data[0]['rights_yellowpages'];
  212.         $right['theme'$data[0]['rights_theme'];
  213.         $right['scale'$data[0]['rights_scale'];
  214.         $right['level'$data[0]['rights_level'];
  215.         $right['category_user'$data[0]['rights_category_user'];
  216.         
  217.         $_SESSION['authenticated'true;
  218.         $_SESSION['userid'base64_encode($data[0]['user_id']);
  219.         $_SESSION['userwhois'base64_encode($data[0]['user_password']);
  220.         $_SESSION['userlogin'$login;
  221.         $_SESSION['lastcon'$data[0]['user_last_con'];
  222.         
  223.         if(!empty($data[0]['profile_firstname'])) {
  224.             $_SESSION['userfirstname'$data[0]['profile_firstname'];
  225.         }
  226.         if(!empty($data[0]['profile_lastname'])) {
  227.             $_SESSION['userlastname'$data[0]['profile_lastname'];
  228.         }
  229.         $_SESSION['userright'$right;
  230.         
  231.         $workgroups $this->retrieveUserWorkgroups(array('login' => $login'pass' => $data[0]['user_password']));
  232.         $table['workshop'base64_encode($workgroups);
  233.         $table['lifetime'time(3600 720;
  234.         
  235.         $this->destroyUserCookie();
  236.             
  237.         // we update the last connexion field
  238.         $GLOBALS['sql_object']->DBQuery('UPDATE l21_user SET user_last_con = NOW() where user_id=' $data[0]['user_id'';');
  239.         
  240.         // Filter data event + return value
  241.         $r $this->dispatcher->filter(new sfEvent(__FUNCTION__'auth.extend_session'array('data' => $table))$table);
  242.         $table $r->getReturnValue();
  243.         
  244.         $this->setUserCookie($table);
  245.         
  246.         return true;
  247.     }
  248.  
  249.     /**
  250.      * GetSessionElement()
  251.      *
  252.      * @param string $item 
  253.      * @return $result 
  254.      */
  255.  
  256.     public function GetSessionElement($item)    {
  257.         // Notify the beginning of the current method
  258.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.get_session_element'array('element' => $item)));
  259.  
  260.         switch ($item{
  261.             case 'id':
  262.                 return base64_decode($_SESSION['userid']);
  263.                 break;
  264.             case 'whois':
  265.                 return base64_decode($_SESSION['userwhois']);
  266.                 break;
  267.             case 'login':
  268.                 return $_SESSION['userlogin'];
  269.                 break;
  270.             case 'firstname':
  271.                 if(isset($_SESSION['userfirstname']))
  272.                     return $_SESSION['userfirstname'];
  273.                 break;
  274.             case 'lastname':
  275.                 if(isset($_SESSION['userlastname']))
  276.                     return $_SESSION['userlastname'];
  277.                 break;
  278.             case 'right_theme':
  279.                 return $_SESSION['userright']['theme'];
  280.                 break;
  281.             case 'right_dashboard':
  282.                 return $_SESSION['userright']['dashboard'];
  283.                 break;
  284.             case 'right_workshop':
  285.                 return $_SESSION['userright']['workshop'];
  286.                 break;
  287.             case 'right_project':
  288.                 return $_SESSION['userright']['project'];
  289.                 break;
  290.             case 'right_publication':
  291.                 return $_SESSION['userright']['publication'];
  292.                 break;
  293.             case 'right_news':
  294.                 return $_SESSION['userright']['news'];
  295.                 break;
  296.             case 'right_yellowpages':
  297.                 return $_SESSION['userright']['yellowpages'];
  298.                 break;
  299.             case 'right_scale':
  300.                 return $_SESSION['userright']['scale'];
  301.                 break;
  302.             case 'right_level':
  303.                 return $_SESSION['userright']['level'];
  304.                 break;
  305.             case 'right_category_user':
  306.                 return $_SESSION['userright']['category_user'];
  307.                 break;
  308.             default:
  309.                 return false;
  310.                 break;
  311.         }
  312.     }
  313.  
  314.     /**
  315.      * isWorkgroupUser()
  316.      * check if the current user belongs to a workgroup
  317.      * used in PUBLIC app
  318.      * @param  $workshop_id 
  319.      * @return boolean 
  320.      */
  321.     public function isWorkgroupUser($workshop_id$sql_object)
  322.     {
  323.         // check if user has cookie, if not we exit
  324.         if(!$this->isAuthenticated()) return false;
  325.  
  326.         // if is SuperAdmin we allow the access
  327.         if($this->isSuperAdmin()) return true;
  328.  
  329.         // Notify the beginning of the current method
  330.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.is_workgroup_user'array('id' => $workshop_id)));
  331.  
  332.         include_once('../admin/'.SQL.'.inc.php');
  333.  
  334.         // 1 -we check that login and password stored in sessions are corrects
  335.         $q SQL_Get_UserInfo4Auth($this->GetSessionElement('login')$this->GetSessionElement('whois'));
  336.         $data $sql_object->DBSelect($q);
  337.  
  338.         if ($data != && count($data== && $data[0]['user_id'== $this->GetSessionElement('id')) {
  339.             // 2 - if ok,  we check that the user belongs to the given group
  340.             $r $sql_object->DBSelect(SQL_Get_isWorkgroupUser($data[0]['user_id']));
  341.  
  342.             for ($i 0$i count($r)$i++{
  343.                 if ($workshop_id == $r[$i]['jwu_workshop_id']{
  344.                     return true;
  345.                 }
  346.  
  347.             }
  348.         }
  349.         return false;
  350.     }
  351.  
  352.     /**
  353.      * isWorkgroupOrganiser()
  354.      * check if a given user is workgroup organiser
  355.      * used in ADMIN app
  356.      * @param  $id_user 
  357.      * @param  $sql_object 
  358.      * @param  $id_workshop 
  359.      * @return boolean 
  360.      */
  361.     public function isWorkgroupOrganiser($id_user$sql_object$id_workshop)
  362.     {
  363.         // Notify the beginning of the current method
  364.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.is_workgroup_organiser'array('id' => $id_user'workgroup_id' => $id_workshop)));
  365.  
  366.         $data $sql_object->DBSelect(SQL_Get_isWorkgroupOrganiser($id_user));
  367.  
  368.         for ($i 0$i count($data)$i++{
  369.             if ($id_workshop == $data[$i]['jwu_workshop_id'])
  370.                 return true;
  371.         }
  372.         return false;
  373.     }
  374.  
  375.     public function isAuthenticated({
  376.         if(isset($_SESSION['authenticated'])) return true;
  377.         else return false;
  378.     }
  379.  
  380.     /**
  381.      * identifyWithLogin()
  382.      * Authentification d'un utilisateur
  383.      * par son login seulement
  384.      * (oubli du mot de passe)
  385.      * @param  $login 
  386.      * @return array 
  387.      */
  388.     public function identifyWithLogin($login{
  389.  
  390.         // Notify the beginning of the current method
  391.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.identify_login'array('login' => $login)));
  392.  
  393.         $login $GLOBALS['sql_object']->DBEscape($login);
  394.         
  395.         $data $GLOBALS['sql_object']->DBSelect(SQL_get_UserInfo($login));
  396.         if ($data != && count($data== 1{
  397.             return $data[0];
  398.         else {
  399.             return false;
  400.         }
  401.     }
  402.  
  403.  
  404.     /**
  405.      * retrieveUserWorkgroups()
  406.      * Retrieve Workgroups for a given user
  407.      * @param  $a array (optional)
  408.      * @return string 
  409.      */
  410.     public function retrieveUserWorkgroups($a array()) {
  411.  
  412.         // user already logged-in
  413.         if(isset($_SESSION['authenticated'])) {
  414.             $login $_SESSION['userlogin'];
  415.             $crypt_pass base64_decode($_SESSION['userwhois']);
  416.         }
  417.         else
  418.         {
  419.             if(isset($a['login'])) {
  420.                 $login $a['login'];
  421.                 $crypt_pass $a['pass'];
  422.             else {
  423.                 return false;
  424.             }
  425.         }
  426.  
  427.         // Notify the beginning of the current method
  428.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.retrieve_user_workgroups'array('login' => $login'password' => $crypt_pass)));
  429.  
  430.         $data_w $GLOBALS['sql_object']->DBSelect(SQL_Get_UserWorkshop($login$crypt_pass));
  431.         $workgroups '';
  432.         for ($i 0$i count($data_w)$i++{
  433.             $workgroups .= "/" $data_w[$i]['jwu_workshop_id'];
  434.         }
  435.         return $workgroups;
  436.  
  437.     }
  438.  
  439.     /**
  440.      * updateSessionPassword()
  441.      * Mise à jour du mot de passe en session (cas de changement)
  442.      * @param  $newpass 
  443.      * @return boolean 
  444.      */
  445.     public function updateSessionPassword($newpass{
  446.  
  447.         // Notify the beginning of the current method;
  448.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.update_cookie_password'));
  449.  
  450.         $_SESSION['userwhois'base64_encode($newpass);
  451.  
  452.         return true;
  453.     }
  454.  
  455.     /**
  456.      * updateCookieWorkshop()
  457.      * Update workgroups list in cookie
  458.      * used in public/ part
  459.      *
  460.      * @return void 
  461.      */
  462.     function updateCookieWorkshop()
  463.     {
  464.         // Notify the beginning of the current method
  465.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.update_cookie_workshop'));
  466.  
  467.         $str base64_encode($this->retrieveUserWorkgroups());
  468.         setcookie('linea21[workshop]'$str$_COOKIE['linea21']['lifetime']'/');
  469.     }
  470.  
  471.     /**
  472.      * setUserCookie()
  473.      * set les infos contenu en tableau PHP en cookie
  474.      *
  475.      * @param  $array 
  476.      * @return void 
  477.      */
  478.     public function setUserCookie($array)
  479.     {
  480.         // Notify the beginning of the current method
  481.         // Filter data event + return value
  482.         $r $this->dispatcher->filter(new sfEvent(__FUNCTION__'auth.set_user_cookie'array('data' => $array))$array);
  483.         $array $r->getReturnValue();
  484.  
  485.         $cookie_expires $array['lifetime'];
  486.         while (list($key$value@each($array)) {
  487.             setcookie('linea21[' $key ']'$value$cookie_expires'/');
  488.         }
  489.     }
  490.  
  491.     /**
  492.      * destroyUserCookie()
  493.      * Détruit les infos contenues en cookie
  494.      *
  495.      * @return void 
  496.      */
  497.     public function destroyUserCookie()
  498.     {
  499.         // Notify the beginning of the current method
  500.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.destroy_user_cookie'));
  501.  
  502.         while (list($key$val@each($_COOKIE['linea21'])) {
  503.             setcookie('linea21[' $key ']'''(time(3600)'/');
  504.         }
  505.     }
  506.  
  507.     /**
  508.      * setCookieLastPage()
  509.      * used for the admin/ part
  510.      */
  511.     public function setCookieLastPage({
  512.  
  513.         // Notify the beginning of the current method
  514.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.set_cookie_lastpage'));
  515.  
  516.         if(isset($_SESSION['authenticated'])) {
  517.             setcookie('linea21_lastpage''index.php?'.$_SERVER['QUERY_STRING'](time(3600 720)'/');
  518.             setcookie('linea21_lastuser'$_SESSION['userlogin'](time(3600 720)'/');
  519.             setcookie('linea21_lastactivity'time()(time(3600 720)'/');
  520.         }
  521.         return true;
  522.     }
  523.     
  524.     /**
  525.      * setCookieLastPage()
  526.      * used for the admin/ part
  527.      */
  528.     public function setCookieRememberMe({
  529.         
  530.         if(!defined('REMEMBER_ME'|| REMEMBER_ME == 0return true;
  531.     
  532.         // Notify the beginning of the current method
  533.         $this->dispatcher->notify(new sfEvent(__FUNCTION__'auth.set_cookie_rememberme'));
  534.     
  535.         if(isset($_SESSION['authenticated'])) {
  536.             $cookiehash md5(str_shuffle($_SESSION['userwhois'$_SESSION['userlogin']));
  537.             setcookie("linea21_uname"$_SESSION['userlogin''_' $cookiehashtime()REMEMBER_ME,'/');
  538.             
  539.             // we update the last connexion field
  540.             $GLOBALS['sql_object']->DBQuery('UPDATE l21_user SET user_cookiehash = "' $GLOBALS['sql_object']->DBEscape($cookiehash'" WHERE lower(user_login) = "' $_SESSION['userlogin''";');
  541.         }
  542.         return true;
  543.     }
  544.     
  545.     /**
  546.      * isActive()
  547.      * check if last activity is less than SESS_INACTIVITY_MAXTIME
  548.      * If more, destroy the session
  549.      */
  550.     public function isActive({
  551.         
  552.         include_once('../admin/'.SQL.'.inc.php');
  553.  
  554.         if (!$this->isAuthenticated()) {
  555.             
  556.             // if rememberme option is disabled, we exit
  557.             if(!defined('REMEMBER_ME'|| REMEMBER_ME == 0return false;
  558.             
  559.             // if rememberme is set
  560.             if(isset($_COOKIE['linea21_uname'])) {
  561.                 
  562.                 list($login$cookiehashexplode ('_' $_COOKIE['linea21_uname']);
  563.  
  564.                 $data $GLOBALS['sql_object']->DBSelect(SQL_Get_UserFromRememberMe($cookiehash$login));
  565.  
  566.                 if ($data != && count($data== 1{
  567.  
  568.                     $this->initSession($data$login);                        
  569.                         
  570.                     return true;
  571.                 }
  572.                     
  573.             else {
  574.                 return false;
  575.             }
  576.         }
  577.  
  578.         if (isset($_SESSION['LAST_ACTIVITY']&& (time($_SESSION['LAST_ACTIVITY'SESS_INACTIVITY_MAXTIME)) {
  579.             // last request was more than SESS_INACTIVITY_MAXTIME (in seconds) ago
  580.             session_unset();     // unset $_SESSION variable for the run-time
  581.             session_destroy();   // destroy session data in storage
  582.             $_SESSION array();
  583.                 
  584.             return false;
  585.         }
  586.         $_SESSION['LAST_ACTIVITY'time()// update last activity time stamp
  587.  
  588.         return true;
  589.     }
  590.  
  591. }
  592.  
  593. ?>

Documentation generated on Thu, 20 Mar 2014 16:46:00 +0100 by phpDocumentor 1.4.1