1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\helpers; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\ErrorException; |
12
|
|
|
use yii\base\Exception; |
13
|
|
|
use yii\base\InvalidArgumentException; |
14
|
|
|
use yii\base\InvalidConfigException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* BaseFileHelper provides concrete implementation for [[FileHelper]]. |
18
|
|
|
* |
19
|
|
|
* Do not use BaseFileHelper. Use [[FileHelper]] instead. |
20
|
|
|
* |
21
|
|
|
* @author Qiang Xue <[email protected]> |
22
|
|
|
* @author Alex Makarov <[email protected]> |
23
|
|
|
* @since 2.0 |
24
|
|
|
*/ |
25
|
|
|
class BaseFileHelper |
26
|
|
|
{ |
27
|
|
|
const PATTERN_NODIR = 1; |
28
|
|
|
const PATTERN_ENDSWITH = 4; |
29
|
|
|
const PATTERN_MUSTBEDIR = 8; |
30
|
|
|
const PATTERN_NEGATIVE = 16; |
31
|
|
|
const PATTERN_CASE_INSENSITIVE = 32; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string the path (or alias) of a PHP file containing MIME type information. |
35
|
|
|
*/ |
36
|
|
|
public static $mimeMagicFile = '@yii/helpers/mimeTypes.php'; |
37
|
|
|
/** |
38
|
|
|
* @var string the path (or alias) of a PHP file containing MIME aliases. |
39
|
|
|
* @since 2.0.14 |
40
|
|
|
*/ |
41
|
|
|
public static $mimeAliasesFile = '@yii/helpers/mimeAliases.php'; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Normalizes a file/directory path. |
46
|
|
|
* |
47
|
|
|
* The normalization does the following work: |
48
|
|
|
* |
49
|
|
|
* - Convert all directory separators into `DIRECTORY_SEPARATOR` (e.g. "\a/b\c" becomes "/a/b/c") |
50
|
|
|
* - Remove trailing directory separators (e.g. "/a/b/c/" becomes "/a/b/c") |
51
|
|
|
* - Turn multiple consecutive slashes into a single one (e.g. "/a///b/c" becomes "/a/b/c") |
52
|
|
|
* - Remove ".." and "." based on their meanings (e.g. "/a/./b/../c" becomes "/a/c") |
53
|
|
|
* |
54
|
|
|
* Note: For registered stream wrappers, the consecutive slashes rule |
55
|
|
|
* and ".."/"." translations are skipped. |
56
|
|
|
* |
57
|
|
|
* @param string $path the file/directory path to be normalized |
58
|
|
|
* @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`. |
59
|
|
|
* @return string the normalized file/directory path |
60
|
36 |
|
*/ |
61
|
|
|
public static function normalizePath($path, $ds = DIRECTORY_SEPARATOR) |
62
|
36 |
|
{ |
63
|
36 |
|
$path = rtrim(strtr($path, '/\\', $ds . $ds), $ds); |
64
|
35 |
|
if (strpos($ds . $path, "{$ds}.") === false && strpos($path, "{$ds}{$ds}") === false) { |
65
|
|
|
return $path; |
66
|
|
|
} |
67
|
1 |
|
// fix #17235 stream wrappers |
68
|
1 |
|
foreach (stream_get_wrappers() as $protocol) { |
69
|
1 |
|
if (strpos($path, "{$protocol}://") === 0) { |
70
|
|
|
return $path; |
71
|
|
|
} |
72
|
|
|
} |
73
|
1 |
|
// the path may contain ".", ".." or double slashes, need to clean them up |
74
|
1 |
|
if (strpos($path, "{$ds}{$ds}") === 0 && $ds == '\\') { |
75
|
|
|
$parts = [$ds]; |
76
|
1 |
|
} else { |
77
|
|
|
$parts = []; |
78
|
1 |
|
} |
79
|
1 |
|
foreach (explode($ds, $path) as $part) { |
80
|
1 |
|
if ($part === '..' && !empty($parts) && end($parts) !== '..') { |
81
|
1 |
|
array_pop($parts); |
82
|
1 |
|
} elseif ($part === '.' || $part === '' && !empty($parts)) { |
83
|
|
|
continue; |
84
|
1 |
|
} else { |
85
|
|
|
$parts[] = $part; |
86
|
|
|
} |
87
|
1 |
|
} |
88
|
1 |
|
$path = implode($ds, $parts); |
89
|
|
|
return $path === '' ? '.' : $path; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the localized version of a specified file. |
94
|
|
|
* |
95
|
|
|
* The searching is based on the specified language code. In particular, |
96
|
|
|
* a file with the same name will be looked for under the subdirectory |
97
|
|
|
* whose name is the same as the language code. For example, given the file "path/to/view.php" |
98
|
|
|
* and language code "zh-CN", the localized file will be looked for as |
99
|
|
|
* "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a language code that is |
100
|
|
|
* "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned. |
101
|
|
|
* |
102
|
|
|
* If the target and the source language codes are the same, |
103
|
|
|
* the original file will be returned. |
104
|
|
|
* |
105
|
|
|
* @param string $file the original file |
106
|
|
|
* @param string $language the target language that the file should be localized to. |
107
|
|
|
* If not set, the value of [[\yii\base\Application::language]] will be used. |
108
|
|
|
* @param string $sourceLanguage the language that the original file is in. |
109
|
|
|
* If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used. |
110
|
|
|
* @return string the matching localized file, or the original file if the localized version is not found. |
111
|
|
|
* If the target and the source language codes are the same, the original file will be returned. |
112
|
155 |
|
*/ |
113
|
|
|
public static function localize($file, $language = null, $sourceLanguage = null) |
114
|
155 |
|
{ |
115
|
154 |
|
if ($language === null) { |
116
|
|
|
$language = Yii::$app->language; |
117
|
155 |
|
} |
118
|
154 |
|
if ($sourceLanguage === null) { |
119
|
|
|
$sourceLanguage = Yii::$app->sourceLanguage; |
120
|
155 |
|
} |
121
|
155 |
|
if ($language === $sourceLanguage) { |
122
|
|
|
return $file; |
123
|
1 |
|
} |
124
|
1 |
|
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file); |
125
|
1 |
|
if (is_file($desiredFile)) { |
126
|
|
|
return $desiredFile; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$language = substr($language, 0, 2); |
130
|
|
|
if ($language === $sourceLanguage) { |
131
|
|
|
return $file; |
132
|
|
|
} |
133
|
|
|
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file); |
134
|
|
|
|
135
|
|
|
return is_file($desiredFile) ? $desiredFile : $file; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Determines the MIME type of the specified file. |
140
|
|
|
* This method will first try to determine the MIME type based on |
141
|
|
|
* [finfo_open](https://secure.php.net/manual/en/function.finfo-open.php). If the `fileinfo` extension is not installed, |
142
|
|
|
* it will fall back to [[getMimeTypeByExtension()]] when `$checkExtension` is true. |
143
|
|
|
* @param string $file the file name. |
144
|
|
|
* @param string $magicFile name of the optional magic database file (or alias), usually something like `/path/to/magic.mime`. |
145
|
|
|
* This will be passed as the second parameter to [finfo_open()](https://secure.php.net/manual/en/function.finfo-open.php) |
146
|
|
|
* when the `fileinfo` extension is installed. If the MIME type is being determined based via [[getMimeTypeByExtension()]] |
147
|
|
|
* and this is null, it will use the file specified by [[mimeMagicFile]]. |
148
|
|
|
* @param bool $checkExtension whether to use the file extension to determine the MIME type in case |
149
|
|
|
* `finfo_open()` cannot determine it. |
150
|
|
|
* @return string|null the MIME type (e.g. `text/plain`). Null is returned if the MIME type cannot be determined. |
151
|
|
|
* @throws InvalidConfigException when the `fileinfo` PHP extension is not installed and `$checkExtension` is `false`. |
152
|
31 |
|
*/ |
153
|
|
|
public static function getMimeType($file, $magicFile = null, $checkExtension = true) |
154
|
31 |
|
{ |
155
|
|
|
if ($magicFile !== null) { |
156
|
|
|
$magicFile = Yii::getAlias($magicFile); |
157
|
31 |
|
} |
158
|
|
|
if (!extension_loaded('fileinfo')) { |
159
|
|
|
if ($checkExtension) { |
160
|
|
|
return static::getMimeTypeByExtension($file, $magicFile); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
throw new InvalidConfigException('The fileinfo PHP extension is not installed.'); |
164
|
31 |
|
} |
165
|
|
|
$info = finfo_open(FILEINFO_MIME_TYPE, $magicFile); |
|
|
|
|
166
|
31 |
|
|
167
|
31 |
|
if ($info) { |
|
|
|
|
168
|
31 |
|
$result = finfo_file($info, $file); |
169
|
|
|
finfo_close($info); |
170
|
31 |
|
|
171
|
31 |
|
if ($result !== false) { |
172
|
|
|
return $result; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $checkExtension ? static::getMimeTypeByExtension($file, $magicFile) : null; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Determines the MIME type based on the extension name of the specified file. |
181
|
|
|
* This method will use a local map between extension names and MIME types. |
182
|
|
|
* @param string $file the file name. |
183
|
|
|
* @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
184
|
|
|
* If this is not set, the file specified by [[mimeMagicFile]] will be used. |
185
|
|
|
* @return string|null the MIME type. Null is returned if the MIME type cannot be determined. |
186
|
11 |
|
*/ |
187
|
|
|
public static function getMimeTypeByExtension($file, $magicFile = null) |
188
|
11 |
|
{ |
189
|
|
|
$mimeTypes = static::loadMimeTypes($magicFile); |
190
|
11 |
|
|
191
|
11 |
|
if (($ext = pathinfo($file, PATHINFO_EXTENSION)) !== '') { |
192
|
11 |
|
$ext = strtolower($ext); |
|
|
|
|
193
|
11 |
|
if (isset($mimeTypes[$ext])) { |
194
|
|
|
return $mimeTypes[$ext]; |
195
|
|
|
} |
196
|
|
|
} |
197
|
1 |
|
|
198
|
|
|
return null; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Determines the extensions by given MIME type. |
203
|
|
|
* This method will use a local map between extension names and MIME types. |
204
|
|
|
* @param string $mimeType file MIME type. |
205
|
|
|
* @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
206
|
|
|
* If this is not set, the file specified by [[mimeMagicFile]] will be used. |
207
|
|
|
* @return array the extensions corresponding to the specified MIME type |
208
|
13 |
|
*/ |
209
|
|
|
public static function getExtensionsByMimeType($mimeType, $magicFile = null) |
210
|
13 |
|
{ |
211
|
13 |
|
$aliases = static::loadMimeAliases(static::$mimeAliasesFile); |
212
|
|
|
if (isset($aliases[$mimeType])) { |
213
|
|
|
$mimeType = $aliases[$mimeType]; |
214
|
|
|
} |
215
|
13 |
|
|
216
|
13 |
|
$mimeTypes = static::loadMimeTypes($magicFile); |
217
|
|
|
return array_keys($mimeTypes, mb_strtolower($mimeType, 'UTF-8'), true); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
private static $_mimeTypes = []; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Loads MIME types from the specified file. |
224
|
|
|
* @param string $magicFile the path (or alias) of the file that contains all available MIME type information. |
225
|
|
|
* If this is not set, the file specified by [[mimeMagicFile]] will be used. |
226
|
|
|
* @return array the mapping from file extensions to MIME types |
227
|
24 |
|
*/ |
228
|
|
|
protected static function loadMimeTypes($magicFile) |
229
|
24 |
|
{ |
230
|
24 |
|
if ($magicFile === null) { |
|
|
|
|
231
|
|
|
$magicFile = static::$mimeMagicFile; |
232
|
24 |
|
} |
233
|
24 |
|
$magicFile = Yii::getAlias($magicFile); |
234
|
1 |
|
if (!isset(self::$_mimeTypes[$magicFile])) { |
235
|
|
|
self::$_mimeTypes[$magicFile] = require $magicFile; |
236
|
|
|
} |
237
|
24 |
|
|
238
|
|
|
return self::$_mimeTypes[$magicFile]; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
private static $_mimeAliases = []; |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Loads MIME aliases from the specified file. |
245
|
|
|
* @param string $aliasesFile the path (or alias) of the file that contains MIME type aliases. |
246
|
|
|
* If this is not set, the file specified by [[mimeAliasesFile]] will be used. |
247
|
|
|
* @return array the mapping from file extensions to MIME types |
248
|
|
|
* @since 2.0.14 |
249
|
13 |
|
*/ |
250
|
|
|
protected static function loadMimeAliases($aliasesFile) |
251
|
13 |
|
{ |
252
|
|
|
if ($aliasesFile === null) { |
|
|
|
|
253
|
|
|
$aliasesFile = static::$mimeAliasesFile; |
254
|
13 |
|
} |
255
|
13 |
|
$aliasesFile = Yii::getAlias($aliasesFile); |
256
|
1 |
|
if (!isset(self::$_mimeAliases[$aliasesFile])) { |
257
|
|
|
self::$_mimeAliases[$aliasesFile] = require $aliasesFile; |
258
|
|
|
} |
259
|
13 |
|
|
260
|
|
|
return self::$_mimeAliases[$aliasesFile]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Copies a whole directory as another one. |
265
|
|
|
* The files and sub-directories will also be copied over. |
266
|
|
|
* @param string $src the source directory |
267
|
|
|
* @param string $dst the destination directory |
268
|
|
|
* @param array $options options for directory copy. Valid options are: |
269
|
|
|
* |
270
|
|
|
* - dirMode: integer, the permission to be set for newly copied directories. Defaults to 0775. |
271
|
|
|
* - fileMode: integer, the permission to be set for newly copied files. Defaults to the current environment setting. |
272
|
|
|
* - filter: callback, a PHP callback that is called for each directory or file. |
273
|
|
|
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
274
|
|
|
* The callback can return one of the following values: |
275
|
|
|
* |
276
|
|
|
* * true: the directory or file will be copied (the "only" and "except" options will be ignored) |
277
|
|
|
* * false: the directory or file will NOT be copied (the "only" and "except" options will be ignored) |
278
|
|
|
* * null: the "only" and "except" options will determine whether the directory or file should be copied |
279
|
|
|
* |
280
|
|
|
* - only: array, list of patterns that the file paths should match if they want to be copied. |
281
|
|
|
* A path matches a pattern if it contains the pattern string at its end. |
282
|
|
|
* For example, '.php' matches all file paths ending with '.php'. |
283
|
|
|
* Note, the '/' characters in a pattern matches both '/' and '\' in the paths. |
284
|
|
|
* If a file path matches a pattern in both "only" and "except", it will NOT be copied. |
285
|
|
|
* - except: array, list of patterns that the files or directories should match if they want to be excluded from being copied. |
286
|
|
|
* A path matches a pattern if it contains the pattern string at its end. |
287
|
|
|
* Patterns ending with '/' apply to directory paths only, and patterns not ending with '/' |
288
|
|
|
* apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; |
289
|
|
|
* and '.svn/' matches directory paths ending with '.svn'. Note, the '/' characters in a pattern matches |
290
|
|
|
* both '/' and '\' in the paths. |
291
|
|
|
* - caseSensitive: boolean, whether patterns specified at "only" or "except" should be case sensitive. Defaults to true. |
292
|
|
|
* - recursive: boolean, whether the files under the subdirectories should also be copied. Defaults to true. |
293
|
|
|
* - beforeCopy: callback, a PHP callback that is called before copying each sub-directory or file. |
294
|
|
|
* If the callback returns false, the copy operation for the sub-directory or file will be cancelled. |
295
|
|
|
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or |
296
|
|
|
* file to be copied from, while `$to` is the copy target. |
297
|
|
|
* - afterCopy: callback, a PHP callback that is called after each sub-directory or file is successfully copied. |
298
|
|
|
* The signature of the callback should be: `function ($from, $to)`, where `$from` is the sub-directory or |
299
|
|
|
* file copied from, while `$to` is the copy target. |
300
|
|
|
* - copyEmptyDirectories: boolean, whether to copy empty directories. Set this to false to avoid creating directories |
301
|
|
|
* that do not contain files. This affects directories that do not contain files initially as well as directories that |
302
|
|
|
* do not contain files at the target destination because files have been filtered via `only` or `except`. |
303
|
|
|
* Defaults to true. This option is available since version 2.0.12. Before 2.0.12 empty directories are always copied. |
304
|
|
|
* @throws InvalidArgumentException if unable to open directory |
305
|
17 |
|
*/ |
306
|
|
|
public static function copyDirectory($src, $dst, $options = []) |
307
|
17 |
|
{ |
308
|
17 |
|
$src = static::normalizePath($src); |
309
|
|
|
$dst = static::normalizePath($dst); |
310
|
17 |
|
|
311
|
2 |
|
if ($src === $dst || strpos($dst, $src . DIRECTORY_SEPARATOR) === 0) { |
312
|
|
|
throw new InvalidArgumentException('Trying to copy a directory to itself or a subdirectory.'); |
313
|
15 |
|
} |
314
|
15 |
|
$dstExists = is_dir($dst); |
315
|
6 |
|
if (!$dstExists && (!isset($options['copyEmptyDirectories']) || $options['copyEmptyDirectories'])) { |
316
|
6 |
|
static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true); |
317
|
|
|
$dstExists = true; |
318
|
|
|
} |
319
|
15 |
|
|
320
|
15 |
|
$handle = opendir($src); |
321
|
|
|
if ($handle === false) { |
322
|
|
|
throw new InvalidArgumentException("Unable to open directory: $src"); |
323
|
15 |
|
} |
324
|
|
|
if (!isset($options['basePath'])) { |
325
|
15 |
|
// this should be done only once |
326
|
15 |
|
$options['basePath'] = realpath($src); |
327
|
|
|
$options = static::normalizeOptions($options); |
328
|
15 |
|
} |
329
|
15 |
|
while (($file = readdir($handle)) !== false) { |
330
|
15 |
|
if ($file === '.' || $file === '..') { |
331
|
|
|
continue; |
332
|
13 |
|
} |
333
|
13 |
|
$from = $src . DIRECTORY_SEPARATOR . $file; |
334
|
13 |
|
$to = $dst . DIRECTORY_SEPARATOR . $file; |
335
|
13 |
|
if (static::filterPath($from, $options)) { |
336
|
2 |
|
if (isset($options['beforeCopy']) && !call_user_func($options['beforeCopy'], $from, $to)) { |
337
|
|
|
continue; |
338
|
11 |
|
} |
339
|
11 |
|
if (is_file($from)) { |
340
|
|
|
if (!$dstExists) { |
341
|
5 |
|
// delay creation of destination directory until the first file is copied to avoid creating empty directories |
342
|
5 |
|
static::createDirectory($dst, isset($options['dirMode']) ? $options['dirMode'] : 0775, true); |
343
|
|
|
$dstExists = true; |
344
|
11 |
|
} |
345
|
11 |
|
copy($from, $to); |
346
|
11 |
|
if (isset($options['fileMode'])) { |
347
|
|
|
@chmod($to, $options['fileMode']); |
|
|
|
|
348
|
|
|
} |
349
|
|
|
} else { |
350
|
8 |
|
// recursive copy, defaults to true |
351
|
7 |
|
if (!isset($options['recursive']) || $options['recursive']) { |
352
|
|
|
static::copyDirectory($from, $to, $options); |
353
|
|
|
} |
354
|
11 |
|
} |
355
|
|
|
if (isset($options['afterCopy'])) { |
356
|
|
|
call_user_func($options['afterCopy'], $from, $to); |
357
|
|
|
} |
358
|
|
|
} |
359
|
15 |
|
} |
360
|
15 |
|
closedir($handle); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Removes a directory (and all its content) recursively. |
365
|
|
|
* |
366
|
|
|
* @param string $dir the directory to be deleted recursively. |
367
|
|
|
* @param array $options options for directory remove. Valid options are: |
368
|
|
|
* |
369
|
|
|
* - traverseSymlinks: boolean, whether symlinks to the directories should be traversed too. |
370
|
|
|
* Defaults to `false`, meaning the content of the symlinked directory would not be deleted. |
371
|
|
|
* Only symlink would be removed in that default case. |
372
|
|
|
* |
373
|
|
|
* @throws ErrorException in case of failure |
374
|
222 |
|
*/ |
375
|
|
|
public static function removeDirectory($dir, $options = []) |
376
|
222 |
|
{ |
377
|
107 |
|
if (!is_dir($dir)) { |
378
|
|
|
return; |
379
|
220 |
|
} |
380
|
220 |
|
if (!empty($options['traverseSymlinks']) || !is_link($dir)) { |
381
|
|
|
if (!($handle = opendir($dir))) { |
382
|
|
|
return; |
383
|
220 |
|
} |
384
|
220 |
|
while (($file = readdir($handle)) !== false) { |
385
|
220 |
|
if ($file === '.' || $file === '..') { |
386
|
|
|
continue; |
387
|
198 |
|
} |
388
|
198 |
|
$path = $dir . DIRECTORY_SEPARATOR . $file; |
389
|
76 |
|
if (is_dir($path)) { |
390
|
|
|
static::removeDirectory($path, $options); |
391
|
176 |
|
} else { |
392
|
|
|
static::unlink($path); |
393
|
|
|
} |
394
|
220 |
|
} |
395
|
|
|
closedir($handle); |
396
|
220 |
|
} |
397
|
2 |
|
if (is_link($dir)) { |
398
|
|
|
static::unlink($dir); |
399
|
220 |
|
} else { |
400
|
|
|
rmdir($dir); |
401
|
220 |
|
} |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Removes a file or symlink in a cross-platform way |
406
|
|
|
* |
407
|
|
|
* @param string $path |
408
|
|
|
* @return bool |
409
|
|
|
* |
410
|
|
|
* @since 2.0.14 |
411
|
177 |
|
*/ |
412
|
|
|
public static function unlink($path) |
413
|
177 |
|
{ |
414
|
|
|
$isWindows = DIRECTORY_SEPARATOR === '\\'; |
415
|
177 |
|
|
416
|
177 |
|
if (!$isWindows) { |
417
|
|
|
return unlink($path); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
if (is_link($path) && is_dir($path)) { |
421
|
|
|
return rmdir($path); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
try { |
425
|
|
|
return unlink($path); |
426
|
|
|
} catch (ErrorException $e) { |
427
|
|
|
// last resort measure for Windows |
428
|
|
|
if (is_dir($path) && count(static::findFiles($path)) !== 0) { |
429
|
|
|
return false; |
430
|
|
|
} |
431
|
|
|
if (function_exists('exec') && file_exists($path)) { |
432
|
|
|
exec('DEL /F/Q ' . escapeshellarg($path)); |
433
|
|
|
|
434
|
|
|
return !file_exists($path); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
return false; |
438
|
|
|
} |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Returns the files found under the specified directory and subdirectories. |
443
|
|
|
* @param string $dir the directory under which the files will be looked for. |
444
|
|
|
* @param array $options options for file searching. Valid options are: |
445
|
|
|
* |
446
|
|
|
* - `filter`: callback, a PHP callback that is called for each directory or file. |
447
|
|
|
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
448
|
|
|
* The callback can return one of the following values: |
449
|
|
|
* |
450
|
|
|
* * `true`: the directory or file will be returned (the `only` and `except` options will be ignored) |
451
|
|
|
* * `false`: the directory or file will NOT be returned (the `only` and `except` options will be ignored) |
452
|
|
|
* * `null`: the `only` and `except` options will determine whether the directory or file should be returned |
453
|
|
|
* |
454
|
|
|
* - `except`: array, list of patterns excluding from the results matching file or directory paths. |
455
|
|
|
* Patterns ending with slash ('/') apply to directory paths only, and patterns not ending with '/' |
456
|
|
|
* apply to file paths only. For example, '/a/b' matches all file paths ending with '/a/b'; |
457
|
|
|
* and `.svn/` matches directory paths ending with `.svn`. |
458
|
|
|
* If the pattern does not contain a slash (`/`), it is treated as a shell glob pattern |
459
|
|
|
* and checked for a match against the pathname relative to `$dir`. |
460
|
|
|
* Otherwise, the pattern is treated as a shell glob suitable for consumption by `fnmatch(3)` |
461
|
|
|
* with the `FNM_PATHNAME` flag: wildcards in the pattern will not match a `/` in the pathname. |
462
|
|
|
* For example, `views/*.php` matches `views/index.php` but not `views/controller/index.php`. |
463
|
|
|
* A leading slash matches the beginning of the pathname. For example, `/*.php` matches `index.php` but not `views/start/index.php`. |
464
|
|
|
* An optional prefix `!` which negates the pattern; any matching file excluded by a previous pattern will become included again. |
465
|
|
|
* If a negated pattern matches, this will override lower precedence patterns sources. Put a backslash (`\`) in front of the first `!` |
466
|
|
|
* for patterns that begin with a literal `!`, for example, `\!important!.txt`. |
467
|
|
|
* Note, the '/' characters in a pattern matches both '/' and '\' in the paths. |
468
|
|
|
* - `only`: array, list of patterns that the file paths should match if they are to be returned. Directory paths |
469
|
|
|
* are not checked against them. Same pattern matching rules as in the `except` option are used. |
470
|
|
|
* If a file path matches a pattern in both `only` and `except`, it will NOT be returned. |
471
|
|
|
* - `caseSensitive`: boolean, whether patterns specified at `only` or `except` should be case sensitive. Defaults to `true`. |
472
|
|
|
* - `recursive`: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`. |
473
|
|
|
* @return array files found under the directory, in no particular order. Ordering depends on the files system used. |
474
|
|
|
* @throws InvalidArgumentException if the dir is invalid. |
475
|
166 |
|
*/ |
476
|
|
|
public static function findFiles($dir, $options = []) |
477
|
166 |
|
{ |
478
|
166 |
|
$dir = self::clearDir($dir); |
479
|
166 |
|
$options = self::setBasePath($dir, $options); |
480
|
166 |
|
$list = []; |
481
|
166 |
|
$handle = self::openDir($dir); |
482
|
166 |
|
while (($file = readdir($handle)) !== false) { |
483
|
166 |
|
if ($file === '.' || $file === '..') { |
484
|
|
|
continue; |
485
|
163 |
|
} |
486
|
163 |
|
$path = $dir . DIRECTORY_SEPARATOR . $file; |
487
|
163 |
|
if (static::filterPath($path, $options)) { |
488
|
161 |
|
if (is_file($path)) { |
489
|
25 |
|
$list[] = $path; |
490
|
24 |
|
} elseif (is_dir($path) && (!isset($options['recursive']) || $options['recursive'])) { |
491
|
|
|
$list = array_merge($list, static::findFiles($path, $options)); |
492
|
|
|
} |
493
|
|
|
} |
494
|
166 |
|
} |
495
|
|
|
closedir($handle); |
496
|
166 |
|
|
497
|
|
|
return $list; |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Returns the directories found under the specified directory and subdirectories. |
502
|
|
|
* @param string $dir the directory under which the files will be looked for. |
503
|
|
|
* @param array $options options for directory searching. Valid options are: |
504
|
|
|
* |
505
|
|
|
* - `filter`: callback, a PHP callback that is called for each directory or file. |
506
|
|
|
* The signature of the callback should be: `function ($path)`, where `$path` refers the full path to be filtered. |
507
|
|
|
* The callback can return one of the following values: |
508
|
|
|
* |
509
|
|
|
* * `true`: the directory will be returned |
510
|
|
|
* * `false`: the directory will NOT be returned |
511
|
|
|
* |
512
|
|
|
* - `recursive`: boolean, whether the files under the subdirectories should also be looked for. Defaults to `true`. |
513
|
|
|
* @return array directories found under the directory, in no particular order. Ordering depends on the files system used. |
514
|
|
|
* @throws InvalidArgumentException if the dir is invalid. |
515
|
|
|
* @since 2.0.14 |
516
|
1 |
|
*/ |
517
|
|
|
public static function findDirectories($dir, $options = []) |
518
|
1 |
|
{ |
519
|
1 |
|
$dir = self::clearDir($dir); |
520
|
1 |
|
$options = self::setBasePath($dir, $options); |
521
|
1 |
|
$list = []; |
522
|
1 |
|
$handle = self::openDir($dir); |
523
|
1 |
|
while (($file = readdir($handle)) !== false) { |
524
|
1 |
|
if ($file === '.' || $file === '..') { |
525
|
|
|
continue; |
526
|
1 |
|
} |
527
|
1 |
|
$path = $dir . DIRECTORY_SEPARATOR . $file; |
528
|
1 |
|
if (is_dir($path) && static::filterPath($path, $options)) { |
529
|
1 |
|
$list[] = $path; |
530
|
1 |
|
if (!isset($options['recursive']) || $options['recursive']) { |
531
|
|
|
$list = array_merge($list, static::findDirectories($path, $options)); |
532
|
|
|
} |
533
|
|
|
} |
534
|
1 |
|
} |
535
|
|
|
closedir($handle); |
536
|
1 |
|
|
537
|
|
|
return $list; |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* @param string $dir |
542
|
167 |
|
*/ |
543
|
|
|
private static function setBasePath($dir, $options) |
544
|
167 |
|
{ |
545
|
|
|
if (!isset($options['basePath'])) { |
546
|
167 |
|
// this should be done only once |
547
|
167 |
|
$options['basePath'] = realpath($dir); |
548
|
|
|
$options = static::normalizeOptions($options); |
549
|
|
|
} |
550
|
167 |
|
|
551
|
|
|
return $options; |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* @param string $dir |
556
|
167 |
|
*/ |
557
|
|
|
private static function openDir($dir) |
558
|
167 |
|
{ |
559
|
167 |
|
$handle = opendir($dir); |
560
|
|
|
if ($handle === false) { |
561
|
|
|
throw new InvalidArgumentException("Unable to open directory: $dir"); |
562
|
167 |
|
} |
563
|
|
|
return $handle; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* @param string $dir |
568
|
167 |
|
*/ |
569
|
|
|
private static function clearDir($dir) |
570
|
167 |
|
{ |
571
|
|
|
if (!is_dir($dir)) { |
572
|
|
|
throw new InvalidArgumentException("The dir argument must be a directory: $dir"); |
573
|
167 |
|
} |
574
|
|
|
return rtrim($dir, DIRECTORY_SEPARATOR); |
575
|
|
|
} |
576
|
|
|
|
577
|
|
|
/** |
578
|
|
|
* Checks if the given file path satisfies the filtering options. |
579
|
|
|
* @param string $path the path of the file or directory to be checked |
580
|
|
|
* @param array $options the filtering options. See [[findFiles()]] for explanations of |
581
|
|
|
* the supported options. |
582
|
|
|
* @return bool whether the file or directory satisfies the filtering options. |
583
|
173 |
|
*/ |
584
|
|
|
public static function filterPath($path, $options) |
585
|
173 |
|
{ |
586
|
2 |
|
if (isset($options['filter'])) { |
587
|
2 |
|
$result = call_user_func($options['filter'], $path); |
588
|
2 |
|
if (is_bool($result)) { |
589
|
|
|
return $result; |
590
|
|
|
} |
591
|
|
|
} |
592
|
172 |
|
|
593
|
105 |
|
if (empty($options['except']) && empty($options['only'])) { |
594
|
|
|
return true; |
595
|
|
|
} |
596
|
76 |
|
|
597
|
|
|
$path = str_replace('\\', '/', $path); |
598
|
76 |
|
|
599
|
56 |
|
if (!empty($options['except'])) { |
600
|
3 |
|
if (($except = self::lastExcludeMatchingFromList($options['basePath'], $path, $options['except'])) !== null) { |
601
|
|
|
return $except['flags'] & self::PATTERN_NEGATIVE; |
|
|
|
|
602
|
|
|
} |
603
|
|
|
} |
604
|
76 |
|
|
605
|
74 |
|
if (!empty($options['only']) && !is_dir($path)) { |
606
|
|
|
if (($except = self::lastExcludeMatchingFromList($options['basePath'], $path, $options['only'])) !== null) { |
|
|
|
|
607
|
72 |
|
// don't check PATTERN_NEGATIVE since those entries are not prefixed with ! |
608
|
|
|
return true; |
609
|
|
|
} |
610
|
20 |
|
|
611
|
|
|
return false; |
612
|
|
|
} |
613
|
20 |
|
|
614
|
|
|
return true; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Creates a new directory. |
619
|
|
|
* |
620
|
|
|
* This method is similar to the PHP `mkdir()` function except that |
621
|
|
|
* it uses `chmod()` to set the permission of the created directory |
622
|
|
|
* in order to avoid the impact of the `umask` setting. |
623
|
|
|
* |
624
|
|
|
* @param string $path path of the directory to be created. |
625
|
|
|
* @param int $mode the permission to be set for the created directory. |
626
|
|
|
* @param bool $recursive whether to create parent directories if they do not exist. |
627
|
|
|
* @return bool whether the directory is created successfully |
628
|
|
|
* @throws \yii\base\Exception if the directory could not be created (i.e. php error due to parallel changes) |
629
|
271 |
|
*/ |
630
|
|
|
public static function createDirectory($path, $mode = 0775, $recursive = true) |
631
|
271 |
|
{ |
632
|
160 |
|
if (is_dir($path)) { |
633
|
|
|
return true; |
634
|
251 |
|
} |
635
|
|
|
$parentDir = dirname($path); |
636
|
251 |
|
// recurse if parent dir does not exist and we are not at the root of the file system. |
637
|
5 |
|
if ($recursive && !is_dir($parentDir) && $parentDir !== $path) { |
638
|
|
|
static::createDirectory($parentDir, $mode, true); |
639
|
|
|
} |
640
|
251 |
|
try { |
641
|
251 |
|
if (!mkdir($path, $mode)) { |
642
|
|
|
return false; |
643
|
|
|
} |
644
|
|
|
} catch (\Exception $e) { |
645
|
|
|
if (!is_dir($path)) {// https://github.com/yiisoft/yii2/issues/9288 |
646
|
|
|
throw new \yii\base\Exception("Failed to create directory \"$path\": " . $e->getMessage(), $e->getCode(), $e); |
647
|
|
|
} |
648
|
|
|
} |
649
|
251 |
|
try { |
650
|
|
|
return chmod($path, $mode); |
651
|
|
|
} catch (\Exception $e) { |
652
|
|
|
throw new \yii\base\Exception("Failed to change permissions for directory \"$path\": " . $e->getMessage(), $e->getCode(), $e); |
653
|
|
|
} |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* Performs a simple comparison of file or directory names. |
658
|
|
|
* |
659
|
|
|
* Based on match_basename() from dir.c of git 1.8.5.3 sources. |
660
|
|
|
* |
661
|
|
|
* @param string $baseName file or directory name to compare with the pattern |
662
|
|
|
* @param string $pattern the pattern that $baseName will be compared against |
663
|
|
|
* @param int|bool $firstWildcard location of first wildcard character in the $pattern |
664
|
|
|
* @param int $flags pattern flags |
665
|
|
|
* @return bool whether the name matches against pattern |
666
|
75 |
|
*/ |
667
|
|
|
private static function matchBasename($baseName, $pattern, $firstWildcard, $flags) |
668
|
75 |
|
{ |
669
|
64 |
|
if ($firstWildcard === false) { |
670
|
64 |
|
if ($pattern === $baseName) { |
671
|
|
|
return true; |
672
|
64 |
|
} |
673
|
|
|
} elseif ($flags & self::PATTERN_ENDSWITH) { |
674
|
63 |
|
/* "*literal" matching against "fooliteral" */ |
675
|
63 |
|
$n = StringHelper::byteLength($pattern); |
676
|
|
|
if (StringHelper::byteSubstr($pattern, 1, $n) === StringHelper::byteSubstr($baseName, -$n, $n)) { |
677
|
|
|
return true; |
678
|
|
|
} |
679
|
|
|
} |
680
|
75 |
|
|
681
|
75 |
|
$matchOptions = []; |
682
|
1 |
|
if ($flags & self::PATTERN_CASE_INSENSITIVE) { |
683
|
|
|
$matchOptions['caseSensitive'] = false; |
684
|
|
|
} |
685
|
75 |
|
|
686
|
|
|
return StringHelper::matchWildcard($pattern, $baseName, $matchOptions); |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* Compares a path part against a pattern with optional wildcards. |
691
|
|
|
* |
692
|
|
|
* Based on match_pathname() from dir.c of git 1.8.5.3 sources. |
693
|
|
|
* |
694
|
|
|
* @param string $path full path to compare |
695
|
|
|
* @param string $basePath base of path that will not be compared |
696
|
|
|
* @param string $pattern the pattern that path part will be compared against |
697
|
|
|
* @param int|bool $firstWildcard location of first wildcard character in the $pattern |
698
|
|
|
* @param int $flags pattern flags |
699
|
|
|
* @return bool whether the path part matches against pattern |
700
|
57 |
|
*/ |
701
|
|
|
private static function matchPathname($path, $basePath, $pattern, $firstWildcard, $flags) |
702
|
|
|
{ |
703
|
57 |
|
// match with FNM_PATHNAME; the pattern has base implicitly in front of it. |
704
|
54 |
|
if (strpos($pattern, '/') === 0) { |
705
|
54 |
|
$pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern)); |
706
|
|
|
if ($firstWildcard !== false && $firstWildcard !== 0) { |
707
|
|
|
$firstWildcard--; |
708
|
|
|
} |
709
|
|
|
} |
710
|
57 |
|
|
711
|
57 |
|
$namelen = StringHelper::byteLength($path) - (empty($basePath) ? 0 : StringHelper::byteLength($basePath) + 1); |
712
|
|
|
$name = StringHelper::byteSubstr($path, -$namelen, $namelen); |
713
|
57 |
|
|
714
|
57 |
|
if ($firstWildcard !== 0) { |
715
|
56 |
|
if ($firstWildcard === false) { |
716
|
|
|
$firstWildcard = StringHelper::byteLength($pattern); |
717
|
|
|
} |
718
|
57 |
|
// if the non-wildcard part is longer than the remaining pathname, surely it cannot match. |
719
|
2 |
|
if ($firstWildcard > $namelen) { |
720
|
|
|
return false; |
721
|
|
|
} |
722
|
57 |
|
|
723
|
57 |
|
if (strncmp($pattern, $name, $firstWildcard)) { |
|
|
|
|
724
|
|
|
return false; |
725
|
4 |
|
} |
726
|
4 |
|
$pattern = StringHelper::byteSubstr($pattern, $firstWildcard, StringHelper::byteLength($pattern)); |
|
|
|
|
727
|
|
|
$name = StringHelper::byteSubstr($name, $firstWildcard, $namelen); |
728
|
|
|
|
729
|
4 |
|
// If the whole pattern did not have a wildcard, then our prefix match is all we need; we do not need to call fnmatch at all. |
730
|
3 |
|
if (empty($pattern) && empty($name)) { |
731
|
|
|
return true; |
732
|
|
|
} |
733
|
|
|
} |
734
|
|
|
|
735
|
2 |
|
$matchOptions = [ |
736
|
|
|
'filePath' => true |
737
|
2 |
|
]; |
738
|
|
|
if ($flags & self::PATTERN_CASE_INSENSITIVE) { |
739
|
|
|
$matchOptions['caseSensitive'] = false; |
740
|
|
|
} |
741
|
2 |
|
|
742
|
|
|
return StringHelper::matchWildcard($pattern, $name, $matchOptions); |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
/** |
746
|
|
|
* Scan the given exclude list in reverse to see whether pathname |
747
|
|
|
* should be ignored. The first match (i.e. the last on the list), if |
748
|
|
|
* any, determines the fate. Returns the element which |
749
|
|
|
* matched, or null for undecided. |
750
|
|
|
* |
751
|
|
|
* Based on last_exclude_matching_from_list() from dir.c of git 1.8.5.3 sources. |
752
|
|
|
* |
753
|
|
|
* @param string $basePath |
754
|
|
|
* @param string $path |
755
|
|
|
* @param array $excludes list of patterns to match $path against |
756
|
|
|
* @return array|null null or one of $excludes item as an array with keys: 'pattern', 'flags' |
757
|
|
|
* @throws InvalidArgumentException if any of the exclude patterns is not a string or an array with keys: pattern, flags, firstWildcard. |
758
|
76 |
|
*/ |
759
|
|
|
private static function lastExcludeMatchingFromList($basePath, $path, $excludes) |
760
|
76 |
|
{ |
761
|
76 |
|
foreach (array_reverse($excludes) as $exclude) { |
762
|
|
|
if (is_string($exclude)) { |
763
|
|
|
$exclude = self::parseExcludePattern($exclude, false); |
764
|
76 |
|
} |
765
|
|
|
if (!isset($exclude['pattern']) || !isset($exclude['flags']) || !isset($exclude['firstWildcard'])) { |
766
|
|
|
throw new InvalidArgumentException('If exclude/include pattern is an array it must contain the pattern, flags and firstWildcard keys.'); |
767
|
76 |
|
} |
768
|
|
|
if ($exclude['flags'] & self::PATTERN_MUSTBEDIR && !is_dir($path)) { |
769
|
|
|
continue; |
770
|
|
|
} |
771
|
76 |
|
|
772
|
75 |
|
if ($exclude['flags'] & self::PATTERN_NODIR) { |
773
|
73 |
|
if (self::matchBasename(basename($path), $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) { |
774
|
|
|
return $exclude; |
775
|
74 |
|
} |
776
|
|
|
continue; |
777
|
|
|
} |
778
|
57 |
|
|
779
|
57 |
|
if (self::matchPathname($path, $basePath, $exclude['pattern'], $exclude['firstWildcard'], $exclude['flags'])) { |
780
|
|
|
return $exclude; |
781
|
|
|
} |
782
|
|
|
} |
783
|
75 |
|
|
784
|
|
|
return null; |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* Processes the pattern, stripping special characters like / and ! from the beginning and settings flags instead. |
789
|
|
|
* @param string $pattern |
790
|
|
|
* @param bool $caseSensitive |
791
|
|
|
* @return array with keys: (string) pattern, (int) flags, (int|bool) firstWildcard |
792
|
|
|
* @throws InvalidArgumentException |
793
|
82 |
|
*/ |
794
|
|
|
private static function parseExcludePattern($pattern, $caseSensitive) |
795
|
82 |
|
{ |
796
|
|
|
if (!is_string($pattern)) { |
|
|
|
|
797
|
|
|
throw new InvalidArgumentException('Exclude/include pattern must be a string.'); |
798
|
|
|
} |
799
|
|
|
|
800
|
82 |
|
$result = [ |
801
|
82 |
|
'pattern' => $pattern, |
802
|
|
|
'flags' => 0, |
803
|
|
|
'firstWildcard' => false, |
804
|
|
|
]; |
805
|
82 |
|
|
806
|
1 |
|
if (!$caseSensitive) { |
807
|
|
|
$result['flags'] |= self::PATTERN_CASE_INSENSITIVE; |
808
|
|
|
} |
809
|
82 |
|
|
810
|
|
|
if (empty($pattern)) { |
811
|
|
|
return $result; |
812
|
|
|
} |
813
|
82 |
|
|
814
|
|
|
if (strpos($pattern, '!') === 0) { |
815
|
|
|
$result['flags'] |= self::PATTERN_NEGATIVE; |
816
|
|
|
$pattern = StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern)); |
817
|
82 |
|
} |
818
|
|
|
if (StringHelper::byteLength($pattern) && StringHelper::byteSubstr($pattern, -1, 1) === '/') { |
819
|
|
|
$pattern = StringHelper::byteSubstr($pattern, 0, -1); |
820
|
|
|
$result['flags'] |= self::PATTERN_MUSTBEDIR; |
821
|
82 |
|
} |
822
|
81 |
|
if (strpos($pattern, '/') === false) { |
823
|
|
|
$result['flags'] |= self::PATTERN_NODIR; |
824
|
82 |
|
} |
825
|
82 |
|
$result['firstWildcard'] = self::firstWildcardInPattern($pattern); |
826
|
69 |
|
if (strpos($pattern, '*') === 0 && self::firstWildcardInPattern(StringHelper::byteSubstr($pattern, 1, StringHelper::byteLength($pattern))) === false) { |
827
|
|
|
$result['flags'] |= self::PATTERN_ENDSWITH; |
828
|
82 |
|
} |
829
|
|
|
$result['pattern'] = $pattern; |
830
|
82 |
|
|
831
|
|
|
return $result; |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
/** |
835
|
|
|
* Searches for the first wildcard character in the pattern. |
836
|
|
|
* @param string $pattern the pattern to search in |
837
|
|
|
* @return int|bool position of first wildcard character or false if not found |
838
|
82 |
|
*/ |
839
|
|
|
private static function firstWildcardInPattern($pattern) |
840
|
82 |
|
{ |
841
|
82 |
|
$wildcards = ['*', '?', '[', '\\']; |
842
|
82 |
|
$wildcardSearch = function ($r, $c) use ($pattern) { |
843
|
|
|
$p = strpos($pattern, $c); |
844
|
82 |
|
|
845
|
82 |
|
return $r === false ? $p : ($p === false ? $r : min($r, $p)); |
846
|
|
|
}; |
847
|
82 |
|
|
848
|
|
|
return array_reduce($wildcards, $wildcardSearch, false); |
849
|
|
|
} |
850
|
|
|
|
851
|
|
|
/** |
852
|
|
|
* @param array $options raw options |
853
|
|
|
* @return array normalized options |
854
|
|
|
* @since 2.0.12 |
855
|
178 |
|
*/ |
856
|
|
|
protected static function normalizeOptions(array $options) |
857
|
178 |
|
{ |
858
|
177 |
|
if (!array_key_exists('caseSensitive', $options)) { |
859
|
|
|
$options['caseSensitive'] = true; |
860
|
178 |
|
} |
861
|
62 |
|
if (isset($options['except'])) { |
862
|
62 |
|
foreach ($options['except'] as $key => $value) { |
863
|
62 |
|
if (is_string($value)) { |
864
|
|
|
$options['except'][$key] = self::parseExcludePattern($value, $options['caseSensitive']); |
865
|
|
|
} |
866
|
|
|
} |
867
|
178 |
|
} |
868
|
80 |
|
if (isset($options['only'])) { |
869
|
80 |
|
foreach ($options['only'] as $key => $value) { |
870
|
80 |
|
if (is_string($value)) { |
871
|
|
|
$options['only'][$key] = self::parseExcludePattern($value, $options['caseSensitive']); |
872
|
|
|
} |
873
|
|
|
} |
874
|
|
|
} |
875
|
178 |
|
|
876
|
|
|
return $options; |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
/** |
880
|
|
|
* Changes the Unix user and/or group ownership of a file or directory, and optionally the mode. |
881
|
|
|
* Note: This function will not work on remote files as the file to be examined must be accessible |
882
|
|
|
* via the server's filesystem. |
883
|
|
|
* Note: On Windows, this function fails silently when applied on a regular file. |
884
|
|
|
* @param string $path the path to the file or directory. |
885
|
|
|
* @param string|array|int|null $ownership the user and/or group ownership for the file or directory. |
886
|
|
|
* When $ownership is a string, the format is 'user:group' where both are optional. E.g. |
887
|
|
|
* 'user' or 'user:' will only change the user, |
888
|
|
|
* ':group' will only change the group, |
889
|
|
|
* 'user:group' will change both. |
890
|
|
|
* When $owners is an index array the format is [0 => user, 1 => group], e.g. `[$myUser, $myGroup]`. |
891
|
|
|
* It is also possible to pass an associative array, e.g. ['user' => $myUser, 'group' => $myGroup]. |
892
|
|
|
* In case $owners is an integer it will be used as user id. |
893
|
|
|
* If `null`, an empty array or an empty string is passed, the ownership will not be changed. |
894
|
|
|
* @param int|null $mode the permission to be set for the file or directory. |
895
|
|
|
* If `null` is passed, the mode will not be changed. |
896
|
|
|
* |
897
|
|
|
* @since 2.0.43 |
898
|
|
|
*/ |
899
|
|
|
public static function changeOwnership($path, $ownership, $mode = null) |
900
|
|
|
{ |
901
|
|
|
if (!file_exists($path)) { |
902
|
|
|
throw new InvalidArgumentException('Unable to change ownerhip, "' . $path . '" is not a file or directory.'); |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
if (empty($ownership) && $ownership !== 0 && $mode === null) { |
906
|
|
|
return; |
907
|
|
|
} |
908
|
|
|
|
909
|
|
|
$user = $group = null; |
910
|
|
|
if (!empty($ownership) || $ownership === 0 || $ownership === '0') { |
911
|
|
|
if (is_int($ownership)) { |
912
|
|
|
$user = $ownership; |
913
|
|
|
} elseif (is_string($ownership)) { |
914
|
|
|
$ownerParts = explode(':', $ownership); |
915
|
|
|
$user = $ownerParts[0]; |
916
|
|
|
if (count($ownerParts) > 1) { |
917
|
|
|
$group = $ownerParts[1]; |
918
|
|
|
} |
919
|
|
|
} elseif (is_array($ownership)) { |
|
|
|
|
920
|
|
|
$ownershipIsIndexed = ArrayHelper::isIndexed($ownership); |
921
|
|
|
$user = ArrayHelper::getValue($ownership, $ownershipIsIndexed ? 0 : 'user'); |
922
|
|
|
$group = ArrayHelper::getValue($ownership, $ownershipIsIndexed ? 1 : 'group'); |
923
|
|
|
} else { |
924
|
|
|
throw new InvalidArgumentException('$ownership must be an integer, string, array, or null.'); |
925
|
|
|
} |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
if ($mode !== null) { |
929
|
|
|
if (!is_int($mode)) { |
|
|
|
|
930
|
|
|
throw new InvalidArgumentException('$mode must be an integer or null.'); |
931
|
|
|
} |
932
|
|
|
if (!chmod($path, $mode)) { |
933
|
|
|
throw new Exception('Unable to change mode of "' . $path . '" to "0' . decoct($mode) . '".'); |
934
|
|
|
} |
935
|
|
|
} |
936
|
|
|
if ($user !== null && $user !== '') { |
937
|
|
|
if (is_numeric($user)) { |
938
|
|
|
$user = (int) $user; |
939
|
|
|
} elseif (!is_string($user)) { |
940
|
|
|
throw new InvalidArgumentException('The user part of $ownership must be an integer, string, or null.'); |
941
|
|
|
} |
942
|
|
|
if (!chown($path, $user)) { |
943
|
|
|
throw new Exception('Unable to change user ownership of "' . $path . '" to "' . $user . '".'); |
944
|
|
|
} |
945
|
|
|
} |
946
|
|
|
if ($group !== null && $group !== '') { |
947
|
|
|
if (is_numeric($group)) { |
948
|
|
|
$group = (int) $group; |
949
|
|
|
} elseif (!is_string($group)) { |
950
|
|
|
throw new InvalidArgumentException('The group part of $ownership must be an integer, string or null.'); |
951
|
|
|
} |
952
|
|
|
if (!chgrp($path, $group)) { |
953
|
|
|
throw new Exception('Unable to change group ownership of "' . $path . '" to "' . $group . '".'); |
954
|
|
|
} |
955
|
|
|
} |
956
|
|
|
} |
957
|
|
|
} |
958
|
|
|
|