Exemplo n.º 1
0
 static function init($index = "index.php", $force_VirtualHost = false)
 {
     $isCGI = substr(php_sapi_name(), 0, 3) == 'cgi';
     $instance = eZSys::instance();
     if (eZSys::isDebugEnabled()) {
         eZDebug::writeNotice(eZSys::serverVariable('PHP_SELF'), 'PHP_SELF');
         eZDebug::writeNotice(eZSys::serverVariable('SCRIPT_FILENAME'), 'SCRIPT_FILENAME');
         eZDebug::writeNotice(eZSys::serverVariable('DOCUMENT_ROOT'), 'DOCUMENT_ROOT');
         eZDebug::writeNotice(eZSys::serverVariable('REQUEST_URI'), 'REQUEST_URI');
         eZDebug::writeNotice(eZSys::serverVariable('QUERY_STRING'), 'QUERY_STRING');
         eZDebug::writeNotice(ini_get('include_path'), 'include_path');
     }
     $phpSelf = eZSys::serverVariable('PHP_SELF');
     // Find out, where our files are.
     if (preg_match("!(.*/){$index}\$!", eZSys::serverVariable('SCRIPT_FILENAME'), $regs)) {
         $siteDir = $regs[1];
         $index = "/{$index}";
     } elseif (preg_match("!(.*/){$index}/?!", $phpSelf, $regs)) {
         // Some people using CGI have their $_SERVER['SCRIPT_FILENAME'] not right... so we are trying this.
         $siteDir = eZSys::serverVariable('DOCUMENT_ROOT') . $regs[1];
         $index = "/{$index}";
     } else {
         // Fallback... doesn't work with virtual-hosts, but better than nothing
         $siteDir = "./";
         $index = "/{$index}";
     }
     if ($isCGI and !$force_VirtualHost) {
         $index .= '?';
     }
     // Setting the right include_path
     $includePath = ini_get("include_path");
     if (trim($includePath) != "") {
         $includePath = $includePath . $instance->envSeparator() . $siteDir;
     } else {
         $includePath = $siteDir;
     }
     ini_set("include_path", $includePath);
     $scriptName = eZSys::serverVariable('SCRIPT_NAME');
     // Get the webdir.
     $wwwDir = "";
     if ($force_VirtualHost) {
         $wwwDir = "";
     } else {
         if (preg_match("!(.*){$index}\$!", $scriptName, $regs)) {
             $wwwDir = $regs[1];
         }
         if (preg_match("!(.*){$index}\$!", $phpSelf, $regs)) {
             $wwwDir = $regs[1];
         }
     }
     if (!$isCGI || $force_VirtualHost) {
         $requestURI = eZSys::serverVariable('REQUEST_URI');
     } else {
         $requestURI = eZSys::serverVariable('QUERY_STRING');
         /* take out PHPSESSID, if url-encoded */
         if (preg_match("/(.*)&PHPSESSID=[^&]+(.*)/", $requestURI, $matches)) {
             $requestURI = $matches[1] . $matches[2];
         }
     }
     // Fallback... Finding the paths above failed, so $_SERVER['PHP_SELF'] is not set right.
     if ($siteDir == "./") {
         $phpSelf = $requestURI;
     }
     if (!$isCGI) {
         $index_reg = str_replace(".", "\\.", $index);
         // Trick: Rewrite setup doesn't have index.php in $_SERVER['PHP_SELF'], so we don't want an $index
         if (!preg_match("!.*{$index_reg}.*!", $phpSelf) || $force_VirtualHost) {
             $index = "";
         } else {
             if (eZSys::isDebugEnabled()) {
                 eZDebug::writeNotice("{$wwwDir}{$index}", '$wwwDir$index');
             }
             // Get the right $_SERVER['REQUEST_URI'], when using nVH setup.
             if (preg_match("!^{$wwwDir}{$index}(.*)!", $phpSelf, $req)) {
                 if (!$req[1]) {
                     if ($phpSelf != "{$wwwDir}{$index}" and preg_match("!^{$wwwDir}(.*)!", $requestURI, $req)) {
                         $requestURI = $req[1];
                         $index = '';
                     } elseif ($phpSelf == "{$wwwDir}{$index}" and (preg_match("!^{$wwwDir}{$index}(.*)!", $requestURI, $req) or preg_match("!^{$wwwDir}(.*)!", $requestURI, $req))) {
                         $requestURI = $req[1];
                     }
                 } else {
                     $requestURI = $req[1];
                 }
             }
         }
     }
     if ($isCGI and $force_VirtualHost) {
         $index = '';
     }
     // Remove url parameters
     if ($isCGI and !$force_VirtualHost) {
         $pattern = "!(\\/[^&]+)!";
     } else {
         $pattern = "!([^?]+)!";
     }
     if (preg_match($pattern, $requestURI, $regs)) {
         $requestURI = $regs[1];
     }
     // Remove internal links
     if (preg_match("!([^#]+)!", $requestURI, $regs)) {
         $requestURI = $regs[1];
     }
     if (!$isCGI) {
         $currentPath = substr(eZSys::serverVariable('SCRIPT_FILENAME'), 0, -strlen('index.php'));
         if (strpos($currentPath, eZSys::serverVariable('DOCUMENT_ROOT')) === 0) {
             $prependRequest = substr($currentPath, strlen(eZSys::serverVariable('DOCUMENT_ROOT')));
             if (strpos($requestURI, $prependRequest) === 0) {
                 $requestURI = substr($requestURI, strlen($prependRequest) - 1);
                 $wwwDir = substr($prependRequest, 0, -1);
             }
         }
     }
     $instance->AccessPath = array('siteaccess' => array('name' => '', 'url' => array()), 'path' => array('name' => '', 'url' => array()));
     $instance->SiteDir = $siteDir;
     $instance->WWWDir = $wwwDir;
     $instance->IndexFile = $index;
     $instance->RequestURI = $requestURI;
     if (eZSys::isDebugEnabled()) {
         eZDebug::writeNotice($instance->SiteDir, 'SiteDir');
         eZDebug::writeNotice($instance->WWWDir, 'WWWDir');
         eZDebug::writeNotice($instance->IndexFile, 'IndexFile');
         eZDebug::writeNotice(eZSys::requestURI(), 'eZSys::requestURI()');
     }
 }