/**
* 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 - 2012 Core Developer Team
+ * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 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
$ipResolved = gethostbyname($hostname);
// Was it fine?
- if (($ipResolved !== false) && ($ipResolved != $hostname)) {
+ if (($ipResolved !== FALSE) && ($ipResolved != $hostname)) {
// Okay, this works!
$ip = $ipResolved;
// Add last new-line
$proxyTunnel .= self::HTTP_EOL;
- //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS: proxyTunnel=' . $proxyTunnel);
+ //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: proxyTunnel=' . $proxyTunnel);
// Write request
fwrite($socketResource, $proxyTunnel);
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
try {
// Get a file pointer
- $io = FrameworkFileInputPointer::createFrameworkFileInputPointer($helperInstance->getConfigInstance()->getConfigEntry('hostname_file'));
+ $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);
/**
* 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
+ * 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.
* Compare to following link:
* http://de.php.net/manual/en/function.socket-create.php#49368
*
- * @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 static function determineExternalIp () {
+ public static function determineExternalAddress () {
// Get helper instance
$helperInstance = new ConsoleTools();
// @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'));
} // 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
// Debug message
- /* DEBUG: */ self::createDebugInstance(__CLASS__)->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 determined external address
return $externalAddress;
}