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@shipsimu.org>
12 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
13 * @license GNU GPL 3.0 or any newer version
14 * @link http://www.shipsimu.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 application's emergency exit
38 * @param $message The optional message we shall output on exit
39 * @param $code Error code from exception
40 * @param $extraData Extra information from exceptions
41 * @param $silentMode Whether silent mode is turned on
43 * @todo This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
45 public static final function app_exit ($message = '', $code = FALSE, $extraData = '', $silentMode = FALSE) {
46 // Is this method already called?
47 if (isset($GLOBALS['app_die_called'])) {
48 // Then output the text directly
52 // This method shall not be called twice
53 $GLOBALS['app_die_called'] = TRUE;
56 if (empty($message)) {
57 // No message provided
58 $message = 'No message provided.';
61 // Get config instance
62 $configInstance = FrameworkConfiguration::getSelfInstance();
64 // Do we have debug installation?
65 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === TRUE)) {
71 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('html_template_class');
72 $languageInstance = LanguageSystem::getSelfInstance();
74 // Initialize template instance here to avoid warnings in IDE
75 $templateInstance = NULL;
77 // Get response instance
78 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
80 // Is the template engine loaded?
81 if ((class_exists($tpl)) && (is_object($languageInstance))) {
82 // Use the template engine for putting out (nicer look) the message
84 // Get the template instance from our object factory
85 $templateInstance = ObjectFactory::createObjectByName($tpl);
86 } catch (FrameworkException $e) {
87 exit(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
92 // Get and prepare backtrace for output
93 $backtraceArray = debug_backtrace();
95 foreach ($backtraceArray as $key => $trace) {
96 // Set missing array elements
97 if (!isset($trace['file'])) {
98 $trace['file'] = __FILE__;
100 if (!isset($trace['line'])) {
101 $trace['line'] = __LINE__;
103 if (!isset($trace['args'])) {
104 $trace['args'] = array();
107 // Add the traceback path to the final output
108 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
109 basename($trace['file']),
112 count($trace['args'])
116 // Init application instance
117 $applicationInstance = NULL;
119 // Is the class there?
120 if (class_exists('ApplicationHelper')) {
121 // Get application instance
122 $applicationInstance = ApplicationHelper::getSelfInstance();
124 // Assign application data
125 $templateInstance->assignApplicationData($applicationInstance);
131 $templateInstance->assignVariable('message', $message);
132 $templateInstance->assignVariable('code', $code);
133 $templateInstance->assignVariable('extra', $extraData);
134 $templateInstance->assignVariable('backtrace', $backtrace);
135 $templateInstance->assignVariable('total_includes', ClassLoader::getSelfInstance()->getTotal());
136 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
137 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
140 $templateInstance->loadCodeTemplate('emergency_exit');
142 // Compile the template
143 $templateInstance->compileTemplate();
145 // Compile all variables
146 $templateInstance->compileVariables();
148 // Transfer data to response
149 $templateInstance->transferToResponse($responseInstance);
151 // Flush the response
152 $responseInstance->flushBuffer();
153 } catch (FileIoException $e) {
154 // Even the template 'emergency_exit' wasn't found so output both message
155 exit($message . ', exception: ' . $e->getMessage());
161 // Output message and die
162 exit(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 $corePath Base path (core) for all includes
174 protected static final 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 final function main () {
195 require(self::detectCorePath() . '/inc/config.php');
197 // Load all include files
198 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
200 // Include the application selector
201 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
205 // Developer mode active? Comment out if no dev!
206 define('DEVELOPER', TRUE);
208 // Log all exceptions (only debug! This option can create large error logs)
209 //define('LOG_EXCEPTIONS', TRUE);
211 //* DEBUG: */ xdebug_start_trace();
213 // Do not remove the following line:
214 ApplicationEntryPoint::main();