db79276a083632009b5eb02cb44ae24a9ba58c34
[core.git] / framework / main / classes / client / http / class_HttpClient.ph
1
2         /**
3          * Determines own remote IP address (e.g. can be used to probe if we are
4          * reachable from outside by determining external address and then connect to it.
5          * This is accomblished by connecting to the IP of www.shipsimu.org which
6          * should default to 188.138.90.169 and requesting /ip.php which does only
7          * return the content of $_SERVER['REMOTE_ADDR']. Of course, this method
8          * requires a working Internet connection.
9          *
10          * This method is taken from a user comment on php.net and heavily rewritten.
11          * Compare to following link:
12          * http://de.php.net/manual/en/function.socket-create.php#49368
13          *
14          * @return      $externalAddress        The determined external address address
15          * @todo        Make IP, host name, port and script name configurable
16          */
17         public static function determineExternalAddress () {
18                 // Get helper instance
19                 $helperInstance = new ConsoleTools();
20
21                 // First get a socket
22                 // @TODO Add some DNS caching here
23
24                 // Open connection
25                 if ($helperInstance->isProxyUsed() === true) {
26                         // Resolve hostname into IP address
27                         $ip = ConsoleTools::resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host'));
28
29                         // @TODO Handle $ip = false
30
31                         // Connect to host through proxy connection
32                         $socketResource = fsockopen($ip, $helperInstance->getConfigInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30);
33                 } else {
34                         // Connect to host directly
35                         $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 30);
36                 }
37
38                 // Check if there was an error else
39                 if ($errorNo > 0) {
40                         // Then throw again
41                         throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET);
42                 } // END - if
43
44                 // Prepare the GET request
45                 $request  = 'GET ' . ($helperInstance->isProxyUsed() === true ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
46                 $request .= 'Host: shipsimu.org' . self::HTTP_EOL;
47                 $request .= 'User-Agent: ' . $this->getUserAgent() . self::HTTP_EOL;
48                 $request .= 'Connection: close' . self::HTTP_EOL;
49
50                 // Do we use proxy?
51                 if ($helperInstance->isProxyUsed() === true) {
52                         // CONNECT method?
53                         if ($helperInstance->getConfigInstance()->getConfigEntry('proxy_connect_method') == 'Y') {
54                                 // Setup proxy tunnel
55                                 $response = $helperInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource);
56
57                                 // If the response is invalid, abort
58                                 if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) {
59                                         // Invalid response!
60                                         $helperInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, true));
61                                 } // END - if
62                         } else {
63                                 // Add header for proxy
64                                 $request .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL;
65                         }
66                 } // END - if
67
68                 // Add last HTTP_EOL
69                 $request .= self::HTTP_EOL;
70
71                 // Send it to the socket
72                 fwrite($socketResource, $request);
73
74                 // Init IP (this will always be the last line)
75                 $externalAddress = 'invalid';
76
77                 // And read the reply
78                 while (!feof($socketResource)) {
79                         // Get line
80                         $externalAddress = trim(fgets($socketResource, 128));
81
82                         // Detect HTTP response
83                         if ((substr($externalAddress, 0, 7) == 'HTTP/1.') && (substr($externalAddress, -6, 6) != '200 OK')) {
84                                 // Stop processing
85                                 break;
86                         } // END - if
87                 } // END - while
88
89                 // Close socket
90                 fclose($socketResource);
91
92
93                 // Debug message
94                 /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Resolved external address: ' . $externalAddress);
95
96                 // Return determined external address
97                 return $externalAddress;
98         }