-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathFile.php
More file actions
145 lines (125 loc) · 3.73 KB
/
Copy pathFile.php
File metadata and controls
145 lines (125 loc) · 3.73 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
<?php
declare(strict_types=1);
namespace Platformsh\Client\Session\Storage;
class File implements SessionStorageInterface
{
public const FILE_MODE = 0600;
public const DIR_MODE = 0700;
protected string $directory;
/**
* @param string|null $directory
* A writable directory where session files will be saved. Leave null
* to use the default.
*/
public function __construct(?string $directory = null)
{
$this->directory = $directory ?: $this->getDefaultDirectory();
}
/**
* @throws \Exception
*/
public function save(string $sessionId, array $data): void
{
$filename = $this->getFilename($sessionId);
if (empty($data)) {
if (file_exists($filename)) {
unlink($filename);
}
return;
}
$this->mkDir(dirname($filename));
$result = file_put_contents($filename, json_encode($data), LOCK_EX);
if ($result === false) {
throw new \Exception("Failed to save session to file: {$filename}");
}
chmod($filename, self::FILE_MODE);
}
public function load(string $sessionId): array
{
$data = [];
$filename = $this->getFilename($sessionId);
if (is_readable($filename)) {
$raw = file_get_contents($filename);
if ($raw !== false) {
$data = json_decode($raw, true);
}
}
return is_array($data) ? $data : [];
}
/**
* Get the default directory for session files.
*/
protected function getDefaultDirectory(): string
{
// Default to ~/.platformsh/.session, but if it's not writable, fall
// back to the temporary directory.
$home = $this->getHomeDirectory();
if ($home !== null) {
$default = rtrim($home, '/') . '/.platformsh/.session';
if ($this->canWrite($default)) {
return $default;
}
}
$temp = sys_get_temp_dir() . '/.platformsh-client/.session';
if ($this->canWrite($temp)) {
return $temp;
}
throw new \RuntimeException('Unable to find a writable session storage directory');
}
/**
* Tests whether a file path is writable (even if it doesn't exist).
*/
protected function canWrite(string $path): bool
{
if (is_writable($path)) {
return true;
}
$current = $path;
while (! file_exists($current) && ($parent = dirname($current)) && $parent !== $current) {
if (is_writable($parent)) {
return true;
}
$current = $parent;
}
return false;
}
/**
* Finds the user's home directory.
*/
protected function getHomeDirectory(): ?string
{
$home = getenv('HOME');
if (! $home && ($userProfile = getenv('USERPROFILE'))) {
$home = $userProfile;
}
if (! $home || ! is_dir($home)) {
return null;
}
return $home;
}
protected function getFilename(string $sessionId): string
{
$id = preg_replace('/[^\w\-]+/', '-', $sessionId);
$dir = $this->getDirectory();
return "{$dir}/sess-{$id}/sess-{$id}.json";
}
protected function getDirectory(): string
{
return rtrim($this->directory, '/');
}
/**
* Create a directory.
*
*@throws \Exception
*/
protected function mkDir(string $dir): void
{
if (! file_exists($dir)) {
mkdir($dir, self::DIR_MODE, true);
chmod($dir, self::DIR_MODE);
}
if (! is_dir($dir)) {
throw new \Exception("Failed to create directory: {$dir}");
}
}
}