ftp_sockets

Socket Based FTP implementation.

Defined (1)

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

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