Added very basic support (unfinished) for menu rendering
[core.git] / index.php
1 <?php
2 /**
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
6  * local variables.
7  *
8  * But good little boys and girls would always initialize their variables... ;-)
9  *
10  * @author              Roland Haeder <webmaster@shipsimu.org>
11  * @version             0.0.0
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
15  *
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.
20  *
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.
25  *
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/>.
28  */
29 final class ApplicationEntryPoint {
30         /**
31          * Core path
32          */
33         private static $corePath = '';
34
35         /**
36          * The application's emergency exit
37          *
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
42          * @return      void
43          * @todo        This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
44          */
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
49                         exit($message);
50                 } // END - if
51
52                 // This method shall not be called twice
53                 $GLOBALS['app_die_called'] = TRUE;
54
55                 // Is a message set?
56                 if (empty($message)) {
57                         // No message provided
58                         $message = 'No message provided.';
59                 } // END - if
60
61                 // Get config instance
62                 $configInstance = FrameworkConfiguration::getSelfInstance();
63
64                 // Do we have debug installation?
65                 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === TRUE)) {
66                         // Abort here
67                         exit();
68                 } // END - if
69
70                 // Get some instances
71                 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('html_template_class');
72                 $languageInstance = LanguageSystem::getSelfInstance();
73
74                 // Initialize template instance here to avoid warnings in IDE
75                 $templateInstance = NULL;
76
77                 // Get response instance
78                 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
79
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
83                         try {
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>",
88                                         $e->getMessage()
89                                 ));
90                         }
91
92                         // Get and prepare backtrace for output
93                         $backtraceArray = debug_backtrace();
94                         $backtrace = '';
95                         foreach ($backtraceArray as $key => $trace) {
96                                 // Set missing array elements
97                                 if (!isset($trace['file'])) {
98                                         $trace['file'] = __FILE__;
99                                 } // END - if
100                                 if (!isset($trace['line'])) {
101                                         $trace['line'] = __LINE__;
102                                 } // END - if
103                                 if (!isset($trace['args'])) {
104                                         $trace['args'] = array();
105                                 } // END - if
106
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']),
110                                         $trace['line'],
111                                         $trace['function'],
112                                         count($trace['args'])
113                                 );
114                         } // END - foreach
115
116                         // Init application instance
117                         $applicationInstance = NULL;
118
119                         // Is the class there?
120                         if (class_exists('ApplicationHelper')) {
121                                 // Get application instance
122                                 $applicationInstance = ApplicationHelper::getSelfInstance();
123
124                                 // Assign application data
125                                 $templateInstance->assignApplicationData($applicationInstance);
126                         } // END - if
127
128                         // We only try this
129                         try {
130                                 // Assign variables
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'));
138
139                                 // Load the template
140                                 $templateInstance->loadCodeTemplate('emergency_exit');
141
142                                 // Compile the template
143                                 $templateInstance->compileTemplate();
144
145                                 // Compile all variables
146                                 $templateInstance->compileVariables();
147
148                                 // Transfer data to response
149                                 $templateInstance->transferToResponse($responseInstance);
150
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());
156                         }
157
158                         // Good bye...
159                         exit();
160                 } else {
161                         // Output message and die
162                         exit(sprintf('[Main:] Emergency exit reached: <span class="emergency_span">%s</span>',
163                                 $message
164                         ));
165                 }
166         }
167
168         /**
169          * Determines the correct absolute path for all includes only once per run.
170          * Other calls of this method are being "cached".
171          *
172          * @return      $corePath       Base path (core) for all includes
173          */
174         protected static final function detectCorePath () {
175                 // Is it not set?
176                 if (empty(self::$corePath)) {
177                         // Auto-detect our core path
178                         self::$corePath = str_replace("\\", '/', dirname(__FILE__));
179                 } // END - if
180
181                 // Return it
182                 return self::$corePath;
183         }
184
185         /**
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
189          * point in Java.
190          *
191          * @return      void
192          */
193         public static final function main () {
194                 // Load config file
195                 require(self::detectCorePath() . '/inc/config.php');
196
197                 // Load all include files
198                 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
199
200                 // Load all framework classes
201                 require($cfg->getConfigEntry('base_path') . 'inc/classes.php');
202
203                 // Include the application selector
204                 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
205         } // END - main()
206 } // END - class
207
208 // Developer mode active? Comment out if no dev!
209 define('DEVELOPER', TRUE);
210
211 // Log all exceptions (only debug! This option can create large error logs)
212 //define('LOG_EXCEPTIONS', TRUE);
213
214 //xdebug_start_trace();
215
216 // Do not remove the following line:
217 ApplicationEntryPoint::main();
218
219 // [EOF]
220 ?>