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