1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\web; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* MultiFieldSession is the base class for session storage implementations with multi-field data storage support. |
12
|
|
|
* |
13
|
|
|
* With multi-field data storage, session data can be split between several fields in the storage record. |
14
|
|
|
* Using such a storage allows saving particular session data into separated field, which then can be used |
15
|
|
|
* to manipulate sessions in the way plain PHP does not allow. |
16
|
|
|
* |
17
|
|
|
* For example the ID of the authenticated user can be saved as separated column in the MySQL 'session' table, |
18
|
|
|
* which allows to query all active sessions for a particular user or terminate them at will. |
19
|
|
|
* |
20
|
|
|
* Customizing of the session writing is performed via [[writeCallback]], reading via [[readCallback]]. |
21
|
|
|
* |
22
|
|
|
* While extending this class you should use [[composeFields()]] method - while writing the session data into the storage and |
23
|
|
|
* [[extractData()]] - while reading session data from the storage. |
24
|
|
|
* |
25
|
|
|
* @property bool $useCustomStorage Whether to use custom storage. This property is read-only. |
26
|
|
|
* |
27
|
|
|
* @author Paul Klimov <[email protected]> |
28
|
|
|
* @since 2.0.6 |
29
|
|
|
*/ |
30
|
|
|
abstract class MultiFieldSession extends Session |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var callable a callback that will be called during session data reading. |
34
|
|
|
* The signature of the callback should be as follows: |
35
|
|
|
* |
36
|
|
|
* ``` |
37
|
|
|
* function ($fields) |
38
|
|
|
* ``` |
39
|
|
|
* |
40
|
|
|
* where `$fields` is the storage field set for read session and `$session` is this session instance. |
41
|
|
|
* If callback returns an array, it will be merged into the session data. |
42
|
|
|
* |
43
|
|
|
* For example: |
44
|
|
|
* |
45
|
|
|
* ```php |
46
|
|
|
* function ($fields) { |
47
|
|
|
* return [ |
48
|
|
|
* 'expireDate' => Yii::$app->formatter->asDate($fields['expire']), |
49
|
|
|
* ]; |
50
|
|
|
* } |
51
|
|
|
* ``` |
52
|
|
|
*/ |
53
|
|
|
public $readCallback; |
54
|
|
|
/** |
55
|
|
|
* @var callable a callback that will be called during session data writing. |
56
|
|
|
* The signature of the callback should be as follows: |
57
|
|
|
* |
58
|
|
|
* ``` |
59
|
|
|
* function ($session) |
60
|
|
|
* ``` |
61
|
|
|
* |
62
|
|
|
* where `$session` is this session instance, this variable can be used to retrieve session data. |
63
|
|
|
* Callback should return the actual fields set, which should be saved into the session storage. |
64
|
|
|
* |
65
|
|
|
* For example: |
66
|
|
|
* |
67
|
|
|
* ```php |
68
|
|
|
* function ($session) { |
69
|
|
|
* return [ |
70
|
|
|
* 'user_id' => Yii::$app->user->id, |
71
|
|
|
* 'ip' => $_SERVER['REMOTE_ADDR'], |
72
|
|
|
* 'is_trusted' => $session->get('is_trusted', false), |
73
|
|
|
* ]; |
74
|
|
|
* } |
75
|
|
|
* ``` |
76
|
|
|
*/ |
77
|
|
|
public $writeCallback; |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns a value indicating whether to use custom session storage. |
82
|
|
|
* This method overrides the parent implementation and always returns true. |
83
|
|
|
* @return bool whether to use custom storage. |
84
|
|
|
*/ |
85
|
3 |
|
public function getUseCustomStorage() |
86
|
|
|
{ |
87
|
3 |
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Composes storage field set for session writing. |
92
|
|
|
* @param string $id Optional session id |
93
|
|
|
* @param string $data Optional session data |
94
|
|
|
* @return array storage fields |
95
|
|
|
*/ |
96
|
6 |
|
protected function composeFields($id = null, $data = null) |
97
|
|
|
{ |
98
|
6 |
|
$fields = $this->writeCallback ? call_user_func($this->writeCallback, $this) : []; |
99
|
6 |
|
if ($id !== null) { |
100
|
|
|
$fields['id'] = $id; |
101
|
|
|
} |
102
|
6 |
|
if ($data !== null) { |
103
|
|
|
$fields['data'] = $data; |
104
|
|
|
} |
105
|
6 |
|
return $fields; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Extracts session data from storage field set. |
110
|
|
|
* @param array $fields storage fields. |
111
|
|
|
* @return string session data. |
112
|
|
|
*/ |
113
|
|
|
protected function extractData($fields) |
114
|
|
|
{ |
115
|
|
|
if ($this->readCallback !== null) { |
116
|
|
|
if (!isset($fields['data'])) { |
117
|
|
|
$fields['data'] = ''; |
118
|
|
|
} |
119
|
|
|
$extraData = call_user_func($this->readCallback, $fields); |
120
|
|
|
if (!empty($extraData)) { |
121
|
|
|
session_decode($fields['data']); |
122
|
|
|
$_SESSION = array_merge((array) $_SESSION, (array) $extraData); |
123
|
|
|
return session_encode(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $fields['data']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return isset($fields['data']) ? $fields['data'] : ''; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|