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