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