AbstractPlatformFilesystem.php
8.74 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
namespace Nextend\Framework\Filesystem;
use Nextend\Framework\Url\Url;
if (!defined('NEXTEND_RELATIVE_CACHE_WEB')) {
define('NEXTEND_RELATIVE_CACHE_WEB', '/cache/nextend/web');
define('NEXTEND_CUSTOM_CACHE', 0);
} else {
define('NEXTEND_CUSTOM_CACHE', 1);
}
if (!defined('NEXTEND_RELATIVE_CACHE_NOTWEB')) {
define('NEXTEND_RELATIVE_CACHE_NOTWEB', '/cache/nextend/notweb');
}
abstract class AbstractPlatformFilesystem {
public $paths = array();
/**
* @var string Absolute path which match to the baseuri. It must not end with /
* @example /asd/xyz/wordpress
*/
protected $_basepath;
protected $dirPermission = 0777;
protected $filePermission = 0666;
protected $translate = array();
public function init() {
}
public function getPaths() {
return $this->paths;
}
public function check($base, $folder) {
static $checked = array();
if (!isset($checked[$base . '/' . $folder])) {
$cacheFolder = $base . '/' . $folder;
if (!$this->existsFolder($cacheFolder)) {
if ($this->is_writable($base)) {
$this->createFolder($cacheFolder);
} else {
die('<div style="position:fixed;background:#fff;width:100%;height:100%;top:0;left:0;z-index:100000;">' . sprintf('<h2><b>%s</b> is not writable.</h2>', $base) . '</div>');
}
} else if (!$this->is_writable($cacheFolder)) {
die('<div style="position:fixed;background:#fff;width:100%;height:100%;top:0;left:0;z-index:100000;">' . sprintf('<h2><b>%s</b> is not writable.</h2>', $cacheFolder) . '</div>');
}
$checked[$base . '/' . $folder] = true;
}
}
public function measurePermission($testDir) {
while ('.' != $testDir && !is_dir($testDir)) {
$testDir = dirname($testDir);
}
if ($stat = @stat($testDir)) {
$this->dirPermission = $stat['mode'] & 0007777;
$this->filePermission = $this->dirPermission & 0000666;
}
}
/**
* @param $path
*
* @return mixed
*/
public function toLinux($path) {
return str_replace(DIRECTORY_SEPARATOR, '/', $path);
}
/**
* @return string
*/
public function getBasePath() {
return $this->_basepath;
}
/**
* @param $path
*/
public function setBasePath($path) {
$this->_basepath = $path;
}
public function getWebCachePath() {
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_WEB;
}
public function getNotWebCachePath() {
return $this->getBasePath() . NEXTEND_RELATIVE_CACHE_NOTWEB;
}
/**
* @param $path
*
* @return string
*/
public function pathToAbsoluteURL($path) {
return Url::pathToUri($path);
}
/**
* @param $path
*
* @return string
*/
public function pathToRelativePath($path) {
return preg_replace('/^' . preg_quote($this->_basepath, '/') . '/', '', str_replace('/', DIRECTORY_SEPARATOR, $path));
}
/**
* @param $path
*
* @return string
*/
public function pathToAbsolutePath($path) {
return $this->_basepath . str_replace('/', DIRECTORY_SEPARATOR, $path);
}
/**
* @param $url
*
* @return string
*/
public function absoluteURLToPath($url) {
$fullUri = Url::getFullUri();
if (substr($url, 0, strlen($fullUri)) == $fullUri) {
return str_replace($fullUri, $this->_basepath, $url);
}
return $url;
}
/**
* @param $file
*
* @return bool
*/
public function fileexists($file) {
return is_file($file);
}
/**
* @param $file
*
* @return bool
*/
public function safefileexists($file) {
return realpath($file) && is_file($file);
}
/**
*
* @param $dir
*
* @return array Folder names without trailing slash
*/
public function folders($dir) {
if (!is_dir($dir)) {
return array();
}
$folders = array();
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) $folders[] = $file;
}
return $folders;
}
/**
* @param $path
*
* @return bool
*/
public function is_writable($path) {
return is_writable($path);
}
/**
* @param $path
*
* @return bool
*/
public function createFolder($path) {
return mkdir($path, $this->dirPermission, true);
}
public function deleteFolder($dir) {
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (!$this->deleteFolder($dir . DIRECTORY_SEPARATOR . $file)) {
chmod($dir . DIRECTORY_SEPARATOR . $file, $this->dirPermission);
if (!$this->deleteFolder($dir . DIRECTORY_SEPARATOR . $file)) return false;
}
}
return rmdir($dir);
}
public function existsFolder($path) {
return is_dir($path);
}
public function files($path) {
$files = array();
if (is_dir($path)) {
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
if ($file[0] != ".") {
$files[] = $file;
}
}
closedir($dh);
}
}
return $files;
}
/**
* @param $path
*
* @return bool
*/
public function existsFile($path) {
return file_exists($path);
}
/**
* @param $path
* @param $buffer
*
* @return int
*/
public function createFile($path, $buffer) {
return file_put_contents($path, $buffer);
}
/**
* @param $path
*
* @return string
*/
public function readFile($path) {
return file_get_contents($path);
}
/**
* convert dir alias to normal format
*
* @param $pathName
*
* @return mixed
*/
public function dirFormat($pathName) {
return str_replace(".", DIRECTORY_SEPARATOR, $pathName);
}
public function getImagesFolder() {
return '';
}
public function realpath($path) {
return rtrim(realpath($path), '/\\');
}
public function registerTranslate($from, $to) {
$this->translate[$from] = $to;
}
protected function trailingslashit($string) {
return $this->untrailingslashit($string) . '/';
}
protected function untrailingslashit($string) {
return rtrim($string, '/\\');
}
public function convertToRealDirectorySeparator($path) {
return str_replace(DIRECTORY_SEPARATOR == '/' ? '\\' : '/', DIRECTORY_SEPARATOR, $path);
}
public function get_temp_dir() {
static $temp = '';
if (defined('SS_TEMP_DIR')) return $this->trailingslashit(SS_TEMP_DIR);
if ($temp) return $this->trailingslashit($temp);
if (function_exists('sys_get_temp_dir')) {
$temp = sys_get_temp_dir();
if (@is_dir($temp) && $this->is_writable($temp)) return $this->trailingslashit($temp);
}
$temp = ini_get('upload_tmp_dir');
if (@is_dir($temp) && $this->is_writable($temp)) return $this->trailingslashit($temp);
$temp = $this->getNotWebCachePath() . '/';
if (is_dir($temp) && $this->is_writable($temp)) return $temp;
return '/tmp/';
}
public function tempnam($filename = '', $dir = '') {
if (empty($dir)) {
$dir = $this->get_temp_dir();
}
if (empty($filename) || '.' == $filename || '/' == $filename || '\\' == $filename) {
$filename = time();
}
// Use the basename of the given file without the extension as the name for the temporary directory
$temp_filename = basename($filename);
$temp_filename = preg_replace('|\.[^.]*$|', '', $temp_filename);
// If the folder is falsey, use its parent directory name instead.
if (!$temp_filename) {
return $this->tempnam(dirname($filename), $dir);
}
// Suffix some random data to avoid filename conflicts
$temp_filename .= '-' . md5(uniqid(rand() . time()));
$temp_filename .= '.tmp';
$temp_filename = $dir . $temp_filename;
$fp = @fopen($temp_filename, 'x');
if (!$fp && is_writable($dir) && file_exists($temp_filename)) {
return $this->tempnam($filename, $dir);
}
if ($fp) {
fclose($fp);
}
return $temp_filename;
}
}