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 | |
| 39 | var $CI; |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Constructor - Sets Preferences |
| 44 | * |
| 45 | * The constructor can be passed an array of config values |
| 46 | */ |
| 47 | function CI_FTP($config = array()) |
| 48 | { |
| 49 | if (count($config) > 0) |
| 50 | { |
| 51 | $this->initialize($config); |
| 52 | } |
| 53 | |
| 54 | log_message('debug', "FTP Class Initialized"); |
| 55 | } |
| 56 | |
| 57 | // -------------------------------------------------------------------- |
| 58 | |
| 59 | /** |
| 60 | * Initialize preferences |
| 61 | * |
| 62 | * @access public |
| 63 | * @param array |
| 64 | * @return void |
| 65 | */ |
| 66 | function initialize($config = array()) |
| 67 | { |
| 68 | foreach ($config as $key => $val) |
| 69 | { |
| 70 | if (isset($this->$key)) |
| 71 | { |
| 72 | $this->$key = $val; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | $this->hostname = str_replace(array('ftp://', 'sftp://'), '', $this->hostname); |
| 77 | } |
| 78 | |
| 79 | // -------------------------------------------------------------------- |
| 80 | |
| 81 | /** |
| 82 | * FTP Connect |
| 83 | * |
| 84 | * @access public |
| 85 | * @return bool |
| 86 | */ |
| 87 | function connect() |
| 88 | { |
| 89 | $method = ($this->secure == FALSE) ? 'ftp_connect' : 'ftp_ssl_connect'; |
| 90 | |
| 91 | if (FALSE === ($this->conn_id = @$method($this->hostname, $this->port))) |
| 92 | { |
| 93 | if ($this->debug == TRUE) |
| 94 | { |
| 95 | $this->_error('ftp_unable_to_connect'); |
| 96 | } |
| 97 | return FALSE; |
| 98 | } |
| 99 | |
| 100 | if ( ! $this->_login()) |
| 101 | { |
| 102 | if ($this->debug == TRUE) |
| 103 | { |
| 104 | $this->_error('ftp_unable_to_login'); |
| 105 | } |
| 106 | return FALSE; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | if ($this->passive == TRUE) |
| 111 | { |
| 112 | ftp_pasv($this->conn_id, TRUE); |
| 113 | } |
| 114 | |
| 115 | return TRUE; |
| 116 | } |
| 117 | |
| 118 | // -------------------------------------------------------------------- |
| 119 | |
| 120 | /** |
| 121 | * FTP Login |
| 122 | * |
| 123 | * @access private |
| 124 | * @return bool |
| 125 | */ |
| 126 | function _login() |
| 127 | { |
| 128 | return @ftp_login($this->conn_id, $this->username, $this->password); |
| 129 | } |
| 130 | |
| 131 | // -------------------------------------------------------------------- |
| 132 | |
| 133 | /** |
| 134 | * Change direcotries |
| 135 | * |
| 136 | * @access public |
| 137 | * @param string |
| 138 | * @return array |
| 139 | */ |
| 140 | function changedir($path = '') |
| 141 | { |
| 142 | if ($path == '') |
| 143 | { |
| 144 | return FALSE; |
| 145 | } |
| 146 | |
| 147 | //$path = preg_replace("|(.+)/$|", "\\1", $path); |
| 148 | |
| 149 | $result = @ftp_chdir($this->conn_id, $path); |
| 150 | |
| 151 | if ($result === FALSE) |
| 152 | { |
| 153 | if ($this->debug == TRUE) |
| 154 | { |
| 155 | $this->_error('ftp_unable_to_changedir'); |
| 156 | } |
| 157 | return FALSE; |
| 158 | } |
| 159 | |
| 160 | return TRUE; |
| 161 | |
| 162 | } |
| 163 | |
| 164 | // -------------------------------------------------------------------- |
| 165 | |
| 166 | /** |
| 167 | * Create a directory |
| 168 | * |
| 169 | * @access public |
| 170 | * @param string |
| 171 | * @return array |
| 172 | */ |
| 173 | function mkdir($path = '') |
| 174 | { |
| 175 | if ($path == '') |
| 176 | { |
| 177 | return FALSE; |
| 178 | } |
| 179 | |
| 180 | $result = @ftp_mkdir($this->conn_id, $path); |
| 181 | |
| 182 | if ($result === FALSE) |
| 183 | { |
| 184 | if ($this->debug == TRUE) |
| 185 | { |
| 186 | $this->_error('ftp_unable_to_makdir'); |
| 187 | } |
| 188 | return FALSE; |
| 189 | } |
| 190 | |
| 191 | return TRUE; |
| 192 | } |
| 193 | |
| 194 | // -------------------------------------------------------------------- |
| 195 | |
| 196 | /** |
| 197 | * Upload a file to the server |
| 198 | * |
| 199 | * @access public |
| 200 | * @param string |
| 201 | * @param string |
| 202 | * @param string |
| 203 | * @return array |
| 204 | */ |
| 205 | function upload($locpath, $rempath, $mode = 'ascii', $permissions = NULL) |
| 206 | { |
| 207 | if ( ! file_exists($locpath)) |
| 208 | { |
| 209 | $this->_error('ftp_no_source_file'); |
| 210 | |
| 211 | return FALSE; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY; |
| 216 | $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode); |
| 217 | |
| 218 | if ($result === FALSE) |
| 219 | { |
| 220 | if ($this->debug == TRUE) |
| 221 | { |
| 222 | $this->_error('ftp_unable_to_upload'); |
| 223 | } |
| 224 | return FALSE; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | if ( ! is_null($permissions)) |
| 229 | { |
| 230 | $this->chmod($rempath, (int)$permissions); |
| 231 | } |
| 232 | |
| 233 | return TRUE; |
| 234 | } |
| 235 | |
| 236 | // -------------------------------------------------------------------- |
| 237 | |
| 238 | /** |
| 239 | * Set file permissions |
| 240 | * |
| 241 | * @access public |
| 242 | * @param string the file path |
| 243 | * @param string the permissions |
| 244 | * @return array |
| 245 | */ |
| 246 | function chmod($path, $perm) |
| 247 | { |
| 248 | $result = @ftp_chmod($this->conn_id, $perm, $path); |
| 249 | |
| 250 | if ($result === FALSE) |
| 251 | { |
| 252 | if ($this->debug == TRUE) |
| 253 | { |
| 254 | $this->_error('ftp_unable_to_chmod'); |
| 255 | } |
| 256 | return FALSE; |
| 257 | } |
| 258 | |
| 259 | return TRUE; |
| 260 | } |
| 261 | |
| 262 | // -------------------------------------------------------------------- |
| 263 | |
| 264 | /** |
| 265 | * FTP List files in the specified directory |
| 266 | * |
| 267 | * @access public |
| 268 | * @return array |
| 269 | */ |
| 270 | function filelist($path = '.') |
| 271 | { |
| 272 | return ftp_nlist($this->conn_id, $path); |
| 273 | } |
| 274 | |
| 275 | // ------------------------------------------------------------------------ |
| 276 | |
| 277 | /** |
| 278 | * Read a directory and recreates it remotely |
| 279 | * |
| 280 | * This function recursively reads a folder and everything it contains (including |
| 281 | * sub-folders) and creates a mirror via FTP based on it. Whatever directory structure |
| 282 | * is in the original file path will be recreated in the zip file. |
| 283 | * |
| 284 | * @access public |
| 285 | * @param string path to source |
| 286 | * @param string path to destination |
| 287 | * @return bool |
| 288 | */ |
| 289 | function mirror($locpath, $rempath) |
| 290 | { |
| 291 | // Open the local file path |
| 292 | if ($fp = @opendir($locpath)) |
| 293 | { |
| 294 | // Attempt to open the remote file path. |
| 295 | if ( ! $this->changedir($rempath)) |
| 296 | { |
| 297 | // If it doesn't exist we'll attempt to create the direcotory |
| 298 | if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath)) |
| 299 | { |
| 300 | return FALSE; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | // Recursively read the local directory |
| 305 | while (FALSE !== ($file = readdir($fp))) |
| 306 | { |
| 307 | if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.') |
| 308 | { |
| 309 | $this->mirror($locpath.$file."/", $rempath.$file."/"); |
| 310 | } |
| 311 | elseif (substr($file, 0, 1) != ".") |
| 312 | { |
| 313 | $mode = 'ascii'; |
| 314 | $this->upload($locpath.$file, $rempath.$file, $mode); |
| 315 | } |
| 316 | } |
| 317 | return TRUE; |
| 318 | } |
| 319 | |
| 320 | return FALSE; |
| 321 | } |
| 322 | |
| 323 | // ------------------------------------------------------------------------ |
| 324 | |
| 325 | /** |
| 326 | * Close the connection |
| 327 | * |
| 328 | * @access public |
| 329 | * @param string path to source |
| 330 | * @param string path to destination |
| 331 | * @return bool |
| 332 | */ |
| 333 | function close() |
| 334 | { |
| 335 | @ftp_close($this->conn_id); |
| 336 | } |
| 337 | |
| 338 | // ------------------------------------------------------------------------ |
| 339 | |
| 340 | /** |
| 341 | * Display error message |
| 342 | * |
| 343 | * @access private |
| 344 | * @param string |
| 345 | * @return bool |
| 346 | */ |
| 347 | function _error($line) |
| 348 | { |
| 349 | $CI =& get_instance(); |
| 350 | $CI->lang->load('ftp'); |
| 351 | show_error($CI->lang->line($line)); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | } |
| 356 | |
| 357 | ?> |