admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
| 2 | /** |
| 3 | * Code Igniter |
| 4 | * |
| 5 | * An open source application development framework for PHP 4.3.2 or newer |
| 6 | * |
| 7 | * @package CodeIgniter |
| 8 | * @author Rick Ellis |
| 9 | * @copyright Copyright (c) 2006, pMachine, Inc. |
| 10 | * @license http://www.codeignitor.com/user_guide/license.html |
| 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
| 15 | |
| 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * FTP Class |
| 20 | * |
| 21 | * @package CodeIgniter |
| 22 | * @subpackage Libraries |
| 23 | * @category Libraries |
| 24 | * @author Rick Ellis |
| 25 | * @link http://www.codeigniter.com/user_guide/libraries/encryption.html |
| 26 | */ |
| 27 | class CI_FTP { |
| 28 | |
| 29 | var $hostname = ''; |
| 30 | var $username = ''; |
| 31 | var $password = ''; |
| 32 | var $port = 21; |
| 33 | var $passive = TRUE; |
| 34 | var $secure = FALSE; |
| 35 | var $debug = FALSE; |
| 36 | var $conn_id; |
| 37 | |
| 38 | |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Constructor - Sets Preferences |
| 41 | * |
| 42 | * The constructor can be passed an array of config values |
| 43 | */ |
| 44 | function CI_FTP($config = array()) |
| 45 | { |
| 46 | if (count($config) > 0) |
| 47 | { |
| 48 | $this->initialize($config); |
| 49 | } |
| 50 | |
| 51 | log_message('debug', "FTP Class Initialized"); |
| 52 | } |
| 53 | |
| 54 | // -------------------------------------------------------------------- |
| 55 | |
| 56 | /** |
| 57 | * Initialize preferences |
| 58 | * |
| 59 | * @access public |
| 60 | * @param array |
| 61 | * @return void |
| 62 | */ |
| 63 | function initialize($config = array()) |
| 64 | { |
| 65 | foreach ($config as $key => $val) |
| 66 | { |
| 67 | if (isset($this->$key)) |
| 68 | { |
| 69 | $this->$key = $val; |
| 70 | } |
| 71 | } |
| 72 | |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 73 | // Prep the hostname |
| 74 | $this->hostname = preg_replace('|.+?://|', '', $this->hostname); |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // -------------------------------------------------------------------- |
| 78 | |
| 79 | /** |
| 80 | * FTP Connect |
| 81 | * |
| 82 | * @access public |
| 83 | * @return bool |
| 84 | */ |
| 85 | function connect() |
| 86 | { |
| 87 | $method = ($this->secure == FALSE) ? 'ftp_connect' : 'ftp_ssl_connect'; |
| 88 | |
| 89 | if (FALSE === ($this->conn_id = @$method($this->hostname, $this->port))) |
| 90 | { |
| 91 | if ($this->debug == TRUE) |
| 92 | { |
| 93 | $this->_error('ftp_unable_to_connect'); |
| 94 | } |
| 95 | return FALSE; |
| 96 | } |
| 97 | |
| 98 | if ( ! $this->_login()) |
| 99 | { |
| 100 | if ($this->debug == TRUE) |
| 101 | { |
| 102 | $this->_error('ftp_unable_to_login'); |
| 103 | } |
| 104 | return FALSE; |
| 105 | } |
| 106 | |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 107 | // Set passive mode if needed |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 108 | if ($this->passive == TRUE) |
| 109 | { |
| 110 | ftp_pasv($this->conn_id, TRUE); |
| 111 | } |
| 112 | |
| 113 | return TRUE; |
| 114 | } |
| 115 | |
| 116 | // -------------------------------------------------------------------- |
| 117 | |
| 118 | /** |
| 119 | * FTP Login |
| 120 | * |
| 121 | * @access private |
| 122 | * @return bool |
| 123 | */ |
| 124 | function _login() |
| 125 | { |
| 126 | return @ftp_login($this->conn_id, $this->username, $this->password); |
| 127 | } |
| 128 | |
| 129 | // -------------------------------------------------------------------- |
| 130 | |
| 131 | /** |
| 132 | * Change direcotries |
| 133 | * |
| 134 | * @access public |
| 135 | * @param string |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 136 | * @param bool lets us momentarily turn off debugging. |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 137 | * @return array |
| 138 | */ |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 139 | function changedir($path = '', $supress_debug = FALSE) |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 140 | { |
| 141 | if ($path == '') |
| 142 | { |
| 143 | return FALSE; |
| 144 | } |
| 145 | |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 146 | $result = @ftp_chdir($this->conn_id, $path); |
| 147 | |
| 148 | if ($result === FALSE) |
| 149 | { |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 150 | if ($this->debug == TRUE AND $supress_debug != TRUE) |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 151 | { |
| 152 | $this->_error('ftp_unable_to_changedir'); |
| 153 | } |
| 154 | return FALSE; |
| 155 | } |
| 156 | |
| 157 | return TRUE; |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // -------------------------------------------------------------------- |
| 161 | |
| 162 | /** |
| 163 | * Create a directory |
| 164 | * |
| 165 | * @access public |
| 166 | * @param string |
| 167 | * @return array |
| 168 | */ |
| 169 | function mkdir($path = '') |
| 170 | { |
| 171 | if ($path == '') |
| 172 | { |
| 173 | return FALSE; |
| 174 | } |
| 175 | |
| 176 | $result = @ftp_mkdir($this->conn_id, $path); |
| 177 | |
| 178 | if ($result === FALSE) |
| 179 | { |
| 180 | if ($this->debug == TRUE) |
| 181 | { |
| 182 | $this->_error('ftp_unable_to_makdir'); |
| 183 | } |
| 184 | return FALSE; |
| 185 | } |
| 186 | |
| 187 | return TRUE; |
| 188 | } |
| 189 | |
| 190 | // -------------------------------------------------------------------- |
| 191 | |
| 192 | /** |
| 193 | * Upload a file to the server |
| 194 | * |
| 195 | * @access public |
| 196 | * @param string |
| 197 | * @param string |
| 198 | * @param string |
| 199 | * @return array |
| 200 | */ |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 201 | function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL) |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 202 | { |
| 203 | if ( ! file_exists($locpath)) |
| 204 | { |
| 205 | $this->_error('ftp_no_source_file'); |
| 206 | |
| 207 | return FALSE; |
| 208 | } |
| 209 | |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 210 | // Set the mode if not specified |
| 211 | if ($mode == 'auto') |
| 212 | { |
| 213 | // Get the file extension so we can se the upload type |
| 214 | $ext = $this->_getext($locpath); |
| 215 | $mode = $this->_settype($ext); |
| 216 | } |
| 217 | |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 218 | $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 219 | |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 220 | $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode); |
| 221 | |
| 222 | if ($result === FALSE) |
| 223 | { |
| 224 | if ($this->debug == TRUE) |
| 225 | { |
| 226 | $this->_error('ftp_unable_to_upload'); |
| 227 | } |
| 228 | return FALSE; |
| 229 | } |
| 230 | |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 231 | // Set file permissions if needed |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 232 | if ( ! is_null($permissions)) |
| 233 | { |
| 234 | $this->chmod($rempath, (int)$permissions); |
| 235 | } |
| 236 | |
| 237 | return TRUE; |
| 238 | } |
| 239 | |
| 240 | // -------------------------------------------------------------------- |
| 241 | |
| 242 | /** |
| 243 | * Set file permissions |
| 244 | * |
| 245 | * @access public |
| 246 | * @param string the file path |
| 247 | * @param string the permissions |
| 248 | * @return array |
| 249 | */ |
| 250 | function chmod($path, $perm) |
| 251 | { |
| 252 | $result = @ftp_chmod($this->conn_id, $perm, $path); |
| 253 | |
| 254 | if ($result === FALSE) |
| 255 | { |
| 256 | if ($this->debug == TRUE) |
| 257 | { |
| 258 | $this->_error('ftp_unable_to_chmod'); |
| 259 | } |
| 260 | return FALSE; |
| 261 | } |
| 262 | |
| 263 | return TRUE; |
| 264 | } |
| 265 | |
| 266 | // -------------------------------------------------------------------- |
| 267 | |
| 268 | /** |
| 269 | * FTP List files in the specified directory |
| 270 | * |
| 271 | * @access public |
| 272 | * @return array |
| 273 | */ |
| 274 | function filelist($path = '.') |
| 275 | { |
| 276 | return ftp_nlist($this->conn_id, $path); |
| 277 | } |
| 278 | |
| 279 | // ------------------------------------------------------------------------ |
| 280 | |
| 281 | /** |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 282 | * Read a directory and recreate it remotely |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 283 | * |
| 284 | * This function recursively reads a folder and everything it contains (including |
| 285 | * sub-folders) and creates a mirror via FTP based on it. Whatever directory structure |
| 286 | * is in the original file path will be recreated in the zip file. |
| 287 | * |
| 288 | * @access public |
| 289 | * @param string path to source |
| 290 | * @param string path to destination |
| 291 | * @return bool |
| 292 | */ |
| 293 | function mirror($locpath, $rempath) |
| 294 | { |
| 295 | // Open the local file path |
| 296 | if ($fp = @opendir($locpath)) |
| 297 | { |
| 298 | // Attempt to open the remote file path. |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 299 | if ( ! $this->changedir($rempath, TRUE)) |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 300 | { |
| 301 | // If it doesn't exist we'll attempt to create the direcotory |
| 302 | if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath)) |
| 303 | { |
| 304 | return FALSE; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | // Recursively read the local directory |
| 309 | while (FALSE !== ($file = readdir($fp))) |
| 310 | { |
| 311 | if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.') |
| 312 | { |
| 313 | $this->mirror($locpath.$file."/", $rempath.$file."/"); |
| 314 | } |
| 315 | elseif (substr($file, 0, 1) != ".") |
| 316 | { |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 317 | // Get the file extension so we can se the upload type |
| 318 | $ext = $this->_getext($file); |
| 319 | $mode = $this->_settype($ext); |
| 320 | |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 321 | $this->upload($locpath.$file, $rempath.$file, $mode); |
| 322 | } |
| 323 | } |
| 324 | return TRUE; |
| 325 | } |
| 326 | |
| 327 | return FALSE; |
| 328 | } |
| 329 | |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 330 | |
| 331 | // -------------------------------------------------------------------- |
| 332 | |
| 333 | /** |
| 334 | * Extract the file extension |
| 335 | * |
| 336 | * @access private |
| 337 | * @param string |
| 338 | * @return string |
| 339 | */ |
| 340 | function _getext($filename) |
| 341 | { |
| 342 | if (FALSE === strpos($filename, '.')) |
| 343 | { |
| 344 | return 'txt'; |
| 345 | } |
| 346 | |
| 347 | $x = explode('.', $filename); |
| 348 | return end($x); |
| 349 | } |
| 350 | |
| 351 | |
| 352 | // -------------------------------------------------------------------- |
| 353 | |
| 354 | /** |
| 355 | * Set the upload type |
| 356 | * |
| 357 | * @access private |
| 358 | * @param string |
| 359 | * @return string |
| 360 | */ |
| 361 | function _settype($ext) |
| 362 | { |
| 363 | $text_types = array( |
| 364 | 'txt', |
| 365 | 'text', |
| 366 | 'php', |
| 367 | 'phps', |
| 368 | 'php4', |
| 369 | 'js', |
| 370 | 'css', |
| 371 | 'htm', |
| 372 | 'html', |
| 373 | 'phtml', |
| 374 | 'shtml', |
| 375 | 'log', |
| 376 | 'xml' |
| 377 | ); |
| 378 | |
| 379 | |
| 380 | return (in_array($ext, $text_types)) ? 'ascii' : 'binary'; |
| 381 | } |
| 382 | |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 383 | // ------------------------------------------------------------------------ |
| 384 | |
| 385 | /** |
| 386 | * Close the connection |
| 387 | * |
| 388 | * @access public |
| 389 | * @param string path to source |
| 390 | * @param string path to destination |
| 391 | * @return bool |
| 392 | */ |
| 393 | function close() |
| 394 | { |
| 395 | @ftp_close($this->conn_id); |
| 396 | } |
| 397 | |
| 398 | // ------------------------------------------------------------------------ |
| 399 | |
| 400 | /** |
| 401 | * Display error message |
| 402 | * |
| 403 | * @access private |
| 404 | * @param string |
| 405 | * @return bool |
| 406 | */ |
| 407 | function _error($line) |
| 408 | { |
| 409 | $CI =& get_instance(); |
| 410 | $CI->lang->load('ftp'); |
| 411 | show_error($CI->lang->line($line)); |
| 412 | } |
| 413 | |
| 414 | |
| 415 | } |
admin | 58e3d02 | 2006-10-26 23:33:11 +0000 | [diff] [blame] | 416 | // END FTP Class |
admin | 29932c2 | 2006-10-26 23:03:43 +0000 | [diff] [blame] | 417 | ?> |