PageRenderTime 47ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/class-ftp.php

https://bitbucket.org/aqge/deptandashboard
PHP | 906 lines | 792 code | 41 blank | 73 comment | 122 complexity | 950dc27ed9307f8d9c6a1345f739e763 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PemFTP - A Ftp implementation in pure PHP
  4. *
  5. * @package PemFTP
  6. * @since 2.5
  7. *
  8. * @version 1.0
  9. * @copyright Alexey Dotsenko
  10. * @author Alexey Dotsenko
  11. * @link http://www.phpclasses.org/browse/package/1743.html Site
  12. * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
  13. */
  14. /**
  15. * Defines the newline characters, if not defined already.
  16. *
  17. * This can be redefined.
  18. *
  19. * @since 2.5
  20. * @var string
  21. */
  22. if(!defined('CRLF')) define('CRLF',"\r\n");
  23. /**
  24. * Sets whatever to autodetect ASCII mode.
  25. *
  26. * This can be redefined.
  27. *
  28. * @since 2.5
  29. * @var int
  30. */
  31. if(!defined("FTP_AUTOASCII")) define("FTP_AUTOASCII", -1);
  32. /**
  33. *
  34. * This can be redefined.
  35. * @since 2.5
  36. * @var int
  37. */
  38. if(!defined("FTP_BINARY")) define("FTP_BINARY", 1);
  39. /**
  40. *
  41. * This can be redefined.
  42. * @since 2.5
  43. * @var int
  44. */
  45. if(!defined("FTP_ASCII")) define("FTP_ASCII", 0);
  46. /**
  47. * Whether to force FTP.
  48. *
  49. * This can be redefined.
  50. *
  51. * @since 2.5
  52. * @var bool
  53. */
  54. if(!defined('FTP_FORCE')) define('FTP_FORCE', true);
  55. /**
  56. * @since 2.5
  57. * @var string
  58. */
  59. define('FTP_OS_Unix','u');
  60. /**
  61. * @since 2.5
  62. * @var string
  63. */
  64. define('FTP_OS_Windows','w');
  65. /**
  66. * @since 2.5
  67. * @var string
  68. */
  69. define('FTP_OS_Mac','m');
  70. /**
  71. * PemFTP base class
  72. *
  73. */
  74. class ftp_base {
  75. /* Public variables */
  76. var $LocalEcho;
  77. var $Verbose;
  78. var $OS_local;
  79. var $OS_remote;
  80. /* Private variables */
  81. var $_lastaction;
  82. var $_errors;
  83. var $_type;
  84. var $_umask;
  85. var $_timeout;
  86. var $_passive;
  87. var $_host;
  88. var $_fullhost;
  89. var $_port;
  90. var $_datahost;
  91. var $_dataport;
  92. var $_ftp_control_sock;
  93. var $_ftp_data_sock;
  94. var $_ftp_temp_sock;
  95. var $_ftp_buff_size;
  96. var $_login;
  97. var $_password;
  98. var $_connected;
  99. var $_ready;
  100. var $_code;
  101. var $_message;
  102. var $_can_restore;
  103. var $_port_available;
  104. var $_curtype;
  105. var $_features;
  106. var $_error_array;
  107. var $AuthorizedTransferMode;
  108. var $OS_FullName;
  109. var $_eol_code;
  110. var $AutoAsciiExt;
  111. /* Constructor */
  112. function ftp_base($port_mode=FALSE) {
  113. $this->__construct($port_mode);
  114. }
  115. function __construct($port_mode=FALSE, $verb=FALSE, $le=FALSE) {
  116. $this->LocalEcho=$le;
  117. $this->Verbose=$verb;
  118. $this->_lastaction=NULL;
  119. $this->_error_array=array();
  120. $this->_eol_code=array(FTP_OS_Unix=>"\n", FTP_OS_Mac=>"\r", FTP_OS_Windows=>"\r\n");
  121. $this->AuthorizedTransferMode=array(FTP_AUTOASCII, FTP_ASCII, FTP_BINARY);
  122. $this->OS_FullName=array(FTP_OS_Unix => 'UNIX', FTP_OS_Windows => 'WINDOWS', FTP_OS_Mac => 'MACOS');
  123. $this->AutoAsciiExt=array("ASP","BAT","C","CPP","CSS","CSV","JS","H","HTM","HTML","SHTML","INI","LOG","PHP3","PHTML","PL","PERL","SH","SQL","TXT");
  124. $this->_port_available=($port_mode==TRUE);
  125. $this->SendMSG("Staring FTP client class".($this->_port_available?"":" without PORT mode support"));
  126. $this->_connected=FALSE;
  127. $this->_ready=FALSE;
  128. $this->_can_restore=FALSE;
  129. $this->_code=0;
  130. $this->_message="";
  131. $this->_ftp_buff_size=4096;
  132. $this->_curtype=NULL;
  133. $this->SetUmask(0022);
  134. $this->SetType(FTP_AUTOASCII);
  135. $this->SetTimeout(30);
  136. $this->Passive(!$this->_port_available);
  137. $this->_login="anonymous";
  138. $this->_password="anon@ftp.com";
  139. $this->_features=array();
  140. $this->OS_local=FTP_OS_Unix;
  141. $this->OS_remote=FTP_OS_Unix;
  142. $this->features=array();
  143. if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') $this->OS_local=FTP_OS_Windows;
  144. elseif(strtoupper(substr(PHP_OS, 0, 3)) === 'MAC') $this->OS_local=FTP_OS_Mac;
  145. }
  146. // <!-- --------------------------------------------------------------------------------------- -->
  147. // <!-- Public functions -->
  148. // <!-- --------------------------------------------------------------------------------------- -->
  149. function parselisting($line) {
  150. $is_windows = ($this->OS_remote == FTP_OS_Windows);
  151. if ($is_windows && preg_match("/([0-9]{2})-([0-9]{2})-([0-9]{2}) +([0-9]{2}):([0-9]{2})(AM|PM) +([0-9]+|<DIR>) +(.+)/",$line,$lucifer)) {
  152. $b = array();
  153. if ($lucifer[3]<70) { $lucifer[3]+=2000; } else { $lucifer[3]+=1900; } // 4digit year fix
  154. $b['isdir'] = ($lucifer[7]=="<DIR>");
  155. if ( $b['isdir'] )
  156. $b['type'] = 'd';
  157. else
  158. $b['type'] = 'f';
  159. $b['size'] = $lucifer[7];
  160. $b['month'] = $lucifer[1];
  161. $b['day'] = $lucifer[2];
  162. $b['year'] = $lucifer[3];
  163. $b['hour'] = $lucifer[4];
  164. $b['minute'] = $lucifer[5];
  165. $b['time'] = @mktime($lucifer[4]+(strcasecmp($lucifer[6],"PM")==0?12:0),$lucifer[5],0,$lucifer[1],$lucifer[2],$lucifer[3]);
  166. $b['am/pm'] = $lucifer[6];
  167. $b['name'] = $lucifer[8];
  168. } else if (!$is_windows && $lucifer=preg_split("/[ ]/",$line,9,PREG_SPLIT_NO_EMPTY)) {
  169. //echo $line."\n";
  170. $lcount=count($lucifer);
  171. if ($lcount<8) return '';
  172. $b = array();
  173. $b['isdir'] = $lucifer[0]{0} === "d";
  174. $b['islink'] = $lucifer[0]{0} === "l";
  175. if ( $b['isdir'] )
  176. $b['type'] = 'd';
  177. elseif ( $b['islink'] )
  178. $b['type'] = 'l';
  179. else
  180. $b['type'] = 'f';
  181. $b['perms'] = $lucifer[0];
  182. $b['number'] = $lucifer[1];
  183. $b['owner'] = $lucifer[2];
  184. $b['group'] = $lucifer[3];
  185. $b['size'] = $lucifer[4];
  186. if ($lcount==8) {
  187. sscanf($lucifer[5],"%d-%d-%d",$b['year'],$b['month'],$b['day']);
  188. sscanf($lucifer[6],"%d:%d",$b['hour'],$b['minute']);
  189. $b['time'] = @mktime($b['hour'],$b['minute'],0,$b['month'],$b['day'],$b['year']);
  190. $b['name'] = $lucifer[7];
  191. } else {
  192. $b['month'] = $lucifer[5];
  193. $b['day'] = $lucifer[6];
  194. if (preg_match("/([0-9]{2}):([0-9]{2})/",$lucifer[7],$l2)) {
  195. $b['year'] = date("Y");
  196. $b['hour'] = $l2[1];
  197. $b['minute'] = $l2[2];
  198. } else {
  199. $b['year'] = $lucifer[7];
  200. $b['hour'] = 0;
  201. $b['minute'] = 0;
  202. }
  203. $b['time'] = strtotime(sprintf("%d %s %d %02d:%02d",$b['day'],$b['month'],$b['year'],$b['hour'],$b['minute']));
  204. $b['name'] = $lucifer[8];
  205. }
  206. }
  207. return $b;
  208. }
  209. function SendMSG($message = "", $crlf=true) {
  210. if ($this->Verbose) {
  211. echo $message.($crlf?CRLF:"");
  212. flush();
  213. }
  214. return TRUE;
  215. }
  216. function SetType($mode=FTP_AUTOASCII) {
  217. if(!in_array($mode, $this->AuthorizedTransferMode)) {
  218. $this->SendMSG("Wrong type");
  219. return FALSE;
  220. }
  221. $this->_type=$mode;
  222. $this->SendMSG("Transfer type: ".($this->_type==FTP_BINARY?"binary":($this->_type==FTP_ASCII?"ASCII":"auto ASCII") ) );
  223. return TRUE;
  224. }
  225. function _settype($mode=FTP_ASCII) {
  226. if($this->_ready) {
  227. if($mode==FTP_BINARY) {
  228. if($this->_curtype!=FTP_BINARY) {
  229. if(!$this->_exec("TYPE I", "SetType")) return FALSE;
  230. $this->_curtype=FTP_BINARY;
  231. }
  232. } elseif($this->_curtype!=FTP_ASCII) {
  233. if(!$this->_exec("TYPE A", "SetType")) return FALSE;
  234. $this->_curtype=FTP_ASCII;
  235. }
  236. } else return FALSE;
  237. return TRUE;
  238. }
  239. function Passive($pasv=NULL) {
  240. if(is_null($pasv)) $this->_passive=!$this->_passive;
  241. else $this->_passive=$pasv;
  242. if(!$this->_port_available and !$this->_passive) {
  243. $this->SendMSG("Only passive connections available!");
  244. $this->_passive=TRUE;
  245. return FALSE;
  246. }
  247. $this->SendMSG("Passive mode ".($this->_passive?"on":"off"));
  248. return TRUE;
  249. }
  250. function SetServer($host, $port=21, $reconnect=true) {
  251. if(!is_long($port)) {
  252. $this->verbose=true;
  253. $this->SendMSG("Incorrect port syntax");
  254. return FALSE;
  255. } else {
  256. $ip=@gethostbyname($host);
  257. $dns=@gethostbyaddr($host);
  258. if(!$ip) $ip=$host;
  259. if(!$dns) $dns=$host;
  260. // Validate the IPAddress PHP4 returns -1 for invalid, PHP5 false
  261. // -1 === "255.255.255.255" which is the broadcast address which is also going to be invalid
  262. $ipaslong = ip2long($ip);
  263. if ( ($ipaslong == false) || ($ipaslong === -1) ) {
  264. $this->SendMSG("Wrong host name/address \"".$host."\"");
  265. return FALSE;
  266. }
  267. $this->_host=$ip;
  268. $this->_fullhost=$dns;
  269. $this->_port=$port;
  270. $this->_dataport=$port-1;
  271. }
  272. $this->SendMSG("Host \"".$this->_fullhost."(".$this->_host."):".$this->_port."\"");
  273. if($reconnect){
  274. if($this->_connected) {
  275. $this->SendMSG("Reconnecting");
  276. if(!$this->quit(FTP_FORCE)) return FALSE;
  277. if(!$this->connect()) return FALSE;
  278. }
  279. }
  280. return TRUE;
  281. }
  282. function SetUmask($umask=0022) {
  283. $this->_umask=$umask;
  284. umask($this->_umask);
  285. $this->SendMSG("UMASK 0".decoct($this->_umask));
  286. return TRUE;
  287. }
  288. function SetTimeout($timeout=30) {
  289. $this->_timeout=$timeout;
  290. $this->SendMSG("Timeout ".$this->_timeout);
  291. if($this->_connected)
  292. if(!$this->_settimeout($this->_ftp_control_sock)) return FALSE;
  293. return TRUE;
  294. }
  295. function connect($server=NULL) {
  296. if(!empty($server)) {
  297. if(!$this->SetServer($server)) return false;
  298. }
  299. if($this->_ready) return true;
  300. $this->SendMsg('Local OS : '.$this->OS_FullName[$this->OS_local]);
  301. if(!($this->_ftp_control_sock = $this->_connect($this->_host, $this->_port))) {
  302. $this->SendMSG("Error : Cannot connect to remote host \"".$this->_fullhost." :".$this->_port."\"");
  303. return FALSE;
  304. }
  305. $this->SendMSG("Connected to remote host \"".$this->_fullhost.":".$this->_port."\". Waiting for greeting.");
  306. do {
  307. if(!$this->_readmsg()) return FALSE;
  308. if(!$this->_checkCode()) return FALSE;
  309. $this->_lastaction=time();
  310. } while($this->_code<200);
  311. $this->_ready=true;
  312. $syst=$this->systype();
  313. if(!$syst) $this->SendMSG("Can't detect remote OS");
  314. else {
  315. if(preg_match("/win|dos|novell/i", $syst[0])) $this->OS_remote=FTP_OS_Windows;
  316. elseif(preg_match("/os/i", $syst[0])) $this->OS_remote=FTP_OS_Mac;
  317. elseif(preg_match("/(li|u)nix/i", $syst[0])) $this->OS_remote=FTP_OS_Unix;
  318. else $this->OS_remote=FTP_OS_Mac;
  319. $this->SendMSG("Remote OS: ".$this->OS_FullName[$this->OS_remote]);
  320. }
  321. if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
  322. else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
  323. return TRUE;
  324. }
  325. function quit($force=false) {
  326. if($this->_ready) {
  327. if(!$this->_exec("QUIT") and !$force) return FALSE;
  328. if(!$this->_checkCode() and !$force) return FALSE;
  329. $this->_ready=false;
  330. $this->SendMSG("Session finished");
  331. }
  332. $this->_quit();
  333. return TRUE;
  334. }
  335. function login($user=NULL, $pass=NULL) {
  336. if(!is_null($user)) $this->_login=$user;
  337. else $this->_login="anonymous";
  338. if(!is_null($pass)) $this->_password=$pass;
  339. else $this->_password="anon@anon.com";
  340. if(!$this->_exec("USER ".$this->_login, "login")) return FALSE;
  341. if(!$this->_checkCode()) return FALSE;
  342. if($this->_code!=230) {
  343. if(!$this->_exec((($this->_code==331)?"PASS ":"ACCT ").$this->_password, "login")) return FALSE;
  344. if(!$this->_checkCode()) return FALSE;
  345. }
  346. $this->SendMSG("Authentication succeeded");
  347. if(empty($this->_features)) {
  348. if(!$this->features()) $this->SendMSG("Can't get features list. All supported - disabled");
  349. else $this->SendMSG("Supported features: ".implode(", ", array_keys($this->_features)));
  350. }
  351. return TRUE;
  352. }
  353. function pwd() {
  354. if(!$this->_exec("PWD", "pwd")) return FALSE;
  355. if(!$this->_checkCode()) return FALSE;
  356. return ereg_replace("^[0-9]{3} \"(.+)\".+", "\\1", $this->_message);
  357. }
  358. function cdup() {
  359. if(!$this->_exec("CDUP", "cdup")) return FALSE;
  360. if(!$this->_checkCode()) return FALSE;
  361. return true;
  362. }
  363. function chdir($pathname) {
  364. if(!$this->_exec("CWD ".$pathname, "chdir")) return FALSE;
  365. if(!$this->_checkCode()) return FALSE;
  366. return TRUE;
  367. }
  368. function rmdir($pathname) {
  369. if(!$this->_exec("RMD ".$pathname, "rmdir")) return FALSE;
  370. if(!$this->_checkCode()) return FALSE;
  371. return TRUE;
  372. }
  373. function mkdir($pathname) {
  374. if(!$this->_exec("MKD ".$pathname, "mkdir")) return FALSE;
  375. if(!$this->_checkCode()) return FALSE;
  376. return TRUE;
  377. }
  378. function rename($from, $to) {
  379. if(!$this->_exec("RNFR ".$from, "rename")) return FALSE;
  380. if(!$this->_checkCode()) return FALSE;
  381. if($this->_code==350) {
  382. if(!$this->_exec("RNTO ".$to, "rename")) return FALSE;
  383. if(!$this->_checkCode()) return FALSE;
  384. } else return FALSE;
  385. return TRUE;
  386. }
  387. function filesize($pathname) {
  388. if(!isset($this->_features["SIZE"])) {
  389. $this->PushError("filesize", "not supported by server");
  390. return FALSE;
  391. }
  392. if(!$this->_exec("SIZE ".$pathname, "filesize")) return FALSE;
  393. if(!$this->_checkCode()) return FALSE;
  394. return ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
  395. }
  396. function abort() {
  397. if(!$this->_exec("ABOR", "abort")) return FALSE;
  398. if(!$this->_checkCode()) {
  399. if($this->_code!=426) return FALSE;
  400. if(!$this->_readmsg("abort")) return FALSE;
  401. if(!$this->_checkCode()) return FALSE;
  402. }
  403. return true;
  404. }
  405. function mdtm($pathname) {
  406. if(!isset($this->_features["MDTM"])) {
  407. $this->PushError("mdtm", "not supported by server");
  408. return FALSE;
  409. }
  410. if(!$this->_exec("MDTM ".$pathname, "mdtm")) return FALSE;
  411. if(!$this->_checkCode()) return FALSE;
  412. $mdtm = ereg_replace("^[0-9]{3} ([0-9]+)".CRLF, "\\1", $this->_message);
  413. $date = sscanf($mdtm, "%4d%2d%2d%2d%2d%2d");
  414. $timestamp = mktime($date[3], $date[4], $date[5], $date[1], $date[2], $date[0]);
  415. return $timestamp;
  416. }
  417. function systype() {
  418. if(!$this->_exec("SYST", "systype")) return FALSE;
  419. if(!$this->_checkCode()) return FALSE;
  420. $DATA = explode(" ", $this->_message);
  421. return array($DATA[1], $DATA[3]);
  422. }
  423. function delete($pathname) {
  424. if(!$this->_exec("DELE ".$pathname, "delete")) return FALSE;
  425. if(!$this->_checkCode()) return FALSE;
  426. return TRUE;
  427. }
  428. function site($command, $fnction="site") {
  429. if(!$this->_exec("SITE ".$command, $fnction)) return FALSE;
  430. if(!$this->_checkCode()) return FALSE;
  431. return TRUE;
  432. }
  433. function chmod($pathname, $mode) {
  434. if(!$this->site( sprintf('CHMOD %o %s', $mode, $pathname), "chmod")) return FALSE;
  435. return TRUE;
  436. }
  437. function restore($from) {
  438. if(!isset($this->_features["REST"])) {
  439. $this->PushError("restore", "not supported by server");
  440. return FALSE;
  441. }
  442. if($this->_curtype!=FTP_BINARY) {
  443. $this->PushError("restore", "can't restore in ASCII mode");
  444. return FALSE;
  445. }
  446. if(!$this->_exec("REST ".$from, "resore")) return FALSE;
  447. if(!$this->_checkCode()) return FALSE;
  448. return TRUE;
  449. }
  450. function features() {
  451. if(!$this->_exec("FEAT", "features")) return FALSE;
  452. if(!$this->_checkCode()) return FALSE;
  453. $f=preg_split("/[".CRLF."]+/", preg_replace("/[0-9]{3}[ -].*[".CRLF."]+/", "", $this->_message), -1, PREG_SPLIT_NO_EMPTY);
  454. $this->_features=array();
  455. foreach($f as $k=>$v) {
  456. $v=explode(" ", trim($v));
  457. $this->_features[array_shift($v)]=$v;
  458. }
  459. return true;
  460. }
  461. function rawlist($pathname="", $arg="") {
  462. return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "LIST", "rawlist");
  463. }
  464. function nlist($pathname="") {
  465. return $this->_list(($arg?" ".$arg:"").($pathname?" ".$pathname:""), "NLST", "nlist");
  466. }
  467. function is_exists($pathname) {
  468. return $this->file_exists($pathname);
  469. }
  470. function file_exists($pathname) {
  471. $exists=true;
  472. if(!$this->_exec("RNFR ".$pathname, "rename")) $exists=FALSE;
  473. else {
  474. if(!$this->_checkCode()) $exists=FALSE;
  475. $this->abort();
  476. }
  477. if($exists) $this->SendMSG("Remote file ".$pathname." exists");
  478. else $this->SendMSG("Remote file ".$pathname." does not exist");
  479. return $exists;
  480. }
  481. function fget($fp, $remotefile,$rest=0) {
  482. if($this->_can_restore and $rest!=0) fseek($fp, $rest);
  483. $pi=pathinfo($remotefile);
  484. if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
  485. else $mode=FTP_BINARY;
  486. if(!$this->_data_prepare($mode)) {
  487. return FALSE;
  488. }
  489. if($this->_can_restore and $rest!=0) $this->restore($rest);
  490. if(!$this->_exec("RETR ".$remotefile, "get")) {
  491. $this->_data_close();
  492. return FALSE;
  493. }
  494. if(!$this->_checkCode()) {
  495. $this->_data_close();
  496. return FALSE;
  497. }
  498. $out=$this->_data_read($mode, $fp);
  499. $this->_data_close();
  500. if(!$this->_readmsg()) return FALSE;
  501. if(!$this->_checkCode()) return FALSE;
  502. return $out;
  503. }
  504. function get($remotefile, $localfile=NULL, $rest=0) {
  505. if(is_null($localfile)) $localfile=$remotefile;
  506. if (@file_exists($localfile)) $this->SendMSG("Warning : local file will be overwritten");
  507. $fp = @fopen($localfile, "w");
  508. if (!$fp) {
  509. $this->PushError("get","can't open local file", "Cannot create \"".$localfile."\"");
  510. return FALSE;
  511. }
  512. if($this->_can_restore and $rest!=0) fseek($fp, $rest);
  513. $pi=pathinfo($remotefile);
  514. if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
  515. else $mode=FTP_BINARY;
  516. if(!$this->_data_prepare($mode)) {
  517. fclose($fp);
  518. return FALSE;
  519. }
  520. if($this->_can_restore and $rest!=0) $this->restore($rest);
  521. if(!$this->_exec("RETR ".$remotefile, "get")) {
  522. $this->_data_close();
  523. fclose($fp);
  524. return FALSE;
  525. }
  526. if(!$this->_checkCode()) {
  527. $this->_data_close();
  528. fclose($fp);
  529. return FALSE;
  530. }
  531. $out=$this->_data_read($mode, $fp);
  532. fclose($fp);
  533. $this->_data_close();
  534. if(!$this->_readmsg()) return FALSE;
  535. if(!$this->_checkCode()) return FALSE;
  536. return $out;
  537. }
  538. function fput($remotefile, $fp) {
  539. if($this->_can_restore and $rest!=0) fseek($fp, $rest);
  540. $pi=pathinfo($remotefile);
  541. if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
  542. else $mode=FTP_BINARY;
  543. if(!$this->_data_prepare($mode)) {
  544. return FALSE;
  545. }
  546. if($this->_can_restore and $rest!=0) $this->restore($rest);
  547. if(!$this->_exec("STOR ".$remotefile, "put")) {
  548. $this->_data_close();
  549. return FALSE;
  550. }
  551. if(!$this->_checkCode()) {
  552. $this->_data_close();
  553. return FALSE;
  554. }
  555. $ret=$this->_data_write($mode, $fp);
  556. $this->_data_close();
  557. if(!$this->_readmsg()) return FALSE;
  558. if(!$this->_checkCode()) return FALSE;
  559. return $ret;
  560. }
  561. function put($localfile, $remotefile=NULL, $rest=0) {
  562. if(is_null($remotefile)) $remotefile=$localfile;
  563. if (!file_exists($localfile)) {
  564. $this->PushError("put","can't open local file", "No such file or directory \"".$localfile."\"");
  565. return FALSE;
  566. }
  567. $fp = @fopen($localfile, "r");
  568. if (!$fp) {
  569. $this->PushError("put","can't open local file", "Cannot read file \"".$localfile."\"");
  570. return FALSE;
  571. }
  572. if($this->_can_restore and $rest!=0) fseek($fp, $rest);
  573. $pi=pathinfo($localfile);
  574. if($this->_type==FTP_ASCII or ($this->_type==FTP_AUTOASCII and in_array(strtoupper($pi["extension"]), $this->AutoAsciiExt))) $mode=FTP_ASCII;
  575. else $mode=FTP_BINARY;
  576. if(!$this->_data_prepare($mode)) {
  577. fclose($fp);
  578. return FALSE;
  579. }
  580. if($this->_can_restore and $rest!=0) $this->restore($rest);
  581. if(!$this->_exec("STOR ".$remotefile, "put")) {
  582. $this->_data_close();
  583. fclose($fp);
  584. return FALSE;
  585. }
  586. if(!$this->_checkCode()) {
  587. $this->_data_close();
  588. fclose($fp);
  589. return FALSE;
  590. }
  591. $ret=$this->_data_write($mode, $fp);
  592. fclose($fp);
  593. $this->_data_close();
  594. if(!$this->_readmsg()) return FALSE;
  595. if(!$this->_checkCode()) return FALSE;
  596. return $ret;
  597. }
  598. function mput($local=".", $remote=NULL, $continious=false) {
  599. $local=realpath($local);
  600. if(!@file_exists($local)) {
  601. $this->PushError("mput","can't open local folder", "Cannot stat folder \"".$local."\"");
  602. return FALSE;
  603. }
  604. if(!is_dir($local)) return $this->put($local, $remote);
  605. if(empty($remote)) $remote=".";
  606. elseif(!$this->file_exists($remote) and !$this->mkdir($remote)) return FALSE;
  607. if($handle = opendir($local)) {
  608. $list=array();
  609. while (false !== ($file = readdir($handle))) {
  610. if ($file != "." && $file != "..") $list[]=$file;
  611. }
  612. closedir($handle);
  613. } else {
  614. $this->PushError("mput","can't open local folder", "Cannot read folder \"".$local."\"");
  615. return FALSE;
  616. }
  617. if(empty($list)) return TRUE;
  618. $ret=true;
  619. foreach($list as $el) {
  620. if(is_dir($local."/".$el)) $t=$this->mput($local."/".$el, $remote."/".$el);
  621. else $t=$this->put($local."/".$el, $remote."/".$el);
  622. if(!$t) {
  623. $ret=FALSE;
  624. if(!$continious) break;
  625. }
  626. }
  627. return $ret;
  628. }
  629. function mget($remote, $local=".", $continious=false) {
  630. $list=$this->rawlist($remote, "-lA");
  631. if($list===false) {
  632. $this->PushError("mget","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
  633. return FALSE;
  634. }
  635. if(empty($list)) return true;
  636. if(!@file_exists($local)) {
  637. if(!@mkdir($local)) {
  638. $this->PushError("mget","can't create local folder", "Cannot create folder \"".$local."\"");
  639. return FALSE;
  640. }
  641. }
  642. foreach($list as $k=>$v) {
  643. $list[$k]=$this->parselisting($v);
  644. if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
  645. }
  646. $ret=true;
  647. foreach($list as $el) {
  648. if($el["type"]=="d") {
  649. if(!$this->mget($remote."/".$el["name"], $local."/".$el["name"], $continious)) {
  650. $this->PushError("mget", "can't copy folder", "Can't copy remote folder \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
  651. $ret=false;
  652. if(!$continious) break;
  653. }
  654. } else {
  655. if(!$this->get($remote."/".$el["name"], $local."/".$el["name"])) {
  656. $this->PushError("mget", "can't copy file", "Can't copy remote file \"".$remote."/".$el["name"]."\" to local \"".$local."/".$el["name"]."\"");
  657. $ret=false;
  658. if(!$continious) break;
  659. }
  660. }
  661. @chmod($local."/".$el["name"], $el["perms"]);
  662. $t=strtotime($el["date"]);
  663. if($t!==-1 and $t!==false) @touch($local."/".$el["name"], $t);
  664. }
  665. return $ret;
  666. }
  667. function mdel($remote, $continious=false) {
  668. $list=$this->rawlist($remote, "-la");
  669. if($list===false) {
  670. $this->PushError("mdel","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
  671. return false;
  672. }
  673. foreach($list as $k=>$v) {
  674. $list[$k]=$this->parselisting($v);
  675. if($list[$k]["name"]=="." or $list[$k]["name"]=="..") unset($list[$k]);
  676. }
  677. $ret=true;
  678. foreach($list as $el) {
  679. if ( empty($el) )
  680. continue;
  681. if($el["type"]=="d") {
  682. if(!$this->mdel($remote."/".$el["name"], $continious)) {
  683. $ret=false;
  684. if(!$continious) break;
  685. }
  686. } else {
  687. if (!$this->delete($remote."/".$el["name"])) {
  688. $this->PushError("mdel", "can't delete file", "Can't delete remote file \"".$remote."/".$el["name"]."\"");
  689. $ret=false;
  690. if(!$continious) break;
  691. }
  692. }
  693. }
  694. if(!$this->rmdir($remote)) {
  695. $this->PushError("mdel", "can't delete folder", "Can't delete remote folder \"".$remote."/".$el["name"]."\"");
  696. $ret=false;
  697. }
  698. return $ret;
  699. }
  700. function mmkdir($dir, $mode = 0777) {
  701. if(empty($dir)) return FALSE;
  702. if($this->is_exists($dir) or $dir == "/" ) return TRUE;
  703. if(!$this->mmkdir(dirname($dir), $mode)) return false;
  704. $r=$this->mkdir($dir, $mode);
  705. $this->chmod($dir,$mode);
  706. return $r;
  707. }
  708. function glob($pattern, $handle=NULL) {
  709. $path=$output=null;
  710. if(PHP_OS=='WIN32') $slash='\\';
  711. else $slash='/';
  712. $lastpos=strrpos($pattern,$slash);
  713. if(!($lastpos===false)) {
  714. $path=substr($pattern,0,-$lastpos-1);
  715. $pattern=substr($pattern,$lastpos);
  716. } else $path=getcwd();
  717. if(is_array($handle) and !empty($handle)) {
  718. while($dir=each($handle)) {
  719. if($this->glob_pattern_match($pattern,$dir))
  720. $output[]=$dir;
  721. }
  722. } else {
  723. $handle=@opendir($path);
  724. if($handle===false) return false;
  725. while($dir=readdir($handle)) {
  726. if($this->glob_pattern_match($pattern,$dir))
  727. $output[]=$dir;
  728. }
  729. closedir($handle);
  730. }
  731. if(is_array($output)) return $output;
  732. return false;
  733. }
  734. function glob_pattern_match($pattern,$string) {
  735. $out=null;
  736. $chunks=explode(';',$pattern);
  737. foreach($chunks as $pattern) {
  738. $escape=array('$','^','.','{','}','(',')','[',']','|');
  739. while(strpos($pattern,'**')!==false)
  740. $pattern=str_replace('**','*',$pattern);
  741. foreach($escape as $probe)
  742. $pattern=str_replace($probe,"\\$probe",$pattern);
  743. $pattern=str_replace('?*','*',
  744. str_replace('*?','*',
  745. str_replace('*',".*",
  746. str_replace('?','.{1,1}',$pattern))));
  747. $out[]=$pattern;
  748. }
  749. if(count($out)==1) return($this->glob_regexp("^$out[0]$",$string));
  750. else {
  751. foreach($out as $tester)
  752. if($this->my_regexp("^$tester$",$string)) return true;
  753. }
  754. return false;
  755. }
  756. function glob_regexp($pattern,$probe) {
  757. $sensitive=(PHP_OS!='WIN32');
  758. return ($sensitive?
  759. ereg($pattern,$probe):
  760. eregi($pattern,$probe)
  761. );
  762. }
  763. function dirlist($remote) {
  764. $list=$this->rawlist($remote, "-la");
  765. if($list===false) {
  766. $this->PushError("dirlist","can't read remote folder list", "Can't read remote folder \"".$remote."\" contents");
  767. return false;
  768. }
  769. $dirlist = array();
  770. foreach($list as $k=>$v) {
  771. $entry=$this->parselisting($v);
  772. if ( empty($entry) )
  773. continue;
  774. if($entry["name"]=="." or $entry["name"]=="..")
  775. continue;
  776. $dirlist[$entry['name']] = $entry;
  777. }
  778. return $dirlist;
  779. }
  780. // <!-- --------------------------------------------------------------------------------------- -->
  781. // <!-- Private functions -->
  782. // <!-- --------------------------------------------------------------------------------------- -->
  783. function _checkCode() {
  784. return ($this->_code<400 and $this->_code>0);
  785. }
  786. function _list($arg="", $cmd="LIST", $fnction="_list") {
  787. if(!$this->_data_prepare()) return false;
  788. if(!$this->_exec($cmd.$arg, $fnction)) {
  789. $this->_data_close();
  790. return FALSE;
  791. }
  792. if(!$this->_checkCode()) {
  793. $this->_data_close();
  794. return FALSE;
  795. }
  796. $out="";
  797. if($this->_code<200) {
  798. $out=$this->_data_read();
  799. $this->_data_close();
  800. if(!$this->_readmsg()) return FALSE;
  801. if(!$this->_checkCode()) return FALSE;
  802. if($out === FALSE ) return FALSE;
  803. $out=preg_split("/[".CRLF."]+/", $out, -1, PREG_SPLIT_NO_EMPTY);
  804. // $this->SendMSG(implode($this->_eol_code[$this->OS_local], $out));
  805. }
  806. return $out;
  807. }
  808. // <!-- --------------------------------------------------------------------------------------- -->
  809. // <!-- Partie : gestion des erreurs -->
  810. // <!-- --------------------------------------------------------------------------------------- -->
  811. // Gnre une erreur pour traitement externe la classe
  812. function PushError($fctname,$msg,$desc=false){
  813. $error=array();
  814. $error['time']=time();
  815. $error['fctname']=$fctname;
  816. $error['msg']=$msg;
  817. $error['desc']=$desc;
  818. if($desc) $tmp=' ('.$desc.')'; else $tmp='';
  819. $this->SendMSG($fctname.': '.$msg.$tmp);
  820. return(array_push($this->_error_array,$error));
  821. }
  822. // Rcupre une erreur externe
  823. function PopError(){
  824. if(count($this->_error_array)) return(array_pop($this->_error_array));
  825. else return(false);
  826. }
  827. }
  828. $mod_sockets=TRUE;
  829. if (!extension_loaded('sockets')) {
  830. $prefix = (PHP_SHLIB_SUFFIX == 'dll') ? 'php_' : '';
  831. if(!@dl($prefix . 'sockets.' . PHP_SHLIB_SUFFIX)) $mod_sockets=FALSE;
  832. }
  833. require_once "class-ftp-".($mod_sockets?"sockets":"pure").".php";
  834. ?>