e00e757e8100211028ed9c0c16dc5051ae697420
[core.git] / inc / classes / main / console / class_ConsoleTools.php
1 <?php
2 /**
3  * This class contains static helper functions for console applications
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24 class ConsoleTools extends BaseFrameworkSystem {
25         /**
26          * Protected constructor
27          *
28          * @return      void
29          */
30         protected function __construct () {
31                 // Call parent constructor
32                 parent::__construct(__CLASS__);
33         }
34
35         /**
36          * Tries to resolve an IP address from given hostname. Currently only IPv
37          * addresses are resolved.
38          *
39          * @param       $hostname       Host name we shall resolve
40          * @return      $ip                     IP address resolved from host name
41          * @todo        We should connect this to a caching class to cache DNS requests
42          */
43         protected function resolveIpAddress ($hostname) {
44                 // Debug message
45                 $this->debugOutput(sprintf("[%s:] Our host name is: <span class=\"data\">%s</span>",
46                         $this->__toString(),
47                         $hostname
48                 ));
49
50                 // Default is an invalid one
51                 $ip = '0.0.0.0';
52
53                 // Resolve it
54                 // @TODO Here should the cacher be implemented
55                 $ipResolved = gethostbyname($hostname);
56
57                 // Was it fine?
58                 if (($ipResolved !== false) && ($ipResolved != $hostname)) {
59                         // Okay, this works!
60                         $ip = $ipResolved;
61
62                         // Debug message
63                         $this->debugOutput(sprintf("[%s:] Resolved IP address is: <span class=\"data\">%s</span>\n",
64                                 $this->__toString(),
65                                 $ip
66                         ));
67                 } else {
68                         // Problem while resolving IP address
69                         $this->debugOutput(sprintf("[%s:] Problem resolving IP address for host <span class=\"data\">%s</span>. Please check your /etc/hosts file.",
70                                 $this->__toString(),
71                                 $hostname
72                         ));
73                 }
74
75                 // Return resolved IP
76                 return $ip;
77         }
78
79         /**
80          * Aquires the IP address of this host by reading the /etc/hostname file and solving it
81          *
82          * @return      $ip             The resolved IP address
83          */
84         public static function acquireSelfIPAddress () {
85                 // Local IP by default
86                 $ip = '127.0.0.1';
87
88                 // Get a new instance
89                 $helper = new ConsoleTools();
90
91                 try {
92                         // Get a file pointer
93                         $io = FrameworkFileInputPointer::createFrameworkFileInputPointer('/etc/hostname');
94
95                         // Read the file
96                         $hostname = trim($io->readFromFile());
97
98                         // Close the file
99                         $io->closeFile();
100
101                         // Resolve the IP number
102                         $ip = $helper->resolveIpAddress($hostname);
103                 } catch (FileNotFoundException $e) {
104                         // Fall-back to 'SESSION_SVR' which found on my Sun Station
105                         if (isset($_SERVER['SESSION_SVR'])) {
106                                 // Resolve it
107                                 $ip = $helper->resolveIpAddress($_SERVER['SESSION_SVR']);
108                         } else {
109                                 // Could not find our hostname
110                                 $helper->debugOutput(sprintf("[%s:] WARNING: Cannot resolve my own IP address.",
111                                         $helper->__toString()
112                                 ));
113                         }
114                 } catch (FrameworkException $e) {
115                         // Output debug message
116                         $helper->debugOutput(sprintf("[%s:] Problem while resolving own IP address: [%s|%s]:%s",
117                                 $helper->__toString(),
118                                 $e->__toString(),
119                                 $e->getHexCode(),
120                                 $e->getMessage()
121                         ));
122                 }
123
124                 // Return the IP address
125                 return $ip;
126         }
127
128         /**
129          * Determines own remote IP address (e.g. can be used to probe if we are
130          * reachable from outside by determining external IP and then connect to it.
131          * This is accomblished by connecting to the IP of www.ship-simu.org which
132          * should default to 217.172.186.31.
133          *
134          * This method is taken and lightly rewritten from a user comment on php.net:
135          * http://de.php.net/manual/en/function.socket-create.php#49368
136          *
137          * @param       $dest                           IP destination we should connect to
138          * @param       $port                           Port destination we should connect to
139          * @return      $externalAddress        The determined external IP address
140          * @throws      InvalidSocketException  If socket initialization wents wrong
141          */
142         public function determineExternalIp ($dest = '217.172.186.31', $port = 80) {
143                 // First get a socket
144                 $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
145                 if (!is_resource($socket)) {
146                         // Throw InvalidSocketException
147                         throw new InvalidSocketException (array($this, gettype($socket), 0, 'unknown'));
148                 } // END - if
149
150                 // Connect to the destination
151                 socket_connect($socket, $dest, $port);
152
153                 // Get the socket address (our external IP) and port (ignored)
154                 socket_getsockname($socket, $externalAddress, $ourPort);
155
156                 // Close the socket
157                 socket_close($socket);
158
159                 // Return determined external IP
160                 return $externalAddress;
161         }
162 }
163
164 // [EOF]
165 ?>