]> git.mxchange.org Git - mailer.git/blob - inc/classes/main/console/class_ConsoleTools.php
0.3.0 inital import
[mailer.git] / inc / classes / main / console / class_ConsoleTools.php
1 <?php
2 /**
3  * This class contains static helper functions for console applications
4  */
5 class ConsoleTools extends BaseFrameworkSystem {
6         /**
7          * Private constructor
8          *
9          * @return      void
10          */
11         private function __construct () {
12                 // Call parent constructor
13                 parent::constructor(__CLASS__);
14
15                 // Set description
16                 $this->setPartDescr("Console-Tools");
17
18                 // Create an unique ID
19                 $this->createUniqueID();
20         }
21
22         /**
23          * Aquires the IP address of this host by reading the /etc/hostname file and solving it
24          *
25          * @return      $ip             The resolved IP address
26          */
27         public static function aquireSelfIPAddress () {
28                 // Local IP by default
29                 $ip = "127.0.0.1";
30
31                 // Get a new instance
32                 $helper = new ConsoleTools();
33
34                 try {
35                         // Get a file pointer
36                         $io = FrameworkFileInputPointer::createFrameworkFileInputPointer("/etc/hostname");
37
38                         // Read the file
39                         $hostname = trim($io->readFromFile());
40                         $helper->getDebugInstance()->output(sprintf("[%s:] Our host name is: <strong>%s</strong><br />\n",
41                                 $helper->__toString(),
42                                 $hostname
43                         ));
44
45                         // Close the file
46                         $io->closeFile();
47
48                         // Resolve it
49                         $ipResolved = gethostbyname($hostname);
50                         if (($ipResolved !== false) && ($ipResolved != $hostname)) {
51                                 // Okay, this works!
52                                 $ip = $ipResolved;
53
54                                 // Debug message
55                                 $helper->getDebugInstance()->output(sprintf("[%s:] Resolved IP address is: <strong>%s</strong><br />\n",
56                                         $helper->__toString(),
57                                         $ip
58                                 ));
59                         }
60                 } catch (FrameworkException $e) {
61                         // Do nothing here
62                 }
63
64                 // Return the IP address
65                 return $ip;
66         }
67 }
68
69 // [EOF]
70 ?>