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 FTP Sockets Filesystem. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Filesystem |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* WordPress Filesystem Class for implementing FTP Sockets. |
|
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_ftpsockets extends WP_Filesystem_Base { |
|
9 | 17 |
|
5 | 18 |
/** |
9 | 19 |
* @since 2.5.0 |
5 | 20 |
* @var ftp |
21 |
*/ |
|
22 |
public $ftp; |
|
0 | 23 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
/** |
9 | 25 |
* Constructor. |
26 |
* |
|
27 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
* @param array $opt |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
*/ |
9 | 31 |
public function __construct( $opt = '' ) { |
0 | 32 |
$this->method = 'ftpsockets'; |
33 |
$this->errors = new WP_Error(); |
|
34 |
||
35 |
// Check if possible to use ftp functions. |
|
16 | 36 |
if ( ! include_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) { |
5 | 37 |
return; |
38 |
} |
|
16 | 39 |
|
0 | 40 |
$this->ftp = new ftp(); |
41 |
||
9 | 42 |
if ( empty( $opt['port'] ) ) { |
0 | 43 |
$this->options['port'] = 21; |
9 | 44 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
$this->options['port'] = (int) $opt['port']; |
9 | 46 |
} |
0 | 47 |
|
9 | 48 |
if ( empty( $opt['hostname'] ) ) { |
49 |
$this->errors->add( 'empty_hostname', __( 'FTP hostname is required' ) ); |
|
50 |
} else { |
|
0 | 51 |
$this->options['hostname'] = $opt['hostname']; |
9 | 52 |
} |
0 | 53 |
|
54 |
// Check if the options provided are OK. |
|
9 | 55 |
if ( empty( $opt['username'] ) ) { |
56 |
$this->errors->add( 'empty_username', __( 'FTP username is required' ) ); |
|
57 |
} else { |
|
0 | 58 |
$this->options['username'] = $opt['username']; |
9 | 59 |
} |
0 | 60 |
|
9 | 61 |
if ( empty( $opt['password'] ) ) { |
62 |
$this->errors->add( 'empty_password', __( 'FTP password is required' ) ); |
|
63 |
} else { |
|
0 | 64 |
$this->options['password'] = $opt['password']; |
9 | 65 |
} |
0 | 66 |
} |
67 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
/** |
9 | 69 |
* Connects filesystem. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* |
9 | 71 |
* @since 2.5.0 |
72 |
* |
|
73 |
* @return bool True on success, false on failure. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
*/ |
5 | 75 |
public function connect() { |
9 | 76 |
if ( ! $this->ftp ) { |
0 | 77 |
return false; |
9 | 78 |
} |
0 | 79 |
|
9 | 80 |
$this->ftp->setTimeout( FS_CONNECT_TIMEOUT ); |
0 | 81 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
if ( ! $this->ftp->SetServer( $this->options['hostname'], $this->options['port'] ) ) { |
9 | 83 |
$this->errors->add( |
84 |
'connect', |
|
85 |
sprintf( |
|
16 | 86 |
/* translators: %s: hostname:port */ |
9 | 87 |
__( 'Failed to connect to FTP Server %s' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
$this->options['hostname'] . ':' . $this->options['port'] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
); |
16 | 91 |
|
0 | 92 |
return false; |
93 |
} |
|
94 |
||
95 |
if ( ! $this->ftp->connect() ) { |
|
9 | 96 |
$this->errors->add( |
97 |
'connect', |
|
98 |
sprintf( |
|
16 | 99 |
/* translators: %s: hostname:port */ |
9 | 100 |
__( 'Failed to connect to FTP Server %s' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
$this->options['hostname'] . ':' . $this->options['port'] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
); |
16 | 104 |
|
0 | 105 |
return false; |
106 |
} |
|
107 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
if ( ! $this->ftp->login( $this->options['username'], $this->options['password'] ) ) { |
9 | 109 |
$this->errors->add( |
110 |
'auth', |
|
111 |
sprintf( |
|
16 | 112 |
/* translators: %s: Username. */ |
9 | 113 |
__( 'Username/Password incorrect for %s' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
$this->options['username'] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
116 |
); |
16 | 117 |
|
0 | 118 |
return false; |
119 |
} |
|
120 |
||
121 |
$this->ftp->SetType( FTP_BINARY ); |
|
122 |
$this->ftp->Passive( true ); |
|
123 |
$this->ftp->setTimeout( FS_TIMEOUT ); |
|
16 | 124 |
|
0 | 125 |
return true; |
126 |
} |
|
127 |
||
5 | 128 |
/** |
9 | 129 |
* Reads entire file into a string. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
131 |
* @since 2.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
* |
9 | 133 |
* @param string $file Name of the file to read. |
134 |
* @return string|false Read data on success, false if no temporary file could be opened, |
|
135 |
* or if the file couldn't be retrieved. |
|
5 | 136 |
*/ |
137 |
public function get_contents( $file ) { |
|
9 | 138 |
if ( ! $this->exists( $file ) ) { |
0 | 139 |
return false; |
9 | 140 |
} |
0 | 141 |
|
16 | 142 |
$tempfile = wp_tempnam( $file ); |
143 |
$temphandle = fopen( $tempfile, 'w+' ); |
|
0 | 144 |
|
16 | 145 |
if ( ! $temphandle ) { |
146 |
unlink( $tempfile ); |
|
0 | 147 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
} |
0 | 149 |
|
150 |
mbstring_binary_safe_encoding(); |
|
151 |
||
9 | 152 |
if ( ! $this->ftp->fget( $temphandle, $file ) ) { |
153 |
fclose( $temphandle ); |
|
16 | 154 |
unlink( $tempfile ); |
0 | 155 |
|
156 |
reset_mbstring_encoding(); |
|
157 |
||
16 | 158 |
return ''; // Blank document. File does exist, it's just blank. |
0 | 159 |
} |
160 |
||
161 |
reset_mbstring_encoding(); |
|
162 |
||
16 | 163 |
fseek( $temphandle, 0 ); // Skip back to the start of the file being written to. |
0 | 164 |
$contents = ''; |
165 |
||
9 | 166 |
while ( ! feof( $temphandle ) ) { |
167 |
$contents .= fread( $temphandle, 8 * KB_IN_BYTES ); |
|
168 |
} |
|
0 | 169 |
|
9 | 170 |
fclose( $temphandle ); |
16 | 171 |
unlink( $tempfile ); |
172 |
||
0 | 173 |
return $contents; |
174 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
|
5 | 176 |
/** |
9 | 177 |
* Reads entire file into an array. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* |
9 | 179 |
* @since 2.5.0 |
180 |
* |
|
181 |
* @param string $file Path to the file. |
|
182 |
* @return array|false File contents in an array on success, false on failure. |
|
5 | 183 |
*/ |
9 | 184 |
public function get_contents_array( $file ) { |
185 |
return explode( "\n", $this->get_contents( $file ) ); |
|
0 | 186 |
} |
187 |
||
5 | 188 |
/** |
9 | 189 |
* Writes a string to a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
* |
9 | 191 |
* @since 2.5.0 |
192 |
* |
|
193 |
* @param string $file Remote path to the file where to write the data. |
|
194 |
* @param string $contents The data to write. |
|
195 |
* @param int|false $mode Optional. The file permissions as octal number, usually 0644. |
|
196 |
* Default false. |
|
197 |
* @return bool True on success, false on failure. |
|
5 | 198 |
*/ |
9 | 199 |
public function put_contents( $file, $contents, $mode = false ) { |
16 | 200 |
$tempfile = wp_tempnam( $file ); |
201 |
$temphandle = @fopen( $tempfile, 'w+' ); |
|
202 |
||
203 |
if ( ! $temphandle ) { |
|
204 |
unlink( $tempfile ); |
|
0 | 205 |
return false; |
206 |
} |
|
207 |
||
16 | 208 |
// The FTP class uses string functions internally during file download/upload. |
0 | 209 |
mbstring_binary_safe_encoding(); |
210 |
||
211 |
$bytes_written = fwrite( $temphandle, $contents ); |
|
16 | 212 |
|
18 | 213 |
if ( false === $bytes_written || strlen( $contents ) !== $bytes_written ) { |
0 | 214 |
fclose( $temphandle ); |
16 | 215 |
unlink( $tempfile ); |
0 | 216 |
|
217 |
reset_mbstring_encoding(); |
|
218 |
||
219 |
return false; |
|
220 |
} |
|
221 |
||
16 | 222 |
fseek( $temphandle, 0 ); // Skip back to the start of the file being written to. |
0 | 223 |
|
9 | 224 |
$ret = $this->ftp->fput( $file, $temphandle ); |
0 | 225 |
|
226 |
reset_mbstring_encoding(); |
|
227 |
||
9 | 228 |
fclose( $temphandle ); |
16 | 229 |
unlink( $tempfile ); |
0 | 230 |
|
9 | 231 |
$this->chmod( $file, $mode ); |
0 | 232 |
|
233 |
return $ret; |
|
234 |
} |
|
235 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
/** |
9 | 237 |
* Gets the current working directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
* |
9 | 239 |
* @since 2.5.0 |
240 |
* |
|
241 |
* @return string|false The current working directory on success, false on failure. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
*/ |
5 | 243 |
public function cwd() { |
0 | 244 |
$cwd = $this->ftp->pwd(); |
16 | 245 |
|
9 | 246 |
if ( $cwd ) { |
247 |
$cwd = trailingslashit( $cwd ); |
|
248 |
} |
|
16 | 249 |
|
0 | 250 |
return $cwd; |
251 |
} |
|
252 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
/** |
9 | 254 |
* Changes current directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* |
9 | 256 |
* @since 2.5.0 |
257 |
* |
|
258 |
* @param string $dir The new current directory. |
|
259 |
* @return bool True on success, false on failure. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
*/ |
9 | 261 |
public function chdir( $dir ) { |
262 |
return $this->ftp->chdir( $dir ); |
|
0 | 263 |
} |
264 |
||
5 | 265 |
/** |
9 | 266 |
* Changes filesystem permissions. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
* |
9 | 268 |
* @since 2.5.0 |
269 |
* |
|
270 |
* @param string $file Path to the file. |
|
271 |
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
|
272 |
* 0755 for directories. Default false. |
|
16 | 273 |
* @param bool $recursive Optional. If set to true, changes file permissions recursively. |
9 | 274 |
* Default false. |
275 |
* @return bool True on success, false on failure. |
|
5 | 276 |
*/ |
9 | 277 |
public function chmod( $file, $mode = false, $recursive = false ) { |
0 | 278 |
if ( ! $mode ) { |
9 | 279 |
if ( $this->is_file( $file ) ) { |
0 | 280 |
$mode = FS_CHMOD_FILE; |
9 | 281 |
} elseif ( $this->is_dir( $file ) ) { |
0 | 282 |
$mode = FS_CHMOD_DIR; |
9 | 283 |
} else { |
0 | 284 |
return false; |
9 | 285 |
} |
0 | 286 |
} |
287 |
||
288 |
// chmod any sub-objects if recursive. |
|
9 | 289 |
if ( $recursive && $this->is_dir( $file ) ) { |
290 |
$filelist = $this->dirlist( $file ); |
|
16 | 291 |
|
9 | 292 |
foreach ( (array) $filelist as $filename => $filemeta ) { |
293 |
$this->chmod( $file . '/' . $filename, $mode, $recursive ); |
|
294 |
} |
|
0 | 295 |
} |
296 |
||
16 | 297 |
// chmod the file or directory. |
9 | 298 |
return $this->ftp->chmod( $file, $mode ); |
0 | 299 |
} |
300 |
||
5 | 301 |
/** |
9 | 302 |
* Gets the file owner. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* |
9 | 304 |
* @since 2.5.0 |
305 |
* |
|
306 |
* @param string $file Path to the file. |
|
307 |
* @return string|false Username of the owner on success, false on failure. |
|
5 | 308 |
*/ |
9 | 309 |
public function owner( $file ) { |
310 |
$dir = $this->dirlist( $file ); |
|
16 | 311 |
|
9 | 312 |
return $dir[ $file ]['owner']; |
0 | 313 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
314 |
|
5 | 315 |
/** |
9 | 316 |
* Gets the permissions of the specified file or filepath in their octal format. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
317 |
* |
9 | 318 |
* @since 2.5.0 |
319 |
* |
|
320 |
* @param string $file Path to the file. |
|
321 |
* @return string Mode of the file (the last 3 digits). |
|
5 | 322 |
*/ |
9 | 323 |
public function getchmod( $file ) { |
324 |
$dir = $this->dirlist( $file ); |
|
16 | 325 |
|
9 | 326 |
return $dir[ $file ]['permsn']; |
0 | 327 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
|
5 | 329 |
/** |
9 | 330 |
* Gets the file's group. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
* |
9 | 332 |
* @since 2.5.0 |
333 |
* |
|
334 |
* @param string $file Path to the file. |
|
335 |
* @return string|false The group on success, false on failure. |
|
5 | 336 |
*/ |
9 | 337 |
public function group( $file ) { |
338 |
$dir = $this->dirlist( $file ); |
|
16 | 339 |
|
9 | 340 |
return $dir[ $file ]['group']; |
0 | 341 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
|
5 | 343 |
/** |
9 | 344 |
* Copies a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* |
9 | 346 |
* @since 2.5.0 |
347 |
* |
|
348 |
* @param string $source Path to the source file. |
|
349 |
* @param string $destination Path to the destination file. |
|
350 |
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
|
351 |
* Default false. |
|
352 |
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
|
353 |
* 0755 for dirs. Default false. |
|
354 |
* @return bool True on success, false on failure. |
|
5 | 355 |
*/ |
9 | 356 |
public function copy( $source, $destination, $overwrite = false, $mode = false ) { |
357 |
if ( ! $overwrite && $this->exists( $destination ) ) { |
|
0 | 358 |
return false; |
9 | 359 |
} |
0 | 360 |
|
9 | 361 |
$content = $this->get_contents( $source ); |
16 | 362 |
|
9 | 363 |
if ( false === $content ) { |
0 | 364 |
return false; |
9 | 365 |
} |
0 | 366 |
|
9 | 367 |
return $this->put_contents( $destination, $content, $mode ); |
0 | 368 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
|
5 | 370 |
/** |
9 | 371 |
* Moves a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
* |
9 | 373 |
* @since 2.5.0 |
374 |
* |
|
375 |
* @param string $source Path to the source file. |
|
376 |
* @param string $destination Path to the destination file. |
|
377 |
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
|
378 |
* Default false. |
|
379 |
* @return bool True on success, false on failure. |
|
5 | 380 |
*/ |
9 | 381 |
public function move( $source, $destination, $overwrite = false ) { |
382 |
return $this->ftp->rename( $source, $destination ); |
|
0 | 383 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
|
5 | 385 |
/** |
9 | 386 |
* Deletes a file or directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
387 |
* |
9 | 388 |
* @since 2.5.0 |
389 |
* |
|
390 |
* @param string $file Path to the file or directory. |
|
16 | 391 |
* @param bool $recursive Optional. If set to true, deletes files and folders recursively. |
9 | 392 |
* Default false. |
393 |
* @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
|
394 |
* Default false. |
|
395 |
* @return bool True on success, false on failure. |
|
5 | 396 |
*/ |
9 | 397 |
public function delete( $file, $recursive = false, $type = false ) { |
398 |
if ( empty( $file ) ) { |
|
0 | 399 |
return false; |
9 | 400 |
} |
16 | 401 |
|
402 |
if ( 'f' === $type || $this->is_file( $file ) ) { |
|
9 | 403 |
return $this->ftp->delete( $file ); |
404 |
} |
|
16 | 405 |
|
9 | 406 |
if ( ! $recursive ) { |
407 |
return $this->ftp->rmdir( $file ); |
|
408 |
} |
|
0 | 409 |
|
9 | 410 |
return $this->ftp->mdel( $file ); |
0 | 411 |
} |
412 |
||
5 | 413 |
/** |
9 | 414 |
* Checks if a file or directory exists. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
415 |
* |
9 | 416 |
* @since 2.5.0 |
417 |
* |
|
418 |
* @param string $file Path to file or directory. |
|
419 |
* @return bool Whether $file exists or not. |
|
5 | 420 |
*/ |
421 |
public function exists( $file ) { |
|
0 | 422 |
$list = $this->ftp->nlist( $file ); |
5 | 423 |
|
424 |
if ( empty( $list ) && $this->is_dir( $file ) ) { |
|
425 |
return true; // File is an empty directory. |
|
426 |
} |
|
427 |
||
16 | 428 |
return ! empty( $list ); // Empty list = no file, so invert. |
5 | 429 |
// Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server. |
0 | 430 |
} |
431 |
||
5 | 432 |
/** |
9 | 433 |
* Checks if resource is a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
* |
9 | 435 |
* @since 2.5.0 |
436 |
* |
|
437 |
* @param string $file File path. |
|
438 |
* @return bool Whether $file is a file. |
|
5 | 439 |
*/ |
9 | 440 |
public function is_file( $file ) { |
441 |
if ( $this->is_dir( $file ) ) { |
|
0 | 442 |
return false; |
9 | 443 |
} |
16 | 444 |
|
9 | 445 |
if ( $this->exists( $file ) ) { |
0 | 446 |
return true; |
9 | 447 |
} |
16 | 448 |
|
0 | 449 |
return false; |
450 |
} |
|
451 |
||
5 | 452 |
/** |
9 | 453 |
* Checks if resource is a directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
454 |
* |
9 | 455 |
* @since 2.5.0 |
456 |
* |
|
457 |
* @param string $path Directory path. |
|
458 |
* @return bool Whether $path is a directory. |
|
5 | 459 |
*/ |
9 | 460 |
public function is_dir( $path ) { |
0 | 461 |
$cwd = $this->cwd(); |
16 | 462 |
|
9 | 463 |
if ( $this->chdir( $path ) ) { |
464 |
$this->chdir( $cwd ); |
|
0 | 465 |
return true; |
466 |
} |
|
16 | 467 |
|
0 | 468 |
return false; |
469 |
} |
|
470 |
||
5 | 471 |
/** |
9 | 472 |
* Checks if a file is readable. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
* |
9 | 474 |
* @since 2.5.0 |
475 |
* |
|
476 |
* @param string $file Path to file. |
|
477 |
* @return bool Whether $file is readable. |
|
5 | 478 |
*/ |
9 | 479 |
public function is_readable( $file ) { |
0 | 480 |
return true; |
481 |
} |
|
482 |
||
5 | 483 |
/** |
9 | 484 |
* Checks if a file or directory is writable. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
485 |
* |
9 | 486 |
* @since 2.5.0 |
487 |
* |
|
488 |
* @param string $file Path to file or directory. |
|
489 |
* @return bool Whether $file is writable. |
|
5 | 490 |
*/ |
9 | 491 |
public function is_writable( $file ) { |
0 | 492 |
return true; |
493 |
} |
|
494 |
||
5 | 495 |
/** |
9 | 496 |
* Gets the file's last access time. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
* |
9 | 498 |
* @since 2.5.0 |
499 |
* |
|
500 |
* @param string $file Path to file. |
|
501 |
* @return int|false Unix timestamp representing last access time, false on failure. |
|
5 | 502 |
*/ |
9 | 503 |
public function atime( $file ) { |
0 | 504 |
return false; |
505 |
} |
|
506 |
||
5 | 507 |
/** |
9 | 508 |
* Gets the file modification time. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
* |
9 | 510 |
* @since 2.5.0 |
511 |
* |
|
512 |
* @param string $file Path to file. |
|
513 |
* @return int|false Unix timestamp representing modification time, false on failure. |
|
5 | 514 |
*/ |
9 | 515 |
public function mtime( $file ) { |
516 |
return $this->ftp->mdtm( $file ); |
|
0 | 517 |
} |
518 |
||
5 | 519 |
/** |
9 | 520 |
* Gets the file size (in bytes). |
521 |
* |
|
522 |
* @since 2.5.0 |
|
523 |
* |
|
524 |
* @param string $file Path to file. |
|
525 |
* @return int|false Size of the file in bytes on success, false on failure. |
|
5 | 526 |
*/ |
9 | 527 |
public function size( $file ) { |
528 |
return $this->ftp->filesize( $file ); |
|
0 | 529 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
|
5 | 531 |
/** |
9 | 532 |
* 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
|
533 |
* |
9 | 534 |
* Note: If $file doesn't exist, it will be created. |
535 |
* |
|
536 |
* @since 2.5.0 |
|
537 |
* |
|
538 |
* @param string $file Path to file. |
|
539 |
* @param int $time Optional. Modified time to set for file. |
|
540 |
* Default 0. |
|
541 |
* @param int $atime Optional. Access time to set for file. |
|
542 |
* Default 0. |
|
543 |
* @return bool True on success, false on failure. |
|
5 | 544 |
*/ |
9 | 545 |
public function touch( $file, $time = 0, $atime = 0 ) { |
0 | 546 |
return false; |
547 |
} |
|
548 |
||
5 | 549 |
/** |
9 | 550 |
* Creates a directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
551 |
* |
9 | 552 |
* @since 2.5.0 |
553 |
* |
|
18 | 554 |
* @param string $path Path for new directory. |
555 |
* @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod). |
|
556 |
* Default false. |
|
557 |
* @param string|int|false $chown Optional. A user name or number (or false to skip chown). |
|
558 |
* Default false. |
|
559 |
* @param string|int|false $chgrp Optional. A group name or number (or false to skip chgrp). |
|
560 |
* Default false. |
|
9 | 561 |
* @return bool True on success, false on failure. |
5 | 562 |
*/ |
9 | 563 |
public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { |
564 |
$path = untrailingslashit( $path ); |
|
16 | 565 |
|
9 | 566 |
if ( empty( $path ) ) { |
0 | 567 |
return false; |
9 | 568 |
} |
0 | 569 |
|
9 | 570 |
if ( ! $this->ftp->mkdir( $path ) ) { |
0 | 571 |
return false; |
9 | 572 |
} |
16 | 573 |
|
9 | 574 |
if ( ! $chmod ) { |
0 | 575 |
$chmod = FS_CHMOD_DIR; |
9 | 576 |
} |
16 | 577 |
|
9 | 578 |
$this->chmod( $path, $chmod ); |
16 | 579 |
|
0 | 580 |
return true; |
581 |
} |
|
582 |
||
5 | 583 |
/** |
9 | 584 |
* Deletes a directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
* |
9 | 586 |
* @since 2.5.0 |
587 |
* |
|
588 |
* @param string $path Path to directory. |
|
589 |
* @param bool $recursive Optional. Whether to recursively remove files/directories. |
|
590 |
* Default false. |
|
591 |
* @return bool True on success, false on failure. |
|
5 | 592 |
*/ |
9 | 593 |
public function rmdir( $path, $recursive = false ) { |
594 |
return $this->delete( $path, $recursive ); |
|
0 | 595 |
} |
596 |
||
5 | 597 |
/** |
9 | 598 |
* 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
|
599 |
* |
9 | 600 |
* @since 2.5.0 |
601 |
* |
|
602 |
* @param string $path Path to directory or file. |
|
603 |
* @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. |
|
604 |
* Default true. |
|
605 |
* @param bool $recursive Optional. Whether to recursively include file details in nested directories. |
|
606 |
* Default false. |
|
607 |
* @return array|false { |
|
608 |
* Array of files. False if unable to list directory contents. |
|
609 |
* |
|
610 |
* @type string $name Name of the file or directory. |
|
611 |
* @type string $perms *nix representation of permissions. |
|
19 | 612 |
* @type string $permsn Octal representation of permissions. |
9 | 613 |
* @type string $owner Owner name or ID. |
614 |
* @type int $size Size of file in bytes. |
|
615 |
* @type int $lastmodunix Last modified unix timestamp. |
|
616 |
* @type mixed $lastmod Last modified month (3 letter) and day (without leading 0). |
|
617 |
* @type int $time Last modified time. |
|
618 |
* @type string $type Type of resource. 'f' for file, 'd' for directory. |
|
19 | 619 |
* @type mixed $files If a directory and `$recursive` is true, contains another array of files. |
9 | 620 |
* } |
5 | 621 |
*/ |
9 | 622 |
public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) { |
623 |
if ( $this->is_file( $path ) ) { |
|
624 |
$limit_file = basename( $path ); |
|
625 |
$path = dirname( $path ) . '/'; |
|
0 | 626 |
} else { |
627 |
$limit_file = false; |
|
628 |
} |
|
629 |
||
630 |
mbstring_binary_safe_encoding(); |
|
631 |
||
9 | 632 |
$list = $this->ftp->dirlist( $path ); |
16 | 633 |
|
0 | 634 |
if ( empty( $list ) && ! $this->exists( $path ) ) { |
635 |
||
636 |
reset_mbstring_encoding(); |
|
637 |
||
638 |
return false; |
|
639 |
} |
|
640 |
||
641 |
$ret = array(); |
|
16 | 642 |
|
0 | 643 |
foreach ( $list as $struc ) { |
644 |
||
16 | 645 |
if ( '.' === $struc['name'] || '..' === $struc['name'] ) { |
0 | 646 |
continue; |
9 | 647 |
} |
0 | 648 |
|
16 | 649 |
if ( ! $include_hidden && '.' === $struc['name'][0] ) { |
0 | 650 |
continue; |
9 | 651 |
} |
0 | 652 |
|
18 | 653 |
if ( $limit_file && $struc['name'] !== $limit_file ) { |
0 | 654 |
continue; |
9 | 655 |
} |
0 | 656 |
|
16 | 657 |
if ( 'd' === $struc['type'] ) { |
9 | 658 |
if ( $recursive ) { |
659 |
$struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); |
|
660 |
} else { |
|
0 | 661 |
$struc['files'] = array(); |
9 | 662 |
} |
0 | 663 |
} |
664 |
||
16 | 665 |
// Replace symlinks formatted as "source -> target" with just the source name. |
9 | 666 |
if ( $struc['islink'] ) { |
0 | 667 |
$struc['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $struc['name'] ); |
9 | 668 |
} |
0 | 669 |
|
16 | 670 |
// Add the octal representation of the file permissions. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
$struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
672 |
|
0 | 673 |
$ret[ $struc['name'] ] = $struc; |
674 |
} |
|
675 |
||
676 |
reset_mbstring_encoding(); |
|
677 |
||
678 |
return $ret; |
|
679 |
} |
|
680 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
681 |
/** |
9 | 682 |
* Destructor. |
683 |
* |
|
684 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
685 |
*/ |
5 | 686 |
public function __destruct() { |
0 | 687 |
$this->ftp->quit(); |
688 |
} |
|
689 |
} |