|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pimf |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
|
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Pimf\Session\Storages; |
|
10
|
|
|
|
|
11
|
|
|
use Pimf\Contracts\Cleanable; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @package Session_Storages |
|
15
|
|
|
* @author Gjero Krsteski <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class File extends Storage implements Cleanable |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* The path to which the session files should be written. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $path; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $path |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct($path) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->path = (string)$path; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Load a session from storage by a given ID. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $key |
|
38
|
|
|
* |
|
39
|
|
|
* @return array|mixed|null |
|
40
|
|
|
*/ |
|
41
|
|
|
public function load($key) |
|
42
|
|
|
{ |
|
43
|
|
|
if (file_exists($path = $this->path . $key)) { |
|
44
|
|
|
return unserialize(file_get_contents($path)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
return null; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Save a given session to storage. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $session |
|
54
|
|
|
* @param array $config |
|
55
|
|
|
* @param bool $exists |
|
56
|
|
|
*/ |
|
57
|
|
|
public function save($session, $config, $exists) |
|
58
|
|
|
{ |
|
59
|
|
|
file_put_contents($this->path . $session['id'], serialize($session), LOCK_EX); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $key |
|
64
|
|
|
*/ |
|
65
|
|
|
public function delete($key) |
|
66
|
|
|
{ |
|
67
|
|
|
if (file_exists($this->path . $key)) { |
|
68
|
|
|
unlink($this->path . $key); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Delete all expired sessions from persistent storage. |
|
74
|
|
|
* |
|
75
|
|
|
* @param int $expiration |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed|void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function clean($expiration) |
|
80
|
|
|
{ |
|
81
|
|
|
$files = glob($this->path . '*'); |
|
82
|
|
|
|
|
83
|
|
|
if ($files === false) { |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
foreach ($files as $file) { |
|
88
|
|
|
if (filetype($file) == 'file' && filemtime($file) < $expiration) { |
|
89
|
|
|
unlink($file); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|