3 * This class contains static helper functions for console applications
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class ConsoleTools extends BaseFrameworkSystem {
26 * Protected constructor
30 protected function __construct () {
31 // Call parent constructor
32 parent::__construct(__CLASS__);
36 * Tries to resolve an IP address from given hostname. Currently only IPv
37 * addresses are resolved.
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
43 protected function resolveIpAddress ($hostname) {
45 $this->debugOutput(sprintf("[%s:] Our host name is: <span class=\"data\">%s</span>",
50 // Default is an invalid one
54 // @TODO Here should the cacher be implemented
55 $ipResolved = gethostbyname($hostname);
58 if (($ipResolved !== false) && ($ipResolved != $hostname)) {
63 $this->debugOutput(sprintf("[%s:] Resolved IP address is: <span class=\"data\">%s</span>\n",
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.",
80 * Aquires the IP address of this host by reading the /etc/hostname file
81 * and solving it. It is now stored in configuration
85 public static function acquireSelfIPAddress () {
86 // Local IP by default
90 $helper = new ConsoleTools();
94 $io = FrameworkFileInputPointer::createFrameworkFileInputPointer('/etc/hostname');
97 $hostname = trim($io->readFromFile());
102 // Resolve the IP number
103 $ip = $helper->resolveIpAddress($hostname);
104 } catch (FileNotFoundException $e) {
105 // Fall-back to 'SESSION_SVR' which found on my Sun Station
106 if (isset($_SERVER['SESSION_SVR'])) {
108 $ip = $helper->resolveIpAddress($_SERVER['SESSION_SVR']);
110 // Could not find our hostname
111 $helper->debugOutput(sprintf("[%s:] WARNING: Cannot resolve my own IP address.",
112 $helper->__toString()
115 } catch (FrameworkException $e) {
116 // Output debug message
117 $helper->debugOutput(sprintf("[%s:] Problem while resolving own IP address: [%s|%s]:%s",
118 $helper->__toString(),
125 // Set it in configuration
126 FrameworkConfiguration::getInstance()->setServerAddress($ip);
130 * Determines own remote IP address (e.g. can be used to probe if we are
131 * reachable from outside by determining external IP and then connect to it.
132 * This is accomblished by connecting to the IP of www.ship-simu.org which
133 * should default to 217.172.186.31.
135 * This method is taken and lightly rewritten from a user comment on php.net:
136 * http://de.php.net/manual/en/function.socket-create.php#49368
138 * @param $dest IP destination we should connect to
139 * @param $port Port destination we should connect to
140 * @return $externalAddress The determined external IP address
141 * @throws InvalidSocketException If socket initialization wents wrong
143 public function determineExternalIp ($dest = '217.172.186.31', $port = 80) {
144 // First get a socket
145 $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
146 if (!is_resource($socket)) {
147 // Throw InvalidSocketException
148 throw new InvalidSocketException (array($this, gettype($socket), 0, 'unknown'));
151 // Connect to the destination
152 socket_connect($socket, $dest, $port);
154 // Get the socket address (our external IP) and port (ignored)
155 socket_getsockname($socket, $externalAddress, $ourPort);
158 socket_close($socket);
160 // Return determined external IP
161 return $externalAddress;