]> git.mxchange.org Git - hub.git/blob - application/hub/main/tools/class_HubTools.php
4525f503e2ae72707d1c2da747c4f9f28b5a068a
[hub.git] / application / hub / main / tools / class_HubTools.php
1 <?php
2 /**
3  * This class contains static helper functions for our hub
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009, 2010 Hub 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 HubTools extends BaseFrameworkSystem {
25         // Constants for exceptions
26         const EXCEPTION_SESSION_ID_IS_INVALID = 0x200;
27
28         /**
29          * Cache for session ids
30          */
31         private $sessionIdCache = array();
32
33         /**
34          * Length for session id (should be 32+salt_length
35          */
36         private $sessionIdLength = 0;
37
38         /**
39          * Self instance
40          */
41         private static $selfInstance = null;
42
43         /**
44          * Protected constructor
45          *
46          * @return      void
47          */
48         protected function __construct () {
49                 // Call parent constructor
50                 parent::__construct(__CLASS__);
51
52                 // Init salt length
53                 $this->sessionIdLength = 32 + $this->getConfigInstance()->getConfigEntry('salt_length');
54         }
55
56         /**
57          * Singleton getter for self instance
58          *
59          * @retuen      $selfInstance   An instance of this class
60          */
61         public final static function getInstance () {
62                 // Is the instance set
63                 if (is_null(self::$selfInstance)) {
64                         // Then set it
65                         self::$selfInstance = new HubTools();
66                 } // END - if
67
68                 // Return own instance
69                 return self::$selfInstance;
70         }
71
72         /**
73          * Getter for session id length
74          *
75          * @return      $sessionIdLength        Length of session ids
76          */
77         protected final function getSessionIdLength () {
78                 return $this->sessionIdLength;
79         }
80
81         /**
82          * Resolves a session id into an ip:port combination
83          *
84          * @param       $sessionId      A valid session id
85          * @return      $recipient      Recipient as ip:port combination
86          */
87         protected function resolveIpPortBySessionId ($sessionId) {
88                 // Get a wrapper instance
89                 $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class');
90
91                 // And ask it for the session id
92                 $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId);
93
94                 // Return result
95                 return $recipient;
96         }
97
98         /**
99          * Resolves given session id into an ip:port combination, if ip:port is set, it won't be translated
100          *
101          * @param       $sessionId      Session id or ip:port combination
102          * @return      $recipient      Recipient as ip:port combination
103          * @throws      InvalidSessionIdException       If the provided session id is invalid (and no ip:port combination)
104          */
105         public static function resolveSessionId ($sessionId) {
106                 // Get an own instance
107                 $selfInstance = self::getInstance();
108
109                 // Default is direct ip:port
110                 $recipient = $sessionId;
111
112                 // Does it match a direct ip:port? (hint: see www.regexlib.com for the regular expression)
113                 if (!preg_match('/((?:2[0-5]{2}|1\d{2}|[1-9]\d|[1-9])\.(?:(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)\.){2}(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)):(\d|[1-9]\d|[1-9]\d{2,3}|[1-5]\d{4}|6[0-4]\d{3}|654\d{2}|655[0-2]\d|6553[0-5])/', $sessionId)) {
114                         // Is it in cache?
115                         if (isset($selfInstance->sessionIdCache[$sessionId])) {
116                                 // Debug message
117                                 $selfInstance->debugOutput('HUB-TOOLS: Using entry from sessionIdCache[] array.');
118
119                                 // Then use it
120                                 $recipient = $selfInstance->sessionIdCache[$sessionId];
121                         } elseif (!preg_match('/([a-f0-9]{' . $selfInstance->getSessionIdLength() . '})/', $sessionId)) {
122                                 // Invalid session id
123                                 throw new InvalidSessionIdException($sessionId, self::EXCEPTION_SESSION_ID_IS_INVALID);
124                         } else {
125                                 // Debug message
126                                 $selfInstance->debugOutput('HUB-TOOLS: Using internal resolver.');
127
128                                 // Resolve it here
129                                 $recipient = $selfInstance->resolveIpPortBySessionId($sessionId);
130                         }
131                 } // END - if
132
133                 // Output message
134                 $selfInstance->debugOutput('HUB-TOOLS: Session id ' . $sessionId . ' resolved to ' . $recipient);
135
136                 // Return it
137                 return $recipient;
138         }
139 }
140
141 // [EOF]
142 ?>