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