]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/DbSession.php
added DB_Session support if PearDB is used.
[SourceForge/phpwiki.git] / lib / DbSession.php
1 <?php
2 ini_set('session.save_handler','user'); 
3
4 //----------------------------------------------------+
5 // This class is based on PEAR (http://pear.php.net) -+
6 //----------------------------------------------------+
7
8 // {{{ includes
9
10 //include 'DB.php';
11 include_once 'pear/DB.php';
12 require_once('lib/WikiDB/backend/PearDB.php');
13
14 // }}} includes
15
16 // {{{ classes
17
18 /**
19  * Store sessions data in Pear DB.
20  * Minor rewrite by Reini Urban <rurban@x-ray.at> for Phpwiki.
21  *
22  * @package Session
23  * @class   DB_Session
24  * @author  Stanislav Shramko <stanis@movingmail.com>
25  * @access  public
26  * @version $Revision: 1.1 $
27  *
28  */
29
30 class DB_Session 
31 extends WikiDB_backend_PearDB
32 {
33
34     /**
35      * Constructor.
36      *
37      * @param  string $table a name of the table
38      * @access public
39      */
40     function DB_Session($table = 'session_table') {
41         global $db_session_dbh, $db_session_dsn, $db_session_persistent, $session_table;
42         // $this->PEAR();
43         $session_table = $table;
44         if (!$this->_dbh) { // Fixme. This should not happen.
45             $db_session_dbh = DB::connect($GLOBALS['DBParams']['dsn'], 
46                                           array('persistent' => true, 'debug' => 2));
47             if (DB::isError($db_session_dbh)) {
48                 die("DB_Session allocated the following problem: " . 
49                     $db_session_dbh->getMessage());
50             }
51         }
52         else {
53             $db_session_dbh =& $this->_dbh;
54         }
55         // We always use persistent connections for now.
56         $db_session_persistent = true; //$dbh->get('persistent');
57         session_set_save_handler ("_db_session_open", 
58                                   "_db_session_close", "_db_session_read", 
59                                   "_db_session_write", "_db_session_destroy", 
60                                   "_db_session_gc");
61         session_start();
62     }
63
64     /**
65      * Destructor.
66      *
67      * Used to remove a persistent connection if it's made.
68      *
69      * @access private
70      */
71     function _DB_Session() {
72         global $db_session_persistent, $db_session_dbh;
73         if ($db_session_persistent == true && isset($db_session_dbh) &&
74             get_class($db_session_dbh) == 'db') {
75             $db_session_dbh->disconnect();
76         }
77     }
78 }
79
80 // }}} classes
81
82 // {{{ functions
83
84 /**
85  * Opens a session.
86  *
87  * Actually this function is a fake for session_set_save_handle.
88  * @param  string $save_path a path to stored files
89  * @param  string $session_name a name of the concrete file
90  * @return boolean true just a variable to notify PHP that everything 
91  * is good.
92  * @access private
93  */
94
95 function _db_session_open ($save_path, $session_name) {
96     return(true);
97 }
98
99 /**
100  * Closes a session.
101  *
102  * This function is called just after 
103  * <i>_db_session_write</i> call.
104  *
105  * @return boolean true just a variable to notify PHP that everything 
106  * is good.
107  * @access private
108  */
109 function _db_session_close() {
110     return(true);
111 }
112
113 /*
114  * Reads the session data from DB.
115  *
116  * @param  string $id an id of current session
117  * @return string
118  * @access private
119  */
120 function _db_session_read ($id) {
121     global $db_session_dbh, $db_session_dsn, $session_table, $db_session_persistent;
122
123     if ($db_session_persistent != true) {
124         $db_session_dbh = DB::connect($db_session_dsn);
125     }
126     if (!DB::isError($db_session_dbh)) {
127         $res = $db_session_dbh->getOne("SELECT sess_data 
128               FROM $session_table WHERE sess_id = " . $db_session_dbh->quote($id));
129         if (DB::isError($res)) {
130             $res = "";
131         }
132     } else {
133         $res = "";
134     }
135     if ($db_session_persistent != true) {
136         $db_session_dbh->disconnect();
137     }
138     return $res;
139 }
140
141 /**
142  * Saves the session data into DB.
143  *
144  * Just  a  comment:       The  "write"  handler  is  not 
145  * executed until after the output stream is closed. Thus,
146  * output from debugging statements in the "write" handler
147  * will  never be seen in the browser. If debugging output
148  * is  necessary, it is suggested that the debug output be
149  * written to a file instead.
150  *
151  * @param  string $id
152  * @param  string $sess_data
153  * @return boolean true if data saved successfully  and false
154  * otherwise.
155  * @access private
156  */
157 function _db_session_write ($id, $sess_data) {
158     global $db_session_dbh, $db_session_dsn, $session_table, $db_session_persistent;
159     if ($db_session_persistent != true) {
160         $db_session_dbh = DB::connect($db_session_dsn);
161         // var_dump($db_session_dbh);
162     }
163     if (!DB::isError($db_session_dbh)) {
164         $count = $db_session_dbh->getOne("SELECT COUNT(*) FROM $session_table
165               WHERE sess_id = " . $db_session_dbh->quote($id));
166         if ($count == 0) {
167             $sql = "INSERT INTO $session_table " . 
168                 "(sess_id, sess_data, sess_date) VALUES " .
169                 "(" . $db_session_dbh->quote($id) . ", " . 
170                 $db_session_dbh->quote($sess_data) . ", " .
171                 $db_session_dbh->quote(time()) . ")";
172             $res = $db_session_dbh->query($sql);
173             if (DB::isError($res)) {
174                 echo $res->getMessage();
175             }
176         } else {
177             $sql = "UPDATE $session_table SET " .
178                 "sess_data = " . $db_session_dbh->quote($sess_data) . ", " .
179                 "sess_date = " . $db_session_dbh->quote(time()) . 
180                 " WHERE sess_id = " . $db_session_dbh->quote($id);
181             $res = $db_session_dbh->query($sql);
182         }
183         if (DB::isError($res)) {
184             $res = false;
185         } 
186     } else {
187         echo $db_session_dbh->getMessage();
188         $res = false;
189     }
190     if ($db_session_persistent != true) {
191         $db_session_dbh->disconnect();
192     }
193     return $res;
194 }
195
196 /**
197  * Destroys a session.
198  *
199  * Removes a session from the table.
200  *
201  * @param  string $id
202  * @return boolean true 
203  * @access private
204  */
205 function _db_session_destroy ($id) {
206     global $db_session_dbh, $db_session_dsn, $session_table, $db_session_persistent;
207     if ($db_session_persistent != true) {
208         $db_session_dbh = DB::connect($db_session_dsn);
209         var_dump($db_session_dbh);
210     }
211     if (!DB::isError($db_session_dbh)) {
212         $db_session_dbh->query("DELETE FROM $session_table ".
213                         "WHERE sess_id = " . $db_session_dbh->quote($id));
214     }
215     if ($db_session_persistent != true) {
216         $db_session_dbh->disconnect();
217     }
218     return true;     
219 }
220
221 /**
222  * Cleans out all expired sessions.
223  *
224  * @param  int $maxlifetime session's time to live.
225  * @return boolean true
226  * @access private
227  */
228 function _db_session_gc ($maxlifetime) {
229     global $db_session_dbh, $db_session_dsn, $session_table, $db_session_persistent;
230     if ($db_session_persistent != true) {
231         $db_session_dbh = DB::connect($db_session_dsn);
232     }
233     $db_session_dbh->query("DELETE FROM $session_table WHERE sess_date < " . 
234                            time() - $maxlifetime);
235     //$db_session_dbh->query("FLUSH TABLES");
236     if ($db_session_persistent != true) {
237         $db_session_dbh->disconnect();
238     }
239     return true;
240 }
241
242 // }}} functions
243
244 ?>