This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
forked from symfony/symfony1
/
sfPDOSessionStorage.class.php
174 lines (155 loc) · 5.25 KB
/
sfPDOSessionStorage.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
* This file is part of the symfony package.
* (c) 2004, 2005 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004, 2005 Sean Kerr <sean@code-box.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Provides support for session storage using a PDO database abstraction layer.
*
* <b>parameters:</b> see sfDatabaseSessionStorage
*
* @package symfony
* @subpackage storage
* @author Mathew Toth <developer@poetryleague.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Sean Kerr <sean@code-box.org>
* @version SVN: $Id: sfPDOSessionStorage.class.php 33545 2012-10-05 10:49:45Z fabien $
*/
class sfPDOSessionStorage extends sfDatabaseSessionStorage
{
/**
* Destroys a session.
*
* @param string $id A session ID
*
* @return bool true, if the session was destroyed, otherwise an exception is thrown
*
* @throws <b>DatabaseException</b> If the session cannot be destroyed
*/
public function sessionDestroy($id)
{
// get table/column
$db_table = $this->options['db_table'];
$db_id_col = $this->options['db_id_col'];
// delete the record associated with this id
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $id, PDO::PARAM_STR);
$stmt->execute();
}
catch (PDOException $e)
{
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
}
return true;
}
/**
* Cleans up old sessions.
*
* @param int $lifetime The lifetime of a session
*
* @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
*
* @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
*/
public function sessionGC($lifetime)
{
// get table/column
$db_table = $this->options['db_table'];
$db_time_col = $this->options['db_time_col'];
// delete the record associated with this id
$sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
try
{
$this->db->query($sql);
}
catch (PDOException $e)
{
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
}
return true;
}
/**
* Reads a session.
*
* @param string $id A session ID
*
* @return string The session data if the session was read or created, otherwise an exception is thrown
*
* @throws <b>DatabaseException</b> If the session cannot be read
*/
public function sessionRead($id)
{
// get table/columns
$db_table = $this->options['db_table'];
$db_data_col = $this->options['db_data_col'];
$db_id_col = $this->options['db_id_col'];
$db_time_col = $this->options['db_time_col'];
try
{
$sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $id, PDO::PARAM_STR, 255);
$stmt->execute();
// it is recommended to use fetchAll so that PDO can close the DB cursor
// we anyway expect either no rows, or one row with one column. fetchColumn, seems to be buggy #4777
$sessionRows = $stmt->fetchAll(PDO::FETCH_NUM);
if (count($sessionRows) == 1)
{
return is_resource($sessionRows[0][0]) ? stream_get_contents($sessionRows[0][0]) : $sessionRows[0][0];
}
else
{
// session does not exist, create it
$sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $id, PDO::PARAM_STR);
$stmt->bindValue(2, '', PDO::PARAM_STR);
$stmt->bindValue(3, time(), PDO::PARAM_INT);
$stmt->execute();
return '';
}
}
catch (PDOException $e)
{
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
}
}
/**
* Writes session data.
*
* @param string $id A session ID
* @param string $data A serialized chunk of session data
*
* @return bool true, if the session was written, otherwise an exception is thrown
*
* @throws <b>DatabaseException</b> If the session data cannot be written
*/
public function sessionWrite($id, $data)
{
// get table/column
$db_table = $this->options['db_table'];
$db_data_col = $this->options['db_data_col'];
$db_id_col = $this->options['db_id_col'];
$db_time_col = $this->options['db_time_col'];
$sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $data, PDO::PARAM_STR);
$stmt->bindParam(2, $id, PDO::PARAM_STR);
$stmt->execute();
}
catch (PDOException $e)
{
throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
}
return true;
}
}