Source for file jConfigCompiler.class.php

Documentation is available at jConfigCompiler.class.php

  1. <?php
  2. /**
  3. * @package      jelix
  4. * @subpackage   core
  5. * @author       Jouanneau Laurent
  6. * @contributor  Thibault PIRONT < nuKs >
  7. * @copyright    2006-2008 Jouanneau laurent
  8. * @copyright    2007 Thibault PIRONT
  9. * @link         http://www.jelix.org
  10. * @licence      GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  11. */
  12.  
  13. /**
  14.  * jConfigCompiler merge two ini file in a single array and store it in a temporary file
  15.  * This is a static class
  16.  * @package  jelix
  17.  * @subpackage core
  18.  * @static
  19.  */
  20. class jConfigCompiler {
  21.  
  22.     private function __construct (){ }
  23.  
  24.     /**
  25.      * read the given ini file. Merge it with the content of defaultconfig.ini.php
  26.      * It also calculates some options. It stores the result in a temporary file
  27.      * @param string $configFile the config file name
  28.      * @return object an object which contains configuration values
  29.      */
  30.     static public function read($configFile){
  31.  
  32.         $config = jIniFile::read(JELIX_LIB_CORE_PATH.'defaultconfig.ini.php');
  33.  
  34.         if( $commonConfig = parse_ini_file(JELIX_APP_CONFIG_PATH.'defaultconfig.ini.php',true)){
  35.             self::_mergeConfig($config, $commonConfig);
  36.         }
  37.  
  38.         if($configFile !='defaultconfig.ini.php'){
  39.             if(!file_exists(JELIX_APP_CONFIG_PATH.$configFile))
  40.                 die("Jelix config file $configFile is missing !");
  41.             if( false === ($userConfig = parse_ini_file(JELIX_APP_CONFIG_PATH.$configFile,true)))
  42.                 die("Syntax error in the Jelix config file $configFile !");
  43.             self::_mergeConfig($config, $userConfig);
  44.         }
  45.         $config['isWindows'] =  (DIRECTORY_SEPARATOR == '\\');
  46.         if(trim( $config['startAction']) == '') {
  47.             $config['startAction'] = ':';
  48.         }
  49.  
  50.         $config['_allBasePath'] = array();
  51.         $unusedModules = split(' *, *',$config['unusedModules']);
  52.         $config['_modulesPathList'] = self::_loadPathList($config['modulesPath'], $unusedModules, $config['_allBasePath']);
  53.  
  54.         self::_loadPluginsPathList($config);
  55.  
  56.         if($config['checkTrustedModules']){
  57.             $config['_trustedModules'] = explode(',',$config['trustedModules']);
  58.             if(!in_array('jelix',$config['_trustedModules']))
  59.                 $config['_trustedModules'][]='jelix';
  60.         }else{
  61.             $config['_trustedModules'] = array_keys($config['_modulesPathList']);
  62.         }
  63.  
  64.         if($config['urlengine']['scriptNameServerVariable'] == '') {
  65.             $config['urlengine']['scriptNameServerVariable'] = self::_findServerName($config['urlengine']['entrypointExtension']);
  66.         }
  67.  
  68.         $path=$config['urlengine']['basePath'];
  69.         if($path!='/' && $path!=''){
  70.             if($path{0} != '/') $path='/'.$path;
  71.             if(substr($path,-1) != '/') $path.='/';
  72.             $config['urlengine']['basePath'] = $path;
  73.         }
  74.  
  75.         if($path!='' && $config['urlengine']['jelixWWWPath']{0} != '/')
  76.             $config['urlengine']['jelixWWWPath'] = $path.$config['urlengine']['jelixWWWPath'];
  77.  
  78.         /*if(preg_match("/^([a-zA-Z]{2})(?:_([a-zA-Z]{2}))?$/",$config['locale'],$m)){
  79.             if(!isset($m[2])){
  80.                 $m[2] = $m[1];
  81.             }
  82.             $config['defaultLang'] = strtolower($m[1]);
  83.             $config['defaultCountry'] = strtoupper($m[2]);
  84.             $config['locale'] = $config['defaultLang'].'_'.$config['defaultCountry'];
  85.         }else{
  86.             die("Syntax error in the locale parameter in Jelix config file $configFile !");
  87.         }*/
  88.  
  89.         if(BYTECODE_CACHE_EXISTS){
  90.             $filename=JELIX_APP_TEMP_PATH.str_replace('/','~',$configFile).'.conf.php';
  91.             if ($f = @fopen($filename, 'wb')) {
  92.                 fwrite($f, '<?php $config = '.var_export($config,true).";\n?>");
  93.                 fclose($f);
  94.             } else {
  95.                 throw new Exception('(24)Error while writing config cache file '.$filename);
  96.             }
  97.         }else{
  98.             jIniFile::write($config, JELIX_APP_TEMP_PATH.str_replace('/','~',$configFile).'.resultini.php', ";<?php die('');?>\n");
  99.         }
  100.         $config = (object) $config;
  101.         return $config;
  102.     }
  103.  
  104.     /**
  105.      * Analyse and check the "lib:" and "app:" path.
  106.      * @param array $list list of "lib:*" and "app:*" path
  107.      * @return array list of full path
  108.      */
  109.     static private function _loadPathList($list, $forbiddenList, &$allBasePath){
  110.         $list = split(' *, *',$list);
  111.         array_unshift($list, JELIX_LIB_PATH.'core-modules/');
  112.         $result=array();
  113.         foreach($list as $k=>$path){
  114.             if(trim($path) == '') continue;
  115.             $p = str_replace(array('lib:','app:'), array(LIB_PATH, JELIX_APP_PATH), $path);
  116.             if(!file_exists($p)){
  117.                 trigger_error('The path, '.$path.' given in the jelix config, doesn\'t exists !',E_USER_ERROR);
  118.                 exit;
  119.             }
  120.             if(substr($p,-1) !='/')
  121.                 $p.='/';
  122.             if($k!=0) 
  123.                 $allBasePath[]=$p;
  124.             if ($handle = opendir($p)) {
  125.                 while (false !== ($f = readdir($handle))) {
  126.                     if ($f{0} != '.' && is_dir($p.$f) && !in_array($f, $forbiddenList)) {
  127.                         $result[$f]=$p.$f.'/';
  128.                     }
  129.                 }
  130.                 closedir($handle);
  131.             }
  132.         }
  133.         return $result;
  134.     }
  135.  
  136.  
  137.     /**
  138.      * Analyse plugin paths
  139.      * @param array|object $config the config container
  140.      */
  141.     static private function _loadPluginsPathList(&$config){
  142.         $list = split(' *, *',$config['pluginsPath']);
  143.         array_unshift($list, JELIX_LIB_PATH.'plugins/'); 
  144.         foreach($list as $k=>$path){
  145.             if(trim($path) == '') continue;
  146.             $p = str_replace(array('lib:','app:'), array(LIB_PATH, JELIX_APP_PATH), $path);
  147.             if(!file_exists($p)){
  148.                 trigger_error('The path, '.$path.' given in the jelix config, doesn\'t exists !',E_USER_ERROR);
  149.                 exit;
  150.             }
  151.             if(substr($p,-1) !='/')
  152.                 $p.='/';
  153.  
  154.             if ($handle = opendir($p)) {
  155.                 while (false !== ($f = readdir($handle))) {
  156.                     if ($f{0} != '.' && is_dir($p.$f)) {
  157.                         if($subdir = opendir($p.$f)){
  158.                             if($k!=0) 
  159.                                $config['_allBasePath'][]=$p.$f.'/';
  160.                             while (false !== ($subf = readdir($subdir))) {
  161.                                 if ($subf{0} != '.' && is_dir($p.$f.'/'.$subf)) {
  162.                                     if($f == 'tpl'){
  163.                                         $config['_tplpluginsPathList_'.$subf][] = $p.$f.'/'.$subf.'/';
  164.                                     }else{
  165.                                         $config['_pluginsPathList_'.$f][$subf] =$p.$f.'/'.$subf.'/';
  166.                                     }
  167.                                 }
  168.                             }
  169.                             closedir($subdir);
  170.                         }
  171.                     }
  172.                 }
  173.                 closedir($handle);
  174.             }
  175.         }
  176.     }
  177.  
  178.     static private function _findServerName($ext) {
  179.         $varname = '';
  180.         $extlen = strlen($ext);
  181.  
  182.         if(strrpos($_SERVER['SCRIPT_NAME'], $ext) === (strlen($_SERVER['SCRIPT_NAME']) - $extlen) || php_sapi_name() == 'cli') {
  183.             return 'SCRIPT_NAME';
  184.         }else if (isset($_SERVER['REDIRECT_URL']) && strrpos( $_SERVER['REDIRECT_URL'], $ext) === (strlen( $_SERVER['REDIRECT_URL']) -$extlen)) {
  185.             return 'REDIRECT_URL';
  186.         }else if (isset($_SERVER['ORIG_SCRIPT_NAME']) && strrpos( $_SERVER['ORIG_SCRIPT_NAME'], $ext) === (strlen( $_SERVER['ORIG_SCRIPT_NAME']) - $extlen)) {
  187.             return 'ORIG_SCRIPT_NAME';
  188.         }
  189.         throw new Exception('Jelix Error: in config file the parameter urlengine:scriptNameServerVariable is empty and Jelix don\'t find
  190.             the variable in $_SERVER which contains the script name. You must see phpinfo and setup this parameter in your config file.');
  191.     }
  192.  
  193.  
  194.     /**
  195.      * merge two array which are the result of a parse_ini_file call
  196.      * @param $array the main array
  197.      * @param $tomerge the array to merge in the first one
  198.      */
  199.     static private function _mergeConfig(&$array, $tomerge){
  200.  
  201.         foreach($tomerge as $k=>$v){
  202.             if(!isset($array[$k])){
  203.                 $array[$k] = $v;
  204.                 continue;
  205.             }
  206.             if($k{1} == '_')
  207.                 continue;
  208.             if(is_array($v)){
  209.                 $array[$k] = array_merge($array[$k], $v);
  210.             }else{
  211.                 $array[$k] = $v;
  212.             }
  213.         }
  214.  
  215.     }
  216. }
  217.  
  218. ?>

Documentation generated on Wed, 07 Sep 2011 13:46:46 +0200 by phpDocumentor 1.4.3