2133c11e742dbc7babcdf75dd93bd3f9d41e7e87
[shipsimu.git] / inc / selector.php
1 <?php
2 /**
3  * The application selector main include file
4  *
5  * @author              Roland Haeder <webmaster@mxchange.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <http://www.gnu.org/licenses/>.
23  */
24
25 // Does the user has an application specified?
26 if (!empty($_GET[FrameworkConfiguration::getInstance()->readConfig("app_selector_get")])) {
27         // Set the application from string
28         $application = (string) $_GET[FrameworkConfiguration::getInstance()->readConfig("app_selector_get")];
29 } elseif (!empty($_SERVER['argv'][1])) {
30         // Set the application from string
31         $application = (string) $_SERVER['argv'][1];
32         $app = explode("=", trim($application));
33         if ($app[0] == FrameworkConfiguration::getInstance()->readConfig("app_selector_get")) {
34                 // Application is valid!
35                 $application = trim($app[1]);
36         } else {
37                 // Invalid entry found, first must be "app"!
38                 $application = FrameworkConfiguration::getInstance()->readConfig("default_application");
39         }
40 } else {
41         // Set the "application selector" application
42         $application = FrameworkConfiguration::getInstance()->readConfig("default_application");
43 }
44
45 // Secure it, by keeping out tags
46 $application = htmlentities(strip_tags($application), ENT_QUOTES);
47
48 // Secure it a little more with a reg.exp.
49 $application = preg_replace('/([^a-z_-])+/i', "", $application);
50
51 // Try to load these includes in the given order
52 $configAppIncludes = array(
53         sprintf("class_%s", FrameworkConfiguration::getInstance()->readConfig("app_helper_class")), // The ApplicationHelper class
54         "config",               // The application's own configuration
55         "init",                 // The application initializer
56         "loader",               // The application's class loader
57         "debug",                // Some debugging stuff
58         "exceptions",   // The application's own exception handler
59         "starter",              // The application starter (calls entryPoint(), etc.)
60 );
61
62 // Load them all (try only)
63 foreach ($configAppIncludes as $inc) {
64         // Skip starter in test mode
65         if (($inc == "starter") && (defined('TEST'))) {
66                 // Skip it here
67                 continue;
68         }
69
70         // Generate a FQFN for the helper class
71         $fqfn = sprintf("%s%s/%s/%s%s",
72                 PATH,
73                 FrameworkConfiguration::getInstance()->readConfig("application_path"),
74                 $application,
75                 $inc,
76                 FrameworkConfiguration::getInstance()->readConfig("php_extension")
77         );
78
79         // Does the include file exists?
80         if ((file_exists($fqfn)) && (is_file($fqfn)) && (is_readable($fqfn))) {
81                 // Load it
82                 require_once($fqfn);
83         } elseif (FrameworkConfiguration::getInstance()->readConfig("verbose_level") > 0) {
84                 // File is missing
85                 trigger_error(sprintf("Cannot load application script %s! File is missing or read-protected.",
86                         $inc . FrameworkConfiguration::getInstance()->readConfig("php_extension")
87                 ));
88         }
89 }
90
91 // [EOF]
92 ?>