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 | } |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame^] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | return FALSE; |
| 152 | } |
| 153 | |
| 154 | if ( ! $this->_login()) |
| 155 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 156 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | { |
| 158 | $this->_error('ftp_unable_to_login'); |
| 159 | } |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame^] | 160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | return FALSE; |
| 162 | } |
| 163 | |
| 164 | // Set passive mode if needed |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 165 | if ($this->passive === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
| 167 | ftp_pasv($this->conn_id, TRUE); |
| 168 | } |
| 169 | |
| 170 | return TRUE; |
| 171 | } |
| 172 | |
| 173 | // -------------------------------------------------------------------- |
| 174 | |
| 175 | /** |
| 176 | * FTP Login |
| 177 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | * @return bool |
| 179 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 180 | protected function _login() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | { |
| 182 | return @ftp_login($this->conn_id, $this->username, $this->password); |
| 183 | } |
| 184 | |
| 185 | // -------------------------------------------------------------------- |
| 186 | |
| 187 | /** |
| 188 | * Validates the connection ID |
| 189 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 190 | * @return bool |
| 191 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 192 | protected function _is_conn() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 193 | { |
| 194 | if ( ! is_resource($this->conn_id)) |
| 195 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 196 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | { |
| 198 | $this->_error('ftp_no_connection'); |
| 199 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 200 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | return FALSE; |
| 202 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 203 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 204 | return TRUE; |
| 205 | } |
| 206 | |
| 207 | // -------------------------------------------------------------------- |
| 208 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 209 | /** |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 210 | * Change directory |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | * |
| 212 | * The second parameter lets us momentarily turn off debugging so that |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 213 | * 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] | 214 | * without throwing an error. There's no FTP equivalent to is_dir() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | * so we do it by trying to change to a particular directory. |
Derek Allard | a14ab12 | 2009-07-13 12:01:01 +0000 | [diff] [blame] | 216 | * Internally, this parameter is only used by the "mirror" function below. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 218 | * @param string $path |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 219 | * @param bool $suppress_debug |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | * @return bool |
| 221 | */ |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 222 | public function changedir($path, $suppress_debug = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 223 | { |
Andrey Andreev | 45549a2 | 2014-01-06 13:55:38 +0200 | [diff] [blame] | 224 | if ( ! $this->_is_conn()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
| 226 | return FALSE; |
| 227 | } |
| 228 | |
| 229 | $result = @ftp_chdir($this->conn_id, $path); |
| 230 | |
| 231 | if ($result === FALSE) |
| 232 | { |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 233 | if ($this->debug === TRUE && $suppress_debug === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 234 | { |
| 235 | $this->_error('ftp_unable_to_changedir'); |
| 236 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 237 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 238 | return FALSE; |
| 239 | } |
| 240 | |
| 241 | return TRUE; |
| 242 | } |
| 243 | |
| 244 | // -------------------------------------------------------------------- |
| 245 | |
| 246 | /** |
| 247 | * Create a directory |
| 248 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 249 | * @param string $path |
| 250 | * @param int $permissions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | * @return bool |
| 252 | */ |
Andrey Andreev | 430b14c | 2014-01-03 17:31:26 +0200 | [diff] [blame] | 253 | public function mkdir($path, $permissions = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 255 | if ($path === '' OR ! $this->_is_conn()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | { |
| 257 | return FALSE; |
| 258 | } |
| 259 | |
| 260 | $result = @ftp_mkdir($this->conn_id, $path); |
| 261 | |
| 262 | if ($result === FALSE) |
| 263 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 264 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 265 | { |
Andrey Andreev | b4c6218 | 2014-01-03 17:39:26 +0200 | [diff] [blame] | 266 | $this->_error('ftp_unable_to_mkdir'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 267 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 268 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 269 | return FALSE; |
| 270 | } |
| 271 | |
| 272 | // Set file permissions if needed |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 273 | if ($permissions !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 275 | $this->chmod($path, (int) $permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | return TRUE; |
| 279 | } |
| 280 | |
| 281 | // -------------------------------------------------------------------- |
| 282 | |
| 283 | /** |
| 284 | * Upload a file to the server |
| 285 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 286 | * @param string $locpath |
| 287 | * @param string $rempath |
| 288 | * @param string $mode |
| 289 | * @param int $permissions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 290 | * @return bool |
| 291 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 292 | public function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | { |
| 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 Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 306 | if ($mode === 'auto') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 307 | { |
| 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 Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 313 | $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 314 | |
| 315 | $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode); |
| 316 | |
| 317 | if ($result === FALSE) |
| 318 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 319 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 320 | { |
| 321 | $this->_error('ftp_unable_to_upload'); |
| 322 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 323 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 324 | return FALSE; |
| 325 | } |
| 326 | |
| 327 | // Set file permissions if needed |
vlakoff | 1228fe2 | 2013-01-14 01:30:09 +0100 | [diff] [blame] | 328 | if ($permissions !== NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | { |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 330 | $this->chmod($rempath, (int) $permissions); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return TRUE; |
| 334 | } |
| 335 | |
| 336 | // -------------------------------------------------------------------- |
| 337 | |
| 338 | /** |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 339 | * Download a file from a remote server to the local server |
| 340 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 341 | * @param string $rempath |
| 342 | * @param string $locpath |
| 343 | * @param string $mode |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 344 | * @return bool |
| 345 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 346 | public function download($rempath, $locpath, $mode = 'auto') |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 347 | { |
| 348 | if ( ! $this->_is_conn()) |
| 349 | { |
| 350 | return FALSE; |
| 351 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 352 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 353 | // Set the mode if not specified |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 354 | if ($mode === 'auto') |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 355 | { |
| 356 | // Get the file extension so we can set the upload type |
| 357 | $ext = $this->_getext($rempath); |
| 358 | $mode = $this->_settype($ext); |
| 359 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 360 | |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 361 | $mode = ($mode === 'ascii') ? FTP_ASCII : FTP_BINARY; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 362 | |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 363 | $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode); |
| 364 | |
| 365 | if ($result === FALSE) |
| 366 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 367 | if ($this->debug === TRUE) |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 368 | { |
| 369 | $this->_error('ftp_unable_to_download'); |
| 370 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 371 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 372 | return FALSE; |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 373 | } |
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 | return TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 376 | } |
Phil Sturgeon | 28b2937 | 2010-03-12 00:43:28 +0000 | [diff] [blame] | 377 | |
| 378 | // -------------------------------------------------------------------- |
| 379 | |
| 380 | /** |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | * Rename (or move) a file |
| 382 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 383 | * @param string $old_file |
| 384 | * @param string $new_file |
| 385 | * @param bool $move |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 386 | * @return bool |
| 387 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 388 | public function rename($old_file, $new_file, $move = FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 389 | { |
| 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 Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 399 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 400 | { |
Andrey Andreev | 3ca060a | 2013-11-27 16:30:31 +0200 | [diff] [blame] | 401 | $this->_error('ftp_unable_to_'.($move === FALSE ? 'rename' : 'move')); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 402 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 403 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 404 | return FALSE; |
| 405 | } |
| 406 | |
| 407 | return TRUE; |
| 408 | } |
| 409 | |
| 410 | // -------------------------------------------------------------------- |
| 411 | |
| 412 | /** |
| 413 | * Move a file |
| 414 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 415 | * @param string $old_file |
| 416 | * @param string $new_file |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 417 | * @return bool |
| 418 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 419 | public function move($old_file, $new_file) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | { |
| 421 | return $this->rename($old_file, $new_file, TRUE); |
| 422 | } |
| 423 | |
| 424 | // -------------------------------------------------------------------- |
| 425 | |
| 426 | /** |
| 427 | * Rename (or move) a file |
| 428 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 429 | * @param string $filepath |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 430 | * @return bool |
| 431 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 432 | public function delete_file($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | { |
| 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 Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 443 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | { |
| 445 | $this->_error('ftp_unable_to_delete'); |
| 446 | } |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 447 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | 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 Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 460 | * @param string $filepath |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 461 | * @return bool |
| 462 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 463 | public function delete_dir($filepath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 464 | { |
| 465 | if ( ! $this->_is_conn()) |
| 466 | { |
| 467 | return FALSE; |
| 468 | } |
| 469 | |
| 470 | // Add a trailing slash to the file path if needed |
Andrey Andreev | 838a9d6 | 2012-12-03 14:37:47 +0200 | [diff] [blame] | 471 | $filepath = preg_replace('/(.+?)\/*$/', '\\1/', $filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 472 | |
| 473 | $list = $this->list_files($filepath); |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 474 | if ( ! empty($list)) |
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 | for ($i = 0, $c = count($list); $i < $c; $i++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 477 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 478 | // 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 481 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 482 | $this->delete_dir($list[$i]); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 487 | if (@ftp_rmdir($this->conn_id, $filepath) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 488 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 489 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 490 | { |
| 491 | $this->_error('ftp_unable_to_delete'); |
| 492 | } |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 493 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 494 | return FALSE; |
| 495 | } |
| 496 | |
| 497 | return TRUE; |
| 498 | } |
| 499 | |
| 500 | // -------------------------------------------------------------------- |
| 501 | |
| 502 | /** |
| 503 | * Set file permissions |
| 504 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 505 | * @param string $path File path |
| 506 | * @param int $perm Permissions |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 507 | * @return bool |
| 508 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 509 | public function chmod($path, $perm) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | { |
| 511 | if ( ! $this->_is_conn()) |
| 512 | { |
| 513 | return FALSE; |
| 514 | } |
| 515 | |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 516 | if (@ftp_chmod($this->conn_id, $perm, $path) === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 517 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 518 | if ($this->debug === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 519 | { |
| 520 | $this->_error('ftp_unable_to_chmod'); |
| 521 | } |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 522 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 523 | return FALSE; |
| 524 | } |
| 525 | |
| 526 | return TRUE; |
| 527 | } |
| 528 | |
| 529 | // -------------------------------------------------------------------- |
| 530 | |
| 531 | /** |
| 532 | * FTP List files in the specified directory |
| 533 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 534 | * @param string $path |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 535 | * @return array |
| 536 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 537 | public function list_files($path = '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 538 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 539 | return $this->_is_conn() |
| 540 | ? ftp_nlist($this->conn_id, $path) |
| 541 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // ------------------------------------------------------------------------ |
| 545 | |
| 546 | /** |
| 547 | * Read a directory and recreate it remotely |
| 548 | * |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 549 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 553 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 554 | * @param string $locpath Path to source with trailing slash |
| 555 | * @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] | 556 | * @return bool |
| 557 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 558 | public function mirror($locpath, $rempath) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 559 | { |
| 560 | if ( ! $this->_is_conn()) |
| 561 | { |
| 562 | return FALSE; |
| 563 | } |
| 564 | |
| 565 | // Open the local file path |
| 566 | if ($fp = @opendir($locpath)) |
| 567 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 568 | // 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] | 569 | if ( ! $this->changedir($rempath, TRUE) && ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 570 | { |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 571 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | // Recursively read the local directory |
| 575 | while (FALSE !== ($file = readdir($fp))) |
| 576 | { |
Andrey Andreev | 382b513 | 2014-02-26 18:41:59 +0200 | [diff] [blame^] | 577 | if (is_dir($locpath.$file) && $file[0] !== '.') |
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 | $this->mirror($locpath.$file.'/', $rempath.$file.'/'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 580 | } |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 581 | elseif ($file[0] !== '.') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 582 | { |
| 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 Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 590 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 591 | return TRUE; |
| 592 | } |
| 593 | |
| 594 | return FALSE; |
| 595 | } |
| 596 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 597 | // -------------------------------------------------------------------- |
| 598 | |
| 599 | /** |
| 600 | * Extract the file extension |
| 601 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 602 | * @param string $filename |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 603 | * @return string |
| 604 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 605 | protected function _getext($filename) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 606 | { |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 607 | return (($dot = strrpos($filename, '.')) === FALSE) |
| 608 | ? 'txt' |
| 609 | : substr($filename, $dot + 1); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 612 | // -------------------------------------------------------------------- |
| 613 | |
| 614 | /** |
| 615 | * Set the upload type |
| 616 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 617 | * @param string $ext Filename extension |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 618 | * @return string |
| 619 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 620 | protected function _settype($ext) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 621 | { |
Andrey Andreev | e52e426 | 2014-02-21 17:11:54 +0200 | [diff] [blame] | 622 | return in_array($ext, array('txt', 'text', 'php', 'phps', 'php4', 'js', 'css', 'htm', 'html', 'phtml', 'shtml', 'log', 'xml'), TRUE) |
| 623 | ? 'ascii' |
| 624 | : 'binary'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | // ------------------------------------------------------------------------ |
| 628 | |
| 629 | /** |
| 630 | * Close the connection |
| 631 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 632 | * @return bool |
| 633 | */ |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 634 | public function close() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 635 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 636 | return $this->_is_conn() |
| 637 | ? @ftp_close($this->conn_id) |
| 638 | : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | // ------------------------------------------------------------------------ |
| 642 | |
| 643 | /** |
| 644 | * Display error message |
| 645 | * |
Andrey Andreev | 597ea27 | 2012-11-01 22:56:26 +0200 | [diff] [blame] | 646 | * @param string $line |
Andrey Andreev | 0a75d6e | 2011-12-22 19:45:33 +0200 | [diff] [blame] | 647 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 648 | */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 649 | protected function _error($line) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 650 | { |
| 651 | $CI =& get_instance(); |
| 652 | $CI->lang->load('ftp'); |
| 653 | show_error($CI->lang->line($line)); |
| 654 | } |
| 655 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 656 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | |
| 658 | /* End of file Ftp.php */ |
Andrey Andreev | c4d979c | 2012-03-26 14:53:00 +0300 | [diff] [blame] | 659 | /* Location: ./system/libraries/Ftp.php */ |