author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress Direct Filesystem. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Filesystem |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* WordPress Filesystem Class for direct PHP file and folder manipulation. |
|
11 |
* |
|
5 | 12 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @see WP_Filesystem_Base |
0 | 15 |
*/ |
16 |
class WP_Filesystem_Direct extends WP_Filesystem_Base { |
|
17 |
||
18 |
/** |
|
9 | 19 |
* Constructor. |
0 | 20 |
* |
9 | 21 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* |
9 | 23 |
* @param mixed $arg Not used. |
0 | 24 |
*/ |
9 | 25 |
public function __construct( $arg ) { |
0 | 26 |
$this->method = 'direct'; |
27 |
$this->errors = new WP_Error(); |
|
28 |
} |
|
29 |
||
30 |
/** |
|
9 | 31 |
* Reads entire file into a string. |
0 | 32 |
* |
9 | 33 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
34 |
* |
0 | 35 |
* @param string $file Name of the file to read. |
9 | 36 |
* @return string|false Read data on success, false on failure. |
0 | 37 |
*/ |
9 | 38 |
public function get_contents( $file ) { |
39 |
return @file_get_contents( $file ); |
|
0 | 40 |
} |
41 |
||
42 |
/** |
|
9 | 43 |
* Reads entire file into an array. |
0 | 44 |
* |
9 | 45 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* |
0 | 47 |
* @param string $file Path to the file. |
9 | 48 |
* @return array|false File contents in an array on success, false on failure. |
0 | 49 |
*/ |
9 | 50 |
public function get_contents_array( $file ) { |
51 |
return @file( $file ); |
|
0 | 52 |
} |
53 |
||
54 |
/** |
|
9 | 55 |
* Writes a string to a file. |
0 | 56 |
* |
9 | 57 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* |
9 | 59 |
* @param string $file Remote path to the file where to write the data. |
60 |
* @param string $contents The data to write. |
|
61 |
* @param int|false $mode Optional. The file permissions as octal number, usually 0644. |
|
62 |
* Default false. |
|
63 |
* @return bool True on success, false on failure. |
|
0 | 64 |
*/ |
5 | 65 |
public function put_contents( $file, $contents, $mode = false ) { |
0 | 66 |
$fp = @fopen( $file, 'wb' ); |
16 | 67 |
|
9 | 68 |
if ( ! $fp ) { |
0 | 69 |
return false; |
9 | 70 |
} |
0 | 71 |
|
72 |
mbstring_binary_safe_encoding(); |
|
73 |
||
74 |
$data_length = strlen( $contents ); |
|
75 |
||
76 |
$bytes_written = fwrite( $fp, $contents ); |
|
77 |
||
78 |
reset_mbstring_encoding(); |
|
79 |
||
80 |
fclose( $fp ); |
|
81 |
||
9 | 82 |
if ( $data_length !== $bytes_written ) { |
0 | 83 |
return false; |
9 | 84 |
} |
0 | 85 |
|
86 |
$this->chmod( $file, $mode ); |
|
87 |
||
88 |
return true; |
|
89 |
} |
|
90 |
||
91 |
/** |
|
9 | 92 |
* Gets the current working directory. |
0 | 93 |
* |
9 | 94 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
* |
9 | 96 |
* @return string|false The current working directory on success, false on failure. |
0 | 97 |
*/ |
5 | 98 |
public function cwd() { |
16 | 99 |
return getcwd(); |
0 | 100 |
} |
101 |
||
102 |
/** |
|
9 | 103 |
* Changes current directory. |
0 | 104 |
* |
9 | 105 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
* |
0 | 107 |
* @param string $dir The new current directory. |
9 | 108 |
* @return bool True on success, false on failure. |
0 | 109 |
*/ |
9 | 110 |
public function chdir( $dir ) { |
111 |
return @chdir( $dir ); |
|
0 | 112 |
} |
113 |
||
114 |
/** |
|
9 | 115 |
* Changes the file group. |
0 | 116 |
* |
9 | 117 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
* |
9 | 119 |
* @param string $file Path to the file. |
120 |
* @param string|int $group A group name or number. |
|
121 |
* @param bool $recursive Optional. If set to true, changes file group recursively. |
|
122 |
* Default false. |
|
123 |
* @return bool True on success, false on failure. |
|
0 | 124 |
*/ |
9 | 125 |
public function chgrp( $file, $group, $recursive = false ) { |
126 |
if ( ! $this->exists( $file ) ) { |
|
0 | 127 |
return false; |
9 | 128 |
} |
16 | 129 |
|
9 | 130 |
if ( ! $recursive ) { |
16 | 131 |
return chgrp( $file, $group ); |
9 | 132 |
} |
16 | 133 |
|
9 | 134 |
if ( ! $this->is_dir( $file ) ) { |
16 | 135 |
return chgrp( $file, $group ); |
9 | 136 |
} |
16 | 137 |
|
138 |
// Is a directory, and we want recursive. |
|
9 | 139 |
$file = trailingslashit( $file ); |
140 |
$filelist = $this->dirlist( $file ); |
|
16 | 141 |
|
9 | 142 |
foreach ( $filelist as $filename ) { |
143 |
$this->chgrp( $file . $filename, $group, $recursive ); |
|
144 |
} |
|
0 | 145 |
|
146 |
return true; |
|
147 |
} |
|
148 |
||
149 |
/** |
|
9 | 150 |
* Changes filesystem permissions. |
0 | 151 |
* |
9 | 152 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* |
9 | 154 |
* @param string $file Path to the file. |
155 |
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
|
156 |
* 0755 for directories. Default false. |
|
16 | 157 |
* @param bool $recursive Optional. If set to true, changes file permissions recursively. |
9 | 158 |
* Default false. |
159 |
* @return bool True on success, false on failure. |
|
0 | 160 |
*/ |
9 | 161 |
public function chmod( $file, $mode = false, $recursive = false ) { |
0 | 162 |
if ( ! $mode ) { |
9 | 163 |
if ( $this->is_file( $file ) ) { |
0 | 164 |
$mode = FS_CHMOD_FILE; |
9 | 165 |
} elseif ( $this->is_dir( $file ) ) { |
0 | 166 |
$mode = FS_CHMOD_DIR; |
9 | 167 |
} else { |
0 | 168 |
return false; |
9 | 169 |
} |
0 | 170 |
} |
171 |
||
9 | 172 |
if ( ! $recursive || ! $this->is_dir( $file ) ) { |
16 | 173 |
return chmod( $file, $mode ); |
9 | 174 |
} |
16 | 175 |
|
176 |
// Is a directory, and we want recursive. |
|
9 | 177 |
$file = trailingslashit( $file ); |
178 |
$filelist = $this->dirlist( $file ); |
|
16 | 179 |
|
9 | 180 |
foreach ( (array) $filelist as $filename => $filemeta ) { |
181 |
$this->chmod( $file . $filename, $mode, $recursive ); |
|
182 |
} |
|
0 | 183 |
|
184 |
return true; |
|
185 |
} |
|
186 |
||
187 |
/** |
|
9 | 188 |
* Changes the owner of a file or directory. |
0 | 189 |
* |
9 | 190 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
* |
9 | 192 |
* @param string $file Path to the file or directory. |
193 |
* @param string|int $owner A user name or number. |
|
194 |
* @param bool $recursive Optional. If set to true, changes file owner recursively. |
|
195 |
* Default false. |
|
196 |
* @return bool True on success, false on failure. |
|
0 | 197 |
*/ |
9 | 198 |
public function chown( $file, $owner, $recursive = false ) { |
199 |
if ( ! $this->exists( $file ) ) { |
|
0 | 200 |
return false; |
9 | 201 |
} |
16 | 202 |
|
9 | 203 |
if ( ! $recursive ) { |
16 | 204 |
return chown( $file, $owner ); |
9 | 205 |
} |
16 | 206 |
|
9 | 207 |
if ( ! $this->is_dir( $file ) ) { |
16 | 208 |
return chown( $file, $owner ); |
9 | 209 |
} |
16 | 210 |
|
211 |
// Is a directory, and we want recursive. |
|
9 | 212 |
$filelist = $this->dirlist( $file ); |
16 | 213 |
|
9 | 214 |
foreach ( $filelist as $filename ) { |
215 |
$this->chown( $file . '/' . $filename, $owner, $recursive ); |
|
0 | 216 |
} |
16 | 217 |
|
0 | 218 |
return true; |
219 |
} |
|
220 |
||
221 |
/** |
|
9 | 222 |
* Gets the file owner. |
0 | 223 |
* |
9 | 224 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
* |
0 | 226 |
* @param string $file Path to the file. |
9 | 227 |
* @return string|false Username of the owner on success, false on failure. |
0 | 228 |
*/ |
9 | 229 |
public function owner( $file ) { |
230 |
$owneruid = @fileowner( $file ); |
|
16 | 231 |
|
9 | 232 |
if ( ! $owneruid ) { |
0 | 233 |
return false; |
9 | 234 |
} |
16 | 235 |
|
9 | 236 |
if ( ! function_exists( 'posix_getpwuid' ) ) { |
0 | 237 |
return $owneruid; |
9 | 238 |
} |
16 | 239 |
|
9 | 240 |
$ownerarray = posix_getpwuid( $owneruid ); |
16 | 241 |
|
242 |
if ( ! $ownerarray ) { |
|
243 |
return false; |
|
244 |
} |
|
245 |
||
0 | 246 |
return $ownerarray['name']; |
247 |
} |
|
248 |
||
249 |
/** |
|
9 | 250 |
* Gets the permissions of the specified file or filepath in their octal format. |
0 | 251 |
* |
252 |
* FIXME does not handle errors in fileperms() |
|
253 |
* |
|
9 | 254 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* |
0 | 256 |
* @param string $file Path to the file. |
9 | 257 |
* @return string Mode of the file (the last 3 digits). |
0 | 258 |
*/ |
9 | 259 |
public function getchmod( $file ) { |
5 | 260 |
return substr( decoct( @fileperms( $file ) ), -3 ); |
0 | 261 |
} |
262 |
||
5 | 263 |
/** |
9 | 264 |
* Gets the file's group. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
* |
9 | 266 |
* @since 2.5.0 |
267 |
* |
|
268 |
* @param string $file Path to the file. |
|
269 |
* @return string|false The group on success, false on failure. |
|
5 | 270 |
*/ |
9 | 271 |
public function group( $file ) { |
272 |
$gid = @filegroup( $file ); |
|
16 | 273 |
|
9 | 274 |
if ( ! $gid ) { |
0 | 275 |
return false; |
9 | 276 |
} |
16 | 277 |
|
9 | 278 |
if ( ! function_exists( 'posix_getgrgid' ) ) { |
0 | 279 |
return $gid; |
9 | 280 |
} |
16 | 281 |
|
9 | 282 |
$grouparray = posix_getgrgid( $gid ); |
16 | 283 |
|
284 |
if ( ! $grouparray ) { |
|
285 |
return false; |
|
286 |
} |
|
287 |
||
0 | 288 |
return $grouparray['name']; |
289 |
} |
|
290 |
||
5 | 291 |
/** |
9 | 292 |
* Copies a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
293 |
* |
9 | 294 |
* @since 2.5.0 |
295 |
* |
|
296 |
* @param string $source Path to the source file. |
|
297 |
* @param string $destination Path to the destination file. |
|
298 |
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
|
299 |
* Default false. |
|
300 |
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
|
301 |
* 0755 for dirs. Default false. |
|
302 |
* @return bool True on success, false on failure. |
|
5 | 303 |
*/ |
9 | 304 |
public function copy( $source, $destination, $overwrite = false, $mode = false ) { |
305 |
if ( ! $overwrite && $this->exists( $destination ) ) { |
|
0 | 306 |
return false; |
9 | 307 |
} |
0 | 308 |
|
9 | 309 |
$rtval = copy( $source, $destination ); |
16 | 310 |
|
9 | 311 |
if ( $mode ) { |
312 |
$this->chmod( $destination, $mode ); |
|
313 |
} |
|
16 | 314 |
|
0 | 315 |
return $rtval; |
316 |
} |
|
317 |
||
5 | 318 |
/** |
9 | 319 |
* Moves a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
320 |
* |
9 | 321 |
* @since 2.5.0 |
322 |
* |
|
323 |
* @param string $source Path to the source file. |
|
324 |
* @param string $destination Path to the destination file. |
|
325 |
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
|
326 |
* Default false. |
|
327 |
* @return bool True on success, false on failure. |
|
5 | 328 |
*/ |
9 | 329 |
public function move( $source, $destination, $overwrite = false ) { |
330 |
if ( ! $overwrite && $this->exists( $destination ) ) { |
|
0 | 331 |
return false; |
9 | 332 |
} |
0 | 333 |
|
5 | 334 |
// Try using rename first. if that fails (for example, source is read only) try copy. |
9 | 335 |
if ( @rename( $source, $destination ) ) { |
0 | 336 |
return true; |
9 | 337 |
} |
0 | 338 |
|
9 | 339 |
if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) { |
340 |
$this->delete( $source ); |
|
16 | 341 |
|
0 | 342 |
return true; |
343 |
} else { |
|
344 |
return false; |
|
345 |
} |
|
346 |
} |
|
347 |
||
5 | 348 |
/** |
9 | 349 |
* Deletes a file or directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* |
9 | 351 |
* @since 2.5.0 |
352 |
* |
|
353 |
* @param string $file Path to the file or directory. |
|
16 | 354 |
* @param bool $recursive Optional. If set to true, deletes files and folders recursively. |
9 | 355 |
* Default false. |
356 |
* @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
|
357 |
* Default false. |
|
358 |
* @return bool True on success, false on failure. |
|
5 | 359 |
*/ |
9 | 360 |
public function delete( $file, $recursive = false, $type = false ) { |
16 | 361 |
if ( empty( $file ) ) { |
362 |
// Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. |
|
0 | 363 |
return false; |
9 | 364 |
} |
0 | 365 |
|
16 | 366 |
$file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise. |
367 |
||
368 |
if ( 'f' === $type || $this->is_file( $file ) ) { |
|
9 | 369 |
return @unlink( $file ); |
370 |
} |
|
16 | 371 |
|
9 | 372 |
if ( ! $recursive && $this->is_dir( $file ) ) { |
373 |
return @rmdir( $file ); |
|
374 |
} |
|
0 | 375 |
|
16 | 376 |
// At this point it's a folder, and we're in recursive mode. |
9 | 377 |
$file = trailingslashit( $file ); |
378 |
$filelist = $this->dirlist( $file, true ); |
|
0 | 379 |
|
380 |
$retval = true; |
|
16 | 381 |
|
0 | 382 |
if ( is_array( $filelist ) ) { |
383 |
foreach ( $filelist as $filename => $fileinfo ) { |
|
9 | 384 |
if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) { |
0 | 385 |
$retval = false; |
9 | 386 |
} |
0 | 387 |
} |
388 |
} |
|
389 |
||
9 | 390 |
if ( file_exists( $file ) && ! @rmdir( $file ) ) { |
0 | 391 |
$retval = false; |
9 | 392 |
} |
0 | 393 |
|
394 |
return $retval; |
|
395 |
} |
|
9 | 396 |
|
5 | 397 |
/** |
9 | 398 |
* Checks if a file or directory exists. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
399 |
* |
9 | 400 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
401 |
* |
9 | 402 |
* @param string $file Path to file or directory. |
403 |
* @return bool Whether $file exists or not. |
|
5 | 404 |
*/ |
9 | 405 |
public function exists( $file ) { |
406 |
return @file_exists( $file ); |
|
0 | 407 |
} |
9 | 408 |
|
5 | 409 |
/** |
9 | 410 |
* Checks if resource is a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
411 |
* |
9 | 412 |
* @since 2.5.0 |
413 |
* |
|
414 |
* @param string $file File path. |
|
415 |
* @return bool Whether $file is a file. |
|
5 | 416 |
*/ |
9 | 417 |
public function is_file( $file ) { |
418 |
return @is_file( $file ); |
|
0 | 419 |
} |
420 |
||
5 | 421 |
/** |
9 | 422 |
* Checks if resource is a directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
423 |
* |
9 | 424 |
* @since 2.5.0 |
425 |
* |
|
426 |
* @param string $path Directory path. |
|
427 |
* @return bool Whether $path is a directory. |
|
5 | 428 |
*/ |
9 | 429 |
public function is_dir( $path ) { |
430 |
return @is_dir( $path ); |
|
0 | 431 |
} |
432 |
||
5 | 433 |
/** |
9 | 434 |
* Checks if a file is readable. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
* |
9 | 436 |
* @since 2.5.0 |
437 |
* |
|
438 |
* @param string $file Path to file. |
|
439 |
* @return bool Whether $file is readable. |
|
5 | 440 |
*/ |
9 | 441 |
public function is_readable( $file ) { |
442 |
return @is_readable( $file ); |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* Checks if a file or directory is writable. |
|
447 |
* |
|
448 |
* @since 2.5.0 |
|
449 |
* |
|
450 |
* @param string $file Path to file or directory. |
|
451 |
* @return bool Whether $file is writable. |
|
452 |
*/ |
|
453 |
public function is_writable( $file ) { |
|
454 |
return @is_writable( $file ); |
|
0 | 455 |
} |
456 |
||
5 | 457 |
/** |
9 | 458 |
* Gets the file's last access time. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
* |
9 | 460 |
* @since 2.5.0 |
461 |
* |
|
462 |
* @param string $file Path to file. |
|
463 |
* @return int|false Unix timestamp representing last access time, false on failure. |
|
5 | 464 |
*/ |
9 | 465 |
public function atime( $file ) { |
466 |
return @fileatime( $file ); |
|
0 | 467 |
} |
468 |
||
5 | 469 |
/** |
9 | 470 |
* Gets the file modification time. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
* |
9 | 472 |
* @since 2.5.0 |
473 |
* |
|
474 |
* @param string $file Path to file. |
|
475 |
* @return int|false Unix timestamp representing modification time, false on failure. |
|
5 | 476 |
*/ |
9 | 477 |
public function mtime( $file ) { |
478 |
return @filemtime( $file ); |
|
0 | 479 |
} |
480 |
||
5 | 481 |
/** |
9 | 482 |
* Gets the file size (in bytes). |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
483 |
* |
9 | 484 |
* @since 2.5.0 |
485 |
* |
|
486 |
* @param string $file Path to file. |
|
487 |
* @return int|false Size of the file in bytes on success, false on failure. |
|
5 | 488 |
*/ |
9 | 489 |
public function size( $file ) { |
490 |
return @filesize( $file ); |
|
0 | 491 |
} |
492 |
||
5 | 493 |
/** |
9 | 494 |
* Sets the access and modification times of a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
495 |
* |
9 | 496 |
* Note: If $file doesn't exist, it will be created. |
497 |
* |
|
498 |
* @since 2.5.0 |
|
499 |
* |
|
500 |
* @param string $file Path to file. |
|
501 |
* @param int $time Optional. Modified time to set for file. |
|
502 |
* Default 0. |
|
503 |
* @param int $atime Optional. Access time to set for file. |
|
504 |
* Default 0. |
|
505 |
* @return bool True on success, false on failure. |
|
5 | 506 |
*/ |
9 | 507 |
public function touch( $file, $time = 0, $atime = 0 ) { |
18 | 508 |
if ( 0 === $time ) { |
0 | 509 |
$time = time(); |
9 | 510 |
} |
16 | 511 |
|
18 | 512 |
if ( 0 === $atime ) { |
0 | 513 |
$atime = time(); |
9 | 514 |
} |
16 | 515 |
|
516 |
return touch( $file, $time, $atime ); |
|
0 | 517 |
} |
518 |
||
5 | 519 |
/** |
9 | 520 |
* Creates a directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
521 |
* |
9 | 522 |
* @since 2.5.0 |
523 |
* |
|
18 | 524 |
* @param string $path Path for new directory. |
525 |
* @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod). |
|
526 |
* Default false. |
|
527 |
* @param string|int|false $chown Optional. A user name or number (or false to skip chown). |
|
528 |
* Default false. |
|
529 |
* @param string|int|false $chgrp Optional. A group name or number (or false to skip chgrp). |
|
530 |
* Default false. |
|
9 | 531 |
* @return bool True on success, false on failure. |
5 | 532 |
*/ |
9 | 533 |
public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { |
5 | 534 |
// Safe mode fails with a trailing slash under certain PHP versions. |
9 | 535 |
$path = untrailingslashit( $path ); |
16 | 536 |
|
9 | 537 |
if ( empty( $path ) ) { |
0 | 538 |
return false; |
9 | 539 |
} |
0 | 540 |
|
9 | 541 |
if ( ! $chmod ) { |
0 | 542 |
$chmod = FS_CHMOD_DIR; |
9 | 543 |
} |
0 | 544 |
|
9 | 545 |
if ( ! @mkdir( $path ) ) { |
0 | 546 |
return false; |
9 | 547 |
} |
16 | 548 |
|
9 | 549 |
$this->chmod( $path, $chmod ); |
16 | 550 |
|
9 | 551 |
if ( $chown ) { |
552 |
$this->chown( $path, $chown ); |
|
553 |
} |
|
16 | 554 |
|
9 | 555 |
if ( $chgrp ) { |
556 |
$this->chgrp( $path, $chgrp ); |
|
557 |
} |
|
16 | 558 |
|
0 | 559 |
return true; |
560 |
} |
|
561 |
||
5 | 562 |
/** |
9 | 563 |
* Deletes a directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
* |
9 | 565 |
* @since 2.5.0 |
566 |
* |
|
567 |
* @param string $path Path to directory. |
|
568 |
* @param bool $recursive Optional. Whether to recursively remove files/directories. |
|
569 |
* Default false. |
|
570 |
* @return bool True on success, false on failure. |
|
5 | 571 |
*/ |
9 | 572 |
public function rmdir( $path, $recursive = false ) { |
573 |
return $this->delete( $path, $recursive ); |
|
0 | 574 |
} |
575 |
||
5 | 576 |
/** |
9 | 577 |
* Gets details for files in a directory or a specific file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
578 |
* |
9 | 579 |
* @since 2.5.0 |
580 |
* |
|
581 |
* @param string $path Path to directory or file. |
|
582 |
* @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. |
|
583 |
* Default true. |
|
584 |
* @param bool $recursive Optional. Whether to recursively include file details in nested directories. |
|
585 |
* Default false. |
|
586 |
* @return array|false { |
|
587 |
* Array of files. False if unable to list directory contents. |
|
588 |
* |
|
589 |
* @type string $name Name of the file or directory. |
|
590 |
* @type string $perms *nix representation of permissions. |
|
19 | 591 |
* @type string $permsn Octal representation of permissions. |
9 | 592 |
* @type string $owner Owner name or ID. |
593 |
* @type int $size Size of file in bytes. |
|
594 |
* @type int $lastmodunix Last modified unix timestamp. |
|
595 |
* @type mixed $lastmod Last modified month (3 letter) and day (without leading 0). |
|
596 |
* @type int $time Last modified time. |
|
597 |
* @type string $type Type of resource. 'f' for file, 'd' for directory. |
|
19 | 598 |
* @type mixed $files If a directory and `$recursive` is true, contains another array of files. |
9 | 599 |
* } |
5 | 600 |
*/ |
9 | 601 |
public function dirlist( $path, $include_hidden = true, $recursive = false ) { |
602 |
if ( $this->is_file( $path ) ) { |
|
603 |
$limit_file = basename( $path ); |
|
604 |
$path = dirname( $path ); |
|
0 | 605 |
} else { |
606 |
$limit_file = false; |
|
607 |
} |
|
608 |
||
16 | 609 |
if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) { |
0 | 610 |
return false; |
9 | 611 |
} |
0 | 612 |
|
16 | 613 |
$dir = dir( $path ); |
614 |
||
9 | 615 |
if ( ! $dir ) { |
0 | 616 |
return false; |
9 | 617 |
} |
0 | 618 |
|
619 |
$ret = array(); |
|
620 |
||
9 | 621 |
while ( false !== ( $entry = $dir->read() ) ) { |
622 |
$struc = array(); |
|
0 | 623 |
$struc['name'] = $entry; |
624 |
||
16 | 625 |
if ( '.' === $struc['name'] || '..' === $struc['name'] ) { |
0 | 626 |
continue; |
9 | 627 |
} |
0 | 628 |
|
16 | 629 |
if ( ! $include_hidden && '.' === $struc['name'][0] ) { |
0 | 630 |
continue; |
9 | 631 |
} |
0 | 632 |
|
18 | 633 |
if ( $limit_file && $struc['name'] !== $limit_file ) { |
9 | 634 |
continue; |
635 |
} |
|
636 |
||
637 |
$struc['perms'] = $this->gethchmod( $path . '/' . $entry ); |
|
638 |
$struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); |
|
639 |
$struc['number'] = false; |
|
640 |
$struc['owner'] = $this->owner( $path . '/' . $entry ); |
|
641 |
$struc['group'] = $this->group( $path . '/' . $entry ); |
|
642 |
$struc['size'] = $this->size( $path . '/' . $entry ); |
|
643 |
$struc['lastmodunix'] = $this->mtime( $path . '/' . $entry ); |
|
16 | 644 |
$struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] ); |
645 |
$struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] ); |
|
9 | 646 |
$struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f'; |
0 | 647 |
|
16 | 648 |
if ( 'd' === $struc['type'] ) { |
9 | 649 |
if ( $recursive ) { |
650 |
$struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); |
|
651 |
} else { |
|
0 | 652 |
$struc['files'] = array(); |
9 | 653 |
} |
0 | 654 |
} |
655 |
||
656 |
$ret[ $struc['name'] ] = $struc; |
|
657 |
} |
|
16 | 658 |
|
0 | 659 |
$dir->close(); |
9 | 660 |
unset( $dir ); |
16 | 661 |
|
0 | 662 |
return $ret; |
663 |
} |
|
664 |
} |