|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Amplexor\XConnect library |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT |
|
6
|
|
|
* @link https://github.com/amplexor-drupal/xconnect/ |
|
7
|
|
|
* @version 1.0.0 |
|
8
|
|
|
* @package Amplexor.XConnect |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Amplexor\XConnect\Service; |
|
15
|
|
|
|
|
16
|
|
|
use Amplexor\XConnect\Request\File\FileInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* FTP Service. |
|
20
|
|
|
*/ |
|
21
|
|
|
class FtpService extends ServiceAbstract |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The FTP transfer mode. |
|
25
|
|
|
* |
|
26
|
|
|
* We set this value = 2, thats the same as the FTP_BINARY constant. |
|
27
|
|
|
* |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
const TRANSFER_MODE = 2; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Service connection configuration. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
private $config = array( |
|
38
|
|
|
'hostname' => '', |
|
39
|
|
|
'port' => 21, |
|
40
|
|
|
'username' => '', |
|
41
|
|
|
'password' => '', |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The FTP connection resource. |
|
46
|
|
|
* |
|
47
|
|
|
* @var resource |
|
48
|
|
|
*/ |
|
49
|
|
|
private $ftp; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Class constructor. |
|
53
|
|
|
* |
|
54
|
|
|
* Create a new service by passing the configuration: |
|
55
|
|
|
* - hostname : The hostname of the FTP service. |
|
56
|
|
|
* - port : The FRP port. |
|
57
|
|
|
* - username : The username to connect to the FTP service. |
|
58
|
|
|
* - password : The password to connect to the FTP service. |
|
59
|
|
|
* |
|
60
|
|
|
* Optional configuration: |
|
61
|
|
|
* - directory_send : The remote directory to store the request file in. |
|
62
|
|
|
* - directory_send_processed : The remote directory where the processed |
|
63
|
|
|
* request files are stored. |
|
64
|
|
|
* - directory_receive : The remote directory where the translated files are |
|
65
|
|
|
* stored to be picked up. |
|
66
|
|
|
* - directory_receive_processed : The repome directory where to put the |
|
67
|
|
|
* translation files that are successfully processed by the local system. |
|
68
|
|
|
* |
|
69
|
|
|
* @param array $config |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct(array $config) |
|
72
|
|
|
{ |
|
73
|
|
|
parent::__construct($config); |
|
74
|
|
|
$this->setConfig($config); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
|
|
public function send(FileInterface $file) |
|
81
|
|
|
{ |
|
82
|
|
|
$connection = $this->getConnection(); |
|
83
|
|
|
$from = $file->getPath(); |
|
84
|
|
|
$to = $this->getDirectorySend() . '/' . $file->getFileName(); |
|
85
|
|
|
$result = ftp_put($connection, $to, $from, self::TRANSFER_MODE); |
|
86
|
|
|
|
|
87
|
|
|
if (!$result) { |
|
88
|
|
|
throw new ServiceException( |
|
89
|
|
|
sprintf( |
|
90
|
|
|
'File "%s" could not be uploaded to "%s".', |
|
91
|
|
|
$from, |
|
92
|
|
|
$to |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $result; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @inheritDoc |
|
102
|
|
|
*/ |
|
103
|
|
|
public function scan() |
|
104
|
|
|
{ |
|
105
|
|
|
$connection = $this->getConnection(); |
|
106
|
|
|
$directory = $this->getDirectoryReceive(); |
|
107
|
|
|
|
|
108
|
|
|
// Get the full file list. |
|
109
|
|
|
$files = ftp_nlist($connection, $directory); |
|
110
|
|
|
|
|
111
|
|
|
// We only want files, no directories. |
|
112
|
|
|
foreach ($files as $key => $file) { |
|
113
|
|
|
if (ftp_size($connection, $directory . '/' . $file) < 0) { |
|
114
|
|
|
unset($files[$key]); |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return array_values($files); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritDoc |
|
124
|
|
|
*/ |
|
125
|
|
|
public function receive($fileName, $directory) |
|
126
|
|
|
{ |
|
127
|
|
|
$connection = $this->getConnection(); |
|
128
|
|
|
$to = $directory . '/' . $fileName; |
|
129
|
|
|
$from = $this->getDirectoryReceive() . '/' . $fileName; |
|
130
|
|
|
|
|
131
|
|
|
$result = ftp_get($connection, $to, $from, self::TRANSFER_MODE); |
|
132
|
|
|
|
|
133
|
|
|
if (!$result) { |
|
134
|
|
|
throw new ServiceException( |
|
135
|
|
|
sprintf( |
|
136
|
|
|
'File "%s" could not be downloaded to "%s".', |
|
137
|
|
|
$from, |
|
138
|
|
|
$to |
|
139
|
|
|
) |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $to; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @inheritDoc |
|
148
|
|
|
*/ |
|
149
|
|
|
public function processed($fileName) |
|
150
|
|
|
{ |
|
151
|
|
|
$connection = $this->getConnection(); |
|
152
|
|
|
|
|
153
|
|
|
$from = $this->getDirectoryReceive() . '/' . $fileName; |
|
154
|
|
|
$to = $this->getDirectoryReceiveProcessed() . '/' . $fileName; |
|
155
|
|
|
|
|
156
|
|
|
$result = ftp_rename($connection, $from, $to); |
|
157
|
|
|
|
|
158
|
|
|
if (!$result) { |
|
159
|
|
|
throw new ServiceException( |
|
160
|
|
|
sprintf( |
|
161
|
|
|
'File "%s" could not be moved to "%s".', |
|
162
|
|
|
$from, |
|
163
|
|
|
$to |
|
164
|
|
|
) |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
return $result; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Get the connection. |
|
173
|
|
|
* |
|
174
|
|
|
* @return resource |
|
175
|
|
|
* A FTP connection resource. |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function getConnection() |
|
178
|
|
|
{ |
|
179
|
|
|
if (!$this->ftp) { |
|
180
|
|
|
$this->connect(); |
|
181
|
|
|
} |
|
182
|
|
|
return $this->ftp; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Connect to the FTP server. |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function connect() |
|
189
|
|
|
{ |
|
190
|
|
|
$connection = ftp_connect($this->getHostname(), $this->getPort()); |
|
191
|
|
|
if (!$connection) { |
|
192
|
|
|
throw new ServiceException( |
|
193
|
|
|
sprintf('Can\'t connect to host "%s"', $this->getHostname()) |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Login to host. |
|
198
|
|
|
$result = ftp_login($connection, $this->getUsername(), $this->getPassword()); |
|
199
|
|
|
if (!$result) { |
|
200
|
|
|
throw new ServiceException( |
|
201
|
|
|
sprintf('Can\'t login with user "%s"', $this->getUsername()) |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
// Set connection to passive mode. |
|
206
|
|
|
ftp_pasv($connection, true); |
|
207
|
|
|
|
|
208
|
|
|
$this->ftp = $connection; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Write the config to the config array. |
|
214
|
|
|
* |
|
215
|
|
|
* @param array $config |
|
216
|
|
|
*/ |
|
217
|
|
|
protected function setConfig(array $config) |
|
218
|
|
|
{ |
|
219
|
|
|
$keyNames = array_keys($this->config); |
|
220
|
|
|
foreach ($keyNames as $key) { |
|
221
|
|
|
if (array_key_exists($key, $config)) { |
|
222
|
|
|
$this->config[$key] = $config[$key]; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Get the hostname from the config. |
|
229
|
|
|
* |
|
230
|
|
|
* @return string |
|
231
|
|
|
*/ |
|
232
|
|
|
protected function getHostname() |
|
233
|
|
|
{ |
|
234
|
|
|
return $this->config['hostname']; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Get the portnumber from the config. |
|
239
|
|
|
* |
|
240
|
|
|
* @return int |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function getPort() |
|
243
|
|
|
{ |
|
244
|
|
|
return (int) $this->config['port']; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get the username. |
|
249
|
|
|
* |
|
250
|
|
|
* @return string |
|
251
|
|
|
*/ |
|
252
|
|
|
protected function getUsername() |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->config['username']; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Get the password. |
|
259
|
|
|
* |
|
260
|
|
|
* @return string |
|
261
|
|
|
*/ |
|
262
|
|
|
protected function getPassword() |
|
263
|
|
|
{ |
|
264
|
|
|
return $this->config['password']; |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|