Issues (836)

framework/web/DbSession.php (1 issue)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\db\Connection;
13
use yii\db\PdoValue;
14
use yii\db\Query;
15
use yii\di\Instance;
16
17
/**
18
 * DbSession extends [[Session]] by using database as session data storage.
19
 *
20
 * By default, DbSession stores session data in a DB table named 'session'. This table
21
 * must be pre-created. The table name can be changed by setting [[sessionTable]].
22
 *
23
 * The following example shows how you can configure the application to use DbSession:
24
 * Add the following to your application config under `components`:
25
 *
26
 * ```php
27
 * 'session' => [
28
 *     'class' => 'yii\web\DbSession',
29
 *     // 'db' => 'mydb',
30
 *     // 'sessionTable' => 'my_session',
31
 * ]
32
 * ```
33
 *
34
 * DbSession extends [[MultiFieldSession]], thus it allows saving extra fields into the [[sessionTable]].
35
 * Refer to [[MultiFieldSession]] for more details.
36
 *
37
 * @author Qiang Xue <[email protected]>
38
 * @since 2.0
39
 */
40
class DbSession extends MultiFieldSession
41
{
42
    /**
43
     * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
44
     * After the DbSession object is created, if you want to change this property, you should only assign it
45
     * with a DB connection object.
46
     * Starting from version 2.0.2, this can also be a configuration array for creating the object.
47
     */
48
    public $db = 'db';
49
    /**
50
     * @var string the name of the DB table that stores the session data.
51
     * The table should be pre-created as follows:
52
     *
53
     * ```sql
54
     * CREATE TABLE session
55
     * (
56
     *     id CHAR(40) NOT NULL PRIMARY KEY,
57
     *     expire INTEGER,
58
     *     data BLOB
59
     * )
60
     * ```
61
     *
62
     * where 'BLOB' refers to the BLOB-type of your preferred DBMS. Below are the BLOB type
63
     * that can be used for some popular DBMS:
64
     *
65
     * - MySQL: LONGBLOB
66
     * - PostgreSQL: BYTEA
67
     * - MSSQL: BLOB
68
     *
69
     * When using DbSession in a production server, we recommend you create a DB index for the 'expire'
70
     * column in the session table to improve the performance.
71
     *
72
     * Note that according to the php.ini setting of `session.hash_function`, you may need to adjust
73
     * the length of the `id` column. For example, if `session.hash_function=sha256`, you should use
74
     * length 64 instead of 40.
75
     */
76
    public $sessionTable = '{{%session}}';
77
78
    /**
79
     * @var array Session fields to be written into session table columns
80
     * @since 2.0.17
81
     */
82
    protected $fields = [];
83
84
85
    /**
86
     * Initializes the DbSession component.
87
     * This method will initialize the [[db]] property to make sure it refers to a valid DB connection.
88
     * @throws InvalidConfigException if [[db]] is invalid.
89
     */
90
    public function init()
91
    {
92
        parent::init();
93
        $this->db = Instance::ensure($this->db, Connection::class);
94
    }
95
96
    /**
97
     * Session open handler.
98
     * @internal Do not call this method directly.
99
     * @param string $savePath session save path
100
     * @param string $sessionName session name
101
     * @return bool whether session is opened successfully
102
     */
103
    public function openSession($savePath, $sessionName)
104
    {
105
        if ($this->getUseStrictMode()) {
106
            $id = $this->getId();
107
            if (!$this->getReadQuery($id)->exists($this->db)) {
108
                //This session id does not exist, mark it for forced regeneration
109
                $this->_forceRegenerateId = $id;
110
            }
111
        }
112
113
        return parent::openSession($savePath, $sessionName);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function regenerateID($deleteOldSession = false)
120
    {
121
        $oldID = session_id();
122
123
        // if no session is started, there is nothing to regenerate
124
        if (empty($oldID)) {
125
            return;
126
        }
127
128
        parent::regenerateID(false);
129
        $newID = session_id();
130
        // if session id regeneration failed, no need to create/update it.
131
        if (empty($newID)) {
132
            Yii::warning('Failed to generate new session ID', __METHOD__);
133
            return;
134
        }
135
136
        $row = $this->db->useMaster(function () use ($oldID) {
137
            return (new Query())->from($this->sessionTable)
138
               ->where(['id' => $oldID])
139
               ->createCommand($this->db)
140
               ->queryOne();
141
        });
142
143
        if ($row !== false && $this->getIsActive()) {
144
            if ($deleteOldSession) {
145
                $this->db->createCommand()
146
                    ->update($this->sessionTable, ['id' => $newID], ['id' => $oldID])
147
                    ->execute();
148
            } else {
149
                $row['id'] = $newID;
150
                $this->db->createCommand()
151
                    ->insert($this->sessionTable, $row)
152
                    ->execute();
153
            }
154
        }
155
    }
156
157
    /**
158
     * Ends the current session and store session data.
159
     * @since 2.0.17
160
     */
161
    public function close()
162
    {
163
        if ($this->getIsActive()) {
164
            // prepare writeCallback fields before session closes
165
            $this->fields = $this->composeFields();
166
            YII_DEBUG ? session_write_close() : @session_write_close();
167
        }
168
    }
169
170
    /**
171
     * Session read handler.
172
     * @internal Do not call this method directly.
173
     * @param string $id session ID
174
     * @return string|false the session data, or false on failure
175
     */
176
    public function readSession($id)
177
    {
178
        $query = $this->getReadQuery($id);
179
180
        if ($this->readCallback !== null) {
181
            $fields = $query->one($this->db);
182
            return $fields === false ? '' : $this->extractData($fields);
183
        }
184
185
        $data = $query->select(['data'])->scalar($this->db);
186
        return $data === false ? '' : $data;
187
    }
188
189
    /**
190
     * Session write handler.
191
     * @internal Do not call this method directly.
192
     * @param string $id session ID
193
     * @param string $data session data
194
     * @return bool whether session write is successful
195
     */
196
    public function writeSession($id, $data)
197
    {
198
        if ($this->getUseStrictMode() && $id === $this->_forceRegenerateId) {
199
            //Ignore write when forceRegenerate is active for this id
200
            return true;
201
        }
202
203
        // exception must be caught in session write handler
204
        // https://www.php.net/manual/en/function.session-set-save-handler.php#refsect1-function.session-set-save-handler-notes
205
        try {
206
            // ensure backwards compatability (fixed #9438)
207
            if ($this->writeCallback && !$this->fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
208
                $this->fields = $this->composeFields();
209
            }
210
            // ensure data consistency
211
            if (!isset($this->fields['data'])) {
212
                $this->fields['data'] = $data;
213
            } else {
214
                $_SESSION = $this->fields['data'];
215
            }
216
            // ensure 'id' and 'expire' are never affected by [[writeCallback]]
217
            $this->fields = array_merge($this->fields, [
218
                'id' => $id,
219
                'expire' => time() + $this->getTimeout(),
220
            ]);
221
            $this->fields = $this->typecastFields($this->fields);
222
            $this->db->createCommand()->upsert($this->sessionTable, $this->fields)->execute();
223
            $this->fields = [];
224
        } catch (\Exception $e) {
225
            Yii::$app->errorHandler->handleException($e);
226
            return false;
227
        }
228
        return true;
229
    }
230
231
    /**
232
     * Session destroy handler.
233
     * @internal Do not call this method directly.
234
     * @param string $id session ID
235
     * @return bool whether session is destroyed successfully
236
     */
237
    public function destroySession($id)
238
    {
239
        $this->db->createCommand()
240
            ->delete($this->sessionTable, ['id' => $id])
241
            ->execute();
242
243
        return true;
244
    }
245
246
    /**
247
     * Session GC (garbage collection) handler.
248
     * @internal Do not call this method directly.
249
     * @param int $maxLifetime the number of seconds after which data will be seen as 'garbage' and cleaned up.
250
     * @return int|false the number of deleted sessions on success, or false on failure
251
     */
252
    public function gcSession($maxLifetime)
253
    {
254
        return $this->db->createCommand()
255
            ->delete($this->sessionTable, '[[expire]]<:expire', [':expire' => time()])
256
            ->execute();
257
    }
258
259
    /**
260
     * Generates a query to get the session from db
261
     * @param string $id The id of the session
262
     * @return Query
263
     */
264
    protected function getReadQuery($id)
265
    {
266
        return (new Query())
267
            ->from($this->sessionTable)
268
            ->where('[[expire]]>:expire AND [[id]]=:id', [':expire' => time(), ':id' => $id]);
269
    }
270
271
    /**
272
     * Method typecasts $fields before passing them to PDO.
273
     * Default implementation casts field `data` to `\PDO::PARAM_LOB`.
274
     * You can override this method in case you need special type casting.
275
     *
276
     * @param array $fields Fields, that will be passed to PDO. Key - name, Value - value
277
     * @return array
278
     * @since 2.0.13
279
     */
280
    protected function typecastFields($fields)
281
    {
282
        if (isset($fields['data']) && !is_array($fields['data']) && !is_object($fields['data'])) {
283
            $fields['data'] = new PdoValue($fields['data'], \PDO::PARAM_LOB);
284
        }
285
286
        return $fields;
287
    }
288
}
289