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

Source for file functions_auth.php

Documentation is available at functions_auth.php

  1. <?php
  2. /**
  3.  * @package linea21.utils
  4.  * @subpackage lib
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  */
  10.  
  11. /**
  12. * AuthenthificationProcess()
  13. * Authentification d'un utilisateur
  14. * 
  15. * @param  $login 
  16. * @param  $pass 
  17. * @param  $type ='PUBLIC' || 'ADMIN'
  18. * @return boolean (true) ou message d'erreur
  19. */
  20.  
  21. function AuthenthificationProcess($login, $pass, $type)
  22. {
  23.     LogAccess($type, $login);
  24.     $crypt_pass = crypt($pass, SALT_CRYPT);
  25.     $requete = SQL_Get_UserInfo4Auth($login, $crypt_pass);
  26.  
  27.     $data = $GLOBALS['sql_object']->DBSelect(SQL_Get_UserInfo4Auth($login, $crypt_pass));
  28.     if ($data != 0 && count($data) == 1) {
  29.         if ($type == 'PUBLIC') {
  30.             $requete = SQL_Get_UserWorkshop($login, $crypt_pass);
  31.             $data_w = $GLOBALS['sql_object']->DBSelect(SQL_Get_UserWorkshop($login, $crypt_pass));
  32.             $workshop_list = '';
  33.             for ($i = 0; $i < count($data_w); $i++) {
  34.                 $workshop_list .= "/" . $data_w[$i]['jwu_workshop_id'];
  35.             } 
  36.             $table['id'] = $data[0]['user_id'];
  37.             $table['whois'] = base64_encode($crypt_pass);
  38.             $table['login'] = $login;
  39.             $table['workshop'] = base64_encode($workshop_list);
  40.             DestroyUserCookie();
  41.             SetUserCookie($table);
  42.         } 
  43.         if ($type == 'ADMIN') {
  44.             // préparation du tableau de droits
  45.             $right['dashboard'] = $data[0]['rights_dashboard'];
  46.             $right['workshop'] = $data[0]['rights_workshop'];
  47.             $right['project'] = $data[0]['rights_project'];
  48.             $right['publication'] = $data[0]['rights_publication'];
  49.             $right['news'] = $data[0]['rights_news'];
  50.             $right['yellowpages'] = $data[0]['rights_yellowpages'];
  51.             $right['theme'] = $data[0]['rights_theme'];
  52.             $right['scale'] = $data[0]['rights_scale'];
  53.             $right['level'] = $data[0]['rights_level'];
  54.             $right['category_user'] = $data[0]['rights_category_user']; 
  55.             // est simple utilisateur
  56.             if (!array_search ('A' , $right) && !array_search ('O' , $right)) {
  57.                 return _t('divers','errorauth'); 
  58.                 // est utilisateur avec droits
  59.             } else {
  60.                 $_SESSION['sess_is_known'] = true;
  61.                 $_SESSION['sess_id'] = base64_encode($data[0]['user_id']);
  62.                 $_SESSION['sess_whois'] = base64_encode($crypt_pass);
  63.                 $_SESSION['sess_login'] = $login;
  64.                 $_SESSION['sess_right'] = $right;
  65.                 Set_Cookie_last_connexion();
  66.             } 
  67.         } 
  68.         return true;
  69.     } else {
  70.         $error_msg = _t('divers','errorauth');
  71.         return $error_msg;
  72.     } 
  73. } 
  74.  
  75. /**
  76. * WorkshopAuth_id()
  77. * 
  78. * @param  $id_user 
  79. * @param  $sql_object 
  80. * @param  $id_workshop 
  81. * @return boolean 
  82. */
  83. function WorkshopAuth_id ($id_user, $sql_object, $id_workshop)
  84. {
  85.     $data = $sql_object->DBSelect(SQL_Get_UserInfo4Authid($id_user));
  86.     $workshop_list = '';
  87.     $result = false;
  88.     for ($i = 0; $i < count($data); $i++) {
  89.         if ($id_workshop == $data[$i]['jwu_workshop_id']) $result = true;
  90.     } 
  91.     return $result;
  92. } 
  93.  
  94. /**
  95. * AuthUser4Workshop()
  96. * 
  97. * @param integer $workshop_id 
  98. * @param object $sql_object 
  99. * @return boolean 
  100. */
  101. function AuthUser4Workshop ($workshop_id, $sql_object)
  102. {
  103.     $data = $sql_object->DBSelect(SQL_Get_UserWorkshopOwner($workshop_id));
  104.     for ($i = 0; $i < count($data); $i++) {
  105.         if (GetSessionElement('id') == $data[$i]['jwu_user_id']) return true;
  106.     } 
  107.     return false;
  108. } 
  109.  
  110. /**
  111. * IsSuperAdmin()
  112. * 
  113. * @return boolean 
  114. */
  115. function IsSuperAdmin()
  116. {
  117.     return HaveRight('theme');
  118. } 
  119.  
  120. /**
  121. * HaveRights()
  122. * 
  123. * @param string $item 
  124. * @return bool 
  125. */
  126. function HaveRight($item)
  127. {
  128.     switch ($item) {
  129.         case 'category_user':
  130.             if (GetSessionElement('right_category_user') != 'A') return false;
  131.             else return true;
  132.         case 'theme':
  133.             if (GetSessionElement('right_theme') != 'A') return false;
  134.             else return true;
  135.         case 'level':
  136.             if (GetSessionElement('right_level') != 'A') return false;
  137.             else return true;
  138.         case 'scale':
  139.             if (GetSessionElement('right_scale') != 'A') return false;
  140.             else return true;
  141.         case 'yellowpages':
  142.             if (GetSessionElement('right_yellowpages') != 'A') return false;
  143.             else return true;
  144.         case 'news':
  145.             if (GetSessionElement('right_news') != 'U') return true;
  146.             else return false;
  147.         case 'project':
  148.             if (GetSessionElement('right_project') != 'A') return false;
  149.             else return true;
  150.         case 'publication':
  151.             if (GetSessionElement('right_publication') != 'U') return true;
  152.             else return false;
  153.         case 'workshop':
  154.             if (GetSessionElement('right_workshop') != 'U') return true;
  155.             else return false;
  156.         case 'dashboard':
  157.             if (GetSessionElement('right_dashboard') != 'U') return true;
  158.             else return false;
  159.         default:
  160.             return false;
  161.     } 
  162. } 
  163.  
  164. /**
  165. * GetSessionElement()
  166. * 
  167. * @param string $item (id, whois, login, right)
  168. * @return $result 
  169. */
  170.  
  171. function GetSessionElement($item)
  172. {
  173.     switch ($item) {
  174.         case 'id':
  175.             return base64_decode($_SESSION['sess_id']);
  176.             break;
  177.         case 'whois':
  178.             return base64_decode($_SESSION['sess_whois']);
  179.             break;
  180.         case 'login':
  181.             return $_SESSION['sess_login'];
  182.             break;
  183.         case 'right_theme':
  184.             return $_SESSION['sess_right']['theme'];
  185.             break;
  186.         case 'right_dashboard':
  187.             return $_SESSION['sess_right']['dashboard'];
  188.             break;
  189.         case 'right_workshop':
  190.             return $_SESSION['sess_right']['workshop'];
  191.             break;
  192.         case 'right_project':
  193.             return $_SESSION['sess_right']['project'];
  194.             break;
  195.         case 'right_publication':
  196.             return $_SESSION['sess_right']['publication'];
  197.             break;
  198.         case 'right_news':
  199.             return $_SESSION['sess_right']['news'];
  200.             break;
  201.         case 'right_yellowpages':
  202.             return $_SESSION['sess_right']['yellowpages'];
  203.             break;
  204.         case 'right_scale':
  205.             return $_SESSION['sess_right']['scale'];
  206.             break;
  207.         case 'right_level':
  208.             return $_SESSION['sess_right']['level'];
  209.             break;
  210.         case 'right_category_user':
  211.             return $_SESSION['sess_right']['category_user'];
  212.             break;
  213.     } 
  214. } 
  215.  
  216. /**
  217. * SetUserCookie()
  218. * set les infos contenu en tableau PHP en cookie
  219. * 
  220. * @param  $table 
  221. * @return void 
  222. */
  223. function SetUserCookie($table)
  224. {
  225.     while (list($key, $value) = @each($table)) {
  226.         setcookie('linea21[' . $key . ']', $value, (time() + 3600 * 720), '/');
  227.     } 
  228. } 
  229.  
  230. /**
  231. * DestroyUserCookie()
  232. * Détruit les infos contenu en cookie
  233. * 
  234. * @return void 
  235. */
  236. function DestroyUserCookie()
  237. {
  238.     while (list($key, $val) = @each($_COOKIE['linea21'])) {
  239.         setcookie('linea21[' . $key . ']', '', (time() - 3600), '/');
  240.     } 
  241. } 
  242.  
  243. /**
  244. * UpdateCookiePassword()
  245. * Mise à jour du mot de passe dans le cookie (cas de changement)
  246. * 
  247. * @param  $newpass 
  248. * @return void 
  249. */
  250. function UpdateCookiePassword($newpass)
  251. {
  252.     $newpass = base64_encode($newpass);
  253.     setcookie('linea21[whois]', $newpass, (time() + 3600 * 720), '/');
  254. } 
  255.  
  256. /**
  257. * UpdateCookieWorkshop()
  258. * Ajout d'un Groupe de travail aux droits d'un utiliateur
  259. * 
  260. * @param  $workshop_id 
  261. * @return 
  262. */
  263. function UpdateCookieWorkshop($workshop_id)
  264. {
  265.     $string_id = base64_decode($_COOKIE['linea21']['workshop']);
  266.     $string_id .= "/" . $workshop_id;
  267.     $string_id = base64_encode($string_id);
  268.     setcookie('linea21[workshop]', $string_id, (time() + 3600 * 720), '/');
  269. } 
  270.  
  271. /**
  272. * CookieWorkshopid()
  273. * vérification de l'inscription de l'utilisateur au groupe de travail
  274. * 
  275. * @param  $workshop_id 
  276. * @return boolean 
  277. */
  278. function CookieWorkshopid($workshop_id)
  279. {
  280.     if (isset($_COOKIE['linea21']['workshop'])) {
  281.         $string_id = base64_decode($_COOKIE['linea21']['workshop']);
  282.  
  283.         $string_id = str_replace(" ", "", $string_id);
  284.         $table_id = @explode('/', $string_id);
  285.         for ($i = 0; $i < count($table_id); $i++) {
  286.             if ($workshop_id == $table_id[$i])return true;
  287.         } 
  288.         return false;
  289.     } 
  290.     return false;
  291. } 
  292.  
  293. /**
  294. * Set_Cookie_last_connexion()
  295. */
  296. {
  297.     setcookie('linea21_lastcon', time(), (time() + 3600 * 720), '/');
  298. } 
  299.  
  300. /**
  301. * LogAccess()
  302. * 
  303. * @param integer $login 
  304. * @param integer $pass 
  305. * @return void 
  306. */
  307. function LogAccess($type, $login = -1, $pass = -1)
  308. {
  309.     if ($type == 'PUBLIC') $file = LOG_PUBLIC_ACCESS;
  310.     if ($type == 'ADMIN') $file = LOG_ADMIN_ACCESS;
  311.     $sep = '##';
  312.     $fp = @fopen($file, 'a');
  313.     @fwrite($fp, i2c_realip() . $sep); // adresse
  314.     @fwrite($fp, $_SERVER['HTTP_USER_AGENT'] . $sep); //agent
  315.     @fwrite($fp, $_SERVER['HTTP_REFERER'] . $sep); // referer
  316.     @fwrite($fp, $_SERVER['REQUEST_METHOD'] . $sep); // methode requetes HTTP
  317.     @fwrite($fp, $_SERVER['REQUEST_URI'] . $sep); // script courant
  318.     @fwrite($fp, $_SERVER['QUERY_STRING'] . $sep); // requete url
  319.     @fwrite($fp, $_SERVER['HTTP_COOKIE'] . $sep); // cookie
  320.     @fwrite($fp, date('[d-m-y H:i:s]' . $sep)); // date
  321.     if ($login != -1) @fwrite($fp, $login . $sep); // login
  322.     if ($pass != -1) @fwrite($fp, $pass . $sep); // mot de passe
  323.     @fwrite($fp, END_LINE);
  324.     @fclose($fp);
  325. } 
  326. // Returns the real IP address of the user
  327. function i2c_realip()
  328. { 
  329.     // No IP found (will be overwritten by for
  330.     // if any IP is found behind a firewall)
  331.     $ip = false; 
  332.     // If HTTP_CLIENT_IP is set, then give it priority
  333.     if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
  334.         $ip = $_SERVER["HTTP_CLIENT_IP"];
  335.     } 
  336.     // User is behind a proxy and check that we discard RFC1918 IP addresses
  337.     // if they are behind a proxy then only figure out which IP belongs to the
  338.     // user.  Might not need any more hackin if there is a squid reverse proxy
  339.     // infront of apache.
  340.     if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  341.         // Put the IP's into an array which we shall work with shortly.
  342.         $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  343.         if ($ip) {
  344.             array_unshift($ips, $ip);
  345.             $ip = false;
  346.         } 
  347.  
  348.         for ($i = 0; $i < count($ips); $i++) {
  349.             // Skip RFC 1918 IP's 10.0.0.0/8, 172.16.0.0/12 and
  350.             // 192.168.0.0/16 -- jim kill me later with my regexp pattern
  351.             // below.
  352.             if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
  353.                 $ip = $ips[$i];
  354.                 break;
  355.             } 
  356.         } 
  357.     } 
  358.     // Return with the found IP or the remote address
  359.     return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  360. } 
  361.  
  362. ?>

Documentation generated on Fri, 16 Oct 2009 09:32:52 +0200 by phpDocumentor 1.4.1