blob: fc02f8300458e84323025fe356d66d0499d0b16f [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev0a75d6e2011-12-22 19:45:33 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev0a75d6e2011-12-22 19:45:33 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * FTP Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/ftp.html
37 */
38class CI_FTP {
39
Andrey Andreev597ea272012-11-01 22:56:26 +020040 /**
41 * FTP Server hostname
42 *
43 * @var string
44 */
Andrey Andreeve52e4262014-02-21 17:11:54 +020045 public $hostname = '';
Andrey Andreev597ea272012-11-01 22:56:26 +020046
47 /**
48 * FTP Username
49 *
50 * @var string
51 */
Andrey Andreeve52e4262014-02-21 17:11:54 +020052 public $username = '';
Andrey Andreev597ea272012-11-01 22:56:26 +020053
54 /**
55 * FTP Password
56 *
57 * @var string
58 */
Andrey Andreeve52e4262014-02-21 17:11:54 +020059 public $password = '';
Andrey Andreev597ea272012-11-01 22:56:26 +020060
61 /**
62 * FTP Server port
63 *
64 * @var int
65 */
Andrey Andreeve52e4262014-02-21 17:11:54 +020066 public $port = 21;
Andrey Andreev597ea272012-11-01 22:56:26 +020067
68 /**
69 * Passive mode flag
70 *
71 * @var bool
72 */
Andrey Andreeve52e4262014-02-21 17:11:54 +020073 public $passive = TRUE;
Andrey Andreev597ea272012-11-01 22:56:26 +020074
75 /**
76 * Debug flag
77 *
78 * Specifies whether to display error messages.
79 *
80 * @var bool
81 */
Andrey Andreeve52e4262014-02-21 17:11:54 +020082 public $debug = FALSE;
83
84 // --------------------------------------------------------------------
Andrey Andreev597ea272012-11-01 22:56:26 +020085
86 /**
Andrey Andreeve52e4262014-02-21 17:11:54 +020087 * Connection ID
Andrey Andreev597ea272012-11-01 22:56:26 +020088 *
89 * @var resource
90 */
Andrey Andreeve52e4262014-02-21 17:11:54 +020091 protected $conn_id;
Derek Allard2067d1a2008-11-13 22:59:24 +000092
Andrey Andreev597ea272012-11-01 22:56:26 +020093 // --------------------------------------------------------------------
94
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030095 /**
96 * Constructor
97 *
Andrey Andreev597ea272012-11-01 22:56:26 +020098 * @param array $config
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030099 * @return void
100 */
Greg Akera9263282010-11-10 15:26:43 -0600101 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 {
Andrey Andreeve52e4262014-02-21 17:11:54 +0200103 empty($config) OR $this->initialize($config);
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300104 log_message('debug', 'FTP Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 }
106
107 // --------------------------------------------------------------------
108
109 /**
110 * Initialize preferences
111 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200112 * @param array $config
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @return void
114 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200115 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 foreach ($config as $key => $val)
118 {
119 if (isset($this->$key))
120 {
121 $this->$key = $val;
122 }
123 }
124
125 // Prep the hostname
126 $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
127 }
128
129 // --------------------------------------------------------------------
130
131 /**
132 * FTP Connect
133 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200134 * @param array $config Connection values
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 * @return bool
136 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200137 public function connect($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 if (count($config) > 0)
140 {
141 $this->initialize($config);
142 }
143
144 if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
145 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100146 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 {
148 $this->_error('ftp_unable_to_connect');
149 }
150 return FALSE;
151 }
152
153 if ( ! $this->_login())
154 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100155 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
157 $this->_error('ftp_unable_to_login');
158 }
159 return FALSE;
160 }
161
162 // Set passive mode if needed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100163 if ($this->passive === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
165 ftp_pasv($this->conn_id, TRUE);
166 }
167
168 return TRUE;
169 }
170
171 // --------------------------------------------------------------------
172
173 /**
174 * FTP Login
175 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @return bool
177 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300178 protected function _login()
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
180 return @ftp_login($this->conn_id, $this->username, $this->password);
181 }
182
183 // --------------------------------------------------------------------
184
185 /**
186 * Validates the connection ID
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @return bool
189 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300190 protected function _is_conn()
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
192 if ( ! is_resource($this->conn_id))
193 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100194 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
196 $this->_error('ftp_no_connection');
197 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 return FALSE;
200 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 return TRUE;
203 }
204
205 // --------------------------------------------------------------------
206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 /**
Derek Allarda14ab122009-07-13 12:01:01 +0000208 * Change directory
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 *
210 * The second parameter lets us momentarily turn off debugging so that
Derek Allarda14ab122009-07-13 12:01:01 +0000211 * this function can be used to test for the existence of a folder
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300212 * without throwing an error. There's no FTP equivalent to is_dir()
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * so we do it by trying to change to a particular directory.
Derek Allarda14ab122009-07-13 12:01:01 +0000214 * Internally, this parameter is only used by the "mirror" function below.
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200216 * @param string $path
Andrey Andreevb4c62182014-01-03 17:39:26 +0200217 * @param bool $suppress_debug
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @return bool
219 */
Andrey Andreevb4c62182014-01-03 17:39:26 +0200220 public function changedir($path, $suppress_debug = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Andrey Andreev45549a22014-01-06 13:55:38 +0200222 if ( ! $this->_is_conn())
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
224 return FALSE;
225 }
226
227 $result = @ftp_chdir($this->conn_id, $path);
228
229 if ($result === FALSE)
230 {
Andrey Andreevb4c62182014-01-03 17:39:26 +0200231 if ($this->debug === TRUE && $suppress_debug === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
233 $this->_error('ftp_unable_to_changedir');
234 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 return FALSE;
237 }
238
239 return TRUE;
240 }
241
242 // --------------------------------------------------------------------
243
244 /**
245 * Create a directory
246 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200247 * @param string $path
248 * @param int $permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 * @return bool
250 */
Andrey Andreev430b14c2014-01-03 17:31:26 +0200251 public function mkdir($path, $permissions = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000252 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100253 if ($path === '' OR ! $this->_is_conn())
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 return FALSE;
256 }
257
258 $result = @ftp_mkdir($this->conn_id, $path);
259
260 if ($result === FALSE)
261 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100262 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 {
Andrey Andreevb4c62182014-01-03 17:39:26 +0200264 $this->_error('ftp_unable_to_mkdir');
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200266
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 return FALSE;
268 }
269
270 // Set file permissions if needed
vlakoff1228fe22013-01-14 01:30:09 +0100271 if ($permissions !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300273 $this->chmod($path, (int) $permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275
276 return TRUE;
277 }
278
279 // --------------------------------------------------------------------
280
281 /**
282 * Upload a file to the server
283 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200284 * @param string $locpath
285 * @param string $rempath
286 * @param string $mode
287 * @param int $permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 * @return bool
289 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200290 public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
292 if ( ! $this->_is_conn())
293 {
294 return FALSE;
295 }
296
297 if ( ! file_exists($locpath))
298 {
299 $this->_error('ftp_no_source_file');
300 return FALSE;
301 }
302
303 // Set the mode if not specified
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200304 if ($mode === 'auto')
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
306 // Get the file extension so we can set the upload type
307 $ext = $this->_getext($locpath);
308 $mode = $this->_settype($ext);
309 }
310
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200311 $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312
313 $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
314
315 if ($result === FALSE)
316 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100317 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $this->_error('ftp_unable_to_upload');
320 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 return FALSE;
323 }
324
325 // Set file permissions if needed
vlakoff1228fe22013-01-14 01:30:09 +0100326 if ($permissions !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300328 $this->chmod($rempath, (int) $permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 }
330
331 return TRUE;
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
Phil Sturgeon28b29372010-03-12 00:43:28 +0000337 * Download a file from a remote server to the local server
338 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200339 * @param string $rempath
340 * @param string $locpath
341 * @param string $mode
Phil Sturgeon28b29372010-03-12 00:43:28 +0000342 * @return bool
343 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200344 public function download($rempath, $locpath, $mode = 'auto')
Phil Sturgeon28b29372010-03-12 00:43:28 +0000345 {
346 if ( ! $this->_is_conn())
347 {
348 return FALSE;
349 }
Barry Mienydd671972010-10-04 16:33:58 +0200350
Phil Sturgeon28b29372010-03-12 00:43:28 +0000351 // Set the mode if not specified
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200352 if ($mode === 'auto')
Phil Sturgeon28b29372010-03-12 00:43:28 +0000353 {
354 // Get the file extension so we can set the upload type
355 $ext = $this->_getext($rempath);
356 $mode = $this->_settype($ext);
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200359 $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
Barry Mienydd671972010-10-04 16:33:58 +0200360
Phil Sturgeon28b29372010-03-12 00:43:28 +0000361 $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);
362
363 if ($result === FALSE)
364 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100365 if ($this->debug === TRUE)
Phil Sturgeon28b29372010-03-12 00:43:28 +0000366 {
367 $this->_error('ftp_unable_to_download');
368 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200369
Barry Mienydd671972010-10-04 16:33:58 +0200370 return FALSE;
Phil Sturgeon28b29372010-03-12 00:43:28 +0000371 }
Barry Mienydd671972010-10-04 16:33:58 +0200372
Phil Sturgeon28b29372010-03-12 00:43:28 +0000373 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200374 }
Phil Sturgeon28b29372010-03-12 00:43:28 +0000375
376 // --------------------------------------------------------------------
377
378 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 * Rename (or move) a file
380 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200381 * @param string $old_file
382 * @param string $new_file
383 * @param bool $move
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @return bool
385 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200386 public function rename($old_file, $new_file, $move = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 if ( ! $this->_is_conn())
389 {
390 return FALSE;
391 }
392
393 $result = @ftp_rename($this->conn_id, $old_file, $new_file);
394
395 if ($result === FALSE)
396 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100397 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
Andrey Andreev3ca060a2013-11-27 16:30:31 +0200399 $this->_error('ftp_unable_to_'.($move === FALSE ? 'rename' : 'move'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 return FALSE;
403 }
404
405 return TRUE;
406 }
407
408 // --------------------------------------------------------------------
409
410 /**
411 * Move a file
412 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200413 * @param string $old_file
414 * @param string $new_file
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 * @return bool
416 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200417 public function move($old_file, $new_file)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 return $this->rename($old_file, $new_file, TRUE);
420 }
421
422 // --------------------------------------------------------------------
423
424 /**
425 * Rename (or move) a file
426 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200427 * @param string $filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @return bool
429 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200430 public function delete_file($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
432 if ( ! $this->_is_conn())
433 {
434 return FALSE;
435 }
436
437 $result = @ftp_delete($this->conn_id, $filepath);
438
439 if ($result === FALSE)
440 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100441 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 $this->_error('ftp_unable_to_delete');
444 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 return FALSE;
447 }
448
449 return TRUE;
450 }
451
452 // --------------------------------------------------------------------
453
454 /**
455 * Delete a folder and recursively delete everything (including sub-folders)
456 * containted within it.
457 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200458 * @param string $filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 * @return bool
460 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200461 public function delete_dir($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 {
463 if ( ! $this->_is_conn())
464 {
465 return FALSE;
466 }
467
468 // Add a trailing slash to the file path if needed
Andrey Andreev838a9d62012-12-03 14:37:47 +0200469 $filepath = preg_replace('/(.+?)\/*$/', '\\1/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000470
471 $list = $this->list_files($filepath);
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200472 if ( ! empty($list))
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200474 for ($i = 0, $c = count($list); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200476 // If we can't delete the item it's probaly a directory,
477 // so we'll recursively call delete_dir()
478 if ( ! @ftp_delete($this->conn_id, $list[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200480 $this->delete_dir($list[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
482 }
483 }
484
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200485 if (@ftp_rmdir($this->conn_id, $filepath) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100487 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 $this->_error('ftp_unable_to_delete');
490 }
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 return FALSE;
493 }
494
495 return TRUE;
496 }
497
498 // --------------------------------------------------------------------
499
500 /**
501 * Set file permissions
502 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200503 * @param string $path File path
504 * @param int $perm Permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000505 * @return bool
506 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200507 public function chmod($path, $perm)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 if ( ! $this->_is_conn())
510 {
511 return FALSE;
512 }
513
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200514 if (@ftp_chmod($this->conn_id, $perm, $path) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100516 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 $this->_error('ftp_unable_to_chmod');
519 }
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 return FALSE;
522 }
523
524 return TRUE;
525 }
526
527 // --------------------------------------------------------------------
528
529 /**
530 * FTP List files in the specified directory
531 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200532 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @return array
534 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200535 public function list_files($path = '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200537 return $this->_is_conn()
538 ? ftp_nlist($this->conn_id, $path)
539 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541
542 // ------------------------------------------------------------------------
543
544 /**
545 * Read a directory and recreate it remotely
546 *
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300547 * This function recursively reads a folder and everything it contains
548 * (including sub-folders) and creates a mirror via FTP based on it.
549 * Whatever the directory structure of the original file path will be
550 * recreated on the server.
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200552 * @param string $locpath Path to source with trailing slash
553 * @param string $rempath Path to destination - include the base folder with trailing slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 * @return bool
555 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200556 public function mirror($locpath, $rempath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
558 if ( ! $this->_is_conn())
559 {
560 return FALSE;
561 }
562
563 // Open the local file path
564 if ($fp = @opendir($locpath))
565 {
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200566 // Attempt to open the remote file path and try to create it, if it doesn't exist
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300567 if ( ! $this->changedir($rempath, TRUE) && ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200569 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 }
571
572 // Recursively read the local directory
573 while (FALSE !== ($file = readdir($fp)))
574 {
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200575 if (@is_dir($locpath.$file) && $file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300577 $this->mirror($locpath.$file.'/', $rempath.$file.'/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 }
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300579 elseif ($file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
581 // Get the file extension so we can se the upload type
582 $ext = $this->_getext($file);
583 $mode = $this->_settype($ext);
584
585 $this->upload($locpath.$file, $rempath.$file, $mode);
586 }
587 }
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200588
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 return TRUE;
590 }
591
592 return FALSE;
593 }
594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 // --------------------------------------------------------------------
596
597 /**
598 * Extract the file extension
599 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200600 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 * @return string
602 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300603 protected function _getext($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
Andrey Andreeve52e4262014-02-21 17:11:54 +0200605 return (($dot = strrpos($filename, '.')) === FALSE)
606 ? 'txt'
607 : substr($filename, $dot + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
609
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 // --------------------------------------------------------------------
611
612 /**
613 * Set the upload type
614 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200615 * @param string $ext Filename extension
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 * @return string
617 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300618 protected function _settype($ext)
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 {
Andrey Andreeve52e4262014-02-21 17:11:54 +0200620 return in_array($ext, array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'), TRUE)
621 ? 'ascii'
622 : 'binary';
Derek Allard2067d1a2008-11-13 22:59:24 +0000623 }
624
625 // ------------------------------------------------------------------------
626
627 /**
628 * Close the connection
629 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 * @return bool
631 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200632 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200634 return $this->_is_conn()
635 ? @ftp_close($this->conn_id)
636 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 }
638
639 // ------------------------------------------------------------------------
640
641 /**
642 * Display error message
643 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200644 * @param string $line
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200645 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300647 protected function _error($line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 {
649 $CI =& get_instance();
650 $CI->lang->load('ftp');
651 show_error($CI->lang->line($line));
652 }
653
Derek Allard2067d1a2008-11-13 22:59:24 +0000654}
Derek Allard2067d1a2008-11-13 22:59:24 +0000655
656/* End of file Ftp.php */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300657/* Location: ./system/libraries/Ftp.php */