1
|
|
|
<?php |
2
|
|
|
namespace FMUP\Ftp\Driver; |
3
|
|
|
|
4
|
|
|
use FMUP\Ftp\FtpAbstract; |
5
|
|
|
use FMUP\Ftp\Exception as FtpException; |
6
|
|
|
use FMUP\Logger; |
7
|
|
|
|
8
|
|
|
class Ftp extends FtpAbstract |
9
|
|
|
{ |
10
|
|
|
const TIMEOUT = 'timeout'; |
11
|
|
|
const MODE = 'mode'; |
12
|
|
|
const RESUME_POS = 'resumepos'; |
13
|
|
|
const START_POS = 'startpos'; |
14
|
|
|
const PASSIVE_MODE = 'passive_mode'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Connection timeout in second |
18
|
|
|
* @return int |
19
|
|
|
*/ |
20
|
2 |
|
protected function getTimeout() |
21
|
|
|
{ |
22
|
2 |
|
return isset($this->settings[self::TIMEOUT]) ? $this->settings[self::TIMEOUT] : 90; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Transfer mode (FTP_ASCII | FTP_BINARY) |
27
|
|
|
* @return int |
28
|
|
|
*/ |
29
|
1 |
|
protected function getMode() |
30
|
|
|
{ |
31
|
1 |
|
return isset($this->settings[self::MODE]) ? $this->settings[self::MODE] : FTP_ASCII; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Begin download position in remote file |
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
1 |
|
protected function getResumePos() |
39
|
|
|
{ |
40
|
1 |
|
return isset($this->settings[self::RESUME_POS]) ? $this->settings[self::RESUME_POS] : 0; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Begin upload position in local file to put in remote file |
45
|
|
|
* @return int |
46
|
|
|
*/ |
47
|
1 |
|
protected function getStartPos() |
48
|
|
|
{ |
49
|
1 |
|
return isset($this->settings[self::START_POS]) ? $this->settings[self::START_POS] : 0; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Begin upload position in local file to put in remote file |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
1 |
|
protected function getPassiveMode() |
57
|
|
|
{ |
58
|
1 |
|
return isset($this->settings[self::PASSIVE_MODE]) ? $this->settings[self::PASSIVE_MODE] : true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $host |
63
|
|
|
* @param int $port |
64
|
|
|
* @return $this |
65
|
|
|
* @throws FtpException |
66
|
|
|
*/ |
67
|
1 |
|
public function connect($host, $port = 21) |
68
|
|
|
{ |
69
|
|
|
|
70
|
1 |
|
$this->setSession($this->ftpConnect($host, $port, $this->getTimeout())); |
71
|
1 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $host |
76
|
|
|
* @param int $port |
77
|
|
|
* @param int $timeout |
78
|
|
|
* @return resource |
79
|
|
|
* @codeCoverageIgnore |
80
|
|
|
*/ |
81
|
|
|
protected function ftpConnect($host, $port = 21, $timeout = 90) |
82
|
|
|
{ |
83
|
|
|
return ftp_connect($host, $port, $timeout); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $user |
88
|
|
|
* @param string $pass |
89
|
|
|
* @return bool |
90
|
|
|
* @throws FtpException |
91
|
|
|
*/ |
92
|
2 |
|
public function login($user, $pass) |
93
|
|
|
{ |
94
|
2 |
|
$ret = $this->ftpLogin($this->getSession(), $user, $pass); |
95
|
2 |
|
if (!$ret) { |
96
|
1 |
|
$this->log(Logger::ERROR, "Unable to login to FTP server", (array)$this->getSettings()); |
97
|
1 |
|
throw new FtpException('Unable to login to the FTP server'); |
98
|
|
|
} |
99
|
1 |
|
$this->ftpPasv($this->getSession(), $this->getPassiveMode()); |
|
|
|
|
100
|
1 |
|
return $ret; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param resource $ftpStream |
105
|
|
|
* @param string $username |
106
|
|
|
* @param string $password |
107
|
|
|
* @return bool |
108
|
|
|
* @codeCoverageIgnore |
109
|
|
|
*/ |
110
|
|
|
protected function ftpLogin($ftpStream, $username, $password) |
111
|
|
|
{ |
112
|
|
|
return ftp_login($ftpStream, $username, $password); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $localFile |
117
|
|
|
* @param string $remoteFile |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
1 |
|
public function get($localFile, $remoteFile) |
121
|
|
|
{ |
122
|
1 |
|
return $this->ftpGet($this->getSession(), $localFile, $remoteFile, $this->getMode(), $this->getResumePos()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Put file on ftp server |
127
|
|
|
* @param string $remoteFile |
128
|
|
|
* @param string $localFile |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
1 |
|
public function put($remoteFile, $localFile) |
132
|
|
|
{ |
133
|
1 |
|
return $this->ftpPut($this->getSession(), $remoteFile, $localFile, $this->getMode(), $this->getStartPos()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param resource $ftpStream |
138
|
|
|
* @param string $localFile |
139
|
|
|
* @param string $remoteFile |
140
|
|
|
* @param int $mode |
141
|
|
|
* @param int $resumePos |
142
|
|
|
* @return bool |
143
|
|
|
* @codeCoverageIgnore |
144
|
|
|
*/ |
145
|
|
|
protected function ftpGet($ftpStream, $localFile, $remoteFile, $mode, $resumePos) |
146
|
|
|
{ |
147
|
|
|
return ftp_get($ftpStream, $localFile, $remoteFile, $mode, $resumePos); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param resource $ftpStream |
152
|
|
|
* @param string $remoteFile |
153
|
|
|
* @param string $localFile |
154
|
|
|
* @param int $mode |
155
|
|
|
* @param int $startpos |
156
|
|
|
* @return bool |
157
|
|
|
* @codeCoverageIgnore |
158
|
|
|
*/ |
159
|
|
|
protected function ftpPut($ftpStream, $remoteFile, $localFile, $mode, $startpos) |
160
|
|
|
{ |
161
|
|
|
return ftp_put($ftpStream, $remoteFile, $localFile, $mode, $startpos); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $file |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
1 |
|
public function delete($file) |
169
|
|
|
{ |
170
|
1 |
|
return $this->ftpDelete($this->getSession(), $file); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param resource $ftpStream |
175
|
|
|
* @param string $path |
176
|
|
|
* @return bool |
177
|
|
|
* @codeCoverageIgnore |
178
|
|
|
*/ |
179
|
|
|
protected function ftpDelete($ftpStream, $path) |
180
|
|
|
{ |
181
|
|
|
return ftp_delete($ftpStream, $path); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
1 |
|
public function close() |
188
|
|
|
{ |
189
|
1 |
|
return $this->ftpClose($this->getSession()); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param resource $ftpStream |
194
|
|
|
* @return bool |
195
|
|
|
* @codeCoverageIgnore |
196
|
|
|
*/ |
197
|
|
|
protected function ftpClose($ftpStream) |
198
|
|
|
{ |
199
|
|
|
return ftp_close($ftpStream); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param resource $ftpStream |
204
|
|
|
* @param bool $pasv |
205
|
|
|
* @return bool |
206
|
|
|
* @codeCoverageIgnore |
207
|
|
|
*/ |
208
|
|
|
protected function ftpPasv($ftpStream, $pasv) |
209
|
|
|
{ |
210
|
|
|
return ftp_pasv($ftpStream, $pasv); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: