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