+ /**
+ * Checks wether proxy configuration is used
+ *
+ * @return $isUsed Wether proxy is used
+ */
+ protected function isProxyUsed () {
+ // 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
+
+ // Return cache
+ return $GLOBALS[__METHOD__];
+ }
+
+ /**
+ * Sets up a proxy tunnel for given hostname and through resource
+ *
+ * @param $host Host to connect to
+ * @param $port Port number to connect to
+ * @param $socketResource Resource of a socket
+ * @return $response Response array
+ */
+ protected function setupProxyTunnel ($host, $port, $socketResource) {
+ // Initialize array
+ $response = array('', '', '');
+ $proxyTunnel = '';
+
+ // Generate CONNECT request header
+ $proxyTunnel .= 'CONNECT ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL;
+ $proxyTunnel .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL;
+ $proxyTunnel .= 'Proxy-Connection: Keep-Alive' . self::HTTP_EOL;
+
+ // Use login data to proxy? (username at least!)
+ if ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') {
+ // Add it as well
+ $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->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__)->debugOutput('CONSOLE-TOOLS[' . __METHOD__ . ':' . __LINE__ . ']: proxyTunnel=' . $proxyTunnel);
+
+ // Write request
+ fwrite($socketResource, $proxyTunnel);
+
+ // Got response?
+ if (feof($socketResource)) {
+ // No response received
+ return $response;
+ } // END - if
+
+ // Read the first line
+ $resp = trim(fgets($socketResource, 10240));
+ $respArray = explode(' ', $resp);
+ 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;
+ }
+
+ /**
+ * 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;
+ }
+