Quick Search:

View

Revision:

Diff

Diff from 1667 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/session/database.php

Annotated File View

hpdl
1666
1 <?php
2 /*
3   $Id: $
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2007 osCommerce
9
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License v2 (1991)
12   as published by the Free Software Foundation.
13 */
14
15 /**
16  * The osC_Session_database class stores the session data in the database
17  */
18
19   class osC_Session_database extends osC_Session {
20
21 /**
22  * Constructor, loads the database based session storage handler
23  *
24  * @param string $name The name of the session
25  * @access public
26  */
27
28     public function __construct($name = null) {
hpdl
1667
29       parent::__construct($name);
hpdl
1666
30
31       session_set_save_handler(array(&$this, '_custom_open'),
32                                array(&$this, '_custom_close'),
33                                array(&$this, '_custom_read'),
34                                array(&$this, '_custom_write'),
35                                array(&$this, '_custom_destroy'),
36                                array(&$this, '_custom_gc'));
37     }
38
39 /**
40  * Opens the database based session storage handler
41  *
42  * @access protected
43  */
44
45     protected function _custom_open() {
46       return true;
47     }
48
49 /**
50  * Closes the database based session storage handler
51  *
52  * @access protected
53  */
54
55     protected function _custom_close() {
56       return true;
57     }
58
59 /**
60  * Read session data from the database based session storage handler
61  *
hpdl
1667
62  * @param string $id The ID of the session
hpdl
1666
63  * @access protected
64  */
65
hpdl
1667
66     protected function _custom_read($id) {
hpdl
1666
67       global $osC_Database;
68
hpdl
1667
69       $Qsession = $osC_Database->query('select value from :table_sessions where sesskey = :sesskey');
hpdl
1666
70       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1667
71       $Qsession->bindValue(':sesskey', $id);
hpdl
1666
72       $Qsession->execute();
73
74       if ( $Qsession->numberOfRows() === 1 ) {
75         $value = $Qsession->value('value');
76
77         $Qsession->freeResult();
78
79         return $value;
80       }
81
82       return false;
83     }
84
85 /**
86  * Writes session data to the database based session storage handler
87  *
hpdl
1667
88  * @param string $id The ID of the session
89  * @param string $value The session data to store
hpdl
1666
90  * @access protected
91  */
92
hpdl
1667
93     protected function _custom_write($id, $value) {
hpdl
1666
94       global $osC_Database;
95
hpdl
1667
96       $Qsession = $osC_Database->query('select sesskey from :table_sessions where sesskey = :sesskey');
hpdl
1666
97       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1667
98       $Qsession->bindValue(':sesskey', $id);
hpdl
1666
99       $Qsession->execute();
100
101       if ( $Qsession->numberOfRows() === 1 ) {
102         $Qsession = $osC_Database->query('update :table_sessions set expiry = :expiry, value = :value where sesskey = :sesskey');
103       } else {
104         $Qsession = $osC_Database->query('insert into :table_sessions values (:sesskey, :expiry, :value)');
105       }
106       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1667
107       $Qsession->bindValue(':sesskey', $id);
108       $Qsession->bindValue(':expiry', time());
hpdl
1666
109       $Qsession->bindValue(':value', $value);
110       $Qsession->execute();
111
112       return ( $Qsession->affectedRows() === 1 );
113     }
114
115 /**
hpdl
1667
116  * Destroys the session data from the database based session storage handler
hpdl
1666
117  *
hpdl
1667
118  * @param string $id The ID of the session
hpdl
1666
119  * @access protected
120  */
121
hpdl
1667
122     protected function _custom_destroy($id) {
123       return $this->delete($id);
hpdl
1666
124     }
125
126 /**
127  * Garbage collector for the database based session storage handler
128  *
hpdl
1667
129  * @param string $max_life_time The maxmimum time a session should exist
hpdl
1666
130  * @access protected
131  */
132
hpdl
1667
133     protected function _custom_gc($max_life_time) {
hpdl
1666
134       global $osC_Database;
135
136       $Qsession = $osC_Database->query('delete from :table_sessions where expiry < :expiry');
137       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1667
138       $Qsession->bindValue(':expiry', time() - $max_life_time);
hpdl
1666
139       $Qsession->execute();
hpdl
1667
140
141       return ( $Qsession->affectedRows() > 0 );
hpdl
1666
142     }
143
144 /**
145  * Deletes the session data from the database based session storage handler
146  *
hpdl
1667
147  * @param string $id The ID of the session
148  * @access public
hpdl
1666
149  */
150
hpdl
1667
151     public function delete($id = null) {
152       global $osC_Database;
153
154       if ( empty($id) ) {
155         $id = $this->_id;
156       }
157
158       $Qsession = $osC_Database->query('delete from :table_sessions where sesskey = :sesskey');
159       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
160       $Qsession->bindValue(':sesskey', $id);
161       $Qsession->execute();
162
163       return ( $Qsession->affectedRows() === 1 );
hpdl
1666
164     }
165   }
166 ?>