| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_DEFENDER_VENDOR\Safe; |
| 4 |
|
| 5 |
use WP_DEFENDER_VENDOR\Safe\Exceptions\FtpException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Sends an ALLO command to the remote FTP server to |
| 9 |
* allocate space for a file to be uploaded. |
| 10 |
* |
| 11 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 12 |
* @param int $filesize The number of bytes to allocate. |
| 13 |
* @param string|null $result A textual representation of the servers response will be returned by |
| 14 |
* reference in result if a variable is provided. |
| 15 |
* |
| 16 |
* @throws FtpException |
| 17 |
* |
| 18 |
*/ |
| 19 |
function ftp_alloc($ftp_stream, int $filesize, ?string &$result = null): void |
| 20 |
{ |
| 21 |
error_clear_last(); |
| 22 |
$result = \ftp_alloc($ftp_stream, $filesize, $result); |
| 23 |
if ($result === false) { |
| 24 |
throw FtpException::createFromPhpError(); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* |
| 31 |
* |
| 32 |
* @param resource $ftp |
| 33 |
* @param string $remote_file |
| 34 |
* @param string $local_file |
| 35 |
* @param int $mode |
| 36 |
* @throws FtpException |
| 37 |
* |
| 38 |
*/ |
| 39 |
function ftp_append($ftp, string $remote_file, string $local_file, int $mode = FTP_BINARY): void |
| 40 |
{ |
| 41 |
error_clear_last(); |
| 42 |
$result = \ftp_append($ftp, $remote_file, $local_file, $mode); |
| 43 |
if ($result === false) { |
| 44 |
throw FtpException::createFromPhpError(); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
/** |
| 50 |
* Changes to the parent directory. |
| 51 |
* |
| 52 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 53 |
* @throws FtpException |
| 54 |
* |
| 55 |
*/ |
| 56 |
function ftp_cdup($ftp_stream): void |
| 57 |
{ |
| 58 |
error_clear_last(); |
| 59 |
$result = \ftp_cdup($ftp_stream); |
| 60 |
if ($result === false) { |
| 61 |
throw FtpException::createFromPhpError(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Changes the current directory to the specified one. |
| 68 |
* |
| 69 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 70 |
* @param string $directory The target directory. |
| 71 |
* @throws FtpException |
| 72 |
* |
| 73 |
*/ |
| 74 |
function ftp_chdir($ftp_stream, string $directory): void |
| 75 |
{ |
| 76 |
error_clear_last(); |
| 77 |
$result = \ftp_chdir($ftp_stream, $directory); |
| 78 |
if ($result === false) { |
| 79 |
throw FtpException::createFromPhpError(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* Sets the permissions on the specified remote file to |
| 86 |
* mode. |
| 87 |
* |
| 88 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 89 |
* @param int $mode The new permissions, given as an octal value. |
| 90 |
* @param string $filename The remote file. |
| 91 |
* @return int Returns the new file permissions on success. |
| 92 |
* @throws FtpException |
| 93 |
* |
| 94 |
*/ |
| 95 |
function ftp_chmod($ftp_stream, int $mode, string $filename): int |
| 96 |
{ |
| 97 |
error_clear_last(); |
| 98 |
$result = \ftp_chmod($ftp_stream, $mode, $filename); |
| 99 |
if ($result === false) { |
| 100 |
throw FtpException::createFromPhpError(); |
| 101 |
} |
| 102 |
return $result; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* ftp_close closes the given link identifier |
| 108 |
* and releases the resource. |
| 109 |
* |
| 110 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 111 |
* @throws FtpException |
| 112 |
* |
| 113 |
*/ |
| 114 |
function ftp_close($ftp_stream): void |
| 115 |
{ |
| 116 |
error_clear_last(); |
| 117 |
$result = \ftp_close($ftp_stream); |
| 118 |
if ($result === false) { |
| 119 |
throw FtpException::createFromPhpError(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* ftp_connect opens an FTP connection to the |
| 126 |
* specified host. |
| 127 |
* |
| 128 |
* @param string $host The FTP server address. This parameter shouldn't have any trailing |
| 129 |
* slashes and shouldn't be prefixed with ftp://. |
| 130 |
* @param int $port This parameter specifies an alternate port to connect to. If it is |
| 131 |
* omitted or set to zero, then the default FTP port, 21, will be used. |
| 132 |
* @param int $timeout This parameter specifies the timeout in seconds for all subsequent network operations. |
| 133 |
* If omitted, the default value is 90 seconds. The timeout can be changed and |
| 134 |
* queried at any time with ftp_set_option and |
| 135 |
* ftp_get_option. |
| 136 |
* @return resource Returns a FTP stream on success. |
| 137 |
* @throws FtpException |
| 138 |
* |
| 139 |
*/ |
| 140 |
function ftp_connect(string $host, int $port = 21, int $timeout = 90) |
| 141 |
{ |
| 142 |
error_clear_last(); |
| 143 |
$result = \ftp_connect($host, $port, $timeout); |
| 144 |
if ($result === false) { |
| 145 |
throw FtpException::createFromPhpError(); |
| 146 |
} |
| 147 |
return $result; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* ftp_delete deletes the file specified by |
| 153 |
* path from the FTP server. |
| 154 |
* |
| 155 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 156 |
* @param string $path The file to delete. |
| 157 |
* @throws FtpException |
| 158 |
* |
| 159 |
*/ |
| 160 |
function ftp_delete($ftp_stream, string $path): void |
| 161 |
{ |
| 162 |
error_clear_last(); |
| 163 |
$result = \ftp_delete($ftp_stream, $path); |
| 164 |
if ($result === false) { |
| 165 |
throw FtpException::createFromPhpError(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* ftp_fget retrieves remote_file |
| 172 |
* from the FTP server, and writes it to the given file pointer. |
| 173 |
* |
| 174 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 175 |
* @param resource $handle An open file pointer in which we store the data. |
| 176 |
* @param string $remote_file The remote file path. |
| 177 |
* @param int $mode The transfer mode. Must be either FTP_ASCII or |
| 178 |
* FTP_BINARY. |
| 179 |
* @param int $resumepos The position in the remote file to start downloading from. |
| 180 |
* @throws FtpException |
| 181 |
* |
| 182 |
*/ |
| 183 |
function ftp_fget($ftp_stream, $handle, string $remote_file, int $mode = FTP_BINARY, int $resumepos = 0): void |
| 184 |
{ |
| 185 |
error_clear_last(); |
| 186 |
$result = \ftp_fget($ftp_stream, $handle, $remote_file, $mode, $resumepos); |
| 187 |
if ($result === false) { |
| 188 |
throw FtpException::createFromPhpError(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* ftp_fput uploads the data from a file pointer |
| 195 |
* to a remote file on the FTP server. |
| 196 |
* |
| 197 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 198 |
* @param string $remote_file The remote file path. |
| 199 |
* @param resource $handle An open file pointer on the local file. Reading stops at end of file. |
| 200 |
* @param int $mode The transfer mode. Must be either FTP_ASCII or |
| 201 |
* FTP_BINARY. |
| 202 |
* @param int $startpos The position in the remote file to start uploading to. |
| 203 |
* @throws FtpException |
| 204 |
* |
| 205 |
*/ |
| 206 |
function ftp_fput($ftp_stream, string $remote_file, $handle, int $mode = FTP_BINARY, int $startpos = 0): void |
| 207 |
{ |
| 208 |
error_clear_last(); |
| 209 |
$result = \ftp_fput($ftp_stream, $remote_file, $handle, $mode, $startpos); |
| 210 |
if ($result === false) { |
| 211 |
throw FtpException::createFromPhpError(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* ftp_get retrieves a remote file from the FTP server, |
| 218 |
* and saves it into a local file. |
| 219 |
* |
| 220 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 221 |
* @param string $local_file The local file path (will be overwritten if the file already exists). |
| 222 |
* @param string $remote_file The remote file path. |
| 223 |
* @param int $mode The transfer mode. Must be either FTP_ASCII or |
| 224 |
* FTP_BINARY. |
| 225 |
* @param int $resumepos The position in the remote file to start downloading from. |
| 226 |
* @throws FtpException |
| 227 |
* |
| 228 |
*/ |
| 229 |
function ftp_get($ftp_stream, string $local_file, string $remote_file, int $mode = FTP_BINARY, int $resumepos = 0): void |
| 230 |
{ |
| 231 |
error_clear_last(); |
| 232 |
$result = \ftp_get($ftp_stream, $local_file, $remote_file, $mode, $resumepos); |
| 233 |
if ($result === false) { |
| 234 |
throw FtpException::createFromPhpError(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Logs in to the given FTP stream. |
| 241 |
* |
| 242 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 243 |
* @param string $username The username (USER). |
| 244 |
* @param string $password The password (PASS). |
| 245 |
* @throws FtpException |
| 246 |
* |
| 247 |
*/ |
| 248 |
function ftp_login($ftp_stream, string $username, string $password): void |
| 249 |
{ |
| 250 |
error_clear_last(); |
| 251 |
$result = \ftp_login($ftp_stream, $username, $password); |
| 252 |
if ($result === false) { |
| 253 |
throw FtpException::createFromPhpError(); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* Creates the specified directory on the FTP server. |
| 260 |
* |
| 261 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 262 |
* @param string $directory The name of the directory that will be created. |
| 263 |
* @return string Returns the newly created directory name on success. |
| 264 |
* @throws FtpException |
| 265 |
* |
| 266 |
*/ |
| 267 |
function ftp_mkdir($ftp_stream, string $directory): string |
| 268 |
{ |
| 269 |
error_clear_last(); |
| 270 |
$result = \ftp_mkdir($ftp_stream, $directory); |
| 271 |
if ($result === false) { |
| 272 |
throw FtpException::createFromPhpError(); |
| 273 |
} |
| 274 |
return $result; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* |
| 280 |
* |
| 281 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 282 |
* @param string $directory The directory to be listed. |
| 283 |
* @return array Returns an array of arrays with file infos from the specified directory on success. |
| 284 |
* @throws FtpException |
| 285 |
* |
| 286 |
*/ |
| 287 |
function ftp_mlsd($ftp_stream, string $directory): array |
| 288 |
{ |
| 289 |
error_clear_last(); |
| 290 |
$result = \ftp_mlsd($ftp_stream, $directory); |
| 291 |
if ($result === false) { |
| 292 |
throw FtpException::createFromPhpError(); |
| 293 |
} |
| 294 |
return $result; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
/** |
| 299 |
* |
| 300 |
* |
| 301 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 302 |
* @param string $directory The directory to be listed. This parameter can also include arguments, eg. |
| 303 |
* ftp_nlist($conn_id, "-la /your/dir"); |
| 304 |
* Note that this parameter isn't escaped so there may be some issues with |
| 305 |
* filenames containing spaces and other characters. |
| 306 |
* @return array Returns an array of filenames from the specified directory on success. |
| 307 |
* @throws FtpException |
| 308 |
* |
| 309 |
*/ |
| 310 |
function ftp_nlist($ftp_stream, string $directory): array |
| 311 |
{ |
| 312 |
error_clear_last(); |
| 313 |
$result = \ftp_nlist($ftp_stream, $directory); |
| 314 |
if ($result === false) { |
| 315 |
throw FtpException::createFromPhpError(); |
| 316 |
} |
| 317 |
return $result; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
/** |
| 322 |
* ftp_pasv turns on or off passive mode. In |
| 323 |
* passive mode, data connections are initiated by the client, |
| 324 |
* rather than by the server. |
| 325 |
* It may be needed if the client is behind firewall. |
| 326 |
* |
| 327 |
* Please note that ftp_pasv can only be called after a |
| 328 |
* successful login or otherwise it will fail. |
| 329 |
* |
| 330 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 331 |
* @param bool $pasv If TRUE, the passive mode is turned on, else it's turned off. |
| 332 |
* @throws FtpException |
| 333 |
* |
| 334 |
*/ |
| 335 |
function ftp_pasv($ftp_stream, bool $pasv): void |
| 336 |
{ |
| 337 |
error_clear_last(); |
| 338 |
$result = \ftp_pasv($ftp_stream, $pasv); |
| 339 |
if ($result === false) { |
| 340 |
throw FtpException::createFromPhpError(); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
/** |
| 346 |
* ftp_put stores a local file on the FTP server. |
| 347 |
* |
| 348 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 349 |
* @param string $remote_file The remote file path. |
| 350 |
* @param string $local_file The local file path. |
| 351 |
* @param int $mode The transfer mode. Must be either FTP_ASCII or |
| 352 |
* FTP_BINARY. |
| 353 |
* @param int $startpos The position in the remote file to start uploading to. |
| 354 |
* @throws FtpException |
| 355 |
* |
| 356 |
*/ |
| 357 |
function ftp_put($ftp_stream, string $remote_file, string $local_file, int $mode = FTP_BINARY, int $startpos = 0): void |
| 358 |
{ |
| 359 |
error_clear_last(); |
| 360 |
$result = \ftp_put($ftp_stream, $remote_file, $local_file, $mode, $startpos); |
| 361 |
if ($result === false) { |
| 362 |
throw FtpException::createFromPhpError(); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* |
| 369 |
* |
| 370 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 371 |
* @return string Returns the current directory name. |
| 372 |
* @throws FtpException |
| 373 |
* |
| 374 |
*/ |
| 375 |
function ftp_pwd($ftp_stream): string |
| 376 |
{ |
| 377 |
error_clear_last(); |
| 378 |
$result = \ftp_pwd($ftp_stream); |
| 379 |
if ($result === false) { |
| 380 |
throw FtpException::createFromPhpError(); |
| 381 |
} |
| 382 |
return $result; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
/** |
| 387 |
* ftp_rename renames a file or a directory on the FTP |
| 388 |
* server. |
| 389 |
* |
| 390 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 391 |
* @param string $oldname The old file/directory name. |
| 392 |
* @param string $newname The new name. |
| 393 |
* @throws FtpException |
| 394 |
* |
| 395 |
*/ |
| 396 |
function ftp_rename($ftp_stream, string $oldname, string $newname): void |
| 397 |
{ |
| 398 |
error_clear_last(); |
| 399 |
$result = \ftp_rename($ftp_stream, $oldname, $newname); |
| 400 |
if ($result === false) { |
| 401 |
throw FtpException::createFromPhpError(); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* Removes the specified directory on the FTP server. |
| 408 |
* |
| 409 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 410 |
* @param string $directory The directory to delete. This must be either an absolute or relative |
| 411 |
* path to an empty directory. |
| 412 |
* @throws FtpException |
| 413 |
* |
| 414 |
*/ |
| 415 |
function ftp_rmdir($ftp_stream, string $directory): void |
| 416 |
{ |
| 417 |
error_clear_last(); |
| 418 |
$result = \ftp_rmdir($ftp_stream, $directory); |
| 419 |
if ($result === false) { |
| 420 |
throw FtpException::createFromPhpError(); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
/** |
| 426 |
* ftp_site sends the given SITE |
| 427 |
* command to the FTP server. |
| 428 |
* |
| 429 |
* SITE commands are not standardized, and vary from server |
| 430 |
* to server. They are useful for handling such things as file permissions and |
| 431 |
* group membership. |
| 432 |
* |
| 433 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 434 |
* @param string $command The SITE command. Note that this parameter isn't escaped so there may |
| 435 |
* be some issues with filenames containing spaces and other characters. |
| 436 |
* @throws FtpException |
| 437 |
* |
| 438 |
*/ |
| 439 |
function ftp_site($ftp_stream, string $command): void |
| 440 |
{ |
| 441 |
error_clear_last(); |
| 442 |
$result = \ftp_site($ftp_stream, $command); |
| 443 |
if ($result === false) { |
| 444 |
throw FtpException::createFromPhpError(); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
/** |
| 450 |
* ftp_ssl_connect opens an explicit SSL-FTP connection to the |
| 451 |
* specified host. That implies that |
| 452 |
* ftp_ssl_connect will succeed even if the server is not |
| 453 |
* configured for SSL-FTP, or its certificate is invalid. Only when |
| 454 |
* ftp_login is called, the client will send the |
| 455 |
* appropriate AUTH FTP command, so ftp_login will fail in |
| 456 |
* the mentioned cases. |
| 457 |
* |
| 458 |
* @param string $host The FTP server address. This parameter shouldn't have any trailing |
| 459 |
* slashes and shouldn't be prefixed with ftp://. |
| 460 |
* @param int $port This parameter specifies an alternate port to connect to. If it is |
| 461 |
* omitted or set to zero, then the default FTP port, 21, will be used. |
| 462 |
* @param int $timeout This parameter specifies the timeout for all subsequent network operations. |
| 463 |
* If omitted, the default value is 90 seconds. The timeout can be changed and |
| 464 |
* queried at any time with ftp_set_option and |
| 465 |
* ftp_get_option. |
| 466 |
* @return resource Returns a SSL-FTP stream on success. |
| 467 |
* @throws FtpException |
| 468 |
* |
| 469 |
*/ |
| 470 |
function ftp_ssl_connect(string $host, int $port = 21, int $timeout = 90) |
| 471 |
{ |
| 472 |
error_clear_last(); |
| 473 |
$result = \ftp_ssl_connect($host, $port, $timeout); |
| 474 |
if ($result === false) { |
| 475 |
throw FtpException::createFromPhpError(); |
| 476 |
} |
| 477 |
return $result; |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
/** |
| 482 |
* Returns the system type identifier of the remote FTP server. |
| 483 |
* |
| 484 |
* @param resource $ftp_stream The link identifier of the FTP connection. |
| 485 |
* @return string Returns the remote system type. |
| 486 |
* @throws FtpException |
| 487 |
* |
| 488 |
*/ |
| 489 |
function ftp_systype($ftp_stream): string |
| 490 |
{ |
| 491 |
error_clear_last(); |
| 492 |
$result = \ftp_systype($ftp_stream); |
| 493 |
if ($result === false) { |
| 494 |
throw FtpException::createFromPhpError(); |
| 495 |
} |
| 496 |
return $result; |
| 497 |
} |
| 498 |
|