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