Tried to write an hostname extraction methods tries to get rid of comment lines ...
authorRoland Häder <roland@mxchange.org>
Fri, 22 Feb 2013 01:33:30 +0000 (01:33 +0000)
committerRoland Häder <roland@mxchange.org>
Fri, 22 Feb 2013 01:33:30 +0000 (01:33 +0000)
inc/classes/main/console/class_ConsoleTools.php

index 43d2503f62e1e80761fe91b1dad73d6894159c84..cedf677e127e30baac71133b6ecc83d57ff9827e 100644 (file)
@@ -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($trim);
+
+                       // 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
@@ -164,11 +210,14 @@ class ConsoleTools extends BaseFrameworkSystem {
                        $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) {