|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Service_WindowsAzure |
|
17 * @subpackage Session |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: SessionHandler.php 20785 2010-01-31 09:43:03Z mikaelkael $ |
|
21 */ |
|
22 |
|
23 /** Zend_Service_WindowsAzure_Storage_Table */ |
|
24 require_once 'Zend/Service/WindowsAzure/Storage/Table.php'; |
|
25 |
|
26 /** |
|
27 * @see Zend_Service_WindowsAzure_Exception |
|
28 */ |
|
29 require_once 'Zend/Service/WindowsAzure/Exception.php'; |
|
30 |
|
31 /** |
|
32 * @category Zend |
|
33 * @package Zend_Service_WindowsAzure |
|
34 * @subpackage Session |
|
35 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
36 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
37 */ |
|
38 class Zend_Service_WindowsAzure_SessionHandler |
|
39 { |
|
40 /** |
|
41 * Table storage |
|
42 * |
|
43 * @var Zend_Service_WindowsAzure_Storage_Table |
|
44 */ |
|
45 protected $_tableStorage; |
|
46 |
|
47 /** |
|
48 * Session table name |
|
49 * |
|
50 * @var string |
|
51 */ |
|
52 protected $_sessionTable; |
|
53 |
|
54 /** |
|
55 * Session table partition |
|
56 * |
|
57 * @var string |
|
58 */ |
|
59 protected $_sessionTablePartition; |
|
60 |
|
61 /** |
|
62 * Creates a new Zend_Service_WindowsAzure_SessionHandler instance |
|
63 * |
|
64 * @param Zend_Service_WindowsAzure_Storage_Table $tableStorage Table storage |
|
65 * @param string $sessionTable Session table name |
|
66 * @param string $sessionTablePartition Session table partition |
|
67 */ |
|
68 public function __construct(Zend_Service_WindowsAzure_Storage_Table $tableStorage, $sessionTable = 'phpsessions', $sessionTablePartition = 'sessions') |
|
69 { |
|
70 // Set properties |
|
71 $this->_tableStorage = $tableStorage; |
|
72 $this->_sessionTable = $sessionTable; |
|
73 $this->_sessionTablePartition = $sessionTablePartition; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Registers the current session handler as PHP's session handler |
|
78 * |
|
79 * @return boolean |
|
80 */ |
|
81 public function register() |
|
82 { |
|
83 return session_set_save_handler(array($this, 'open'), |
|
84 array($this, 'close'), |
|
85 array($this, 'read'), |
|
86 array($this, 'write'), |
|
87 array($this, 'destroy'), |
|
88 array($this, 'gc') |
|
89 ); |
|
90 } |
|
91 |
|
92 /** |
|
93 * Open the session store |
|
94 * |
|
95 * @return bool |
|
96 */ |
|
97 public function open() |
|
98 { |
|
99 // Make sure table exists |
|
100 $tableExists = $this->_tableStorage->tableExists($this->_sessionTable); |
|
101 if (!$tableExists) { |
|
102 $this->_tableStorage->createTable($this->_sessionTable); |
|
103 } |
|
104 |
|
105 // Ok! |
|
106 return true; |
|
107 } |
|
108 |
|
109 /** |
|
110 * Close the session store |
|
111 * |
|
112 * @return bool |
|
113 */ |
|
114 public function close() |
|
115 { |
|
116 return true; |
|
117 } |
|
118 |
|
119 /** |
|
120 * Read a specific session |
|
121 * |
|
122 * @param int $id Session Id |
|
123 * @return string |
|
124 */ |
|
125 public function read($id) |
|
126 { |
|
127 try |
|
128 { |
|
129 $sessionRecord = $this->_tableStorage->retrieveEntityById( |
|
130 $this->_sessionTable, |
|
131 $this->_sessionTablePartition, |
|
132 $id |
|
133 ); |
|
134 return base64_decode($sessionRecord->serializedData); |
|
135 } |
|
136 catch (Zend_Service_WindowsAzure_Exception $ex) |
|
137 { |
|
138 return ''; |
|
139 } |
|
140 } |
|
141 |
|
142 /** |
|
143 * Write a specific session |
|
144 * |
|
145 * @param int $id Session Id |
|
146 * @param string $serializedData Serialized PHP object |
|
147 */ |
|
148 public function write($id, $serializedData) |
|
149 { |
|
150 $sessionRecord = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity($this->_sessionTablePartition, $id); |
|
151 $sessionRecord->sessionExpires = time(); |
|
152 $sessionRecord->serializedData = base64_encode($serializedData); |
|
153 |
|
154 $sessionRecord->setAzurePropertyType('sessionExpires', 'Edm.Int32'); |
|
155 |
|
156 try |
|
157 { |
|
158 $this->_tableStorage->updateEntity($this->_sessionTable, $sessionRecord); |
|
159 } |
|
160 catch (Zend_Service_WindowsAzure_Exception $unknownRecord) |
|
161 { |
|
162 $this->_tableStorage->insertEntity($this->_sessionTable, $sessionRecord); |
|
163 } |
|
164 } |
|
165 |
|
166 /** |
|
167 * Destroy a specific session |
|
168 * |
|
169 * @param int $id Session Id |
|
170 * @return boolean |
|
171 */ |
|
172 public function destroy($id) |
|
173 { |
|
174 try |
|
175 { |
|
176 $sessionRecord = $this->_tableStorage->retrieveEntityById( |
|
177 $this->_sessionTable, |
|
178 $this->_sessionTablePartition, |
|
179 $id |
|
180 ); |
|
181 $this->_tableStorage->deleteEntity($this->_sessionTable, $sessionRecord); |
|
182 |
|
183 return true; |
|
184 } |
|
185 catch (Zend_Service_WindowsAzure_Exception $ex) |
|
186 { |
|
187 return false; |
|
188 } |
|
189 } |
|
190 |
|
191 /** |
|
192 * Garbage collector |
|
193 * |
|
194 * @param int $lifeTime Session maximal lifetime |
|
195 * @see session.gc_divisor 100 |
|
196 * @see session.gc_maxlifetime 1440 |
|
197 * @see session.gc_probability 1 |
|
198 * @usage Execution rate 1/100 (session.gc_probability/session.gc_divisor) |
|
199 * @return boolean |
|
200 */ |
|
201 public function gc($lifeTime) |
|
202 { |
|
203 try |
|
204 { |
|
205 $result = $this->_tableStorage->retrieveEntities($this->_sessionTable, 'PartitionKey eq \'' . $this->_sessionTablePartition . '\' and sessionExpires lt ' . (time() - $lifeTime)); |
|
206 foreach ($result as $sessionRecord) |
|
207 { |
|
208 $this->_tableStorage->deleteEntity($this->_sessionTable, $sessionRecord); |
|
209 } |
|
210 return true; |
|
211 } |
|
212 catch (Zend_Service_WindowsAzure_exception $ex) |
|
213 { |
|
214 return false; |
|
215 } |
|
216 } |
|
217 } |