File indexing completed on 2024-05-12 17:26:17

0001 <?php
0002 
0003 /*
0004  *   GFX 4.0
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: http://trt-gfx.googlecode.com
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the MIT License.
0011  *   http://opensource.org/licenses/mit-license.php
0012  */ 
0013 
0014 
0015 //starting session with cookies
0016 session_start();
0017 
0018 /*
0019  * Small module includer. Used to include php modules automatically.
0020  */
0021 
0022 class ELoader{
0023   
0024   public static $prev_path; // /var/www/html
0025   public static $abs_path; // /var/www/html/gfx/src
0026   public static $root_path; // localhost
0027   public static $request_uri = "";
0028   public static $subsite_path = ""; //only the name of the main rendered subsite
0029   public static $source_path = "gfx3/src";
0030   public static $cache_path = "gfx3/cache";
0031   public static $libs_path = "gfx3/libs";
0032   public static $config_path = "config";
0033   public static $controllers_path = "controllers";
0034   public static $models_path = "models";
0035   public static $views_path = "views";
0036   public static $locallibs_path = "libs";
0037   
0038   //actual website path, containg config, models, views and controllers
0039   public static $site_path = "";
0040   public static $setted_config = false;
0041   
0042   //look for gfx3 installation path
0043   public static function getLibInstallPath(){
0044     if(is_dir(ELoader::$config_path) and !ELoader::$setted_config){
0045       ELoader::$subsite_path = str_replace(ELoader::$prev_path,'',getcwd());
0046       ELoader::$config_path = getcwd()."/".ELoader::$config_path;
0047       ELoader::$controllers_path = getcwd()."/".ELoader::$controllers_path;
0048       ELoader::$models_path = getcwd()."/".ELoader::$models_path;
0049       ELoader::$views_path = getcwd()."/".ELoader::$views_path;
0050       ELoader::$locallibs_path = getcwd()."/".ELoader::$locallibs_path;
0051       ELoader::$setted_config = true;
0052     }
0053     if(is_dir("gfx3")){
0054       ELoader::$cache_path = getcwd()."/".ELoader::$cache_path;
0055       ELoader::$libs_path = getcwd()."/".ELoader::$libs_path;
0056       return getcwd()."/".ELoader::$source_path;
0057     } else {
0058       chdir("..");
0059       return ELoader::getLibInstallPath();
0060     }
0061   }
0062   
0063   /*
0064    * This method gives precedence to sub-websites
0065    * located in subfolders when the url is called
0066    * and set the correct path for loading:
0067    * config, controllers, models and views
0068    * in the subdirectory.
0069    */
0070   public static function checkForSubsites(){
0071     ELoader::$request_uri = $current_uri = $_SERVER['REQUEST_URI'];
0072     $dirs = explode("/", $current_uri);
0073     
0074     foreach($dirs as $key => $dir){
0075       if(is_dir($dir)){
0076         chdir($dir);
0077         if(is_dir("config")){
0078           //url from gfxroot to config, controllers etc...
0079           $base_url = implode("/", array_slice($dirs, 0, $key+1));
0080           
0081           $pos = strpos($_SERVER['REQUEST_URI'],$base_url);
0082           if ($pos !== false) {
0083             if($pos==0){
0084               $rewritten = str_replace($base_url, "", $_SERVER['REQUEST_URI']);
0085               $_SERVER['REQUEST_URI'] = $rewritten;
0086             }
0087           }
0088           
0089           ELoader::$site_path = getcwd();
0090           break;
0091         }
0092       }
0093     }
0094   }
0095   
0096   //load all modules dynamically
0097   public static function loadAllModules(){
0098     
0099     ELoader::$root_path = $_SERVER["HTTP_HOST"];
0100     ELoader::$prev_path = getcwd();
0101     
0102     //handle eventual subsites, changes directory and leaves unchanged
0103     //the engine will later check for gfx presence in an upper folder
0104     ELoader::checkForSubsites();
0105     
0106     ELoader::$abs_path = ELoader::getLibInstallPath(); 
0107     
0108     //include source
0109     if(chdir(ELoader::$abs_path)){
0110       foreach(glob("*.class.php") as $filename){
0111         include_once($filename);
0112       }
0113     } else {
0114       ELog::error("critical error including gfx source. Path: ".ELoader::$abs_path);
0115     }
0116     //include controllers
0117     if(chdir(ELoader::$controllers_path)){
0118       foreach(glob("*.controller.php") as $filename){
0119         include_once($filename);
0120       }
0121     } else {
0122       ELog::error("critical error including controllers. Path: ".ELoader::$controllers_path);
0123     }
0124     
0125     //include models
0126     if(chdir(ELoader::$models_path)){
0127       foreach(glob("*.model.php") as $filename){
0128         include_once($filename);
0129       }
0130     } else {
0131       ELog::error("critical error including models. Path: ".ELoader::$models_path);
0132     }
0133     
0134     //include local external libraries
0135     //this is optional, if not found nothing happens
0136     if(file_exists(ELoader::$locallibs_path)){
0137       if(chdir(ELoader::$locallibs_path)){
0138         foreach(glob("*.class.php") as $filename){
0139           include_once($filename);
0140         }
0141       } else {
0142         ELog::error("critical error including controllers. Path: ".ELoader::$locallibs_path);
0143       }
0144     }
0145     
0146     //include global external libraries
0147     if(file_exists(ELoader::$libs_path)){
0148       if(chdir(ELoader::$libs_path)){
0149         foreach(glob("*.class.php") as $filename){
0150           include_once($filename);
0151         }
0152       } else {
0153         ELog::error("critical error including external libs. Path: ".ELoader::$libs_path);
0154       }
0155     }
0156     
0157     chdir(ELoader::$prev_path);
0158   }
0159 }
0160 
0161 class EUnloader{
0162   public function __destruct(){
0163     EDatabase::unload();
0164   }
0165 }
0166 
0167 $unloader = new EUnloader();
0168 
0169 //including all modules
0170 ELoader::loadAllModules();
0171 
0172 //loading current website configuration
0173 EConfig::load();
0174 
0175 //protecting whole website with auth and enabled
0176 EProtect::load();
0177 
0178 //rewrite url if needed
0179 if (EConfig::$data['generic']['rewrite'] == "yes"){
0180   ERewriter::enable();
0181   ERewriter::load();
0182 } else {
0183   ERewriter::disable();
0184 }
0185 
0186 //loading get/post
0187 if (EConfig::$data['generic']['protectheaders'] == "yes"){
0188   EHeaderDataParser::load();
0189 }
0190 
0191 //loading database
0192 if (EConfig::$data['generic']['database'] == "yes"){
0193   EDatabase::load();
0194 }
0195 
0196 //loading user system
0197 if (EConfig::$data['generic']['users'] == "yes"){
0198   OCSUser::client_login();
0199 }
0200 
0201 //rendering the page
0202 if (EConfig::$data['generic']['mvc'] == "yes"){
0203   if (EConfig::$data['generic']['rewrite'] == "yes"){
0204     EStructure::render(); //rendering default page
0205   } else {
0206     ELog::error("You must activate 'rewrite' module under config/generic before using 'mvc'!");
0207     return;
0208   }
0209 }
0210 
0211 ?>