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.ftp.php
executable file
·482 lines (451 loc) · 19.3 KB
/
class.ftp.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?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 ftp_client {
private $id;
/////////////////////////////////////////////////////////////////////////
// Public methods
/////////////////////////////////////////////////////////////////////////
public function startConnection($host, $user, $pass, $port, $ftps) {
$_SESSION['ftp_host'] = $host;
$_SESSION['ftp_user'] = $user;
$_SESSION['ftp_pass'] = $pass;
$_SESSION['ftp_port'] = $port;
$_SESSION['ftps'] = $ftps;
$this->connect();
$this->disconnect();
}
public function stopConnection() {
unset($_SESSION['ftp_host']);
unset($_SESSION['ftp_user']);
unset($_SESSION['ftp_pass']);
unset($_SESSION['ftp_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) {
set_time_limit(0);
$this->connect();
$array = array();
if (isset($this->id)) {
//Get index
$id = $this->id;
if (ftp_chdir($id, $path) === false) {
$this->getError("Impossible to Change Directory");
} else {
$raw = ftp_rawlist($id, "-al .");
$parsed = $this->parseRawList($raw);
//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->ftp_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
$array['status'] = 'success';
$array['files'] = $parsed;
}
} else {
//Error
$this->getError("No Connection ID");
}
$this->disconnect();
return json_encode($array);
}
/////////////////////////////////////////////////////////////////////////
// Transfer a file to remote server
/////////////////////////////////////////////////////////////////////////
public function transferFileToServer($cPath, $sPath, $fName, $mode) {
set_time_limit(0);
$this->connect();
$msg = array();
if (isset($this->id)) {
if (!ftp_chdir($this->id, $sPath)) {
//Create Directory
if (ftp_mkdir($this->id, $sPath) === false) {
//Error
$msg = $this->getError("Failed To Create Directory");
}
} else {
$mode = $this->getTransferMode($mode);
if (is_dir($cPath)) {
$result = $this->putDirectory($cPath, $sPath."/".$fName, $mode);
} else {
$result = ftp_put($this->id, $fName, $cPath, $mode);
}
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, $mode) {
set_time_limit(0);
$this->connect();
$msg = array();
if (isset($this->id)) {
if (!ftp_chdir($this->id, $sPath)) {
//Directory doesn't exist
$msg = $this->getError("Server Directory Doesn't Exist");
} else {
$mode = $this->getTransferMode($mode);
if ($this->ftp_isdir($sPath."/".$fName)) {
$result = $this->getDirectory($cPath, $sPath, $fName, $mode);
} else {
$result = ftp_get($this->id, $cPath, $fName, $mode);
}
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();
$pos = strrpos($path, "/") + 1;
$name = substr($path, $pos, strlen($path));
$path = substr($path, 0, $pos-1);
if (!ftp_chdir($this->id, $path)) {
//Directory doesn't exist
$msg = $this->getError("Server Directory Doesn't Exist");
} else {
if (ftp_mkdir($this->id, $name) === 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 = ftp_pwd($this->id);
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 (ftp_delete($this->id, $path)) {
$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->removeServerTree($path)) {
$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 on remote server
/////////////////////////////////////////////////////////////////////////
public function changeServerFileMode($path, $mode) {
$this->connect();
$msg = array();
if ($mode[0] != '0') {
$mode = '0'.$mode;
}
$mode = intval($mode, 8);
if (ftp_chmod($this->id, $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 (!ftp_chdir($this->id, $path)) {
//Directory doesn't exist
$msg = $this->getError("Server Directory Doesn't Exist");
} else {
if (ftp_rename($this->id, $old, $new)) {
$msg['status'] = "success";
$msg['message'] = "Successfully Renamed";
} else {
$msg = $this->getError("Failed To Rename");
}
}
$this->disconnect();
return json_encode($msg);
}
/////////////////////////////////////////////////////////////////////////
// Edit file locally
/////////////////////////////////////////////////////////////////////////
public function editFileLocally($cName, $cBase, $sPath, $fName, $mode) {
$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 ,$mode);
$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() {
if (isset($_SESSION['ftps']) && $_SESSION['ftps'] == "true") {
$connection_id = ftp_ssl_connect($_SESSION['ftp_host'], $_SESSION['ftp_port']);
$_SESSION['msg'] = "SSL ";
} else {
$connection_id = ftp_connect($_SESSION['ftp_host'], $_SESSION['ftp_port']);
$_SESSION['msg'] = "";
}
if ($connection_id === false) {
die('{"status":"error","message":"Connection failed! Wrong Host or Port?"}');
}
$login_result = ftp_login($connection_id, $_SESSION['ftp_user'], $_SESSION['ftp_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() {
ftp_close($this->id);
unset($this->id);
}
private function removeServerTree($path) {
set_time_limit(0);
if (!isset($this->id)) {
return false;
}
$files = $this->parseRawList(ftp_rawlist($this->id, $path));
foreach ($files as $file) {
if ($file['type'] == 'd') {
$this->removeServerTree($path.'/'.$file['name']);
} else {
ftp_delete($this->id, $path.'/'.$file['name']);
}
}
return ftp_rmdir($this->id, $path);
}
private function getError($msg) {
$error = array();
$error['status'] = 'error';
$error['message']= $msg;
return $error;
}
/////////////////////////////////////////////////////////////////////////
// Parse transfermode
/////////////////////////////////////////////////////////////////////////
private function getTransferMode($mode) {
//Workaround
if ($mode == "FTP_ASCII") {
return FTP_ASCII;
} else {
return FTP_BINARY;
}
}
/////////////////////////////////////////////////////////////////////////
// Transfer directory to server
/////////////////////////////////////////////////////////////////////////
private function putDirectory($cPath, $sPath, $mode) {
if (!ftp_chdir($this->id, $sPath)) {
//Create Directory
if (ftp_mkdir($this->id, $sPath) === false) {
return false;
}
ftp_chdir($this->id, $sPath);
}
$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, $mode)) {
return false;
}
} else {
if (!ftp_put($this->id, $file, $cPath."/".$file, $mode)) {
return false;
}
}
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// Transfer Directory to client
/////////////////////////////////////////////////////////////////////////
private function getDirectory($cPath, $sPath, $fName, $mode) {
if (!file_exists($cPath)) {
if (!mkdir($cPath)) {
return false;
}
}
if (!ftp_chdir($this->id, $sPath."/".$fName)) {
return false;
}
$files = $this->parseRawList(ftp_rawlist($this->id, "-al ."));
foreach ($files as $file) {
//filter . and ..
if ($file['name'] != "." && $file['name'] != "..") {
if ($this->ftp_isdir($sPath."/".$fName."/".$file['name'])) {
if (!$this->getDirectory($cPath."/".$file['name'], $sPath."/".$fName, $file['name'], $mode)) {
return false;
}
} else {
if (!ftp_get($this->id, $cPath."/".$file['name'], $sPath."/".$fName."/".$file['name'], $mode)) {
return false;
}
}
}
}
return true;
}
/////////////////////////////////////////////////////////////////////////
// FTP-IsDir
/////////////////////////////////////////////////////////////////////////
private function ftp_isdir($path) {
return is_dir('ftp://'.$_SESSION['ftp_user'].':'.$_SESSION['ftp_pass'].'@'.$_SESSION['ftp_host'].':'.$_SESSION['ftp_port'].$path);
}
private function parseRawList($rawList)
{
//@Do not touch - More: http://de3.php.net/manual/de/function.ftp-rawlist.php#110561
$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);
}
}
?>