X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fconsole%2Fclass_ConsoleTools.php;h=b7807d30010a2b5d63c33e27da3c00fa5951832a;hp=b82eb19e5e42fd5f3db63dc0a7377a07daaee9ee;hb=53e46a945b95d6414af86dcf0d14de200dff15a5;hpb=b5fb7669b0dd02d9d8bf3fcef0381bcd7f6bee28 diff --git a/inc/classes/main/console/class_ConsoleTools.php b/inc/classes/main/console/class_ConsoleTools.php index b82eb19e..b7807d30 100644 --- a/inc/classes/main/console/class_ConsoleTools.php +++ b/inc/classes/main/console/class_ConsoleTools.php @@ -2,11 +2,11 @@ /** * This class contains static helper functions for console applications * - * @author Roland Haeder + * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 @@ -46,7 +46,7 @@ class ConsoleTools extends BaseFrameworkSystem { */ protected function resolveIpAddress ($hostname) { // Debug message - $this->debugOutput(sprintf('[%s:] Host name to resolve is: %s', + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] Host name to resolve is: %s', $this->__toString(), $hostname )); @@ -59,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: %s', + 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 %s. 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 )); @@ -123,7 +123,7 @@ class ConsoleTools extends BaseFrameworkSystem { // Add last new-line $proxyTunnel .= self::HTTP_EOL; - //* DEBUG: */ $this->debugOutput('CONSOLE-TOOLS: proxyTunnel=' . $proxyTunnel); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: proxyTunnel=' . $proxyTunnel); // Write request fwrite($socketResource, $proxyTunnel); @@ -146,6 +146,52 @@ class ConsoleTools extends BaseFrameworkSystem { 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 @@ -161,13 +207,16 @@ class ConsoleTools extends BaseFrameworkSystem { 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 = $helperInstance->resolveIpAddress($hostname); @@ -181,13 +230,13 @@ class ConsoleTools extends BaseFrameworkSystem { $ip = $helperInstance->resolveIpAddress($_SERVER['COMPUTERNAME']); } else { // Could not find our hostname - $helperInstance->debugOutput(sprintf('[%s:] WARNING: Cannot resolve my own IP address.', + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] WARNING: Cannot resolve my own IP address.', $helperInstance->__toString() )); } } catch (FrameworkException $e) { // Output debug message - $helperInstance->debugOutput(sprintf('[%s:] Problem while resolving own IP address: [%s|%s]:%s', + self::createDebugInstance(__CLASS__)->debugOutput(sprintf('[%s:] Problem while resolving own IP address: [%s|%s]:%s', $helperInstance->__toString(), $e->__toString(), $e->getHexCode(), @@ -205,7 +254,7 @@ class ConsoleTools extends BaseFrameworkSystem { /** * 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 + * 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. @@ -219,7 +268,7 @@ class ConsoleTools extends BaseFrameworkSystem { * @todo This should be moved out to an external class, e.g. HttpClient * @todo Make IP, host name, port and script name configurable */ - public static function determineExternalIp () { + public static function determineExternalAddress () { // Get helper instance $helperInstance = new ConsoleTools(); @@ -227,7 +276,7 @@ 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')); @@ -245,22 +294,22 @@ class ConsoleTools extends BaseFrameworkSystem { } // END - if // Prepare the GET request - $request = 'GET ' . ($helperInstance->isProxyUsed() === true ? 'http://ship-simu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL; - $request .= 'Host: ship-simu.org' . 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 - $response = $helperInstance->setupProxyTunnel('ship-simu.org', 80, $socketResource); + $response = $helperInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource); // 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 @@ -294,7 +343,7 @@ class ConsoleTools extends BaseFrameworkSystem { // Debug message - /* DEBUG: */ $helperInstance->debugOutput('CONSOLE-TOOLS: Resolved external address: ' . $externalAddress); + /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Resolved external address: ' . $externalAddress); // Return determined external IP return $externalAddress;