|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Session |
|
4
|
|
|
* |
|
5
|
|
|
* セッション操作のためのファンクション群 |
|
6
|
|
|
* |
|
7
|
|
|
* @package risoluto |
|
8
|
|
|
* @author Risoluto Developers |
|
9
|
|
|
* @license http://opensource.org/licenses/bsd-license.php new BSD license |
|
10
|
|
|
* @copyright (C) 2008-2015 Risoluto Developers / All Rights Reserved. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
//------------------------------------------------------// |
|
14
|
|
|
// 名前空間の定義 |
|
15
|
|
|
//------------------------------------------------------// |
|
16
|
|
|
namespace Risoluto; |
|
17
|
|
|
|
|
18
|
|
|
class Session |
|
19
|
|
|
{ |
|
20
|
|
|
//------------------------------------------------------// |
|
21
|
|
|
// クラス変数定義 |
|
22
|
|
|
//------------------------------------------------------// |
|
23
|
|
|
/** |
|
24
|
|
|
* $sesspath |
|
25
|
|
|
* @access private |
|
26
|
|
|
* @var string セッションファイル保存ディレクトリ |
|
27
|
|
|
*/ |
|
28
|
|
|
private $sesspath = RISOLUTO_SESS; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* $sessname |
|
32
|
|
|
* @access private |
|
33
|
|
|
* @var string セッション名 |
|
34
|
|
|
*/ |
|
35
|
|
|
private $sessname = 'RISOLUTOSESS'; |
|
36
|
|
|
|
|
37
|
|
|
//------------------------------------------------------// |
|
38
|
|
|
// クラスメソッド定義 |
|
39
|
|
|
//------------------------------------------------------// |
|
40
|
|
|
/** |
|
41
|
|
|
* start($path = '', $name = '') |
|
42
|
|
|
* |
|
43
|
|
|
* セッションを開始する |
|
44
|
|
|
* もし、すでにセッションが存在している場合は |
|
45
|
|
|
* そのセッションIDを用いてセッションをスタートする |
|
46
|
|
|
* セッションが存在しない場合は新規にセッションを生成し、スタートする |
|
47
|
|
|
* |
|
48
|
|
|
* @access public |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $path セッションファイル保存ディレクトリ |
|
51
|
|
|
* @param string $name セッション名 |
|
52
|
|
|
* |
|
53
|
|
|
* @return boolean セッション開始結果(true:正常終了/false:異常終了) |
|
54
|
|
|
*/ |
|
55
|
|
|
public function start($path = '', $name = '') |
|
56
|
|
|
{ |
|
57
|
|
|
// セッション保存ディレクトリが指定されていたらその値を採用 |
|
58
|
|
|
if (!empty($path)) { |
|
59
|
|
|
$this->sesspath = $path; |
|
60
|
|
|
} |
|
61
|
|
|
// セッション名が指定されていたらその値を採用 |
|
62
|
|
|
if (!empty($name)) { |
|
63
|
|
|
$this->sessname = $name; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// セッション保存ディレクトリをセット |
|
67
|
|
|
if (!empty($this->sesspath) and is_writable($this->sesspath)) { |
|
68
|
|
|
session_save_path($this->sesspath); |
|
69
|
|
|
// 指定されていないか書き込めないならfalseを返す |
|
70
|
|
|
} else { |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// セッション名の指定 |
|
75
|
|
|
session_name($this->sessname); |
|
76
|
|
|
|
|
77
|
|
|
// セッションが存在しない場合の処理 |
|
78
|
|
|
if (empty($_COOKIE[$this->sessname])) { |
|
79
|
|
|
// 生成したセッションIDを付与する |
|
80
|
|
|
$base = $this->genRand(); |
|
81
|
|
|
session_id($base); |
|
82
|
|
|
} // end of if |
|
83
|
|
|
|
|
84
|
|
|
// セッションタイムアウトの秒数をコンフィグから取得しセット |
|
85
|
|
|
$conf = new Conf; |
|
86
|
|
|
$conf->parse( RISOLUTO_CONF . 'risoluto.ini' ); |
|
87
|
|
|
session_set_cookie_params($conf->getIni( 'SESSION', 'timeout' )); |
|
88
|
|
|
|
|
89
|
|
|
// セッションの開始 |
|
90
|
|
|
return session_start(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* restart($path = '', $name = '') |
|
95
|
|
|
* |
|
96
|
|
|
* セッションを再スタートする() |
|
97
|
|
|
* |
|
98
|
|
|
* @access public |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $path セッションファイル保存ディレクトリ |
|
101
|
|
|
* @param string $name セッション名 |
|
102
|
|
|
* |
|
103
|
|
|
* @return boolean セッション再開始結果(true:正常終了/false:異常終了) |
|
104
|
|
|
*/ |
|
105
|
|
|
public function restart($path = '', $name = '') |
|
106
|
|
|
{ |
|
107
|
|
|
// セッションを終了してスタートさせる |
|
108
|
|
|
$this->end(); |
|
109
|
|
|
|
|
110
|
|
|
return $this->start($path, $name); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* end() |
|
115
|
|
|
* |
|
116
|
|
|
* セッションを終了する |
|
117
|
|
|
* |
|
118
|
|
|
* @access public |
|
119
|
|
|
* |
|
120
|
|
|
* @param void |
|
121
|
|
|
* |
|
122
|
|
|
* @return boolean セッション終了結果(true:正常終了/false:異常終了) |
|
123
|
|
|
*/ |
|
124
|
|
|
public function end() |
|
125
|
|
|
{ |
|
126
|
|
|
// クッキーを削除 |
|
127
|
|
|
if (isset($_COOKIE[$this->sessname])) { |
|
128
|
|
|
setcookie($this->sessname, '', time() - 42000, '/'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// スーパーグローバルな$_COOKIEと$_SESSIONをクリア |
|
132
|
|
|
$_COOKIE[$this->sessname] = []; |
|
133
|
|
|
$_SESSION = []; |
|
134
|
|
|
|
|
135
|
|
|
// セッションファイルを削除 |
|
136
|
|
|
$target = $this->sesspath . 'sess_' . session_id(); |
|
137
|
|
|
|
|
138
|
|
|
clearstatcache(true); |
|
139
|
|
|
if (file_exists($target) and is_file($target) and is_writeable($target)) { |
|
140
|
|
|
unlink($target); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return session_destroy(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* store($destination, $val) |
|
148
|
|
|
* |
|
149
|
|
|
* セッションへ値を格納する |
|
150
|
|
|
* 引数で指定された名称の変数へ、同じく引数で指定された値を格納する |
|
151
|
|
|
* |
|
152
|
|
|
* @access public |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $destination 格納先セッション変数名 |
|
155
|
|
|
* @param mixed $val 格納する値(number or string) |
|
156
|
|
|
* |
|
157
|
|
|
* @return boolean 常にtrue |
|
158
|
|
|
*/ |
|
159
|
|
|
public function store($destination, $val) |
|
160
|
|
|
{ |
|
161
|
|
|
if (isset($destination) and isset($val)) { |
|
162
|
|
|
$_SESSION[$destination] = $val; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
return true; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* load($from) |
|
170
|
|
|
* |
|
171
|
|
|
* セッションから値を取得する |
|
172
|
|
|
* 引数で指定された名称のセッション変数から値を取得する |
|
173
|
|
|
* |
|
174
|
|
|
* @access public |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $from 取得元セッション変数名 |
|
177
|
|
|
* |
|
178
|
|
|
* @return mixed 取得した値 |
|
179
|
|
|
*/ |
|
180
|
|
|
public function load($from) |
|
181
|
|
|
{ |
|
182
|
|
|
if (isset($from) and isset($_SESSION[$from])) { |
|
183
|
|
|
return $_SESSION[$from]; |
|
184
|
|
|
} else { |
|
185
|
|
|
return null; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* isThere($chkName) |
|
191
|
|
|
* |
|
192
|
|
|
* セッション中に引数で指定された名称を持つ値が存在するかをチェックする |
|
193
|
|
|
* |
|
194
|
|
|
* @access public |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $chkName 判定対象セッション変数名 |
|
197
|
|
|
* |
|
198
|
|
|
* @return boolean 存在状況(true:存在する/false:存在しない) |
|
199
|
|
|
*/ |
|
200
|
|
|
public function isThere($chkName) |
|
201
|
|
|
{ |
|
202
|
|
|
return isset($_SESSION[$chkName]); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* revoke($chkName) |
|
207
|
|
|
* |
|
208
|
|
|
* セッション中の引数で指定された名称を持つ値を抹消する |
|
209
|
|
|
* |
|
210
|
|
|
* @access public |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $chkName 抹消対象セッション変数名 |
|
213
|
|
|
* |
|
214
|
|
|
* @return boolean 常にtrue |
|
215
|
|
|
*/ |
|
216
|
|
|
public function revoke($chkName) |
|
217
|
|
|
{ |
|
218
|
|
|
if (isset($_SESSION[$chkName])) { |
|
219
|
|
|
unset($_SESSION[$chkName]); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return true; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* revokeAll() |
|
227
|
|
|
* |
|
228
|
|
|
* セッション中のすべての値を抹消する |
|
229
|
|
|
* |
|
230
|
|
|
* @access public |
|
231
|
|
|
* |
|
232
|
|
|
* @param void |
|
233
|
|
|
* |
|
234
|
|
|
* @return boolean 常にtrue |
|
235
|
|
|
*/ |
|
236
|
|
|
public function revokeAll() |
|
237
|
|
|
{ |
|
238
|
|
|
// セッション変数が存在するかをチェック |
|
239
|
|
|
if (isset($_SESSION)) { |
|
240
|
|
|
// すべての値を抹消する |
|
241
|
|
|
foreach ($_SESSION as $key) { |
|
242
|
|
|
$this->revoke($key); |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return true; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* genRand() |
|
251
|
|
|
* |
|
252
|
|
|
* 乱数を生成する |
|
253
|
|
|
* |
|
254
|
|
|
* @access public |
|
255
|
|
|
* |
|
256
|
|
|
* @param void |
|
257
|
|
|
* |
|
258
|
|
|
* @return string 生成された乱数値 |
|
259
|
|
|
*/ |
|
260
|
|
|
public function genRand() |
|
261
|
|
|
{ |
|
262
|
|
|
if (function_exists('openssl_random_pseudo_bytes')) { |
|
263
|
|
|
// openssl_random_pseudo_bytes()が使用できない場合はそれを使って乱数を生成 |
|
264
|
|
|
$retval = bin2hex(openssl_random_pseudo_bytes(32)); |
|
265
|
|
|
} else { |
|
266
|
|
|
// openssl_random_pseudo_bytes()が使用できない場合は |
|
267
|
|
|
// システムよりマイクロセコンドの精度で時刻情報を取得し乱数を生成 |
|
268
|
|
|
list($usec, $sec) = explode(" ", microtime()); |
|
269
|
|
|
$seed = (double)$sec + ((double)$usec * 100000); |
|
270
|
|
|
|
|
271
|
|
|
mt_srand($seed); |
|
272
|
|
|
$retval = mt_rand(); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return $retval; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|