Quick Search:

View

Revision:

Diff

Diff from 1668 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
1668
70
71       if ( SERVICE_SESSION_EXPIRATION_TIME > 0 ) {
72         $Qsession->appendQuery('and expiry > :expiry');
73         $Qsession->bindInt(':expiry', time());
74       }
75
hpdl
1666
76       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1667
77       $Qsession->bindValue(':sesskey', $id);
hpdl
1666
78       $Qsession->execute();
79
80       if ( $Qsession->numberOfRows() === 1 ) {
hpdl
1668
81         return $Qsession->value('value');
hpdl
1666
82       }
83
84       return false;
85     }
86
87 /**
88  * Writes session data to the database based session storage handler
89  *
hpdl
1667
90  * @param string $id The ID of the session
91  * @param string $value The session data to store
hpdl
1666
92  * @access protected
93  */
94
hpdl
1667
95     protected function _custom_write($id, $value) {
hpdl
1666
96       global $osC_Database;
97
hpdl
1667
98       $Qsession = $osC_Database->query('select sesskey from :table_sessions where sesskey = :sesskey');
hpdl
1666
99       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1667
100       $Qsession->bindValue(':sesskey', $id);
hpdl
1666
101       $Qsession->execute();
102
103       if ( $Qsession->numberOfRows() === 1 ) {
104         $Qsession = $osC_Database->query('update :table_sessions set expiry = :expiry, value = :value where sesskey = :sesskey');
105       } else {
106         $Qsession = $osC_Database->query('insert into :table_sessions values (:sesskey, :expiry, :value)');
107       }
108       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1668
109       $Qsession->bindInt(':expiry', time() + (SERVICE_SESSION_EXPIRATION_TIME * 60));
110       $Qsession->bindValue(':value', $value);
hpdl
1667
111       $Qsession->bindValue(':sesskey', $id);
hpdl
1666
112       $Qsession->execute();
113
114       return ( $Qsession->affectedRows() === 1 );
115     }
116
117 /**
hpdl
1667
118  * Destroys the session data from the database based session storage handler
hpdl
1666
119  *
hpdl
1667
120  * @param string $id The ID of the session
hpdl
1666
121  * @access protected
122  */
123
hpdl
1667
124     protected function _custom_destroy($id) {
125       return $this->delete($id);
hpdl
1666
126     }
127
128 /**
129  * Garbage collector for the database based session storage handler
130  *
hpdl
1667
131  * @param string $max_life_time The maxmimum time a session should exist
hpdl
1666
132  * @access protected
133  */
134
hpdl
1667
135     protected function _custom_gc($max_life_time) {
hpdl
1666
136       global $osC_Database;
137
hpdl
1668
138 // $max_life_time is already added to the time in the _custom_write method
139
hpdl
1666
140       $Qsession = $osC_Database->query('delete from :table_sessions where expiry < :expiry');
141       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
hpdl
1668
142       $Qsession->bindValue(':expiry', time());
hpdl
1666
143       $Qsession->execute();
hpdl
1667
144
145       return ( $Qsession->affectedRows() > 0 );
hpdl
1666
146     }
147
148 /**
149  * Deletes the session data from the database based session storage handler
150  *
hpdl
1667
151  * @param string $id The ID of the session
152  * @access public
hpdl
1666
153  */
154
hpdl
1667
155     public function delete($id = null) {
156       global $osC_Database;
157
158       if ( empty($id) ) {
159         $id = $this->_id;
160       }
161
162       $Qsession = $osC_Database->query('delete from :table_sessions where sesskey = :sesskey');
163       $Qsession->bindTable(':table_sessions', TABLE_SESSIONS);
164       $Qsession->bindValue(':sesskey', $id);
165       $Qsession->execute();
166
167       return ( $Qsession->affectedRows() === 1 );
hpdl
1666
168     }
169   }
170 ?>