3 * The main class with the entry point to the whole application. This class
4 * "emulates" Java's entry point call. Additionally it covers local
5 * variables from outside access to prevent possible attacks on uninitialized
8 * But good little boys and girls would always initialize their variables... ;-)
10 * @author Roland Haeder <webmaster@ship-simu.org>
12 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
13 * @license GNU GPL 3.0 or any newer version
14 * @link http://www.ship-simu.org
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 final class ApplicationEntryPoint {
33 private static $corePath = '';
36 * The instances we want to remove after all is done
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
50 * The application's emergency exit
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 Whether silent mode is turned on
57 * @todo This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
59 public static final function app_die ($message = '', $code = false, $extraData = '', $silentMode = false) {
60 // Is this method already called?
61 if (isset($GLOBALS['app_die_called'])) {
62 // Then output the text directly
66 // This method shall not be called twice
67 $GLOBALS['app_die_called'] = true;
70 if (empty($message)) {
71 // No message provided
72 $message = 'No message provided!';
75 // Get config instance
76 $configInstance = FrameworkConfiguration::getSelfInstance();
78 // Do we have debug installation?
79 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === true)) {
85 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('web_template_class');
86 $languageInstance = LanguageSystem::getSelfInstance();
88 // Initialize template instance here to avoid warnings in IDE
89 $templateInstance = NULL;
91 // Get response instance
92 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
94 // Is the template engine loaded?
95 if ((class_exists($tpl)) && (is_object($languageInstance))) {
96 // Use the template engine for putting out (nicer look) the message
98 // Get the template instance from our object factory
99 $templateInstance = ObjectFactory::createObjectByName($tpl);
100 } catch (FrameworkException $e) {
101 die(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
106 // Get and prepare backtrace for output
107 $backtraceArray = debug_backtrace();
109 foreach ($backtraceArray as $key => $trace) {
110 // Set missing array elements
111 if (!isset($trace['file'])) {
112 $trace['file'] = __FILE__;
114 if (!isset($trace['line'])) {
115 $trace['line'] = __LINE__;
117 if (!isset($trace['args'])) {
118 $trace['args'] = array();
121 // Add the traceback path to the final output
122 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
123 basename($trace['file']),
126 count($trace['args'])
130 // Init application instance
131 $applicationInstance = NULL;
133 // Is the class there?
134 if (class_exists('ApplicationHelper')) {
135 // Get application instance
136 $applicationInstance = ApplicationHelper::getSelfInstance();
138 // Assign application data
139 $templateInstance->assignApplicationData($applicationInstance);
145 $templateInstance->assignVariable('message', $message);
146 $templateInstance->assignVariable('code', $code);
147 $templateInstance->assignVariable('extra', $extraData);
148 $templateInstance->assignVariable('backtrace', $backtrace);
149 $templateInstance->assignVariable('total_includes', ClassLoader::getSelfInstance()->getTotal());
150 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
151 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
154 $templateInstance->loadCodeTemplate('emergency_exit');
156 // Compile the template
157 $templateInstance->compileTemplate();
159 // Compile all variables
160 $templateInstance->compileVariables();
162 // Transfer data to response
163 $templateInstance->transferToResponse($responseInstance);
165 // Flush the response
166 $responseInstance->flushBuffer();
167 } catch (FileIoException $e) {
168 // Even the template 'emergency_exit' wasn't found so output both message
169 die($message . ', exception: ' . $e->getMessage());
175 // Output message and die
176 die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
183 * Determines the correct absolute path for all includes only once per run.
184 * Other calls of this method are being "cached".
186 * @return $corePath Base path (core) for all includes
188 protected static final function detectCorePath () {
190 if (empty(self::$corePath)) {
191 // Auto-detect our core path
192 self::$corePath = str_replace("\\", '/', dirname(__FILE__));
196 return self::$corePath;
200 * The application's main entry point. This class isolates some local
201 * variables which shall not become visible to outside because of security
202 * concerns. We are doing this here to "emulate" the well-known entry
207 public static final function main () {
209 require(self::detectCorePath() . '/inc/config.php');
211 // Load all include files
212 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
214 // Load all framework classes
215 require($cfg->getConfigEntry('base_path') . 'inc/classes.php');
217 // Include the application selector
218 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
222 // Developer mode active? Comment out if no dev!
223 define('DEVELOPER', true);
225 // Log all exceptions (only debug! This option can create large error logs)
226 //define('LOG_EXCEPTIONS', true);
228 //xdebug_start_trace();
230 // Do not remove the following line:
231 ApplicationEntryPoint::main();