-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
237 lines (216 loc) · 5.53 KB
/
code.power
File metadata and controls
237 lines (216 loc) · 5.53 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* The KeyLoader
*
* @var KeyLoader
* @since 3.2.0
*/
protected KeyLoader $key;
/**
* The client object
*
* @var SftpClient|null
* @since 3.2.0
**/
protected ?SftpClient $client = null;
/**
* The server details
*
* @var object
* @since 3.2.0
**/
protected ?object $details = null;
/**
* Application object.
*
* @var CMSApplication
* @since 3.2.0
**/
protected CMSApplication $app;
/**
* Constructor
*
* @param KeyLoader $key The key loader object.
* @param CMSApplication|null $app The app object.
*
* @since 3.2.0
*/
public function __construct(KeyLoader $key, ?CMSApplication $app = null)
{
$this->key = $key;
$this->app = $app ?: Factory::getApplication();
}
/**
* set the server details
*
* @param object $details The server details
*
* @return Sftp
* @since 3.2.0
**/
public function set(object $details): Sftp
{
// we need to make sure the if the details changed to get a new server client
if (!ObjectHelper::equal($details, $this->details))
{
// set the details
$this->details = $details;
// reset the client if it was set before
$this->client = null;
}
return $this;
}
/**
* move a file to server with the FTP client
*
* @param string $localPath The full local path to the file
* @param string $fileName The file name
*
* @return bool
* @since 3.2.0
**/
public function move(string $localPath, string $fileName): bool
{
if ($this->connected() &&
($data = FileHelper::getContent($localPath, null)) !== null)
{
// get the remote path
$path = '';
if (isset($this->details->path) &&
StringHelper::check($this->details->path) &&
$this->details->path !== '/')
{
$path = trim((string) $this->details->path);
$path = '/' . trim($path, '/') . '/';
}
try
{
return $this->client->put($path . trim($fileName), $data);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::sprintf('Moving of the %s failed', $fileName) . ': ' . $e->getMessage(),
'Error'
);
}
}
return false;
}
/**
* Make sure we are connected
*
* @return bool
* @since 3.2.0
**/
private function connected(): bool
{
// check if we have a connection
if ($this->client instanceof SftpClient && ($this->client->isConnected() || $this->client->ping()))
{
return true;
}
$this->client = $this->getClient();
return $this->client instanceof SftpClient;
}
/**
* get the SftpClient object
*
* @return SftpClient|null
* @since 3.2.0
**/
private function getClient(): ?SftpClient
{
// make sure we have a host value set
if (isset($this->details->host) && StringHelper::check($this->details->host) &&
isset($this->details->username) && StringHelper::check($this->details->username))
{
// insure the port is set
$port = (int)($this->details->port ?? 22);
// open the connection
$sftp = new SftpClient($this->details->host, $port);
// set the passphrase if it exist
$passphrase = (isset($this->details->secret) && StringHelper::check(trim($this->details->secret))) ? trim($this->details->secret) : false;
// set the password if it exist
$password = (isset($this->details->password) && StringHelper::check(trim($this->details->password))) ? trim($this->details->password) : false;
// now login based on authentication type
$key = null;
switch($this->details->authentication)
{
case 1: // password
$key = $password ?? null;
$password = null;
break;
case 2: // private key file
case 3: // both password and private key file
if (isset($this->details->private) && StringHelper::check($this->details->private) &&
($private_key = FileHelper::getContent($this->details->private, null)) !== null)
{
try
{
$key = $this->key::load(trim($private_key), $passphrase);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Loading the private key file failed') . ': ' . $e->getMessage(),
'Error'
);
$key = null;
}
}
break;
case 4: // private key field
case 5: // both password and private key field
if (isset($this->details->private_key) && StringHelper::check($this->details->private_key))
{
try
{
$key = $this->key::load(trim($this->details->private_key), $passphrase);
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Loading the private key text failed') . ': ' . $e->getMessage(),
'Error'
);
$key = null;
}
}
break;
}
// remove any null bites from the username
$this->details->username = trim($this->details->username);
// login
if (!empty($key) && !empty($password))
{
try
{
$sftp->login($this->details->username, $key, $password);
return $sftp;
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Login failed') . ': ' . $e->getMessage(),
'Error'
);
}
}
elseif (!empty($key))
{
try
{
$sftp->login($this->details->username, $key);
return $sftp;
}
catch(\Exception $e)
{
$this->app->enqueueMessage(
Text::_('Login failed') . ': ' . $e->getMessage(),
'Error'
);
}
}
}
return null;
}