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