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