author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
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 |
/** |
|
19 |
* constructor |
|
20 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* |
0 | 22 |
* @param mixed $arg ignored argument |
23 |
*/ |
|
5 | 24 |
public function __construct($arg) { |
0 | 25 |
$this->method = 'direct'; |
26 |
$this->errors = new WP_Error(); |
|
27 |
} |
|
28 |
||
29 |
/** |
|
30 |
* Reads entire file into a string |
|
31 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
32 |
* |
0 | 33 |
* @param string $file Name of the file to read. |
34 |
* @return string|bool The function returns the read data or false on failure. |
|
35 |
*/ |
|
5 | 36 |
public function get_contents($file) { |
0 | 37 |
return @file_get_contents($file); |
38 |
} |
|
39 |
||
40 |
/** |
|
41 |
* Reads entire file into an array |
|
42 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
* |
0 | 44 |
* @param string $file Path to the file. |
45 |
* @return array|bool the file contents in an array or false on failure. |
|
46 |
*/ |
|
5 | 47 |
public function get_contents_array($file) { |
0 | 48 |
return @file($file); |
49 |
} |
|
50 |
||
51 |
/** |
|
52 |
* Write a string to a file |
|
53 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* |
5 | 55 |
* @param string $file Remote path to the file where to write the data. |
0 | 56 |
* @param string $contents The data to write. |
5 | 57 |
* @param int $mode Optional. The file permissions as octal number, usually 0644. |
58 |
* Default false. |
|
59 |
* @return bool False upon failure, true otherwise. |
|
0 | 60 |
*/ |
5 | 61 |
public function put_contents( $file, $contents, $mode = false ) { |
0 | 62 |
$fp = @fopen( $file, 'wb' ); |
63 |
if ( ! $fp ) |
|
64 |
return false; |
|
65 |
||
66 |
mbstring_binary_safe_encoding(); |
|
67 |
||
68 |
$data_length = strlen( $contents ); |
|
69 |
||
70 |
$bytes_written = fwrite( $fp, $contents ); |
|
71 |
||
72 |
reset_mbstring_encoding(); |
|
73 |
||
74 |
fclose( $fp ); |
|
75 |
||
76 |
if ( $data_length !== $bytes_written ) |
|
77 |
return false; |
|
78 |
||
79 |
$this->chmod( $file, $mode ); |
|
80 |
||
81 |
return true; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Gets the current working directory |
|
86 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* |
0 | 88 |
* @return string|bool the current working directory on success, or false on failure. |
89 |
*/ |
|
5 | 90 |
public function cwd() { |
0 | 91 |
return @getcwd(); |
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Change directory |
|
96 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
97 |
* |
0 | 98 |
* @param string $dir The new current directory. |
99 |
* @return bool Returns true on success or false on failure. |
|
100 |
*/ |
|
5 | 101 |
public function chdir($dir) { |
0 | 102 |
return @chdir($dir); |
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Changes file group |
|
107 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
* |
5 | 109 |
* @param string $file Path to the file. |
110 |
* @param mixed $group A group name or number. |
|
111 |
* @param bool $recursive Optional. If set True changes file group recursively. Default false. |
|
0 | 112 |
* @return bool Returns true on success or false on failure. |
113 |
*/ |
|
5 | 114 |
public function chgrp($file, $group, $recursive = false) { |
0 | 115 |
if ( ! $this->exists($file) ) |
116 |
return false; |
|
117 |
if ( ! $recursive ) |
|
118 |
return @chgrp($file, $group); |
|
119 |
if ( ! $this->is_dir($file) ) |
|
120 |
return @chgrp($file, $group); |
|
121 |
// Is a directory, and we want recursive |
|
122 |
$file = trailingslashit($file); |
|
123 |
$filelist = $this->dirlist($file); |
|
124 |
foreach ($filelist as $filename) |
|
125 |
$this->chgrp($file . $filename, $group, $recursive); |
|
126 |
||
127 |
return true; |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Changes filesystem permissions |
|
132 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
133 |
* |
5 | 134 |
* @param string $file Path to the file. |
135 |
* @param int $mode Optional. The permissions as octal number, usually 0644 for files, |
|
136 |
* 0755 for dirs. Default false. |
|
137 |
* @param bool $recursive Optional. If set True changes file group recursively. Default false. |
|
0 | 138 |
* @return bool Returns true on success or false on failure. |
139 |
*/ |
|
5 | 140 |
public function chmod($file, $mode = false, $recursive = false) { |
0 | 141 |
if ( ! $mode ) { |
142 |
if ( $this->is_file($file) ) |
|
143 |
$mode = FS_CHMOD_FILE; |
|
144 |
elseif ( $this->is_dir($file) ) |
|
145 |
$mode = FS_CHMOD_DIR; |
|
146 |
else |
|
147 |
return false; |
|
148 |
} |
|
149 |
||
150 |
if ( ! $recursive || ! $this->is_dir($file) ) |
|
151 |
return @chmod($file, $mode); |
|
152 |
// Is a directory, and we want recursive |
|
153 |
$file = trailingslashit($file); |
|
154 |
$filelist = $this->dirlist($file); |
|
155 |
foreach ( (array)$filelist as $filename => $filemeta) |
|
156 |
$this->chmod($file . $filename, $mode, $recursive); |
|
157 |
||
158 |
return true; |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Changes file owner |
|
163 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
* |
5 | 165 |
* @param string $file Path to the file. |
166 |
* @param mixed $owner A user name or number. |
|
167 |
* @param bool $recursive Optional. If set True changes file owner recursively. |
|
168 |
* Default false. |
|
0 | 169 |
* @return bool Returns true on success or false on failure. |
170 |
*/ |
|
5 | 171 |
public function chown($file, $owner, $recursive = false) { |
0 | 172 |
if ( ! $this->exists($file) ) |
173 |
return false; |
|
174 |
if ( ! $recursive ) |
|
175 |
return @chown($file, $owner); |
|
176 |
if ( ! $this->is_dir($file) ) |
|
177 |
return @chown($file, $owner); |
|
178 |
// Is a directory, and we want recursive |
|
179 |
$filelist = $this->dirlist($file); |
|
180 |
foreach ($filelist as $filename) { |
|
181 |
$this->chown($file . '/' . $filename, $owner, $recursive); |
|
182 |
} |
|
183 |
return true; |
|
184 |
} |
|
185 |
||
186 |
/** |
|
187 |
* Gets file owner |
|
188 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
* |
0 | 190 |
* @param string $file Path to the file. |
191 |
* @return string|bool Username of the user or false on error. |
|
192 |
*/ |
|
5 | 193 |
public function owner($file) { |
0 | 194 |
$owneruid = @fileowner($file); |
195 |
if ( ! $owneruid ) |
|
196 |
return false; |
|
197 |
if ( ! function_exists('posix_getpwuid') ) |
|
198 |
return $owneruid; |
|
199 |
$ownerarray = posix_getpwuid($owneruid); |
|
200 |
return $ownerarray['name']; |
|
201 |
} |
|
202 |
||
203 |
/** |
|
204 |
* Gets file permissions |
|
205 |
* |
|
206 |
* FIXME does not handle errors in fileperms() |
|
207 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* |
0 | 209 |
* @param string $file Path to the file. |
5 | 210 |
* @return string Mode of the file (last 3 digits). |
0 | 211 |
*/ |
5 | 212 |
public function getchmod($file) { |
213 |
return substr( decoct( @fileperms( $file ) ), -3 ); |
|
0 | 214 |
} |
215 |
||
5 | 216 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
* |
5 | 218 |
* @param string $file |
219 |
* @return string|false |
|
220 |
*/ |
|
221 |
public function group($file) { |
|
0 | 222 |
$gid = @filegroup($file); |
223 |
if ( ! $gid ) |
|
224 |
return false; |
|
225 |
if ( ! function_exists('posix_getgrgid') ) |
|
226 |
return $gid; |
|
227 |
$grouparray = posix_getgrgid($gid); |
|
228 |
return $grouparray['name']; |
|
229 |
} |
|
230 |
||
5 | 231 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
* |
5 | 233 |
* @param string $source |
234 |
* @param string $destination |
|
235 |
* @param bool $overwrite |
|
236 |
* @param int $mode |
|
237 |
* @return bool |
|
238 |
*/ |
|
239 |
public function copy($source, $destination, $overwrite = false, $mode = false) { |
|
0 | 240 |
if ( ! $overwrite && $this->exists($destination) ) |
241 |
return false; |
|
242 |
||
243 |
$rtval = copy($source, $destination); |
|
244 |
if ( $mode ) |
|
245 |
$this->chmod($destination, $mode); |
|
246 |
return $rtval; |
|
247 |
} |
|
248 |
||
5 | 249 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
* |
5 | 251 |
* @param string $source |
252 |
* @param string $destination |
|
253 |
* @param bool $overwrite |
|
254 |
* @return bool |
|
255 |
*/ |
|
256 |
public function move($source, $destination, $overwrite = false) { |
|
0 | 257 |
if ( ! $overwrite && $this->exists($destination) ) |
258 |
return false; |
|
259 |
||
5 | 260 |
// Try using rename first. if that fails (for example, source is read only) try copy. |
0 | 261 |
if ( @rename($source, $destination) ) |
262 |
return true; |
|
263 |
||
264 |
if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) { |
|
265 |
$this->delete($source); |
|
266 |
return true; |
|
267 |
} else { |
|
268 |
return false; |
|
269 |
} |
|
270 |
} |
|
271 |
||
5 | 272 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
273 |
* |
5 | 274 |
* @param string $file |
275 |
* @param bool $recursive |
|
276 |
* @param string $type |
|
277 |
* @return bool |
|
278 |
*/ |
|
279 |
public function delete($file, $recursive = false, $type = false) { |
|
0 | 280 |
if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. |
281 |
return false; |
|
282 |
$file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise |
|
283 |
||
284 |
if ( 'f' == $type || $this->is_file($file) ) |
|
285 |
return @unlink($file); |
|
286 |
if ( ! $recursive && $this->is_dir($file) ) |
|
287 |
return @rmdir($file); |
|
288 |
||
289 |
// At this point it's a folder, and we're in recursive mode |
|
290 |
$file = trailingslashit($file); |
|
291 |
$filelist = $this->dirlist($file, true); |
|
292 |
||
293 |
$retval = true; |
|
294 |
if ( is_array( $filelist ) ) { |
|
295 |
foreach ( $filelist as $filename => $fileinfo ) { |
|
296 |
if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) ) |
|
297 |
$retval = false; |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
if ( file_exists($file) && ! @rmdir($file) ) |
|
302 |
$retval = false; |
|
303 |
||
304 |
return $retval; |
|
305 |
} |
|
5 | 306 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
* |
5 | 308 |
* @param string $file |
309 |
* @return bool |
|
310 |
*/ |
|
311 |
public function exists($file) { |
|
0 | 312 |
return @file_exists($file); |
313 |
} |
|
5 | 314 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
315 |
* |
5 | 316 |
* @param string $file |
317 |
* @return bool |
|
318 |
*/ |
|
319 |
public function is_file($file) { |
|
0 | 320 |
return @is_file($file); |
321 |
} |
|
5 | 322 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
323 |
* |
5 | 324 |
* @param string $path |
325 |
* @return bool |
|
326 |
*/ |
|
327 |
public function is_dir($path) { |
|
0 | 328 |
return @is_dir($path); |
329 |
} |
|
330 |
||
5 | 331 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
* |
5 | 333 |
* @param string $file |
334 |
* @return bool |
|
335 |
*/ |
|
336 |
public function is_readable($file) { |
|
0 | 337 |
return @is_readable($file); |
338 |
} |
|
339 |
||
5 | 340 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
* |
5 | 342 |
* @param string $file |
343 |
* @return bool |
|
344 |
*/ |
|
345 |
public function is_writable($file) { |
|
0 | 346 |
return @is_writable($file); |
347 |
} |
|
348 |
||
5 | 349 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* |
5 | 351 |
* @param string $file |
352 |
* @return int |
|
353 |
*/ |
|
354 |
public function atime($file) { |
|
0 | 355 |
return @fileatime($file); |
356 |
} |
|
357 |
||
5 | 358 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
* |
5 | 360 |
* @param string $file |
361 |
* @return int |
|
362 |
*/ |
|
363 |
public function mtime($file) { |
|
0 | 364 |
return @filemtime($file); |
365 |
} |
|
366 |
||
5 | 367 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* |
5 | 369 |
* @param string $file |
370 |
* @return int |
|
371 |
*/ |
|
372 |
public function size($file) { |
|
0 | 373 |
return @filesize($file); |
374 |
} |
|
375 |
||
5 | 376 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
* |
5 | 378 |
* @param string $file |
379 |
* @param int $time |
|
380 |
* @param int $atime |
|
381 |
* @return bool |
|
382 |
*/ |
|
383 |
public function touch($file, $time = 0, $atime = 0) { |
|
0 | 384 |
if ($time == 0) |
385 |
$time = time(); |
|
386 |
if ($atime == 0) |
|
387 |
$atime = time(); |
|
388 |
return @touch($file, $time, $atime); |
|
389 |
} |
|
390 |
||
5 | 391 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* |
5 | 393 |
* @param string $path |
394 |
* @param mixed $chmod |
|
395 |
* @param mixed $chown |
|
396 |
* @param mixed $chgrp |
|
397 |
* @return bool |
|
398 |
*/ |
|
399 |
public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) { |
|
400 |
// Safe mode fails with a trailing slash under certain PHP versions. |
|
0 | 401 |
$path = untrailingslashit($path); |
402 |
if ( empty($path) ) |
|
403 |
return false; |
|
404 |
||
405 |
if ( ! $chmod ) |
|
406 |
$chmod = FS_CHMOD_DIR; |
|
407 |
||
408 |
if ( ! @mkdir($path) ) |
|
409 |
return false; |
|
410 |
$this->chmod($path, $chmod); |
|
411 |
if ( $chown ) |
|
412 |
$this->chown($path, $chown); |
|
413 |
if ( $chgrp ) |
|
414 |
$this->chgrp($path, $chgrp); |
|
415 |
return true; |
|
416 |
} |
|
417 |
||
5 | 418 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
* |
5 | 420 |
* @param string $path |
421 |
* @param bool $recursive |
|
422 |
* @return bool |
|
423 |
*/ |
|
424 |
public function rmdir($path, $recursive = false) { |
|
0 | 425 |
return $this->delete($path, $recursive); |
426 |
} |
|
427 |
||
5 | 428 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
* |
5 | 430 |
* @param string $path |
431 |
* @param bool $include_hidden |
|
432 |
* @param bool $recursive |
|
433 |
* @return bool|array |
|
434 |
*/ |
|
435 |
public function dirlist($path, $include_hidden = true, $recursive = false) { |
|
0 | 436 |
if ( $this->is_file($path) ) { |
437 |
$limit_file = basename($path); |
|
438 |
$path = dirname($path); |
|
439 |
} else { |
|
440 |
$limit_file = false; |
|
441 |
} |
|
442 |
||
443 |
if ( ! $this->is_dir($path) ) |
|
444 |
return false; |
|
445 |
||
446 |
$dir = @dir($path); |
|
447 |
if ( ! $dir ) |
|
448 |
return false; |
|
449 |
||
450 |
$ret = array(); |
|
451 |
||
452 |
while (false !== ($entry = $dir->read()) ) { |
|
453 |
$struc = array(); |
|
454 |
$struc['name'] = $entry; |
|
455 |
||
456 |
if ( '.' == $struc['name'] || '..' == $struc['name'] ) |
|
457 |
continue; |
|
458 |
||
459 |
if ( ! $include_hidden && '.' == $struc['name'][0] ) |
|
460 |
continue; |
|
461 |
||
462 |
if ( $limit_file && $struc['name'] != $limit_file) |
|
463 |
continue; |
|
464 |
||
465 |
$struc['perms'] = $this->gethchmod($path.'/'.$entry); |
|
466 |
$struc['permsn'] = $this->getnumchmodfromh($struc['perms']); |
|
467 |
$struc['number'] = false; |
|
468 |
$struc['owner'] = $this->owner($path.'/'.$entry); |
|
469 |
$struc['group'] = $this->group($path.'/'.$entry); |
|
470 |
$struc['size'] = $this->size($path.'/'.$entry); |
|
471 |
$struc['lastmodunix']= $this->mtime($path.'/'.$entry); |
|
472 |
$struc['lastmod'] = date('M j',$struc['lastmodunix']); |
|
473 |
$struc['time'] = date('h:i:s',$struc['lastmodunix']); |
|
474 |
$struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f'; |
|
475 |
||
476 |
if ( 'd' == $struc['type'] ) { |
|
477 |
if ( $recursive ) |
|
478 |
$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive); |
|
479 |
else |
|
480 |
$struc['files'] = array(); |
|
481 |
} |
|
482 |
||
483 |
$ret[ $struc['name'] ] = $struc; |
|
484 |
} |
|
485 |
$dir->close(); |
|
486 |
unset($dir); |
|
487 |
return $ret; |
|
488 |
} |
|
489 |
} |