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