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