source: trunk/instant_messenger/IMManager.php @ 64

Revision 64, 8.2 KB checked in by niltonneto, 17 years ago (diff)

* empty log message *

  • Property svn:eol-style set to native
  • Property svn:executable set to *
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', stream_socket_server(TARGET_NON_SECURITY, $errno_non_security, $errstr_non_security, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN));
16   #
17   # definitions of socket - END
18   ###
19
20
21   if ( !SOCKET_NON_SECURITY )
22   {
23      printf("NON SECURITY:\n(%d) %s", $errno_non_security, $errstr_non_security);
24      exit(0);
25   }
26
27   $master = array(SOCKET_NON_SECURITY);
28   $users = array();
29   $send = false;
30
31   while ( true )
32   {
33      $read = $master;
34      $_w = NULL;
35      $_e = NULL;
36       
37      $mod_fd = @stream_select($read, $_w , $_e , READ_SOCKET_AFTER_OF );
38               
39      if ( $mod_fd === FALSE )
40      {
41         break;
42      }
43
44      for ( $i = 0; $i < $mod_fd; ++$i )
45      {
46         if ( $read[$i] === SOCKET_NON_SECURITY )
47         {
48            $conn_non_security = stream_socket_accept(SOCKET_NON_SECURITY);
49            $master[] = &$conn_non_security;
50         }
51         else
52         {
53            $sock_data = fread($read[$i], 1024);
54            if ( strlen($sock_data) === 0 )
55            {
56               $key_to_del = array_search($read[$i], $master, TRUE);
57               fclose($read[$i]);
58               unset($master[$key_to_del]);
59            }
60            else if ( $sock_data === FALSE )
61            {
62               $key_to_del = array_search($read[$i], $master, TRUE);
63               unset($master[$key_to_del]);
64            }
65            else
66            {
67               $headers = (split("\n", $sock_data));
68
69               preg_match('/^(GET|POST) \/(.+) HTTP\/1\.\d/U', $headers[0], $request);
70               preg_match('/^Host: \w*:(.+)/', $headers[1], $host);
71
72               $conn = &$conn_non_security;
73
74               $request = explode(';', $request[2]);
75
76               if ( $request[0] == CONNECT && isset($request[1]) && !isset($users[$request[1]]) )
77               {
78                  connect($users, $request[1]);
79               }
80
81               if ( $request[0] == CLOSE && isset($request[1]) && isset($users[$request[1]]) )
82               {   
83                close($request[1]);
84               }
85
86               fclose($read[$i]);
87               unset($master[array_search($read[$i], $master)]);
88            }
89         }
90      }
91     
92      foreach ( $users as $key => $val )
93      {
94         if( $users[$key]['time0'] > $users[$key]['time1'] )
95         {
96                $users[$key]['time1'] = $users[$key]['time0'];
97                $users[$key]['attempt'] = 0;
98                readSocket($users, $key);
99         }
100         else
101         {
102                                $users[$key]['attempt']++;
103                                if( $users[$key]['attempt'] > 5 )
104                                {
105                                $users[$key]['jabber']->disconnect();
106                                unset($users[$key]);                                   
107                                }
108                                else
109                                {
110                                        readSocket($users, $key);
111                                }               
112         }
113      }
114   }
115       
116   exit(0);
117
118   function connect(&$pUsers, $pSession)
119   {
120                global $users;
121                     
122      $sessionpath = explode(';', session_save_path());
123      foreach ( $sessionpath as $path )
124      {
125         $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/';
126
127         if ( file_exists($path . 'sess_' . $pSession) )
128         {
129            session_id($pSession);
130
131            ob_start();
132            session_start();
133            ob_clean();
134            ob_end_flush();
135
136            $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']);
137            if ( $is_user )
138            {
139               $_user     = $_SESSION['phpgw_info']['instant_messenger']['user'];
140               $_pass     = $_SESSION['phpgw_info']['instant_messenger']['passwd'];
141               $_server   = $_SESSION['phpgw_info']['instant_messenger']['name_jabber'];
142               $_port     = $_SESSION['phpgw_info']['instant_messenger']['port_jabber'];
143               $_resource = $_SESSION['phpgw_info']['instant_messenger']['resource_jabber'];
144               $_time     = time();
145               $_SESSION['phpgw_info']['instant_messenger']['time'] = $_time;
146               $_SESSION['phpgw_info']['instant_messenger']['socket'] = NULL;
147               $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] = NULL;
148               $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] = array();
149               $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL;
150               $_SESSION['phpgw_info']['instant_messenger']['socket']['block'] = false;
151
152               session_write_close();
153
154               $a = new Jabberd2;
155               if ( ($a->connect($_user . '@' . $_server . '/' . $_resource . ':' . $_port, $_pass)) )
156               {
157                  $pUsers[$pSession] = array();
158                  $pUsers[$pSession]['jabber'] = $a;
159                  $pUsers[$pSession]['time0'] = $_time;
160                                                $pUsers[$pSession]['time1'] = 0;                 
161                  $pUsers[$pSession]['attempt'] = 0;
162               }
163               else
164               {
165                  unset($a);
166               }
167               break;
168            }
169            session_write_close();
170         }
171      }
172   }
173
174   function close($pUser)
175   {
176      global $users;
177      $users[$pUser]['jabber']->disconnect();
178      unset($users[$pUser]);
179   }
180
181   function readSocket(&$pUsers, $pSession)
182   {
183      global $send;
184      $sessionpath = explode(';', session_save_path());
185      foreach ( $sessionpath as $path )
186      {
187         $path = ( strrpos($path, '/') == strlen($path) - 1 ) ? $path : $path . '/';
188
189         if ( file_exists($path . 'sess_' . $pSession) )
190         {
191            session_id($pSession);
192
193            ob_start();
194            session_start();
195            ob_clean();
196            ob_end_flush();
197
198            $is_user = isset($_SESSION['phpgw_info']['instant_messenger']['user']) and isset($_SESSION['phpgw_info']['instant_messenger']['passwd']);
199            if ( $is_user )
200            {
201               $pUsers[$pSession]['time0'] = $_SESSION['phpgw_info']['instant_messenger']['time'];
202                                        $readSocket = @$pUsers[$pSession]['jabber']->readSocket();
203               
204               $connection_close_timeout = strpos($readSocket, "<stream:error");
205               $connection_close_server = strpos($readSocket,"</stream:stream>");
206               
207               if ( ($readSocket !== false) && ($connection_close_timeout === false ) && ($connection_close_server === false ))
208               {
209                  if ( !empty($readSocket) )
210                  {
211                     $_SESSION['phpgw_info']['instant_messenger']['socket']['in'] .= $readSocket;
212                  }
213
214                                                if ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] )
215                                                        foreach ( $_SESSION['phpgw_info']['instant_messenger']['socket']['out'] as $action => $value )
216                                                        {
217                                                                if ( $action === 'message' )
218                                                                {
219                                                                        if ( !is_null($value) )
220                                                                        {
221                                                                                $pUsers[$pSession]['jabber']->writeSocket($value);
222                                                                                $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL;
223                                                                        }
224                                                                }
225                                                                else if ( $action === 'vcard' )
226                                                                {
227                                                                        if ( !empty($value) )
228                                                                        {
229                                                                                foreach ( $value as $key => $val )
230                                                                                {
231                                                                                        $pUsers[$pSession]['jabber']->getVcard($val);
232                                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][$key]);
233                                                                                }
234                                                                        }
235                                                                }
236                                                                else
237                                                                {
238                                                                        if ( method_exists($pUsers[$pSession]['jabber'], $action) )
239                                                                                if ( $value )
240                                                                                        $pUsers[$pSession]['jabber']->$action($value);
241                                                                                else
242                                                                                        $pUsers[$pSession]['jabber']->$action();
243                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out'][$action]);
244                                                                }
245                                                        }
246               }
247               else
248               {
249                close($pSession);                       
250               }
251               break;
252            }
253            else
254            {
255               close($pSession);
256            }
257            break;
258         }
259      }
260      session_write_close();
261   }
262?>
Note: See TracBrowser for help on using the repository browser.