ftp

FTP implementation using fsockopen to connect.

Defined (2)

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

/wp-admin/includes/class-ftp-pure.php  
  1. class ftp extends ftp_base { 
  2.  
  3. function __construct($verb=FALSE, $le=FALSE) { 
  4. parent::__construct(false, $verb, $le); 
  5.  
  6. function ftp($verb=FALSE, $le=FALSE) { 
  7. $this->__construct($verb, $le); 
  8.  
  9. // <!-- --------------------------------------------------------------------------------------- --> 
  10. // <!-- Private functions --> 
  11. // <!-- --------------------------------------------------------------------------------------- --> 
  12.  
  13. function _settimeout($sock) { 
  14. if(!@stream_set_timeout($sock, $this->_timeout)) { 
  15. $this->PushError('_settimeout', 'socket set send timeout'); 
  16. $this->_quit(); 
  17. return FALSE; 
  18. return TRUE; 
  19.  
  20. function _connect($host, $port) { 
  21. $this->SendMSG("Creating socket"); 
  22. $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout); 
  23. if (!$sock) { 
  24. $this->PushError('_connect', 'socket connect failed', $errstr." (".$errno.")"); 
  25. return FALSE; 
  26. $this->_connected=true; 
  27. return $sock; 
  28.  
  29. function _readmsg($fnction="_readmsg") { 
  30. if(!$this->_connected) { 
  31. $this->PushError($fnction, 'Connect first'); 
  32. return FALSE; 
  33. $result=true; 
  34. $this->_message=""; 
  35. $this->_code=0; 
  36. $go=true; 
  37. do { 
  38. $tmp=@fgets($this->_ftp_control_sock, 512); 
  39. if($tmp===false) { 
  40. $go=$result=false; 
  41. $this->PushError($fnction, 'Read failed'); 
  42. } else { 
  43. $this->_message.=$tmp; 
  44. if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1, 2})+\\1)? [^".CRLF."]+[".CRLF."]{1, 2}$/", $this->_message, $regs)) $go=false; 
  45. } while($go); 
  46. if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF; 
  47. $this->_code=(int)$regs[1]; 
  48. return $result; 
  49.  
  50. function _exec($cmd, $fnction="_exec") { 
  51. if(!$this->_ready) { 
  52. $this->PushError($fnction, 'Connect first'); 
  53. return FALSE; 
  54. if($this->LocalEcho) echo "PUT > ", $cmd, CRLF; 
  55. $status=@fputs($this->_ftp_control_sock, $cmd.CRLF); 
  56. if($status===false) { 
  57. $this->PushError($fnction, 'socket write failed'); 
  58. return FALSE; 
  59. $this->_lastaction=time(); 
  60. if(!$this->_readmsg($fnction)) return FALSE; 
  61. return TRUE; 
  62.  
  63. function _data_prepare($mode=FTP_ASCII) { 
  64. if(!$this->_settype($mode)) return FALSE; 
  65. if($this->_passive) { 
  66. if(!$this->_exec("PASV", "pasv")) { 
  67. $this->_data_close(); 
  68. return FALSE; 
  69. if(!$this->_checkCode()) { 
  70. $this->_data_close(); 
  71. return FALSE; 
  72. $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)); 
  73. $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3]; 
  74. $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]); 
  75. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); 
  76. $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout); 
  77. if(!$this->_ftp_data_sock) { 
  78. $this->PushError("_data_prepare", "fsockopen fails", $errstr." (".$errno.")"); 
  79. $this->_data_close(); 
  80. return FALSE; 
  81. else $this->_ftp_data_sock; 
  82. } else { 
  83. $this->SendMSG("Only passive connections available!"); 
  84. return FALSE; 
  85. return TRUE; 
  86.  
  87. function _data_read($mode=FTP_ASCII, $fp=NULL) { 
  88. if(is_resource($fp)) $out=0; 
  89. else $out=""; 
  90. if(!$this->_passive) { 
  91. $this->SendMSG("Only passive connections available!"); 
  92. return FALSE; 
  93. while (!feof($this->_ftp_data_sock)) { 
  94. $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size); 
  95. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block); 
  96. if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block)); 
  97. else $out.=$block; 
  98. return $out; 
  99.  
  100. function _data_write($mode=FTP_ASCII, $fp=NULL) { 
  101. if(is_resource($fp)) $out=0; 
  102. else $out=""; 
  103. if(!$this->_passive) { 
  104. $this->SendMSG("Only passive connections available!"); 
  105. return FALSE; 
  106. if(is_resource($fp)) { 
  107. while(!feof($fp)) { 
  108. $block=fread($fp, $this->_ftp_buff_size); 
  109. if(!$this->_data_write_block($mode, $block)) return false; 
  110. } elseif(!$this->_data_write_block($mode, $fp)) return false; 
  111. return TRUE; 
  112.  
  113. function _data_write_block($mode, $block) { 
  114. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block); 
  115. do { 
  116. if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) { 
  117. $this->PushError("_data_write", "Can't write to socket"); 
  118. return FALSE; 
  119. $block=substr($block, $t); 
  120. } while(!empty($block)); 
  121. return true; 
  122.  
  123. function _data_close() { 
  124. @fclose($this->_ftp_data_sock); 
  125. $this->SendMSG("Disconnected data from remote host"); 
  126. return TRUE; 
  127.  
  128. function _quit($force=FALSE) { 
  129. if($this->_connected or $force) { 
  130. @fclose($this->_ftp_control_sock); 
  131. $this->_connected=false; 
  132. $this->SendMSG("Socket closed"); 
/wp-admin/includes/class-ftp-sockets.php  
  1. class ftp extends ftp_base { 
  2.  
  3. function __construct($verb=FALSE, $le=FALSE) { 
  4. parent::__construct(true, $verb, $le); 
  5.  
  6. function ftp($verb=FALSE, $le=FALSE) { 
  7. $this->__construct($verb, $le); 
  8.  
  9. // <!-- --------------------------------------------------------------------------------------- --> 
  10. // <!-- Private functions --> 
  11. // <!-- --------------------------------------------------------------------------------------- --> 
  12.  
  13. function _settimeout($sock) { 
  14. if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) { 
  15. $this->PushError('_connect', 'socket set receive timeout', socket_strerror(socket_last_error($sock))); 
  16. @socket_close($sock); 
  17. return FALSE; 
  18. if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) { 
  19. $this->PushError('_connect', 'socket set send timeout', socket_strerror(socket_last_error($sock))); 
  20. @socket_close($sock); 
  21. return FALSE; 
  22. return true; 
  23.  
  24. function _connect($host, $port) { 
  25. $this->SendMSG("Creating socket"); 
  26. if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) { 
  27. $this->PushError('_connect', 'socket create failed', socket_strerror(socket_last_error($sock))); 
  28. return FALSE; 
  29. if(!$this->_settimeout($sock)) return FALSE; 
  30. $this->SendMSG("Connecting to \"".$host.":".$port."\""); 
  31. if (!($res = @socket_connect($sock, $host, $port))) { 
  32. $this->PushError('_connect', 'socket connect failed', socket_strerror(socket_last_error($sock))); 
  33. @socket_close($sock); 
  34. return FALSE; 
  35. $this->_connected=true; 
  36. return $sock; 
  37.  
  38. function _readmsg($fnction="_readmsg") { 
  39. if(!$this->_connected) { 
  40. $this->PushError($fnction, 'Connect first'); 
  41. return FALSE; 
  42. $result=true; 
  43. $this->_message=""; 
  44. $this->_code=0; 
  45. $go=true; 
  46. do { 
  47. $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ); 
  48. if($tmp===false) { 
  49. $go=$result=false; 
  50. $this->PushError($fnction, 'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock))); 
  51. } else { 
  52. $this->_message.=$tmp; 
  53. $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs); 
  54. } while($go); 
  55. if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF; 
  56. $this->_code=(int)$regs[1]; 
  57. return $result; 
  58.  
  59. function _exec($cmd, $fnction="_exec") { 
  60. if(!$this->_ready) { 
  61. $this->PushError($fnction, 'Connect first'); 
  62. return FALSE; 
  63. if($this->LocalEcho) echo "PUT > ", $cmd, CRLF; 
  64. $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF); 
  65. if($status===false) { 
  66. $this->PushError($fnction, 'socket write failed', socket_strerror(socket_last_error($this->stream))); 
  67. return FALSE; 
  68. $this->_lastaction=time(); 
  69. if(!$this->_readmsg($fnction)) return FALSE; 
  70. return TRUE; 
  71.  
  72. function _data_prepare($mode=FTP_ASCII) { 
  73. if(!$this->_settype($mode)) return FALSE; 
  74. $this->SendMSG("Creating data socket"); 
  75. $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); 
  76. if ($this->_ftp_data_sock < 0) { 
  77. $this->PushError('_data_prepare', 'socket create failed', socket_strerror(socket_last_error($this->_ftp_data_sock))); 
  78. return FALSE; 
  79. if(!$this->_settimeout($this->_ftp_data_sock)) { 
  80. $this->_data_close(); 
  81. return FALSE; 
  82. if($this->_passive) { 
  83. if(!$this->_exec("PASV", "pasv")) { 
  84. $this->_data_close(); 
  85. return FALSE; 
  86. if(!$this->_checkCode()) { 
  87. $this->_data_close(); 
  88. return FALSE; 
  89. $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)); 
  90. $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3]; 
  91. $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]); 
  92. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); 
  93. if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) { 
  94. $this->PushError("_data_prepare", "socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock))); 
  95. $this->_data_close(); 
  96. return FALSE; 
  97. else $this->_ftp_temp_sock=$this->_ftp_data_sock; 
  98. } else { 
  99. if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) { 
  100. $this->PushError("_data_prepare", "can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock))); 
  101. $this->_data_close(); 
  102. return FALSE; 
  103. if(!@socket_bind($this->_ftp_data_sock, $addr)) { 
  104. $this->PushError("_data_prepare", "can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock))); 
  105. $this->_data_close(); 
  106. return FALSE; 
  107. if(!@socket_listen($this->_ftp_data_sock)) { 
  108. $this->PushError("_data_prepare", "can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock))); 
  109. $this->_data_close(); 
  110. return FALSE; 
  111. if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) { 
  112. $this->PushError("_data_prepare", "can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock))); 
  113. $this->_data_close(); 
  114. return FALSE; 
  115. if(!$this->_exec('PORT '.str_replace('.', ', ', $this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) { 
  116. $this->_data_close(); 
  117. return FALSE; 
  118. if(!$this->_checkCode()) { 
  119. $this->_data_close(); 
  120. return FALSE; 
  121. return TRUE; 
  122.  
  123. function _data_read($mode=FTP_ASCII, $fp=NULL) { 
  124. $NewLine=$this->_eol_code[$this->OS_local]; 
  125. if(is_resource($fp)) $out=0; 
  126. else $out=""; 
  127. if(!$this->_passive) { 
  128. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); 
  129. $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock); 
  130. if($this->_ftp_temp_sock===FALSE) { 
  131. $this->PushError("_data_read", "socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock))); 
  132. $this->_data_close(); 
  133. return FALSE; 
  134.  
  135. while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) { 
  136. if($block==="") break; 
  137. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block); 
  138. if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block)); 
  139. else $out.=$block; 
  140. return $out; 
  141.  
  142. function _data_write($mode=FTP_ASCII, $fp=NULL) { 
  143. $NewLine=$this->_eol_code[$this->OS_local]; 
  144. if(is_resource($fp)) $out=0; 
  145. else $out=""; 
  146. if(!$this->_passive) { 
  147. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport); 
  148. $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock); 
  149. if($this->_ftp_temp_sock===FALSE) { 
  150. $this->PushError("_data_write", "socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock))); 
  151. $this->_data_close(); 
  152. return false; 
  153. if(is_resource($fp)) { 
  154. while(!feof($fp)) { 
  155. $block=fread($fp, $this->_ftp_buff_size); 
  156. if(!$this->_data_write_block($mode, $block)) return false; 
  157. } elseif(!$this->_data_write_block($mode, $fp)) return false; 
  158. return true; 
  159.  
  160. function _data_write_block($mode, $block) { 
  161. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block); 
  162. do { 
  163. if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) { 
  164. $this->PushError("_data_write", "socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock))); 
  165. $this->_data_close(); 
  166. return FALSE; 
  167. $block=substr($block, $t); 
  168. } while(!empty($block)); 
  169. return true; 
  170.  
  171. function _data_close() { 
  172. @socket_close($this->_ftp_temp_sock); 
  173. @socket_close($this->_ftp_data_sock); 
  174. $this->SendMSG("Disconnected data from remote host"); 
  175. return TRUE; 
  176.  
  177. function _quit() { 
  178. if($this->_connected) { 
  179. @socket_close($this->_ftp_control_sock); 
  180. $this->_connected=false; 
  181. $this->SendMSG("Socket closed");