2 // Developer mode active? Comment out if no dev!
3 define('DEVELOPER', true);
4 //xdebug_start_trace();
6 * The main class with the entry point to the whole application. This class
7 * "emulates" Java's entry point call. Additionally it covers local
8 * variables from outside access to prevent possible attacks on uninitialized
11 * But good little boys and girls would always initialize their variables... ;-)
13 * @author Roland Haeder <webmaster@ship-simu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.ship-simu.org
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.
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.
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/>.
32 final class ApplicationEntryPoint {
36 private static $corePath = '';
39 * The instances we want to remove after all is done
43 private static $instances = array (
44 'cfg', // The configuration system
45 'loader', // The class loader system
46 'debug', // Debug output
47 'db', // Database layer
48 'io', // Base I/O system (local file [or network])
49 'engine', // Template engine ( for ApplicationEntryPoint::app_die() )
50 'lang', // Language sub-system
51 'app', // The ApplicationHelper instance
55 * The application's emergency exit
57 * @param $message The optional message we shall output on exit
58 * @param $code Error code from exception
59 * @param $extraData Extra information from exceptions
60 * @param $silentMode Wether not silent mode is turned on
63 public static function app_die ($message = '', $code = false, $extraData = '', $silentMode = false) {
64 // Is this method already called?
65 if (isset($GLOBALS['app_die_called'])) {
66 // Then output the text directly
70 // This method shall not be called twice
71 $GLOBALS['app_die_called'] = true;
74 if (empty($message)) {
75 // No message provided
76 $message = 'No message provided!';
79 // Get config instance
80 $configInstance = FrameworkConfiguration::getInstance();
82 // Do we have debug installation?
83 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === true)) {
89 $tpl = FrameworkConfiguration::getInstance()->getConfigEntry('web_template_class');
90 $languageInstance = LanguageSystem::getInstance();
92 // Get response instance
93 $responseInstance = ApplicationHelper::getInstance()->getResponseInstance();
95 // Is the template engine loaded?
96 if ((class_exists($tpl)) && (is_object($languageInstance))) {
97 // Use the template engine for putting out (nicer look) the message
99 // Get the template instance from our object factory
100 $templateInstance = ObjectFactory::createObjectByName($tpl, array(ApplicationHelper::getInstance()));
101 } catch (FrameworkException $e) {
102 die(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
107 // Get and prepare backtrace for output
108 $backtraceArray = debug_backtrace();
110 foreach ($backtraceArray as $key => $trace) {
111 if (!isset($trace['file'])) $trace['file'] = __FILE__;
112 if (!isset($trace['line'])) $trace['line'] = __LINE__;
113 if (!isset($trace['args'])) $trace['args'] = array();
114 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
115 basename($trace['file']),
118 count($trace['args'])
122 // Init application instance
125 // Is the class there?
126 if (class_exists('ApplicationHelper')) {
127 // Get application instance
128 $appInstance = ApplicationHelper::getInstance();
130 // Assign application data
131 $templateInstance->assignApplicationData($appInstance);
135 $templateInstance->assignVariable('message', $message);
136 $templateInstance->assignVariable('code', $code);
137 $templateInstance->assignVariable('extra', $extraData);
138 $templateInstance->assignVariable('backtrace', $backtrace);
139 $templateInstance->assignVariable('total_includes', ClassLoader::getInstance()->getTotal());
140 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
141 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
144 $templateInstance->loadCodeTemplate('emergency_exit');
146 // Compile the template
147 $templateInstance->compileTemplate();
149 // Compile all variables
150 $templateInstance->compileVariables();
152 // Transfer data to response
153 $templateInstance->transferToResponse($responseInstance);
155 // Flush the response
156 $responseInstance->flushBuffer();
161 // Output message and die
162 die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
169 * Determines the correct absolute path for all includes only once per run.
170 * Other calls of this method are being "cached".
172 * @return $basePath Base path (core) for all includes
174 protected static function detectCorePath () {
176 if (empty(self::$corePath)) {
177 // Auto-detect our core path
178 self::$corePath = str_replace("\\", '/', dirname(__FILE__));
182 return self::$corePath;
186 * The application's main entry point. This class isolates some local
187 * variables which shall not become visible to outside because of security
188 * concerns. We are doing this here to "emulate" the well-known entry
193 public static function main () {
195 require(self::detectCorePath() . '/inc/config.php');
197 // Load all include files
198 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
200 // Load all framework classes
201 require($cfg->getConfigEntry('base_path') . 'inc/classes.php');
203 // Include the application selector
204 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
208 // Do not remove the following line:
209 ApplicationEntryPoint::main();