1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
21 * @package CodeIgniter
\r
22 * @subpackage Libraries
\r
23 * @category Libraries
\r
24 * @author ExpressionEngine Dev Team
\r
25 * @link http://codeigniter.com/user_guide/libraries/ftp.html
\r
33 var $passive = TRUE;
\r
35 var $conn_id = FALSE;
\r
39 * Constructor - Sets Preferences
\r
41 * The constructor can be passed an array of config values
\r
43 function CI_FTP($config = array())
\r
45 if (count($config) > 0)
\r
47 $this->initialize($config);
\r
50 log_message('debug', "FTP Class Initialized");
\r
53 // --------------------------------------------------------------------
\r
56 * Initialize preferences
\r
62 function initialize($config = array())
\r
64 foreach ($config as $key => $val)
\r
66 if (isset($this->$key))
\r
72 // Prep the hostname
\r
73 $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
\r
76 // --------------------------------------------------------------------
\r
82 * @param array the connection values
\r
85 function connect($config = array())
\r
87 if (count($config) > 0)
\r
89 $this->initialize($config);
\r
92 if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
\r
94 if ($this->debug == TRUE)
\r
96 $this->_error('ftp_unable_to_connect');
\r
101 if ( ! $this->_login())
\r
103 if ($this->debug == TRUE)
\r
105 $this->_error('ftp_unable_to_login');
\r
110 // Set passive mode if needed
\r
111 if ($this->passive == TRUE)
\r
113 ftp_pasv($this->conn_id, TRUE);
\r
119 // --------------------------------------------------------------------
\r
129 return @ftp_login($this->conn_id, $this->username, $this->password);
\r
132 // --------------------------------------------------------------------
\r
135 * Validates the connection ID
\r
140 function _is_conn()
\r
142 if ( ! is_resource($this->conn_id))
\r
144 if ($this->debug == TRUE)
\r
146 $this->_error('ftp_no_connection');
\r
153 // --------------------------------------------------------------------
\r
159 * The second parameter lets us momentarily turn off debugging so that
\r
160 * this function can be used to test for the existance of a folder
\r
161 * without throwing an error. There's no FTP equivalent to is_dir()
\r
162 * so we do it by trying to change to a particular directory.
\r
163 * Internally, this paramter is only used by the "mirror" function below.
\r
170 function changedir($path = '', $supress_debug = FALSE)
\r
172 if ($path == '' OR ! $this->_is_conn())
\r
177 $result = @ftp_chdir($this->conn_id, $path);
\r
179 if ($result === FALSE)
\r
181 if ($this->debug == TRUE AND $supress_debug == FALSE)
\r
183 $this->_error('ftp_unable_to_changedir');
\r
191 // --------------------------------------------------------------------
\r
194 * Create a directory
\r
200 function mkdir($path = '', $permissions = NULL)
\r
202 if ($path == '' OR ! $this->_is_conn())
\r
207 $result = @ftp_mkdir($this->conn_id, $path);
\r
209 if ($result === FALSE)
\r
211 if ($this->debug == TRUE)
\r
213 $this->_error('ftp_unable_to_makdir');
\r
218 // Set file permissions if needed
\r
219 if ( ! is_null($permissions))
\r
221 $this->chmod($path, (int)$permissions);
\r
227 // --------------------------------------------------------------------
\r
230 * Upload a file to the server
\r
238 function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
\r
240 if ( ! $this->_is_conn())
\r
245 if ( ! file_exists($locpath))
\r
247 $this->_error('ftp_no_source_file');
\r
251 // Set the mode if not specified
\r
252 if ($mode == 'auto')
\r
254 // Get the file extension so we can set the upload type
\r
255 $ext = $this->_getext($locpath);
\r
256 $mode = $this->_settype($ext);
\r
259 $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
\r
261 $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
\r
263 if ($result === FALSE)
\r
265 if ($this->debug == TRUE)
\r
267 $this->_error('ftp_unable_to_upload');
\r
272 // Set file permissions if needed
\r
273 if ( ! is_null($permissions))
\r
275 $this->chmod($rempath, (int)$permissions);
\r
281 // --------------------------------------------------------------------
\r
284 * Rename (or move) a file
\r
292 function rename($old_file, $new_file, $move = FALSE)
\r
294 if ( ! $this->_is_conn())
\r
299 $result = @ftp_rename($this->conn_id, $old_file, $new_file);
\r
301 if ($result === FALSE)
\r
303 if ($this->debug == TRUE)
\r
305 $msg = ($move == FALSE) ? 'ftp_unable_to_rename' : 'ftp_unable_to_move';
\r
307 $this->_error($msg);
\r
315 // --------------------------------------------------------------------
\r
325 function move($old_file, $new_file)
\r
327 return $this->rename($old_file, $new_file, TRUE);
\r
330 // --------------------------------------------------------------------
\r
333 * Rename (or move) a file
\r
339 function delete_file($filepath)
\r
341 if ( ! $this->_is_conn())
\r
346 $result = @ftp_delete($this->conn_id, $filepath);
\r
348 if ($result === FALSE)
\r
350 if ($this->debug == TRUE)
\r
352 $this->_error('ftp_unable_to_delete');
\r
360 // --------------------------------------------------------------------
\r
363 * Delete a folder and recursively delete everything (including sub-folders)
\r
364 * containted within it.
\r
370 function delete_dir($filepath)
\r
372 if ( ! $this->_is_conn())
\r
377 // Add a trailing slash to the file path if needed
\r
378 $filepath = preg_replace("/(.+?)\/*$/", "\\1/", $filepath);
\r
380 $list = $this->list_files($filepath);
\r
382 if ($list !== FALSE AND count($list) > 0)
\r
384 foreach ($list as $item)
\r
386 // If we can't delete the item it's probaly a folder so
\r
387 // we'll recursively call delete_dir()
\r
388 if ( ! @ftp_delete($this->conn_id, $item))
\r
390 $this->delete_dir($item);
\r
395 $result = @ftp_rmdir($this->conn_id, $filepath);
\r
397 if ($result === FALSE)
\r
399 if ($this->debug == TRUE)
\r
401 $this->_error('ftp_unable_to_delete');
\r
409 // --------------------------------------------------------------------
\r
412 * Set file permissions
\r
415 * @param string the file path
\r
416 * @param string the permissions
\r
419 function chmod($path, $perm)
\r
421 if ( ! $this->_is_conn())
\r
426 // Permissions can only be set when running PHP 5
\r
427 if ( ! function_exists('ftp_chmod'))
\r
429 if ($this->debug == TRUE)
\r
431 $this->_error('ftp_unable_to_chmod');
\r
436 $result = @ftp_chmod($this->conn_id, $perm, $path);
\r
438 if ($result === FALSE)
\r
440 if ($this->debug == TRUE)
\r
442 $this->_error('ftp_unable_to_chmod');
\r
450 // --------------------------------------------------------------------
\r
453 * FTP List files in the specified directory
\r
458 function list_files($path = '.')
\r
460 if ( ! $this->_is_conn())
\r
465 return ftp_nlist($this->conn_id, $path);
\r
468 // ------------------------------------------------------------------------
\r
471 * Read a directory and recreate it remotely
\r
473 * This function recursively reads a folder and everything it contains (including
\r
474 * sub-folders) and creates a mirror via FTP based on it. Whatever the directory structure
\r
475 * of the original file path will be recreated on the server.
\r
478 * @param string path to source with trailing slash
\r
479 * @param string path to destination - include the base folder with trailing slash
\r
482 function mirror($locpath, $rempath)
\r
484 if ( ! $this->_is_conn())
\r
489 // Open the local file path
\r
490 if ($fp = @opendir($locpath))
\r
492 // Attempt to open the remote file path.
\r
493 if ( ! $this->changedir($rempath, TRUE))
\r
495 // If it doesn't exist we'll attempt to create the direcotory
\r
496 if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))
\r
502 // Recursively read the local directory
\r
503 while (FALSE !== ($file = readdir($fp)))
\r
505 if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.')
\r
507 $this->mirror($locpath.$file."/", $rempath.$file."/");
\r
509 elseif (substr($file, 0, 1) != ".")
\r
511 // Get the file extension so we can se the upload type
\r
512 $ext = $this->_getext($file);
\r
513 $mode = $this->_settype($ext);
\r
515 $this->upload($locpath.$file, $rempath.$file, $mode);
\r
525 // --------------------------------------------------------------------
\r
528 * Extract the file extension
\r
534 function _getext($filename)
\r
536 if (FALSE === strpos($filename, '.'))
\r
541 $x = explode('.', $filename);
\r
546 // --------------------------------------------------------------------
\r
549 * Set the upload type
\r
555 function _settype($ext)
\r
557 $text_types = array(
\r
574 return (in_array($ext, $text_types)) ? 'ascii' : 'binary';
\r
577 // ------------------------------------------------------------------------
\r
580 * Close the connection
\r
583 * @param string path to source
\r
584 * @param string path to destination
\r
589 if ( ! $this->_is_conn())
\r
594 @ftp_close($this->conn_id);
\r
597 // ------------------------------------------------------------------------
\r
600 * Display error message
\r
606 function _error($line)
\r
608 $CI =& get_instance();
\r
609 $CI->lang->load('ftp');
\r
610 show_error($CI->lang->line($line));
\r
617 /* End of file Ftp.php */
\r
618 /* Location: ./system/libraries/Ftp.php */