Edit File by line
/home/zeestwma/richards.../wp-admin/includes
File: class-ftp-sockets.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* PemFTP - An Ftp implementation in pure PHP
[2] Fix | Delete
*
[3] Fix | Delete
* @package PemFTP
[4] Fix | Delete
* @since 2.5.0
[5] Fix | Delete
*
[6] Fix | Delete
* @version 1.0
[7] Fix | Delete
* @copyright Alexey Dotsenko
[8] Fix | Delete
* @author Alexey Dotsenko
[9] Fix | Delete
* @link https://www.phpclasses.org/package/1743-PHP-FTP-client-in-pure-PHP.html
[10] Fix | Delete
* @license LGPL https://opensource.org/licenses/lgpl-license.html
[11] Fix | Delete
*/
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Socket Based FTP implementation
[15] Fix | Delete
*
[16] Fix | Delete
* @package PemFTP
[17] Fix | Delete
* @subpackage Socket
[18] Fix | Delete
* @since 2.5.0
[19] Fix | Delete
*
[20] Fix | Delete
* @version 1.0
[21] Fix | Delete
* @copyright Alexey Dotsenko
[22] Fix | Delete
* @author Alexey Dotsenko
[23] Fix | Delete
* @link https://www.phpclasses.org/package/1743-PHP-FTP-client-in-pure-PHP.html
[24] Fix | Delete
* @license LGPL https://opensource.org/licenses/lgpl-license.html
[25] Fix | Delete
*/
[26] Fix | Delete
class ftp_sockets extends ftp_base {
[27] Fix | Delete
[28] Fix | Delete
function __construct($verb=FALSE, $le=FALSE) {
[29] Fix | Delete
parent::__construct(true, $verb, $le);
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
// <!-- --------------------------------------------------------------------------------------- -->
[33] Fix | Delete
// <!-- Private functions -->
[34] Fix | Delete
// <!-- --------------------------------------------------------------------------------------- -->
[35] Fix | Delete
[36] Fix | Delete
function _settimeout($sock) {
[37] Fix | Delete
if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
[38] Fix | Delete
$this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
[39] Fix | Delete
@socket_close($sock);
[40] Fix | Delete
return FALSE;
[41] Fix | Delete
}
[42] Fix | Delete
if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
[43] Fix | Delete
$this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
[44] Fix | Delete
@socket_close($sock);
[45] Fix | Delete
return FALSE;
[46] Fix | Delete
}
[47] Fix | Delete
return true;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
function _connect($host, $port) {
[51] Fix | Delete
$this->SendMSG("Creating socket");
[52] Fix | Delete
if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
[53] Fix | Delete
$this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
[54] Fix | Delete
return FALSE;
[55] Fix | Delete
}
[56] Fix | Delete
if(!$this->_settimeout($sock)) return FALSE;
[57] Fix | Delete
$this->SendMSG("Connecting to \"".$host.":".$port."\"");
[58] Fix | Delete
if (!($res = @socket_connect($sock, $host, $port))) {
[59] Fix | Delete
$this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
[60] Fix | Delete
@socket_close($sock);
[61] Fix | Delete
return FALSE;
[62] Fix | Delete
}
[63] Fix | Delete
$this->_connected=true;
[64] Fix | Delete
return $sock;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
function _readmsg($fnction="_readmsg"){
[68] Fix | Delete
if(!$this->_connected) {
[69] Fix | Delete
$this->PushError($fnction,'Connect first');
[70] Fix | Delete
return FALSE;
[71] Fix | Delete
}
[72] Fix | Delete
$result=true;
[73] Fix | Delete
$this->_message="";
[74] Fix | Delete
$this->_code=0;
[75] Fix | Delete
$go=true;
[76] Fix | Delete
do {
[77] Fix | Delete
$tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
[78] Fix | Delete
if($tmp===false) {
[79] Fix | Delete
$go=$result=false;
[80] Fix | Delete
$this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
[81] Fix | Delete
} else {
[82] Fix | Delete
$this->_message.=$tmp;
[83] Fix | Delete
$go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
[84] Fix | Delete
}
[85] Fix | Delete
} while($go);
[86] Fix | Delete
if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
[87] Fix | Delete
$this->_code=(int)$regs[1];
[88] Fix | Delete
return $result;
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
function _exec($cmd, $fnction="_exec") {
[92] Fix | Delete
if(!$this->_ready) {
[93] Fix | Delete
$this->PushError($fnction,'Connect first');
[94] Fix | Delete
return FALSE;
[95] Fix | Delete
}
[96] Fix | Delete
if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
[97] Fix | Delete
$status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
[98] Fix | Delete
if($status===false) {
[99] Fix | Delete
$this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
[100] Fix | Delete
return FALSE;
[101] Fix | Delete
}
[102] Fix | Delete
$this->_lastaction=time();
[103] Fix | Delete
if(!$this->_readmsg($fnction)) return FALSE;
[104] Fix | Delete
return TRUE;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
function _data_prepare($mode=FTP_ASCII) {
[108] Fix | Delete
if(!$this->_settype($mode)) return FALSE;
[109] Fix | Delete
$this->SendMSG("Creating data socket");
[110] Fix | Delete
$this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
[111] Fix | Delete
if ($this->_ftp_data_sock < 0) {
[112] Fix | Delete
$this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
[113] Fix | Delete
return FALSE;
[114] Fix | Delete
}
[115] Fix | Delete
if(!$this->_settimeout($this->_ftp_data_sock)) {
[116] Fix | Delete
$this->_data_close();
[117] Fix | Delete
return FALSE;
[118] Fix | Delete
}
[119] Fix | Delete
if($this->_passive) {
[120] Fix | Delete
if(!$this->_exec("PASV", "pasv")) {
[121] Fix | Delete
$this->_data_close();
[122] Fix | Delete
return FALSE;
[123] Fix | Delete
}
[124] Fix | Delete
if(!$this->_checkCode()) {
[125] Fix | Delete
$this->_data_close();
[126] Fix | Delete
return FALSE;
[127] Fix | Delete
}
[128] Fix | Delete
$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));
[129] Fix | Delete
$this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
[130] Fix | Delete
$this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
[131] Fix | Delete
$this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
[132] Fix | Delete
if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
[133] Fix | Delete
$this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
[134] Fix | Delete
$this->_data_close();
[135] Fix | Delete
return FALSE;
[136] Fix | Delete
}
[137] Fix | Delete
else $this->_ftp_temp_sock=$this->_ftp_data_sock;
[138] Fix | Delete
} else {
[139] Fix | Delete
if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
[140] Fix | Delete
$this->PushError("_data_prepare","cannot get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
[141] Fix | Delete
$this->_data_close();
[142] Fix | Delete
return FALSE;
[143] Fix | Delete
}
[144] Fix | Delete
if(!@socket_bind($this->_ftp_data_sock,$addr)){
[145] Fix | Delete
$this->PushError("_data_prepare","cannot bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
[146] Fix | Delete
$this->_data_close();
[147] Fix | Delete
return FALSE;
[148] Fix | Delete
}
[149] Fix | Delete
if(!@socket_listen($this->_ftp_data_sock)) {
[150] Fix | Delete
$this->PushError("_data_prepare","cannot listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
[151] Fix | Delete
$this->_data_close();
[152] Fix | Delete
return FALSE;
[153] Fix | Delete
}
[154] Fix | Delete
if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
[155] Fix | Delete
$this->PushError("_data_prepare","cannot get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
[156] Fix | Delete
$this->_data_close();
[157] Fix | Delete
return FALSE;
[158] Fix | Delete
}
[159] Fix | Delete
if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
[160] Fix | Delete
$this->_data_close();
[161] Fix | Delete
return FALSE;
[162] Fix | Delete
}
[163] Fix | Delete
if(!$this->_checkCode()) {
[164] Fix | Delete
$this->_data_close();
[165] Fix | Delete
return FALSE;
[166] Fix | Delete
}
[167] Fix | Delete
}
[168] Fix | Delete
return TRUE;
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
function _data_read($mode=FTP_ASCII, $fp=NULL) {
[172] Fix | Delete
$NewLine=$this->_eol_code[$this->OS_local];
[173] Fix | Delete
if(is_resource($fp)) $out=0;
[174] Fix | Delete
else $out="";
[175] Fix | Delete
if(!$this->_passive) {
[176] Fix | Delete
$this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
[177] Fix | Delete
$this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
[178] Fix | Delete
if($this->_ftp_temp_sock===FALSE) {
[179] Fix | Delete
$this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
[180] Fix | Delete
$this->_data_close();
[181] Fix | Delete
return FALSE;
[182] Fix | Delete
}
[183] Fix | Delete
}
[184] Fix | Delete
[185] Fix | Delete
while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
[186] Fix | Delete
if($block==="") break;
[187] Fix | Delete
if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
[188] Fix | Delete
if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
[189] Fix | Delete
else $out.=$block;
[190] Fix | Delete
}
[191] Fix | Delete
return $out;
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
function _data_write($mode=FTP_ASCII, $fp=NULL) {
[195] Fix | Delete
$NewLine=$this->_eol_code[$this->OS_local];
[196] Fix | Delete
if(is_resource($fp)) $out=0;
[197] Fix | Delete
else $out="";
[198] Fix | Delete
if(!$this->_passive) {
[199] Fix | Delete
$this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
[200] Fix | Delete
$this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
[201] Fix | Delete
if($this->_ftp_temp_sock===FALSE) {
[202] Fix | Delete
$this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
[203] Fix | Delete
$this->_data_close();
[204] Fix | Delete
return false;
[205] Fix | Delete
}
[206] Fix | Delete
}
[207] Fix | Delete
if(is_resource($fp)) {
[208] Fix | Delete
while(!feof($fp)) {
[209] Fix | Delete
$block=fread($fp, $this->_ftp_buff_size);
[210] Fix | Delete
if(!$this->_data_write_block($mode, $block)) return false;
[211] Fix | Delete
}
[212] Fix | Delete
} elseif(!$this->_data_write_block($mode, $fp)) return false;
[213] Fix | Delete
return true;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
function _data_write_block($mode, $block) {
[217] Fix | Delete
if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
[218] Fix | Delete
do {
[219] Fix | Delete
if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
[220] Fix | Delete
$this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
[221] Fix | Delete
$this->_data_close();
[222] Fix | Delete
return FALSE;
[223] Fix | Delete
}
[224] Fix | Delete
$block=substr($block, $t);
[225] Fix | Delete
} while(!empty($block));
[226] Fix | Delete
return true;
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
function _data_close() {
[230] Fix | Delete
@socket_close($this->_ftp_temp_sock);
[231] Fix | Delete
@socket_close($this->_ftp_data_sock);
[232] Fix | Delete
$this->SendMSG("Disconnected data from remote host");
[233] Fix | Delete
return TRUE;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
function _quit() {
[237] Fix | Delete
if($this->_connected) {
[238] Fix | Delete
@socket_close($this->_ftp_control_sock);
[239] Fix | Delete
$this->_connected=false;
[240] Fix | Delete
$this->SendMSG("Socket closed");
[241] Fix | Delete
}
[242] Fix | Delete
}
[243] Fix | Delete
}
[244] Fix | Delete
?>
[245] Fix | Delete
[246] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function