-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathftp.php
145 lines (127 loc) · 3.77 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
<?php
class FTPRemoteFS extends RemoteFS {
protected $connection = null;
protected $secure = false;
protected $host;
protected $port = 21;
protected $path = '/';
protected $login = 'anonymous';
protected $password = '';
protected $config;
protected $errors;
public function __construct($config) {
$this->config = $config;
$url = parse_url($config);
$this->secure = $url['scheme'] == 'ftps';
$this->host = $url['host'];
if (isset($url['port'])) $this->port = $url['port'];
if (isset($url['user'])) $this->login = $url['user'];
if (isset($url['pass'])) $this->password = $url['pass'];
if (isset($url['path'])) $this->path = $url['path'];
if ($this->path[strlen($this->path)-1] != '/') $this->path .= '/';
register_shutdown_function(array($this, '_shutdown'));
}
protected function setupErrorHandler() {
set_error_handler(array($this, '_errorHandler'), E_ALL);
}
protected function releaseErrorHandler() {
restore_error_handler();
}
public function _errorHandler($code, $message, $file = null, $line = -1, $context = null) {
$error = array(
"code" => $code,
"message" => $message,
"file" => $file,
"line" => $line,
"context" => $context
);
if (!is_array($this->errors)) $this->errors = array($error);
else $this->errors[] = $error;
}
public function _shutdown() {
$this->disconnect();
}
public function errors() {
return $this->errors;
}
protected function connect() {
if ($this->connection) return true;
$this->setupErrorHandler();
$connection = $this->secure ? @ftp_ssl_connect($this->host, $this->port) : @ftp_connect($this->host, $this->port);
if ($connection) {
$logged = @ftp_login($connection, $this->login, $this->password);
if ($logged) {
@ftp_pasv($connection, true);
$this->connection = $connection;
$this->releaseErrorHandler();
return true;
} else {
@ftp_close($connection);
}
}
$this->releaseErrorHandler();
return false;
}
protected function disconnect() {
if ($this->connection) {
@ftp_close($this->connection);
$this->connection = null;
}
}
protected function mkdirp($path) {
$parts = explode("/", $path);
@ftp_chdir($this->connection, "/");
foreach ($parts as $part) {
if (empty($part)) continue;
if (!@ftp_chdir($this->connection, $part)) {
@ftp_mkdir($this->connection, $part);
if (!@ftp_chdir($this->connection, $part)) return false;
}
}
return $this->isDirectory($path);
}
protected function isDirectory($path) {
return @ftp_chdir($this->connection, $path);
}
public function get($path, $localPath) {
if (!$this->connect()) return false;
$this->setupErrorHandler();
$result = @ftp_get($this->connection, $localPath, $this->path . $path, FTP_BINARY);
$this->releaseErrorHandler();
return $result;
}
public function put($path, $localPath) {
if (!$this->connect()) return false;
$dir = dirname($this->path . $path);
$this->setupErrorHandler();
if (!$this->isDirectory($dir) && !$this->mkdirp($dir)) {
$this->releaseErrorHandler();
return false;
}
$result = @ftp_put($this->connection, $this->path . $path, $localPath, FTP_BINARY);
$this->releaseErrorHandler();
return $result;
}
public function delete($path) {
if (!$this->connect()) return false;
$this->setupErrorHandler();
$result = @ftp_delete($this->connection, $this->path . $path);
$this->releaseErrorHandler();
return $result;
}
public function exists($path) {
if (!$this->connect()) return false;
$this->setupErrorHandler();
$result = @ftp_size($this->connection, $this->path . $path) != -1;
$this->releaseErrorHandler();
return $result;
}
}
RemoteFS::register("FTPRemoteFS", function($config) {
try {
$url = parse_url($config);
return in_array($url['scheme'], array('ftp', 'ftps'));
} catch (Exception $e) {
return false;
}
});