source: trunk/instant_messenger/bkp/instant_messenger_64/IMManager.php @ 151

Revision 151, 8.9 KB checked in by niltonneto, 16 years ago (diff)

Commit da nova versão do módulo, usando agente em C.
Vide Página do módulo do Trac:
http://www.expressolivre.org/dev/wiki/messenger

A versão anterior encontra-se na subpasta bkp (32/64).

Line 
1#!/usr/bin/php -Cq
2<?php
3
4   require_once('inc/IMConfigure.php');
5        require_once('inc/Jabberd2.abstract.php');
6
7        ## limit to indefinite execution
8        set_time_limit (0);
9   
10   ###
11   # definitions of socket - BEGIN
12   #
13      define('URI', '0.0.0.0');
14      define('TARGET_NON_SECURITY', TRANSPORT_NON_SECURITY . '://' . URI  . ':' . PORT_NON_SECURITY);
15      define('SOCKET_NON_SECURITY', socket_create(AF_INET, SOCK_STREAM, SOL_TCP));
16   #
17   # definitions of socket - END
18   ###
19      // create a streaming socket, of type TCP/IP
20      // set the option to reuse the port
21      socket_set_option(SOCKET_NON_SECURITY, SOL_SOCKET, SO_REUSEADDR, 1);
22
23      // "bind" the socket to the address to "localhost", on port $port
24      // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc..
25      socket_bind(SOCKET_NON_SECURITY, 0, 8000);
26
27      // start listen for connections
28      socket_listen(SOCKET_NON_SECURITY);
29
30   if ( !SOCKET_NON_SECURITY )
31   {
32      printf("NON SECURITY:\n(%d) %s", $errno_non_security, $errstr_non_security);
33      exit(0);
34   }
35
36   $master = array(SOCKET_NON_SECURITY);
37   $users = array();
38   $send = false;
39
40   while ( true )
41   {
42      $read = $master;
43      $_w = NULL;
44      $_e = NULL;
45      //$mod_fd = @stream_select($read, $_w , $_e , READ_SOCKET_AFTER_OF );
46      $mod_fd = socket_select($read, $_w = NULL, $_e = NULL, READ_SOCKET_AFTER_OF);
47
48      if ( $mod_fd === FALSE )
49      {
50         break;
51      }
52
53      for ( $i = 0; $i < $mod_fd; ++$i )
54      {
55         if ( $read[$i] === SOCKET_NON_SECURITY )
56         {
57            $conn_non_security = socket_accept(SOCKET_NON_SECURITY);
58            $master[] = &$conn_non_security;
59         }
60         else
61         {
62            $sock_data = socket_read($read[$i], 1024);
63            if ( strlen($sock_data) === 0 )
64            {
65               $key_to_del = array_search($read[$i], $master, TRUE);
66               socket_close($read[$i]);
67               unset($master[$key_to_del]);
68            }
69            else if ( $sock_data === FALSE )
70            {
71               $key_to_del = array_search($read[$i], $master, TRUE);
72               unset($master[$key_to_del]);
73            }
74            else
75            {
76               $headers = (split("\n", $sock_data));
77
78               preg_match('/^(GET|POST) \/(.+) HTTP\/1\.\d/U', $headers[0], $request);
79               preg_match('/^Host: \w*:(.+)/', $headers[1], $host);
80
81               $conn = &$conn_non_security;
82
83               $request = explode(';', $request[2]);
84
85               if ( $request[0] == CONNECT && isset($request[1]) && !isset($users[$request[1]]) )
86               {
87                  connect($users, $request[1]);
88               }
89
90               if ( $request[0] == CLOSE && isset($request[1]) && isset($users[$request[1]]) )
91               {   
92                close($request[1]);
93               }
94
95               socket_close($read[$i]);
96               unset($master[array_search($read[$i], $master)]);
97            }
98         }
99      }
100     
101      foreach ( $users as $key => $val )
102      {
103                $time_now = time();
104                if ( ( $time_now - $users[$key]['timeClient'] ) < DISCONNECT_AFTERWARDS_OF )         
105         {
106                readSocket($users, $key);
107         }
108         else
109         {
110                                if((($time_now - $users[$key]['timeClient']) / DISCONNECT_AFTERWARDS_OF) > 2 )
111                                {
112                                $users[$key]['jabber']->disconnect();
113                                unset($users[$key]);                                   
114                                }
115                                else
116                                {
117                                        readSocket($users, $key);
118                                }               
119         }
120      }
121   }
122       
123   exit(0);
124
125   function connect(&$pUsers, $pSession)
126   {
127        global $users;
128                     
129      $sessionpath = explode(';', session_save_path());
130      foreach ( $sessionpath as $path )
131      {
132         $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/';
133
134         if ( file_exists($path . 'sess_' . $pSession) )
135         {
136            session_id($pSession);
137
138            ob_start();
139            session_start();
140            ob_clean();
141            ob_end_flush();
142
143            $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']);
144            if ( $is_user )
145            {
146               $_user     = $_SESSION['phpgw_info']['instant_messenger']['user'];
147               $_pass     = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
148               $_server   = $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
149               $_port     = $_SESSION['phpgw_info']['instant_messenger']['port_jabber'];
150               $_resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber'];
151               $_time     = time();
152               $_SESSION['phpgw_info']['instant_messenger']['timeClient'] = $_time;
153                                        $_SESSION['phpgw_info']['instant_messenger']['timeServer'] = $_time;               
154               $_SESSION['phpgw_info']['instant_messenger']['socket'] = NULL;
155               $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] = NULL;
156               $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] = array();
157               $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL;
158               $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = false;
159
160               session_write_close();
161
162               $a = new Jabberd2;
163               if ( ($a->connect($_user . '@' . $_server . '/' . $_resource . ':' . $_port, $_pass)) )
164               {
165                  $pUsers[$pSession] = array();
166                  $pUsers[$pSession]['jabber'] = $a;
167                  $pUsers[$pSession]['timeClient'] = $_time;
168               }
169               else
170               {
171                  unset($a);
172               }
173               break;
174            }
175            session_write_close();
176         }
177      }
178   }
179
180   function close($pUser)
181   {
182      global $users;
183      $users[$pUser]['jabber']->disconnect();
184      unset($users[$pUser]);
185   }
186
187   function readSocket(&$pUsers, $pSession)
188   {
189      global $send;
190      $sessionpath = explode(';', session_save_path());
191      foreach ( $sessionpath as $path )
192      {
193         $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/';
194
195         if ( file_exists($path . 'sess_' . $pSession) )
196         {
197            session_id($pSession);
198
199            ob_start();
200            session_start();
201            ob_clean();
202            ob_end_flush();
203
204            $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']);
205            if ( $is_user )
206            {
207               $pUsers[$pSession]['timeClient'] = $_SESSION['phpgw_info']['instant_messenger']['timeClient'];
208               $_SESSION['phpgw_info']['instant_messenger']['timeServer'] = time();
209               
210                                        $readSocket = @$pUsers[$pSession]['jabber']->readSocket();
211               
212               $connection_close_timeout = strpos($readSocket, "<stream:error");
213               $connection_close_server = strpos($readSocket,"</stream:stream>");
214               
215               if ( ($readSocket !== false) && ($connection_close_timeout === false ) && ($connection_close_server === false ))
216               {
217                  $_SESSION['phpgw_info']['instant_messenger']['IM_disconnect'] = false;
218                  if ( !empty($readSocket) )
219                  {
220                     $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] .= $readSocket;
221                  }
222
223                                                if ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] )
224                                                        foreach ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] as $action => $value )
225                                                        {
226                                                                if ( $action === 'message' )
227                                                                {
228                                                                        if ( !is_null($value) )
229                                                                        {
230                                                                                $pUsers[$pSession]['jabber']->writeSocket($value);
231                                                                                $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL;
232                                                                        }
233                                                                }
234                                                                else if ( $action === 'vcard' )
235                                                                {
236                                                                        if ( !empty($value) )
237                                                                        {
238                                                                                foreach ( $value as $key => $val )
239                                                                                {
240                                                                                        $pUsers[$pSession]['jabber']->getVcard($val);
241                                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][$key]);
242                                                                                }
243                                                                        }
244                                                                }
245                                                                else
246                                                                {
247                                                                        if ( method_exists($pUsers[$pSession]['jabber'], $action) )
248                                                                                if ( $value )
249                                                                                        $pUsers[$pSession]['jabber']->$action($value);
250                                                                                else
251                                                                                        $pUsers[$pSession]['jabber']->$action();
252                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out'][$action]);
253                                                                }
254                                                        }
255               }
256               else
257               {
258                $_SESSION['phpgw_info']['instant_messenger']['IM_disconnect'] = true;
259                close($pSession);                       
260               }
261               break;
262            }
263            else
264            {
265               close($pSession);
266            }
267            break;
268         }
269      }
270      session_write_close();
271   }
272?>
Note: See TracBrowser for help on using the repository browser.