<?php
// Own namespace
-namespace CoreFramework\Tools;
+namespace CoreFramework\Console\Tools;
// Import framework stuff
use CoreFramework\Configuration\FrameworkConfiguration;
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
*
} // 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);
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();
$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.',
}
// Set it in configuration
- FrameworkConfiguration::getSelfInstance()->setServerAddress($ip);
+ FrameworkConfiguration::getSelfInstance()->setServerAddress($ipAddress);
// Return it
- return $ip;
+ return $ipAddress;
}
/**
// @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);
} // 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
// 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
$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