Renamed getInstance() to getSelfInstance(), conflicts BaseRegistry versus several...
[mailer.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@ship-simu.org>
11  * @version             0.0.0
12  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2011 Core Developer Team
13  * @license             GNU GPL 3.0 or any newer version
14  * @link                http://www.ship-simu.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 instances we want to remove after all is done
37          */
38         private static $instances = array (
39                 'cfg',    // The configuration system
40                 'loader', // The class loader system
41                 'debug',  // Debug output
42                 'db',     // Database layer
43                 'io',     // Base I/O system (local file [or network])
44                 'engine', // Template engine ( for ApplicationEntryPoint::app_die() )
45                 'lang',   // Language sub-system
46                 'app',    // The ApplicationHelper instance
47         );
48
49         /**
50          * The application's emergency exit
51          *
52          * @param       $message                The optional message we shall output on exit
53          * @param       $code                   Error code from exception
54          * @param       $extraData              Extra information from exceptions
55          * @param       $silentMode             Wether not silent mode is turned on
56          * @return      void
57          * @todo        This method is old code and needs heavy rewrite and should be moved to ApplicationHelper
58          */
59         public static final function app_die ($message = '', $code = false, $extraData = '', $silentMode = false) {
60                 // Is this method already called?
61                 if (isset($GLOBALS['app_die_called'])) {
62                         // Then output the text directly
63                         die($message);
64                 } // END - if
65
66                 // This method shall not be called twice
67                 $GLOBALS['app_die_called'] = true;
68
69                 // Is a message set?
70                 if (empty($message)) {
71                         // No message provided
72                         $message = 'No message provided!';
73                 } // END - if
74
75                 // Get config instance
76                 $configInstance = FrameworkConfiguration::getSelfInstance();
77
78                 // Do we have debug installation?
79                 if (($configInstance->getConfigEntry('product_install_mode') == 'productive') || ($silentMode === true)) {
80                         // Abort here
81                         die();
82                 } // END - if
83
84                 // Get some instances
85                 $tpl = FrameworkConfiguration::getSelfInstance()->getConfigEntry('web_template_class');
86                 $languageInstance = LanguageSystem::getSelfInstance();
87
88                 // Initialize template instance here to avoid warnings in IDE
89                 $templateInstance = NULL;
90
91                 // Get response instance
92                 $responseInstance = ApplicationHelper::getSelfInstance()->getResponseInstance();
93
94                 // Is the template engine loaded?
95                 if ((class_exists($tpl)) && (is_object($languageInstance))) {
96                         // Use the template engine for putting out (nicer look) the message
97                         try {
98                                 // Get the template instance from our object factory
99                                 $templateInstance = ObjectFactory::createObjectByName($tpl);
100                         } catch (FrameworkException $e) {
101                                 die(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
102                                         $e->getMessage()
103                                 ));
104                         }
105
106                         // Get and prepare backtrace for output
107                         $backtraceArray = debug_backtrace();
108                         $backtrace = '';
109                         foreach ($backtraceArray as $key => $trace) {
110                                 // Set missing array elements
111                                 if (!isset($trace['file'])) {
112                                         $trace['file'] = __FILE__;
113                                 } // END - if
114                                 if (!isset($trace['line'])) {
115                                         $trace['line'] = __LINE__;
116                                 } // END - if
117                                 if (!isset($trace['args'])) {
118                                         $trace['args'] = array();
119                                 } // END - if
120
121                                 // Add the traceback path to the final output
122                                 $backtrace .= sprintf("<span class=\"backtrace_file\">%s</span>:%d, <span class=\"backtrace_function\">%s(%d)</span><br />\n",
123                                         basename($trace['file']),
124                                         $trace['line'],
125                                         $trace['function'],
126                                         count($trace['args'])
127                                 );
128                         } // END - foreach
129
130                         // Init application instance
131                         $applicationInstance = NULL;
132
133                         // Is the class there?
134                         if (class_exists('ApplicationHelper')) {
135                                 // Get application instance
136                                 $applicationInstance = ApplicationHelper::getSelfInstance();
137
138                                 // Assign application data
139                                 $templateInstance->assignApplicationData($applicationInstance);
140                         } // END - if
141
142                         // We only try this
143                         try {
144                                 // Assign variables
145                                 $templateInstance->assignVariable('message', $message);
146                                 $templateInstance->assignVariable('code', $code);
147                                 $templateInstance->assignVariable('extra', $extraData);
148                                 $templateInstance->assignVariable('backtrace', $backtrace);
149                                 $templateInstance->assignVariable('total_includes', ClassLoader::getSelfInstance()->getTotal());
150                                 $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
151                                 $templateInstance->assignVariable('title', $languageInstance->getMessage('emergency_exit_title'));
152
153                                 // Load the template
154                                 $templateInstance->loadCodeTemplate('emergency_exit');
155
156                                 // Compile the template
157                                 $templateInstance->compileTemplate();
158
159                                 // Compile all variables
160                                 $templateInstance->compileVariables();
161
162                                 // Transfer data to response
163                                 $templateInstance->transferToResponse($responseInstance);
164
165                                 // Flush the response
166                                 $responseInstance->flushBuffer();
167                         } catch (FileIoException $e) {
168                                 // Even the template 'emergency_exit' wasn't found so output both message
169                                 die($message . ', exception: ' . $e->getMessage());
170                         }
171
172                         // Good bye...
173                         exit();
174                 } else {
175                         // Output message and die
176                         die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
177                                 $message
178                         ));
179                 }
180         }
181
182         /**
183          * Determines the correct absolute path for all includes only once per run.
184          * Other calls of this method are being "cached".
185          *
186          * @return      $corePath       Base path (core) for all includes
187          */
188         protected static final function detectCorePath () {
189                 // Is it not set?
190                 if (empty(self::$corePath)) {
191                         // Auto-detect our core path
192                         self::$corePath = str_replace("\\", '/', dirname(__FILE__));
193                 } // END - if
194
195                 // Return it
196                 return self::$corePath;
197         }
198
199         /**
200          * The application's main entry point. This class isolates some local
201          * variables which shall not become visible to outside because of security
202          * concerns. We are doing this here to "emulate" the well-known entry
203          * point in Java.
204          *
205          * @return      void
206          */
207         public static final function main () {
208                 // Load config file
209                 require(self::detectCorePath() . '/inc/config.php');
210
211                 // Load all include files
212                 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
213
214                 // Load all framework classes
215                 require($cfg->getConfigEntry('base_path') . 'inc/classes.php');
216
217                 // Include the application selector
218                 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
219         } // END - main()
220 } // END - class
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 // Do not remove the following line:
231 ApplicationEntryPoint::main();
232
233 // [EOF]
234 ?>