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          * @param       $code                   Error code from exception
54          * @param       $extraData              Extra information from exceptions
55          * @param       $silentMode             Wether not silent mode is turned on
56          * @return      void
57          */
58         public static function app_die ($message = "", $code = false, $extraData = "", $silentMode = false) {
59                 // Is this method already called?
60                 if (defined('EMERGENCY_EXIT_CALLED')) {
61                         // Then output the text directly
62                         die($message);
63                 } // END - if
64
65                 // This method shall not be called twice
66                 define('EMERGENCY_EXIT_CALLED', true);
67
68                 // Is a message set?
69                 if (empty($message)) {
70                         // No message provided
71                         $message = "No message provided!";
72                 } // END - if
73
74                 // Get config instance
75                 $configInstance = FrameworkConfiguration::getInstance();
76
77                 // Do we have debug installation?
78                 if (($configInstance->readConfig('product_install_mode') == "productive") || ($silentMode === true)) {
79                         // Abort here
80                         die();
81                 } // END - if
82
83                 // Get some instances
84                 $tpl = FrameworkConfiguration::getInstance()->readConfig('template_class');
85                 $lang = LanguageSystem::getInstance();
86                 $io = FileIoHandler::getInstance();
87
88                 // Get response instance
89                 $responseInstance = ApplicationHelper::getInstance()->getResponseInstance();
90
91                 // Is the template engine loaded?
92                 if ((class_exists($tpl)) && (is_object($lang)) && (is_object($io))) {
93                         // Use the template engine for putting out (nicer look) the message
94                         try {
95                                 // Get the template instance from our object factory
96                                 $templateInstance = ObjectFactory::createObjectByName($tpl, array(FrameworkConfiguration::getInstance()->readConfig('tpl_base_path'), $lang, $io));
97                         } catch (FrameworkException $e) {
98                                 die(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
99                                         $e->getMessage()
100                                 ));
101                         }
102
103                         // Get and prepare backtrace for output
104                         $backtraceArray = debug_backtrace();
105                         $backtrace = "";
106                         foreach ($backtraceArray as $key => $trace) {
107                                 if (!isset($trace['file'])) $trace['file'] = __FILE__;
108                                 if (!isset($trace['line'])) $trace['line'] = __LINE__;
109                                 if (!isset($trace['args'])) $trace['args'] = array();
110                                 $backtrace .= "<span class=\"backtrace_file\">".basename($trace['file'])."</span>:".$trace['line'].", <span class=\"backtrace_function\">".$trace['function']."(".count($trace['args']).")</span><br />";
111                         } // END - foreach
112
113                         // Init application instance
114                         $appInstance = null;
115
116                         // Is the class there?
117                         if (class_exists('ApplicationHelper')) {
118                                 // Get application instance
119                                 $appInstance = ApplicationHelper::getInstance();
120
121                                 // Assign application data
122                                 $templateInstance->assignApplicationData($appInstance);
123                         } // END - if
124
125                         // Assign variables
126                         $templateInstance->assignVariable('message', $message);
127                         $templateInstance->assignVariable('code', $code);
128                         $templateInstance->assignVariable('extra', $extraData);
129                         $templateInstance->assignVariable('backtrace', $backtrace);
130                         $templateInstance->assignVariable('total_includes', ClassLoader::getInstance()->getTotal());
131                         $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
132                         $templateInstance->assignVariable('title', $lang->getMessage('emergency_exit_title'));
133
134                         // Load the template
135                         $templateInstance->loadCodeTemplate('emergency_exit');
136
137                         // Compile the template
138                         $templateInstance->compileTemplate();
139
140                         // Compile all variables
141                         $templateInstance->compileVariables();
142
143                         // Transfer data to response
144                         $templateInstance->transferToResponse($responseInstance);
145
146                         // Flush the response
147                         $responseInstance->flushBuffer();
148
149                         // Good bye...
150                         exit();
151                 } else {
152                         // Output message and die
153                         die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
154                                 $message
155                         ));
156                 }
157         }
158
159         /**
160          * The application's main entry point. This class isolates some local
161          * variables which shall not become visible to outside because of security
162          * concerns. We are doing this here to "emulate" the well-known entry
163          * point in Java(tm).
164          *
165          * @return      void
166          */
167         public static function main () {
168                 // Some non-global common arrays we need...
169                 global $_SERVER;
170
171                 // Load config file
172                 require(dirname(__FILE__) . '/inc/config.php');
173
174                 // Load all include files
175                 require($cfg->readConfig('base_path') . 'inc/includes.php');
176
177                 // Load all framework classes
178                 require($cfg->readConfig('base_path') . 'inc/classes.php');
179
180                 // Include the application selector
181                 require($cfg->readConfig('base_path') . 'inc/selector.php');
182         } // END - main()
183
184 } // END - class
185
186 // Do not remove the following line:
187 ApplicationEntryPoint::main();
188
189 // [EOF]
190 ?>