ftp_pure

FTP implementation using fsockopen to connect.

Defined (1)

The class is defined in the following location(s).

/wp-admin/includes/class-ftp-pure.php  
  1. class ftp_pure extends ftp_base { 
  2.  
  3. function __construct($verb=FALSE, $le=FALSE) { 
  4. parent::__construct(false, $verb, $le); 
  5.  
  6. // <!-- --------------------------------------------------------------------------------------- --> 
  7. // <!-- Private functions --> 
  8. // <!-- --------------------------------------------------------------------------------------- --> 
  9.  
  10. function _settimeout($sock) { 
  11. if(!@stream_set_timeout($sock, $this->_timeout)) { 
  12. $this->PushError('_settimeout', 'socket set send timeout'); 
  13. $this->_quit(); 
  14. return FALSE; 
  15. return TRUE; 
  16.  
  17. function _connect($host, $port) { 
  18. $this->SendMSG("Creating socket"); 
  19. $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); 
  20. if (!$sock) { 
  21. $this->PushError('_connect', 'socket connect failed', $errstr." (".$errno.")"); 
  22. return FALSE; 
  23. $this->_connected=true; 
  24. return $sock; 
  25.  
  26. function _readmsg($fnction="_readmsg") { 
  27. if(!$this->_connected) { 
  28. $this->PushError($fnction, 'Connect first'); 
  29. return FALSE; 
  30. $result=true; 
  31. $this->_message=""; 
  32. $this->_code=0; 
  33. $go=true; 
  34. do { 
  35. $tmp=@fgets($this->_ftp_control_sock, 512); 
  36. if($tmp===false) { 
  37. $go=$result=false; 
  38. $this->PushError($fnction, 'Read failed'); 
  39. } else { 
  40. $this->_message.=$tmp; 
  41. if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1, 2})+\\1)? [^".CRLF."]+[".CRLF."]{1, 2}$/", $this->_message, $regs)) $go=false; 
  42. } while($go); 
  43. if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF; 
  44. $this->_code=(int)$regs[1]; 
  45. return $result; 
  46.  
  47. function _exec($cmd, $fnction="_exec") { 
  48. if(!$this->_ready) { 
  49. $this->PushError($fnction, 'Connect first'); 
  50. return FALSE; 
  51. if($this->LocalEcho) echo "PUT > ", $cmd, CRLF; 
  52. $status=@fputs($this->_ftp_control_sock, $cmd.CRLF); 
  53. if($status===false) { 
  54. $this->PushError($fnction, 'socket write failed'); 
  55. return FALSE; 
  56. $this->_lastaction=time(); 
  57. if(!$this->_readmsg($fnction)) return FALSE; 
  58. return TRUE; 
  59.  
  60. function _data_prepare($mode=FTP_ASCII) { 
  61. if(!$this->_settype($mode)) return FALSE; 
  62. if($this->_passive) { 
  63. if(!$this->_exec("PASV", "pasv")) { 
  64. $this->_data_close(); 
  65. return FALSE; 
  66. if(!$this->_checkCode()) { 
  67. $this->_data_close(); 
  68. return FALSE; 
  69. $ip_port = explode(", ", preg_replace("/^.+ \\(?([0-9]{1, 3}, [0-9]{1, 3}, [0-9]{1, 3}, [0-9]{1, 3}, [0-9]+, [0-9]+)\\)?.*$/s", "\\1", $this->_message)); 
  70. $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3]; 
  71. $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]); 
  72. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); 
  73. $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout); 
  74. if(!$this->_ftp_data_sock) { 
  75. $this->PushError("_data_prepare", "fsockopen fails", $errstr." (".$errno.")"); 
  76. $this->_data_close(); 
  77. return FALSE; 
  78. else $this->_ftp_data_sock; 
  79. } else { 
  80. $this->SendMSG("Only passive connections available!"); 
  81. return FALSE; 
  82. return TRUE; 
  83.  
  84. function _data_read($mode=FTP_ASCII, $fp=NULL) { 
  85. if(is_resource($fp)) $out=0; 
  86. else $out=""; 
  87. if(!$this->_passive) { 
  88. $this->SendMSG("Only passive connections available!"); 
  89. return FALSE; 
  90. while (!feof($this->_ftp_data_sock)) { 
  91. $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size); 
  92. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block); 
  93. if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block)); 
  94. else $out.=$block; 
  95. return $out; 
  96.  
  97. function _data_write($mode=FTP_ASCII, $fp=NULL) { 
  98. if(is_resource($fp)) $out=0; 
  99. else $out=""; 
  100. if(!$this->_passive) { 
  101. $this->SendMSG("Only passive connections available!"); 
  102. return FALSE; 
  103. if(is_resource($fp)) { 
  104. while(!feof($fp)) { 
  105. $block=fread($fp, $this->_ftp_buff_size); 
  106. if(!$this->_data_write_block($mode, $block)) return false; 
  107. } elseif(!$this->_data_write_block($mode, $fp)) return false; 
  108. return TRUE; 
  109.  
  110. function _data_write_block($mode, $block) { 
  111. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block); 
  112. do { 
  113. if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) { 
  114. $this->PushError("_data_write", "Can't write to socket"); 
  115. return FALSE; 
  116. $block=substr($block, $t); 
  117. } while(!empty($block)); 
  118. return true; 
  119.  
  120. function _data_close() { 
  121. @fclose($this->_ftp_data_sock); 
  122. $this->SendMSG("Disconnected data from remote host"); 
  123. return TRUE; 
  124.  
  125. function _quit($force=FALSE) { 
  126. if($this->_connected or $force) { 
  127. @fclose($this->_ftp_control_sock); 
  128. $this->_connected=false; 
  129. $this->SendMSG("Socket closed");