Continued:
[core.git] / index.php
1 <?php
2 // Own namespace (watch out: core)
3 namespace CoreFramework\EntryPoint;
4
5 // Import framework stuff
6 use CoreFramework\Configuration\FrameworkConfiguration;
7
8 /**
9  * The main class with the entry point to the whole application. This class
10  * "emulates" Java's entry point call. Additionally it covers local
11  * variables from outside access to prevent possible attacks on uninitialized
12  * local variables.
13  *
14  * But good little boys and girls would always initialize their variables... ;-)
15  *
16  * @author              Roland Haeder <webmaster@shipsimu.org>
17  * @version             0.0.0
18  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
19  * @license             GNU GPL 3.0 or any newer version
20  * @link                http://www.shipsimu.org
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 final class ApplicationEntryPoint {
36         /**
37          * Core path
38          */
39         private static $corePath = '';
40
41         /**
42          * The application's emergency exit
43          *
44          * @param       $message                The optional message we shall output on exit
45          * @param       $code                   Error code from exception
46          * @param       $extraData              Extra information from exceptions
47          * @param       $silentMode             Whether silent mode is turned on
48          * @return      void
49          * @todo        This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
50          */
51         public static final function app_exit ($message = '', $code = FALSE, $extraData = '', $silentMode = FALSE) {
52                 // Is this method already called?
53                 if (isset($GLOBALS['app_die_called'])) {
54                         // Then output the text directly
55                         exit($message);
56                 } // END - if
57
58                 // This method shall not be called twice
59                 $GLOBALS['app_die_called'] = TRUE;
60
61                 // Is a message set?
62                 if (empty($message)) {
63                         // No message provided
64                         $message = 'No message provided.';
65                 } // END - if
66
67                 // Get config instance
68                 $configInstance = FrameworkConfiguration::getSelfInstance();
69
70                 // Do we have debug installation?
71                 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === TRUE)) {
72                         // Abort here
73                         exit();
74                 } // END - if
75
76                 // Get some instances
77                 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('html_template_class');
78                 $languageInstance = LanguageSystem::getSelfInstance();
79
80                 // Initialize template instance here to avoid warnings in IDE
81                 $templateInstance = NULL;
82
83                 // Get response instance
84                 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
85
86                 // Is the template engine loaded?
87                 if ((class_exists($tpl)) && (is_object($languageInstance))) {
88                         // Use the template engine for putting out (nicer look) the message
89                         try {
90                                 // Get the template instance from our object factory
91                                 $templateInstance = ObjectFactory::createObjectByName($tpl);
92                         } catch (FrameworkException $e) {
93                                 exit(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
94                                         $e->getMessage()
95                                 ));
96                         }
97
98                         // Get and prepare backtrace for output
99                         $backtraceArray = debug_backtrace();
100                         $backtrace = '';
101                         foreach ($backtraceArray as $key => $trace) {
102                                 // Set missing array elements
103                                 if (!isset($trace['file'])) {
104                                         $trace['file'] = __FILE__;
105                                 } // END - if
106                                 if (!isset($trace['line'])) {
107                                         $trace['line'] = __LINE__;
108                                 } // END - if
109                                 if (!isset($trace['args'])) {
110                                         $trace['args'] = array();
111                                 } // END - if
112
113                                 // Add the traceback path to the final output
114                                 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
115                                         basename($trace['file']),
116                                         $trace['line'],
117                                         $trace['function'],
118                                         count($trace['args'])
119                                 );
120                         } // END - foreach
121
122                         // Init application instance
123                         $applicationInstance = NULL;
124
125                         // Is the class there?
126                         if (class_exists('ApplicationHelper')) {
127                                 // Get application instance
128                                 $applicationInstance = ApplicationHelper::getSelfInstance();
129
130                                 // Assign application data
131                                 $templateInstance->assignApplicationData($applicationInstance);
132                         } // END - if
133
134                         // We only try this
135                         try {
136                                 // Assign variables
137                                 $templateInstance->assignVariable('message', $message);
138                                 $templateInstance->assignVariable('code', $code);
139                                 $templateInstance->assignVariable('extra', $extraData);
140                                 $templateInstance->assignVariable('backtrace', $backtrace);
141                                 $templateInstance->assignVariable('total_includes', ClassLoader::getSelfInstance()->getTotal());
142                                 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
143                                 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
144
145                                 // Load the template
146                                 $templateInstance->loadCodeTemplate('emergency_exit');
147
148                                 // Compile the template
149                                 $templateInstance->compileTemplate();
150
151                                 // Compile all variables
152                                 $templateInstance->compileVariables();
153
154                                 // Transfer data to response
155                                 $templateInstance->transferToResponse($responseInstance);
156
157                                 // Flush the response
158                                 $responseInstance->flushBuffer();
159                         } catch (FileNotFoundException $e) {
160                                 // Even the template 'emergency_exit' wasn't found so output both message
161                                 exit($message . ', exception: ' . $e->getMessage());
162                         }
163
164                         // Good bye...
165                         exit();
166                 } else {
167                         // Output message and die
168                         exit(sprintf('[Main:] Emergency exit reached: <span class="emergency_span">%s</span>',
169                                 $message
170                         ));
171                 }
172         }
173
174         /**
175          * Determines the correct absolute path for all includes only once per run.
176          * Other calls of this method are being "cached".
177          *
178          * @return      $corePath       Base path (core) for all includes
179          */
180         protected static final function detectCorePath () {
181                 // Is it not set?
182                 if (empty(self::$corePath)) {
183                         // Auto-detect our core path
184                         self::$corePath = str_replace("\\", '/', dirname(__FILE__));
185                 } // END - if
186
187                 // Return it
188                 return self::$corePath;
189         }
190
191         /**
192          * The framework's main entry point. This class isolates some local
193          * variables which shall not become visible to outside because of security
194          * concerns. This is done here to "emulate" the well-known entry point in
195          * Java.
196          *
197          * @return      void
198          */
199         public static final function main () {
200                 // Load config file, this provides $cfg
201                 require(self::detectCorePath() . '/inc/config.php');
202
203                 // Get a new configuration instance
204                 $cfg = FrameworkConfiguration::getSelfInstance();
205
206                 // Load bootstrap class
207                 require($cfg->getConfigEntry('base_path') . 'inc/bootstrap/class_BootstrapFramework.php');
208
209                 // ----- Below is deprecated -----
210
211                 // Load all include files
212                 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
213
214                 // Include the application selector
215                 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
216         }
217 }
218
219 // Developer mode active? Comment out if no dev!
220 define('DEVELOPER', TRUE);
221
222 // Log all exceptions (only debug! This option can create large error logs)
223 //define('LOG_EXCEPTIONS', TRUE);
224
225 //xdebug_start_trace();
226
227 // Call above main() method
228 ApplicationEntryPoint::main();
229
230 // [EOF]
231 ?>