This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
class.scp.php
executable file
·464 lines (437 loc) · 18.7 KB
/
class.scp.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See [root]/license.md for more information. This information must remain intact.
*/
class scp_client {
private $id;
/////////////////////////////////////////////////////////////////////////
// Public methods
/////////////////////////////////////////////////////////////////////////
public function startConnection($host, $user, $pass, $port) {
$_SESSION['ssh2_host'] = $host;
$_SESSION['ssh2_user'] = $user;
$_SESSION['ssh2_pass'] = $pass;
$_SESSION['ssh2_port'] = $port;
$this->connect();
$this->disconnect();
}
public function stopConnection() {
unset($_SESSION['ssh2_host']);
unset($_SESSION['ssh2_user']);
unset($_SESSION['ssh2_pass']);
unset($_SESSION['ssh2_port']);
}
/////////////////////////////////////////////////////////////////////////
// Remote server index (Returns a list of files and directorys on the
// remote server as json) For more information: see parseRawList();
/////////////////////////////////////////////////////////////////////////
public function getServerFiles($path) {
$this->connect();
$msg = array();
if (isset($this->id)) {
//Get index
if ($this->execCommand("cd ".$path) === false) {
$msg = $this->getError("Impossible to Change Directory");
} else {
$raw = $this->execCommand("ls -al ".$path."/");
$raw = explode("\n", $raw);
$raw = array_slice($raw, 1);
$parsed = $this->parseRawList($raw);
$parsed = array_slice($parsed, 1);
//Correct style
$dirs = array();
$files = array();
for ($i = 0; $i < count($parsed); $i++) {
//Change Name
$parsed[$i]['fName'] = $parsed[$i]['name'];
if ($path == "/") {
$parsed[$i]['name'] = $path . $parsed[$i]['name'];
} else {
$parsed[$i]['name'] = $path ."/". $parsed[$i]['name'];
}
//Edit type
$type = $parsed[$i]['type'];
if ($type == 'd') {
$parsed[$i]['type'] = "directory";
} else if ($type == 'l') {
if ($this->scp_isdir($parsed[$i]['name'])) {
//Is directory
$parsed[$i]['type'] = "directory";
} else {
//Is file
$parsed[$i]['type'] = "file";
}
} else if ($type == '-') {
$parsed[$i]['type'] = "file";
} else {
$parsed[$i]['type'] = "error";
}
//Sort entries I
if ($parsed[$i]['type'] == "directory") {
array_push($dirs, $parsed[$i]);
} else {
array_push($files, $parsed[$i]);
}
}
//Sort entries II
usort($dirs, "transfer_controller::mySort");
usort($files, "transfer_controller::mySort");
$parsed = array_merge($dirs, $files);
//Result
$msg['status'] = 'success';
$msg['files'] = $parsed;
}
} else {
//Error
$this->getError("No Connection ID");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Transfer a file to remote server
/////////////////////////////////////////////////////////////////////////
public function transferFileToServer($cPath, $sPath, $fName) {
set_time_limit(0);
$this->connect();
$msg = array();
if (isset($this->id)) {
if ($this->execCommand("cd ".$sPath) === false) {
//Create Directory
if ($this->execCommand("mkdir ".$sPath) === false) {
//Error
$msg = $this->getError("Failed To Create Directory");
}
} else {
if (is_dir($cPath)) {
$result = $this->putDirectory($cPath, $sPath."/".$fName);
} else {
$result = ssh2_scp_send($this->id, $cPath, $sPath."/".$fName);
}
if ($result) {
$msg['status'] = 'success';
$msg['message'] = $fName.' uploaded';
} else {
//Error
$msg = $this->getError("Failed to upload ".$fName);
}
}
} else {
$msg = $this->getError("No Connection ID");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Transfer a file to Codiad Server
/////////////////////////////////////////////////////////////////////////
public function transferFileToClient($cPath, $sPath, $fName) {
set_time_limit(0);
$this->connect();
$msg = array();
if (isset($this->id)) {
if ($this->execCommand("cd ".$sPath) === false) {
//Directory doesn't exist
$msg = $this->getError("Server Directory Doesn't Exist");
} else {
if ($this->scp_isdir($sPath."/".$fName)) {
$result = $this->getDirectory($cPath, $sPath, $fName);
} else {
$result = ssh2_scp_recv($this->id, $sPath."/".$fName, $cPath);
}
if ($result) {
$msg['status'] = 'success';
$msg['message'] = $fName.' downloaded';
} else {
//Error
$msg = $this->getError("Failed to download ".$fName);
}
}
} else {
$msg = $this->getError("No Connection ID");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Create directory on remote server
/////////////////////////////////////////////////////////////////////////
public function createServerDirectory($path) {
$this->connect();
$msg = array();
if ($this->execCommand("mkdir ".$path) === false) {
//Error
$msg = $this->getError("Failed To Create Directory");
} else {
$msg['status'] = 'success';
$msg['message'] = 'Directory Created';
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Get current directory name
/////////////////////////////////////////////////////////////////////////
public function getSeverDirectory() {
$this->connect();
$array = array();
$pwd = $this->execCommand("cd");
if ($pwd === false) {
$array = $this->getError('Impossible to Get Directory');
} else {
$array['status'] = "success";
$array['dir'] = $pwd;
}
$this->disconnect();
return json_encode($array);
}
/////////////////////////////////////////////////////////////////////////
// Remove file on remote server
/////////////////////////////////////////////////////////////////////////
public function removeServerFile($path) {
set_time_limit(0);
$this->connect();
$msg = array();
if ($this->execCommand("rm ".$path) !== false) {
$msg['status'] = "success";
$msg['message'] = "File Removed";
} else {
$msg = $this->getError("Failed To Delete File");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Remove directory on remote server
/////////////////////////////////////////////////////////////////////////
public function removeServerDirectory($path) {
set_time_limit(0);
$this->connect();
$msg = array();
if ($this->execCommand("rm -R ".$path) !== false) {
$msg['status'] = "success";
$msg['message'] = "Directory Removed";
} else {
$msg = $this->getError("Failed To Delete Directory");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Change permissions of file or directory on remote server
/////////////////////////////////////////////////////////////////////////
public function changeServerFileMode($path, $mode) {
$this->connect();
$msg = array();
if ($this->execCommand("chmod -R ".$mode." ".$path) !== false) {
$msg['status'] = "success";
$msg['message'] = "Permissions Changed";
} else {
$msg = $this->getError("Failed To Change Permissions");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Rename directory or file
/////////////////////////////////////////////////////////////////////////
public function rename($path, $old, $new) {
$this->connect();
$msg = array();
if ($this->execCommand("mv ".$path."/".$old." ".$path."/".$new) !== false) {
$msg['status'] = "success";
$msg['message'] = "Successfully Renamed";
} else {
$msg = $this->getError("Failed To Rename");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Change group
/////////////////////////////////////////////////////////////////////////
public function changeServerGroup($path, $name) {
$this->connect();
$msg = array();
if ($this->execCommand("chgrp -R $name $path") !== false) {
$msg['status'] = "success";
$msg['message'] = "Group Changed";
$msg['res'] = $res;
} else {
$msg = $this->getError("Failed To Change Group");
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Edit file locally
/////////////////////////////////////////////////////////////////////////
public function editFileLocally($cName, $cBase, $sPath, $fName) {
$cPath = $cBase . "/" . $_SESSION['ct_mt_rand'];
if (!is_dir($cPath)) {
if (!mkdir($cPath)) {
$msg = $this->getError("Failed to create temporary directory");
return json_encode($msg);
}
}
$cPath .= "/" . $fName;
$msg = $this->transferFileToClient($cPath, $sPath, $fName);
$msg = json_decode($msg, true);
$msg['file'] = $cName . "/" . $_SESSION['ct_mt_rand'] . "/" . $fName;
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
//
// Private methods
//
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// Connect remote server
/////////////////////////////////////////////////////////////////////////
private function connect() {
$connection_id = ssh2_connect($_SESSION['ssh2_host'], $_SESSION['ssh2_port']);
if ($connection_id === false) {
die('{"status":"error","message":"Connection failed! Wrong Host or Port?"}');
}
$login_result = ssh2_auth_password($connection_id, $_SESSION['ssh2_user'], $_SESSION['ssh2_pass']);
if ($login_result === false) {
die('{"status":"error","message":"Connection failed! Wrong Username or Password?"}');
}
$this->id = $connection_id;
}
/////////////////////////////////////////////////////////////////////////
// Disconnect remote server
/////////////////////////////////////////////////////////////////////////
private function disconnect() {
$this->execCommand("exit;");
unset($this->id);
}
private function execCommand($cmd) {
$stream = ssh2_exec($this->id, $cmd);
$err = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($err, true);
stream_set_blocking($stream, true);
$err = stream_get_contents($err);
if ($err === "") {
return stream_get_contents($stream);
} else {
return false;
}
}
private function getError($msg) {
$error = array();
$error['status'] = 'error';
$error['message']= $msg;
return $error;
}
/////////////////////////////////////////////////////////////////////////
// Transfer directory to server
/////////////////////////////////////////////////////////////////////////
private function putDirectory($cPath, $sPath) {
if ($this->execCommand("cd ".$sPath) === false) {
//Create Directory
if ($this->execCommand("mkdir ".$sPath) === false) {
return false;
}
}
$files = scandir($cPath);
foreach ($files as $file) {
//filter . and ..
if ($file != "." && $file != "..") {
//check if $file is a folder
if (is_dir($cPath."/".$file)) {
if (!$this->putDirectory($cPath."/".$file, $sPath."/".$file)) {
return false;
}
} else {
if (!ssh2_scp_send($this->id, $cPath."/".$file, $sPath."/".$file)) {
return false;
}
}
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// Transfer Directory to client
/////////////////////////////////////////////////////////////////////////
private function getDirectory($cPath, $sPath, $fName) {
if (!file_exists($cPath)) {
if (!mkdir($cPath)) {
return false;
}
}
if ($this->execCommand("cd ".$sPath."/".$fName) === false) {
return false;
}
$raw = $this->execCommand("ls -al ".$sPath."/".$fName."/");
$raw = explode("\n", $raw);
$raw = array_slice($raw, 1);
$files = $this->parseRawList($raw);
$files = array_slice($files, 1);
foreach ($files as $file) {
//filter . and ..
if ($file['name'] != "." && $file['name'] != "..") {
if ($this->scp_isdir($sPath."/".$fName."/".$file['name'])) {
if (!$this->getDirectory($cPath."/".$file['name'], $sPath."/".$fName, $file['name'])) {
return false;
}
} else {
if (!ssh2_scp_recv($this->id, $sPath."/".$fName."/".$file['name'], $cPath."/".$file['name']))
{
return false;
}
}
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// SCP-IsDir
/////////////////////////////////////////////////////////////////////////
private function scp_isdir($path) {
if ($this->execCommand("ls -al ".$path."/") === false) {
return false;
} else {
return true;
}
}
private function parseRawList($rawList)
{
//@Do not touch - More: http://de3.php.net/manual/de/function.ftp-rawlist.php#110561
//@Do not touch - 26.07.2013 - Andr3as
$start = 2;
$orderList = array("d", "l", "-");
$typeCol = "type";
$cols = array("permissions", "number", "owner", "group", "size", "month", "day", "time", "name");
foreach($rawList as $key=>$value)
{
$parser = null;
if($key >= $start) $parser = explode(" ", preg_replace('!\s+!', ' ', $value));
if(isset($parser))
{
foreach($parser as $key=>$item)
{
$parser[$cols[$key]] = $item;
unset($parser[$key]);
}
$parsedList[] = $parser;
}
}
foreach($orderList as $order)
{
foreach($parsedList as $key=>$parsedItem) {
$type = substr(current($parsedItem), 0, 1);
if($type == $order) {
$parsedItem[$typeCol] = $type;
unset($parsedList[$key]);
$parsedList[] = $parsedItem;
}
}
}
return array_values($parsedList);
}
}
?>