-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileHandler.php
More file actions
178 lines (152 loc) · 4.7 KB
/
Copy pathFileHandler.php
File metadata and controls
178 lines (152 loc) · 4.7 KB
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
175
176
177
178
<?php
namespace Bolt\Session\Handler;
use Bolt\Common\Thrower;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
/**
* Local filesystem session handler.
*
* @author Carson Full <carsonfull@gmail.com>
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class FileHandler extends AbstractHandler implements LazyWriteHandlerInterface
{
/** @var int */
protected $mode;
/** @var string */
protected $savePath;
/** @var \Symfony\Component\Filesystem\Filesystem */
protected $fs;
/** @var \Psr\Log\LoggerInterface */
protected $logger;
/**
* Constructor.
*
* @param string $savePath Path of directory to save session files.
* Default null will leave setting as defined by system temp directory.
* @param LoggerInterface $logger
* @param Filesystem $filesystem
*/
public function __construct($savePath = null, LoggerInterface $logger = null, Filesystem $filesystem = null)
{
$this->fs = $filesystem ?: new Filesystem();
$this->logger = $logger ?: new NullLogger();
// @see http://php.net/manual/en/session.configuration.php#ini.session.save-path
$mode = 0600;
$savePath = $savePath ?: sys_get_temp_dir();
// Handle 'N;/path' and 'N;octal-mode;/path` values. We don't currently support depth handling.
if ($count = substr_count($savePath, ';')) {
if ($count > 2) {
throw new \InvalidArgumentException(sprintf('Invalid argument $savePath \'%s\'', $savePath));
}
$path = explode(';', $savePath);
if ($count === 1) {
$savePath = $path[1];
} else {
$mode = intval($path[1], 8);
$savePath = $path[2];
}
}
if (!is_dir($savePath)) {
$this->fs->mkdir($savePath);
}
$this->mode = $mode;
$this->savePath = $savePath;
}
/**
* {@inheritdoc}
*/
public function read($sessionId)
{
$file = $this->getSessionFileName($sessionId);
try {
if ($this->fs->exists($file)) {
try {
return Thrower::call('file_get_contents', $file);
} catch (\ErrorException $e) {
$this->logger->error(sprintf('Unable to read session file: %s', $file), ['exception' => $e]);
}
}
} catch (IOException $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
return '';
}
/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$file = $this->getSessionFileName($sessionId);
try {
$this->fs->dumpFile($file, $data);
} catch (IOException $e) {
$this->logger->error('Unable to write session file to ' . $this->savePath);
return false;
}
try {
$this->fs->chmod($file, $this->mode);
} catch (IOException $e) {
$this->logger->error('Unable to set correct permissions on session file in ' . $this->savePath);
}
return true;
}
/**
* {@inheritdoc}
*/
public function updateTimestamp($sessionId, $data)
{
try {
$this->fs->touch($this->getSessionFileName($sessionId));
} catch (IOException $e) {
}
}
/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
$file = $this->getSessionFileName($sessionId);
try {
$this->fs->remove($file);
return true;
} catch (IOException $e) {
$this->logger->error('Unable to remove session file ' . $file);
return false;
}
}
/**
* {@inheritdoc}
*/
public function gc($maxlifetime)
{
$finder = new Finder();
$files = $finder->files()
->in($this->savePath)
->name('/\.sess$/')
->date("< now - $maxlifetime seconds")
;
foreach ($files as $file) {
try {
$this->fs->remove($file);
} catch (IOException $e) {
$this->logger->error('Unable to remove session file ' . $file);
}
}
return true;
}
/**
* Get the fully qualified file name of a session file based on ID.
*
* @param string $sessionId
*
* @return string
*/
private function getSessionFileName($sessionId)
{
return $this->savePath . DIRECTORY_SEPARATOR . $sessionId . '.sess';
}
}