[ Index ] |
|
Code source de Dotclear 2.0-beta6 |
1 <?php 2 # ***** BEGIN LICENSE BLOCK ***** 3 # This file is part of Clearbricks. 4 # Copyright (c) 2006 Olivier Meunier and contributors. All rights 5 # reserved. 6 # 7 # Clearbricks is free software; you can redistribute it and/or modify 8 # it under the terms of the GNU General Public License as published by 9 # the Free Software Foundation; either version 2 of the License, or 10 # (at your option) any later version. 11 # 12 # Clearbricks is distributed in the hope that it will be useful, 13 # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 # GNU General Public License for more details. 16 # 17 # You should have received a copy of the GNU General Public License 18 # along with Clearbricks; if not, write to the Free Software 19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 # 21 # ***** END LICENSE BLOCK ***** 22 23 class sessionDB 24 { 25 private $con; 26 private $table; 27 private $cookie_name; 28 private $cookie_path; 29 private $ttl = '-120 minutes'; 30 31 public function __construct(&$con,$table,$cookie_name,$cookie_path='/') 32 { 33 $this->con =& $con; 34 $this->table = $table; 35 $this->cookie_name = $cookie_name; 36 $this->cookie_path = $cookie_path; 37 38 if(function_exists('ini_set')) 39 { 40 @ini_set('session.use_cookies','1'); 41 @ini_set('session.use_only_cookies','1'); 42 @ini_set('url_rewriter.tags',''); 43 @ini_set('session.use_trans_sid','0'); 44 @ini_set('session.cookie_path',$this->cookie_path); 45 } 46 } 47 48 public function __destruct() 49 { 50 if (isset($_SESSION)) { 51 session_write_close(); 52 } 53 } 54 55 public function start() 56 { 57 session_set_save_handler( 58 array(&$this, '_open'), 59 array(&$this, '_close'), 60 array(&$this, '_read'), 61 array(&$this, '_write'), 62 array(&$this, '_destroy'), 63 array(&$this, '_gc') 64 ); 65 66 if (!isset($_COOKIE[$this->cookie_name])) { 67 session_id(sha1(uniqid(rand(),true))); 68 } 69 70 session_name($this->cookie_name); 71 session_start(); 72 } 73 74 public function destroy() 75 { 76 $_SESSION = array(); 77 session_unset(); 78 session_destroy(); 79 setcookie(session_name(),'',0,$this->cookie_path); 80 } 81 82 83 public function _open($path,$name) 84 { 85 return true; 86 } 87 88 public function _close() 89 { 90 $this->_gc(); 91 return true; 92 } 93 94 public function _read($ses_id) 95 { 96 $strReq = 'SELECT ses_value FROM '.$this->table.' '. 97 'WHERE ses_id = \''.$this->checkID($ses_id).'\' '; 98 99 $rs = $this->con->select($strReq); 100 101 if ($rs->isEmpty()) { 102 return ''; 103 } else { 104 return $rs->f('ses_value'); 105 } 106 } 107 108 public function _write($ses_id, $data) 109 { 110 $strReq = 'SELECT ses_id '. 111 'FROM '.$this->table.' '. 112 "WHERE ses_id = '".$this->checkID($ses_id)."' "; 113 114 $rs = $this->con->select($strReq); 115 116 $cur = $this->con->openCursor($this->table); 117 $cur->ses_time = (string) time(); 118 $cur->ses_value = (string) $data; 119 120 if (!$rs->isEmpty()) 121 { 122 $cur->update("WHERE ses_id = '".$this->checkID($ses_id)."' "); 123 } 124 else 125 { 126 $cur->ses_id = (string) $this->checkID($ses_id); 127 $cur->ses_start = (string) time(); 128 129 $cur->insert(); 130 } 131 132 return true; 133 } 134 135 public function _destroy($ses_id) 136 { 137 $strReq = 'DELETE FROM '.$this->table.' '. 138 'WHERE ses_id = \''.$this->checkID($ses_id).'\' '; 139 140 $this->con->execute($strReq); 141 142 $this->_optimize(); 143 return true; 144 } 145 146 public function _gc() 147 { 148 $ses_life = strtotime($this->ttl); 149 150 $strReq = 'DELETE FROM '.$this->table.' '. 151 'WHERE ses_time < '.$ses_life.' '; 152 153 154 $this->con->execute($strReq); 155 156 if ($this->con->changes() > 0) { 157 $this->_optimize(); 158 } 159 return true; 160 } 161 162 private function _optimize() 163 { 164 $this->con->vacuum($this->table); 165 } 166 167 private function checkID($id) 168 { 169 if (!preg_match('/^([0-9a-f]{40})$/i',$id)) { 170 return null; 171 } 172 return $id; 173 } 174 } 175 176 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Feb 23 22:16:06 2007 | par Balluche grâce à PHPXref 0.7 |