-
Notifications
You must be signed in to change notification settings - Fork 1
/
ftp.php
378 lines (317 loc) · 7.78 KB
/
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
<?php
/**
*
* @package Kleeja
* @copyright (c) 2007 Kleeja.com
* @license http://www.kleeja.com/license
*
*/
/**
* @ignore
*/
if (! defined('IN_COMMON'))
{
exit();
}
/**
* Make changes with files using FTP
*/
class kleeja_ftp
{
/**
* TimeOut before disconnection
*/
public $timeout = 30;
/**
* Move to this folder after connection
*/
public $root = '';
/**
* If enabled, debug mode will be activated
*/
public $debug = false;
/**
* FTP current connection handler
*/
private $handler = null;
private $unique_name = '';
private $link = '';
/**
* Connect to FTP server
*
* @param string $host FTP server address
* @param string $user FTP server username
* @param string $password FTP server password
* @param int $port FTP server port
* @param string $rootPath
* @param bool $passive
* @param mixed $ssl
* @param mixed $timeout
* @return bool|kleeja_ftp
* @internal param string $path FTP server path
*/
public function open($host, $user, $password, $port = 21, $rootPath = '/', $passive = true, $ssl = false, $timeout = 90)
{
$this->timeout = $timeout;
$this->debug = defined('DEV_STAGE');
//connect to the server
if ($ssl)
{
$this->handler = @ftp_ssl_connect($host, $port, $this->timeout);
}
else
{
$this->handler = @ftp_connect($host, $port, $this->timeout);
}
if (! $this->handler)
{
// if($this->debug)
// {
// echo 'can not connect<br>';
// var_dump($this->handler);
//
// }
return false;
}
//pasv mode
@ftp_pasv($this->handler, $passive);
//login to the server
if (! ftp_login($this->handler, $user, $password))
{
// if($this->debug)
// {
// echo 'can not login<br>';
// var_dump($this->handler);
// }
return false;
}
//move to the path
$rootPath = trim($rootPath);
if ($rootPath == '/')
{
$rootPath = '';
}
if ($rootPath != '')
{
if (substr($rootPath, -1, 1) == '/')
{
$rootPath = substr($rootPath, 0, -1);
}
}
$this->root = $rootPath;
if ($this->root != '')
{
$this->link = 'http://' . $host . '/' . ltrim($rootPath, '/');
if (! $this->file_exists($this->root))
{
$this->create_folder('');
}
}
return $this;
}
/**
* Go to the given folder
* @param string $dir
* @return bool
*/
public function go_to($dir = '')
{
if ($dir && $dir !== '/')
{
if (substr($dir, -1, 1) == '/')
{
$dir = substr($dir, 0, -1);
}
}
return @ftp_chdir($this->handler, $dir);
}
/**
* Close current FTP connection
*/
public function close()
{
if (! $this->handler)
{
return;
}
ftp_quit($this->handler);
}
/**
* Get the current folder that we are in now
* @return string
*/
public function current_folder()
{
return ftp_pwd($this->handler);
}
/**
* Change the file or folder permission
* @param string $file
* @param int $perm
* @return bool
*/
public function chmod($file, $perm = 0644)
{
if (function_exists('ftp_chmod'))
{
$action = @ftp_chmod($this->handler, $perm, $this->_fixpath($file));
}
else
{
$chmod_cmd = 'CHMOD ' . base_convert($perm, 10, 8) . ' ' . $this->_fixpath($file);
$action = ftp_site($this->handler, $chmod_cmd);
}
return $action;
}
/**
* is file exists
* @return bool
* @param mixed $file
*/
public function file_exists($file)
{
return ftp_size($this->handler, $this->_fixpath($file)) > -1;
}
/**
* fix the given path to be compatible with the FTP
* @param string $path
* @return string
*/
private function _fixpath($path)
{
return ($this->root != '' ? $this->root . '/' : '') . $path;
}
/**
* Delete given file
* @param string $file
* @return bool
*/
public function delete($file)
{
return @ftp_delete($this->handler, $this->_fixpath($file));
}
/**
* Create a file and write the given content to it
* @param string $filePath
* @param $content
* @return bool
*/
public function write($filePath, $content)
{
$cached_file = PATH . 'cache/cached_ftp_' . uniqid(time());
//make it as a cached file
$h = @fopen($cached_file, 'wb');
fwrite($h, $content);
@fclose($h);
$r = @ftp_put($this->handler, $this->_fixpath($filePath), $cached_file, FTP_BINARY);
kleeja_unlink($cached_file, true);
return $r;
}
/**
* Upload a local file to the FTP server
* @param string $local_file
* @param string $server_file
* @param mixed $deleteLocal
* @return bool
*/
public function upload($local_file, $server_file, $deleteLocal = true)
{
//Initate the upload
//TODO if slow, use ftp_put
$ret = ftp_nb_put($this->handler, $this->_fixpath($server_file), $local_file, FTP_BINARY);
while ($ret == FTP_MOREDATA)
{
//still uploading
if ($this->debug)
{
print ftell($this->handler) . "\n";
}
$ret = ftp_nb_continue($this->handler);
}
if ($deleteLocal)
{
kleeja_unlink($local_file);
}
//bad uploading
if ($ret != FTP_FINISHED)
{
return false;
}
return true;
}
/**
* Rename a file
* @param string $old_file
* @param string $new_file
* @return bool
*/
public function rename($old_file, $new_file)
{
return @ftp_rename($this->handler, $this->_fixpath($old_file), $this->_fixpath($new_file));
}
/**
* Create a folder
* @param string $dir
* @param int $perm
* @return bool
*/
public function create_folder($dir, $perm = 0755)
{
// if($this->debug)
// {
// var_dump($this->_fixpath($dir));
// }
if (ftp_mkdir($this->handler, $this->_fixpath($dir)) === false)
{
return false;
}
$this->chmod($this->_fixpath($dir), $perm);
return true;
}
/**
* Delete the given folder
* @param string $dir
* @return bool
*/
public function delete_folder($dir)
{
return @ftp_rmdir($this->handler, $this->_fixpath($dir));
}
/**
* @param string $unique_name
*/
public function setUniqueName($unique_name)
{
$this->unique_name = $unique_name;
}
/**
* @return string
*/
public function getUniqueName()
{
return $this->unique_name;
}
/**
* @param string $link
*/
public function setLink($link)
{
if (trim($link) == '')
{
return;
}
if (substr($link, -1, 1) == '/')
{
$link = substr($link, 0, -1);
}
$this->link = $link;
}
/**
* @return string
* @param mixed $path
*/
public function getLink($path)
{
return $this->link . '/' . $path;
}
}