X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=framework%2Fmain%2Fclasses%2Ftools%2Fconsole%2Fclass_ConsoleTools.php;h=31be16e58418fff5adf4a000c5a41f6261d4f6e3;hb=2218902056efcf9a2c66fe7c24995e066bd7cd11;hp=a5d50ba2a97d05365ad558d6f48bf71b4bd29a27;hpb=24814f48bc7bad92f55e8763bf3f657fb41b131d;p=core.git diff --git a/framework/main/classes/tools/console/class_ConsoleTools.php b/framework/main/classes/tools/console/class_ConsoleTools.php index a5d50ba2..31be16e5 100644 --- a/framework/main/classes/tools/console/class_ConsoleTools.php +++ b/framework/main/classes/tools/console/class_ConsoleTools.php @@ -1,16 +1,17 @@ * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Hub Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.shipsimu.org * @@ -38,7 +39,7 @@ use \SplFileInfo; class ConsoleTools extends BaseFrameworkSystem { // Constants const HTTP_EOL = "\r\n"; - const HTTP_USER_AGENT = 'ConsoleTools/1.0'; + const HTTP_USER_AGENT = 'ConsoleTools/1.0.1'; /** * Default is that this class is noisy @@ -67,8 +68,14 @@ class ConsoleTools extends BaseFrameworkSystem { // Do we have cache? if (!isset($GLOBALS[__METHOD__])) { // Determine it - $GLOBALS[__METHOD__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0)); - } // END - if + $GLOBALS[__METHOD__] = ( + ( + FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_host') != '' + ) && ( + FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_port') > 0 + ) + ); + } // Return cache return $GLOBALS[__METHOD__]; @@ -81,10 +88,23 @@ class ConsoleTools extends BaseFrameworkSystem { * @param $port Port number to connect to * @param $socketResource Resource of a socket * @return $response Response array + * @throws InvalidArgumentException If a parameter is not valid */ - protected function setupProxyTunnel ($host, $port, $socketResource) { + protected function setupProxyTunnel (string $host, int $port, $socketResource) { + // Validate parameter + if (empty($host)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "host" is empty'); + } elseif ($port < 1) { + // Throw IAE + throw new InvalidArgumentException(sprintf('port=%d is not valid', $port)); + } elseif (!is_resource($socketResource)) { + // Throw IAE + throw new InvalidArgumentException(sprintf('socketResource[]=%s is not valid', gettype($socketResource))); + } + // Initialize array - $response = array('', '', ''); + $response = ['', '', '']; $proxyTunnel = ''; // Generate CONNECT request header @@ -93,15 +113,15 @@ class ConsoleTools extends BaseFrameworkSystem { $proxyTunnel .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL; // Use login data to proxy? (username at least!) - if ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') { + if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_username') != '') { // Add it as well - $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->getConfigEntry('proxy_password')); + $encodedAuth = base64_encode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_username') . ':' . FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_password')); $proxyTunnel .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL; - } // END - if + } // Add last new-line $proxyTunnel .= self::HTTP_EOL; - //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: proxyTunnel=' . $proxyTunnel); + //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONSOLE-TOOLS: proxyTunnel=' . $proxyTunnel); // Write request fwrite($socketResource, $proxyTunnel); @@ -110,7 +130,7 @@ class ConsoleTools extends BaseFrameworkSystem { if (feof($socketResource)) { // No response received return $response; - } // END - if + } // Read the first line $resp = trim(fgets($socketResource, 10240)); @@ -118,7 +138,7 @@ class ConsoleTools extends BaseFrameworkSystem { if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) { // Invalid response! return $response; - } // END - if + } // All fine! return $respArray; @@ -129,8 +149,15 @@ class ConsoleTools extends BaseFrameworkSystem { * * @param $rawData Raw data from /etc/hostname file * @return $hostname Extracted host name + * @throws InvalidArgumentException If a parameter is not valid */ - protected function extractHostnameFromRawData ($rawData) { + protected function extractHostnameFromRawData (string $rawData) { + // Validate parameter + if (empty($rawData)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "rawData" is empty'); + } + // Default is invalid $hostname = 'invalid'; @@ -146,7 +173,7 @@ class ConsoleTools extends BaseFrameworkSystem { if (substr($line, 0, 1) == '#') { // Then skip it continue; - } // END - if + } // Has an equals sign? if (strpos($line, '=') !== false) { @@ -157,14 +184,18 @@ class ConsoleTools extends BaseFrameworkSystem { assert(count($hostData) == 2); // Try to get it and abort - $hostname = str_replace(array('"', chr(39)), array('', ''), $hostData[1]); + $hostname = str_replace( + ['"', chr(39)], + ['', ''], + $hostData[1] + ); break; } else { // Use it directly $hostname = $line; break; } - } // END - foreach + } // Return it return $hostname; @@ -176,17 +207,21 @@ class ConsoleTools extends BaseFrameworkSystem { * * @param $hostname Host name we shall resolve * @return $ipAddress IPv4 address resolved from host name + * @throws InvalidArgumentException If a parameter is not valid * @todo This should be connected to a caching class to cache DNS requests */ - public static function resolveIpAddress ($hostname) { - // Quiet? - if (self::$quietResolver !== TRUE) { + public static function resolveIpAddress (string $hostname) { + // Validate parameter + if (empty($hostname)) { + // Throw IAE + throw new InvalidArgumentException('Parameter "hostname" is empty'); + } elseif (self::$quietResolver !== TRUE) { // Debug message self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:] Host name to resolve is: %s', __CLASS__, $hostname )); - } // END - if + } // Default is false $ipAddress = false; @@ -201,7 +236,7 @@ class ConsoleTools extends BaseFrameworkSystem { * other domain, basically. */ $hostname .= '.'; - } // END - if + } // Resolve it // @TODO Here should the cacher be implemented @@ -219,7 +254,7 @@ class ConsoleTools extends BaseFrameworkSystem { __CLASS__, $ipAddress )); - } // END - if + } } 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.', @@ -246,7 +281,7 @@ class ConsoleTools extends BaseFrameworkSystem { $helperInstance = new ConsoleTools(); // Get SplFileInfo instance - $infoInstance = new SplFileInfo($helperInstance->getConfigInstance()->getConfigEntry('hostname_file')); + $infoInstance = new SplFileInfo(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('hostname_file')); try { // Get a file pointer @@ -318,10 +353,10 @@ class ConsoleTools extends BaseFrameworkSystem { // Open connection if ($helperInstance->isProxyUsed() === true) { // Resolve hostname into IP address - $ipAddress = self::resolveIpAddress($helperInstance->getConfigInstance()->getConfigEntry('proxy_host')); + $ipAddress = self::resolveIpAddress(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_host')); // Connect to host through proxy connection - $socketResource = fsockopen($ipAddress, $helperInstance->getConfigInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30); + $socketResource = fsockopen($ipAddress, FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_port'), $errorNo, $errorStr, 30); } else { // Connect to host directly $socketResource = fsockopen('188.138.90.169', 80, $errorNo, $errorStr, 30); @@ -331,7 +366,7 @@ class ConsoleTools extends BaseFrameworkSystem { if ($errorNo > 0) { // Then throw again throw new InvalidSocketException(array($helperInstance, $socketResource, $errorNo, $errorStr), BaseFrameworkSystem::EXCEPTION_INVALID_SOCKET); - } // END - if + } // Prepare the GET request $request = 'GET ' . ($helperInstance->isProxyUsed() === true ? 'http://shipsimu.org' : '') . '/ip.php HTTP/1.0' . self::HTTP_EOL; @@ -342,7 +377,7 @@ class ConsoleTools extends BaseFrameworkSystem { // Do we use proxy? if ($helperInstance->isProxyUsed() === true) { // CONNECT method? - if ($helperInstance->getConfigInstance()->getConfigEntry('proxy_connect_method') == 'Y') { + if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_connect_method') == 'Y') { // Setup proxy tunnel $response = $helperInstance->setupProxyTunnel('shipsimu.org', 80, $socketResource); @@ -350,12 +385,12 @@ class ConsoleTools extends BaseFrameworkSystem { 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)); - } // END - if + } } else { // Add header for proxy $request .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL; } - } // END - if + } // Add last HTTP_EOL $request .= self::HTTP_EOL; @@ -375,17 +410,14 @@ class ConsoleTools extends BaseFrameworkSystem { if ((substr($externalAddress, 0, 7) == 'HTTP/1.') && (substr($externalAddress, -6, 6) != '200 OK')) { // Stop processing break; - } // END - if - } // END - while + } + } // Close socket fclose($socketResource); - - // Debug message - /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: Resolved external address: ' . $externalAddress); - // Return determined external address + /* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('CONSOLE-TOOLS: Resolved external address: ' . $externalAddress); return $externalAddress; } @@ -404,7 +436,7 @@ class ConsoleTools extends BaseFrameworkSystem { if ((isset($_SERVER['REQUEST_METHOD'])) || (isset($_SERVER['QUERY_STRING']))) { // Possibly HTTP request $type = 'http'; - } // END - if + } // Return it return $type; @@ -431,7 +463,7 @@ class ConsoleTools extends BaseFrameworkSystem { if (self::analyzeEnvironmentForType() == 'http') { // Possibly HTTP request $type = 'web'; - } // END - if + } // Return it return $type;