blob: 2489f490f423141cab35841d189fb8eedb8617f0 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 Andreev0a75d6e2011-12-22 19:45:33 +020045 public $hostname = '';
Andrey Andreev597ea272012-11-01 22:56:26 +020046
47 /**
48 * FTP Username
49 *
50 * @var string
51 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +020052 public $username = '';
Andrey Andreev597ea272012-11-01 22:56:26 +020053
54 /**
55 * FTP Password
56 *
57 * @var string
58 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +020059 public $password = '';
Andrey Andreev597ea272012-11-01 22:56:26 +020060
61 /**
62 * FTP Server port
63 *
64 * @var int
65 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +020066 public $port = 21;
Andrey Andreev597ea272012-11-01 22:56:26 +020067
68 /**
69 * Passive mode flag
70 *
71 * @var bool
72 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +030073 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 Andreev0a75d6e2011-12-22 19:45:33 +020082 public $debug = FALSE;
Andrey Andreev597ea272012-11-01 22:56:26 +020083
84 /**
85 * Connection
86 *
87 * @var resource
88 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +030089 public $conn_id = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000090
Andrey Andreev597ea272012-11-01 22:56:26 +020091 // --------------------------------------------------------------------
92
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030093 /**
94 * Constructor
95 *
Andrey Andreev597ea272012-11-01 22:56:26 +020096 * @param array $config
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030097 * @return void
98 */
Greg Akera9263282010-11-10 15:26:43 -060099 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
101 if (count($config) > 0)
102 {
103 $this->initialize($config);
104 }
105
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300106 log_message('debug', 'FTP Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
108
109 // --------------------------------------------------------------------
110
111 /**
112 * Initialize preferences
113 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200114 * @param array $config
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @return void
116 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200117 public function initialize($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 foreach ($config as $key => $val)
120 {
121 if (isset($this->$key))
122 {
123 $this->$key = $val;
124 }
125 }
126
127 // Prep the hostname
128 $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
129 }
130
131 // --------------------------------------------------------------------
132
133 /**
134 * FTP Connect
135 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200136 * @param array $config Connection values
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @return bool
138 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200139 public function connect($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
141 if (count($config) > 0)
142 {
143 $this->initialize($config);
144 }
145
146 if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
147 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100148 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 $this->_error('ftp_unable_to_connect');
151 }
152 return FALSE;
153 }
154
155 if ( ! $this->_login())
156 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100157 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 {
159 $this->_error('ftp_unable_to_login');
160 }
161 return FALSE;
162 }
163
164 // Set passive mode if needed
Alex Bilbied261b1e2012-06-02 11:12:16 +0100165 if ($this->passive === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 {
167 ftp_pasv($this->conn_id, TRUE);
168 }
169
170 return TRUE;
171 }
172
173 // --------------------------------------------------------------------
174
175 /**
176 * FTP Login
177 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @return bool
179 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300180 protected function _login()
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 {
182 return @ftp_login($this->conn_id, $this->username, $this->password);
183 }
184
185 // --------------------------------------------------------------------
186
187 /**
188 * Validates the connection ID
189 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 * @return bool
191 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300192 protected function _is_conn()
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
194 if ( ! is_resource($this->conn_id))
195 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100196 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 $this->_error('ftp_no_connection');
199 }
200 return FALSE;
201 }
202 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
217 * @param bool $supress_debug
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 * @return bool
219 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200220 public function changedir($path = '', $supress_debug = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100222 if ($path === '' OR ! $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 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100231 if ($this->debug === TRUE && $supress_debug === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 {
233 $this->_error('ftp_unable_to_changedir');
234 }
235 return FALSE;
236 }
237
238 return TRUE;
239 }
240
241 // --------------------------------------------------------------------
242
243 /**
244 * Create a directory
245 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200246 * @param string $path
247 * @param int $permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 * @return bool
249 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200250 public function mkdir($path = '', $permissions = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100252 if ($path === '' OR ! $this->_is_conn())
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
254 return FALSE;
255 }
256
257 $result = @ftp_mkdir($this->conn_id, $path);
258
259 if ($result === FALSE)
260 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100261 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
263 $this->_error('ftp_unable_to_makdir');
264 }
265 return FALSE;
266 }
267
268 // Set file permissions if needed
vlakoff1228fe22013-01-14 01:30:09 +0100269 if ($permissions !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300271 $this->chmod($path, (int) $permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000272 }
273
274 return TRUE;
275 }
276
277 // --------------------------------------------------------------------
278
279 /**
280 * Upload a file to the server
281 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200282 * @param string $locpath
283 * @param string $rempath
284 * @param string $mode
285 * @param int $permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 * @return bool
287 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200288 public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 {
290 if ( ! $this->_is_conn())
291 {
292 return FALSE;
293 }
294
295 if ( ! file_exists($locpath))
296 {
297 $this->_error('ftp_no_source_file');
298 return FALSE;
299 }
300
301 // Set the mode if not specified
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200302 if ($mode === 'auto')
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 // Get the file extension so we can set the upload type
305 $ext = $this->_getext($locpath);
306 $mode = $this->_settype($ext);
307 }
308
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200309 $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
Derek Allard2067d1a2008-11-13 22:59:24 +0000310
311 $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
312
313 if ($result === FALSE)
314 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100315 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 $this->_error('ftp_unable_to_upload');
318 }
319 return FALSE;
320 }
321
322 // Set file permissions if needed
vlakoff1228fe22013-01-14 01:30:09 +0100323 if ($permissions !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300325 $this->chmod($rempath, (int) $permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 }
327
328 return TRUE;
329 }
330
331 // --------------------------------------------------------------------
332
333 /**
Phil Sturgeon28b29372010-03-12 00:43:28 +0000334 * Download a file from a remote server to the local server
335 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200336 * @param string $rempath
337 * @param string $locpath
338 * @param string $mode
Phil Sturgeon28b29372010-03-12 00:43:28 +0000339 * @return bool
340 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200341 public function download($rempath, $locpath, $mode = 'auto')
Phil Sturgeon28b29372010-03-12 00:43:28 +0000342 {
343 if ( ! $this->_is_conn())
344 {
345 return FALSE;
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Phil Sturgeon28b29372010-03-12 00:43:28 +0000348 // Set the mode if not specified
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200349 if ($mode === 'auto')
Phil Sturgeon28b29372010-03-12 00:43:28 +0000350 {
351 // Get the file extension so we can set the upload type
352 $ext = $this->_getext($rempath);
353 $mode = $this->_settype($ext);
354 }
Barry Mienydd671972010-10-04 16:33:58 +0200355
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200356 $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
Barry Mienydd671972010-10-04 16:33:58 +0200357
Phil Sturgeon28b29372010-03-12 00:43:28 +0000358 $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);
359
360 if ($result === FALSE)
361 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100362 if ($this->debug === TRUE)
Phil Sturgeon28b29372010-03-12 00:43:28 +0000363 {
364 $this->_error('ftp_unable_to_download');
365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366 return FALSE;
Phil Sturgeon28b29372010-03-12 00:43:28 +0000367 }
Barry Mienydd671972010-10-04 16:33:58 +0200368
Phil Sturgeon28b29372010-03-12 00:43:28 +0000369 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200370 }
Phil Sturgeon28b29372010-03-12 00:43:28 +0000371
372 // --------------------------------------------------------------------
373
374 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 * Rename (or move) a file
376 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200377 * @param string $old_file
378 * @param string $new_file
379 * @param bool $move
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 * @return bool
381 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200382 public function rename($old_file, $new_file, $move = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 {
384 if ( ! $this->_is_conn())
385 {
386 return FALSE;
387 }
388
389 $result = @ftp_rename($this->conn_id, $old_file, $new_file);
390
391 if ($result === FALSE)
392 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100393 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 {
Andrey Andreev3ca060a2013-11-27 16:30:31 +0200395 $this->_error('ftp_unable_to_'.($move === FALSE ? 'rename' : 'move'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 }
397 return FALSE;
398 }
399
400 return TRUE;
401 }
402
403 // --------------------------------------------------------------------
404
405 /**
406 * Move a file
407 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200408 * @param string $old_file
409 * @param string $new_file
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 * @return bool
411 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200412 public function move($old_file, $new_file)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return $this->rename($old_file, $new_file, TRUE);
415 }
416
417 // --------------------------------------------------------------------
418
419 /**
420 * Rename (or move) a file
421 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200422 * @param string $filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 * @return bool
424 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200425 public function delete_file($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 if ( ! $this->_is_conn())
428 {
429 return FALSE;
430 }
431
432 $result = @ftp_delete($this->conn_id, $filepath);
433
434 if ($result === FALSE)
435 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100436 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
438 $this->_error('ftp_unable_to_delete');
439 }
440 return FALSE;
441 }
442
443 return TRUE;
444 }
445
446 // --------------------------------------------------------------------
447
448 /**
449 * Delete a folder and recursively delete everything (including sub-folders)
450 * containted within it.
451 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200452 * @param string $filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 * @return bool
454 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200455 public function delete_dir($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 {
457 if ( ! $this->_is_conn())
458 {
459 return FALSE;
460 }
461
462 // Add a trailing slash to the file path if needed
Andrey Andreev838a9d62012-12-03 14:37:47 +0200463 $filepath = preg_replace('/(.+?)\/*$/', '\\1/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000464
465 $list = $this->list_files($filepath);
466
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300467 if ($list !== FALSE && count($list) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 foreach ($list as $item)
470 {
471 // If we can't delete the item it's probaly a folder so
472 // we'll recursively call delete_dir()
473 if ( ! @ftp_delete($this->conn_id, $item))
474 {
475 $this->delete_dir($item);
476 }
477 }
478 }
479
480 $result = @ftp_rmdir($this->conn_id, $filepath);
481
482 if ($result === FALSE)
483 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100484 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 {
486 $this->_error('ftp_unable_to_delete');
487 }
488 return FALSE;
489 }
490
491 return TRUE;
492 }
493
494 // --------------------------------------------------------------------
495
496 /**
497 * Set file permissions
498 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200499 * @param string $path File path
500 * @param int $perm Permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 * @return bool
502 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200503 public function chmod($path, $perm)
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 {
505 if ( ! $this->_is_conn())
506 {
507 return FALSE;
508 }
509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 $result = @ftp_chmod($this->conn_id, $perm, $path);
511
512 if ($result === FALSE)
513 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100514 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 {
516 $this->_error('ftp_unable_to_chmod');
517 }
518 return FALSE;
519 }
520
521 return TRUE;
522 }
523
524 // --------------------------------------------------------------------
525
526 /**
527 * FTP List files in the specified directory
528 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200529 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 * @return array
531 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200532 public function list_files($path = '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 if ( ! $this->_is_conn())
535 {
536 return FALSE;
537 }
538
539 return ftp_nlist($this->conn_id, $path);
540 }
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 }
588 return TRUE;
589 }
590
591 return FALSE;
592 }
593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 // --------------------------------------------------------------------
595
596 /**
597 * Extract the file extension
598 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200599 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 * @return string
601 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300602 protected function _getext($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
604 if (FALSE === strpos($filename, '.'))
605 {
606 return 'txt';
607 }
608
609 $x = explode('.', $filename);
610 return end($x);
611 }
612
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 // --------------------------------------------------------------------
614
615 /**
616 * Set the upload type
617 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200618 * @param string $ext Filename extension
Derek Allard2067d1a2008-11-13 22:59:24 +0000619 * @return string
620 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300621 protected function _settype($ext)
Derek Allard2067d1a2008-11-13 22:59:24 +0000622 {
623 $text_types = array(
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300624 'txt',
625 'text',
626 'php',
627 'phps',
628 'php4',
629 'js',
630 'css',
631 'htm',
632 'html',
633 'phtml',
634 'shtml',
635 'log',
636 'xml'
637 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000638
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300639 return in_array($ext, $text_types) ? 'ascii' : 'binary';
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
641
642 // ------------------------------------------------------------------------
643
644 /**
645 * Close the connection
646 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000647 * @return bool
648 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200649 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
651 if ( ! $this->_is_conn())
652 {
653 return FALSE;
654 }
655
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200656 return @ftp_close($this->conn_id);
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
658
659 // ------------------------------------------------------------------------
660
661 /**
662 * Display error message
663 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200664 * @param string $line
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200665 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300667 protected function _error($line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 {
669 $CI =& get_instance();
670 $CI->lang->load('ftp');
671 show_error($CI->lang->line($line));
672 }
673
Derek Allard2067d1a2008-11-13 22:59:24 +0000674}
Derek Allard2067d1a2008-11-13 22:59:24 +0000675
676/* End of file Ftp.php */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300677/* Location: ./system/libraries/Ftp.php */