Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
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 |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, 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 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * FTP Class |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/libraries/ftp.html |
| 36 | */ |
| 37 | class CI_FTP { |
| 38 | |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 39 | public $hostname = ''; |
| 40 | public $username = ''; |
| 41 | public $password = ''; |
| 42 | public $port = 21; |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 43 | public $passive = TRUE; |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 44 | public $debug = FALSE; |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 45 | public $conn_id = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 46 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 47 | public function __construct($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | { |
| 49 | if (count($config) > 0) |
| 50 | { |
| 51 | $this->initialize($config); |
| 52 | } |
| 53 | |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 54 | log_message('debug', 'FTP Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // -------------------------------------------------------------------- |
| 58 | |
| 59 | /** |
| 60 | * Initialize preferences |
| 61 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 62 | * @param array |
| 63 | * @return void |
| 64 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 65 | public function initialize($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 66 | { |
| 67 | foreach ($config as $key => $val) |
| 68 | { |
| 69 | if (isset($this->$key)) |
| 70 | { |
| 71 | $this->$key = $val; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Prep the hostname |
| 76 | $this->hostname = preg_replace('|.+?://|', '', $this->hostname); |
| 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * FTP Connect |
| 83 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 84 | * @param array the connection values |
| 85 | * @return bool |
| 86 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 87 | public function connect($config = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 88 | { |
| 89 | if (count($config) > 0) |
| 90 | { |
| 91 | $this->initialize($config); |
| 92 | } |
| 93 | |
| 94 | if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port))) |
| 95 | { |
| 96 | if ($this->debug == TRUE) |
| 97 | { |
| 98 | $this->_error('ftp_unable_to_connect'); |
| 99 | } |
| 100 | return FALSE; |
| 101 | } |
| 102 | |
| 103 | if ( ! $this->_login()) |
| 104 | { |
| 105 | if ($this->debug == TRUE) |
| 106 | { |
| 107 | $this->_error('ftp_unable_to_login'); |
| 108 | } |
| 109 | return FALSE; |
| 110 | } |
| 111 | |
| 112 | // Set passive mode if needed |
| 113 | if ($this->passive == TRUE) |
| 114 | { |
| 115 | ftp_pasv($this->conn_id, TRUE); |
| 116 | } |
| 117 | |
| 118 | return TRUE; |
| 119 | } |
| 120 | |
| 121 | // -------------------------------------------------------------------- |
| 122 | |
| 123 | /** |
| 124 | * FTP Login |
| 125 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 126 | * @return bool |
| 127 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 128 | protected function _login() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 129 | { |
| 130 | return @ftp_login($this->conn_id, $this->username, $this->password); |
| 131 | } |
| 132 | |
| 133 | // -------------------------------------------------------------------- |
| 134 | |
| 135 | /** |
| 136 | * Validates the connection ID |
| 137 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | * @return bool |
| 139 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 140 | protected function _is_conn() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
| 142 | if ( ! is_resource($this->conn_id)) |
| 143 | { |
| 144 | if ($this->debug == TRUE) |
| 145 | { |
| 146 | $this->_error('ftp_no_connection'); |
| 147 | } |
| 148 | return FALSE; |
| 149 | } |
| 150 | return TRUE; |
| 151 | } |
| 152 | |
| 153 | // -------------------------------------------------------------------- |
| 154 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 155 | /** |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 156 | * Change directory |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | * |
| 158 | * The second parameter lets us momentarily turn off debugging so that |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 159 | * 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] | 160 | * without throwing an error. There's no FTP equivalent to is_dir() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | * so we do it by trying to change to a particular directory. |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 162 | * Internally, this parameter is only used by the "mirror" function below. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 163 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | * @param string |
| 165 | * @param bool |
| 166 | * @return bool |
| 167 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 168 | public function changedir($path = '', $supress_debug = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | { |
| 170 | if ($path == '' OR ! $this->_is_conn()) |
| 171 | { |
| 172 | return FALSE; |
| 173 | } |
| 174 | |
| 175 | $result = @ftp_chdir($this->conn_id, $path); |
| 176 | |
| 177 | if ($result === FALSE) |
| 178 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 179 | if ($this->debug == TRUE && $supress_debug == FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | { |
| 181 | $this->_error('ftp_unable_to_changedir'); |
| 182 | } |
| 183 | return FALSE; |
| 184 | } |
| 185 | |
| 186 | return TRUE; |
| 187 | } |
| 188 | |
| 189 | // -------------------------------------------------------------------- |
| 190 | |
| 191 | /** |
| 192 | * Create a directory |
| 193 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | * @param string |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 195 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | * @return bool |
| 197 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 198 | public function mkdir($path = '', $permissions = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 199 | { |
| 200 | if ($path == '' OR ! $this->_is_conn()) |
| 201 | { |
| 202 | return FALSE; |
| 203 | } |
| 204 | |
| 205 | $result = @ftp_mkdir($this->conn_id, $path); |
| 206 | |
| 207 | if ($result === FALSE) |
| 208 | { |
| 209 | if ($this->debug == TRUE) |
| 210 | { |
| 211 | $this->_error('ftp_unable_to_makdir'); |
| 212 | } |
| 213 | return FALSE; |
| 214 | } |
| 215 | |
| 216 | // Set file permissions if needed |
| 217 | if ( ! is_null($permissions)) |
| 218 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 219 | $this->chmod($path, (int) $permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | return TRUE; |
| 223 | } |
| 224 | |
| 225 | // -------------------------------------------------------------------- |
| 226 | |
| 227 | /** |
| 228 | * Upload a file to the server |
| 229 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | * @param string |
| 231 | * @param string |
| 232 | * @param string |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 233 | * @param int |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | * @return bool |
| 235 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 236 | public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 237 | { |
| 238 | if ( ! $this->_is_conn()) |
| 239 | { |
| 240 | return FALSE; |
| 241 | } |
| 242 | |
| 243 | if ( ! file_exists($locpath)) |
| 244 | { |
| 245 | $this->_error('ftp_no_source_file'); |
| 246 | return FALSE; |
| 247 | } |
| 248 | |
| 249 | // Set the mode if not specified |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 250 | if ($mode === 'auto') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | { |
| 252 | // Get the file extension so we can set the upload type |
| 253 | $ext = $this->_getext($locpath); |
| 254 | $mode = $this->_settype($ext); |
| 255 | } |
| 256 | |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 257 | $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | |
| 259 | $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode); |
| 260 | |
| 261 | if ($result === FALSE) |
| 262 | { |
| 263 | if ($this->debug == TRUE) |
| 264 | { |
| 265 | $this->_error('ftp_unable_to_upload'); |
| 266 | } |
| 267 | return FALSE; |
| 268 | } |
| 269 | |
| 270 | // Set file permissions if needed |
| 271 | if ( ! is_null($permissions)) |
| 272 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 273 | $this->chmod($rempath, (int) $permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | return TRUE; |
| 277 | } |
| 278 | |
| 279 | // -------------------------------------------------------------------- |
| 280 | |
| 281 | /** |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 282 | * Download a file from a remote server to the local server |
| 283 | * |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 284 | * @param string |
| 285 | * @param string |
| 286 | * @param string |
| 287 | * @return bool |
| 288 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 289 | public function download($rempath, $locpath, $mode = 'auto') |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 290 | { |
| 291 | if ( ! $this->_is_conn()) |
| 292 | { |
| 293 | return FALSE; |
| 294 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 295 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 296 | // Set the mode if not specified |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 297 | if ($mode === 'auto') |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 298 | { |
| 299 | // Get the file extension so we can set the upload type |
| 300 | $ext = $this->_getext($rempath); |
| 301 | $mode = $this->_settype($ext); |
| 302 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 303 | |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 304 | $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 305 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 306 | $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode); |
| 307 | |
| 308 | if ($result === FALSE) |
| 309 | { |
| 310 | if ($this->debug == TRUE) |
| 311 | { |
| 312 | $this->_error('ftp_unable_to_download'); |
| 313 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 314 | return FALSE; |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 315 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 316 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 317 | return TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 318 | } |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 319 | |
| 320 | // -------------------------------------------------------------------- |
| 321 | |
| 322 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | * Rename (or move) a file |
| 324 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | * @param string |
| 326 | * @param string |
| 327 | * @param bool |
| 328 | * @return bool |
| 329 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 330 | public function rename($old_file, $new_file, $move = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | { |
| 332 | if ( ! $this->_is_conn()) |
| 333 | { |
| 334 | return FALSE; |
| 335 | } |
| 336 | |
| 337 | $result = @ftp_rename($this->conn_id, $old_file, $new_file); |
| 338 | |
| 339 | if ($result === FALSE) |
| 340 | { |
| 341 | if ($this->debug == TRUE) |
| 342 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 343 | $this->_error('ftp_unable_to_' . ($move == FALSE ? 'rename' : 'move')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 344 | } |
| 345 | return FALSE; |
| 346 | } |
| 347 | |
| 348 | return TRUE; |
| 349 | } |
| 350 | |
| 351 | // -------------------------------------------------------------------- |
| 352 | |
| 353 | /** |
| 354 | * Move a file |
| 355 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 356 | * @param string |
| 357 | * @param string |
| 358 | * @return bool |
| 359 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 360 | public function move($old_file, $new_file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 361 | { |
| 362 | return $this->rename($old_file, $new_file, TRUE); |
| 363 | } |
| 364 | |
| 365 | // -------------------------------------------------------------------- |
| 366 | |
| 367 | /** |
| 368 | * Rename (or move) a file |
| 369 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 370 | * @param string |
| 371 | * @return bool |
| 372 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 373 | public function delete_file($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | { |
| 375 | if ( ! $this->_is_conn()) |
| 376 | { |
| 377 | return FALSE; |
| 378 | } |
| 379 | |
| 380 | $result = @ftp_delete($this->conn_id, $filepath); |
| 381 | |
| 382 | if ($result === FALSE) |
| 383 | { |
| 384 | if ($this->debug == TRUE) |
| 385 | { |
| 386 | $this->_error('ftp_unable_to_delete'); |
| 387 | } |
| 388 | return FALSE; |
| 389 | } |
| 390 | |
| 391 | return TRUE; |
| 392 | } |
| 393 | |
| 394 | // -------------------------------------------------------------------- |
| 395 | |
| 396 | /** |
| 397 | * Delete a folder and recursively delete everything (including sub-folders) |
| 398 | * containted within it. |
| 399 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | * @param string |
| 401 | * @return bool |
| 402 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 403 | public function delete_dir($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | { |
| 405 | if ( ! $this->_is_conn()) |
| 406 | { |
| 407 | return FALSE; |
| 408 | } |
| 409 | |
| 410 | // Add a trailing slash to the file path if needed |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 411 | $filepath = preg_replace('/(.+?)\/*$/', '\\1/', $filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | |
| 413 | $list = $this->list_files($filepath); |
| 414 | |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 415 | if ($list !== FALSE && count($list) > 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 416 | { |
| 417 | foreach ($list as $item) |
| 418 | { |
| 419 | // If we can't delete the item it's probaly a folder so |
| 420 | // we'll recursively call delete_dir() |
| 421 | if ( ! @ftp_delete($this->conn_id, $item)) |
| 422 | { |
| 423 | $this->delete_dir($item); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | $result = @ftp_rmdir($this->conn_id, $filepath); |
| 429 | |
| 430 | if ($result === FALSE) |
| 431 | { |
| 432 | if ($this->debug == TRUE) |
| 433 | { |
| 434 | $this->_error('ftp_unable_to_delete'); |
| 435 | } |
| 436 | return FALSE; |
| 437 | } |
| 438 | |
| 439 | return TRUE; |
| 440 | } |
| 441 | |
| 442 | // -------------------------------------------------------------------- |
| 443 | |
| 444 | /** |
| 445 | * Set file permissions |
| 446 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 447 | * @param string the file path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | * @param string the permissions |
| 449 | * @return bool |
| 450 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 451 | public function chmod($path, $perm) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 452 | { |
| 453 | if ( ! $this->_is_conn()) |
| 454 | { |
| 455 | return FALSE; |
| 456 | } |
| 457 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 458 | $result = @ftp_chmod($this->conn_id, $perm, $path); |
| 459 | |
| 460 | if ($result === FALSE) |
| 461 | { |
| 462 | if ($this->debug == TRUE) |
| 463 | { |
| 464 | $this->_error('ftp_unable_to_chmod'); |
| 465 | } |
| 466 | return FALSE; |
| 467 | } |
| 468 | |
| 469 | return TRUE; |
| 470 | } |
| 471 | |
| 472 | // -------------------------------------------------------------------- |
| 473 | |
| 474 | /** |
| 475 | * FTP List files in the specified directory |
| 476 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | * @return array |
| 478 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 479 | public function list_files($path = '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 480 | { |
| 481 | if ( ! $this->_is_conn()) |
| 482 | { |
| 483 | return FALSE; |
| 484 | } |
| 485 | |
| 486 | return ftp_nlist($this->conn_id, $path); |
| 487 | } |
| 488 | |
| 489 | // ------------------------------------------------------------------------ |
| 490 | |
| 491 | /** |
| 492 | * Read a directory and recreate it remotely |
| 493 | * |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 494 | * This function recursively reads a folder and everything it contains |
| 495 | * (including sub-folders) and creates a mirror via FTP based on it. |
| 496 | * Whatever the directory structure of the original file path will be |
| 497 | * recreated on the server. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 499 | * @param string path to source with trailing slash |
| 500 | * @param string path to destination - include the base folder with trailing slash |
| 501 | * @return bool |
| 502 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 503 | public function mirror($locpath, $rempath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 504 | { |
| 505 | if ( ! $this->_is_conn()) |
| 506 | { |
| 507 | return FALSE; |
| 508 | } |
| 509 | |
| 510 | // Open the local file path |
| 511 | if ($fp = @opendir($locpath)) |
| 512 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 513 | // 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] | 514 | if ( ! $this->changedir($rempath, TRUE) && ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 515 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 516 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // Recursively read the local directory |
| 520 | while (FALSE !== ($file = readdir($fp))) |
| 521 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 522 | if (@is_dir($locpath.$file) && $file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 524 | $this->mirror($locpath.$file.'/', $rempath.$file.'/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 525 | } |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 526 | elseif ($file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 527 | { |
| 528 | // Get the file extension so we can se the upload type |
| 529 | $ext = $this->_getext($file); |
| 530 | $mode = $this->_settype($ext); |
| 531 | |
| 532 | $this->upload($locpath.$file, $rempath.$file, $mode); |
| 533 | } |
| 534 | } |
| 535 | return TRUE; |
| 536 | } |
| 537 | |
| 538 | return FALSE; |
| 539 | } |
| 540 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 541 | // -------------------------------------------------------------------- |
| 542 | |
| 543 | /** |
| 544 | * Extract the file extension |
| 545 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 546 | * @param string |
| 547 | * @return string |
| 548 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 549 | protected function _getext($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 550 | { |
| 551 | if (FALSE === strpos($filename, '.')) |
| 552 | { |
| 553 | return 'txt'; |
| 554 | } |
| 555 | |
| 556 | $x = explode('.', $filename); |
| 557 | return end($x); |
| 558 | } |
| 559 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | // -------------------------------------------------------------------- |
| 561 | |
| 562 | /** |
| 563 | * Set the upload type |
| 564 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 565 | * @param string |
| 566 | * @return string |
| 567 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 568 | protected function _settype($ext) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 569 | { |
| 570 | $text_types = array( |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 571 | 'txt', |
| 572 | 'text', |
| 573 | 'php', |
| 574 | 'phps', |
| 575 | 'php4', |
| 576 | 'js', |
| 577 | 'css', |
| 578 | 'htm', |
| 579 | 'html', |
| 580 | 'phtml', |
| 581 | 'shtml', |
| 582 | 'log', |
| 583 | 'xml' |
| 584 | ); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 585 | |
| 586 | |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 587 | return in_array($ext, $text_types) ? 'ascii' : 'binary'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // ------------------------------------------------------------------------ |
| 591 | |
| 592 | /** |
| 593 | * Close the connection |
| 594 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 595 | * @return bool |
| 596 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 597 | public function close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | { |
| 599 | if ( ! $this->_is_conn()) |
| 600 | { |
| 601 | return FALSE; |
| 602 | } |
| 603 | |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 604 | return @ftp_close($this->conn_id); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // ------------------------------------------------------------------------ |
| 608 | |
| 609 | /** |
| 610 | * Display error message |
| 611 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 612 | * @param string |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 613 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 614 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 615 | protected function _error($line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 616 | { |
| 617 | $CI =& get_instance(); |
| 618 | $CI->lang->load('ftp'); |
| 619 | show_error($CI->lang->line($line)); |
| 620 | } |
| 621 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 622 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 623 | |
| 624 | /* End of file Ftp.php */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 625 | /* Location: ./system/libraries/Ftp.php */ |