Cleanup of previous commit.
[core.git] / inc / classes / main / console / class_ConsoleTools.php
index 1599cdc8fbfff3a7584c46eb047f23890aa26341..5c29a4c0edbf183bde71db0270523f4fe7b6db86 100644 (file)
@@ -2,11 +2,11 @@
 /**
  * This class contains static helper functions for console applications
  *
- * @author             Roland Haeder <webmaster@ship-simu.org>
+ * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2014 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.ship-simu.org
+ * @link               http://www.shipsimu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class ConsoleTools extends BaseFrameworkSystem {
+       // Constants
+       const HTTP_EOL = "\r\n";
+       const HTTP_USER_AGENT = 'ConsoleTools/1.0';
+
        /**
         * Protected constructor
         *
@@ -42,7 +46,7 @@ class ConsoleTools extends BaseFrameworkSystem {
         */
        protected function resolveIpAddress ($hostname) {
                // Debug message
-               $this->debugOutput(sprintf("[%s:] Our host name is: <span class=\"data\">%s</span>",
+               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] Host name to resolve is: %s',
                        $this->__toString(),
                        $hostname
                ));
@@ -55,18 +59,18 @@ class ConsoleTools extends BaseFrameworkSystem {
                $ipResolved = gethostbyname($hostname);
 
                // Was it fine?
-               if (($ipResolved !== false) && ($ipResolved != $hostname)) {
+               if (($ipResolved !== FALSE) && ($ipResolved != $hostname)) {
                        // Okay, this works!
                        $ip = $ipResolved;
 
                        // Debug message
-                       $this->debugOutput(sprintf("[%s:] Resolved IP address is: <span class=\"data\">%s</span>\n",
+                       self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] Resolved IP address is: %s',
                                $this->__toString(),
                                $ip
                        ));
                } else {
                        // Problem while resolving IP address
-                       $this->debugOutput(sprintf("[%s:] Problem resolving IP address for host <span class=\"data\">%s</span>. Please check your /etc/hosts file.",
+                       self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] Problem resolving IP address for host %s. Please check your /etc/hosts file.',
                                $this->__toString(),
                                $hostname
                        ));
@@ -76,46 +80,164 @@ class ConsoleTools extends BaseFrameworkSystem {
                return $ip;
        }
 
+       /**
+        * Checks wether proxy configuration is used
+        *
+        * @return      $isUsed         Wether proxy is used
+        */
+       protected function isProxyUsed () {
+               // Do we have cache?
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Determine it
+                       $GLOBALS[__METHOD__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0));
+               } // END - if
+
+               // Return cache
+               return $GLOBALS[__METHOD__];
+       }
+
+       /**
+        * Sets up a proxy tunnel for given hostname and through resource
+        *
+        * @param       $host                           Host to connect to
+        * @param       $port                           Port number to connect to
+        * @param       $socketResource         Resource of a socket
+        * @return      $response                       Response array
+        */
+       protected function setupProxyTunnel ($host, $port, $socketResource) {
+               // Initialize array
+               $response = array('', '', '');
+               $proxyTunnel = '';
+
+               // Generate CONNECT request header
+               $proxyTunnel .= 'CONNECT ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL;
+               $proxyTunnel .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL;
+               $proxyTunnel .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL;
+
+               // Use login data to proxy? (username at least!)
+               if ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') {
+                       // Add it as well
+                       $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->getConfigEntry('proxy_password'));
+                       $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL;
+               } // END - if
+
+               // Add last new-line
+               $proxyTunnel .= self::HTTP_EOL;
+               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: proxyTunnel=' . $proxyTunnel);
+
+               // Write request
+               fwrite($socketResource, $proxyTunnel);
+
+               // Got response?
+               if (feof($socketResource)) {
+                       // No response received
+                       return $response;
+               } // END - if
+
+               // Read the first line
+               $resp = trim(fgets($socketResource, 10240));
+               $respArray = explode(' ', $resp);
+               if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
+                       // Invalid response!
+                       return $response;
+               } // END - if
+
+               // All fine!
+               return $respArray;
+       }
+
+       /**
+        * Tried to extract hostname from given raw data. On a Genntoo system, this could be multiple lines with # as comments. So try to get rid of it
+        *
+        * @param       $rawData        Raw data from /etc/hostname file
+        * @return      $hostname       Extracted host name
+        */
+       protected function extractHostnameFromRawData ($rawData) {
+               // Default is invalid
+               $hostname = 'invalid';
+
+               // Try to "explode" it
+               $data = explode(PHP_EOL, $rawData);
+
+               // "Walk" through it
+               foreach ($data as $line) {
+                       // Trim it
+                       $line = trim($line);
+
+                       // Begins with a hash (#) = comment?
+                       if (substr($line, 0, 1) == '#') {
+                               // Then skip it
+                               continue;
+                       } // END - if
+
+                       // Has an equals sign?
+                       if (strpos($line, '=') !== FALSE) {
+                               // Then "explode" it again, right part is hostname in quotes
+                               $hostData = explode('=', $line);
+
+                               // Make sure only a key=value pair goes through
+                               assert(count($hostData) == 2);
+
+                               // Try to get it and abort
+                               $hostname = str_replace(array('"', chr(39)), array('', ''), $hostData[1]);
+                               break;
+                       } else {
+                               // Use it directly
+                               $hostname = $line;
+                               break;
+                       }
+               } // END - foreach
+
+               // Return it
+               return $hostname;
+       }
+
        /**
         * Aquires the IP address of this host by reading the /etc/hostname file
         * and solving it. It is now stored in configuration
         *
-        * @return      void
+        * @return      $ip             Aquired IP address
         */
        public static function acquireSelfIPAddress () {
                // Local IP by default
                $ip = '127.0.0.1';
 
                // Get a new instance
-               $helper = new ConsoleTools();
+               $helperInstance = new ConsoleTools();
 
                try {
                        // Get a file pointer
-                       $io = FrameworkFileInputPointer::createFrameworkFileInputPointer('/etc/hostname');
+                       $fileInstance = ObjectFactory::createObjectByConfiguredName('file_raw_input_class', array($helperInstance->getConfigInstance()->getConfigEntry('hostname_file')));
 
                        // Read the file
-                       $hostname = trim($io->readFromFile());
+                       $rawData = trim($fileInstance->readFromFile());
 
                        // Close the file
-                       $io->closeFile();
+                       $fileInstance->closeFile();
+
+                       // Extract hostname from it
+                       $hostname = $helperInstance->extractHostnameFromRawData($rawData);
 
                        // Resolve the IP number
-                       $ip = $helper->resolveIpAddress($hostname);
-               } catch (FileNotFoundException $e) {
+                       $ip = $helperInstance->resolveIpAddress($hostname);
+               } catch (FileIoException $e) {
                        // Fall-back to 'SESSION_SVR' which found on my Sun Station
                        if (isset($_SERVER['SESSION_SVR'])) {
                                // Resolve it
-                               $ip = $helper->resolveIpAddress($_SERVER['SESSION_SVR']);
+                               $ip = $helperInstance->resolveIpAddress($_SERVER['SESSION_SVR']);
+                       } elseif (isset($_SERVER['COMPUTERNAME'])) {
+                               // May happen on some XP systems, so also try this
+                               $ip = $helperInstance->resolveIpAddress($_SERVER['COMPUTERNAME']);
                        } else {
                                // Could not find our hostname
-                               $helper->debugOutput(sprintf("[%s:] WARNING: Cannot resolve my own IP address.",
-                                       $helper->__toString()
+                               self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] WARNING: Cannot resolve my own IP address.',
+                                       $helperInstance->__toString()
                                ));
                        }
                } catch (FrameworkException $e) {
                        // Output debug message
-                       $helper->debugOutput(sprintf("[%s:] Problem while resolving own IP address: [%s|%s]:%s",
-                               $helper->__toString(),
+                       self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] Problem while resolving own IP address: [%s|%s]:%s',
+                               $helperInstance->__toString(),
                                $e->__toString(),
                                $e->getHexCode(),
                                $e->getMessage()
@@ -123,55 +245,157 @@ class ConsoleTools extends BaseFrameworkSystem {
                }
 
                // Set it in configuration
-               FrameworkConfiguration::getInstance()->setServerAddress($ip);
+               FrameworkConfiguration::getSelfInstance()->setServerAddress($ip);
+
+               // Return it
+               return $ip;
        }
 
        /**
         * Determines own remote IP address (e.g. can be used to probe if we are
-        * reachable from outside by determining external IP and then connect to it.
-        * This is accomblished by connecting to the IP of www.ship-simu.org which
-        * should default to 217.172.186.31. Of course, this method requires a valid
-        * and working Internet connection to work properly.
+        * reachable from outside by determining external address and then connect to it.
+        * This is accomblished by connecting to the IP of www.shipsimu.org which
+        * should default to 188.138.90.169 and requesting /ip.php which does only
+        * return the content of $_SERVER['REMOTE_ADDR']. Of course, this method
+        * requires a working Internet connection.
         *
-        * This method is taken and lightly rewritten from a user comment on php.net:
+        * This method is taken from a user comment on php.net and heavily rewritten.
+        * Compare to following link:
         * http://de.php.net/manual/en/function.socket-create.php#49368
         *
-        * @param       $dest                           IP destination we should connect to
-        * @param       $port                           Port destination we should connect to
-        * @return      $externalAddress        The determined external IP address
+        * @return      $externalAddress        The determined external address address
         * @throws      InvalidSocketException  If socket initialization wents wrong or if an errors occurs
+        * @todo        This should be moved out to an external class, e.g. HttpClient
+        * @todo        Make IP, host name, port and script name configurable
         */
-       public function determineExternalIp ($dest = '217.172.186.31', $port = 80) {
+       public static function determineExternalAddress () {
+               // Get helper instance
+               $helperInstance = new ConsoleTools();
+
                // First get a socket
-               $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
+               // @TODO Add some DNS caching here
 
-               // Is it valid?
-               if (!is_resource($socket)) {
-                       // Throw InvalidSocketException
-                       throw new InvalidSocketException (array($this, gettype($socket), 0, 'unknown'));
-               } // END - if
+               // Open connection
+               if ($helperInstance->isProxyUsed() === TRUE) {
+                       // Resolve hostname into IP address
+                       $ip = $helperInstance->resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host'));
 
-               // Get socket error code for verification
-               $socketError = socket_last_error($socket);
+                       // Connect to host through proxy connection
+                       $socketResource = fsockopen($ip, $helperInstance->getConfigInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30);
+               } else {
+                       // Connect to host directly
+                       $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 30);
+               }
 
                // Check if there was an error else
-               if ($socketError > 0) {
+               if ($errorNo > 0) {
                        // Then throw again
-                       throw new InvalidSocketException(array($this, gettype($socket), $socketError, socket_strerror($socketError)), BaseListener::EXCEPTION_INVALID_SOCKET);
+                       throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseListener::EXCEPTION_INVALID_SOCKET);
                } // END - if
 
-               // Connect to the destination
-               socket_connect($socket, $dest, $port);
+               // Prepare the GET request
+               $request  = 'GET ' . ($helperInstance->isProxyUsed() === TRUE ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
+               $request .= 'Host: shipsimu.org' . self::HTTP_EOL;
+               $request .= 'User-Agent: ' . self::HTTP_USER_AGENT . self::HTTP_EOL;
+               $request .= 'Connection: close' . self::HTTP_EOL;
 
-               // Get the socket address (our external IP) and port (ignored)
-               socket_getsockname($socket, $externalAddress, $ourPort);
+               // Do we use proxy?
+               if ($helperInstance->isProxyUsed() === TRUE) {
+                       // CONNECT method?
+                       if ($helperInstance->getConfigInstance()->getConfigEntry('proxy_connect_method') == 'Y') {
+                               // Setup proxy tunnel
+                               $response = $helperInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource);
 
-               // Close the socket
-               socket_close($socket);
+                               // If the response is invalid, abort
+                               if ((count($response) == 3) && (empty($response[0])) && (empty($response[1])) && (empty($response[2]))) {
+                                       // Invalid response!
+                                       $helperInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, TRUE));
+                               } // END - if
+                       } else {
+                               // Add header for proxy
+                               $request .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL;
+                       }
+               } // END - if
+
+               // Add last HTTP_EOL
+               $request .= self::HTTP_EOL;
+
+               // Send it to the socket
+               fwrite($socketResource, $request);
+
+               // Init IP (this will always be the last line)
+               $externalAddress = 'invalid';
+
+               // And read the reply
+               while (!feof($socketResource)) {
+                       // Get line
+                       $externalAddress = trim(fgets($socketResource, 128));
 
-               // Return determined external IP
+                       // Detect HTTP response
+                       if ((substr($externalAddress, 0, 7) == 'HTTP/1.') && (substr($externalAddress, -6, 6) != '200 OK')) {
+                               // Stop processing
+                               break;
+                       } // END - if
+               } // END - while
+
+               // Close socket
+               fclose($socketResource);
+
+
+               // Debug message
+               /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Resolved external address: ' . $externalAddress);
+
+               // Return determined external address
                return $externalAddress;
        }
