PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/public/frontend/wp-admin/includes/class-ftp-pure.php

https://bitbucket.org/floppyxyz/musical
PHP | 190 lines | 150 code | 12 blank | 28 comment | 32 complexity | ecc03dc71a4ecbaa30831d6375c6e15c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * PemFTP - A Ftp implementation in pure PHP
  4. *
  5. * @package PemFTP
  6. * @since 2.5
  7. *
  8. * @version 1.0
  9. * @copyright Alexey Dotsenko
  10. * @author Alexey Dotsenko
  11. * @link http://www.phpclasses.org/browse/package/1743.html Site
  12. * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
  13. */
  14. /**
  15. * FTP implementation using fsockopen to connect.
  16. *
  17. * @package PemFTP
  18. * @subpackage Pure
  19. * @since 2.5
  20. *
  21. * @version 1.0
  22. * @copyright Alexey Dotsenko
  23. * @author Alexey Dotsenko
  24. * @link http://www.phpclasses.org/browse/package/1743.html Site
  25. * @license LGPL License http://www.opensource.org/licenses/lgpl-license.html
  26. */
  27. class ftp extends ftp_base {
  28. function ftp($verb=FALSE, $le=FALSE) {
  29. $this->__construct($verb, $le);
  30. }
  31. function __construct($verb=FALSE, $le=FALSE) {
  32. parent::__construct(false, $verb, $le);
  33. }
  34. // <!-- --------------------------------------------------------------------------------------- -->
  35. // <!-- Private functions -->
  36. // <!-- --------------------------------------------------------------------------------------- -->
  37. function _settimeout($sock) {
  38. if(!@stream_set_timeout($sock, $this->_timeout)) {
  39. $this->PushError('_settimeout','socket set send timeout');
  40. $this->_quit();
  41. return FALSE;
  42. }
  43. return TRUE;
  44. }
  45. function _connect($host, $port) {
  46. $this->SendMSG("Creating socket");
  47. $sock = @fsockopen($host, $port, $errno, $errstr, $this->_timeout);
  48. if (!$sock) {
  49. $this->PushError('_connect','socket connect failed', $errstr." (".$errno.")");
  50. return FALSE;
  51. }
  52. $this->_connected=true;
  53. return $sock;
  54. }
  55. function _readmsg($fnction="_readmsg"){
  56. if(!$this->_connected) {
  57. $this->PushError($fnction, 'Connect first');
  58. return FALSE;
  59. }
  60. $result=true;
  61. $this->_message="";
  62. $this->_code=0;
  63. $go=true;
  64. do {
  65. $tmp=@fgets($this->_ftp_control_sock, 512);
  66. if($tmp===false) {
  67. $go=$result=false;
  68. $this->PushError($fnction,'Read failed');
  69. } else {
  70. $this->_message.=$tmp;
  71. if(preg_match("/^([0-9]{3})(-(.*[".CRLF."]{1,2})+\\1)? [^".CRLF."]+[".CRLF."]{1,2}$/", $this->_message, $regs)) $go=false;
  72. }
  73. } while($go);
  74. if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
  75. $this->_code=(int)$regs[1];
  76. return $result;
  77. }
  78. function _exec($cmd, $fnction="_exec") {
  79. if(!$this->_ready) {
  80. $this->PushError($fnction,'Connect first');
  81. return FALSE;
  82. }
  83. if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
  84. $status=@fputs($this->_ftp_control_sock, $cmd.CRLF);
  85. if($status===false) {
  86. $this->PushError($fnction,'socket write failed');
  87. return FALSE;
  88. }
  89. $this->_lastaction=time();
  90. if(!$this->_readmsg($fnction)) return FALSE;
  91. return TRUE;
  92. }
  93. function _data_prepare($mode=FTP_ASCII) {
  94. if(!$this->_settype($mode)) return FALSE;
  95. if($this->_passive) {
  96. if(!$this->_exec("PASV", "pasv")) {
  97. $this->_data_close();
  98. return FALSE;
  99. }
  100. if(!$this->_checkCode()) {
  101. $this->_data_close();
  102. return FALSE;
  103. }
  104. $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));
  105. $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
  106. $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
  107. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
  108. $this->_ftp_data_sock=@fsockopen($this->_datahost, $this->_dataport, $errno, $errstr, $this->_timeout);
  109. if(!$this->_ftp_data_sock) {
  110. $this->PushError("_data_prepare","fsockopen fails", $errstr." (".$errno.")");
  111. $this->_data_close();
  112. return FALSE;
  113. }
  114. else $this->_ftp_data_sock;
  115. } else {
  116. $this->SendMSG("Only passive connections available!");
  117. return FALSE;
  118. }
  119. return TRUE;
  120. }
  121. function _data_read($mode=FTP_ASCII, $fp=NULL) {
  122. if(is_resource($fp)) $out=0;
  123. else $out="";
  124. if(!$this->_passive) {
  125. $this->SendMSG("Only passive connections available!");
  126. return FALSE;
  127. }
  128. while (!feof($this->_ftp_data_sock)) {
  129. $block=fread($this->_ftp_data_sock, $this->_ftp_buff_size);
  130. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
  131. if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
  132. else $out.=$block;
  133. }
  134. return $out;
  135. }
  136. function _data_write($mode=FTP_ASCII, $fp=NULL) {
  137. if(is_resource($fp)) $out=0;
  138. else $out="";
  139. if(!$this->_passive) {
  140. $this->SendMSG("Only passive connections available!");
  141. return FALSE;
  142. }
  143. if(is_resource($fp)) {
  144. while(!feof($fp)) {
  145. $block=fread($fp, $this->_ftp_buff_size);
  146. if(!$this->_data_write_block($mode, $block)) return false;
  147. }
  148. } elseif(!$this->_data_write_block($mode, $fp)) return false;
  149. return TRUE;
  150. }
  151. function _data_write_block($mode, $block) {
  152. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
  153. do {
  154. if(($t=@fwrite($this->_ftp_data_sock, $block))===FALSE) {
  155. $this->PushError("_data_write","Can't write to socket");
  156. return FALSE;
  157. }
  158. $block=substr($block, $t);
  159. } while(!empty($block));
  160. return true;
  161. }
  162. function _data_close() {
  163. @fclose($this->_ftp_data_sock);
  164. $this->SendMSG("Disconnected data from remote host");
  165. return TRUE;
  166. }
  167. function _quit($force=FALSE) {
  168. if($this->_connected or $force) {
  169. @fclose($this->_ftp_control_sock);
  170. $this->_connected=false;
  171. $this->SendMSG("Socket closed");
  172. }
  173. }
  174. }
  175. ?>