This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
FTP.php
319 lines (267 loc) · 6.95 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
<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');
/**
* Contao Open Source CMS
* Copyright (C) 2005-2010 Leo Feyer
*
* Formerly known as TYPOlight Open Source CMS.
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, please visit the Free
* Software Foundation website at <http://www.gnu.org/licenses/>.
*
* PHP version 5
* @copyright Leo Feyer 2005-2010
* @author Leo Feyer <http://www.contao.org>
* @package System
* @license LGPL
* @filesource
*/
/**
* Class FTP
*
* Provide methods to modify files and folders via FTP.
* @copyright Leo Feyer 2005-2010
* @author Leo Feyer <http://www.contao.org>
* @package Library
*/
class FTP extends Files
{
/**
* FTP connection
* @var resource
*/
protected $resConnection;
/**
* Is connected
* @var boolean
*/
protected $blnIsConnected = false;
/**
* Files array
* @var array
*/
protected $arrFiles = array();
/**
* Disconnect from ftp server
*/
public function __destruct()
{
if ($this->blnIsConnected)
{
@ftp_close($this->resConnection);
}
}
/**
* Establish an FTP connection
* @throws Exception
*/
public function connect()
{
if ($this->blnIsConnected)
{
return;
}
// Check the FTP credentials
if ($GLOBALS['TL_CONFIG']['ftpHost'] == '')
{
throw new Exception('The FTP host must not be empty');
}
elseif ($GLOBALS['TL_CONFIG']['ftpUser'] == '')
{
throw new Exception('The FTP username must not be empty');
}
elseif ($GLOBALS['TL_CONFIG']['ftpPass'] == '')
{
throw new Exception('The FTP password must not be empty');
}
$ftp_connect = ($GLOBALS['TL_CONFIG']['ftpSSL'] && function_exists('ftp_ssl_connect')) ? 'ftp_ssl_connect' : 'ftp_connect';
// Try to connect
if (($resConnection = $ftp_connect($GLOBALS['TL_CONFIG']['ftpHost'], $GLOBALS['TL_CONFIG']['ftpPort'], 5)) == false)
{
throw new Exception('Could not connect to the FTP server');
}
// Try to login
elseif (ftp_login($resConnection, $GLOBALS['TL_CONFIG']['ftpUser'], $GLOBALS['TL_CONFIG']['ftpPass']) == false)
{
throw new Exception('Authentication failed');
}
// Switch to passive mode
ftp_pasv($resConnection, true);
$this->blnIsConnected = true;
$this->resConnection = $resConnection;
// Make folders writable
if (!is_writable(TL_ROOT . '/system/tmp'))
{
$this->chmod('system/tmp', 0777);
}
if (!is_writable(TL_ROOT . '/system/html'))
{
$this->chmod('system/html', 0777);
}
if (!is_writable(TL_ROOT . '/system/logs'))
{
$this->chmod('system/logs', 0777);
}
}
/**
* Create a directory
* @param string
* @return boolean
*/
public function mkdir($strDirectory)
{
$this->connect();
return @ftp_mkdir($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strDirectory) ? true : false;
}
/**
* Remove a directory
* @param string
* @return boolean
*/
public function rmdir($strDirectory)
{
$this->connect();
return @ftp_rmdir($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strDirectory);
}
/**
* Open a file and return the handle
* @param string
* @param string
* @return resource
*/
public function fopen($strFile, $strMode)
{
$resFile = fopen(TL_ROOT . '/system/tmp/' . md5(uniqid(mt_rand(), true)), $strMode);
// Copy the temp file
if (!file_exists(TL_ROOT . '/' . $strFile))
{
$this->connect();
if (!@ftp_fput($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strFile, $resFile, FTP_BINARY))
{
return false;
}
}
$arrData = stream_get_meta_data($resFile);
$this->arrFiles[$arrData['uri']] = $strFile;
return $resFile;
}
/**
* Close a file
* @param resource
* @return boolean
*/
public function fclose($resFile)
{
if (!is_resource($resFile))
{
return true;
}
$arrData = stream_get_meta_data($resFile);
$fclose = fclose($resFile);
// Move the temp file
if (isset($this->arrFiles[$arrData['uri']]))
{
$this->rename(preg_replace('/^' . preg_quote(TL_ROOT, '/') . '\//i', '', $arrData['uri']), $this->arrFiles[$arrData['uri']]);
}
return $fclose;
}
/**
* Rename a file or folder
* @param string
* @param string
* @return boolean
*/
public function rename($strOldName, $strNewName)
{
$this->connect();
// Windows fix: delete target file
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' && file_exists(TL_ROOT . '/' . $strNewName))
{
$this->delete($strNewName);
}
// Rename directories
if (is_dir(TL_ROOT . '/' . $strOldName))
{
return @ftp_rename($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strOldName, $GLOBALS['TL_CONFIG']['ftpPath'] . $strNewName);
}
// Unix fix: rename case sensitively
if (strcasecmp($strOldName, $strNewName) === 0 && strcmp($strOldName, $strNewName) !== 0)
{
@ftp_rename($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strOldName, $GLOBALS['TL_CONFIG']['ftpPath'] . $strOldName . '__');
$strOldName .= '__';
}
// Copy files to set the correct owner
$return = $this->copy($strOldName, $strNewName);
// Delete the old file
if (!@unlink(TL_ROOT . '/' . $strOldName))
{
$this->delete($strOldName);
}
return $return;
}
/**
* Copy a file or folder
* @param string
* @param string
* @return boolean
*/
public function copy($strSource, $strDestination)
{
$this->connect();
$return = @ftp_put($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strDestination, TL_ROOT . '/' . $strSource, FTP_BINARY);
$this->chmod($strDestination, 0644);
return $return;
}
/**
* Delete a file
* @param string
* @return boolean
*/
public function delete($strFile)
{
$this->connect();
return @ftp_delete($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strFile);
}
/**
* Change file mode
* @param string
* @param mixed
* @return boolean
*/
public function chmod($strFile, $varMode)
{
$this->connect();
return @ftp_chmod($this->resConnection, $varMode, $GLOBALS['TL_CONFIG']['ftpPath'] . $strFile);
}
/**
* Check whether a file is writeable
* @param string
* @return boolean
*/
public function is_writeable($strFile)
{
return true;
}
/**
* Move an uploaded file to another folder
* @param string
* @param string
* @return string
*/
public function move_uploaded_file($strSource, $strDestination)
{
$this->connect();
return @ftp_put($this->resConnection, $GLOBALS['TL_CONFIG']['ftpPath'] . $strDestination, $strSource, FTP_BINARY);
}
}
?>