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