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