source: trunk/instant_messenger/IMManager.php @ 55

Revision 55, 10.2 KB checked in by niltonneto, 17 years ago (diff)

Nova versão do Instant Messenger.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1#!/usr/bin/php -Cq
2<?php
3   //error_reporting(0);
4   //ini_set( 'track_errors', '1' );
5   ###
6   # This file is prepared for receive dependable connections but
7   # exists a BUG in the PHP related with the library OpenSSL that
8   # doesn't permit the enable crypto in connection created.
9   #
10   # See [Bug #40993 stream_socket_accept() : accept failed] in
11   # http://bugs.php.net/bug.php?id=40993&edit=1
12   #
13   #
14   ###
15   require_once('inc/IMConfigure.php');
16
17   ###
18   # definitions of socket - BEGIN
19   #
20      /***
21       * This file is prepared for receive dependable connections but
22       * exists a BUG in the PHP related with the library OpenSSL that
23       * doesn't permit the enable crypto in connection created.
24       *
25       * See [Bug #40993 stream_socket_accept() : accept failed] in
26       * http://bugs.php.net/bug.php?id=40993&edit=1
27       *
28       *
29       * It's necessary to remove all the comments made with '//'
30       * for safe connection
31       *
32       *
33       * WARNING - DANGER:
34       *
35       * Before remove all the comments made with '//' for safe
36       * connection, it's necessary to remove all the comments
37       * made with '//' in the file IMConfigure.php
38       * If this will not be made will happen many ERRORS
39       ***/
40
41      //define("CONTEXT", stream_context_create());
42
43      define('URI', '0.0.0.0');
44
45      define('TARGET_NON_SECURITY', TRANSPORT_NON_SECURITY . '://' . URI  . ':' . PORT_NON_SECURITY);
46      //define('TARGET_SECURITY', TRANSPORT_SECURITY . '://' . URI  . ':' . PORT_SECURITY);
47
48      //stream_context_set_option(CONTEXT, 'ssl', 'local_cert', './apache.pem'); # local_cert must be in PEM format
49      //stream_context_set_option(CONTEXT, 'ssl', 'allow_self_signed', true);
50      //stream_context_set_option(CONTEXT, 'ssl', 'verify_peer', false);
51
52      define('SOCKET_NON_SECURITY', stream_socket_server(TARGET_NON_SECURITY, $errno_non_security, $errstr_non_security, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN));
53      //define('SOCKET_SECURITY', stream_socket_server(TARGET_SECURITY, $errno_security, $errstr_security, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, CONTEXT));
54   #
55   # definitions of socket - END
56   ###
57
58   //if ( !SOCKET_NON_SECURITY || !SOCKET_SECURITY ) # It's necessary to remove the next line for safe connection
59   if ( !SOCKET_NON_SECURITY )
60   {
61      printf("NON SECURITY:\n(%d) %s", $errno_non_security, $errstr_non_security);
62      //printf("SECURITY:\n(%d) %s", $errno_security, $errstr_security);
63      exit(0);
64   }
65
66   include 'inc/Jabberd2.abstract.php';
67
68   //$master = array(SOCKET_NON_SECURITY, SOCKET_SECURITY); # It's necessary to remove the next line for safe connection
69   $master = array(SOCKET_NON_SECURITY);
70
71   $users = array();
72
73   $send = false;
74
75   while ( 1 )
76   {
77      $read = $master;
78      $_w = NULL;
79      $_e = NULL;
80       
81      $mod_fd = @stream_select($read, $_w , $_e , READ_SOCKET_AFTER_OF);
82
83      if ( $mod_fd === FALSE )
84         break;
85
86      for ( $i = 0; $i < $mod_fd; ++$i )
87         if ( $read[$i] === SOCKET_NON_SECURITY )
88         {
89            $conn_non_security = stream_socket_accept(SOCKET_NON_SECURITY);
90            # fwrite($conn_non_security, "Hello! The time is " . date("n/j/Y g:i:s a"));
91            $master[] = &$conn_non_security;
92         }
93         //else if ( $read[$i] === SOCKET_SECURITY )
94         //{
95         //   $conn_security = stream_socket_accept(SOCKET_SECURITY);
96         //   fwrite($conn_security, "Hello! The time is " . date("n/j/Y g:i:s a")."\n");
97         //   $master[] = &$conn_security;
98         //}
99         else
100         {
101            $sock_data = fread($read[$i], 1024);
102            if ( strlen($sock_data) === 0 )
103            {
104               printf("Connection closed");
105               $key_to_del = array_search($read[$i], $master, TRUE);
106               fclose($read[$i]);
107               unset($master[$key_to_del]);
108            }
109            else if ( $sock_data === FALSE )
110            {
111               printf("Something bad happened");
112               $key_to_del = array_search($read[$i], $master, TRUE);
113               unset($master[$key_to_del]);
114            }
115            else
116            {
117               # printf("\n\n%s\n\n", $sock_data);
118               $headers = (split("\n", $sock_data));
119
120               preg_match('/^(GET|POST) \/(.+) HTTP\/1\.1/U', $headers[0], $request);
121               preg_match('/^Host: \w*:(.+)/', $headers[1], $host);
122
123               //if ( (int)$host[1] == PORT_NON_SECURITY )
124                  $conn = &$conn_non_security;
125               //else
126               //   $conn = &$conn_security;
127
128               $request = explode(';', $request[2]);
129
130               if ( $request[0] == CONNECT && isset($request[1]) && !isset($users[$request[1]]) )
131                  connect($users, $request[1]);
132
133               if ( $request[0] == CLOSE && isset($request[1]) && isset($users[$request[1]]) )
134                  close($request[1]);
135
136               fclose($read[$i]);
137               unset($master[array_search($read[$i], $master)]);
138            }
139         }
140
141      foreach ( $users as $key => $val )
142      {
143         if ( (time() - $users[$key]['time'] > DISCONNECT_AFTERWARDS_OF) )
144            if ( $users[$key]['attempt'] > 2 )
145            {
146               $users[$key]['attempt'] = 0;
147               close($key);
148            }
149            else
150            {
151               $users[$key]['attempt']++;
152               readSocket($users, $key);
153            }
154         else
155         {
156            $users[$key]['attempt'] = 0;
157            readSocket($users, $key);
158         }
159      }
160
161      //print_r($users);
162   }
163
164   exit(0);
165
166   function connect(&$pUsers, $pSession)
167   {
168      $sessionpath = explode(';', session_save_path());
169      foreach ( $sessionpath as $path )
170      {
171         $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/';
172
173         if ( file_exists($path . 'sess_' . $pSession) )
174         {
175            session_id($pSession);
176
177            ob_start();
178            session_start();
179            ob_clean();
180            ob_end_flush();
181
182            $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']);
183            if ( $is_user )
184            {
185               $_user     = $_SESSION['phpgw_info']['instant_messenger']['user'];
186               $_pass     = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
187               $_server   = $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
188               $_port     = $_SESSION['phpgw_info']['instant_messenger']['port_jabber'];
189               $_resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber'];
190               $_time     = time();
191               $_SESSION['phpgw_info']['instant_messenger']['time'] = $_time;
192               $_SESSION['phpgw_info']['instant_messenger']['socket'] = NULL;
193               $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] = NULL;
194               $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] = array();
195               $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL;
196               $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = false;
197
198               session_write_close();
199
200               $a = new Jabberd2;
201               if ( ($a->connect($_user . '@' . $_server . '/' . $_resource . ':' . $_port, $_pass)) )
202               {
203                  $pUsers[$pSession] = array();
204                  $pUsers[$pSession]['jabber'] = $a;
205                  $pUsers[$pSession]['time'] = $_time;
206                  $pUsers[$pSession]['attempt'] = 0;
207               }
208               else
209               {
210                  unset($a);
211               }
212               break;
213            }
214            session_write_close();
215         }
216      }
217   }
218
219   function close($pUser)
220   {
221      global $users;
222      $users[$pUser]['jabber']->disconnect();
223      unset($users[$pUser]);
224   }
225
226   function readSocket(&$pUsers, $pSession)
227   {
228      global $send;
229      $sessionpath = explode(';', session_save_path());
230      foreach ( $sessionpath as $path )
231      {
232         $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/';
233
234         if ( file_exists($path . 'sess_' . $pSession) )
235         {
236            session_id($pSession);
237
238            ob_start();
239            session_start();
240            ob_clean();
241            ob_end_flush();
242
243            $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']);
244            if ( $is_user )
245            {
246               $pUsers[$pSession]['time'] = $_SESSION['phpgw_info']['instant_messenger']['time'];
247                                        $readSocket = @$pUsers[$pSession]['jabber']->readSocket();
248               
249               if ( $readSocket !== false )
250               {
251                  if ( !empty($readSocket) )
252                  {
253                     $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] .= $readSocket;
254                  }
255
256                                                if ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] )
257                                                        foreach ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] as $action => $value )
258                                                        {
259                                                                if ( $action === 'message' )
260                                                                {
261                                                                        if ( !is_null($value) )
262                                                                        {
263                                                                                $pUsers[$pSession]['jabber']->writeSocket($value);
264                                                                                $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL;
265                                                                        }
266                                                                }
267                                                                else if ( $action === 'vcard' )
268                                                                {
269                                                                        if ( !empty($value) )
270                                                                        {
271                                                                                foreach ( $value as $key => $val )
272                                                                                {
273                                                                                        $pUsers[$pSession]['jabber']->getVcard($val);
274                                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][$key]);
275                                                                                }
276                                                                        }
277                                                                }
278                                                                else
279                                                                {
280                                                                        if ( method_exists($pUsers[$pSession]['jabber'], $action) )
281                                                                                if ( $value )
282                                                                                        $pUsers[$pSession]['jabber']->$action($value);
283                                                                                else
284                                                                                        $pUsers[$pSession]['jabber']->$action();
285                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out'][$action]);
286                                                                }
287                                                        }
288               }
289               else
290               {
291                  close($pSession);
292               }
293               session_write_close();
294               break;
295            }
296            session_write_close();
297         }
298      }
299   }
300?>
Note: See TracBrowser for help on using the repository browser.