Rewrite continued:
[core.git] / framework / main / classes / console / class_ConsoleTools.php
index 84b72f82ef4173aa999fd53ca3be74447e37c0a2..a2f62337ad9b26cc30ed5e9e66f30fb69431ebfb 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 // Own namespace
-namespace CoreFramework\Tools;
+namespace CoreFramework\Console\Tools;
 
 // Import framework stuff
 use CoreFramework\Configuration\FrameworkConfiguration;
@@ -45,50 +45,6 @@ class ConsoleTools extends BaseFrameworkSystem {
                parent::__construct(__CLASS__);
        }
 
-       /**
-        * Tries to resolve an IP address from given hostname. Currently only IPv
-        * addresses are resolved.
-        *
-        * @param       $hostname       Host name we shall resolve
-        * @return      $ip                     IP address resolved from host name
-        * @todo        We should connect this to a caching class to cache DNS requests
-        */
-       protected function resolveIpAddress ($hostname) {
-               // Debug message
-               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Host name to resolve is: %s',
-                       $this->__toString(),
-                       $hostname
-               ));
-
-               // Default is an invalid one
-               $ip = '0.0.0.0';
-
-               // Resolve it
-               // @TODO Here should the cacher be implemented
-               $ipResolved = gethostbyname($hostname);
-
-               // Was it fine?
-               if (($ipResolved !== FALSE) && ($ipResolved != $hostname)) {
-                       // Okay, this works!
-                       $ip = $ipResolved;
-
-                       // Debug message
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Resolved IP address is: %s',
-                               $this->__toString(),
-                               $ip
-                       ));
-               } else {
-                       // Problem while resolving IP address
-                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Problem resolving IP address for host %s. Please check your /etc/hosts file.',
-                               $this->__toString(),
-                               $hostname
-                       ));
-               }
-
-               // Return resolved IP
-               return $ip;
-       }
-
        /**
         * Checks wether proxy configuration is used
         *
@@ -180,7 +136,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                        } // END - if
 
                        // Has an equals sign?
-                       if (strpos($line, '=') !== FALSE) {
+                       if (strpos($line, '=') !== false) {
                                // Then "explode" it again, right part is hostname in quotes
                                $hostData = explode('=', $line);
 
@@ -201,15 +157,71 @@ class ConsoleTools extends BaseFrameworkSystem {
                return $hostname;
        }
 
+       /**
+        * Tries to resolve an IP address from given hostname. Currently only IPv
+        * addresses are resolved.
+        *
+        * @param       $hostname       Host name we shall resolve
+        * @return      $ipAddress      IPv4 address resolved from host name
+        * @todo        This should be connected to a caching class to cache DNS requests
+        */
+       public static function resolveIpAddress ($hostname) {
+               // Debug message
+               self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Host name to resolve is: %s',
+                       __CLASS__,
+                       $hostname
+               ));
+
+               // Default is false
+               $ipAddress = false;
+
+               // Is a dot at the end?
+               if (substr($hostname, -1, 1) != '.') {
+                       /*
+                        * Then append it to prevent lookup of invalid host names through
+                        * all search-domains. This will greatly improve lookup performance
+                        * and has no disadvantages for anybody. A dot at the end of a
+                        * domain btw means full-qualified domain, do not prepend to any
+                        * other domain, basically.
+                        */
+                       $hostname .= '.';
+               } // END - if
+
+               // Resolve it
+               // @TODO Here should the cacher be implemented
+               $ipResolved = gethostbyname($hostname);
+
+               // Was it fine?
+               if (($ipResolved !== false) && ($ipResolved != $hostname)) {
+                       // Okay, this works!
+                       $ipAddress = $ipResolved;
+
+                       // Debug message
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Resolved IP address is: %s',
+                               __CLASS__,
+                               $ipAddress
+                       ));
+               } else {
+                       // Problem while resolving IP address
+                       self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Problem resolving IP address for host %s. Please check your /etc/hosts file.',
+                               __CLASS__,
+                               $hostname
+                       ));
+               }
+
+               // Return resolved IP
+               return $ipAddress;
+       }
+
        /**
         * Aquires the IP address of this host by reading the /etc/hostname file
         * and solving it. It is now stored in configuration
         *
-        * @return      $ip             Aquired IP address
+        * @return      $ipAddress      Aquired IPv4 address
         */
        public static function acquireSelfIPAddress () {
                // Local IP by default
-               $ip = '127.0.0.1';
+               $ipAddress = '127.0.0.1';
 
                // Get a new instance
                $helperInstance = new ConsoleTools();
@@ -228,15 +240,15 @@ class ConsoleTools extends BaseFrameworkSystem {
                        $hostname = $helperInstance->extractHostnameFromRawData($rawData);
 
                        // Resolve the IP number
-                       $ip = $helperInstance->resolveIpAddress($hostname);
+                       $ipAddress = self::resolveIpAddress($hostname);
                } catch (FileNotFoundException $e) {
                        // Fall-back to 'SESSION_SVR' which found on my Sun Station
                        if (isset($_SERVER['SESSION_SVR'])) {
                                // Resolve it
-                               $ip = $helperInstance->resolveIpAddress($_SERVER['SESSION_SVR']);
+                               $ipAddress = self::resolveIpAddress($_SERVER['SESSION_SVR']);
                        } elseif (isset($_SERVER['COMPUTERNAME'])) {
-                               // May happen on some XP systems, so also try this
-                               $ip = $helperInstance->resolveIpAddress($_SERVER['COMPUTERNAME']);
+                               // May happen on some Windows XP systems, so also try this
+                               $ipAddress = self::resolveIpAddress($_SERVER['COMPUTERNAME']);
                        } else {
                                // Could not find our hostname
                                self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] WARNING: Cannot resolve my own IP address.',
@@ -254,10 +266,10 @@ class ConsoleTools extends BaseFrameworkSystem {
                }
 
                // Set it in configuration
-               FrameworkConfiguration::getSelfInstance()->setServerAddress($ip);
+               FrameworkConfiguration::getSelfInstance()->setServerAddress($ipAddress);
 
                // Return it
-               return $ip;
+               return $ipAddress;
        }
 
        /**
@@ -285,12 +297,12 @@ class ConsoleTools extends BaseFrameworkSystem {
                // @TODO Add some DNS caching here
 
                // Open connection
-               if ($helperInstance->isProxyUsed() === TRUE) {
+               if ($helperInstance->isProxyUsed() === true) {
                        // Resolve hostname into IP address
-                       $ip = $helperInstance->resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host'));
+                       $ipAddress = self::resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host'));
 
                        // Connect to host through proxy connection
-                       $socketResource = fsockopen($ip, $helperInstance->getConfigInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30);
+                       $socketResource = fsockopen($ipAddress, $helperInstance->getConfigInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30);
                } else {
                        // Connect to host directly
                        $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 30);
@@ -303,13 +315,13 @@ class ConsoleTools extends BaseFrameworkSystem {
                } // END - if
 
                // Prepare the GET request
-               $request  = 'GET ' . ($helperInstance->isProxyUsed() === TRUE ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL;
+               $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;
 
                // Do we use proxy?
-               if ($helperInstance->isProxyUsed() === TRUE) {
+               if ($helperInstance->isProxyUsed() === true) {
                        // CONNECT method?
                        if ($helperInstance->getConfigInstance()->getConfigEntry('proxy_connect_method') == 'Y') {
                                // Setup proxy tunnel
@@ -318,7 +330,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                                // 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));
+                                       $helperInstance->debugBackTrace('Proxy tunnel not working: response=' . print_r($response, true));
                                } // END - if
                        } else {
                                // Add header for proxy
@@ -397,7 +409,7 @@ class ConsoleTools extends BaseFrameworkSystem {
                $type = 'console';
 
                // Now, do we have a request method, or query string set?
-               if ((isset($_SERVER['REQUEST_METHOD'])) || (isset($_SERVER['QUERY_STRING']))) {
+               if (self::analyzeEnvironmentForType() == 'http') {
                        // Possibly HTTP request
                        $type = 'web';
                } // END - if