-
Notifications
You must be signed in to change notification settings - Fork 17
/
FtpConnectionProvider.php
110 lines (93 loc) · 3.13 KB
/
FtpConnectionProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace League\Flysystem\Ftp;
use const FTP_USEPASVADDRESS;
use function error_clear_last;
use function error_get_last;
class FtpConnectionProvider implements ConnectionProvider
{
/**
* @return resource
*
* @throws FtpConnectionException
*/
public function createConnection(FtpConnectionOptions $options)
{
$connection = $this->createConnectionResource(
$options->host(),
$options->port(),
$options->timeout(),
$options->ssl()
);
try {
$this->authenticate($options, $connection);
$this->enableUtf8Mode($options, $connection);
$this->ignorePassiveAddress($options, $connection);
$this->makeConnectionPassive($options, $connection);
} catch (FtpConnectionException $exception) {
@ftp_close($connection);
throw $exception;
}
return $connection;
}
/**
* @return resource
*/
private function createConnectionResource(string $host, int $port, int $timeout, bool $ssl)
{
error_clear_last();
$connection = $ssl ? @ftp_ssl_connect($host, $port, $timeout) : @ftp_connect($host, $port, $timeout);
if ($connection === false) {
throw UnableToConnectToFtpHost::forHost($host, $port, $ssl, error_get_last()['message'] ?? '');
}
return $connection;
}
/**
* @param resource $connection
*/
private function authenticate(FtpConnectionOptions $options, $connection): void
{
if ( ! @ftp_login($connection, $options->username(), $options->password())) {
throw new UnableToAuthenticate();
}
}
/**
* @param resource $connection
*/
private function enableUtf8Mode(FtpConnectionOptions $options, $connection): void
{
if ( ! $options->utf8()) {
return;
}
$response = @ftp_raw($connection, "OPTS UTF8 ON");
if ( ! in_array(substr($response[0], 0, 3), ['200', '202'])) {
throw new UnableToEnableUtf8Mode(
'Could not set UTF-8 mode for connection: ' . $options->host() . '::' . $options->port()
);
}
}
/**
* @param resource $connection
*/
private function ignorePassiveAddress(FtpConnectionOptions $options, $connection): void
{
$ignorePassiveAddress = $options->ignorePassiveAddress();
if ( ! is_bool($ignorePassiveAddress) || ! defined('FTP_USEPASVADDRESS')) {
return;
}
if ( ! @ftp_set_option($connection, FTP_USEPASVADDRESS, ! $ignorePassiveAddress)) {
throw UnableToSetFtpOption::whileSettingOption('FTP_USEPASVADDRESS');
}
}
/**
* @param resource $connection
*/
private function makeConnectionPassive(FtpConnectionOptions $options, $connection): void
{
if ( ! @ftp_pasv($connection, $options->passive())) {
throw new UnableToMakeConnectionPassive(
'Could not set passive mode for connection: ' . $options->host() . '::' . $options->port()
);
}
}
}