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
|
|
|
* SFTP Service. |
20
|
|
|
*/ |
21
|
|
|
class SFtpService extends FtpService |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The transfer mode. |
25
|
|
|
* |
26
|
|
|
* Value is substitute for the NET_SFTP_LOCAL_FILE constant. |
27
|
|
|
* |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
const TRANSFER_MODE = 1; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The SFTP connection. |
34
|
|
|
* |
35
|
|
|
* @var \Net_SFTP |
36
|
|
|
*/ |
37
|
|
|
private $sftp; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Is the connection logged in? |
41
|
|
|
* |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $loggedIn = false; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
*/ |
50
|
39 |
|
public function __construct(array $config) |
51
|
|
|
{ |
52
|
39 |
|
if (!isset($config['port'])) { |
53
|
39 |
|
$config['port'] = 22; |
54
|
39 |
|
} |
55
|
|
|
|
56
|
39 |
|
parent::__construct($config); |
57
|
39 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
15 |
|
public function send(FileInterface $file) |
63
|
|
|
{ |
64
|
15 |
|
$connection = $this->getConnection(); |
65
|
12 |
|
$from = $file->getPath(); |
66
|
12 |
|
$to = $this->getDirectorySend() . '/' . $file->getFileName(); |
67
|
|
|
|
68
|
12 |
|
$result = $connection->put($to, $from, self::TRANSFER_MODE); |
69
|
12 |
|
if (!$result) { |
70
|
3 |
|
throw new ServiceException( |
71
|
3 |
|
sprintf( |
72
|
3 |
|
'File "%s" could not be uploaded to "%s".', |
73
|
3 |
|
$from, |
74
|
|
|
$to |
75
|
3 |
|
) |
76
|
3 |
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
9 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritDoc |
84
|
|
|
*/ |
85
|
6 |
|
public function scan() |
86
|
|
|
{ |
87
|
6 |
|
$connection = $this->getConnection(); |
88
|
6 |
|
$files = array(); |
89
|
|
|
|
90
|
6 |
|
$rawFiles = $connection->rawlist($this->getDirectoryReceive()); |
91
|
6 |
|
if (empty($rawFiles)) { |
92
|
3 |
|
return $files; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// We only want files, no directories. |
96
|
3 |
|
foreach ($rawFiles as $rawFile) { |
97
|
3 |
|
if ($rawFile['type'] !== 1) { |
98
|
3 |
|
continue; |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
$files[] = $rawFile['filename']; |
102
|
3 |
|
} |
103
|
|
|
|
104
|
3 |
|
return $files; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
9 |
|
public function receive($fileName, $localDirectory) |
111
|
|
|
{ |
112
|
9 |
|
$connection = $this->getConnection(); |
113
|
|
|
|
114
|
9 |
|
$to = $localDirectory . '/' . $fileName; |
115
|
9 |
|
$from = $this->getDirectoryReceive() . '/' . $fileName; |
116
|
9 |
|
$result = $connection->get($from, $to); |
117
|
|
|
|
118
|
9 |
|
if (!$result) { |
119
|
3 |
|
throw new ServiceException( |
120
|
3 |
|
sprintf( |
121
|
3 |
|
'File "%s" could not be downloaded to "%s".', |
122
|
3 |
|
$from, |
123
|
|
|
$to |
124
|
3 |
|
) |
125
|
3 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
6 |
|
return $to; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
9 |
|
public function processed($fileName) |
135
|
|
|
{ |
136
|
9 |
|
$connection = $this->getConnection(); |
137
|
9 |
|
$from = $this->getDirectoryReceive() . '/' . $fileName; |
138
|
9 |
|
$to = $this->getDirectoryReceiveProcessed() . '/' . $fileName; |
139
|
|
|
|
140
|
9 |
|
$result = $connection->rename($from, $to); |
141
|
9 |
|
if (!$result) { |
142
|
3 |
|
throw new ServiceException( |
143
|
3 |
|
sprintf( |
144
|
3 |
|
'File "%s" could not be moved to "%s".', |
145
|
3 |
|
$from, |
146
|
|
|
$to |
147
|
3 |
|
) |
148
|
3 |
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
6 |
|
return $result; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Inject the connection for testing purpose. |
156
|
|
|
* |
157
|
|
|
* @param \Net_SFTP $connection |
158
|
|
|
* The connection object. |
159
|
|
|
*/ |
160
|
39 |
|
public function setConnection(\Net_SFTP $connection) |
161
|
|
|
{ |
162
|
39 |
|
$this->sftp = $connection; |
163
|
39 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get the connection. |
167
|
|
|
* |
168
|
|
|
* @return \Net_SFTP |
169
|
|
|
*/ |
170
|
39 |
|
protected function getConnection() |
171
|
|
|
{ |
172
|
39 |
|
$this->sftp = $this->sftp ?: new \Net_SFTP($this->getHostname(), $this->getPort(), $this->getTimeout()); |
173
|
39 |
|
$this->login(); |
174
|
36 |
|
return $this->sftp; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Login to the SFTP service. |
179
|
|
|
* |
180
|
|
|
* @throws ServiceException |
181
|
|
|
* Whe we can't login to the SFTP server. |
182
|
|
|
*/ |
183
|
39 |
|
protected function login() |
184
|
|
|
{ |
185
|
39 |
|
if ($this->loggedIn) { |
186
|
3 |
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
39 |
|
$result = $this->sftp->login( |
190
|
39 |
|
$this->getUsername(), |
191
|
39 |
|
$this->getPassword() |
192
|
39 |
|
); |
193
|
39 |
|
if (!$result) { |
194
|
3 |
|
throw new ServiceException( |
195
|
3 |
|
sprintf('Can\'t login with user "%s".', $this->getUsername()) |
196
|
3 |
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
36 |
|
$this->loggedIn = true; |
200
|
36 |
|
} |
201
|
|
|
} |
202
|
|
|
|