8a0bec3be1dd52b9dad0a75d128df578d7a336de
[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". This is done by checking
181          * a small list of common paths where the framework can reside and check if
182          * inc/config.php can be found.
183          *
184          * @return      $corePath       Base path (core) for all includes
185          */
186         protected static final function detectCorePath () {
187                 // Is it not set?
188                 if (empty(self::$corePath)) {
189                         // Auto-detect core path (first application-common)
190                         foreach (array('core', '.') as $possiblePath) {
191                                 // Create full path for testing
192                                 $realPath = realpath($possiblePath);
193
194                                 // Debug message
195                                 //* NOISY-DEBUG: */ printf('[%s:%d]: realPath[%s]=%s' . PHP_EOL, __METHOD__, __LINE__, gettype($realPath), $realPath);
196
197                                 // Is it FALSE?
198                                 if ($realPath === FALSE) {
199                                         // Then, not found.
200                                         continue;
201                                 } // END - if
202
203                                 // First create full-qualified file name (FQFN) to inc/config.php
204                                 $fqfn = sprintf(
205                                         '%s%sinc%sconfig.php',
206                                         $realPath,
207                                         DIRECTORY_SEPARATOR,
208                                         DIRECTORY_SEPARATOR,
209                                         $possiblePath
210                                 );
211
212                                 // Debug message
213                                 //* NOISY-DEBUG: */ printf('[%s:%d]: fqfn=%s' . PHP_EOL, __METHOD__, __LINE__, $fqfn);
214
215                                 // Is it readable?
216                                 if (is_readable($fqfn)) {
217                                         // Found one
218                                         self::$corePath = $realPath;
219
220                                         // Abort here
221                                         break;
222                                 } // END - if
223                         } // END - foreach
224                 } // END - if
225
226                 // Return it
227                 return self::$corePath;
228         }
229
230         /**
231          * The framework's main entry point. This class isolates some local
232          * variables which shall not become visible to outside because of security
233          * concerns. This is done here to "emulate" the well-known entry point in
234          * Java.
235          *
236          * @return      void
237          */
238         public static final function main () {
239                 // Load config file, this provides $cfg
240                 require(self::detectCorePath() . '/inc/config.php');
241
242                 // Get a new configuration instance
243                 $cfg = FrameworkConfiguration::getSelfInstance();
244
245                 // Load bootstrap class
246                 require($cfg->getConfigEntry('base_path') . 'inc/bootstrap/class_BootstrapFramework.php');
247
248                 // ----- Below is deprecated -----
249
250                 // Load all include files
251                 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
252
253                 // Include the application selector
254                 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
255         }
256 }
257
258 // Developer mode active? Comment out if no dev!
259 define('DEVELOPER', TRUE);
260
261 // Log all exceptions (only debug! This option can create large error logs)
262 //define('LOG_EXCEPTIONS', TRUE);
263
264 //xdebug_start_trace();
265
266 // Call above main() method
267 ApplicationEntryPoint::main();