2 ini_set('session.save_handler','user');
4 //----------------------------------------------------+
5 // This class is based on PEAR (http://pear.php.net) -+
6 //----------------------------------------------------+
11 include_once 'pear/DB.php';
12 require_once('lib/WikiDB/backend/PearDB.php');
19 * Store sessions data in Pear DB.
20 * Minor rewrite by Reini Urban <rurban@x-ray.at> for Phpwiki.
24 * @author Stanislav Shramko <stanis@movingmail.com>
26 * @version $Revision: 1.1 $
31 extends WikiDB_backend_PearDB
37 * @param string $table a name of the table
40 function DB_Session($table = 'session_table') {
41 global $db_session_dbh, $db_session_dsn, $db_session_persistent, $session_table;
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());
53 $db_session_dbh =& $this->_dbh;
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",
67 * Used to remove a persistent connection if it's made.
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();
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
95 function _db_session_open ($save_path, $session_name) {
102 * This function is called just after
103 * <i>_db_session_write</i> call.
105 * @return boolean true just a variable to notify PHP that everything
109 function _db_session_close() {
114 * Reads the session data from DB.
116 * @param string $id an id of current session
120 function _db_session_read ($id) {
121 global $db_session_dbh, $db_session_dsn, $session_table, $db_session_persistent;
123 if ($db_session_persistent != true) {
124 $db_session_dbh = DB::connect($db_session_dsn);
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)) {
135 if ($db_session_persistent != true) {
136 $db_session_dbh->disconnect();
142 * Saves the session data into DB.
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.
152 * @param string $sess_data
153 * @return boolean true if data saved successfully and false
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);
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));
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();
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);
183 if (DB::isError($res)) {
187 echo $db_session_dbh->getMessage();
190 if ($db_session_persistent != true) {
191 $db_session_dbh->disconnect();
197 * Destroys a session.
199 * Removes a session from the table.
202 * @return boolean true
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);
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));
215 if ($db_session_persistent != true) {
216 $db_session_dbh->disconnect();
222 * Cleans out all expired sessions.
224 * @param int $maxlifetime session's time to live.
225 * @return boolean true
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);
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();