dconnection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. //The following class was created June 30th 2004 by Jeremy(pickle)
  3. //This class is designed to handle a direct connection
  4. class Dconnect
  5. {
  6. var $sock;
  7. var $lastReceived;
  8. var $lastMessage;
  9. var $connected;
  10. var $cookie;
  11. var $type=2;
  12. var $connectedTo;
  13. function Dconnect($ip,$port)
  14. {
  15. if(!$this->connect($ip,$port))
  16. {
  17. sEcho("Connection failed constructor");
  18. $this->connected=false;
  19. }
  20. else
  21. $this->connected=true;
  22. $this->lastMessage="";
  23. $this->lastReceived="";
  24. }
  25. function readDIM()
  26. {
  27. /*
  28. if(!$this->stuffToRead())
  29. {
  30. sEcho("Nothing to read");
  31. $this->lastMessage=$this->lastReceived="";
  32. return false;
  33. }
  34. */
  35. $head=fread($this->sock,6);
  36. if(strlen($head)<=0)
  37. {
  38. sEcho("The direct connection has been closed");
  39. return false;
  40. }
  41. $minihead=unpack("a4ver/nsize",$head);
  42. if($minihead['size'] <=0)
  43. return;
  44. $headerinfo=unpack("nchan/nsix/nzero/a6cookie/Npt1/Npt2/npt3/Nlen/Npt/npt0/ntype/Nzerom/a*sn",fread($this->sock,($minihead['size']-6)));
  45. $allheader=array_merge($minihead,$headerinfo);
  46. sEcho($allheader);
  47. if($allheader['len']>0 && $allheader['len'] <= MAX_DIM_SIZE)
  48. {
  49. $left=$allheader['len'];
  50. $stuff="";
  51. $nonin=0;
  52. while(strlen($stuff) < $allheader['len'] && $nonin<3)
  53. {
  54. $stuffg=fread($this->sock,$left);
  55. if(strlen($stuffg)<0)
  56. {
  57. $nonin++;
  58. continue;
  59. }
  60. $left=$left - strlen($stuffg);
  61. $stuff.=$stuffg;
  62. }
  63. $data=unpack("a*decoded",$stuff);
  64. }
  65. else if($allheader['len'] > MAX_DIM_SIZE)
  66. {
  67. $data['decoded']="too big";
  68. }
  69. else
  70. $data['decoded']="";
  71. $all=array_merge($allheader,$data);
  72. $this->lastReceived=$all;
  73. $this->lastMessage=$all['decoded'];
  74. //$function=$this->DimInf . "(\$all);";
  75. //eval($function);
  76. return $all;
  77. }
  78. function sendMessage($message,$sn)
  79. {
  80. //Make the "mini header"
  81. $minihead=pack("a4n","ODC2",76);
  82. $header=pack("nnna6NNnNNnnNa*",1,6,0,$this->cookie,0,0,0,strlen($message),0,0,96,0,$sn);
  83. $bighead=$minihead . $header;
  84. while(strlen($bighead)<76)
  85. $bighead.=pack("c",0);
  86. $tosend=$bighead . pack("a*",$message);
  87. $w=array($this->sock);
  88. stream_select($r=NULL,$w,$e=NULL,NULL);
  89. //Now send it all
  90. fputs($this->sock,$tosend,strlen($tosend));
  91. }
  92. function stuffToRead()
  93. {
  94. //$info=stream_get_meta_data($this->sock);
  95. //sEcho($info);
  96. $s=array($this->sock);
  97. $changed=stream_select($s,$fds=NULL,$m=NULL,0,20000);
  98. return ($changed>0);
  99. }
  100. function close()
  101. {
  102. $this->connected=false;
  103. return fclose($this->sock);
  104. }
  105. function connect($ip,$port)
  106. {
  107. $this->sock=fsockopen($ip,$port,$en,$es,3);
  108. if(!$this->sock)
  109. { sEcho("Connection failed");
  110. $this->sock=null;
  111. return false;
  112. }
  113. return true;
  114. }
  115. }
  116. class FileSendConnect
  117. {
  118. var $sock;
  119. var $lastReceived;
  120. var $lastMessage;
  121. var $connected;
  122. var $cookie;
  123. var $tpye=3;
  124. function FileSendConnect($ip,$port)
  125. {
  126. if(!$this->connect($ip,$port))
  127. {
  128. sEcho("Connection failed constructor");
  129. $this->connected=false;
  130. }
  131. else
  132. $this->connected=true;
  133. $this->lastMessage="";
  134. $this->lastReceived="";
  135. }
  136. function readDIM()
  137. {
  138. if(!$this->stuffToRead())
  139. {
  140. sEcho("Nothing to read");
  141. $this->lastMessage=$this->lastReceived="";
  142. return;
  143. }
  144. $minihead=unpack("a4ver/nsize",fread($this->sock,6));
  145. if($minihead['size'] <=0)
  146. return;
  147. $headerinfo=unpack("nchan/nsix/nzero/a6cookie/Npt1/Npt2/npt3/Nlen/Npt/npt0/ntype/Nzerom/a*sn",fread($this->sock,($minihead['size']-6)));
  148. $allheader=array_merge($minihead,$headerinfo);
  149. sEcho($allheader);
  150. if($allheader['len']>0)
  151. $data=unpack("a*decoded",fread($this->sock,$allheader['len']));
  152. else
  153. $data['decoded']="";
  154. $all=array_merge($allheader,$data);
  155. $this->lastReceived=$all;
  156. $this->lastMessage=$all['decoded'];
  157. //$function=$this->DimInf . "(\$all);";
  158. //eval($function);
  159. return $all;
  160. }
  161. function sendMessage($message,$sn)
  162. {
  163. //Make the "mini header"
  164. $minihead=pack("a4n","ODC2",76);
  165. $header=pack("nnna6NNnNNnnNa*",1,6,0,$this->cookie,0,0,0,strlen($message),0,0,96,0,$sn);
  166. $bighead=$minihead . $header;
  167. while(strlen($bighead)<76)
  168. $bighead.=pack("c",0);
  169. $tosend=$bighead . pack("a*",$message);
  170. //Now send it all
  171. fwrite($this->sock,$tosend,strlen($tosend));
  172. }
  173. function stuffToRead()
  174. {
  175. //$info=stream_get_meta_data($this->sock);
  176. //sEcho($info);
  177. $s=array($this->sock);
  178. $changed=stream_select($s,$fds=NULL,$m=NULL,1);
  179. return ($changed>0);
  180. }
  181. function close()
  182. {
  183. $this->connected=false;
  184. fclose($this->sock);
  185. unset($this->sock);
  186. return true;
  187. }
  188. function connect($ip,$port)
  189. {
  190. $this->sock=fsockopen($ip,$port,$en,$es,3);
  191. if(!$this->sock)
  192. { sEcho("Connection failed to" . $ip . ":" . $port);
  193. $this->sock=null;
  194. return false;
  195. }
  196. return true;
  197. }
  198. }