-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathphpSFTP.php
More file actions
270 lines (250 loc) · 9.78 KB
/
phpSFTP.php
File metadata and controls
270 lines (250 loc) · 9.78 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
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
<?php
/*
* phpSFTP.php
*
* Copyright (C) 2015 KISS IT Consulting <http://www.kissitconsulting.com/>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class phpSFTP {
private $ftp = null;
private $host = null;
private $user = null;
private $password = null;
private $implicit = null;
private $port = null;
private $pasv = null;
private $binary = null;
private $curlopts = null;
/*
* Here is our constructor, params should be self explanatory. A few notes:
* 1. Probably most important to note is the $implicit flag. Set to true
* to use "Implicit" SFTP (via cURL). Set to false to use "Explicit" SFTP (via PHP ftp_ssl_connect)
* 2. PASV is currently the only supported mode for Implicit SFTP
*/
public function __construct($host, $user, $password, $port = 21, $implicit = false, $pasv = true, $binary = true) {
$this->host = (string)$host;
$this->user = (string)$user;
$this->password = (string)$password;
$this->implicit = (bool)$implicit;
$this->port = (int)$port;
$this->pasv = (bool)$pasv;
$this->binary = (bool)$binary;
$this->connect();
}
// We define the destructor to make sure we close our connection if not already done
public function __destruct() {
$this->close();
}
// Method to close our connection if it exists
public function close() {
if(is_resource($this->ftp)) {
if($this->implicit) {
curl_close($this->ftp);
} else {
ftp_close($this->ftp);
}
}
$this->ftp = null;
}
// Method to connect to the SFTP using the provided details. Exception raised on failure.
public function connect() {
if($this->implicit) {
// We are using Implicit SFTP so must use cURL. First set some default CURLOPT's that should normally stay consistent
$this->curlopts[CURLOPT_USERPWD] = "{$this->user}:{$this->password}";
$this->curlopts[CURLOPT_SSL_VERIFYPEER] = false;
$this->curlopts[CURLOPT_SSL_VERIFYHOST] = false;
$this->curlopts[CURLOPT_FTP_SSL] = CURLFTPSSL_TRY;
$this->curlopts[CURLOPT_FTPSSLAUTH] = CURLFTPAUTH_TLS;
$this->curlopts[CURLOPT_RETURNTRANSFER] = true;
// Now lets run a test to make sure we can connect
try {
$this->dir("/");
} catch(Exception $e) {
throw new Exception("Implicit secure FTP login failed: " . $e->getMessage());
}
} else {
// We are using Explicit SFTP so use the PHP native functions
$this->ftp = ftp_ssl_connect($this->host, $this->port);
if($this->ftp) {
if(ftp_login($this->ftp, $this->user, $this->password)) {
if($this->pasv === true && !ftp_pasv($this->ftp, true)) {
$this->close();
throw new Exception("Explicit secure FTP PASV failed");
}
} else {
$this->close();
throw new Exception("Explicit secure FTP login failed");
}
} else {
throw new Exception("Explicit secure FTP connection failed");
}
}
}
// Method to get a file from the SFTP server.
public function get($local, $remote) {
$return = false;
if(!empty($remote) && !empty($local)) {
if($this->implicit) {
// Implicit SFTP
$fp = fopen($local, "w");
if($fp) {
$opts = $this->getCurlXferMode();
$opts[CURLOPT_URL] = $this->getCurlURL($remote);
$opts[CURLOPT_FILE] = $fp;
$return = $this->callCurl($opts);
}
} else {
// Explicit SFTP
$return = ftp_get($this->ftp, $local, $remote, $this->getMode());
}
}
return $return;
}
// Method to put a file to the SFTP server.
public function put($local, $remote) {
$return = false;
if(!empty($remote) && !empty($local)) {
if($this->implicit) {
// Implicit SFTP
$fp = fopen($local, "r");
if($fp) {
$opts = $this->getCurlXferMode();
$opts[CURLOPT_URL] = $this->getCurlURL($remote);
$opts[CURLOPT_INFILE] = $fp;
$opts[CURLOPT_UPLOAD] = true;
$return = $this->callCurl($opts);
}
} else {
// Explicit SFTP
$return = ftp_put($this->ftp, $remote, $local, $this->getMode());
}
}
return $return;
}
// Method to rename a file on the SFTP server.
public function rename($old, $new) {
$return = false;
if(!empty($old) && !empty($new)) {
if($this->implicit) {
// Implicit SFTP
$opts = array();
$opts[CURLOPT_URL] = $this->getCurlURL();
$opts[CURLOPT_POSTQUOTE] = array("RNFR $old", "RNTO $new");
$return = $this->callCurl($opts);
} else {
// Explicit SFTP
$return = ftp_rename($this->ftp, $old, $new);
}
}
return $return;
}
// Method to delete a file on the SFTP server.
public function delete($file) {
$return = false;
if(!empty($file) && !empty($new)) {
if($this->implicit) {
// Implicit SFTP
$opts = array();
$opts[CURLOPT_URL] = $this->getCurlURL();
$opts[CURLOPT_QUOTE] = array("DELE $file");
$return = $this->callCurl($opts);
} else {
// Explicit SFTP
$return = ftp_delete($this->ftp, $file);
}
}
return $return;
}
// Method to get a list of files from the passed in directory
public function dir($path) {
$return = false;
if(!empty($path)) {
if(!preg_match('/\/$/', $path)) {
$path = "{$path}/";
}
if($this->implicit) {
// Implicit SFTP
$opts = array();
$opts[CURLOPT_URL] = $this->getCurlURL($path);
$opts[CURLOPT_FTPLISTONLY] = true;
$return = $this->callCurl($opts);
if(!empty($return)) {
$return = preg_split("/\r\n|\n|\r/", $return, null, PREG_SPLIT_NO_EMPTY);
}
} else {
// Explicit SFTP
$return = ftp_nlist($this->ftp, $path);
}
}
return $return;
}
// Private method to set the curl options for the transfer mode
private function getCurlXferMode() {
if($this->binary) {
return array(CURLOPT_BINARYTRANSFER => true);
return array(CURLOPT_TRANSFERTEXT => false);
} else {
return array(CURLOPT_TRANSFERTEXT => true);
}
}
// Private method to get the current mode for ftp_ functions
private function getMode() {
if($this->binary) {
return FTP_BINARY;
} else {
return FTP_ASCII;
}
}
// Private method to build a CUROPT_URL from our options
private function getCurlURL($path = '') {
if(!empty($path)) {
if(!preg_match('/^\//', $path)) {
// Force a fully qualified path to avoid issues
$path = "/{$path}";
}
}
return "ftps://{$this->host}:{$this->port}{$path}";
}
// Private method to make a cURL SFTP call based on the options passed in combined w/ the default ones.
private function callCurl($call_opts) {
$return = null;
// Combine our options, allowing those passed in to override defaults
$opts = $this->curlopts;
foreach($call_opts as $key => $value) {
$opts[$key] = $value;
}
// Init our curl handle and try to run the operation. If we get an error throw it as an exception
$this->ftp = curl_init();
if(curl_setopt_array($this->ftp, $opts)) {
$return = curl_exec($this->ftp);
$error = curl_error($this->ftp);
if(!empty($error)) {
throw new Exception($error);
}
}
curl_close($this->ftp);
return $return;
}
}
?>