Source for file jConfigCompiler.class.php

Documentation is available at jConfigCompiler.class.php

  1. <?php
  2. /**
  3. * @package      jelix
  4. * @subpackage   core
  5. * @author       Laurent Jouanneau
  6. * @contributor  Thibault Piront (nuKs), Christophe Thiriot, Philippe Schelté
  7. * @copyright    2006-2012 Laurent Jouanneau
  8. * @copyright    2007 Thibault Piront, 2008 Christophe Thiriot, 2008 Philippe Schelté
  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.     static protected $commonConfig;
  23.  
  24.     private function __construct (){ }
  25.  
  26.     /**
  27.      * read the given ini file, for the current entry point, or for the entrypoint given
  28.      * in $pseudoScriptName. Merge it with the content of defaultconfig.ini.php
  29.      * It also calculates some options.
  30.      * If you are in a CLI script but you want to load a configuration file for a web entry point
  31.      * or vice-versa, you need to indicate the $pseudoScriptName parameter with the name of the entry point
  32.      * @param string $configFile the config file name
  33.      * @param boolean $allModuleInfo may be true for the installer, which needs all informations
  34.      *                                else should be false, these extra informations are
  35.      *                                not needed to run the application
  36.      * @param boolean $isCli  indicate if the configuration to read is for a CLI script or no
  37.      * @param string $pseudoScriptName the name of the entry point, relative to the base path,
  38.      *               corresponding to the readed configuration
  39.      * @return object an object which contains configuration values
  40.      */
  41.     static public function read($configFile, $allModuleInfo = false, $isCli = false, $pseudoScriptName=''){
  42.  
  43.         $tempPath = jApp::tempBasePath();
  44.         $configPath = jApp::configPath();
  45.  
  46.         if($tempPath=='/'){
  47.             // if it equals to '/', this is because realpath has returned false in the application.init.php
  48.             // so this is because the path doesn't exist.
  49.             throw new Exception('Application temp directory doesn\'t exist !', 3);
  50.         }
  51.  
  52.         if(!is_writable($tempPath)){
  53.             throw new Exception('Application temp base directory is not writable -- ('.$tempPath.')', 4);
  54.         }
  55.  
  56.         if(!is_writable(jApp::logPath())) {
  57.             throw new Exception('Application log directory is not writable -- ('.jApp::logPath().')', 4);
  58.         }
  59.  
  60.         
  61.         self::$commonConfig = jIniFile::read($configPath.'defaultconfig.ini.php',true);
  62.  
  63.         $config = jIniFile::read(JELIX_LIB_CORE_PATH.'defaultconfig.ini.php');
  64.  
  65.         if (self::$commonConfig) {
  66.             self::_mergeConfig($config, self::$commonConfig);
  67.         }
  68.  
  69.         if($configFile !='defaultconfig.ini.php'){
  70.             if(!file_exists($configPath.$configFile))
  71.                 throw new Exception("Configuration file is missing -- $configFile ", 5);
  72.             if( false === ($userConfig = parse_ini_file($configPath.$configFile,true)))
  73.                 throw new Exception("Syntax error in the configuration file -- $configFile", 6);
  74.             self::_mergeConfig($config, $userConfig);
  75.         }
  76.         $config = (object) $config;
  77.  
  78.         self::prepareConfig($config, $allModuleInfo, $isCli, $pseudoScriptName);
  79.         self::$commonConfig  = null;
  80.         return $config;
  81.     }
  82.  
  83.     /**
  84.      * Identical to read(), but also stores the result in a temporary file
  85.      * @param string $configFile the config file name
  86.      * @param boolean $isCli 
  87.      * @param string $pseudoScriptName 
  88.      * @return object an object which contains configuration values
  89.      */
  90.     static public function readAndCache($configFile, $isCli = null, $pseudoScriptName = '') {
  91.  
  92.         if ($isCli === null)
  93.             $isCli = jServer::isCLI();
  94.  
  95.         $config = self::read($configFile, false, $isCli, $pseudoScriptName);
  96.         $tempPath = jApp::tempPath();
  97.         jFile::createDir($tempPath);
  98.  
  99.         if(BYTECODE_CACHE_EXISTS){
  100.             $filename=$tempPath.str_replace('/','~',$configFile).'.conf.php';
  101.             if ($f = @fopen($filename, 'wb')) {
  102.                 fwrite($f, '<?php $config = '.var_export(get_object_vars($config),true).";\n?>");
  103.                 fclose($f);
  104.             } else {
  105.                 throw new Exception('Error while writing configuration cache file -- '.$filename);
  106.             }
  107.         }else{
  108.             jIniFile::write(get_object_vars($config), $tempPath.str_replace('/','~',$configFile).'.resultini.php', ";<?php die('');?>\n");
  109.         }
  110.         return $config;
  111.     }
  112.  
  113.     /**
  114.      * fill some config properties with calculated values
  115.      * @param object $config  the config object
  116.      * @param boolean $allModuleInfo may be true for the installer, which needs all informations
  117.      *                                else should be false, these extra informations are
  118.      *                                not needed to run the application
  119.      * @param boolean $isCli  indicate if the configuration to read is for a CLI script or no
  120.      * @param string $pseudoScriptName the name of the entry point, relative to the base path,
  121.      *               corresponding to the readed configuration
  122.      */
  123.     static protected function prepareConfig($config, $allModuleInfo, $isCli, $pseudoScriptName){
  124.  
  125.         $config->isWindows = (DIRECTORY_SEPARATOR === '\\');
  126.         if(trim( $config->startAction) == '') {
  127.             $config->startAction = ':';
  128.         }
  129.  
  130.         if ($config->domainName == "" && isset($_SERVER['SERVER_NAME']))
  131.             $config->domainName = $_SERVER['SERVER_NAME'];
  132.  
  133.         $config->_allBasePath = array();
  134.  
  135.         self::getPaths($config->urlengine, $pseudoScriptName, $isCli);
  136.         self::_loadModuleInfo($config, $allModuleInfo);
  137.         self::_loadPluginsPathList($config);
  138.  
  139.         if ($config->urlengine['engine'] == 'simple')
  140.             trigger_error("The 'simple' url engine is deprecated. use 'basic_significant' or 'significant' url engine", E_USER_NOTICE);
  141.  
  142.         $coordplugins = array();
  143.         foreach ($config->coordplugins as $name=>$conf) {
  144.             if (!isset($config->_pluginsPathList_coord[$name])) {
  145.                 throw new Exception("Error in the main configuration. A plugin doesn't exist -- The coord plugin $name is unknown.", 7);
  146.             }
  147.             if ($conf) {
  148.                 if ($conf != '1' && !file_exists(jApp::configPath($conf))) {
  149.                     throw new Exception("Error in the main configuration. A plugin configuration file doesn't exist -- Configuration file for the coord plugin $name doesn't exist: '$conf'", 8);
  150.                 }
  151.                 $coordplugins[$name] = $conf;
  152.             }
  153.         }
  154.         $config->coordplugins = $coordplugins;
  155.  
  156.         self::_initResponsesPath($config, 'responses');
  157.         self::_initResponsesPath($config, '_coreResponses');
  158.  
  159.         if (trim($config->timeZone) === '') {
  160.             $tz = ini_get('date.timezone');
  161.             if ($tz != '')
  162.                 $config->timeZone = $tz;
  163.             else
  164.                 $config->timeZone = "Europe/Paris";
  165.         }
  166.  
  167.         // lang to locale
  168.         $availableLocales = explode(',', $config->availableLocales);
  169.         foreach ($availableLocales as $locale) {
  170.             if (preg_match("/^([a-z]{2,3})_([A-Z]{2,3})$/", $locale, $m)) {
  171.                 if (!isset($config->langToLocale[$m[1]]))
  172.                     $config->langToLocale[$m[1]] = $locale;
  173.             }
  174.             else {
  175.                 throw new Exception("Error in the main configuration. Bad locale code in available locales -- availableLocales: '$locale' is not a locale code");
  176.             }
  177.         }
  178.  
  179.         $locale = $config->locale;
  180.         if (preg_match("/^([a-z]{2,3})_([A-Z]{2,3})$/", $locale, $m)) {
  181.             $config->langToLocale[$m[1]] = $locale;
  182.         }
  183.         else {
  184.             throw new Exception("Error in the main configuration. Bad locale code in default locale -- config->locale: '$locale' is not a locale code");
  185.         }
  186.  
  187.         if (!in_array($locale, $availableLocales)) {
  188.             array_unshift($availableLocales, $locale);
  189.         }
  190.  
  191.         $config->availableLocales = $availableLocales;
  192.  
  193.         if($config->sessions['storage'] == 'files'){
  194.             $config->sessions['files_path'] = str_replace(array('lib:','app:'), array(LIB_PATH, jApp::appPath()), $config->sessions['files_path']);
  195.         }
  196.  
  197.         $config->sessions['_class_to_load'] = array();
  198.         if ($config->sessions['loadClasses'] != '') {
  199.             $list = preg_split('/ *, */',$config->sessions['loadClasses']);
  200.             foreach($list as $sel) {
  201.                 if(preg_match("/^([a-zA-Z0-9_\.]+)~([a-zA-Z0-9_\.\\/]+)$/", $sel, $m)){
  202.                     if (!isset($config->_modulesPathList[$m[1]])) {
  203.                         throw new Exception('Error in the configuration file -- in loadClasses parameter, '.$m[1].' is not a valid or activated module');
  204.                     }
  205.  
  206.                     if( ($p=strrpos($m[2], '/')) !== false){
  207.                         $className = substr($m[2],$p+1);
  208.                         $subpath = substr($m[2],0,$p+1);
  209.                     }else{
  210.                         $className = $m[2];
  211.                         $subpath ='';
  212.                     }
  213.  
  214.                     $path = $config->_modulesPathList[$m[1]].'classes/'.$subpath.$className.'.class.php';
  215.  
  216.                     if (!file_exists($path) || strpos($subpath,'..') !== false ) {
  217.                         throw new Exception('Error in the configuration file -- in loadClasses parameter, bad class selector: '.$sel);
  218.                     }
  219.                     $config->sessions['_class_to_load'][] = $path;
  220.                 }
  221.                 else
  222.                     throw new Exception('Error in the configuration file --  in loadClasses parameter, bad class selector: '.$sel);
  223.             }
  224.         }
  225.     }
  226.  
  227.     /**
  228.      * Analyse and check the "lib:" and "app:" path.
  229.      * @param object $config  the config object
  230.      * @param boolean $allModuleInfo may be true for the installer, which needs all informations
  231.      *                                else should be false, these extra informations are
  232.      *                                not needed to run the application
  233.      */
  234.     static protected function _loadModuleInfo($config, $allModuleInfo) {
  235.  
  236.         $installerFile = jApp::configPath('installer.ini.php');
  237.  
  238.         if ($config->disableInstallers) {
  239.             $installation = array ();
  240.         }
  241.         else if (!file_exists($installerFile)) {
  242.             if ($allModuleInfo)
  243.                 $installation = array ();
  244.             else
  245.                 throw new Exception("The application is not installed -- installer.ini.php doesn't exist!\n", 9);
  246.         }
  247.         else
  248.             $installation = parse_ini_file($installerFile,true);
  249.  
  250.         $section = $config->urlengine['urlScriptId'];
  251.  
  252.         if (!isset($installation[$section]))
  253.             $installation[$section] = array();
  254.  
  255.         $list = preg_split('/ *, */',$config->modulesPath);
  256.         if (isset(self::$commonConfig['modulesPath']))
  257.             $list = array_merge($list, preg_split('/ *, */',self::$commonConfig['modulesPath']));
  258.         array_unshift($list, JELIX_LIB_PATH.'core-modules/');
  259.         $pathChecked = array();
  260.  
  261.         $config->_autoload_class = array();
  262.         $config->_autoload_namespace = array();
  263.         $config->_autoload_classpattern = array();
  264.         $config->_autoload_includepathmap = array();
  265.         $config->_autoload_includepath = array();
  266.         $config->_autoload_namespacepathmap = array();
  267.         $config->_autoload_autoloader = array();
  268.  
  269.         foreach($list as $k=>$path){
  270.             if(trim($path) == '') continue;
  271.             $p = str_replace(array('lib:','app:'), array(LIB_PATH, jApp::appPath()), $path);
  272.             if (!file_exists($p)) {
  273.                 throw new Exception('Error in the configuration file -- The path, '.$path.' given in the jelix config, doesn\'t exist', 10);
  274.             }
  275.             if (substr($p,-1) !='/')
  276.                 $p.='/';
  277.             if (in_array($p, $pathChecked))
  278.                 continue;
  279.             $pathChecked[] = $p;
  280.  
  281.              // don't include the core-modules into the list of base path. this list is to verify
  282.              // if modules have been modified into repositories
  283.             if ($k!=0 && $config->compilation['checkCacheFiletime'])
  284.                 $config->_allBasePath[]=$p;
  285.  
  286.             if ($handle = opendir($p)) {
  287.                 while (false !== ($f = readdir($handle))) {
  288.                     if ($f[0] != '.' && is_dir($p.$f)) {
  289.  
  290.                         if ($config->disableInstallers)
  291.                             $installation[$section][$f.'.installed'] = 1;
  292.                         else if (!isset($installation[$section][$f.'.installed']))
  293.                             $installation[$section][$f.'.installed'] = 0;
  294.  
  295.                         if ($f == 'jelix') {
  296.                             $config->modules['jelix.access'] = 2; // the jelix module should always be public
  297.                         }
  298.                         else {
  299.                             if ($config->enableAllModules) {
  300.                                 if ($config->disableInstallers
  301.                                     || $installation[$section][$f.'.installed']
  302.                                     || $allModuleInfo)
  303.                                     $config->modules[$f.'.access'] = 2;
  304.                                 else
  305.                                     $config->modules[$f.'.access'] = 0;
  306.                             }
  307.                             else if (!isset($config->modules[$f.'.access'])) {
  308.                                 // no given access in defaultconfig and ep config
  309.                                 $config->modules[$f.'.access'] = 0;
  310.                             }
  311.                             else if($config->modules[$f.'.access'] == 0){
  312.                                 // we want to activate the module if it is not activated
  313.                                 // for the entry point, but is declared activated
  314.                                 // in the default config file. In this case, it means
  315.                                 // that it is activated for an other entry point,
  316.                                 // and then we want the possibility to retrieve its
  317.                                 // urls, at least
  318.                                 if (isset(self::$commonConfig['modules'][$f.'.access'])
  319.                                     && self::$commonConfig['modules'][$f.'.access'] > 0)
  320.                                     $config->modules[$f.'.access'] = 3;
  321.                             }
  322.                             else if (!$installation[$section][$f.'.installed']) {
  323.                                 // module is not installed.
  324.                                 // outside installation mode, we force the access to 0
  325.                                 // so the module is unusable until it is installed
  326.                                 if (!$allModuleInfo)
  327.                                     $config->modules[$f.'.access'] = 0;
  328.                             }
  329.                         }
  330.  
  331.                         if (!isset($installation[$section][$f.'.dbprofile']))
  332.                             $config->modules[$f.'.dbprofile'] = 'default';
  333.                         else
  334.                             $config->modules[$f.'.dbprofile'] = $installation[$section][$f.'.dbprofile'];
  335.  
  336.                         if ($allModuleInfo) {
  337.                             if (!isset($installation[$section][$f.'.version']))
  338.                                 $installation[$section][$f.'.version'] = '';
  339.  
  340.                             if (!isset($installation[$section][$f.'.dataversion']))
  341.                                 $installation[$section][$f.'.dataversion'] = '';
  342.  
  343.                             if (!isset($installation['__modules_data'][$f.'.contexts']))
  344.                                 $installation['__modules_data'][$f.'.contexts'] = '';
  345.  
  346.                             $config->modules[$f.'.version'] = $installation[$section][$f.'.version'];
  347.                             $config->modules[$f.'.dataversion'] = $installation[$section][$f.'.dataversion'];
  348.                             $config->modules[$f.'.installed'] = $installation[$section][$f.'.installed'];
  349.  
  350.                             $config->_allModulesPathList[$f]=$p.$f.'/';
  351.                         }
  352.  
  353.                         if ($config->modules[$f.'.access'] == 3) {
  354.                             $config->_externalModulesPathList[$f]=$p.$f.'/';
  355.                         }
  356.                         elseif ($config->modules[$f.'.access']) {
  357.                             $config->_modulesPathList[$f]=$p.$f.'/';
  358.                             self::readModuleFile($config, $p.$f.'/');
  359.                             if (file_exists( $p.$f.'/plugins')) {
  360.                                 $config->pluginsPath .= ',module:'.$f;
  361.                             }
  362.                         }
  363.                     }
  364.                 }
  365.                 closedir($handle);
  366.             }
  367.         }
  368.     }
  369.  
  370.     static protected function readModuleFile($config, $path) {
  371.         $xml = simplexml_load_file($path.'module.xml');
  372.         if (!isset($xml->autoload))
  373.             return;
  374.         foreach($xml->autoload->children() as $type=>$element) {
  375.             if (isset($element['suffix']))
  376.                 $suffix = '|'.(string)$element['suffix'];
  377.             else
  378.                 $suffix = '|.php';
  379.             switch ($type) {
  380.                 case 'class':
  381.                     $p = $path.((string)$element['file']);
  382.                     if (!file_exists($p))
  383.                         throw new Exception ('Error in autoload configuration -- In '.$path.'/module.xml, this class file doesn\'t exists: '.$p);
  384.                     $config->_autoload_class[(string)$element['name']] = $p;
  385.                     break;
  386.                 case 'classPattern':
  387.                     $p = $path.((string)$element['dir']);
  388.                     if (!file_exists($p))
  389.                         throw new Exception ('Error in the autoload configuration -- In '.$path.'/module.xml, this directory for classPattern doesn\'t exists: '.$p);
  390.                     if (!isset($config->_autoload_classpattern['regexp'])) {
  391.                         $config->_autoload_classpattern['regexp'] = array();
  392.                         $config->_autoload_classpattern['path'] = array();
  393.                     }
  394.                     $config->_autoload_classpattern['regexp'][] = (string)$element['pattern'];
  395.                     $config->_autoload_classpattern['path'][] =  $p.$suffix;
  396.                     break;
  397.                 case 'namespace':
  398.                     $p = $path.((string)$element['dir']);
  399.                     if (!file_exists($p))
  400.                         throw new Exception ('Error in the autoload configuration -- In '.$path.'/module.xml, this directory for namespace doesn\'t exists: '.$p);
  401.                     $config->_autoload_namespace[trim((string)$element['name'],'\\')] = $p.$suffix;
  402.                     break;
  403.                 case 'namespacePathMap':
  404.                     $p = $path.((string)$element['dir']);
  405.                     if (!file_exists($p))
  406.                         throw new Exception ('Error in autoload configuration -- In '.$path.'/module.xml, this directory for namespacePathMap doesn\'t exists: '.$p);
  407.                     $config->_autoload_namespacepathmap[trim((string)$element['name'],'\\')] = $p.$suffix;
  408.                     break;
  409.                 case 'includePath':
  410.                     $p = $path.((string)$element['dir']);
  411.                     if (!file_exists($p))
  412.                         throw new Exception ('Error in autoload configuration -- In '.$path.'/module.xml, this directory for includePath doesn\'t exists: '.$p);
  413.                     if (!isset($config->_autoload_includepath['path'])) {
  414.                         $config->_autoload_includepath['path'] = array();
  415.                     }
  416.                     $config->_autoload_includepath['path'][] =  $p.$suffix;
  417.                     break;
  418.                 case 'autoloader':
  419.                     $p = $path.((string)$element['file']);
  420.                     if (!file_exists($p))
  421.                         throw new Exception ('Error in autoload configuration -- In '.$path.'/module.xml, this autoloader doesn\'t exists: '.$p);
  422.                     $config->_autoload_autoloader[] = $p;
  423.                     break;
  424.             }
  425.         }
  426.     }
  427.  
  428.     /**
  429.      * Analyse plugin paths
  430.      * @param object $config the config container
  431.      */
  432.     static protected function _loadPluginsPathList($config) {
  433.         $list = preg_split('/ *, */',$config->pluginsPath);
  434.         array_unshift($list, JELIX_LIB_PATH.'plugins/');
  435.         foreach($list as $k=>$path){
  436.             if(trim($path) == '') continue;
  437.             if (preg_match('@^module:([^/]+)(/.*)?$@', $path, $m)) {
  438.                 $mod = $m[1];
  439.                 if (isset($config->_modulesPathList[$mod])) {
  440.                     $p = $config->_modulesPathList[$mod];
  441.                     if (isset($m[2]) && strlen($m[2]) > 1)
  442.                         $p.=$m[2];
  443.                     else
  444.                         $p.= '/plugins/';
  445.                 }
  446.                 else {
  447.                     trigger_error('Error in main configuration on pluginsPath -- Path given in pluginsPath for the module '.$mod.' is ignored, since this module is unknown or deactivated', E_USER_NOTICE);
  448.                     continue;
  449.                 }
  450.             }
  451.             else {
  452.                 $p = str_replace(array('lib:','app:'), array(LIB_PATH, jApp::appPath()), $path);
  453.             }
  454.             if(!file_exists($p)){
  455.                 trigger_error('Error in main configuration on pluginsPath -- The path, '.$path.' given in the jelix config, doesn\'t exists !',E_USER_ERROR);
  456.                 exit;
  457.             }
  458.             if(substr($p,-1) !='/')
  459.                 $p.='/';
  460.  
  461.             if ($handle = opendir($p)) {
  462.                 while (false !== ($f = readdir($handle))) {
  463.                     if ($f[0] != '.' && is_dir($p.$f)) {
  464.                         if($subdir = opendir($p.$f)){
  465.                             if($k!=0 && $config->compilation['checkCacheFiletime'])
  466.                                $config->_allBasePath[]=$p.$f.'/';
  467.                             while (false !== ($subf = readdir($subdir))) {
  468.                                 if ($subf[0] != '.' && is_dir($p.$f.'/'.$subf)) {
  469.                                     if($f == 'tpl'){
  470.                                         $prop = '_tplpluginsPathList_'.$subf;
  471.                                         $config->{$prop}[] = $p.$f.'/'.$subf.'/';
  472.                                     }else{
  473.                                         $prop = '_pluginsPathList_'.$f;
  474.                                         $config->{$prop}[$subf] = $p.$f.'/'.$subf.'/';
  475.                                     }
  476.                                 }
  477.                             }
  478.                             closedir($subdir);
  479.                         }
  480.                     }
  481.                 }
  482.                 closedir($handle);
  483.             }
  484.         }
  485.     }
  486.  
  487.     /**
  488.      * calculate miscelaneous path, depending of the server configuration and other informations
  489.      * in the given array : script path, script name, documentRoot ..
  490.      * @param array $urlconf  urlengine configuration. scriptNameServerVariable, basePath,
  491.      *  jelixWWWPath, jqueryPath and entrypointExtension should be present
  492.      */
  493.     static public function getPaths(&$urlconf, $pseudoScriptName ='', $isCli = false) {
  494.         // retrieve the script path+name.
  495.         // for cli, it will be the path from the directory were we execute the script (given to the php exec).
  496.         // for web, it is the path from the root of the url
  497.  
  498.         if ($pseudoScriptName) {
  499.             $urlconf['urlScript'] = $pseudoScriptName;
  500.         }
  501.         else {
  502.             if($urlconf['scriptNameServerVariable'] == '') {
  503.                 $urlconf['scriptNameServerVariable'] = self::findServerName($urlconf['entrypointExtension'], $isCli);
  504.             }
  505.             $urlconf['urlScript'] = $_SERVER[$urlconf['scriptNameServerVariable']];
  506.         }
  507.  
  508.         // now we separate the path and the name of the script, and then the basePath
  509.         if ($isCli) {
  510.             $lastslash = strrpos ($urlconf['urlScript'], DIRECTORY_SEPARATOR);
  511.             if ($lastslash === false) {
  512.                 $urlconf['urlScriptPath'] = ($pseudoScriptName? jApp::appPath('/scripts/'): getcwd().'/');
  513.                 $urlconf['urlScriptName'] = $urlconf['urlScript'];
  514.             }
  515.             else {
  516.                 $urlconf['urlScriptPath'] = getcwd().'/'.substr ($urlconf['urlScript'], 0, $lastslash ).'/';
  517.                 $urlconf['urlScriptName'] = substr ($urlconf['urlScript'], $lastslash+1);
  518.             }
  519.             $basepath = $urlconf['urlScriptPath'];
  520.             $snp = $urlconf['urlScriptName'];
  521.             $urlconf['urlScript'] = $basepath.$snp;
  522.         }
  523.         else {
  524.             $lastslash = strrpos ($urlconf['urlScript'], '/');
  525.             $urlconf['urlScriptPath'] = substr ($urlconf['urlScript'], 0, $lastslash ).'/';
  526.             $urlconf['urlScriptName'] = substr ($urlconf['urlScript'], $lastslash+1);
  527.  
  528.             $basepath = $urlconf['basePath'];
  529.             if ($basepath == '') {
  530.                 // for beginners or simple site, we "guess" the base path
  531.                 $basepath = $localBasePath = $urlconf['urlScriptPath'];
  532.             }
  533.             else {
  534.                 if ($basepath != '/') {
  535.                     if($basepath[0] != '/') $basepath='/'.$basepath;
  536.                     if(substr($basepath,-1) != '/') $basepath.='/';
  537.                 }
  538.  
  539.                 if ($pseudoScriptName) {
  540.                     // with pseudoScriptName, we aren't in a true context, we could be in a cli context
  541.                     // (the installer), and we want the path like when we are in a web context.
  542.                     // $pseudoScriptName is supposed to be relative to the basePath
  543.                     $urlconf['urlScriptPath'] = substr($basepath,0,-1).$urlconf['urlScriptPath'];
  544.                     $urlconf['urlScript'] = $urlconf['urlScriptPath'].$urlconf['urlScriptName'];
  545.                 }
  546.                 $localBasePath = $basepath;
  547.                 if ($urlconf['backendBasePath']) {
  548.                     $localBasePath = $urlconf['backendBasePath'];
  549.                     // we have to change urlScriptPath. it may contains the base path of the backend server
  550.                     // we should replace this base path by the basePath of the frontend server
  551.                     if (strpos($urlconf['urlScriptPath'], $urlconf['backendBasePath']) === 0) {
  552.                         $urlconf['urlScriptPath'] = $basepath.substr( $urlconf['urlScriptPath'], strlen($urlconf['backendBasePath']));
  553.                     }
  554.                     else  {
  555.                         $urlconf['urlScriptPath'] = $basepath.substr($urlconf['urlScriptPath'], 1);
  556.                     }
  557.  
  558.                 }elseif(strpos($urlconf['urlScriptPath'], $basepath) !== 0) {
  559.                     throw new Exception('Error in main configuration on basePath -- basePath ('.$basepath.') in config file doesn\'t correspond to current base path. You should setup it to '.$urlconf['urlScriptPath']);
  560.                 }
  561.             }
  562.  
  563.             $urlconf['basePath'] = $basepath;
  564.  
  565.             if($urlconf['jelixWWWPath'][0] != '/')
  566.                 $urlconf['jelixWWWPath'] = $basepath.$urlconf['jelixWWWPath'];
  567.             if($urlconf['jqueryPath'][0] != '/')
  568.                 $urlconf['jqueryPath'] = $basepath.$urlconf['jqueryPath'];
  569.             $snp = substr($urlconf['urlScript'],strlen($localBasePath));
  570.  
  571.             if ($localBasePath == '/')
  572.                 $urlconf['documentRoot'] = jApp::wwwPath();
  573.             else if(strpos(jApp::wwwPath(), $localBasePath) === false) {
  574.                 if (isset($_SERVER['DOCUMENT_ROOT']))
  575.                     $urlconf['documentRoot'] = $_SERVER['DOCUMENT_ROOT'];
  576.                 else
  577.                     $urlconf['documentRoot'] = jApp::wwwPath();
  578.             }
  579.             else
  580.                 $urlconf['documentRoot'] = substr(jApp::wwwPath(), 0, - (strlen($localBasePath)));
  581.         }
  582.  
  583.         $pos = strrpos($snp, $urlconf['entrypointExtension']);
  584.         if($pos !== false){
  585.             $snp = substr($snp,0,$pos);
  586.         }
  587.         $urlconf['urlScriptId'] = $snp;
  588.         $urlconf['urlScriptIdenc'] = rawurlencode($snp);
  589.     }
  590.  
  591.     static public function findServerName($ext = '.php', $isCli = false) {
  592.         $varname = '';
  593.         $extlen = strlen($ext);
  594.  
  595.         if(strrpos($_SERVER['SCRIPT_NAME'], $ext) === (strlen($_SERVER['SCRIPT_NAME']) - $extlen)
  596.            || $isCli) {
  597.             return 'SCRIPT_NAME';
  598.         }else if (isset($_SERVER['REDIRECT_URL'])
  599.                   && strrpos( $_SERVER['REDIRECT_URL'], $ext) === (strlen( $_SERVER['REDIRECT_URL']) -$extlen)) {
  600.             return 'REDIRECT_URL';
  601.         }else if (isset($_SERVER['ORIG_SCRIPT_NAME'])
  602.                   && strrpos( $_SERVER['ORIG_SCRIPT_NAME'], $ext) === (strlen( $_SERVER['ORIG_SCRIPT_NAME']) - $extlen)) {
  603.             return 'ORIG_SCRIPT_NAME';
  604.         }
  605.         throw new Exception('Error in main configuration on URL engine parameters -- In config file the parameter urlengine:scriptNameServerVariable is empty and Jelix doesn\'t find
  606.             the variable in $_SERVER which contains the script name. You must see phpinfo and setup this parameter in your config file.', 11);
  607.     }
  608.  
  609.     /**
  610.      * get all physical paths of responses file
  611.      */
  612.     static private function _initResponsesPath($config, $list){
  613.         $copylist = $config->$list; // because we modify $list and then it will search for "foo.path" responses...
  614.         foreach ($copylist as $type=>$class) {
  615.             if (strpos($class,'app:') === 0) {
  616.                 $config->{$list}[$type] = $class = substr($class, 4);
  617.                 $config->{$list}[$type.'.path'] = $path = jApp::appPath('responses/'.$class.'.class.php');
  618.                 if (file_exists($path))
  619.                     continue;
  620.             }
  621.             else if (preg_match('@^(?:module:)?([^~]+)~(.+)$@', $class, $m)) {
  622.                 $mod = $m[1];
  623.                 if (isset($config->_modulesPathList[$mod])) {
  624.                     $class = $m[2];
  625.                     $path = $config->_modulesPathList[$mod].'responses/'.$class.'.class.php';
  626.                     $config->{$list}[$type] = $class;
  627.                     $config->{$list}[$type.'.path'] = $path;
  628.                     if (file_exists($path))
  629.                         continue;
  630.                 }
  631.                 else
  632.                     $path = $class;
  633.             }
  634.             else if (file_exists($path=JELIX_LIB_CORE_PATH.'response/'.$class.'.class.php')) {
  635.                 $config->{$list}[$type.'.path'] = $path;
  636.                 continue;
  637.             }
  638.             else if (file_exists($path=jApp::appPath('responses/'.$class.'.class.php'))) {
  639.                 $config->{$list}[$type.'.path'] = $path;
  640.                 continue;
  641.             }
  642.             throw new Exception('Error in main configuration on responses parameters -- the class file of the response type "'.$type.'" is not found ('.$path.')',12);
  643.         }
  644.     }
  645.  
  646.     /**
  647.      * merge two array which are the result of a parse_ini_file call
  648.      * @param array $array the main array
  649.      * @param array $tomerge the array to merge in the first one
  650.      */
  651.     static private function _mergeConfig(&$array, $tomerge){
  652.  
  653.         foreach($tomerge as $k=>$v){
  654.             if(!isset($array[$k])){
  655.                 $array[$k] = $v;
  656.                 continue;
  657.             }
  658.             if($k[1] == '_')
  659.                 continue;
  660.             if(is_array($v)){
  661.                 $array[$k] = array_merge($array[$k], $v);
  662.             }else{
  663.                 $array[$k] = $v;
  664.             }
  665.         }
  666.  
  667.     }
  668. }

Documentation generated on Mon, 26 Oct 2015 21:52:10 +0100 by phpDocumentor 1.4.3