+
+       /**
+        * Analyzes the 'environment', mostly $_SERVER, for presence of elements
+        * which indicates clearly that e.g. this script has been executed from
+        * console or web.
+        *
+        * @return      $type   The analyzed type, can be 'http' or 'console'
+        */
+       public static function analyzeEnvironmentForType () {
+               // Default is the console
+               $type = 'console';
+
+               // Now, do we have a request method, or query string set?
+               if ((isset($_SERVER['REQUEST_METHOD'])) || (isset($_SERVER['QUERY_STRING']))) {
+                       // Possibly HTTP request
+                       $type = 'http';
+               } // END - if
+
+               // Return it
+               return $type;
+       }
+
+       /**
+        * Analyzes the 'environment', mostly $_SERVER, for presence of elements
+        * which indicates clearly that e.g. this script has been executed from
+        * console or web. This method should be used for class names, they
+        * currently are named differently. Here is a list to clarify this:
+        *
+        *   Request type | Class type
+        * -----------------------------
+        *      http      |    web
+        *     console    |  console
+        *
+        * @return      $type   The analyzed type, can be 'http' or 'console'
+        */
+       public static function analyzeEnvironmentForClassType () {
+               // Default is the console
+               $type = 'console';
+
+               // Now, do we have a request method, or query string set?
+               if ((isset($_SERVER['REQUEST_METHOD'])) || (isset($_SERVER['QUERY_STRING']))) {
+                       // Possibly HTTP request
+                       $type = 'web';
+               } // END - if
+
+               // Return it
+               return $type;
+       }
 }
 
 // [EOF]