-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFTP_Implicit_SSL.php
180 lines (158 loc) · 5.99 KB
/
FTP_Implicit_SSL.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
<?php
/**
* FTP with Implicit SSL/TLS Class
*
* Simple wrapper for cURL functions to transfer an ASCII file over FTP with implicit SSL/TLS
*
* @category Class
* @author Damith Jayasinghe
* @since 1.8
*/
class FTP_Implicit_SSL {
/** @var resource cURL resource handle */
private $curl_handle;
/** @var string cURL URL for upload */
private $url;
/**
* Connect to FTP server over Implicit SSL/TLS
*
* @param string $username
* @param string $password
* @param string $server
* @param int $port
* @param string $initial_path
* @param bool $passive_mode
* @throws Exception
*/
public function __construct($username, $password, $server, $port = 990, $initial_path = '', $passive_mode = false) {
// Check for blank username, password, and server
if (empty($username) || empty($server)) {
throw new Exception('FTP Username or Server is blank.');
}
// Set host/initial path
$this->url = "ftps://{$server}/{$initial_path}";
// Initialize cURL handle
$this->curl_handle = curl_init();
// Check for successful connection
if (!$this->curl_handle) {
throw new Exception('Could not initialize cURL.');
}
// Connection options
$options = [
CURLOPT_USERPWD => "$username:$password",
CURLOPT_SSL_VERIFYPEER => false, // Don't verify SSL
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FTP_SSL => CURLFTPSSL_ALL, // Require SSL for both control and data connections
CURLOPT_FTPSSLAUTH => CURLFTPAUTH_DEFAULT, // Let cURL choose the FTP authentication method (either SSL or TLS)
CURLOPT_UPLOAD => true,
CURLOPT_PORT => $port,
CURLOPT_TIMEOUT => 30,
];
// Enable active mode if passive mode is disabled
if (!$passive_mode) {
$options[CURLOPT_FTPPORT] = '-';
}
// Set connection options individually to catch errors
foreach ($options as $option_name => $option_value) {
if (!curl_setopt($this->curl_handle, $option_name, $option_value)) {
throw new Exception("Could not set cURL option: $option_name");
}
}
}
/**
* Upload file to FTP server
*
* @param string $file_name
* @param string $file
* @throws Exception
*/
public function upload($file_name, $file) {
// Set file name
if (!curl_setopt($this->curl_handle, CURLOPT_URL, $this->url . $file_name)) {
throw new Exception("Could not set cURL file name: $file_name");
}
// Open memory stream for writing
$stream = fopen('php://temp', 'w+');
// Check for valid stream handle
if (!$stream) {
throw new Exception('Could not open php://temp for writing.');
}
// Write file into the temporary stream
fwrite($stream, $file);
// Set the file to be uploaded
curl_setopt($this->curl_handle, CURLOPT_INFILE, $stream);
// Upload file
if (!curl_exec($this->curl_handle)) {
throw new Exception(sprintf('Could not upload file. cURL Error: [%s] - %s', curl_errno($this->curl_handle), curl_error($this->curl_handle)));
}
// Close the stream handle
fclose($stream);
}
/**
* Get list of files on FTP server
*
* @return array
* @throws Exception
*/
public function ftpfilelist() {
curl_setopt($this->curl_handle, CURLOPT_URL, $this->url);
curl_setopt($this->curl_handle, CURLOPT_UPLOAD, false);
curl_setopt($this->curl_handle, CURLOPT_FTPLISTONLY, 1);
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($this->curl_handle);
if ($result === false) {
throw new Exception("Failed to retrieve file list. Error: " . curl_error($this->curl_handle));
}
return explode("\n", trim($result));
}
/**
* Download file from FTP server
*
* @param string $file_name
* @param string $local_path
* @return string
* @throws Exception
*/
public function download($file_name, $local_path = '/') {
$file = fopen("$local_path$file_name", "w");
if (!$file) {
throw new Exception("Failed to open local file for writing: $local_path$file_name");
}
curl_setopt($this->curl_handle, CURLOPT_URL, $this->url . $file_name);
curl_setopt($this->curl_handle, CURLOPT_UPLOAD, false);
curl_setopt($this->curl_handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->curl_handle, CURLOPT_FILE, $file);
if (!curl_exec($this->curl_handle)) {
throw new Exception(sprintf('Failed to download file. cURL Error: [%s] - %s', curl_errno($this->curl_handle), curl_error($this->curl_handle)));
}
fclose($file);
return "Download successful: $file_name";
}
/**
* Get remote file size
*
* @param string $file_name
* @return int
* @throws Exception
*/
public function remote_file_size($file_name) {
curl_setopt($this->curl_handle, CURLOPT_URL, $this->url . $file_name);
curl_setopt($this->curl_handle, CURLOPT_UPLOAD, false);
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl_handle, CURLOPT_HEADER, true);
curl_setopt($this->curl_handle, CURLOPT_NOBODY, true);
$data = curl_exec($this->curl_handle);
if ($data === false) {
throw new Exception("Failed to get remote file size for: $file_name. Error: " . curl_error($this->curl_handle));
}
return curl_getinfo($this->curl_handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
}
/**
* Close cURL handle
*/
public function __destruct() {
curl_close($this->curl_handle);
}
}
?>