PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/floppyxyz/musical
PHP | 250 lines | 207 code | 15 blank | 28 comment | 43 complexity | 983e77ea48e8a21676334295e75e1e2f 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. * Socket Based FTP implementation
  16. *
  17. * @package PemFTP
  18. * @subpackage Socket
  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(true, $verb, $le);
  33. }
  34. // <!-- --------------------------------------------------------------------------------------- -->
  35. // <!-- Private functions -->
  36. // <!-- --------------------------------------------------------------------------------------- -->
  37. function _settimeout($sock) {
  38. if(!@socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
  39. $this->PushError('_connect','socket set receive timeout',socket_strerror(socket_last_error($sock)));
  40. @socket_close($sock);
  41. return FALSE;
  42. }
  43. if(!@socket_set_option($sock, SOL_SOCKET , SO_SNDTIMEO, array("sec"=>$this->_timeout, "usec"=>0))) {
  44. $this->PushError('_connect','socket set send timeout',socket_strerror(socket_last_error($sock)));
  45. @socket_close($sock);
  46. return FALSE;
  47. }
  48. return true;
  49. }
  50. function _connect($host, $port) {
  51. $this->SendMSG("Creating socket");
  52. if(!($sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
  53. $this->PushError('_connect','socket create failed',socket_strerror(socket_last_error($sock)));
  54. return FALSE;
  55. }
  56. if(!$this->_settimeout($sock)) return FALSE;
  57. $this->SendMSG("Connecting to \"".$host.":".$port."\"");
  58. if (!($res = @socket_connect($sock, $host, $port))) {
  59. $this->PushError('_connect','socket connect failed',socket_strerror(socket_last_error($sock)));
  60. @socket_close($sock);
  61. return FALSE;
  62. }
  63. $this->_connected=true;
  64. return $sock;
  65. }
  66. function _readmsg($fnction="_readmsg"){
  67. if(!$this->_connected) {
  68. $this->PushError($fnction,'Connect first');
  69. return FALSE;
  70. }
  71. $result=true;
  72. $this->_message="";
  73. $this->_code=0;
  74. $go=true;
  75. do {
  76. $tmp=@socket_read($this->_ftp_control_sock, 4096, PHP_BINARY_READ);
  77. if($tmp===false) {
  78. $go=$result=false;
  79. $this->PushError($fnction,'Read failed', socket_strerror(socket_last_error($this->_ftp_control_sock)));
  80. } else {
  81. $this->_message.=$tmp;
  82. $go = !preg_match("/^([0-9]{3})(-.+\\1)? [^".CRLF."]+".CRLF."$/Us", $this->_message, $regs);
  83. }
  84. } while($go);
  85. if($this->LocalEcho) echo "GET < ".rtrim($this->_message, CRLF).CRLF;
  86. $this->_code=(int)$regs[1];
  87. return $result;
  88. }
  89. function _exec($cmd, $fnction="_exec") {
  90. if(!$this->_ready) {
  91. $this->PushError($fnction,'Connect first');
  92. return FALSE;
  93. }
  94. if($this->LocalEcho) echo "PUT > ",$cmd,CRLF;
  95. $status=@socket_write($this->_ftp_control_sock, $cmd.CRLF);
  96. if($status===false) {
  97. $this->PushError($fnction,'socket write failed', socket_strerror(socket_last_error($this->stream)));
  98. return FALSE;
  99. }
  100. $this->_lastaction=time();
  101. if(!$this->_readmsg($fnction)) return FALSE;
  102. return TRUE;
  103. }
  104. function _data_prepare($mode=FTP_ASCII) {
  105. if(!$this->_settype($mode)) return FALSE;
  106. $this->SendMSG("Creating data socket");
  107. $this->_ftp_data_sock = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  108. if ($this->_ftp_data_sock < 0) {
  109. $this->PushError('_data_prepare','socket create failed',socket_strerror(socket_last_error($this->_ftp_data_sock)));
  110. return FALSE;
  111. }
  112. if(!$this->_settimeout($this->_ftp_data_sock)) {
  113. $this->_data_close();
  114. return FALSE;
  115. }
  116. if($this->_passive) {
  117. if(!$this->_exec("PASV", "pasv")) {
  118. $this->_data_close();
  119. return FALSE;
  120. }
  121. if(!$this->_checkCode()) {
  122. $this->_data_close();
  123. return FALSE;
  124. }
  125. $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));
  126. $this->_datahost=$ip_port[0].".".$ip_port[1].".".$ip_port[2].".".$ip_port[3];
  127. $this->_dataport=(((int)$ip_port[4])<<8) + ((int)$ip_port[5]);
  128. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
  129. if(!@socket_connect($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
  130. $this->PushError("_data_prepare","socket_connect", socket_strerror(socket_last_error($this->_ftp_data_sock)));
  131. $this->_data_close();
  132. return FALSE;
  133. }
  134. else $this->_ftp_temp_sock=$this->_ftp_data_sock;
  135. } else {
  136. if(!@socket_getsockname($this->_ftp_control_sock, $addr, $port)) {
  137. $this->PushError("_data_prepare","can't get control socket information", socket_strerror(socket_last_error($this->_ftp_control_sock)));
  138. $this->_data_close();
  139. return FALSE;
  140. }
  141. if(!@socket_bind($this->_ftp_data_sock,$addr)){
  142. $this->PushError("_data_prepare","can't bind data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
  143. $this->_data_close();
  144. return FALSE;
  145. }
  146. if(!@socket_listen($this->_ftp_data_sock)) {
  147. $this->PushError("_data_prepare","can't listen data socket", socket_strerror(socket_last_error($this->_ftp_data_sock)));
  148. $this->_data_close();
  149. return FALSE;
  150. }
  151. if(!@socket_getsockname($this->_ftp_data_sock, $this->_datahost, $this->_dataport)) {
  152. $this->PushError("_data_prepare","can't get data socket information", socket_strerror(socket_last_error($this->_ftp_data_sock)));
  153. $this->_data_close();
  154. return FALSE;
  155. }
  156. if(!$this->_exec('PORT '.str_replace('.',',',$this->_datahost.'.'.($this->_dataport>>8).'.'.($this->_dataport&0x00FF)), "_port")) {
  157. $this->_data_close();
  158. return FALSE;
  159. }
  160. if(!$this->_checkCode()) {
  161. $this->_data_close();
  162. return FALSE;
  163. }
  164. }
  165. return TRUE;
  166. }
  167. function _data_read($mode=FTP_ASCII, $fp=NULL) {
  168. $NewLine=$this->_eol_code[$this->OS_local];
  169. if(is_resource($fp)) $out=0;
  170. else $out="";
  171. if(!$this->_passive) {
  172. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
  173. $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
  174. if($this->_ftp_temp_sock===FALSE) {
  175. $this->PushError("_data_read","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
  176. $this->_data_close();
  177. return FALSE;
  178. }
  179. }
  180. while(($block=@socket_read($this->_ftp_temp_sock, $this->_ftp_buff_size, PHP_BINARY_READ))!==false) {
  181. if($block==="") break;
  182. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_local], $block);
  183. if(is_resource($fp)) $out+=fwrite($fp, $block, strlen($block));
  184. else $out.=$block;
  185. }
  186. return $out;
  187. }
  188. function _data_write($mode=FTP_ASCII, $fp=NULL) {
  189. $NewLine=$this->_eol_code[$this->OS_local];
  190. if(is_resource($fp)) $out=0;
  191. else $out="";
  192. if(!$this->_passive) {
  193. $this->SendMSG("Connecting to ".$this->_datahost.":".$this->_dataport);
  194. $this->_ftp_temp_sock=socket_accept($this->_ftp_data_sock);
  195. if($this->_ftp_temp_sock===FALSE) {
  196. $this->PushError("_data_write","socket_accept", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
  197. $this->_data_close();
  198. return false;
  199. }
  200. }
  201. if(is_resource($fp)) {
  202. while(!feof($fp)) {
  203. $block=fread($fp, $this->_ftp_buff_size);
  204. if(!$this->_data_write_block($mode, $block)) return false;
  205. }
  206. } elseif(!$this->_data_write_block($mode, $fp)) return false;
  207. return true;
  208. }
  209. function _data_write_block($mode, $block) {
  210. if($mode!=FTP_BINARY) $block=preg_replace("/\r\n|\r|\n/", $this->_eol_code[$this->OS_remote], $block);
  211. do {
  212. if(($t=@socket_write($this->_ftp_temp_sock, $block))===FALSE) {
  213. $this->PushError("_data_write","socket_write", socket_strerror(socket_last_error($this->_ftp_temp_sock)));
  214. $this->_data_close();
  215. return FALSE;
  216. }
  217. $block=substr($block, $t);
  218. } while(!empty($block));
  219. return true;
  220. }
  221. function _data_close() {
  222. @socket_close($this->_ftp_temp_sock);
  223. @socket_close($this->_ftp_data_sock);
  224. $this->SendMSG("Disconnected data from remote host");
  225. return TRUE;
  226. }
  227. function _quit() {
  228. if($this->_connected) {
  229. @socket_close($this->_ftp_control_sock);
  230. $this->_connected=false;
  231. $this->SendMSG("Socket closed");
  232. }
  233. }
  234. }
  235. ?>