Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
darwinel | 871754a | 2014-02-11 17:34:57 +0100 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
| 30 | * FTP Class |
| 31 | * |
| 32 | * @package CodeIgniter |
| 33 | * @subpackage Libraries |
| 34 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 35 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @link http://codeigniter.com/user_guide/libraries/ftp.html |
| 37 | */ |
| 38 | class CI_FTP { |
| 39 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 40 | /** |
| 41 | * FTP Server hostname |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 45 | public $hostname = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * FTP Username |
| 49 | * |
| 50 | * @var string |
| 51 | */ |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 52 | public $username = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * FTP Password |
| 56 | * |
| 57 | * @var string |
| 58 | */ |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 59 | public $password = ''; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * FTP Server port |
| 63 | * |
| 64 | * @var int |
| 65 | */ |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 66 | public $port = 21; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Passive mode flag |
| 70 | * |
| 71 | * @var bool |
| 72 | */ |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 73 | public $passive = TRUE; |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * Debug flag |
| 77 | * |
| 78 | * Specifies whether to display error messages. |
| 79 | * |
| 80 | * @var bool |
| 81 | */ |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 82 | public $debug = FALSE; |
| 83 | |
| 84 | // -------------------------------------------------------------------- |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 85 | |
| 86 | /** |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 87 | * Connection ID |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 88 | * |
| 89 | * @var resource |
| 90 | */ |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 91 | protected $conn_id; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 92 | |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 93 | // -------------------------------------------------------------------- |
| 94 | |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 95 | /** |
| 96 | * Constructor |
| 97 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 98 | * @param array $config |
Andrey Andreev | 5fd3ae8 | 2012-10-24 14:55:35 +0300 | [diff] [blame] | 99 | * @return void |
| 100 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 101 | public function __construct($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | { |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 103 | empty($config) OR $this->initialize($config); |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 104 | log_message('debug', 'FTP Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // -------------------------------------------------------------------- |
| 108 | |
| 109 | /** |
| 110 | * Initialize preferences |
| 111 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 112 | * @param array $config |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | * @return void |
| 114 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 115 | public function initialize($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 116 | { |
| 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 Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 134 | * @param array $config Connection values |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | * @return bool |
| 136 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 137 | public function connect($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | { |
| 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 Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 146 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | { |
| 148 | $this->_error('ftp_unable_to_connect'); |
| 149 | } |
| 150 | return FALSE; |
| 151 | } |
| 152 | |
| 153 | if ( ! $this->_login()) |
| 154 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 155 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | { |
| 157 | $this->_error('ftp_unable_to_login'); |
| 158 | } |
| 159 | return FALSE; |
| 160 | } |
| 161 | |
| 162 | // Set passive mode if needed |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 163 | if ($this->passive === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | { |
| 165 | ftp_pasv($this->conn_id, TRUE); |
| 166 | } |
| 167 | |
| 168 | return TRUE; |
| 169 | } |
| 170 | |
| 171 | // -------------------------------------------------------------------- |
| 172 | |
| 173 | /** |
| 174 | * FTP Login |
| 175 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 176 | * @return bool |
| 177 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 178 | protected function _login() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 179 | { |
| 180 | return @ftp_login($this->conn_id, $this->username, $this->password); |
| 181 | } |
| 182 | |
| 183 | // -------------------------------------------------------------------- |
| 184 | |
| 185 | /** |
| 186 | * Validates the connection ID |
| 187 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | * @return bool |
| 189 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 190 | protected function _is_conn() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
| 192 | if ( ! is_resource($this->conn_id)) |
| 193 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 194 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | { |
| 196 | $this->_error('ftp_no_connection'); |
| 197 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 198 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | return FALSE; |
| 200 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 201 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 202 | return TRUE; |
| 203 | } |
| 204 | |
| 205 | // -------------------------------------------------------------------- |
| 206 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 207 | /** |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 208 | * Change directory |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | * |
| 210 | * The second parameter lets us momentarily turn off debugging so that |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 211 | * this function can be used to test for the existence of a folder |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 212 | * without throwing an error. There's no FTP equivalent to is_dir() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | * so we do it by trying to change to a particular directory. |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 214 | * Internally, this parameter is only used by the "mirror" function below. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 216 | * @param string $path |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 217 | * @param bool $suppress_debug |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | * @return bool |
| 219 | */ |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 220 | public function changedir($path, $suppress_debug = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | { |
Andrey Andreev | 45549a2 | 2014-01-06 13:55:38 +0200 | [diff] [blame] | 222 | if ( ! $this->_is_conn()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | { |
| 224 | return FALSE; |
| 225 | } |
| 226 | |
| 227 | $result = @ftp_chdir($this->conn_id, $path); |
| 228 | |
| 229 | if ($result === FALSE) |
| 230 | { |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 231 | if ($this->debug === TRUE && $suppress_debug === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | { |
| 233 | $this->_error('ftp_unable_to_changedir'); |
| 234 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 235 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | return FALSE; |
| 237 | } |
| 238 | |
| 239 | return TRUE; |
| 240 | } |
| 241 | |
| 242 | // -------------------------------------------------------------------- |
| 243 | |
| 244 | /** |
| 245 | * Create a directory |
| 246 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 247 | * @param string $path |
| 248 | * @param int $permissions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | * @return bool |
| 250 | */ |
Andrey Andreev | 430b14c | 2014-01-03 17:31:26 +0200 | [diff] [blame] | 251 | public function mkdir($path, $permissions = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 252 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 253 | if ($path === '' OR ! $this->_is_conn()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | { |
| 255 | return FALSE; |
| 256 | } |
| 257 | |
| 258 | $result = @ftp_mkdir($this->conn_id, $path); |
| 259 | |
| 260 | if ($result === FALSE) |
| 261 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 262 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | { |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 264 | $this->_error('ftp_unable_to_mkdir'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 266 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | return FALSE; |
| 268 | } |
| 269 | |
| 270 | // Set file permissions if needed |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 271 | if ($permissions !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 272 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 273 | $this->chmod($path, (int) $permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
| 279 | // -------------------------------------------------------------------- |
| 280 | |
| 281 | /** |
| 282 | * Upload a file to the server |
| 283 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 284 | * @param string $locpath |
| 285 | * @param string $rempath |
| 286 | * @param string $mode |
| 287 | * @param int $permissions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | * @return bool |
| 289 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 290 | public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | { |
| 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 Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 304 | if ($mode === 'auto') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | { |
| 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 Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 311 | $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 312 | |
| 313 | $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode); |
| 314 | |
| 315 | if ($result === FALSE) |
| 316 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 317 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 318 | { |
| 319 | $this->_error('ftp_unable_to_upload'); |
| 320 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 321 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 322 | return FALSE; |
| 323 | } |
| 324 | |
| 325 | // Set file permissions if needed |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 326 | if ($permissions !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 327 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 328 | $this->chmod($rempath, (int) $permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | return TRUE; |
| 332 | } |
| 333 | |
| 334 | // -------------------------------------------------------------------- |
| 335 | |
| 336 | /** |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 337 | * Download a file from a remote server to the local server |
| 338 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 339 | * @param string $rempath |
| 340 | * @param string $locpath |
| 341 | * @param string $mode |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 342 | * @return bool |
| 343 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 344 | public function download($rempath, $locpath, $mode = 'auto') |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 345 | { |
| 346 | if ( ! $this->_is_conn()) |
| 347 | { |
| 348 | return FALSE; |
| 349 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 350 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 351 | // Set the mode if not specified |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 352 | if ($mode === 'auto') |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 353 | { |
| 354 | // Get the file extension so we can set the upload type |
| 355 | $ext = $this->_getext($rempath); |
| 356 | $mode = $this->_settype($ext); |
| 357 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 358 | |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 359 | $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 361 | $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode); |
| 362 | |
| 363 | if ($result === FALSE) |
| 364 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 365 | if ($this->debug === TRUE) |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 366 | { |
| 367 | $this->_error('ftp_unable_to_download'); |
| 368 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 369 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 370 | return FALSE; |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 371 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 372 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 373 | return TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 374 | } |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 375 | |
| 376 | // -------------------------------------------------------------------- |
| 377 | |
| 378 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 379 | * Rename (or move) a file |
| 380 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 381 | * @param string $old_file |
| 382 | * @param string $new_file |
| 383 | * @param bool $move |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 384 | * @return bool |
| 385 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 386 | public function rename($old_file, $new_file, $move = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 387 | { |
| 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 Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 397 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | { |
Andrey Andreev | 3ca060a | 2013-11-27 16:30:31 +0200 | [diff] [blame] | 399 | $this->_error('ftp_unable_to_'.($move === FALSE ? 'rename' : 'move')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 401 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | return FALSE; |
| 403 | } |
| 404 | |
| 405 | return TRUE; |
| 406 | } |
| 407 | |
| 408 | // -------------------------------------------------------------------- |
| 409 | |
| 410 | /** |
| 411 | * Move a file |
| 412 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 413 | * @param string $old_file |
| 414 | * @param string $new_file |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 415 | * @return bool |
| 416 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 417 | public function move($old_file, $new_file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | { |
| 419 | return $this->rename($old_file, $new_file, TRUE); |
| 420 | } |
| 421 | |
| 422 | // -------------------------------------------------------------------- |
| 423 | |
| 424 | /** |
| 425 | * Rename (or move) a file |
| 426 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 427 | * @param string $filepath |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 428 | * @return bool |
| 429 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 430 | public function delete_file($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 431 | { |
| 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 Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 441 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 442 | { |
| 443 | $this->_error('ftp_unable_to_delete'); |
| 444 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 445 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 446 | 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 Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 458 | * @param string $filepath |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 459 | * @return bool |
| 460 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 461 | public function delete_dir($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 462 | { |
| 463 | if ( ! $this->_is_conn()) |
| 464 | { |
| 465 | return FALSE; |
| 466 | } |
| 467 | |
| 468 | // Add a trailing slash to the file path if needed |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 469 | $filepath = preg_replace('/(.+?)\/*$/', '\\1/', $filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 470 | |
| 471 | $list = $this->list_files($filepath); |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 472 | if ( ! empty($list)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 473 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 474 | for ($i = 0, $c = count($list); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 475 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 476 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 479 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 480 | $this->delete_dir($list[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 485 | if (@ftp_rmdir($this->conn_id, $filepath) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 487 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | { |
| 489 | $this->_error('ftp_unable_to_delete'); |
| 490 | } |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 491 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 492 | return FALSE; |
| 493 | } |
| 494 | |
| 495 | return TRUE; |
| 496 | } |
| 497 | |
| 498 | // -------------------------------------------------------------------- |
| 499 | |
| 500 | /** |
| 501 | * Set file permissions |
| 502 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 503 | * @param string $path File path |
| 504 | * @param int $perm Permissions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 505 | * @return bool |
| 506 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 507 | public function chmod($path, $perm) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 508 | { |
| 509 | if ( ! $this->_is_conn()) |
| 510 | { |
| 511 | return FALSE; |
| 512 | } |
| 513 | |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 514 | if (@ftp_chmod($this->conn_id, $perm, $path) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 516 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | { |
| 518 | $this->_error('ftp_unable_to_chmod'); |
| 519 | } |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 520 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 521 | return FALSE; |
| 522 | } |
| 523 | |
| 524 | return TRUE; |
| 525 | } |
| 526 | |
| 527 | // -------------------------------------------------------------------- |
| 528 | |
| 529 | /** |
| 530 | * FTP List files in the specified directory |
| 531 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 532 | * @param string $path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | * @return array |
| 534 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 535 | public function list_files($path = '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 537 | return $this->_is_conn() |
| 538 | ? ftp_nlist($this->conn_id, $path) |
| 539 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // ------------------------------------------------------------------------ |
| 543 | |
| 544 | /** |
| 545 | * Read a directory and recreate it remotely |
| 546 | * |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 547 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 551 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 552 | * @param string $locpath Path to source with trailing slash |
| 553 | * @param string $rempath Path to destination - include the base folder with trailing slash |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 554 | * @return bool |
| 555 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 556 | public function mirror($locpath, $rempath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 557 | { |
| 558 | if ( ! $this->_is_conn()) |
| 559 | { |
| 560 | return FALSE; |
| 561 | } |
| 562 | |
| 563 | // Open the local file path |
| 564 | if ($fp = @opendir($locpath)) |
| 565 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 566 | // Attempt to open the remote file path and try to create it, if it doesn't exist |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 567 | if ( ! $this->changedir($rempath, TRUE) && ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 568 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 569 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // Recursively read the local directory |
| 573 | while (FALSE !== ($file = readdir($fp))) |
| 574 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 575 | if (@is_dir($locpath.$file) && $file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 577 | $this->mirror($locpath.$file.'/', $rempath.$file.'/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 578 | } |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 579 | elseif ($file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | { |
| 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 Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 588 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 589 | return TRUE; |
| 590 | } |
| 591 | |
| 592 | return FALSE; |
| 593 | } |
| 594 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | // -------------------------------------------------------------------- |
| 596 | |
| 597 | /** |
| 598 | * Extract the file extension |
| 599 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 600 | * @param string $filename |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | * @return string |
| 602 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 603 | protected function _getext($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 604 | { |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 605 | return (($dot = strrpos($filename, '.')) === FALSE) |
| 606 | ? 'txt' |
| 607 | : substr($filename, $dot + 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 610 | // -------------------------------------------------------------------- |
| 611 | |
| 612 | /** |
| 613 | * Set the upload type |
| 614 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 615 | * @param string $ext Filename extension |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | * @return string |
| 617 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 618 | protected function _settype($ext) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 619 | { |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame^] | 620 | return in_array($ext, array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'), TRUE) |
| 621 | ? 'ascii' |
| 622 | : 'binary'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | // ------------------------------------------------------------------------ |
| 626 | |
| 627 | /** |
| 628 | * Close the connection |
| 629 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 630 | * @return bool |
| 631 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 632 | public function close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 633 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 634 | return $this->_is_conn() |
| 635 | ? @ftp_close($this->conn_id) |
| 636 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | // ------------------------------------------------------------------------ |
| 640 | |
| 641 | /** |
| 642 | * Display error message |
| 643 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 644 | * @param string $line |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 645 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 646 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 647 | protected function _error($line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | { |
| 649 | $CI =& get_instance(); |
| 650 | $CI->lang->load('ftp'); |
| 651 | show_error($CI->lang->line($line)); |
| 652 | } |
| 653 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 655 | |
| 656 | /* End of file Ftp.php */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 657 | /* Location: ./system/libraries/Ftp.php */ |