Ignore:
Timestamp:
08/29/07 08:55:50 (17 years ago)
Author:
niltonneto
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/instant_messenger/IMManager.php

    r55 r63  
    11#!/usr/bin/php -Cq 
    22<?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    ### 
     3 
    154   require_once('inc/IMConfigure.php'); 
    165 
     
    187   # definitions of socket - BEGIN 
    198   # 
    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  
    439      define('URI', '0.0.0.0'); 
    44  
    4510      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  
    5211      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)); 
    5412   # 
    5513   # definitions of socket - END 
    5614   ### 
    5715 
    58    //if ( !SOCKET_NON_SECURITY || !SOCKET_SECURITY ) # It's necessary to remove the next line for safe connection 
     16 
    5917   if ( !SOCKET_NON_SECURITY ) 
    6018   { 
    6119      printf("NON SECURITY:\n(%d) %s", $errno_non_security, $errstr_non_security); 
    62       //printf("SECURITY:\n(%d) %s", $errno_security, $errstr_security); 
    6320      exit(0); 
    6421   } 
     
    6623   include 'inc/Jabberd2.abstract.php'; 
    6724 
    68    //$master = array(SOCKET_NON_SECURITY, SOCKET_SECURITY); # It's necessary to remove the next line for safe connection 
    6925   $master = array(SOCKET_NON_SECURITY); 
    7026 
     
    7935      $_e = NULL; 
    8036        
    81       $mod_fd = @stream_select($read, $_w , $_e , READ_SOCKET_AFTER_OF); 
     37      $mod_fd = @stream_select($read, $_w , $_e , READ_SOCKET_AFTER_OF ); 
    8238 
    8339      if ( $mod_fd === FALSE ) 
     
    8541 
    8642      for ( $i = 0; $i < $mod_fd; ++$i ) 
     43      { 
    8744         if ( $read[$i] === SOCKET_NON_SECURITY ) 
    8845         { 
    8946            $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")); 
    9147            $master[] = &$conn_non_security; 
    9248         } 
    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          //} 
    9949         else 
    10050         { 
     
    10252            if ( strlen($sock_data) === 0 ) 
    10353            { 
    104                printf("Connection closed"); 
    10554               $key_to_del = array_search($read[$i], $master, TRUE); 
    10655               fclose($read[$i]); 
     
    10958            else if ( $sock_data === FALSE ) 
    11059            { 
    111                printf("Something bad happened"); 
    11260               $key_to_del = array_search($read[$i], $master, TRUE); 
    11361               unset($master[$key_to_del]); 
     
    11563            else 
    11664            { 
    117                # printf("\n\n%s\n\n", $sock_data); 
    11865               $headers = (split("\n", $sock_data)); 
    11966 
     
    12168               preg_match('/^Host: \w*:(.+)/', $headers[1], $host); 
    12269 
    123                //if ( (int)$host[1] == PORT_NON_SECURITY )  
    124                   $conn = &$conn_non_security; 
    125                //else 
    126                //   $conn = &$conn_security; 
     70               $conn = &$conn_non_security; 
    12771 
    12872               $request = explode(';', $request[2]); 
    12973 
    13074               if ( $request[0] == CONNECT && isset($request[1]) && !isset($users[$request[1]]) ) 
     75               { 
    13176                  connect($users, $request[1]); 
     77               } 
    13278 
    13379               if ( $request[0] == CLOSE && isset($request[1]) && isset($users[$request[1]]) ) 
    134                   close($request[1]); 
     80               {    
     81                close($request[1]); 
     82               } 
    13583 
    13684               fclose($read[$i]); 
     
    13886            } 
    13987         } 
    140  
     88      }  
     89       
    14190      foreach ( $users as $key => $val ) 
    14291      { 
    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             } 
     92         if($users[$key]['time0'] != $users[$key]['time1']  ) 
     93         { 
     94                $users[$key]['time1'] = $users[$key]['time0']; 
     95                $users[$key]['attempt'] = 0; 
     96                readSocket($key); 
     97                //readSocket($users, $key); 
     98         } 
    15499         else 
    155100         { 
    156             $users[$key]['attempt'] = 0; 
    157             readSocket($users, $key); 
    158          } 
     101                                $users[$key]['attempt']++; 
     102                                if( $users[$key]['attempt'] > 5 ) 
     103                                { 
     104                                $users[$key]['jabber']->disconnect(); 
     105                                unset($users[$key]);                                     
     106                                } 
     107                                else 
     108                                { 
     109                                        readSocket($key); 
     110                                        //readSocket($users, $key); 
     111                                }                
     112         } 
     113 
     114         //print_r("Session : " . $key . " | Count : " . count($users) . " | time 0 : " . $users[$key]['time0'] . " | time 1 : " . $users[$key]['time1'] . " | tentativas : " . $users[$key]['attempt'] . " \n"); 
    159115      } 
    160116 
    161       //print_r($users); 
    162    } 
    163  
     117   } 
     118         
     119         
    164120   exit(0); 
    165121 
     122         
    166123   function connect(&$pUsers, $pSession) 
    167124   { 
     
    203160                  $pUsers[$pSession] = array(); 
    204161                  $pUsers[$pSession]['jabber'] = $a; 
    205                   $pUsers[$pSession]['time'] = $_time; 
     162                  $pUsers[$pSession]['time0'] = $_time; 
     163                                                $pUsers[$pSession]['time1'] = 0;                   
    206164                  $pUsers[$pSession]['attempt'] = 0; 
    207165               } 
     
    224182   } 
    225183 
    226    function readSocket(&$pUsers, $pSession) 
    227    { 
     184   //function readSocket(&$pUsers, $pSession) 
     185   function readSocket($pSession) 
     186   { 
     187      global $users; 
    228188      global $send; 
    229189      $sessionpath = explode(';', session_save_path()); 
     
    244204            if ( $is_user ) 
    245205            { 
    246                $pUsers[$pSession]['time'] = $_SESSION['phpgw_info']['instant_messenger']['time']; 
    247                                         $readSocket = @$pUsers[$pSession]['jabber']->readSocket(); 
     206               $users[$pSession]['time0'] = $_SESSION['phpgw_info']['instant_messenger']['time']; 
     207                                        $readSocket = $users[$pSession]['jabber']->readSocket(); 
    248208                
    249209               if ( $readSocket !== false ) 
     
    261221                                                                        if ( !is_null($value) ) 
    262222                                                                        { 
    263                                                                                 $pUsers[$pSession]['jabber']->writeSocket($value); 
     223                                                                                $users[$pSession]['jabber']->writeSocket($value); 
    264224                                                                                $_SESSION['phpgw_info']['instant_messenger']['socket']['out']['message'] = NULL; 
    265225                                                                        } 
     
    271231                                                                                foreach ( $value as $key => $val ) 
    272232                                                                                { 
    273                                                                                         $pUsers[$pSession]['jabber']->getVcard($val); 
     233                                                                                        $users[$pSession]['jabber']->getVcard($val); 
    274234                                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out']['vcard'][$key]); 
    275235                                                                                } 
     
    278238                                                                else 
    279239                                                                { 
    280                                                                         if ( method_exists($pUsers[$pSession]['jabber'], $action) ) 
     240                                                                        if ( method_exists($users[$pSession]['jabber'], $action) ) 
    281241                                                                                if ( $value ) 
    282                                                                                         $pUsers[$pSession]['jabber']->$action($value); 
     242                                                                                        $users[$pSession]['jabber']->$action($value); 
    283243                                                                                else 
    284                                                                                         $pUsers[$pSession]['jabber']->$action(); 
     244                                                                                        $users[$pSession]['jabber']->$action(); 
    285245                                                                        unset($_SESSION['phpgw_info']['instant_messenger']['socket']['out'][$action]); 
    286246                                                                } 
Note: See TracChangeset for help on using the changeset viewer.