Registration stub added, naming convention applied, support for PHP code (keep it...
[shipsimu.git] / index.php
1 <?php
2 /**
3  * The main class with the entry point to the whole application. This class
4  * "emulates" Java(tm)'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@mxchange.org>
11  * @version             0.0.0
12  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
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 class ApplicationEntryPoint {
30         /**
31          * The instances we want to remove after all is done
32          *
33          * @return      void
34          */
35         private static $instances = array (
36                 'cfg',  // The configuration system
37                 'loader',       // The class loader system
38                 'debug',  // Debug output
39                 'db',     // Database layer
40                 'io',     // Base I/O system (local file [or network])
41                 'engine', // Template engine ( for ApplicationEntryPoint::app_die() )
42                 'lang',   // Language sub-system
43                 'app',    // The ApplicationHelper instance
44         );
45
46         /**
47          * The application's emergency exit
48          *
49          * @param               $message        The optional message we shall output on exit
50          * @return      void
51          */
52         public static function app_die ($message = "") {
53                 // Is this method already called?
54                 if (defined('EMERGENCY_EXIT_CALLED')) {
55                         // Then output the text directly
56                         die($message);
57                 }
58
59                 // This method shall not be called twice
60                 define('EMERGENCY_EXIT_CALLED', true);
61
62                 // Is a message set?
63                 if (empty($message)) {
64                         // No message provided
65                         $message = "No message provided!";
66                 }
67
68                 // Get some instances
69                 $tpl = FrameworkConfiguration::getInstance()->readConfig("tpl_engine");
70                 $lang = LanguageSystem::getInstance();
71                 $io = FileIoHandler::getInstance();
72
73                 // Is the template engine loaded?
74                 if ((class_exists($tpl)) && (is_object($lang)) && (is_object($io))) {
75                         // Use the template engine for putting out (nicer look) the message
76                         try {
77                                 $eval = sprintf("\$tplEngine = %s::create%s(\"%s%s\", \$lang, \$io);",
78                                         FrameworkConfiguration::getInstance()->readConfig("tpl_engine"),
79                                         FrameworkConfiguration::getInstance()->readConfig("tpl_engine"),
80                                         PATH,
81                                         FrameworkConfiguration::getInstance()->readConfig("tpl_base_path")
82                                 );
83                                 eval($eval);
84                         } catch (BasePathIsEmptyException $e) {
85                                 die(sprintf("[Main:] Die Template-Engine konnte nicht initialisieren. Grund: <strong>%s</strong>",
86                                         $e->getMessage()
87                                 ));
88                         } catch (InvalidBasePathStringException $e) {
89                                 die(sprintf("[Main:] Die Template-Engine konnte nicht initialisieren. Grund: <strong>%s</strong>",
90                                         $e->getMessage()
91                                 ));
92                         } catch (BasePathIsNoDirectoryException $e) {
93                                 die(sprintf("[Main:] Die Template-Engine konnte nicht initialisieren. Grund: <strong>%s</strong>",
94                                         $e->getMessage()
95                                 ));
96                         } catch (BasePathReadProtectedException $e) {
97                                 die(sprintf("[Main:] Die Template-Engine konnte nicht initialisieren. Grund: <strong>%s</strong>",
98                                         $e->getMessage()
99                                 ));
100                         }
101
102                         // Backtrace holen und aufbereiten
103                         $backtraceArray = debug_backtrace();
104                         $backtrace = "";
105                         foreach ($backtraceArray as $key=>$trace) {
106                                 if (!isset($trace['file'])) $trace['file'] = __FILE__;
107                                 if (!isset($trace['line'])) $trace['line'] = 5;
108                                 if (!isset($trace['args'])) $trace['args'] = array();
109                                 $backtrace .= "<span class=\"backtrace_file\">".basename($trace['file'])."</span>:".$trace['line'].", <span class=\"backtrace_function\">".$trace['function']."(".count($trace['args']).")</span><br />";
110                         }
111
112                         // Assign variables
113                         $tplEngine->assignVariable("message", $message);
114                         $tplEngine->assignVariable("backtrace", $backtrace);
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 ?>