author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* WordPress FTP Filesystem. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Filesystem |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* WordPress Filesystem Class for implementing FTP. |
|
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_FTPext extends WP_Filesystem_Base { |
|
9 | 17 |
|
18 |
/** |
|
19 |
* @since 2.5.0 |
|
20 |
* @var resource |
|
21 |
*/ |
|
5 | 22 |
public $link; |
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 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
public function __construct( $opt = '' ) { |
0 | 32 |
$this->method = 'ftpext'; |
33 |
$this->errors = new WP_Error(); |
|
34 |
||
35 |
// Check if possible to use ftp functions. |
|
9 | 36 |
if ( ! extension_loaded( 'ftp' ) ) { |
37 |
$this->errors->add( 'no_ftp_ext', __( 'The ftp PHP extension is not available' ) ); |
|
5 | 38 |
return; |
0 | 39 |
} |
40 |
||
16 | 41 |
// This class uses the timeout on a per-connection basis, others use it on a per-action basis. |
9 | 42 |
if ( ! defined( 'FS_TIMEOUT' ) ) { |
43 |
define( 'FS_TIMEOUT', 240 ); |
|
44 |
} |
|
0 | 45 |
|
9 | 46 |
if ( empty( $opt['port'] ) ) { |
0 | 47 |
$this->options['port'] = 21; |
9 | 48 |
} else { |
0 | 49 |
$this->options['port'] = $opt['port']; |
9 | 50 |
} |
0 | 51 |
|
9 | 52 |
if ( empty( $opt['hostname'] ) ) { |
53 |
$this->errors->add( 'empty_hostname', __( 'FTP hostname is required' ) ); |
|
54 |
} else { |
|
0 | 55 |
$this->options['hostname'] = $opt['hostname']; |
9 | 56 |
} |
0 | 57 |
|
58 |
// Check if the options provided are OK. |
|
9 | 59 |
if ( empty( $opt['username'] ) ) { |
60 |
$this->errors->add( 'empty_username', __( 'FTP username is required' ) ); |
|
61 |
} else { |
|
0 | 62 |
$this->options['username'] = $opt['username']; |
9 | 63 |
} |
0 | 64 |
|
9 | 65 |
if ( empty( $opt['password'] ) ) { |
66 |
$this->errors->add( 'empty_password', __( 'FTP password is required' ) ); |
|
67 |
} else { |
|
0 | 68 |
$this->options['password'] = $opt['password']; |
9 | 69 |
} |
0 | 70 |
|
71 |
$this->options['ssl'] = false; |
|
16 | 72 |
|
73 |
if ( isset( $opt['connection_type'] ) && 'ftps' === $opt['connection_type'] ) { |
|
0 | 74 |
$this->options['ssl'] = true; |
9 | 75 |
} |
0 | 76 |
} |
77 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
/** |
9 | 79 |
* Connects filesystem. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
* |
9 | 81 |
* @since 2.5.0 |
82 |
* |
|
83 |
* @return bool True on success, false on failure. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
*/ |
5 | 85 |
public function connect() { |
9 | 86 |
if ( isset( $this->options['ssl'] ) && $this->options['ssl'] && function_exists( 'ftp_ssl_connect' ) ) { |
87 |
$this->link = @ftp_ssl_connect( $this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT ); |
|
88 |
} else { |
|
89 |
$this->link = @ftp_connect( $this->options['hostname'], $this->options['port'], FS_CONNECT_TIMEOUT ); |
|
90 |
} |
|
0 | 91 |
|
92 |
if ( ! $this->link ) { |
|
9 | 93 |
$this->errors->add( |
94 |
'connect', |
|
95 |
sprintf( |
|
16 | 96 |
/* translators: %s: hostname:port */ |
9 | 97 |
__( 'Failed to connect to FTP Server %s' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
$this->options['hostname'] . ':' . $this->options['port'] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
); |
16 | 101 |
|
0 | 102 |
return false; |
103 |
} |
|
104 |
||
9 | 105 |
if ( ! @ftp_login( $this->link, $this->options['username'], $this->options['password'] ) ) { |
106 |
$this->errors->add( |
|
107 |
'auth', |
|
108 |
sprintf( |
|
16 | 109 |
/* translators: %s: Username. */ |
9 | 110 |
__( 'Username/Password incorrect for %s' ), |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
111 |
$this->options['username'] |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
); |
16 | 114 |
|
0 | 115 |
return false; |
116 |
} |
|
117 |
||
16 | 118 |
// Set the connection to use Passive FTP. |
119 |
ftp_pasv( $this->link, true ); |
|
120 |
||
9 | 121 |
if ( @ftp_get_option( $this->link, FTP_TIMEOUT_SEC ) < FS_TIMEOUT ) { |
122 |
@ftp_set_option( $this->link, FTP_TIMEOUT_SEC, FS_TIMEOUT ); |
|
123 |
} |
|
0 | 124 |
|
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, |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* or if the file couldn't be retrieved. |
5 | 136 |
*/ |
137 |
public function get_contents( $file ) { |
|
16 | 138 |
$tempfile = wp_tempnam( $file ); |
139 |
$temphandle = fopen( $tempfile, 'w+' ); |
|
0 | 140 |
|
16 | 141 |
if ( ! $temphandle ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
unlink( $tempfile ); |
0 | 143 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
} |
0 | 145 |
|
16 | 146 |
if ( ! ftp_fget( $this->link, $temphandle, $file, FTP_BINARY ) ) { |
147 |
fclose( $temphandle ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
unlink( $tempfile ); |
0 | 149 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
} |
0 | 151 |
|
16 | 152 |
fseek( $temphandle, 0 ); // Skip back to the start of the file being written to. |
0 | 153 |
$contents = ''; |
154 |
||
16 | 155 |
while ( ! feof( $temphandle ) ) { |
156 |
$contents .= fread( $temphandle, 8 * KB_IN_BYTES ); |
|
9 | 157 |
} |
0 | 158 |
|
16 | 159 |
fclose( $temphandle ); |
9 | 160 |
unlink( $tempfile ); |
16 | 161 |
|
0 | 162 |
return $contents; |
163 |
} |
|
164 |
||
5 | 165 |
/** |
9 | 166 |
* Reads entire file into an array. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* |
9 | 168 |
* @since 2.5.0 |
169 |
* |
|
170 |
* @param string $file Path to the file. |
|
171 |
* @return array|false File contents in an array on success, false on failure. |
|
5 | 172 |
*/ |
9 | 173 |
public function get_contents_array( $file ) { |
174 |
return explode( "\n", $this->get_contents( $file ) ); |
|
0 | 175 |
} |
176 |
||
5 | 177 |
/** |
9 | 178 |
* Writes a string to a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
* |
9 | 180 |
* @since 2.5.0 |
181 |
* |
|
182 |
* @param string $file Remote path to the file where to write the data. |
|
183 |
* @param string $contents The data to write. |
|
184 |
* @param int|false $mode Optional. The file permissions as octal number, usually 0644. |
|
185 |
* Default false. |
|
186 |
* @return bool True on success, false on failure. |
|
5 | 187 |
*/ |
9 | 188 |
public function put_contents( $file, $contents, $mode = false ) { |
16 | 189 |
$tempfile = wp_tempnam( $file ); |
190 |
$temphandle = fopen( $tempfile, 'wb+' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
|
16 | 192 |
if ( ! $temphandle ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
unlink( $tempfile ); |
0 | 194 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
} |
0 | 196 |
|
197 |
mbstring_binary_safe_encoding(); |
|
198 |
||
9 | 199 |
$data_length = strlen( $contents ); |
16 | 200 |
$bytes_written = fwrite( $temphandle, $contents ); |
0 | 201 |
|
202 |
reset_mbstring_encoding(); |
|
203 |
||
204 |
if ( $data_length !== $bytes_written ) { |
|
16 | 205 |
fclose( $temphandle ); |
0 | 206 |
unlink( $tempfile ); |
207 |
return false; |
|
208 |
} |
|
209 |
||
16 | 210 |
fseek( $temphandle, 0 ); // Skip back to the start of the file being written to. |
0 | 211 |
|
16 | 212 |
$ret = ftp_fput( $this->link, $file, $temphandle, FTP_BINARY ); |
0 | 213 |
|
16 | 214 |
fclose( $temphandle ); |
9 | 215 |
unlink( $tempfile ); |
0 | 216 |
|
9 | 217 |
$this->chmod( $file, $mode ); |
0 | 218 |
|
219 |
return $ret; |
|
220 |
} |
|
221 |
||
5 | 222 |
/** |
9 | 223 |
* Gets the current working directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
* |
9 | 225 |
* @since 2.5.0 |
226 |
* |
|
227 |
* @return string|false The current working directory on success, false on failure. |
|
5 | 228 |
*/ |
229 |
public function cwd() { |
|
16 | 230 |
$cwd = ftp_pwd( $this->link ); |
231 |
||
9 | 232 |
if ( $cwd ) { |
233 |
$cwd = trailingslashit( $cwd ); |
|
234 |
} |
|
16 | 235 |
|
0 | 236 |
return $cwd; |
237 |
} |
|
238 |
||
5 | 239 |
/** |
9 | 240 |
* Changes current directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
* |
9 | 242 |
* @since 2.5.0 |
243 |
* |
|
244 |
* @param string $dir The new current directory. |
|
245 |
* @return bool True on success, false on failure. |
|
5 | 246 |
*/ |
9 | 247 |
public function chdir( $dir ) { |
248 |
return @ftp_chdir( $this->link, $dir ); |
|
0 | 249 |
} |
250 |
||
5 | 251 |
/** |
9 | 252 |
* Changes filesystem permissions. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
* |
9 | 254 |
* @since 2.5.0 |
255 |
* |
|
256 |
* @param string $file Path to the file. |
|
257 |
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
|
258 |
* 0755 for directories. Default false. |
|
16 | 259 |
* @param bool $recursive Optional. If set to true, changes file permissions recursively. |
9 | 260 |
* Default false. |
261 |
* @return bool True on success, false on failure. |
|
5 | 262 |
*/ |
9 | 263 |
public function chmod( $file, $mode = false, $recursive = false ) { |
0 | 264 |
if ( ! $mode ) { |
9 | 265 |
if ( $this->is_file( $file ) ) { |
0 | 266 |
$mode = FS_CHMOD_FILE; |
9 | 267 |
} elseif ( $this->is_dir( $file ) ) { |
0 | 268 |
$mode = FS_CHMOD_DIR; |
9 | 269 |
} else { |
0 | 270 |
return false; |
9 | 271 |
} |
0 | 272 |
} |
273 |
||
274 |
// chmod any sub-objects if recursive. |
|
9 | 275 |
if ( $recursive && $this->is_dir( $file ) ) { |
276 |
$filelist = $this->dirlist( $file ); |
|
16 | 277 |
|
9 | 278 |
foreach ( (array) $filelist as $filename => $filemeta ) { |
279 |
$this->chmod( $file . '/' . $filename, $mode, $recursive ); |
|
280 |
} |
|
0 | 281 |
} |
282 |
||
16 | 283 |
// chmod the file or directory. |
9 | 284 |
if ( ! function_exists( 'ftp_chmod' ) ) { |
16 | 285 |
return (bool) ftp_site( $this->link, sprintf( 'CHMOD %o %s', $mode, $file ) ); |
9 | 286 |
} |
16 | 287 |
|
288 |
return (bool) ftp_chmod( $this->link, $mode, $file ); |
|
0 | 289 |
} |
290 |
||
5 | 291 |
/** |
9 | 292 |
* Gets the file owner. |
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 $file Path to the file. |
|
297 |
* @return string|false Username of the owner on success, false on failure. |
|
5 | 298 |
*/ |
9 | 299 |
public function owner( $file ) { |
300 |
$dir = $this->dirlist( $file ); |
|
16 | 301 |
|
9 | 302 |
return $dir[ $file ]['owner']; |
0 | 303 |
} |
9 | 304 |
|
5 | 305 |
/** |
9 | 306 |
* Gets the permissions of the specified file or filepath in their octal format. |
307 |
* |
|
308 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
* |
9 | 310 |
* @param string $file Path to the file. |
311 |
* @return string Mode of the file (the last 3 digits). |
|
5 | 312 |
*/ |
9 | 313 |
public function getchmod( $file ) { |
314 |
$dir = $this->dirlist( $file ); |
|
16 | 315 |
|
9 | 316 |
return $dir[ $file ]['permsn']; |
0 | 317 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
318 |
|
5 | 319 |
/** |
9 | 320 |
* Gets the file's group. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
321 |
* |
9 | 322 |
* @since 2.5.0 |
323 |
* |
|
324 |
* @param string $file Path to the file. |
|
325 |
* @return string|false The group on success, false on failure. |
|
5 | 326 |
*/ |
9 | 327 |
public function group( $file ) { |
328 |
$dir = $this->dirlist( $file ); |
|
16 | 329 |
|
9 | 330 |
return $dir[ $file ]['group']; |
0 | 331 |
} |
332 |
||
5 | 333 |
/** |
9 | 334 |
* Copies a file. |
5 | 335 |
* |
9 | 336 |
* @since 2.5.0 |
337 |
* |
|
338 |
* @param string $source Path to the source file. |
|
339 |
* @param string $destination Path to the destination file. |
|
340 |
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
|
341 |
* Default false. |
|
342 |
* @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
|
343 |
* 0755 for dirs. Default false. |
|
344 |
* @return bool True on success, false on failure. |
|
5 | 345 |
*/ |
9 | 346 |
public function copy( $source, $destination, $overwrite = false, $mode = false ) { |
347 |
if ( ! $overwrite && $this->exists( $destination ) ) { |
|
0 | 348 |
return false; |
9 | 349 |
} |
16 | 350 |
|
9 | 351 |
$content = $this->get_contents( $source ); |
16 | 352 |
|
9 | 353 |
if ( false === $content ) { |
0 | 354 |
return false; |
9 | 355 |
} |
16 | 356 |
|
9 | 357 |
return $this->put_contents( $destination, $content, $mode ); |
0 | 358 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
|
5 | 360 |
/** |
9 | 361 |
* Moves a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
* |
9 | 363 |
* @since 2.5.0 |
364 |
* |
|
365 |
* @param string $source Path to the source file. |
|
366 |
* @param string $destination Path to the destination file. |
|
367 |
* @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
|
368 |
* Default false. |
|
369 |
* @return bool True on success, false on failure. |
|
5 | 370 |
*/ |
9 | 371 |
public function move( $source, $destination, $overwrite = false ) { |
372 |
return ftp_rename( $this->link, $source, $destination ); |
|
0 | 373 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
|
5 | 375 |
/** |
9 | 376 |
* Deletes a file or directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
* |
9 | 378 |
* @since 2.5.0 |
379 |
* |
|
380 |
* @param string $file Path to the file or directory. |
|
16 | 381 |
* @param bool $recursive Optional. If set to true, deletes files and folders recursively. |
9 | 382 |
* Default false. |
383 |
* @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
|
384 |
* Default false. |
|
385 |
* @return bool True on success, false on failure. |
|
5 | 386 |
*/ |
9 | 387 |
public function delete( $file, $recursive = false, $type = false ) { |
388 |
if ( empty( $file ) ) { |
|
0 | 389 |
return false; |
9 | 390 |
} |
16 | 391 |
|
392 |
if ( 'f' === $type || $this->is_file( $file ) ) { |
|
393 |
return ftp_delete( $this->link, $file ); |
|
9 | 394 |
} |
16 | 395 |
|
9 | 396 |
if ( ! $recursive ) { |
16 | 397 |
return ftp_rmdir( $this->link, $file ); |
9 | 398 |
} |
0 | 399 |
|
9 | 400 |
$filelist = $this->dirlist( trailingslashit( $file ) ); |
16 | 401 |
|
9 | 402 |
if ( ! empty( $filelist ) ) { |
403 |
foreach ( $filelist as $delete_file ) { |
|
404 |
$this->delete( trailingslashit( $file ) . $delete_file['name'], $recursive, $delete_file['type'] ); |
|
405 |
} |
|
406 |
} |
|
16 | 407 |
|
408 |
return ftp_rmdir( $this->link, $file ); |
|
0 | 409 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
410 |
|
5 | 411 |
/** |
9 | 412 |
* Checks if a file or directory exists. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
413 |
* |
9 | 414 |
* @since 2.5.0 |
415 |
* |
|
416 |
* @param string $file Path to file or directory. |
|
417 |
* @return bool Whether $file exists or not. |
|
5 | 418 |
*/ |
9 | 419 |
public function exists( $file ) { |
16 | 420 |
$list = ftp_nlist( $this->link, $file ); |
0 | 421 |
|
5 | 422 |
if ( empty( $list ) && $this->is_dir( $file ) ) { |
423 |
return true; // File is an empty directory. |
|
424 |
} |
|
425 |
||
16 | 426 |
return ! empty( $list ); // Empty list = no file, so invert. |
0 | 427 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
|
5 | 429 |
/** |
9 | 430 |
* Checks if resource is a file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
431 |
* |
9 | 432 |
* @since 2.5.0 |
433 |
* |
|
434 |
* @param string $file File path. |
|
435 |
* @return bool Whether $file is a file. |
|
5 | 436 |
*/ |
9 | 437 |
public function is_file( $file ) { |
438 |
return $this->exists( $file ) && ! $this->is_dir( $file ); |
|
0 | 439 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
440 |
|
5 | 441 |
/** |
9 | 442 |
* Checks if resource is a directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
443 |
* |
9 | 444 |
* @since 2.5.0 |
445 |
* |
|
446 |
* @param string $path Directory path. |
|
447 |
* @return bool Whether $path is a directory. |
|
5 | 448 |
*/ |
9 | 449 |
public function is_dir( $path ) { |
450 |
$cwd = $this->cwd(); |
|
451 |
$result = @ftp_chdir( $this->link, trailingslashit( $path ) ); |
|
16 | 452 |
|
18 | 453 |
if ( $result && $path === $this->cwd() || $this->cwd() !== $cwd ) { |
9 | 454 |
@ftp_chdir( $this->link, $cwd ); |
0 | 455 |
return true; |
456 |
} |
|
16 | 457 |
|
0 | 458 |
return false; |
459 |
} |
|
460 |
||
5 | 461 |
/** |
9 | 462 |
* Checks if a file is readable. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
* |
9 | 464 |
* @since 2.5.0 |
465 |
* |
|
466 |
* @param string $file Path to file. |
|
467 |
* @return bool Whether $file is readable. |
|
5 | 468 |
*/ |
9 | 469 |
public function is_readable( $file ) { |
0 | 470 |
return true; |
471 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
|
5 | 473 |
/** |
9 | 474 |
* Checks if a file or directory is writable. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
* |
9 | 476 |
* @since 2.5.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
* |
9 | 478 |
* @param string $file Path to file or directory. |
479 |
* @return bool Whether $file is writable. |
|
5 | 480 |
*/ |
9 | 481 |
public function is_writable( $file ) { |
0 | 482 |
return true; |
483 |
} |
|
484 |
||
5 | 485 |
/** |
9 | 486 |
* Gets the file's last access time. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
487 |
* |
9 | 488 |
* @since 2.5.0 |
489 |
* |
|
490 |
* @param string $file Path to file. |
|
491 |
* @return int|false Unix timestamp representing last access time, false on failure. |
|
5 | 492 |
*/ |
9 | 493 |
public function atime( $file ) { |
494 |
return false; |
|
495 |
} |
|
496 |
||
497 |
/** |
|
498 |
* Gets the file modification time. |
|
499 |
* |
|
500 |
* @since 2.5.0 |
|
501 |
* |
|
502 |
* @param string $file Path to file. |
|
503 |
* @return int|false Unix timestamp representing modification time, false on failure. |
|
504 |
*/ |
|
505 |
public function mtime( $file ) { |
|
506 |
return ftp_mdtm( $this->link, $file ); |
|
507 |
} |
|
508 |
||
509 |
/** |
|
510 |
* Gets the file size (in bytes). |
|
511 |
* |
|
512 |
* @since 2.5.0 |
|
513 |
* |
|
514 |
* @param string $file Path to file. |
|
515 |
* @return int|false Size of the file in bytes on success, false on failure. |
|
516 |
*/ |
|
517 |
public function size( $file ) { |
|
518 |
return ftp_size( $this->link, $file ); |
|
0 | 519 |
} |
520 |
||
5 | 521 |
/** |
9 | 522 |
* 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
|
523 |
* |
9 | 524 |
* Note: If $file doesn't exist, it will be created. |
525 |
* |
|
526 |
* @since 2.5.0 |
|
527 |
* |
|
528 |
* @param string $file Path to file. |
|
529 |
* @param int $time Optional. Modified time to set for file. |
|
530 |
* Default 0. |
|
531 |
* @param int $atime Optional. Access time to set for file. |
|
532 |
* Default 0. |
|
533 |
* @return bool True on success, false on failure. |
|
534 |
*/ |
|
535 |
public function touch( $file, $time = 0, $atime = 0 ) { |
|
536 |
return false; |
|
537 |
} |
|
538 |
||
539 |
/** |
|
540 |
* Creates a directory. |
|
541 |
* |
|
542 |
* @since 2.5.0 |
|
543 |
* |
|
18 | 544 |
* @param string $path Path for new directory. |
545 |
* @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod). |
|
546 |
* Default false. |
|
547 |
* @param string|int|false $chown Optional. A user name or number (or false to skip chown). |
|
548 |
* Default false. |
|
549 |
* @param string|int|false $chgrp Optional. A group name or number (or false to skip chgrp). |
|
550 |
* Default false. |
|
9 | 551 |
* @return bool True on success, false on failure. |
552 |
*/ |
|
553 |
public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { |
|
554 |
$path = untrailingslashit( $path ); |
|
16 | 555 |
|
9 | 556 |
if ( empty( $path ) ) { |
557 |
return false; |
|
558 |
} |
|
559 |
||
16 | 560 |
if ( ! ftp_mkdir( $this->link, $path ) ) { |
9 | 561 |
return false; |
562 |
} |
|
16 | 563 |
|
9 | 564 |
$this->chmod( $path, $chmod ); |
16 | 565 |
|
9 | 566 |
return true; |
567 |
} |
|
568 |
||
569 |
/** |
|
570 |
* Deletes a directory. |
|
571 |
* |
|
572 |
* @since 2.5.0 |
|
573 |
* |
|
574 |
* @param string $path Path to directory. |
|
575 |
* @param bool $recursive Optional. Whether to recursively remove files/directories. |
|
576 |
* Default false. |
|
577 |
* @return bool True on success, false on failure. |
|
578 |
*/ |
|
579 |
public function rmdir( $path, $recursive = false ) { |
|
580 |
return $this->delete( $path, $recursive ); |
|
581 |
} |
|
582 |
||
583 |
/** |
|
5 | 584 |
* @param string $line |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
585 |
* @return array |
5 | 586 |
*/ |
9 | 587 |
public function parselisting( $line ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
588 |
static $is_windows = null; |
16 | 589 |
|
9 | 590 |
if ( is_null( $is_windows ) ) { |
591 |
$is_windows = stripos( ftp_systype( $this->link ), 'win' ) !== false; |
|
592 |
} |
|
0 | 593 |
|
9 | 594 |
if ( $is_windows && preg_match( '/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/', $line, $lucifer ) ) { |
0 | 595 |
$b = array(); |
16 | 596 |
|
9 | 597 |
if ( $lucifer[3] < 70 ) { |
598 |
$lucifer[3] += 2000; |
|
599 |
} else { |
|
16 | 600 |
$lucifer[3] += 1900; // 4-digit year fix. |
9 | 601 |
} |
16 | 602 |
|
603 |
$b['isdir'] = ( '<DIR>' === $lucifer[7] ); |
|
604 |
||
9 | 605 |
if ( $b['isdir'] ) { |
0 | 606 |
$b['type'] = 'd'; |
9 | 607 |
} else { |
0 | 608 |
$b['type'] = 'f'; |
9 | 609 |
} |
16 | 610 |
|
9 | 611 |
$b['size'] = $lucifer[7]; |
612 |
$b['month'] = $lucifer[1]; |
|
613 |
$b['day'] = $lucifer[2]; |
|
614 |
$b['year'] = $lucifer[3]; |
|
615 |
$b['hour'] = $lucifer[4]; |
|
0 | 616 |
$b['minute'] = $lucifer[5]; |
18 | 617 |
$b['time'] = mktime( $lucifer[4] + ( strcasecmp( $lucifer[6], 'PM' ) === 0 ? 12 : 0 ), $lucifer[5], 0, $lucifer[1], $lucifer[2], $lucifer[3] ); |
9 | 618 |
$b['am/pm'] = $lucifer[6]; |
619 |
$b['name'] = $lucifer[8]; |
|
16 | 620 |
} elseif ( ! $is_windows ) { |
621 |
$lucifer = preg_split( '/[ ]/', $line, 9, PREG_SPLIT_NO_EMPTY ); |
|
622 |
||
623 |
if ( $lucifer ) { |
|
624 |
// echo $line."\n"; |
|
625 |
$lcount = count( $lucifer ); |
|
626 |
||
627 |
if ( $lcount < 8 ) { |
|
628 |
return ''; |
|
629 |
} |
|
630 |
||
631 |
$b = array(); |
|
632 |
$b['isdir'] = 'd' === $lucifer[0][0]; |
|
633 |
$b['islink'] = 'l' === $lucifer[0][0]; |
|
634 |
||
635 |
if ( $b['isdir'] ) { |
|
636 |
$b['type'] = 'd'; |
|
637 |
} elseif ( $b['islink'] ) { |
|
638 |
$b['type'] = 'l'; |
|
0 | 639 |
} else { |
16 | 640 |
$b['type'] = 'f'; |
0 | 641 |
} |
16 | 642 |
|
643 |
$b['perms'] = $lucifer[0]; |
|
644 |
$b['permsn'] = $this->getnumchmodfromh( $b['perms'] ); |
|
645 |
$b['number'] = $lucifer[1]; |
|
646 |
$b['owner'] = $lucifer[2]; |
|
647 |
$b['group'] = $lucifer[3]; |
|
648 |
$b['size'] = $lucifer[4]; |
|
649 |
||
18 | 650 |
if ( 8 === $lcount ) { |
16 | 651 |
sscanf( $lucifer[5], '%d-%d-%d', $b['year'], $b['month'], $b['day'] ); |
652 |
sscanf( $lucifer[6], '%d:%d', $b['hour'], $b['minute'] ); |
|
653 |
||
654 |
$b['time'] = mktime( $b['hour'], $b['minute'], 0, $b['month'], $b['day'], $b['year'] ); |
|
655 |
$b['name'] = $lucifer[7]; |
|
656 |
} else { |
|
657 |
$b['month'] = $lucifer[5]; |
|
658 |
$b['day'] = $lucifer[6]; |
|
659 |
||
660 |
if ( preg_match( '/([0-9]{2}):([0-9]{2})/', $lucifer[7], $l2 ) ) { |
|
661 |
$b['year'] = gmdate( 'Y' ); |
|
662 |
$b['hour'] = $l2[1]; |
|
663 |
$b['minute'] = $l2[2]; |
|
664 |
} else { |
|
665 |
$b['year'] = $lucifer[7]; |
|
666 |
$b['hour'] = 0; |
|
667 |
$b['minute'] = 0; |
|
668 |
} |
|
669 |
||
670 |
$b['time'] = strtotime( sprintf( '%d %s %d %02d:%02d', $b['day'], $b['month'], $b['year'], $b['hour'], $b['minute'] ) ); |
|
671 |
$b['name'] = $lucifer[8]; |
|
672 |
} |
|
0 | 673 |
} |
674 |
} |
|
675 |
||
16 | 676 |
// Replace symlinks formatted as "source -> target" with just the source name. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
677 |
if ( isset( $b['islink'] ) && $b['islink'] ) { |
0 | 678 |
$b['name'] = preg_replace( '/(\s*->\s*.*)$/', '', $b['name'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
679 |
} |
0 | 680 |
|
681 |
return $b; |
|
682 |
} |
|
683 |
||
5 | 684 |
/** |
9 | 685 |
* 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
|
686 |
* |
9 | 687 |
* @since 2.5.0 |
688 |
* |
|
689 |
* @param string $path Path to directory or file. |
|
690 |
* @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. |
|
691 |
* Default true. |
|
692 |
* @param bool $recursive Optional. Whether to recursively include file details in nested directories. |
|
693 |
* Default false. |
|
694 |
* @return array|false { |
|
695 |
* Array of files. False if unable to list directory contents. |
|
696 |
* |
|
697 |
* @type string $name Name of the file or directory. |
|
698 |
* @type string $perms *nix representation of permissions. |
|
699 |
* @type int $permsn Octal representation of permissions. |
|
700 |
* @type string $owner Owner name or ID. |
|
701 |
* @type int $size Size of file in bytes. |
|
702 |
* @type int $lastmodunix Last modified unix timestamp. |
|
703 |
* @type mixed $lastmod Last modified month (3 letter) and day (without leading 0). |
|
704 |
* @type int $time Last modified time. |
|
705 |
* @type string $type Type of resource. 'f' for file, 'd' for directory. |
|
706 |
* @type mixed $files If a directory and $recursive is true, contains another array of files. |
|
707 |
* } |
|
5 | 708 |
*/ |
9 | 709 |
public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) { |
710 |
if ( $this->is_file( $path ) ) { |
|
711 |
$limit_file = basename( $path ); |
|
712 |
$path = dirname( $path ) . '/'; |
|
0 | 713 |
} else { |
714 |
$limit_file = false; |
|
715 |
} |
|
716 |
||
16 | 717 |
$pwd = ftp_pwd( $this->link ); |
718 |
||
9 | 719 |
if ( ! @ftp_chdir( $this->link, $path ) ) { // Can't change to folder = folder doesn't exist. |
0 | 720 |
return false; |
9 | 721 |
} |
16 | 722 |
|
723 |
$list = ftp_rawlist( $this->link, '-a', false ); |
|
724 |
||
9 | 725 |
@ftp_chdir( $this->link, $pwd ); |
0 | 726 |
|
9 | 727 |
if ( empty( $list ) ) { // Empty array = non-existent folder (real folder will show . at least). |
0 | 728 |
return false; |
9 | 729 |
} |
0 | 730 |
|
731 |
$dirlist = array(); |
|
16 | 732 |
|
0 | 733 |
foreach ( $list as $k => $v ) { |
9 | 734 |
$entry = $this->parselisting( $v ); |
16 | 735 |
|
9 | 736 |
if ( empty( $entry ) ) { |
0 | 737 |
continue; |
9 | 738 |
} |
0 | 739 |
|
16 | 740 |
if ( '.' === $entry['name'] || '..' === $entry['name'] ) { |
0 | 741 |
continue; |
9 | 742 |
} |
0 | 743 |
|
16 | 744 |
if ( ! $include_hidden && '.' === $entry['name'][0] ) { |
0 | 745 |
continue; |
9 | 746 |
} |
0 | 747 |
|
18 | 748 |
if ( $limit_file && $entry['name'] !== $limit_file ) { |
0 | 749 |
continue; |
9 | 750 |
} |
0 | 751 |
|
752 |
$dirlist[ $entry['name'] ] = $entry; |
|
753 |
} |
|
754 |
||
755 |
$ret = array(); |
|
16 | 756 |
|
9 | 757 |
foreach ( (array) $dirlist as $struc ) { |
16 | 758 |
if ( 'd' === $struc['type'] ) { |
9 | 759 |
if ( $recursive ) { |
760 |
$struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); |
|
761 |
} else { |
|
0 | 762 |
$struc['files'] = array(); |
9 | 763 |
} |
0 | 764 |
} |
765 |
||
766 |
$ret[ $struc['name'] ] = $struc; |
|
767 |
} |
|
16 | 768 |
|
0 | 769 |
return $ret; |
770 |
} |
|
771 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
772 |
/** |
9 | 773 |
* Destructor. |
774 |
* |
|
775 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
776 |
*/ |
5 | 777 |
public function __destruct() { |
9 | 778 |
if ( $this->link ) { |
779 |
ftp_close( $this->link ); |
|
780 |
} |
|
0 | 781 |
} |
782 |
} |