-
+ 56C2FDEF9AA566641B3386D291E7A5B5E145922E360CD27BBD3EB3E1E172D5016B6ADB38BE436DD47680759E28D097C00B4C96BE2B3BCEF78FE522D05D7CA22C
mp-wp/wp-admin/includes/class-ftp-sockets.php
(0 . 0)(1 . 250)
23244 <?php
23245 /**
23246 * PemFTP - A Ftp implementation in pure PHP
23247 *
23248 * @package PemFTP
23249 * @since 2.5
23250 *
23251 * @version 1.0
23252 * @copyright Alexey Dotsenko
23253 * @author Alexey Dotsenko
23254 * @link http://www.phpclasses.org/browse/package/1743.html Site
23255 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
23256 */
23257
23258 /**
23259 * Socket Based FTP implementation
23260 *
23261 * @package PemFTP
23262 * @subpackage Socket
23263 * @since 2.5
23264 *
23265 * @version 1.0
23266 * @copyright Alexey Dotsenko
23267 * @author Alexey Dotsenko
23268 * @link http://www.phpclasses.org/browse/package/1743.html Site
23269 * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
23270 */
23271 class ftp extends ftp_base {
23272
23273 function ftp($verb=FALSE, $le=FALSE) {
23274 $this->__construct($verb, $le);
23275 }
23276
23277 function __construct($verb=FALSE, $le=FALSE) {
23278 parent::__construct(true, $verb, $le);
23279 }
23280
23281 // <!-- --------------------------------------------------------------------------------------- -->
23282 // <!-- Private functions -->
23283 // <!-- --------------------------------------------------------------------------------------- -->
23284
23285 function _settimeout($sock) {
23286 if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
23287 $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
23288 @socket_close($sock);
23289 return FALSE;
23290 }
23291 if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
23292 $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
23293 @socket_close($sock);
23294 return FALSE;
23295 }
23296 return true;
23297 }
23298
23299 function _connect($host, $port) {
23300 $this->SendMSG("Creating socket");
23301 if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
23302 $this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
23303 return FALSE;
23304 }
23305 if(!$this->_settimeout($sock)) return FALSE;
23306 $this->SendMSG("Connecting to \"".$host.":".$port."\"");
23307 if (!($res = @socket_connect($sock, $host, $port))) {
23308 $this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
23309 @socket_close($sock);
23310 return FALSE;
23311 }
23312 $this->_connected=true;
23313 return $sock;
23314 }
23315
23316 function _readmsg($fnction="_readmsg"){
23317 if(!$this->_connected) {
23318 $this->PushError($fnction,'Connect first');
23319 return FALSE;
23320 }
23321 $result=true;
23322 $this->_message="";
23323 $this->_code=0;
23324 $go=true;
23325 do {
23326 $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
23327 if($tmp===false) {
23328 $go=$result=false;
23329 $this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
23330 } else {
23331 $this->_message.=$tmp;
23332 $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
23333 }
23334 } while($go);
23335 if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
23336 $this->_code=(int)$regs[1];
23337 return $result;
23338 }
23339
23340 function _exec($cmd, $fnction="_exec") {
23341 if(!$this->_ready) {
23342 $this->PushError($fnction,'Connect first');
23343 return FALSE;
23344 }
23345 if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
23346 $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
23347 if($status===false) {
23348 $this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
23349 return FALSE;
23350 }
23351 $this->_lastaction=time();
23352 if(!$this->_readmsg($fnction)) return FALSE;
23353 return TRUE;
23354 }
23355
23356 function _data_prepare($mode=FTP_ASCII) {
23357 if(!$this->_settype($mode)) return FALSE;
23358 $this->SendMSG("Creating data socket");
23359 $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
23360 if ($this->_ftp_data_sock < 0) {
23361 $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
23362 return FALSE;
23363 }
23364 if(!$this->_settimeout($this->_ftp_data_sock)) {
23365 $this->_data_close();
23366 return FALSE;
23367 }
23368 if($this->_passive) {
23369 if(!$this->_exec("PASV", "pasv")) {
23370 $this->_data_close();
23371 return FALSE;
23372 }
23373 if(!$this->_checkCode()) {
23374 $this->_data_close();
23375 return FALSE;
23376 }
23377 $ip_port = explode(",", ereg_replace("^.+ \\(?([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0-9]+,[0-9]+)\\)?.*".CRLF."$", "\\1", $this->_message));
23378 $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
23379 $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
23380 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
23381 if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
23382 $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
23383 $this->_data_close();
23384 return FALSE;
23385 }
23386 else $this->_ftp_temp_sock=$this->_ftp_data_sock;
23387 } else {
23388 if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
23389 $this->PushError("_data_prepare","can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
23390 $this->_data_close();
23391 return FALSE;
23392 }
23393 if(!@socket_bind($this->_ftp_data_sock,$addr)){
23394 $this->PushError("_data_prepare","can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
23395 $this->_data_close();
23396 return FALSE;
23397 }
23398 if(!@socket_listen($this->_ftp_data_sock)) {
23399 $this->PushError("_data_prepare","can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
23400 $this->_data_close();
23401 return FALSE;
23402 }
23403 if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
23404 $this->PushError("_data_prepare","can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
23405 $this->_data_close();
23406 return FALSE;
23407 }
23408 if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
23409 $this->_data_close();
23410 return FALSE;
23411 }
23412 if(!$this->_checkCode()) {
23413 $this->_data_close();
23414 return FALSE;
23415 }
23416 }
23417 return TRUE;
23418 }
23419
23420 function _data_read($mode=FTP_ASCII, $fp=NULL) {
23421 $NewLine=$this->_eol_code[$this->OS_local];
23422 if(is_resource($fp)) $out=0;
23423 else $out="";
23424 if(!$this->_passive) {
23425 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
23426 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
23427 if($this->_ftp_temp_sock===FALSE) {
23428 $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
23429 $this->_data_close();
23430 return FALSE;
23431 }
23432 }
23433
23434 while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
23435 if($block==="") break;
23436 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
23437 if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
23438 else $out.=$block;
23439 }
23440 return $out;
23441 }
23442
23443 function _data_write($mode=FTP_ASCII, $fp=NULL) {
23444 $NewLine=$this->_eol_code[$this->OS_local];
23445 if(is_resource($fp)) $out=0;
23446 else $out="";
23447 if(!$this->_passive) {
23448 $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
23449 $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
23450 if($this->_ftp_temp_sock===FALSE) {
23451 $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
23452 $this->_data_close();
23453 return false;
23454 }
23455 }
23456 if(is_resource($fp)) {
23457 while(!feof($fp)) {
23458 $block=fread($fp, $this->_ftp_buff_size);
23459 if(!$this->_data_write_block($mode, $block)) return false;
23460 }
23461 } elseif(!$this->_data_write_block($mode, $fp)) return false;
23462 return true;
23463 }
23464
23465 function _data_write_block($mode, $block) {
23466 if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
23467 do {
23468 if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
23469 $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
23470 $this->_data_close();
23471 return FALSE;
23472 }
23473 $block=substr($block, $t);
23474 } while(!empty($block));
23475 return true;
23476 }
23477
23478 function _data_close() {
23479 @socket_close($this->_ftp_temp_sock);
23480 @socket_close($this->_ftp_data_sock);
23481 $this->SendMSG("Disconnected data from remote host");
23482 return TRUE;
23483 }
23484
23485 function _quit() {
23486 if($this->_connected) {
23487 @socket_close($this->_ftp_control_sock);
23488 $this->_connected=false;
23489 $this->SendMSG("Socket closed");
23490 }
23491 }
23492 }
23493 ?>