Initial import from skeleton
[qa.git] / index.php
1 <?php
2 // Developer mode active? Comment out if no dev!
3 define('DEVELOPER', true);
4 //xdebug_start_trace();
5 /**
6  * The main class with the entry point to the whole application. This class
7  * "emulates" Java's entry point call. Additionally it covers local
8  * variables from outside access to prevent possible attacks on uninitialized
9  * local variables.
10  *
11  * But good little boys and girls would always initialize their variables... ;-)
12  *
13  * @author              Roland Haeder <webmaster@ship-simu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.ship-simu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 final class ApplicationEntryPoint {
33         /**
34          * Core path
35          */
36         private static $corePath = '';
37
38         /**
39          * The instances we want to remove after all is done
40          *
41          * @return      void
42          */
43         private static $instances = array (
44                 'cfg',    // The configuration system
45                 'loader', // The class loader system
46                 'debug',  // Debug output
47                 'db',     // Database layer
48                 'io',     // Base I/O system (local file [or network])
49                 'engine', // Template engine ( for ApplicationEntryPoint::app_die() )
50                 'lang',   // Language sub-system
51                 'app',    // The ApplicationHelper instance
52         );
53
54         /**
55          * The application's emergency exit
56          *
57          * @param       $message                The optional message we shall output on exit
58          * @param       $code                   Error code from exception
59          * @param       $extraData              Extra information from exceptions
60          * @param       $silentMode             Wether not silent mode is turned on
61          * @return      void
62          */
63         public static function app_die ($message = "", $code = false, $extraData = "", $silentMode = false) {
64                 // Is this method already called?
65                 if (defined('EMERGENCY_EXIT_CALLED')) {
66                         // Then output the text directly
67                         die($message);
68                 } // END - if
69
70                 // This method shall not be called twice
71                 define('EMERGENCY_EXIT_CALLED', true);
72
73                 // Is a message set?
74                 if (empty($message)) {
75                         // No message provided
76                         $message = "No message provided!";
77                 } // END - if
78
79                 // Get config instance
80                 $configInstance = FrameworkConfiguration::getInstance();
81
82                 // Do we have debug installation?
83                 if (($configInstance->getConfigEntry('product_install_mode') == "productive") || ($silentMode === true)) {
84                         // Abort here
85                         die();
86                 } // END - if
87
88                 // Get some instances
89                 $tpl = FrameworkConfiguration::getInstance()->getConfigEntry('template_class');
90                 $lang = LanguageSystem::getInstance();
91
92                 // Get response instance
93                 $responseInstance = ApplicationHelper::getInstance()->getResponseInstance();
94
95                 // Is the template engine loaded?
96                 if ((class_exists($tpl)) && (is_object($lang))) {
97                         // Use the template engine for putting out (nicer look) the message
98                         try {
99                                 // Get the template instance from our object factory
100                                 $templateInstance = ObjectFactory::createObjectByName($tpl, array(ApplicationHelper::getInstance()));
101                         } catch (FrameworkException $e) {
102                                 die(sprintf("[Main:] Could not initialize template engine for reason: <span class=\"exception_reason\">%s</span>",
103                                         $e->getMessage()
104                                 ));
105                         }
106
107                         // Get and prepare backtrace for output
108                         $backtraceArray = debug_backtrace();
109                         $backtrace = "";
110                         foreach ($backtraceArray as $key => $trace) {
111                                 if (!isset($trace['file'])) $trace['file'] = __FILE__;
112                                 if (!isset($trace['line'])) $trace['line'] = __LINE__;
113                                 if (!isset($trace['args'])) $trace['args'] = array();
114                                 $backtrace .= "<span class=\"backtrace_file\">".basename($trace['file'])."</span>:".$trace['line'].", <span class=\"backtrace_function\">".$trace['function']."(".count($trace['args']).")</span><br />";
115                         } // END - foreach
116
117                         // Init application instance
118                         $appInstance = null;
119
120                         // Is the class there?
121                         if (class_exists('ApplicationHelper')) {
122                                 // Get application instance
123                                 $appInstance = ApplicationHelper::getInstance();
124
125                                 // Assign application data
126                                 $templateInstance->assignApplicationData($appInstance);
127                         } // END - if
128
129                         // Assign variables
130                         $templateInstance->assignVariable('message', $message);
131                         $templateInstance->assignVariable('code', $code);
132                         $templateInstance->assignVariable('extra', $extraData);
133                         $templateInstance->assignVariable('backtrace', $backtrace);
134                         $templateInstance->assignVariable('total_includes', ClassLoader::getInstance()->getTotal());
135                         $templateInstance->assignVariable('total_objects', ObjectFactory::getTotal());
136                         $templateInstance->assignVariable('title', $lang->getMessage('emergency_exit_title'));
137
138                         // Load the template
139                         $templateInstance->loadCodeTemplate('emergency_exit');
140
141                         // Compile the template
142                         $templateInstance->compileTemplate();
143
144                         // Compile all variables
145                         $templateInstance->compileVariables();
146
147                         // Transfer data to response
148                         $templateInstance->transferToResponse($responseInstance);
149
150                         // Flush the response
151                         $responseInstance->flushBuffer();
152
153                         // Good bye...
154                         exit();
155                 } else {
156                         // Output message and die
157                         die(sprintf("[Main:] Emergency exit reached: <span class=\"emergency_span\">%s</span>",
158                                 $message
159                         ));
160                 }
161         }
162
163         /**
164          * Determines the correct absolute path for all include
165          *
166          * @return      $basePath       Base path (core) for all includes
167          */
168         protected static function detectCorePath () {
169                 // Is it not set?
170                 if (empty(self::$corePath)) {
171                         // Auto-detect our core path
172                         self::$corePath = str_replace("\\", '/', dirname(__FILE__));
173                 } // END - if
174
175                 // Return it
176                 return self::$corePath;
177         }
178
179         /**
180          * The application's main entry point. This class isolates some local
181          * variables which shall not become visible to outside because of security
182          * concerns. We are doing this here to "emulate" the well-known entry
183          * point in Java.
184          *
185          * @return      void
186          */
187         public static function main () {
188                 // Load config file
189                 require(self::detectCorePath() . '/inc/config.php');
190
191                 // Load all include files
192                 require($cfg->getConfigEntry('base_path') . 'inc/includes.php');
193
194                 // Load all framework classes
195                 require($cfg->getConfigEntry('base_path') . 'inc/classes.php');
196
197                 // Include the application selector
198                 require($cfg->getConfigEntry('base_path') . 'inc/selector.php');
199         } // END - main()
200
201 } // END - class
202
203 // Do not remove the following line:
204 ApplicationEntryPoint::main();
205
206 // [EOF]
207 ?>