X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fconsole%2Fclass_ConsoleTools.php;h=a8bd7bd11e75e4f0e76d904e96e2a1a28eecfbd3;hb=2e80331da79b13c3094f9039d4c584e3e4b89894;hp=960aaa42a926fcd412721eb60d6b32560f5a0046;hpb=e7040f10e90178e789f97ef7e195b479250e241a;p=core.git diff --git a/inc/classes/main/console/class_ConsoleTools.php b/inc/classes/main/console/class_ConsoleTools.php index 960aaa42..a8bd7bd1 100644 --- a/inc/classes/main/console/class_ConsoleTools.php +++ b/inc/classes/main/console/class_ConsoleTools.php @@ -59,7 +59,7 @@ 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; @@ -123,7 +123,7 @@ class ConsoleTools extends BaseFrameworkSystem { // Add last new-line $proxyTunnel .= self::HTTP_EOL; - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS: proxyTunnel=' . $proxyTunnel); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __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,14 +207,17 @@ class ConsoleTools extends BaseFrameworkSystem { try { // Get a file pointer - $io = FrameworkFileInputPointer::createFrameworkFileInputPointer('/etc/hostname'); + $io = FrameworkFileInputPointer::createFrameworkFileInputPointer($helperInstance->getConfigInstance()->getConfigEntry('hostname_file')); // Read the file - $hostname = trim($io->readFromFile()); + $rawData = trim($io->readFromFile()); // Close the file $io->closeFile(); + // Extract hostname from it + $hostname = $helperInstance->extractHostnameFromRawData($rawData); + // Resolve the IP number $ip = $helperInstance->resolveIpAddress($hostname); } catch (FileIoException $e) { @@ -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(), @@ -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,13 +294,13 @@ 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 = '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 .= '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 @@ -260,7 +309,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 @@ -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[' . __LINE__ . ']: Resolved external address: ' . $externalAddress); // Return determined external IP return $externalAddress;