Just a space removed
[shipsimu.git] / index.php
1 <?php
2 // Developer mode active? Comment out if no dev!
3 //define('DEVELOPER', true);
4 //xdebug_start_trace();
5 /**
6  * The main class with the entry point to the whole application. This class
7  * "emulates" Java(tm)'s entry point call. Additionally it covers local
8  * variables from outside access to prevent possible attacks on uninitialized
9  * local variables.
10  *
11  * But good little boys and girls would always initialize their variables... ;-)
12  *
13  * @author              Roland Haeder <webmaster@ship-simu.org>
14  * @version             0.0.0
15  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.ship-simu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class ApplicationEntryPoint {
33         /**
34          * The instances we want to remove after all is done
35          *
36          * @return      void
37          */
38         private static $instances = array (
39                 'cfg',    // The configuration system
40                 'loader', // The class loader system
41                 'debug',  // Debug output
42                 'db',     // Database layer
43                 'io',     // Base I/O system (local file [or network])
44                 'engine', // Template engine ( for ApplicationEntryPoint::app_die() )
45                 'lang',   // Language sub-system
46                 'app',    // The ApplicationHelper instance
47         );
48
49         /**
50          * The application's emergency exit
51          *
52          * @param               $message        The optional message we shall output on exit
53          * @return      void
54          */
55         public static function app_die ($message = "") {
56                 // Is this method already called?
57                 if (defined('EMERGENCY_EXIT_CALLED')) {
58                         // Then output the text directly
59                         die($message);
60                 }
61
62                 // This method shall not be called twice
63                 define('EMERGENCY_EXIT_CALLED', true);
64
65                 // Is a message set?
66                 if (empty($message)) {
67                         // No message provided
68                         $message = "No message provided!";
69                 }
70
71                 // Get some instances
72                 $tpl = FrameworkConfiguration::getInstance()->readConfig('tpl_engine');
73                 $lang = LanguageSystem::getInstance();
74                 $io = FileIoHandler::getInstance();
75
76                 // Is the template engine loaded?
77                 if ((class_exists($tpl)) && (is_object($lang)) && (is_object($io))) {
78                         // Use the template engine for putting out (nicer look) the message
79                         try {
80                                 // Get the template instance from our object factory
81                                 $tplEngine = ObjectFactory::createObjectByConfiguredName('tpl_engine', array(FrameworkConfiguration::getInstance()->readConfig('tpl_base_path'), $lang, $io));
82                         } catch (BasePathIsEmptyException $e) {
83                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
84                                         $e->getMessage()
85                                 ));
86                         } catch (InvalidBasePathStringException $e) {
87                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
88                                         $e->getMessage()
89                                 ));
90                         } catch (BasePathIsNoDirectoryException $e) {
91                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
92                                         $e->getMessage()
93                                 ));
94                         } catch (BasePathReadProtectedException $e) {
95                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
96                                         $e->getMessage()
97                                 ));
98                         }
99
100                         // Backtrace holen und aufbereiten
101                         $backtraceArray = debug_backtrace();
102                         $backtrace = "";
103                         foreach ($backtraceArray as $key=>$trace) {
104                                 if (!isset($trace['file'])) $trace['file'] = __FILE__;
105                                 if (!isset($trace['line'])) $trace['line'] = 5;
106                                 if (!isset($trace['args'])) $trace['args'] = array();
107                                 $backtrace .= "<span class=\"backtrace_file\">".basename($trace['file'])."</span>:".$trace['line'].", <span class=\"backtrace_function\">".$trace['function']."(".count($trace['args']).")</span><br />";
108                         }
109
110                         // Assign variables
111                         $tplEngine->assignVariable('message', $message);
112                         $tplEngine->assignVariable('backtrace', $backtrace);
113                         $tplEngine->assignVariable('total_objects', ObjectFactory::getTotal());
114
115                         // Load the template
116                         $tplEngine->loadCodeTemplate('emergency_exit');
117
118                         // Compile the template
119                         $tplEngine->compileTemplate();
120
121                         // Compile all variables
122                         $tplEngine->compileVariables();
123
124                         // Output all
125                         $tplEngine->output();
126
127                         // Good bye...
128                         exit();
129                 } else {
130                         // Output message and die
131                         die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
132                                 $message
133                         ));
134                 }
135         }
136
137         /**
138          * The application's main entry point. This class isolates some local
139          * variables which shall not become visible to outside because of security
140          * concerns. We are doing this here to "emulate" the well-known entry
141          * point in Java(tm).
142          *
143          * @return      void
144          */
145         public static function main () {
146                 // Some non-global common arrays we need...
147                 global $_SERVER;
148
149                 // Load config file
150                 require(dirname(__FILE__) . '/inc/config.php');
151
152                 // Load all include files
153                 require(PATH . 'inc/includes.php');
154
155                 // Load all framework classes
156                 require(PATH . 'inc/classes.php');
157
158                 // Include the application selector
159                 require(PATH . 'inc/selector.php');
160
161         } // END - main()
162
163 } // END - class
164
165 // Do not remove the following line:
166 ApplicationEntryPoint::main();
167
168 // [EOF]
169 ?>