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, this is free software
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 (defined('EMERGENCY_EXIT_CALLED')) {
66 // Then output the text directly
70 // This method shall not be called twice
71 define('EMERGENCY_EXIT_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->readConfig('product_install_mode') == 'productive') || ($silentMode === true)) {
89 $tpl = FrameworkConfiguration::getInstance()->readConfig('template_class');
90 $lang = LanguageSystem::getInstance();
92 // Get response instance
93 $responseInstance = ApplicationHelper::getInstance()->getResponseInstance();
95 // Is the template engine loaded?
96 if ((class_exists($tpl)) && (is_object($lang))) {
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 .= "<span class=\"backtrace_file\">".basename($trace['file'])."</span>:".$trace['line'].", <span class=\"backtrace_function\">".$trace['function']."(".count($trace['args']).")</span><br />";
117 // Init application instance
120 // Is the class there?
121 if (class_exists('ApplicationHelper')) {
122 // Get application instance
123 $appInstance = ApplicationHelper::getInstance();
125 // Assign application data
126 $templateInstance->assignApplicationData($appInstance);
130 $templateInstance->assignVariable('message', $message);
131 $templateInstance->assignVariable('code', $code);
132 $templateInstance->assignVariable('extra', $extraData);
133 $templateInstance->assignVariable('backtrace', $backtrace);
134 $templateInstance->assignVariable('total_includes', ClassLoader::getInstance()->getTotal());
135 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
136 $templateInstance->assignVariable('title', $lang->getMessage('emergency_exit_title'));
139 $templateInstance->loadCodeTemplate('emergency_exit');
141 // Compile the template
142 $templateInstance->compileTemplate();
144 // Compile all variables
145 $templateInstance->compileVariables();
147 // Transfer data to response
148 $templateInstance->transferToResponse($responseInstance);
150 // Flush the response
151 $responseInstance->flushBuffer();
156 // Output message and die
157 die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
164 * Determines the correct absolute path for all includes
166 * @return $basePath Base path (core) for all includes
168 protected static function detectCorePath () {
170 if (empty(self::$corePath)) {
171 // Auto-detect our core path
172 self::$corePath = str_replace("\\", '/', dirname(__FILE__));
176 return self::$corePath;
180 * The application's main entry point. This class isolates some local
181 * variables which shall not become visible to outside because of security
182 * concerns. We are doing this here to "emulate" the well-known entry
187 public static function main () {
189 require(self::detectCorePath() . '/inc/config.php');
191 // Load all include files
192 require($cfg->readConfig('base_path') . 'inc/includes.php');
194 // Load all framework classes
195 require($cfg->readConfig('base_path') . 'inc/classes.php');
197 // Include the application selector
198 require($cfg->readConfig('base_path') . 'inc/selector.php');
203 // Do not remove the following line:
204 ApplicationEntryPoint::main();