You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

class-ftp-pure.php 5.3 KiB

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