PageRenderTime 28ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Ftp.php

https://bitbucket.org/dipendrapkrl/excelapi
PHP | 660 lines | 319 code | 93 blank | 248 comment | 72 complexity | c0d5ee1a88549ec27663c66eb8076835 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * FTP Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Libraries
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/ftp.html
  24. */
  25. class CI_FTP {
  26. var $hostname = '';
  27. var $username = '';
  28. var $password = '';
  29. var $port = 21;
  30. var $passive = TRUE;
  31. var $debug = FALSE;
  32. var $conn_id = FALSE;
  33. /**
  34. * Constructor - Sets Preferences
  35. *
  36. * The constructor can be passed an array of config values
  37. */
  38. public function __construct($config = array())
  39. {
  40. if (count($config) > 0)
  41. {
  42. $this->initialize($config);
  43. }
  44. log_message('debug', "FTP Class Initialized");
  45. }
  46. // --------------------------------------------------------------------
  47. /**
  48. * Initialize preferences
  49. *
  50. * @access public
  51. * @param array
  52. * @return void
  53. */
  54. function initialize($config = array())
  55. {
  56. foreach ($config as $key => $val)
  57. {
  58. if (isset($this->$key))
  59. {
  60. $this->$key = $val;
  61. }
  62. }
  63. // Prep the hostname
  64. $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
  65. }
  66. // --------------------------------------------------------------------
  67. /**
  68. * FTP Connect
  69. *
  70. * @access public
  71. * @param array the connection values
  72. * @return bool
  73. */
  74. function connect($config = array())
  75. {
  76. if (count($config) > 0)
  77. {
  78. $this->initialize($config);
  79. }
  80. if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
  81. {
  82. if ($this->debug == TRUE)
  83. {
  84. $this->_error('ftp_unable_to_connect');
  85. }
  86. return FALSE;
  87. }
  88. if ( ! $this->_login())
  89. {
  90. if ($this->debug == TRUE)
  91. {
  92. $this->_error('ftp_unable_to_login');
  93. }
  94. return FALSE;
  95. }
  96. // Set passive mode if needed
  97. if ($this->passive == TRUE)
  98. {
  99. ftp_pasv($this->conn_id, TRUE);
  100. }
  101. return TRUE;
  102. }
  103. // --------------------------------------------------------------------
  104. /**
  105. * FTP Login
  106. *
  107. * @access private
  108. * @return bool
  109. */
  110. function _login()
  111. {
  112. return @ftp_login($this->conn_id, $this->username, $this->password);
  113. }
  114. // --------------------------------------------------------------------
  115. /**
  116. * Validates the connection ID
  117. *
  118. * @access private
  119. * @return bool
  120. */
  121. function _is_conn()
  122. {
  123. if ( ! is_resource($this->conn_id))
  124. {
  125. if ($this->debug == TRUE)
  126. {
  127. $this->_error('ftp_no_connection');
  128. }
  129. return FALSE;
  130. }
  131. return TRUE;
  132. }
  133. // --------------------------------------------------------------------
  134. /**
  135. * Change directory
  136. *
  137. * The second parameter lets us momentarily turn off debugging so that
  138. * this function can be used to test for the existence of a folder
  139. * without throwing an error. There's no FTP equivalent to is_dir()
  140. * so we do it by trying to change to a particular directory.
  141. * Internally, this parameter is only used by the "mirror" function below.
  142. *
  143. * @access public
  144. * @param string
  145. * @param bool
  146. * @return bool
  147. */
  148. function changedir($path = '', $supress_debug = FALSE)
  149. {
  150. if ($path == '' OR ! $this->_is_conn())
  151. {
  152. return FALSE;
  153. }
  154. $result = @ftp_chdir($this->conn_id, $path);
  155. if ($result === FALSE)
  156. {
  157. if ($this->debug == TRUE AND $supress_debug == FALSE)
  158. {
  159. $this->_error('ftp_unable_to_changedir');
  160. }
  161. return FALSE;
  162. }
  163. return TRUE;
  164. }
  165. // --------------------------------------------------------------------
  166. /**
  167. * Create a directory
  168. *
  169. * @access public
  170. * @param string
  171. * @return bool
  172. */
  173. function mkdir($path = '', $permissions = NULL)
  174. {
  175. if ($path == '' OR ! $this->_is_conn())
  176. {
  177. return FALSE;
  178. }
  179. $result = @ftp_mkdir($this->conn_id, $path);
  180. if ($result === FALSE)
  181. {
  182. if ($this->debug == TRUE)
  183. {
  184. $this->_error('ftp_unable_to_makdir');
  185. }
  186. return FALSE;
  187. }
  188. // Set file permissions if needed
  189. if ( ! is_null($permissions))
  190. {
  191. $this->chmod($path, (int)$permissions);
  192. }
  193. return TRUE;
  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 bool
  204. */
  205. function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
  206. {
  207. if ( ! $this->_is_conn())
  208. {
  209. return FALSE;
  210. }
  211. if ( ! file_exists($locpath))
  212. {
  213. $this->_error('ftp_no_source_file');
  214. return FALSE;
  215. }
  216. // Set the mode if not specified
  217. if ($mode == 'auto')
  218. {
  219. // Get the file extension so we can set the upload type
  220. $ext = $this->_getext($locpath);
  221. $mode = $this->_settype($ext);
  222. }
  223. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  224. $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
  225. if ($result === FALSE)
  226. {
  227. if ($this->debug == TRUE)
  228. {
  229. $this->_error('ftp_unable_to_upload');
  230. }
  231. return FALSE;
  232. }
  233. // Set file permissions if needed
  234. if ( ! is_null($permissions))
  235. {
  236. $this->chmod($rempath, (int)$permissions);
  237. }
  238. return TRUE;
  239. }
  240. // --------------------------------------------------------------------
  241. /**
  242. * Download a file from a remote server to the local server
  243. *
  244. * @access public
  245. * @param string
  246. * @param string
  247. * @param string
  248. * @return bool
  249. */
  250. function download($rempath, $locpath, $mode = 'auto')
  251. {
  252. if ( ! $this->_is_conn())
  253. {
  254. return FALSE;
  255. }
  256. // Set the mode if not specified
  257. if ($mode == 'auto')
  258. {
  259. // Get the file extension so we can set the upload type
  260. $ext = $this->_getext($rempath);
  261. $mode = $this->_settype($ext);
  262. }
  263. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  264. $result = @ftp_get($this->conn_id, $locpath, $rempath, $mode);
  265. if ($result === FALSE)
  266. {
  267. if ($this->debug == TRUE)
  268. {
  269. $this->_error('ftp_unable_to_download');
  270. }
  271. return FALSE;
  272. }
  273. return TRUE;
  274. }
  275. // --------------------------------------------------------------------
  276. /**
  277. * Rename (or move) a file
  278. *
  279. * @access public
  280. * @param string
  281. * @param string
  282. * @param bool
  283. * @return bool
  284. */
  285. function rename($old_file, $new_file, $move = FALSE)
  286. {
  287. if ( ! $this->_is_conn())
  288. {
  289. return FALSE;
  290. }
  291. $result = @ftp_rename($this->conn_id, $old_file, $new_file);
  292. if ($result === FALSE)
  293. {
  294. if ($this->debug == TRUE)
  295. {
  296. $msg = ($move == FALSE) ? 'ftp_unable_to_rename' : 'ftp_unable_to_move';
  297. $this->_error($msg);
  298. }
  299. return FALSE;
  300. }
  301. return TRUE;
  302. }
  303. // --------------------------------------------------------------------
  304. /**
  305. * Move a file
  306. *
  307. * @access public
  308. * @param string
  309. * @param string
  310. * @return bool
  311. */
  312. function move($old_file, $new_file)
  313. {
  314. return $this->rename($old_file, $new_file, TRUE);
  315. }
  316. // --------------------------------------------------------------------
  317. /**
  318. * Rename (or move) a file
  319. *
  320. * @access public
  321. * @param string
  322. * @return bool
  323. */
  324. function delete_file($filepath)
  325. {
  326. if ( ! $this->_is_conn())
  327. {
  328. return FALSE;
  329. }
  330. $result = @ftp_delete($this->conn_id, $filepath);
  331. if ($result === FALSE)
  332. {
  333. if ($this->debug == TRUE)
  334. {
  335. $this->_error('ftp_unable_to_delete');
  336. }
  337. return FALSE;
  338. }
  339. return TRUE;
  340. }
  341. // --------------------------------------------------------------------
  342. /**
  343. * Delete a folder and recursively delete everything (including sub-folders)
  344. * containted within it.
  345. *
  346. * @access public
  347. * @param string
  348. * @return bool
  349. */
  350. function delete_dir($filepath)
  351. {
  352. if ( ! $this->_is_conn())
  353. {
  354. return FALSE;
  355. }
  356. // Add a trailing slash to the file path if needed
  357. $filepath = preg_replace("/(.+?)\/*$/", "\\1/", $filepath);
  358. $list = $this->list_files($filepath);
  359. if ($list !== FALSE AND count($list) > 0)
  360. {
  361. foreach ($list as $item)
  362. {
  363. // If we can't delete the item it's probaly a folder so
  364. // we'll recursively call delete_dir()
  365. if ( ! @ftp_delete($this->conn_id, $item))
  366. {
  367. $this->delete_dir($item);
  368. }
  369. }
  370. }
  371. $result = @ftp_rmdir($this->conn_id, $filepath);
  372. if ($result === FALSE)
  373. {
  374. if ($this->debug == TRUE)
  375. {
  376. $this->_error('ftp_unable_to_delete');
  377. }
  378. return FALSE;
  379. }
  380. return TRUE;
  381. }
  382. // --------------------------------------------------------------------
  383. /**
  384. * Set file permissions
  385. *
  386. * @access public
  387. * @param string the file path
  388. * @param string the permissions
  389. * @return bool
  390. */
  391. function chmod($path, $perm)
  392. {
  393. if ( ! $this->_is_conn())
  394. {
  395. return FALSE;
  396. }
  397. // Permissions can only be set when running PHP 5
  398. if ( ! function_exists('ftp_chmod'))
  399. {
  400. if ($this->debug == TRUE)
  401. {
  402. $this->_error('ftp_unable_to_chmod');
  403. }
  404. return FALSE;
  405. }
  406. $result = @ftp_chmod($this->conn_id, $perm, $path);
  407. if ($result === FALSE)
  408. {
  409. if ($this->debug == TRUE)
  410. {
  411. $this->_error('ftp_unable_to_chmod');
  412. }
  413. return FALSE;
  414. }
  415. return TRUE;
  416. }
  417. // --------------------------------------------------------------------
  418. /**
  419. * FTP List files in the specified directory
  420. *
  421. * @access public
  422. * @return array
  423. */
  424. function list_files($path = '.')
  425. {
  426. if ( ! $this->_is_conn())
  427. {
  428. return FALSE;
  429. }
  430. return ftp_nlist($this->conn_id, $path);
  431. }
  432. // ------------------------------------------------------------------------
  433. /**
  434. * Read a directory and recreate it remotely
  435. *
  436. * This function recursively reads a folder and everything it contains (including
  437. * sub-folders) and creates a mirror via FTP based on it. Whatever the directory structure
  438. * of the original file path will be recreated on the server.
  439. *
  440. * @access public
  441. * @param string path to source with trailing slash
  442. * @param string path to destination - include the base folder with trailing slash
  443. * @return bool
  444. */
  445. function mirror($locpath, $rempath)
  446. {
  447. if ( ! $this->_is_conn())
  448. {
  449. return FALSE;
  450. }
  451. // Open the local file path
  452. if ($fp = @opendir($locpath))
  453. {
  454. // Attempt to open the remote file path.
  455. if ( ! $this->changedir($rempath, TRUE))
  456. {
  457. // If it doesn't exist we'll attempt to create the direcotory
  458. if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))
  459. {
  460. return FALSE;
  461. }
  462. }
  463. // Recursively read the local directory
  464. while (FALSE !== ($file = readdir($fp)))
  465. {
  466. if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.')
  467. {
  468. $this->mirror($locpath.$file."/", $rempath.$file."/");
  469. }
  470. elseif (substr($file, 0, 1) != ".")
  471. {
  472. // Get the file extension so we can se the upload type
  473. $ext = $this->_getext($file);
  474. $mode = $this->_settype($ext);
  475. $this->upload($locpath.$file, $rempath.$file, $mode);
  476. }
  477. }
  478. return TRUE;
  479. }
  480. return FALSE;
  481. }
  482. // --------------------------------------------------------------------
  483. /**
  484. * Extract the file extension
  485. *
  486. * @access private
  487. * @param string
  488. * @return string
  489. */
  490. function _getext($filename)
  491. {
  492. if (FALSE === strpos($filename, '.'))
  493. {
  494. return 'txt';
  495. }
  496. $x = explode('.', $filename);
  497. return end($x);
  498. }
  499. // --------------------------------------------------------------------
  500. /**
  501. * Set the upload type
  502. *
  503. * @access private
  504. * @param string
  505. * @return string
  506. */
  507. function _settype($ext)
  508. {
  509. $text_types = array(
  510. 'txt',
  511. 'text',
  512. 'php',
  513. 'phps',
  514. 'php4',
  515. 'js',
  516. 'css',
  517. 'htm',
  518. 'html',
  519. 'phtml',
  520. 'shtml',
  521. 'log',
  522. 'xml'
  523. );
  524. return (in_array($ext, $text_types)) ? 'ascii' : 'binary';
  525. }
  526. // ------------------------------------------------------------------------
  527. /**
  528. * Close the connection
  529. *
  530. * @access public
  531. * @param string path to source
  532. * @param string path to destination
  533. * @return bool
  534. */
  535. function close()
  536. {
  537. if ( ! $this->_is_conn())
  538. {
  539. return FALSE;
  540. }
  541. @ftp_close($this->conn_id);
  542. }
  543. // ------------------------------------------------------------------------
  544. /**
  545. * Display error message
  546. *
  547. * @access private
  548. * @param string
  549. * @return bool
  550. */
  551. function _error($line)
  552. {
  553. $CI =& get_instance();
  554. $CI->lang->load('ftp');
  555. show_error($CI->lang->line($line));
  556. }
  557. }
  558. // END FTP Class
  559. /* End of file Ftp.php */
  560. /* Location: ./system/libraries/Ftp.php */