PageRenderTime 76ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/php/xuzhou58/xuzhou58.com/include/ftp.class.php

http://jqbird.googlecode.com/
PHP | 716 lines | 385 code | 91 blank | 240 comment | 77 complexity | e9f61337bd8838660d7a3e09b421d78c MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. @set_time_limit(1000);
  3. /**
  4. * FTP ???
  5. * ??? SFTP ? SSL FTP ??, ????? FTP ??.
  6. * ??????????
  7. * ??:
  8. * $config['hostname'] = 'ftp.example.com';
  9. * $config['username'] = 'your-username';
  10. * $config['password'] = 'your-password';
  11. * $config['debug'] = TRUE;
  12. *
  13. * Exp.Tianya<tianya@dedecms.com>
  14. */
  15. class FTP {
  16. var $hostname = '';
  17. var $username = '';
  18. var $password = '';
  19. var $port = 21;
  20. var $passive = TRUE;
  21. var $debug = FALSE;
  22. var $conn_id = FALSE;
  23. /**
  24. * ???? - ????
  25. *
  26. * ?????????????
  27. */
  28. function FTP($config = array())
  29. {
  30. if (count($config) > 0)
  31. {
  32. $this->initialize($config);
  33. }
  34. }
  35. // --------------------------------------------------------------------
  36. /**
  37. * ?????
  38. *
  39. * @access public
  40. * @param array
  41. * @return void
  42. */
  43. function initialize($config = array())
  44. {
  45. foreach ($config as $key => $val)
  46. {
  47. if (isset($this->$key))
  48. {
  49. $this->$key = $val;
  50. }
  51. }
  52. // ?????
  53. $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
  54. }
  55. // --------------------------------------------------------------------
  56. /**
  57. * FTP ??
  58. *
  59. * @access public
  60. * @param array ???
  61. * @return bool
  62. */
  63. function connect($config = array())
  64. {
  65. if (count($config) > 0)
  66. {
  67. $this->initialize($config);
  68. }
  69. if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
  70. {
  71. if ($this->debug == TRUE)
  72. {
  73. $this->_error('????');
  74. }
  75. return FALSE;
  76. }
  77. if ( ! $this->_login())
  78. {
  79. if ($this->debug == TRUE)
  80. {
  81. $this->_error('????');
  82. }
  83. return FALSE;
  84. }
  85. // ???????????
  86. if ($this->passive == TRUE)
  87. {
  88. ftp_pasv($this->conn_id, TRUE);
  89. }
  90. return TRUE;
  91. }
  92. // --------------------------------------------------------------------
  93. /**
  94. * FTP ??
  95. *
  96. * @access private
  97. * @return bool
  98. */
  99. function _login()
  100. {
  101. return @ftp_login($this->conn_id, $this->username, $this->password);
  102. }
  103. // --------------------------------------------------------------------
  104. /**
  105. * ????ID
  106. *
  107. * @access private
  108. * @return bool
  109. */
  110. function _is_conn()
  111. {
  112. if ( ! is_resource($this->conn_id))
  113. {
  114. if ($this->debug == TRUE)
  115. {
  116. $this->_error('????');
  117. }
  118. return FALSE;
  119. }
  120. return TRUE;
  121. }
  122. // --------------------------------------------------------------------
  123. /**
  124. * ????
  125. * ???????????????????
  126. * ?????????????????
  127. * ????????????FTP???is_dir()
  128. * ????????????????
  129. *
  130. * @access public
  131. * @param string
  132. * @param bool
  133. * @return bool
  134. */
  135. function changedir($path = '', $supress_debug = FALSE)
  136. {
  137. if ($path == '' OR ! $this->_is_conn())
  138. {
  139. return FALSE;
  140. }
  141. $result = @ftp_chdir($this->conn_id, $path);
  142. if ($result === FALSE)
  143. {
  144. if ($this->debug == TRUE AND $supress_debug == FALSE)
  145. {
  146. $this->_error('??????');
  147. }
  148. return FALSE;
  149. }
  150. return TRUE;
  151. }
  152. // --------------------------------------------------------------------
  153. /**
  154. * ??????
  155. *
  156. * @access public
  157. * @param string
  158. * @return bool
  159. */
  160. function mkdir($path = '', $permissions = NULL)
  161. {
  162. if ($path == '' OR ! $this->_is_conn())
  163. {
  164. return FALSE;
  165. }
  166. $result = @ftp_mkdir($this->conn_id, $path);
  167. if ($result === FALSE)
  168. {
  169. if ($this->debug == TRUE)
  170. {
  171. $this->_error('???????');
  172. }
  173. return FALSE;
  174. }
  175. // ????????
  176. if ( ! is_null($permissions))
  177. {
  178. $this->chmod($path, (int)$permissions);
  179. }
  180. return TRUE;
  181. }
  182. // --------------------------------------------------------------------
  183. /**
  184. * ??????
  185. *
  186. * @access public
  187. * @param string
  188. * @return bool
  189. */
  190. function rmkdir($path = '', $pathsymbol = '/')
  191. {
  192. $pathArray = explode($pathsymbol,$path);
  193. $pathstr = $pathsymbol;
  194. foreach($pathArray as $val)
  195. {
  196. if(!empty($val))
  197. {
  198. //???????
  199. $pathstr = $pathstr.$val.$pathsymbol;
  200. if (! $this->_is_conn())
  201. {
  202. return FALSE;
  203. }
  204. $result = @ftp_chdir($this->conn_id, $pathstr);
  205. if($result === FALSE)
  206. {
  207. //????????????
  208. if(!$this->mkdir($pathstr))
  209. {
  210. return FALSE;
  211. }
  212. }
  213. }
  214. }
  215. return TRUE;
  216. }
  217. // --------------------------------------------------------------------
  218. /**
  219. * ??????????
  220. *
  221. * @access public
  222. * @param string
  223. * @param string
  224. * @param string
  225. * @return bool
  226. */
  227. function upload($locpath, $rempath, $mode = 'auto', $permissions = NULL)
  228. {
  229. if (!$this->_is_conn())
  230. {
  231. return FALSE;
  232. }
  233. if (!file_exists($locpath))
  234. {
  235. $this->_error('??????');
  236. return FALSE;
  237. }
  238. // ????????
  239. if ($mode == 'auto')
  240. {
  241. // ????????????????
  242. $ext = $this->_getext($locpath);
  243. $mode = $this->_settype($ext);
  244. }
  245. $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
  246. $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
  247. if ($result === FALSE)
  248. {
  249. if ($this->debug == TRUE)
  250. {
  251. $this->_error('????');
  252. }
  253. return FALSE;
  254. }
  255. // ??????????
  256. if ( ! is_null($permissions))
  257. {
  258. $this->chmod($rempath, (int)$permissions);
  259. }
  260. return TRUE;
  261. }
  262. // --------------------------------------------------------------------
  263. /**
  264. * ???(????)????
  265. *
  266. * @access public
  267. * @param string
  268. * @param string
  269. * @param bool
  270. * @return bool
  271. */
  272. function rename($old_file, $new_file, $move = FALSE)
  273. {
  274. if ( ! $this->_is_conn())
  275. {
  276. return FALSE;
  277. }
  278. $result = @ftp_rename($this->conn_id, $old_file, $new_file);
  279. if ($result === FALSE)
  280. {
  281. if ($this->debug == TRUE)
  282. {
  283. $msg = ($move == FALSE) ? '?????' : '????';
  284. $this->_error($msg);
  285. }
  286. return FALSE;
  287. }
  288. return TRUE;
  289. }
  290. // --------------------------------------------------------------------
  291. /**
  292. * ??????
  293. *
  294. * @access public
  295. * @param string
  296. * @param string
  297. * @return bool
  298. */
  299. function move($old_file, $new_file)
  300. {
  301. return $this->rename($old_file, $new_file, TRUE);
  302. }
  303. // --------------------------------------------------------------------
  304. /**
  305. * ???????????
  306. *
  307. * @access public
  308. * @param string
  309. * @return bool
  310. */
  311. function delete_file($filepath)
  312. {
  313. if ( ! $this->_is_conn())
  314. {
  315. return FALSE;
  316. }
  317. $result = @ftp_delete($this->conn_id, $filepath);
  318. if ($result === FALSE)
  319. {
  320. if ($this->debug == TRUE)
  321. {
  322. $this->_error('????');
  323. }
  324. return FALSE;
  325. }
  326. return TRUE;
  327. }
  328. // --------------------------------------------------------------------
  329. /**
  330. * ?????????????????????????
  331. *
  332. * @access public
  333. * @param string
  334. * @return bool
  335. */
  336. function delete_dir($filepath)
  337. {
  338. if ( ! $this->_is_conn())
  339. {
  340. return FALSE;
  341. }
  342. // ???????????"/"
  343. $filepath = preg_replace("/(.+?)\/*$/", "\\1/", $filepath);
  344. $list = $this->list_files($filepath);
  345. if ($list !== FALSE AND count($list) > 0)
  346. {
  347. foreach ($list as $item)
  348. {
  349. // ???????????,??????????
  350. // ??? delete_dir()
  351. if ( ! @ftp_delete($this->conn_id, $item))
  352. {
  353. $this->delete_dir($item);
  354. }
  355. }
  356. }
  357. $result = @ftp_rmdir($this->conn_id, $filepath);
  358. if ($result === FALSE)
  359. {
  360. if ($this->debug == TRUE)
  361. {
  362. $this->_error('????');
  363. }
  364. return FALSE;
  365. }
  366. return TRUE;
  367. }
  368. // --------------------------------------------------------------------
  369. /**
  370. * ??????
  371. *
  372. * @access public
  373. * @param string ????
  374. * @param string ??
  375. * @return bool
  376. */
  377. function chmod($path, $perm)
  378. {
  379. if ( ! $this->_is_conn())
  380. {
  381. return FALSE;
  382. }
  383. // ?PHP5????
  384. if ( ! function_exists('ftp_chmod'))
  385. {
  386. if ($this->debug == TRUE)
  387. {
  388. $this->_error('??????');
  389. }
  390. return FALSE;
  391. }
  392. $result = @ftp_chmod($this->conn_id, $perm, $path);
  393. if ($result === FALSE)
  394. {
  395. if ($this->debug == TRUE)
  396. {
  397. $this->_error('??????');
  398. }
  399. return FALSE;
  400. }
  401. return TRUE;
  402. }
  403. // --------------------------------------------------------------------
  404. /**
  405. * ???????FTP????
  406. *
  407. * @access public
  408. * @return array
  409. */
  410. function list_files($path = '.')
  411. {
  412. if ( ! $this->_is_conn())
  413. {
  414. return FALSE;
  415. }
  416. return ftp_nlist($this->conn_id, $path);
  417. }
  418. // --------------------------------------------------------------------
  419. /**
  420. * ??????????????
  421. *
  422. * @access public
  423. * @return array
  424. */
  425. function list_rawfiles($path = '.', $type='dir')
  426. {
  427. if ( ! $this->_is_conn())
  428. {
  429. return FALSE;
  430. }
  431. $ftp_rawlist = ftp_rawlist($this->conn_id, $path, TRUE);
  432. foreach ($ftp_rawlist as $v) {
  433. $info = array();
  434. $vinfo = preg_split("/[\s]+/", $v, 9);
  435. if ($vinfo[0] !== "total") {
  436. $info['chmod'] = $vinfo[0];
  437. $info['num'] = $vinfo[1];
  438. $info['owner'] = $vinfo[2];
  439. $info['group'] = $vinfo[3];
  440. $info['size'] = $vinfo[4];
  441. $info['month'] = $vinfo[5];
  442. $info['day'] = $vinfo[6];
  443. $info['time'] = $vinfo[7];
  444. $info['name'] = $vinfo[8];
  445. $rawlist[$info['name']] = $info;
  446. }
  447. }
  448. $dir = array();
  449. $file = array();
  450. foreach ($rawlist as $k => $v) {
  451. if ($v['chmod']{0} == "d") {
  452. $dir[$k] = $v;
  453. } elseif ($v['chmod']{0} == "-") {
  454. $file[$k] = $v;
  455. }
  456. }
  457. return ($type == 'dir')? $dir : $file;
  458. }
  459. // ------------------------------------------------------------------------
  460. /**
  461. * ??????????????(??????????)????FTP????????????
  462. * ?????????????????????????????????
  463. *
  464. * @access public
  465. * @param string ????"/"????
  466. * @param string ???? - ????"/"????
  467. * @return bool
  468. */
  469. function mirror($locpath, $rempath)
  470. {
  471. if ( ! $this->_is_conn())
  472. {
  473. return FALSE;
  474. }
  475. // ????????
  476. if ($fp = @opendir($locpath))
  477. {
  478. // ???????????.
  479. if ( ! $this->changedir($rempath, TRUE))
  480. {
  481. // ?????????
  482. if ( ! $this->rmkdir($rempath) OR ! $this->changedir($rempath))
  483. {
  484. return FALSE;
  485. }
  486. }
  487. // ????????
  488. while (FALSE !== ($file = readdir($fp)))
  489. {
  490. if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.')
  491. {
  492. $this->mirror($locpath.$file."/", $rempath.$file."/");
  493. }
  494. elseif (substr($file, 0, 1) != ".")
  495. {
  496. // ????????????????
  497. $ext = $this->_getext($file);
  498. $mode = $this->_settype($ext);
  499. $this->upload($locpath.$file, $rempath.$file, $mode);
  500. }
  501. }
  502. return TRUE;
  503. }
  504. return FALSE;
  505. }
  506. // --------------------------------------------------------------------
  507. /**
  508. * ???????
  509. *
  510. * @access private
  511. * @param string
  512. * @return string
  513. */
  514. function _getext($filename)
  515. {
  516. if (FALSE === strpos($filename, '.'))
  517. {
  518. return 'txt';
  519. }
  520. $x = explode('.', $filename);
  521. return end($x);
  522. }
  523. // --------------------------------------------------------------------
  524. /**
  525. * ??????
  526. *
  527. * @access private
  528. * @param string
  529. * @return string
  530. */
  531. function _settype($ext)
  532. {
  533. $text_types = array(
  534. 'txt',
  535. 'text',
  536. 'php',
  537. 'phps',
  538. 'php4',
  539. 'js',
  540. 'css',
  541. 'htm',
  542. 'html',
  543. 'phtml',
  544. 'shtml',
  545. 'log',
  546. 'xml'
  547. );
  548. return (in_array($ext, $text_types)) ? 'ascii' : 'binary';
  549. }
  550. // ------------------------------------------------------------------------
  551. /**
  552. * ????
  553. *
  554. * @access public
  555. * @param string ???
  556. * @param string ?????
  557. * @return bool
  558. */
  559. function close()
  560. {
  561. if ( ! $this->_is_conn())
  562. {
  563. return FALSE;
  564. }
  565. @ftp_close($this->conn_id);
  566. }
  567. // ------------------------------------------------------------------------
  568. /**
  569. * ??????
  570. *
  571. * @access private
  572. * @param string
  573. * @return bool
  574. */
  575. function _error($msg)
  576. {
  577. $errorTrackFile = dirname(__FILE__).'/../data/ftp_error_trace.inc';
  578. $emsg = '';
  579. $emsg .= "<div><h3>DedeCMS Error Warning!</h3>\r\n";
  580. $emsg .= "<div><a href='http://bbs.dedecms.com' target='_blank' style='color:red'>Technical Support: http://bbs.dedecms.com</a></div>";
  581. $emsg .= "<div style='line-helght:160%;font-size:14px;color:green'>\r\n";
  582. $emsg .= "<div style='color:blue'><br />Error page: <font color='red'>".$this->GetCurUrl()."</font></div>\r\n";
  583. $emsg .= "<div>Error infos: {$msg}</div>\r\n";
  584. $emsg .= "<br /></div></div>\r\n";
  585. echo $emsg;
  586. $savemsg = 'Page: '.$this->GetCurUrl()."\r\nError: ".$msg;
  587. //??????
  588. $fp = @fopen($errorTrackFile, 'a');
  589. @fwrite($fp, '<'.'?php exit();'."\r\n/*\r\n{$savemsg}\r\n*/\r\n?".">\r\n");
  590. @fclose($fp);
  591. }
  592. /**
  593. * ?????????
  594. *
  595. * @access public
  596. * @return string
  597. */
  598. function GetCurUrl()
  599. {
  600. if(!empty($_SERVER["REQUEST_URI"]))
  601. {
  602. $scriptName = $_SERVER["REQUEST_URI"];
  603. $nowurl = $scriptName;
  604. }
  605. else
  606. {
  607. $scriptName = $_SERVER["PHP_SELF"];
  608. if(empty($_SERVER["QUERY_STRING"])) {
  609. $nowurl = $scriptName;
  610. }
  611. else {
  612. $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"];
  613. }
  614. }
  615. return $nowurl;
  616. }
  617. }
  618. ?>