/
File.php
513 lines (468 loc) · 15.5 KB
/
File.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
namespace Concrete\Core\File\Service;
use Zend\Http\Client\Adapter\Exception\TimeoutException;
use Config;
use Environment;
use Exception;
use Core;
use Concrete\Core\Support\Facade\Application as CoreApplication;
/**
* File helper.
*
* Functions useful for working with files and directories.
*
* Used as follows:
* <code>
* $file = Core::make('helper/file');
* $path = 'http://www.concrete5.org/tools/get_latest_version_number';
* $contents = $file->getContents($path);
* echo $contents;
* </code>
*
* \@package Helpers
*
* @category Concrete
*
* @author Andrew Embler <andrew@concrete5.org>
* @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
* @license http://www.concrete5.org/license/ MIT License
*/
class File
{
/**
* Returns the contents of a directory.
*
*
*/
public function getDirectoryContents($dir, $ignoreFiles = [], $recursive = false)
{
$aDir = [];
if (is_dir($dir)) {
$handle = opendir($dir);
while (($file = readdir($handle)) !== false) {
if (substr($file, 0, 1) != '.' && (!in_array($file, $ignoreFiles))) {
if (is_dir($dir . '/' . $file)) {
if ($recursive) {
$aDir = array_merge($aDir, $this->getDirectoryContents($dir . '/' . $file, $ignoreFiles, $recursive));
$file = $dir . '/' . $file;
}
$aDir[] = preg_replace("/\/\//si", '/', $file);
} else {
if ($recursive) {
$file = $dir . '/' . $file;
}
$aDir[] = preg_replace("/\/\//si", '/', $file);
}
}
}
closedir($handle);
}
return $aDir;
}
/**
* Removes the extension of a filename, uncamelcases it.
*
* @param string $filename
*
* @return string
*/
public function unfilename($filename)
{
$parts = $this->splitFilename($filename);
$txt = Core::make('helper/text');
/* @var $txt \Concrete\Core\Utility\Service\Text */
return $txt->unhandle($parts[0] . $parts[1]);
}
/**
* Recursively copies all items in the source directory or file to the target directory.
*
* @param string $source Source dir/file to copy
* @param string $target Place to copy the source
* @param int $mode What to chmod the file to
*/
public function copyAll($source, $target, $mode = null)
{
if (is_dir($source)) {
if ($mode == null) {
@mkdir($target, Config::get('concrete.filesystem.permissions.directory'));
@chmod($target, Config::get('concrete.filesystem.permissions.directory'));
} else {
@mkdir($target, $mode);
@chmod($target, $mode);
}
$d = dir($source);
while (false !== ($entry = $d->read())) {
if (substr($entry, 0, 1) === '.') {
continue;
}
$Entry = $source . '/' . $entry;
if (is_dir($Entry)) {
$this->copyAll($Entry, $target . '/' . $entry, $mode);
continue;
}
copy($Entry, $target . '/' . $entry);
if ($mode == null) {
@chmod($target . '/' . $entry, $this->getCreateFilePermissions($target)->file);
} else {
@chmod($target . '/' . $entry, $mode);
}
}
$d->close();
} else {
if ($mode == null) {
$mode = $this->getCreateFilePermissions(dirname($target))->file;
}
copy($source, $target);
chmod($target, $mode);
}
}
/**
* Returns an object with two permissions modes (octal):
* one for files: $res->file
* and another for directories: $res->dir.
*
* @param string $path (optional)
*
* @return \stdClass
*/
public function getCreateFilePermissions($path = null)
{
try {
if (!isset($path)) {
$path = DIR_FILES_UPLOADED_STANDARD;
}
if (!is_dir($path)) {
$path = @dirname($path);
}
$perms = @fileperms($path);
if (!$perms) {
throw new Exception(t('An error occurred while attempting to determine file permissions.'));
}
clearstatcache();
$dir_perms = substr(decoct($perms), 1);
$file_perms = "0";
$parts[] = substr($dir_perms, 1, 1);
$parts[] = substr($dir_perms, 2, 1);
$parts[] = substr($dir_perms, 3, 1);
foreach ($parts as $p) {
if (intval($p) % 2 == 0) {
$file_perms .= $p;
continue;
}
$file_perms .= intval($p) - 1;
}
} catch (Exception $e) {
return false;
}
$res = new \stdClass();
$res->file = intval($file_perms, 8);
$res->dir = intval($dir_perms, 8);
return $res;
}
/**
* Removes all files from within a specified directory.
*
* @param string $source Directory
* @param bool $inc Remove the passed directory as well or leave it alone
*
* @return bool Whether the methods succeeds or fails
*/
public function removeAll($source, $inc = false)
{
if (!is_dir($source)) {
return false;
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $path) {
if ($path->isDir()) {
if (!@rmdir($path->__toString())) {
return false;
}
} else {
if (!@unlink($path->__toString())) {
return false;
}
}
}
if ($inc) {
if (!@rmdir($source)) {
return false;
}
}
return true;
}
/**
* Takes a path to a file and sends it to the browser, streaming it, and closing the HTTP connection afterwards. Basically a force download method.
*
* @param stings $file
*/
public function forceDownload($file)
{
session_write_close();
ob_clean();
header('Content-type: application/octet-stream');
$filename = basename($file);
header("Content-Disposition: attachment; filename=\"$filename\"");
header('Content-Length: ' . filesize($file));
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Transfer-Encoding: binary");
header("Content-Encoding: plainbinary");
// This code isn't ready yet. It will allow us to no longer force download
/*
$h = Core::make('helper/mime');
$mimeType = $h->mimeFromExtension($this->getExtension($file));
header('Content-type: ' . $mimeType);
*/
$buffer = '';
$chunk = 1024 * 1024;
$handle = fopen($file, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunk);
echo $buffer;
}
fclose($handle);
exit;
}
/**
* Returns the full path to the temporary directory.
*
* @return string
*/
public function getTemporaryDirectory()
{
$temp = Config::get('concrete.filesystem.temp_directory');
if ($temp && @is_dir($temp)) {
return $temp;
}
if (!is_dir(DIR_FILES_UPLOADED_STANDARD . '/tmp')) {
@mkdir(DIR_FILES_UPLOADED_STANDARD . '/tmp', Config::get('concrete.filesystem.permissions.directory'));
@chmod(DIR_FILES_UPLOADED_STANDARD . '/tmp', Config::get('concrete.filesystem.permissions.directory'));
@touch(DIR_FILES_UPLOADED_STANDARD . '/tmp/index.html');
}
if (is_dir(DIR_FILES_UPLOADED_STANDARD . '/tmp') && is_writable(DIR_FILES_UPLOADED_STANDARD . '/tmp')) {
return DIR_FILES_UPLOADED_STANDARD . '/tmp';
}
if ($temp = getenv('TMP')) {
return str_replace(DIRECTORY_SEPARATOR, '/', $temp);
}
if ($temp = getenv('TEMP')) {
return str_replace(DIRECTORY_SEPARATOR, '/', $temp);
}
if ($temp = getenv('TMPDIR')) {
return str_replace(DIRECTORY_SEPARATOR, '/', $temp);
}
$temp = tempnam(__FILE__, '');
if (file_exists($temp)) {
unlink($temp);
return str_replace(DIRECTORY_SEPARATOR, '/', dirname($temp));
}
}
/**
* Adds content to a new line in a file. If a file is not there it will be created.
*
* @param string $filename
* @param string $content
*
* @return bool
*/
public function append($filename, $content)
{
return file_put_contents($filename, $content, FILE_APPEND) !== false;
}
/**
* Just a consistency wrapper for file_get_contents
* Should use curl if it exists and fopen isn't allowed (thanks Remo).
*
* @param string $filename
* @param string $timeout
*
* @throws TimeoutException Request timed out
*
* @return string|bool Returns false in case of failure
*/
public function getContents($file, $timeout = null)
{
$url = @parse_url($file);
if (isset($url['scheme']) && isset($url['host'])) {
$app = CoreApplication::getFacadeApplication();
$client = $app->make('http/client')->setUri($file);
if (!empty($timeout) && $timeout > 0) {
$client->setOptions(['timeout' => $timeout]);
}
try {
$response = $client->send();
} catch (TimeoutException $x) {
throw $x;
} catch (Exception $x) {
$response = null;
}
return $response ? $response->getBody() : false;
} else {
$contents = @file_get_contents($file);
if ($contents !== false) {
return $contents;
}
}
return false;
}
/**
* Removes contents of the file.
*
* @param $filename
*
* @return bool
*/
public function clear($file)
{
return file_put_contents($file, '') !== false;
}
/**
* Cleans up a filename and returns the cleaned up version.
*
* @param string $file
*
* @return string
*/
public function sanitize($file)
{
// Let's build an ASCII-only version of name, to avoid filesystem-specific encoding issues.
$asciiName = Core::make('helper/text')->asciify($file);
// Let's keep only letters, numbers, underscore and dots.
$asciiName = trim(preg_replace(["/[\\s]/", "/[^0-9A-Z_a-z-.]/"], ["_", ""], $asciiName));
// Trim underscores at start and end
$asciiName = trim($asciiName, '_');
if (!strlen(str_replace('.', '', $asciiName))) {
// If the resulting name is empty (or we have only dots in it)
$asciiName = md5($file);
} elseif (preg_match('/^\.\w+$/', $asciiName)) {
// If the resulting name is only composed by the file extension
$asciiName = md5($file) . $asciiName;
}
return $asciiName;
}
/**
* Splits a filename into directory, base file name, extension.
* If the file name starts with a dot and it's the only dot (eg: '.htaccess'), we don't consider the file to have an extension.
*
* @param string $filename
*
* @return array
*/
public function splitFilename($filename)
{
$result = ['', '', ''];
if (is_string($filename)) {
$result[1] = $filename;
$slashAt = strrpos(str_replace('\\', '/', $result[1]), '/');
if ($slashAt !== false) {
$result[0] = substr($result[1], 0, $slashAt + 1);
$result[1] = (string) substr($result[1], $slashAt + 1);
}
$dotAt = strrpos($result[1], '.');
if (($dotAt !== false) && ($dotAt > 0)) {
$result[2] = (string) substr($result[1], $dotAt + 1);
$result[1] = substr($result[1], 0, $dotAt);
}
}
return $result;
}
/**
* Returns the extension for a file name.
*
* @param string $filename
*
* @return string
*/
public function getExtension($filename)
{
$parts = $this->splitFilename($filename);
return $parts[2];
}
/**
* Takes a path and replaces the files extension in that path with the specified extension.
*
* @param string $filename
* @param string $extension
*
* @return string
*/
public function replaceExtension($filename, $extension)
{
$parts = $this->splitFilename($filename);
$newFilename = $parts[0] . $parts[1];
if (is_string($extension) && ($extension !== '')) {
$newFilename .= '.' . $extension;
}
return $newFilename;
}
/**
* Checks if two path are the same, considering directory separator and OS case sensitivity.
*
* @param string $path1
* @param string $path2
*
* @return bool
*/
public function isSamePath($path1, $path2)
{
$path1 = str_replace(DIRECTORY_SEPARATOR, '/', $path1);
$path2 = str_replace(DIRECTORY_SEPARATOR, '/', $path2);
// Check if OS is case insensitive
$checkFile = strtoupper(__FILE__);
if ($checkFile === __FILE__) {
$checkFile = strtolower(__FILE__);
}
if (@is_file($checkFile)) {
$same = (strcasecmp($path1, $path2) === 0) ? true : false;
} else {
$same = ($path1 === $path2) ? true : false;
}
return $same;
}
/**
* Try to set the executable bit of a file.
*
* @param string $file The full path
* @param string $who One of 'user', 'group', 'all'
*
* @throws Exception Throws an exception in case of errors
*/
public function makeExecutable($file, $who = 'all')
{
if (!is_file($file)) {
throw new Exception(t('File %s could not be found.', $file));
}
$perms = @fileperms($file);
if ($perms === false) {
throw new Exception(t('Unable to retrieve the permissions of the file %s', $file));
}
$currentMode = $perms & 0777;
switch ($who) {
case 'user':
$newMode = $currentMode | (1 << 0);
break;
case 'group':
$newMode = $currentMode | (1 << 3);
break;
case 'all':
$newMode = $currentMode | (1 << 6);
break;
default:
throw new Exception(t('Bad parameter: %s', '$who'));
}
if ($currentMode !== $newMode) {
if (@chmod($file, $newMode) === false) {
throw new Exception(t('Unable to set the permissions of the file %s', $file));
}
}
}
}