More code merged from ship-simu
[mailer.git] / index.php
1 <?php
2 // Developer mode active? Comment out if no dev!
3 define('DEVELOPER', true);
4
5 //xdebug_start_trace();
6 /**
7  * The main class with the entry point to the whole application. This class
8  * "emulates" Java(tm)'s entry point call. Additionally it covers local
9  * variables from outside access to prevent possible attacks on uninitialized
10  * local variables.
11  *
12  * But good little boys and girls would always initialize their variables... ;-)
13  *
14  * @author              Roland Haeder <webmaster@mxchange.org>
15  * @version             0.0.0
16  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.ship-simu.org
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class ApplicationEntryPoint {
34         /**
35          * The instances we want to remove after all is done
36          *
37          * @return      void
38          */
39         private static $instances = array (
40                 'cfg',    // The configuration system
41                 'loader', // The class loader system
42                 'debug',  // Debug output
43                 'db',     // Database layer
44                 'io',     // Base I/O system (local file [or network])
45                 'engine', // Template engine ( for ApplicationEntryPoint::app_die() )
46                 'lang',   // Language sub-system
47                 'app',    // The ApplicationHelper instance
48         );
49
50         /**
51          * The application's emergency exit
52          *
53          * @param               $message        The optional message we shall output on exit
54          * @return      void
55          */
56         public static function app_die ($message = "") {
57                 // Is this method already called?
58                 if (defined('EMERGENCY_EXIT_CALLED')) {
59                         // Then output the text directly
60                         die($message);
61                 }
62
63                 // This method shall not be called twice
64                 define('EMERGENCY_EXIT_CALLED', true);
65
66                 // Is a message set?
67                 if (empty($message)) {
68                         // No message provided
69                         $message = "No message provided!";
70                 }
71
72                 // Get some instances
73                 $tpl = FrameworkConfiguration::getInstance()->readConfig('tpl_engine');
74                 $lang = LanguageSystem::getInstance();
75                 $io = FileIoHandler::getInstance();
76
77                 // Is the template engine loaded?
78                 if ((class_exists($tpl)) && (is_object($lang)) && (is_object($io))) {
79                         // Use the template engine for putting out (nicer look) the message
80                         try {
81                                 // Get the template instance from our object factory
82                                 $tplEngine = ObjectFactory::createObjectByConfiguredName('tpl_engine', array(FrameworkConfiguration::getInstance()->readConfig('tpl_base_path'), $lang, $io));
83                         } catch (BasePathIsEmptyException $e) {
84                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
85                                         $e->getMessage()
86                                 ));
87                         } catch (InvalidBasePathStringException $e) {
88                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
89                                         $e->getMessage()
90                                 ));
91                         } catch (BasePathIsNoDirectoryException $e) {
92                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
93                                         $e->getMessage()
94                                 ));
95                         } catch (BasePathReadProtectedException $e) {
96                                 die(sprintf("[Main:] Could not initialize template engine for this reason: <strong>%s</strong>",
97                                         $e->getMessage()
98                                 ));
99                         }
100
101                         // Backtrace holen und aufbereiten
102                         $backtraceArray = debug_backtrace();
103                         $backtrace = "";
104                         foreach ($backtraceArray as $key=>$trace) {
105                                 if (!isset($trace['file'])) $trace['file'] = __FILE__;
106                                 if (!isset($trace['line'])) $trace['line'] = 5;
107                                 if (!isset($trace['args'])) $trace['args'] = array();
108                                 $backtrace .= "<span class=\"backtrace_file\">".basename($trace['file'])."</span>:".$trace['line'].", <span class=\"backtrace_function\">".$trace['function']."(".count($trace['args']).")</span><br />";
109                         }
110
111                         // Assign variables
112                         $tplEngine->assignVariable('message', $message);
113                         $tplEngine->assignVariable('backtrace', $backtrace);
114                         $tplEngine->assignVariable('total_objects', ObjectFactory::getTotal());
115
116                         // Load the template
117                         $tplEngine->loadCodeTemplate('emergency_exit');
118
119                         // Compile the template
120                         $tplEngine->compileTemplate();
121
122                         // Compile all variables
123                         $tplEngine->compileVariables();
124
125                         // Output all
126                         $tplEngine->output();
127
128                         // Good bye...
129                         exit();
130                 } else {
131                         // Output message and die
132                         die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
133                                 $message
134                         ));
135                 }
136         }
137
138         /**
139          * The application's main entry point. This class isolates some local
140          * variables which shall not become visible to outside because of security
141          * concerns. We are doing this here to "emulate" the well-known entry
142          * point in Java(tm).
143          *
144          * @return      void
145          */
146         public static function main () {
147                 // Some non-global common arrays we need...
148                 global $_SERVER;
149
150                 // Load config file
151                 require(dirname(__FILE__) . '/inc/config.php');
152
153                 // Load all include files
154                 require(PATH . 'inc/includes.php');
155
156                 // Load all framework classes
157                 require(PATH . 'inc/classes.php');
158
159                 // Include the application selector
160                 require(PATH . 'inc/selector.php');
161
162         } // END - main()
163
164 } // END - class
165
166 // Do not remove the following line:
167 ApplicationEntryPoint::main();
168
169 // [EOF]
170 ?>