blob: 8401a380e6eae5f81c4bfc429f18192179590af5 [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 }
Andrey Andreev382b5132014-02-26 18:41:59 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 return FALSE;
152 }
153
154 if ( ! $this->_login())
155 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100156 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 $this->_error('ftp_unable_to_login');
159 }
Andrey Andreev382b5132014-02-26 18:41:59 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 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 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 return FALSE;
202 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 return TRUE;
205 }
206
207 // --------------------------------------------------------------------
208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 /**
Derek Allarda14ab122009-07-13 12:01:01 +0000210 * Change directory
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 *
212 * The second parameter lets us momentarily turn off debugging so that
Derek Allarda14ab122009-07-13 12:01:01 +0000213 * this function can be used to test for the existence of a folder
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300214 * without throwing an error. There's no FTP equivalent to is_dir()
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 * so we do it by trying to change to a particular directory.
Derek Allarda14ab122009-07-13 12:01:01 +0000216 * Internally, this parameter is only used by the "mirror" function below.
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200218 * @param string $path
Andrey Andreevb4c62182014-01-03 17:39:26 +0200219 * @param bool $suppress_debug
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 * @return bool
221 */
Andrey Andreevb4c62182014-01-03 17:39:26 +0200222 public function changedir($path, $suppress_debug = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
Andrey Andreev45549a22014-01-06 13:55:38 +0200224 if ( ! $this->_is_conn())
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
226 return FALSE;
227 }
228
229 $result = @ftp_chdir($this->conn_id, $path);
230
231 if ($result === FALSE)
232 {
Andrey Andreevb4c62182014-01-03 17:39:26 +0200233 if ($this->debug === TRUE && $suppress_debug === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
235 $this->_error('ftp_unable_to_changedir');
236 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 return FALSE;
239 }
240
241 return TRUE;
242 }
243
244 // --------------------------------------------------------------------
245
246 /**
247 * Create a directory
248 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200249 * @param string $path
250 * @param int $permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 * @return bool
252 */
Andrey Andreev430b14c2014-01-03 17:31:26 +0200253 public function mkdir($path, $permissions = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100255 if ($path === '' OR ! $this->_is_conn())
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
257 return FALSE;
258 }
259
260 $result = @ftp_mkdir($this->conn_id, $path);
261
262 if ($result === FALSE)
263 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100264 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreevb4c62182014-01-03 17:39:26 +0200266 $this->_error('ftp_unable_to_mkdir');
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 return FALSE;
270 }
271
272 // Set file permissions if needed
vlakoff1228fe22013-01-14 01:30:09 +0100273 if ($permissions !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300275 $this->chmod($path, (int) $permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 }
277
278 return TRUE;
279 }
280
281 // --------------------------------------------------------------------
282
283 /**
284 * Upload a file to the server
285 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200286 * @param string $locpath
287 * @param string $rempath
288 * @param string $mode
289 * @param int $permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 * @return bool
291 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200292 public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 if ( ! $this->_is_conn())
295 {
296 return FALSE;
297 }
298
299 if ( ! file_exists($locpath))
300 {
301 $this->_error('ftp_no_source_file');
302 return FALSE;
303 }
304
305 // Set the mode if not specified
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200306 if ($mode === 'auto')
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 // Get the file extension so we can set the upload type
309 $ext = $this->_getext($locpath);
310 $mode = $this->_settype($ext);
311 }
312
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200313 $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
Derek Allard2067d1a2008-11-13 22:59:24 +0000314
315 $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
316
317 if ($result === FALSE)
318 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100319 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
321 $this->_error('ftp_unable_to_upload');
322 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 return FALSE;
325 }
326
327 // Set file permissions if needed
vlakoff1228fe22013-01-14 01:30:09 +0100328 if ($permissions !== NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300330 $this->chmod($rempath, (int) $permissions);
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 }
332
333 return TRUE;
334 }
335
336 // --------------------------------------------------------------------
337
338 /**
Phil Sturgeon28b29372010-03-12 00:43:28 +0000339 * Download a file from a remote server to the local server
340 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200341 * @param string $rempath
342 * @param string $locpath
343 * @param string $mode
Phil Sturgeon28b29372010-03-12 00:43:28 +0000344 * @return bool
345 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200346 public function download($rempath, $locpath, $mode = 'auto')
Phil Sturgeon28b29372010-03-12 00:43:28 +0000347 {
348 if ( ! $this->_is_conn())
349 {
350 return FALSE;
351 }
Barry Mienydd671972010-10-04 16:33:58 +0200352
Phil Sturgeon28b29372010-03-12 00:43:28 +0000353 // Set the mode if not specified
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200354 if ($mode === 'auto')
Phil Sturgeon28b29372010-03-12 00:43:28 +0000355 {
356 // Get the file extension so we can set the upload type
357 $ext = $this->_getext($rempath);
358 $mode = $this->_settype($ext);
359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200361 $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY;
Barry Mienydd671972010-10-04 16:33:58 +0200362
Phil Sturgeon28b29372010-03-12 00:43:28 +0000363 $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);
364
365 if ($result === FALSE)
366 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100367 if ($this->debug === TRUE)
Phil Sturgeon28b29372010-03-12 00:43:28 +0000368 {
369 $this->_error('ftp_unable_to_download');
370 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200371
Barry Mienydd671972010-10-04 16:33:58 +0200372 return FALSE;
Phil Sturgeon28b29372010-03-12 00:43:28 +0000373 }
Barry Mienydd671972010-10-04 16:33:58 +0200374
Phil Sturgeon28b29372010-03-12 00:43:28 +0000375 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200376 }
Phil Sturgeon28b29372010-03-12 00:43:28 +0000377
378 // --------------------------------------------------------------------
379
380 /**
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * Rename (or move) a file
382 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200383 * @param string $old_file
384 * @param string $new_file
385 * @param bool $move
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 * @return bool
387 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200388 public function rename($old_file, $new_file, $move = FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 {
390 if ( ! $this->_is_conn())
391 {
392 return FALSE;
393 }
394
395 $result = @ftp_rename($this->conn_id, $old_file, $new_file);
396
397 if ($result === FALSE)
398 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100399 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
Andrey Andreev3ca060a2013-11-27 16:30:31 +0200401 $this->_error('ftp_unable_to_'.($move === FALSE ? 'rename' : 'move'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 return FALSE;
405 }
406
407 return TRUE;
408 }
409
410 // --------------------------------------------------------------------
411
412 /**
413 * Move a file
414 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200415 * @param string $old_file
416 * @param string $new_file
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 * @return bool
418 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200419 public function move($old_file, $new_file)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 return $this->rename($old_file, $new_file, TRUE);
422 }
423
424 // --------------------------------------------------------------------
425
426 /**
427 * Rename (or move) a file
428 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200429 * @param string $filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @return bool
431 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200432 public function delete_file($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 {
434 if ( ! $this->_is_conn())
435 {
436 return FALSE;
437 }
438
439 $result = @ftp_delete($this->conn_id, $filepath);
440
441 if ($result === FALSE)
442 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100443 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 {
445 $this->_error('ftp_unable_to_delete');
446 }
Andrey Andreeve52e4262014-02-21 17:11:54 +0200447
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 return FALSE;
449 }
450
451 return TRUE;
452 }
453
454 // --------------------------------------------------------------------
455
456 /**
457 * Delete a folder and recursively delete everything (including sub-folders)
458 * containted within it.
459 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200460 * @param string $filepath
Derek Allard2067d1a2008-11-13 22:59:24 +0000461 * @return bool
462 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200463 public function delete_dir($filepath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 if ( ! $this->_is_conn())
466 {
467 return FALSE;
468 }
469
470 // Add a trailing slash to the file path if needed
Andrey Andreev838a9d62012-12-03 14:37:47 +0200471 $filepath = preg_replace('/(.+?)\/*$/', '\\1/', $filepath);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472
473 $list = $this->list_files($filepath);
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200474 if ( ! empty($list))
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200476 for ($i = 0, $c = count($list); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200478 // If we can't delete the item it's probaly a directory,
479 // so we'll recursively call delete_dir()
480 if ( ! @ftp_delete($this->conn_id, $list[$i]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200482 $this->delete_dir($list[$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 }
484 }
485 }
486
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200487 if (@ftp_rmdir($this->conn_id, $filepath) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100489 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
491 $this->_error('ftp_unable_to_delete');
492 }
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200493
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 return FALSE;
495 }
496
497 return TRUE;
498 }
499
500 // --------------------------------------------------------------------
501
502 /**
503 * Set file permissions
504 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200505 * @param string $path File path
506 * @param int $perm Permissions
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 * @return bool
508 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200509 public function chmod($path, $perm)
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 {
511 if ( ! $this->_is_conn())
512 {
513 return FALSE;
514 }
515
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200516 if (@ftp_chmod($this->conn_id, $perm, $path) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100518 if ($this->debug === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 {
520 $this->_error('ftp_unable_to_chmod');
521 }
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200522
Derek Allard2067d1a2008-11-13 22:59:24 +0000523 return FALSE;
524 }
525
526 return TRUE;
527 }
528
529 // --------------------------------------------------------------------
530
531 /**
532 * FTP List files in the specified directory
533 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200534 * @param string $path
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 * @return array
536 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200537 public function list_files($path = '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200539 return $this->_is_conn()
540 ? ftp_nlist($this->conn_id, $path)
541 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 }
543
544 // ------------------------------------------------------------------------
545
546 /**
547 * Read a directory and recreate it remotely
548 *
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300549 * This function recursively reads a folder and everything it contains
550 * (including sub-folders) and creates a mirror via FTP based on it.
551 * Whatever the directory structure of the original file path will be
552 * recreated on the server.
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200554 * @param string $locpath Path to source with trailing slash
555 * @param string $rempath Path to destination - include the base folder with trailing slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 * @return bool
557 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200558 public function mirror($locpath, $rempath)
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 {
560 if ( ! $this->_is_conn())
561 {
562 return FALSE;
563 }
564
565 // Open the local file path
566 if ($fp = @opendir($locpath))
567 {
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200568 // Attempt to open the remote file path and try to create it, if it doesn't exist
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300569 if ( ! $this->changedir($rempath, TRUE) && ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200571 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
573
574 // Recursively read the local directory
575 while (FALSE !== ($file = readdir($fp)))
576 {
Andrey Andreev382b5132014-02-26 18:41:59 +0200577 if (is_dir($locpath.$file) && $file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300579 $this->mirror($locpath.$file.'/', $rempath.$file.'/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 }
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300581 elseif ($file[0] !== '.')
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
583 // Get the file extension so we can se the upload type
584 $ext = $this->_getext($file);
585 $mode = $this->_settype($ext);
586
587 $this->upload($locpath.$file, $rempath.$file, $mode);
588 }
589 }
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200590
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 return TRUE;
592 }
593
594 return FALSE;
595 }
596
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 // --------------------------------------------------------------------
598
599 /**
600 * Extract the file extension
601 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200602 * @param string $filename
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 * @return string
604 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300605 protected function _getext($filename)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Andrey Andreeve52e4262014-02-21 17:11:54 +0200607 return (($dot = strrpos($filename, '.')) === FALSE)
608 ? 'txt'
609 : substr($filename, $dot + 1);
Derek Allard2067d1a2008-11-13 22:59:24 +0000610 }
611
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 // --------------------------------------------------------------------
613
614 /**
615 * Set the upload type
616 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200617 * @param string $ext Filename extension
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 * @return string
619 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300620 protected function _settype($ext)
Derek Allard2067d1a2008-11-13 22:59:24 +0000621 {
Andrey Andreeve52e4262014-02-21 17:11:54 +0200622 return in_array($ext, array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'), TRUE)
623 ? 'ascii'
624 : 'binary';
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 }
626
627 // ------------------------------------------------------------------------
628
629 /**
630 * Close the connection
631 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 * @return bool
633 */
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200634 public function close()
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200636 return $this->_is_conn()
637 ? @ftp_close($this->conn_id)
638 : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
640
641 // ------------------------------------------------------------------------
642
643 /**
644 * Display error message
645 *
Andrey Andreev597ea272012-11-01 22:56:26 +0200646 * @param string $line
Andrey Andreev0a75d6e2011-12-22 19:45:33 +0200647 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300649 protected function _error($line)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
651 $CI =& get_instance();
652 $CI->lang->load('ftp');
653 show_error($CI->lang->line($line));
654 }
655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656}
Derek Allard2067d1a2008-11-13 22:59:24 +0000657
658/* End of file Ftp.php */
Andrey Andreevc4d979c2012-03-26 14:53:00 +0300659/* Location: ./system/libraries/Ftp.php */