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