PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/class/class_session.php

http://malleo-cms.googlecode.com/
PHP | 343 lines | 238 code | 20 blank | 85 comment | 50 complexity | 09c23c790e80e9346d0edd861b3515a6 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. |------------------------------------------------------------------------------------------------------------
  4. | Software: Malleo ( CMS )
  5. | Contact: SP - http://www.malleo-cms.com
  6. | Support: http://www.malleo-cms.com?module=forum
  7. | Documentation : Support: http://www.malleo-cms.com?module=wiki
  8. |------------------------------------------------------------------------------------------------------------
  9. | Author: Stephane RAJALU
  10. | Copyright (c) 2008-2009, Stephane RAJALU All Rights Reserved
  11. |------------------------------------------------------------------------------------------------------------
  12. | License: Distributed under the CECILL V2 License
  13. | This program is distributed in the hope that it will be useful - WITHOUT
  14. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. | FITNESS FOR A PARTICULAR PURPOSE.
  16. |
  17. | Please read Licence_CeCILL_V2-en.txt
  18. | SVP lisez Licence_CeCILL_V2-fr.txt
  19. |------------------------------------------------------------------------------------------------------------
  20. */
  21. if ( !defined('PROTECT') )
  22. {
  23. die("Tentative de Hacking");
  24. }
  25. class session
  26. {
  27. var $user;
  28. var $id_session;
  29. var $time;
  30. var $info_user;
  31. var $config;
  32. var $liste_ip;
  33. var $ip;
  34. var $navlink = '';
  35. //
  36. // initialisation des infos de session
  37. function session($config)
  38. {
  39. global $root;
  40. $this->time = time();
  41. $this->path_cache = $root.'cache/sessions/';
  42. $this->cf = $config;
  43. $this->get_ip();
  44. }
  45. //
  46. // création d'une session pour un visiteur
  47. function new_session(){
  48. global $c,$cache;
  49. // ------------------------------------------------------
  50. // On verifie que cette IP n'est pas bannie
  51. if (!$this->ip_bannie()) return false;
  52. // ------------------------------------------------------
  53. // Liste des sessions recentes
  54. $this->liste_ip = $this->listing_ip();
  55. // ------------------------------------------------------
  56. // Recherche ID de session
  57. if(isset($_COOKIE[$this->cf['cookie_name']]) && $_COOKIE[$this->cf['cookie_name']]!= null){
  58. // On recupere le cookie si il existe
  59. $this->id_session = preg_replace("/[^a-z0-9]/i",'',$_COOKIE[$this->cf['cookie_name']]);
  60. }else{
  61. // Reconnaissance par l'ip
  62. if (array_key_exists($this->ip,$this->liste_ip) && $this->liste_ip[$this->ip]['user_id'] < 2 ){
  63. $this->id_session = $this->liste_ip[$this->ip]['id_session'];
  64. }else{
  65. // On charge la session PHP
  66. session_start();
  67. $this->id_session = session_id();
  68. }
  69. }
  70. // ------------------------------------------------------
  71. // Recherche de corélation id de session<->utilisateur
  72. $session = false;
  73. foreach($this->liste_ip AS $ip => $sess){
  74. // Session recente donc trouvee en cache
  75. if($sess['id_session'] == $this->id_session){
  76. $session = $this->id_session;
  77. break;
  78. }
  79. }
  80. if ($session == false){
  81. // Recherche directe en base
  82. // La session existe en base ?
  83. $sql = 'SELECT id_session,user_id,date_lastvisite,user_ip
  84. FROM '.TABLE_SESSIONS.' WHERE id_session=\''.$this->id_session.'\'
  85. ORDER BY date_lastvisite DESC LIMIT 1';
  86. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,5,__FILE__,__LINE__,$sql);
  87. if ($c->sql_numrows($resultat)==0){
  88. // La session n'existe pas en base alors on la crée
  89. $sql = 'INSERT INTO '.TABLE_SESSIONS.' (id_session,user_id,date_lastvisite,user_ip)
  90. VALUES (\''.$this->id_session.'\',1,\''.$this->time.'\',\''.$this->get_ip().'\')';
  91. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,5,__FILE__,__LINE__,$sql);
  92. }
  93. $this->listing_ip(true);
  94. }
  95. // ------------------------------------------------------
  96. // Informations sur l'utilisateur
  97. $cache->files_cache['infos_user'] = array($this->path_cache.$this->id_session, 'global $session; return $session->infos_user();',$this->cf['cache_session_user']);
  98. $this->info_user = $cache->appel_cache('infos_user');
  99. return $this->info_user ;
  100. }
  101. //
  102. // Connecte le user et met a jour les cache
  103. function login($user_id,$cookie=false){
  104. global $cache,$droits,$c;
  105. // On détruit la session
  106. $this->destroy_session();
  107. // On crée un nouvel id de session
  108. if (!session_id()) session_start();
  109. @session_regenerate_id();
  110. $this->id_session = session_id();
  111. // On enregistre l'utilisateur
  112. $droits->charge_droits_user($user_id,true); // On force le chargement de ces droits ? chaque login
  113. $sql = 'INSERT INTO '.TABLE_SESSIONS.' (id_session,user_id,date_lastvisite,user_ip) VALUES
  114. (\''.$this->id_session.'\','.$user_id.','.$this->time.',\''.$this->ip.'\')';
  115. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,5,__FILE__,__LINE__,$sql);
  116. // on place le cookie
  117. $expire = ($cookie==true)? ($this->time+$this->cf['cookie_time']):($this->time+3600);
  118. setcookie($this->cf['cookie_name'], $this->id_session, $expire, $this->cf['path']);
  119. // Update cache
  120. $this->listing_ip(true);
  121. }
  122. //
  123. // Deconnecte un utilisateur
  124. function logout(){
  125. global $c;
  126. // Session zone admin
  127. if (isset($_SESSION['digicode_TTL']))$_SESSION['digicode_TTL'] = 0;
  128. // Session publique
  129. $this->destroy_session(false);
  130. // Création d'une session invite
  131. if (!session_id()) session_start();
  132. session_regenerate_id();
  133. $this->id_session = session_id();
  134. $sql = 'INSERT INTO '.TABLE_SESSIONS.' (id_session,user_id,date_lastvisite,user_ip)
  135. VALUES (\''.$this->id_session.'\',1,\''.$this->time.'\',\''.$this->get_ip().'\')';
  136. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,5,__FILE__,__LINE__,$sql);
  137. $this->listing_ip(true);
  138. }
  139. //
  140. // Destruction d'une session
  141. function destroy_session($delete=true)
  142. {
  143. global $c;
  144. // en base
  145. if ($delete==true){
  146. $sql = 'DELETE FROM '.TABLE_SESSIONS.'
  147. WHERE date_lastvisite < '.($this->time - $this->cf['cookie_time']).
  148. ' OR id_session=\''.$this->id_session.'\'';
  149. if (!$c->sql_query($sql)) message_die(E_ERROR,4,__FILE__,__LINE__,$sql);
  150. }
  151. // en cache
  152. if (file_exists($this->path_cache.$this->id_session)){
  153. unlink($this->path_cache.$this->id_session);
  154. }
  155. // chez le visiteur: le cookie
  156. setcookie($this->cf['cookie_name'], null, ($this->time-3600)); // cookie malleo
  157. setcookie(get_cfg_var('session.name'), null, ($this->time-3600)); // eventuelle session type PHPSESSID
  158. $this->id_session = null;
  159. // Purge du dossier
  160. $this->purge_dossier_sessions();
  161. }
  162. //
  163. // Purge du dossier de sessions
  164. function purge_dossier_sessions(){
  165. if (!is_dir($this->path_cache)) return;
  166. $ch = @opendir($this->path_cache);
  167. while ($file = @readdir($ch))
  168. {
  169. if (!is_dir($this->path_cache.$file)
  170. && $file!='.htaccess'
  171. && (filemtime($this->path_cache.$file)<($this->time - 3600))
  172. && is_writable($this->path_cache.$file)){
  173. @unlink($this->path_cache.$file);
  174. }
  175. }
  176. @closedir($ch);
  177. }
  178. //
  179. // Recuperation des infos sur le user
  180. function infos_user()
  181. {
  182. global $c,$droits;
  183. // Recuperation des infos sur le user.
  184. $sql = 'SELECT s.*, u.*
  185. FROM '.TABLE_SESSIONS.' AS s
  186. LEFT JOIN '.TABLE_USERS.' AS u
  187. ON (s.user_id=u.user_id)
  188. WHERE s.id_session=\''.$this->id_session.'\'
  189. ORDER BY date_lastvisite DESC LIMIT 1';
  190. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,6,__FILE__,__LINE__,$sql);
  191. if ($c->sql_numrows($resultat) > 0)
  192. {
  193. $row = $c->sql_fetchrow($resultat);
  194. foreach ($row as $key => $value)
  195. {
  196. $infos[$key]= $value;
  197. }
  198. // On a ? faire ? un Robot, on actualise les données en fonction
  199. // On renseigne le Robot_Name dans la session
  200. if ($infos['user_id']==1){
  201. global $bots;
  202. if($robot=$bots->rechercher_robots()){
  203. $sql = 'UPDATE '.TABLE_SESSIONS.' SET
  204. date_lastvisite='.$this->time.',
  205. pseudo=\''.$robot['robot_name'].'\'
  206. WHERE id_session=\''.$this->id_session.'\'';
  207. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,6,__FILE__,__LINE__,$sql);
  208. $infos['date_lastvisite'] = $this->time;
  209. $infos['pseudo']= $robot['robot_name'];
  210. $infos['localisation']= $robot['robot_url'];
  211. }
  212. $infos['langue'] = $this->cf['default_langue'];
  213. }
  214. // toutes les 5 minutes on rafraichit la date de derniere visite
  215. if ($infos['date_lastvisite'] < ($this->time-300)){
  216. // Maj de la date de dernier passage
  217. $sql = 'UPDATE '.TABLE_SESSIONS.' SET date_lastvisite='.$this->time.'
  218. WHERE id_session=\''.$this->id_session.'\'';
  219. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,6,__FILE__,__LINE__,$sql);
  220. $infos['date_lastvisite'] = $this->time;
  221. }
  222. $infos['user_connecte']= ($infos['user_id']>1)?1:0;
  223. $infos['rules']= $droits->charge_droits_user($infos);
  224. $infos['groupes']= $droits->user_groupes;
  225. return $this->info_user = $infos;
  226. }
  227. return false;
  228. }
  229. //
  230. // fournit la liste des ip
  231. function listing_ip($force=false){
  232. global $cache;
  233. return $this->liste_ip = $cache->appel_cache('listing_ip',$force);
  234. }
  235. //
  236. // on recupere les IP uniques, id_session des membres connectes dans l'heure.
  237. // au dela ils devront accepter les cookies.
  238. function liste_ip()
  239. {
  240. global $c;
  241. $sql = 'SELECT DISTINCT user_ip, id_session, user_id
  242. FROM '.TABLE_SESSIONS.'
  243. WHERE date_lastvisite>'.($this->time-3600).'
  244. ORDER BY date_lastvisite ASC';
  245. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,34,__FILE__,__LINE__,$sql);
  246. $liste_ip = array();
  247. while ($row = $c->sql_fetchrow($resultat))
  248. {
  249. $liste_ip[$row['user_ip']]['id_session'] = $row['id_session'];
  250. $liste_ip[$row['user_ip']]['user_id'] = $row['user_id'];
  251. }
  252. return $liste_ip;
  253. }
  254. //
  255. // IP du visiteur
  256. function get_ip()
  257. {
  258. if (!isset($this->ip)){
  259. if (isset($_SERVER['HTTP_CLIENT_IP'])){
  260. $this->ip = $_SERVER['HTTP_CLIENT_IP'];
  261. }elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
  262. $this->ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  263. }else{
  264. $this->ip = $_SERVER['REMOTE_ADDR'];
  265. }
  266. }
  267. return $this->ip;
  268. }
  269. //
  270. // Test IP bannie
  271. function ip_bannie(){
  272. global $droits,$lang;
  273. $droits->Charge_bannis();
  274. if (!is_array($droits->liste_bannis)) return true;
  275. if (array_key_exists(2,$droits->liste_bannis)){
  276. foreach ($droits->liste_bannis[2] as $pattern=>$val){
  277. if (preg_match('/'.$pattern.'/',$this->ip)){
  278. load_lang('bannis');
  279. $fin_ban = ($val['fin_ban']==0)? $lang['L_DEFINITIF']:date('d/m/Y h\h s\m\i\n',$val['fin_ban']);
  280. die(sprintf($lang['L_ALERTE_BAN_IP'],$fin_ban,$val['raison_ban']));
  281. }
  282. }
  283. }
  284. return true;
  285. }
  286. //
  287. // Creation des navlinks visuels
  288. // les navlinks sont portes dans la session pour un futur projet : le tracage des utilisateurs sur le site
  289. function make_navlinks($texte,$lien=false)
  290. {
  291. if (is_array($texte) && $lien==false){
  292. foreach ($texte as $k=>$v) $this->navlink[] = array($k,$v);
  293. }else{
  294. $this->navlink[] = array($texte,$lien);
  295. }
  296. return true;
  297. }
  298. //
  299. // trace l'activite des membres sur le site
  300. function tracer_page(){
  301. global $c,$tpl;
  302. // le Traceur est-il activé ?
  303. if($this->cf['activer_traceur'] == 1){
  304. $libelle_page = $tpl->titre_page;
  305. $elmts = explode(' :: ',$libelle_page);
  306. array_reverse($elmts);
  307. $libelle_page = implode('> ',$elmts);
  308. $sql = 'INSERT INTO '.TABLE_SESSIONS_SUIVIES.' (user_id,pseudo,url_page,libelle_page,date) VALUES
  309. ('.$this->info_user['user_id'].',\''.$this->info_user['pseudo'].'\',\''.addslashes($_SERVER['REQUEST_URI']).'\',
  310. \''.addslashes($libelle_page).'\','.$this->time.')';
  311. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,34,__FILE__,__LINE__,$sql);
  312. // On supprime les logs superieurs ? 3 mois
  313. // Pour ne pas trop surcharger Malleo on le fait uniquement :
  314. // si le visiteur n'est pas membre du site et 1 fois sur 2 en moyenne
  315. if ($this->info_user['user_id'] == 1 && $this->time%2){
  316. $sql = 'DELETE FROM '.TABLE_SESSIONS_SUIVIES.' WHERE date < '.($this->time-7862400);
  317. if (!$resultat = $c->sql_query($sql))message_die(E_ERROR,34,__FILE__,__LINE__,$sql);
  318. }
  319. }
  320. return true;
  321. }
  322. }
  323. ?>