-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempDirectory.php
196 lines (172 loc) · 4.24 KB
/
TempDirectory.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* @file
* Contains \derhasi\tempdirectory\TempDirectory.
*/
namespace derhasi\tempdirectory;
/**
* Class TempDirectory.
*/
class TempDirectory {
/**
* Root path for the temporary directory.
*
* @var string
*/
protected $root;
/**
* Name for the temp directory.
*
* @var string
*/
protected $name;
/**
* Unique ID for building the root.
*
* @var string
*/
protected $subdir;
/**
* Constructor.
*
* @param string $name
* An optional name for building multiple
*/
function __construct($name = "") {
$this->name = $name;
$this->setSubdir();
$this->createDirectory();
}
/**
* Destructor.
*/
public function __destruct() {
$this->destroyDirectory();
}
/**
* Provides the temp directory root for this instance.
*
* @return string
*/
public function getRoot() {
return $this->root;
}
/**
* Provides the absolute path for a temp directory subfolder.
*
* @param string $subfolder
* Relative path of the subfolder within the temporary directory.
* @return string
* Absolute path to the subfolder within the directory.
*/
public function getPath($subfolder = '') {
// Remove leading and trailing slashes.
trim($subfolder, '/');
if (strpos($subfolder, '..') !== FALSE) {
throw new \Exception('Subfolder must not contain any "..".');
}
if (strlen($subfolder)) {
return $this->root . '/'. $subfolder;
}
return $this->root;
}
protected function getSubdir() {
if (!isset($this->subdir)) {
$this->setSubdir();
}
return $this->subdir;
}
/**
* Creates a unique name for the subdirectory.
*/
protected function setSubdir() {
$sha = sha1($this->name);
// Only allow word characters.
$prefix = preg_replace('/[\W]/i', '', $this->name);
$time = time();
$this->subdir = "$prefix-$sha-$time";
}
/**
* Creates the directory for this instance.
*/
protected function createDirectory() {
if (!isset($this->root)) {
$this->root = $this->buildRoot();
mkdir($this->root);
}
}
/**
* Retrieve root path, that is not already taken.
*
* @return string
*/
protected function buildRoot() {
$root = $this->getAndPrepareSystemTempDir() . '/' . $this->getSubdir();
if (file_exists($root)) {
$count = 1;
while (file_exists($root . '_' . $count)) {
$count++;
}
return $root . '_' . $count;
}
return $root;
}
/**
* Helper to get and prepare system temp directory.
*
* @return string
*/
protected function getAndPrepareSystemTempDir() {
$dir = sys_get_temp_dir();
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
return $dir;
}
/**
* Removes directory for this instance.
*/
protected function destroyDirectory() {
TempDirectory::removeRecursive($this->root);
}
/**
* This function recursively deletes all files and folders under the given
* directory, and then the directory itself.
*
* equivalent to Bash: rm -r $path
* @param string $path
*/
public static function removeRecursive($path) {
/** @var \SplFileInfo $file */
// If the path is not a directory we can simply unlink it.
if (!is_dir($path)) {
chmod($path, 0777);
return unlink($path);
}
// Otherwise we go through the whole directory.
$it = new \RecursiveDirectoryIterator($path);
// Fix permissions for all folders and files first, before we try to change
// the file mode.
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
if ('.' === $file->getBasename() || '..' === $file->getBasename()) {
continue;
}
chmod($file->getPathname(), 0777);
}
// Delete files first, before folders are removed.
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
if ('.' === $file->getBasename() || '..' === $file->getBasename()) {
continue;
}
if ($file->isDir() && !$file->isLink()) {
rmdir($file->getPathname());
}
else {
unlink($file->getPathname());
}
}
return rmdir($path);
}
}