示例#1
0
    /**
     * Goes trough the access matching rules and returns the access match.
     * The returned match is an associative array with:
     *  name     => string Name of the siteaccess (same as folder name)
     *  type     => int The constant that represent the matching used
     *  uri_part => array(string) List of path elements that was used in start of url for the match
     *
     * @since 4.4
     * @param eZURI $uri
     * @param string $host
     * @param string(numeric) $port
     * @param string $file Example '/index.php'
     * @return array
     */
    public static function match( eZURI $uri, $host, $port = 80, $file = '/index.php' )
    {
        eZDebugSetting::writeDebug( 'kernel-siteaccess', array( 'uri' => $uri,
                                                                'host' => $host,
                                                                'port' => $port,
                                                                'file' => $file ), __METHOD__ );
        $ini = eZINI::instance();
        if ( $ini->hasVariable( 'SiteAccessSettings', 'StaticMatch' ) )
        {
            $match = $ini->variable( 'SiteAccessSettings', 'StaticMatch' );
            if ( $match != '' )
            {
                $access = array( 'name' => $match,
                                 'type' => eZSiteAccess::TYPE_STATIC,
                                 'uri_part' => array() );
                return $access;
            }
        }

        list( $siteAccessList, $order ) =
            $ini->variableMulti( 'SiteAccessSettings', array( 'AvailableSiteAccessList', 'MatchOrder' ) );
        $access = array( 'name' => $ini->variable( 'SiteSettings', 'DefaultAccess' ),
                         'type' => eZSiteAccess::TYPE_DEFAULT,
                         'uri_part' => array() );

        if ( $order == 'none' )
            return $access;

        $order = $ini->variableArray( 'SiteAccessSettings', 'MatchOrder' );

        // Change the default type to eZSiteAccess::TYPE_URI if we're using URI MatchOrder.
        // This is to keep backward compatiblity with the ezurl operator. ezurl has since
        // rev 4949 added default siteaccess to generated URLs, even when there is
        // no siteaccess in the current URL.
        if ( in_array( 'uri', $order ) )
        {
            $access['type'] = eZSiteAccess::TYPE_URI;
        }

        foreach ( $order as $matchprobe )
        {
            $name = '';
            $type = '';
            $match_type = '';
            $uri_part = array();

            switch( $matchprobe )
            {
                case 'servervar':
                {
                    if ( $serversiteaccess = eZSys::serverVariable( $ini->variable( 'SiteAccessSettings', 'ServerVariableName' ), true ) )
                    {
                        $access['name'] = $serversiteaccess;
                        $access['type'] = eZSiteAccess::TYPE_SERVER_VAR;
                        return $access;
                    }
                    else
                        continue;
                } break;
                case 'port':
                {
                    if ( $ini->hasVariable( 'PortAccessSettings', $port ) )
                    {
                        $access['name'] = $ini->variable( 'PortAccessSettings', $port );
                        $access['type'] = eZSiteAccess::TYPE_PORT;
                        return $access;
                    }
                    else
                        continue;
                } break;
                case 'uri':
                {
                    $type = eZSiteAccess::TYPE_URI;
                    $match_type = $ini->variable( 'SiteAccessSettings', 'URIMatchType' );

                    if ( $match_type == 'map' )
                    {
                        if ( $ini->hasVariable( 'SiteAccessSettings', 'URIMatchMapItems' ) )
                        {
                            $match_item = $uri->element( 0 );
                            $matchMapItems = $ini->variableArray( 'SiteAccessSettings', 'URIMatchMapItems' );
                            foreach ( $matchMapItems as $matchMapItem )
                            {
                                $matchMapURI = $matchMapItem[0];
                                $matchMapAccess = $matchMapItem[1];
                                if ( $access['name']  == $matchMapAccess and in_array( $matchMapAccess, $siteAccessList ) )
                                {
                                    $uri_part = array( $matchMapURI );
                                }
                                if ( $matchMapURI == $match_item and in_array( $matchMapAccess, $siteAccessList ) )
                                {
                                    $uri->increase( 1 );
                                    $uri->dropBase();
                                    $access['name'] = $matchMapAccess;
                                    $access['type'] = $type;
                                    $access['uri_part'] = array( $matchMapURI );
                                    return $access;
                                }
                            }
                        }
                    }
                    else if ( $match_type == 'element' )
                    {
                        $match_index = $ini->variable( 'SiteAccessSettings', 'URIMatchElement' );
                        $elements = $uri->elements( false );
                        $elements = array_slice( $elements, 0, $match_index );
                        $name = implode( '_', $elements );
                        $uri_part = $elements;
                    }
                    else if ( $match_type == 'text' )
                    {
                        $match_item = $uri->elements();
                        $matcher_pre = $ini->variable( 'SiteAccessSettings', 'URIMatchSubtextPre' );
                        $matcher_post = $ini->variable( 'SiteAccessSettings', 'URIMatchSubtextPost' );
                    }
                    else if ( $match_type == 'regexp' )
                    {
                        $match_item = $uri->elements();
                        $matcher = $ini->variable( 'SiteAccessSettings', 'URIMatchRegexp' );
                        $match_num = $ini->variable( 'SiteAccessSettings', 'URIMatchRegexpItem' );
                    }
                    else
                        continue;
                } break;
                case 'host':
                {
                    $type = eZSiteAccess::TYPE_HTTP_HOST;
                    $match_type = $ini->variable( 'SiteAccessSettings', 'HostMatchType' );
                    $match_item = $host;
                    if ( $match_type == 'map' )
                    {
                        if ( $ini->hasVariable( 'SiteAccessSettings', 'HostMatchMapItems' ) )
                        {
                            $matchMapItems = $ini->variableArray( 'SiteAccessSettings', 'HostMatchMapItems' );
                            foreach ( $matchMapItems as $matchMapItem )
                            {
                                $matchMapHost = $matchMapItem[0];
                                $matchMapAccess = $matchMapItem[1];
                                if ( $matchMapHost == $host )
                                {
                                    $access['name'] = $matchMapAccess;
                                    $access['type'] = $type;
                                    return $access;
                                }
                            }
                        }
                    }
                    else if ( $match_type == 'element' )
                    {
                        $match_index = $ini->variable( 'SiteAccessSettings', 'HostMatchElement' );
                        $match_arr = explode( '.', $match_item );
                        $name = $match_arr[$match_index];
                    }
                    else if ( $match_type == 'text' )
                    {
                        $matcher_pre = $ini->variable( 'SiteAccessSettings', 'HostMatchSubtextPre' );
                        $matcher_post = $ini->variable( 'SiteAccessSettings', 'HostMatchSubtextPost' );
                    }
                    else if ( $match_type == 'regexp' )
                    {
                        $matcher = $ini->variable( 'SiteAccessSettings', 'HostMatchRegexp' );
                        $match_num = $ini->variable( 'SiteAccessSettings', 'HostMatchRegexpItem' );
                    }
                    else
                        continue;
                } break;
                case 'host_uri':
                {
                    $type = eZSiteAccess::TYPE_HTTP_HOST_URI;
                    if ( $ini->hasVariable( 'SiteAccessSettings', 'HostUriMatchMapItems' ) )
                    {
                        $uriString = $uri->elements();
                        $matchMapItems = $ini->variableArray( 'SiteAccessSettings', 'HostUriMatchMapItems' );
                        $defaultHostMatchMethod = $ini->variable( 'SiteAccessSettings', 'HostUriMatchMethodDefault' );

                        foreach ( $matchMapItems as $matchMapItem )
                        {
                            $matchHost       = $matchMapItem[0];
                            $matchURI        = $matchMapItem[1];
                            $matchAccess     = $matchMapItem[2];
                            $matchHostMethod = isset( $matchMapItem[3] ) ? $matchMapItem[3] : $defaultHostMatchMethod;

                            if ( $matchURI !== '' && strpos($uriString, $matchURI) !== 0 )
                                continue;

                            switch( $matchHostMethod )
                            {
                                case 'strict':
                                {
                                    $hasHostMatch = ( $matchHost === $host );
                                } break;
                                case 'start':
                                {
                                    $hasHostMatch = ( strpos($host, $matchHost) === 0 );
                                } break;
                                case 'end':
                                {
                                    $hasHostMatch = ( strstr($host, $matchHost) === $matchHost );
                                } break;
                                case 'part':
                                {
                                    $hasHostMatch = ( strpos($host, $matchHost) !== false );
                                } break;
                                default:
                                {
                                    $hasHostMatch = false;
                                    eZDebug::writeError( "Unknown host_uri host match: $matchHostMethod", "access" );
                                } break;
                            }

                            if ( $hasHostMatch )
                            {
                                if ( $matchURI !== '' )
                                {
                                    $matchURIFolders = explode( '/', $matchURI );
                                    $uri->increase( count( $matchURIFolders ) );
                                    $uri->dropBase();
                                    $access['uri_part'] = $matchURIFolders;
                                }
                                $access['name'] = $matchAccess;
                                $access['type'] = $type;
                                return $access;
                            }
                        }
                    }
                } break;
                case 'index':
                {
                    $type = eZSiteAccess::TYPE_INDEX_FILE;
                    $match_type = $ini->variable( 'SiteAccessSettings', 'IndexMatchType' );
                    $match_item = $file;
                    if ( $match_type == 'element' )
                    {
                        $match_index = $ini->variable( 'SiteAccessSettings', 'IndexMatchElement' );
                        $match_pos = strpos( $match_item, '.php' );
                        if ( $match_pos !== false )
                        {
                            $match_item = substr( $match_item, 0, $match_pos );
                            $match_arr = explode( '_', $match_item );
                            $name = $match_arr[$match_index];
                        }
                    }
                    else if ( $match_type == 'text' )
                    {
                        $matcher_pre = $ini->variable( 'SiteAccessSettings', 'IndexMatchSubtextPre' );
                        $matcher_post = $ini->variable( 'SiteAccessSettings', 'IndexMatchSubtextPost' );
                    }
                    else if ( $match_type == 'regexp' )
                    {
                        $matcher = $ini->variable( 'SiteAccessSettings', 'IndexMatchRegexp' );
                        $match_num = $ini->variable( 'SiteAccessSettings', 'IndexMatchRegexpItem' );
                    }
                    else
                        continue;
                } break;
                default:
                {
                    eZDebug::writeError( "Unknown access match: $matchprobe", "access" );
                } break;
            }

            if ( $match_type == 'regexp' )
                $name = self::matchRegexp( $match_item, $matcher, $match_num );
            else if ( $match_type == 'text' )
                $name = self::matchText( $match_item, $matcher_pre, $matcher_post );

            if ( isset( $name ) && $name != '' )
            {
                $name = preg_replace( array( '/[^a-zA-Z0-9]+/', '/_+/', '/^_/', '/_$/' ),
                                      array( '_', '_', '', '' ),
                                      $name );

                if ( in_array( $name, $siteAccessList ) )
                {
                    if ( $type == eZSiteAccess::TYPE_URI )
                    {
                        if ( $match_type == 'element' )
                        {
                            $uri->increase( $match_index );
                            $uri->dropBase();
                        }
                        else if ( $match_type == 'regexp' )
                        {
                            $uri->setURIString( $match_item );
                        }
                        else if ( $match_type == 'text' )
                        {
                            $uri->setURIString( $match_item );
                        }
                    }
                    $access['type']     = $type;
                    $access['name']     = $name;
                    $access['uri_part'] = $uri_part;
                    return $access;
                }
            }
        }
        return $access;
    }
 /**
  * Runs the dispatch loop
  */
 protected function dispatchLoop()
 {
     $ini = eZINI::instance();
     // Start the module loop
     while ($this->siteBasics['module-run-required']) {
         $objectHasMovedError = false;
         $objectHasMovedURI = false;
         $this->actualRequestedURI = $this->uri->uriString();
         // Extract user specified parameters
         $userParameters = $this->uri->userParameters();
         // Generate a URI which also includes the user parameters
         $this->completeRequestedURI = $this->uri->originalURIString();
         // Check for URL translation
         if ($this->siteBasics['url-translator-allowed'] && eZURLAliasML::urlTranslationEnabledByUri($this->uri)) {
             $translateResult = eZURLAliasML::translate($this->uri);
             if (!is_string($translateResult) && $ini->variable('URLTranslator', 'WildcardTranslation') === 'enabled') {
                 $translateResult = eZURLWildcard::translate($this->uri);
             }
             // Check if the URL has moved
             if (is_string($translateResult)) {
                 $objectHasMovedURI = $translateResult;
                 foreach ($userParameters as $name => $value) {
                     $objectHasMovedURI .= '/(' . $name . ')/' . $value;
                 }
                 $objectHasMovedError = true;
             }
         }
         if ($this->uri->isEmpty()) {
             $tmp_uri = new eZURI($ini->variable("SiteSettings", "IndexPage"));
             $moduleCheck = eZModule::accessAllowed($tmp_uri);
         } else {
             $moduleCheck = eZModule::accessAllowed($this->uri);
         }
         if (!$moduleCheck['result']) {
             if ($ini->variable("SiteSettings", "ErrorHandler") == "defaultpage") {
                 $defaultPage = $ini->variable("SiteSettings", "DefaultPage");
                 $this->uri->setURIString($defaultPage);
                 $moduleCheck['result'] = true;
             }
         }
         $displayMissingModule = false;
         $this->oldURI = $this->uri;
         if ($this->uri->isEmpty()) {
             if (!fetchModule($tmp_uri, $this->check, $this->module, $moduleName, $functionName, $params)) {
                 $displayMissingModule = true;
             }
         } else {
             if (!fetchModule($this->uri, $this->check, $this->module, $moduleName, $functionName, $params)) {
                 if ($ini->variable("SiteSettings", "ErrorHandler") == "defaultpage") {
                     $tmp_uri = new eZURI($ini->variable("SiteSettings", "DefaultPage"));
                     if (!fetchModule($tmp_uri, $this->check, $this->module, $moduleName, $functionName, $params)) {
                         $displayMissingModule = true;
                     }
                 } else {
                     $displayMissingModule = true;
                 }
             }
         }
         if (!$displayMissingModule && $moduleCheck['result'] && $this->module instanceof eZModule) {
             // Run the module/function
             eZDebug::addTimingPoint("Module start '" . $this->module->attribute('name') . "'");
             $moduleAccessAllowed = true;
             $omitPolicyCheck = true;
             $runModuleView = true;
             $availableViewsInModule = $this->module->attribute('views');
             if (!isset($availableViewsInModule[$functionName]) && !$objectHasMovedError && !isset($this->module->Module['function']['script'])) {
                 $moduleResult = $this->module->handleError(eZError::KERNEL_MODULE_VIEW_NOT_FOUND, 'kernel', array("check" => $moduleCheck));
                 $runModuleView = false;
                 $this->siteBasics['policy-check-required'] = false;
                 $omitPolicyCheck = true;
             }
             if ($this->siteBasics['policy-check-required']) {
                 $omitPolicyCheck = false;
                 $moduleName = $this->module->attribute('name');
                 if (in_array($moduleName, $this->siteBasics['policy-check-omit-list'])) {
                     $omitPolicyCheck = true;
                 } else {
                     $policyCheckViewMap = $this->getPolicyCheckViewMap($this->siteBasics['policy-check-omit-list']);
                     if (isset($policyCheckViewMap[$moduleName][$functionName])) {
                         $omitPolicyCheck = true;
                     }
                 }
             }
             if (!$omitPolicyCheck) {
                 $currentUser = eZUser::currentUser();
                 $siteAccessResult = $currentUser->hasAccessTo('user', 'login');
                 $hasAccessToSite = false;
                 if ($siteAccessResult['accessWord'] === 'limited') {
                     $policyChecked = false;
                     foreach (array_keys($siteAccessResult['policies']) as $key) {
                         $policy = $siteAccessResult['policies'][$key];
                         if (isset($policy['SiteAccess'])) {
                             $policyChecked = true;
                             $crc32AccessName = eZSys::ezcrc32($this->access['name']);
                             eZDebugSetting::writeDebug('kernel-siteaccess', $policy['SiteAccess'], $crc32AccessName);
                             if (in_array($crc32AccessName, $policy['SiteAccess'])) {
                                 $hasAccessToSite = true;
                                 break;
                             }
                         }
                         if ($hasAccessToSite) {
                             break;
                         }
                     }
                     if (!$policyChecked) {
                         $hasAccessToSite = true;
                     }
                 } else {
                     if ($siteAccessResult['accessWord'] === 'yes') {
                         eZDebugSetting::writeDebug('kernel-siteaccess', "access is yes");
                         $hasAccessToSite = true;
                     } else {
                         if ($siteAccessResult['accessWord'] === 'no') {
                             $accessList = $siteAccessResult['accessList'];
                         }
                     }
                 }
                 if ($hasAccessToSite) {
                     $accessParams = array();
                     $moduleAccessAllowed = $currentUser->hasAccessToView($this->module, $functionName, $accessParams);
                     if (isset($accessParams['accessList'])) {
                         $accessList = $accessParams['accessList'];
                     }
                 } else {
                     eZDebugSetting::writeDebug('kernel-siteaccess', $this->access, 'not able to get access to siteaccess');
                     $moduleAccessAllowed = false;
                     if ($ini->variable("SiteAccessSettings", "RequireUserLogin") == "true") {
                         $this->module = eZModule::exists('user');
                         if ($this->module instanceof eZModule) {
                             $moduleResult = $this->module->run('login', array(), array('SiteAccessAllowed' => false, 'SiteAccessName' => $this->access['name']));
                             $runModuleView = false;
                         }
                     }
                 }
             }
             $GLOBALS['eZRequestedModule'] = $this->module;
             if ($runModuleView) {
                 if ($objectHasMovedError == true) {
                     $moduleResult = $this->module->handleError(eZError::KERNEL_MOVED, 'kernel', array('new_location' => $objectHasMovedURI));
                 } else {
                     if (!$moduleAccessAllowed) {
                         if (isset($availableViewsInModule[$functionName]['default_navigation_part'])) {
                             $defaultNavigationPart = $availableViewsInModule[$functionName]['default_navigation_part'];
                         }
                         if (isset($accessList)) {
                             $moduleResult = $this->module->handleError(eZError::KERNEL_ACCESS_DENIED, 'kernel', array('AccessList' => $accessList));
                         } else {
                             $moduleResult = $this->module->handleError(eZError::KERNEL_ACCESS_DENIED, 'kernel');
                         }
                         if (isset($defaultNavigationPart)) {
                             $moduleResult['navigation_part'] = $defaultNavigationPart;
                             unset($defaultNavigationPart);
                         }
                     } else {
                         if (!isset($userParameters)) {
                             $userParameters = false;
                         }
                         // Check if we should switch access mode (http/https) for this module view.
                         eZSSLZone::checkModuleView($this->module->attribute('name'), $functionName);
                         $moduleResult = $this->module->run($functionName, $params, false, $userParameters);
                         if ($this->module->exitStatus() == eZModule::STATUS_FAILED && $moduleResult == null) {
                             $moduleResult = $this->module->handleError(eZError::KERNEL_MODULE_VIEW_NOT_FOUND, 'kernel', array('module' => $moduleName, 'view' => $functionName));
                         }
                     }
                 }
             }
         } else {
             if ($moduleCheck['result']) {
                 eZDebug::writeError("Undefined module: {$moduleName}", "index");
                 $this->module = new eZModule("", "", $moduleName);
                 $GLOBALS['eZRequestedModule'] = $this->module;
                 $moduleResult = $this->module->handleError(eZError::KERNEL_MODULE_NOT_FOUND, 'kernel', array('module' => $moduleName));
             } else {
                 if ($moduleCheck['view_checked']) {
                     eZDebug::writeError("View '" . $moduleCheck['view'] . "' in module '" . $moduleCheck['module'] . "' is disabled", "index");
                 } else {
                     eZDebug::writeError("Module '" . $moduleCheck['module'] . "' is disabled", "index");
                 }
                 $GLOBALS['eZRequestedModule'] = $this->module = new eZModule("", "", $moduleCheck['module']);
                 $moduleResult = $this->module->handleError(eZError::KERNEL_MODULE_DISABLED, 'kernel', array('check' => $moduleCheck));
             }
         }
         $this->siteBasics['module-run-required'] = false;
         if ($this->module->exitStatus() == eZModule::STATUS_RERUN) {
             if (isset($moduleResult['rerun_uri'])) {
                 $this->uri = eZURI::instance($moduleResult['rerun_uri']);
                 $this->siteBasics['module-run-required'] = true;
             } else {
                 eZDebug::writeError('No rerun URI specified, cannot continue', 'index.php');
             }
         }
         if (is_array($moduleResult)) {
             if (isset($moduleResult["pagelayout"])) {
                 $this->siteBasics['show-page-layout'] = $moduleResult["pagelayout"];
                 $GLOBALS['eZCustomPageLayout'] = $moduleResult["pagelayout"];
             }
             if (isset($moduleResult["external_css"])) {
                 $this->siteBasics['external-css'] = $moduleResult["external_css"];
             }
         }
     }
     return $moduleResult;
 }
示例#3
0
    /**
     * Transforms the URI if there exists an alias for it.
     *
     * @param eZURI|string $uri
     * @return mixed The translated URI if the resource has moved, or true|false
     *               if translation was (un)successful
     */
    public static function translate( &$uri )
    {
        $result = false;

        // get uri string
        $uriString = ( $uri instanceof eZURI ) ? $uri->elements() : $uri;
        $uriString = eZURLAliasML::cleanURL( $uriString );

        eZDebugSetting::writeDebug( 'kernel-urltranslator', "input uriString: '$uriString'", __METHOD__ );

        if ( !$wildcards = self::wildcardsIndex() )
        {
            eZDebugSetting::writeDebug( 'kernel-urltranslator', "no match callbacks", __METHOD__ );
            return false;
        }

        $ini = eZINI::instance();
        $iteration = $ini->variable( 'URLTranslator', 'MaximumWildcardIterations' );

        eZDebugSetting::writeDebug( 'kernel-urltranslator', "MaximumWildcardIterations: '$iteration'", __METHOD__ );

        // translate
        $urlTranslated = false;
        while ( !$urlTranslated && $iteration >= 0 )
        {
            foreach ( $wildcards as $wildcardNum => $wildcard )
            {
                if ( preg_match( $wildcard, $uriString ) )
                {
                    eZDebugSetting::writeDebug( 'kernel-urltranslator', "matched with: '$wildcard'", __METHOD__ );

                    // get new $uriString from wildcard
                    self::translateWithCache( $wildcardNum, $uriString, $wildcardInfo, $wildcard );

                    eZDebugSetting::writeDebug( 'kernel-urltranslator', "new uri string: '$uriString'", __METHOD__ );

                    // optimization: don't try further translation if wildcard type is 'forward'
                    if ( $wildcardInfo['type'] == self::TYPE_FORWARD )
                    {
                        $urlTranslated = true;
                        break;
                    }

                    // try to tranlsate
                    if ( $urlTranslated = eZURLAliasML::translate( $uriString ) )
                    {
                        // success
                        eZDebugSetting::writeDebug( 'kernel-urltranslator', "uri is translated to '$uriString' with result '$urlTranslated'", __METHOD__ );
                        break;
                    }

                    eZDebugSetting::writeDebug( 'kernel-urltranslator', "uri is not translated, trying another wildcard", __METHOD__ );

                    // translation failed. Try to match new $uriString with another wildcard.
                    --$iteration;
                    continue 2;
                }
            }

            // we here if non of the wildcards is matched
            break;
        }

        // check translation result
        // NOTE: 'eZURLAliasML::translate'(see above) can return 'true', 'false' or new url(in case of 'error/301').
        //       $urlTranslated can also be 'false' if no wildcard is matched.
        if ( $urlTranslated )
        {
            // check wildcard type and set appropriate $result and $uriString
            $wildcardType = $wildcardInfo['type'];

            eZDebugSetting::writeDebug( 'kernel-urltranslator', "wildcard type: $wildcardType", __METHOD__ );

            switch ( $wildcardType )
            {
                case self::TYPE_FORWARD:
                {
                    // do redirect:
                    //   => set $result to translated uri
                    //   => set uri string to a MOVED PERMANENTLY HTTP code
                    $result = $uriString;
                    $uriString = 'error/301';
                }
                break;

                default:
                {
                    eZDebug::writeError( 'Invalid wildcard type.', __METHOD__ );
                    // no break, using eZURLWildcard::TYPE_DIRECT as fallback
                }
                case self::TYPE_DIRECT:
                {
                    $result = $urlTranslated;
                    // $uriString already has correct value
                    break;
                }
            }
        }
        else
        {
            // we are here if:
            // - input url is not matched with any wildcard;
            // - url is matched with wildcard and:
            //   - points to module
            //   - invalide url
            eZDebugSetting::writeDebug( 'kernel-urltranslator', "wildcard is not translated", __METHOD__ );
            $result = false;
        }

        // set value back to $uri
        if ( $uri instanceof eZURI )
        {
            $uri->setURIString( $uriString, false );
        }
        else
        {
            $uri = $uriString;
        }

        eZDebugSetting::writeDebug( 'kernel-urltranslator', "finished with url '$uriString' and result '$result'", __METHOD__ );

        return $result;
    }
 /**
  * Goes trough the access matching rules and returns the access match.
  * The returned match is an associative array with:
  *  name     => string Name of the siteaccess (same as folder name)
  *  type     => int The constant that represent the matching used
  *  uri_part => array(string) List of path elements that was used in start of url for the match
  *
  * @since 4.4
  * @param eZURI $uri
  * @param string $host
  * @param string(numeric) $port
  * @param string $file Example '/index.php'
  * @return array
  */
 public static function match(eZURI $uri, $host, $port = 80, $file = '/index.php')
 {
     eZDebugSetting::writeDebug('kernel-siteaccess', array('uri' => $uri, 'host' => $host, 'port' => $port, 'file' => $file), __METHOD__);
     $ini = eZINI::instance();
     if ($ini->hasVariable('SiteAccessSettings', 'StaticMatch')) {
         $match = $ini->variable('SiteAccessSettings', 'StaticMatch');
         if ($match != '') {
             $access = array('name' => $match, 'type' => eZSiteAccess::TYPE_STATIC, 'uri_part' => array());
             return $access;
         }
     }
     list($siteAccessList, $order) = $ini->variableMulti('SiteAccessSettings', array('AvailableSiteAccessList', 'MatchOrder'));
     $access = array('name' => $ini->variable('SiteSettings', 'DefaultAccess'), 'type' => eZSiteAccess::TYPE_DEFAULT, 'uri_part' => array());
     if ($order == 'none') {
         return $access;
     }
     $order = $ini->variableArray('SiteAccessSettings', 'MatchOrder');
     // Change the default type to eZSiteAccess::TYPE_URI if we're using URI MatchOrder.
     // This is to keep backward compatiblity with the ezurl operator. ezurl has since
     // rev 4949 added default siteaccess to generated URLs, even when there is
     // no siteaccess in the current URL.
     if (in_array('uri', $order)) {
         $access['type'] = eZSiteAccess::TYPE_URI;
     }
     foreach ($order as $matchprobe) {
         $name = '';
         $type = '';
         $match_type = '';
         $uri_part = array();
         switch ($matchprobe) {
             case 'servervar':
                 if ($serversiteaccess = eZSys::serverVariable($ini->variable('SiteAccessSettings', 'ServerVariableName'), true)) {
                     $access['name'] = $serversiteaccess;
                     $access['type'] = eZSiteAccess::TYPE_SERVER_VAR;
                     return $access;
                 } else {
                     continue;
                 }
                 break;
             case 'port':
                 if ($ini->hasVariable('PortAccessSettings', $port)) {
                     $access['name'] = $ini->variable('PortAccessSettings', $port);
                     $access['type'] = eZSiteAccess::TYPE_PORT;
                     return $access;
                 } else {
                     continue;
                 }
                 break;
             case 'uri':
                 $type = eZSiteAccess::TYPE_URI;
                 $match_type = $ini->variable('SiteAccessSettings', 'URIMatchType');
                 if ($match_type == 'map') {
                     if ($ini->hasVariable('SiteAccessSettings', 'URIMatchMapItems')) {
                         $match_item = $uri->element(0);
                         $matchMapItems = $ini->variableArray('SiteAccessSettings', 'URIMatchMapItems');
                         foreach ($matchMapItems as $matchMapItem) {
                             $matchMapURI = $matchMapItem[0];
                             $matchMapAccess = $matchMapItem[1];
                             if ($access['name'] == $matchMapAccess and in_array($matchMapAccess, $siteAccessList)) {
                                 $uri_part = array($matchMapURI);
                             }
                             if ($matchMapURI == $match_item and in_array($matchMapAccess, $siteAccessList)) {
                                 $uri->increase(1);
                                 $uri->dropBase();
                                 $access['name'] = $matchMapAccess;
                                 $access['type'] = $type;
                                 $access['uri_part'] = array($matchMapURI);
                                 return $access;
                             }
                         }
                     }
                 } else {
                     if ($match_type == 'element') {
                         $match_index = $ini->variable('SiteAccessSettings', 'URIMatchElement');
                         $elements = $uri->elements(false);
                         $elements = array_slice($elements, 0, $match_index);
                         $name = implode('_', $elements);
                         $uri_part = $elements;
                     } else {
                         if ($match_type == 'text') {
                             $match_item = $uri->elements();
                             $matcher_pre = $ini->variable('SiteAccessSettings', 'URIMatchSubtextPre');
                             $matcher_post = $ini->variable('SiteAccessSettings', 'URIMatchSubtextPost');
                         } else {
                             if ($match_type == 'regexp') {
                                 $match_item = $uri->elements();
                                 $matcher = $ini->variable('SiteAccessSettings', 'URIMatchRegexp');
                                 $match_num = $ini->variable('SiteAccessSettings', 'URIMatchRegexpItem');
                             } else {
                                 continue;
                             }
                         }
                     }
                 }
                 break;
             case 'host':
                 $type = eZSiteAccess::TYPE_HTTP_HOST;
                 $match_type = $ini->variable('SiteAccessSettings', 'HostMatchType');
                 $match_item = $host;
                 if ($match_type == 'map') {
                     if ($ini->hasVariable('SiteAccessSettings', 'HostMatchMapItems')) {
                         $matchMapItems = $ini->variableArray('SiteAccessSettings', 'HostMatchMapItems');
                         foreach ($matchMapItems as $matchMapItem) {
                             $matchMapHost = $matchMapItem[0];
                             $matchMapAccess = $matchMapItem[1];
                             if ($matchMapHost == $host) {
                                 $access['name'] = $matchMapAccess;
                                 $access['type'] = $type;
                                 return $access;
                             }
                         }
                     }
                 } else {
                     if ($match_type == 'element') {
                         $match_index = $ini->variable('SiteAccessSettings', 'HostMatchElement');
                         $match_arr = explode('.', $match_item);
                         $name = $match_arr[$match_index];
                     } else {
                         if ($match_type == 'text') {
                             $matcher_pre = $ini->variable('SiteAccessSettings', 'HostMatchSubtextPre');
                             $matcher_post = $ini->variable('SiteAccessSettings', 'HostMatchSubtextPost');
                         } else {
                             if ($match_type == 'regexp') {
                                 $matcher = $ini->variable('SiteAccessSettings', 'HostMatchRegexp');
                                 $match_num = $ini->variable('SiteAccessSettings', 'HostMatchRegexpItem');
                             } else {
                                 continue;
                             }
                         }
                     }
                 }
                 break;
             case 'host_uri':
                 $type = eZSiteAccess::TYPE_HTTP_HOST_URI;
                 if ($ini->hasVariable('SiteAccessSettings', 'HostUriMatchMapItems')) {
                     $uriString = $uri->elements();
                     $matchMapItems = $ini->variableArray('SiteAccessSettings', 'HostUriMatchMapItems');
                     $defaultHostMatchMethod = $ini->variable('SiteAccessSettings', 'HostUriMatchMethodDefault');
                     foreach ($matchMapItems as $matchMapItem) {
                         $matchHost = $matchMapItem[0];
                         $matchURI = $matchMapItem[1];
                         $matchAccess = $matchMapItem[2];
                         $matchHostMethod = isset($matchMapItem[3]) ? $matchMapItem[3] : $defaultHostMatchMethod;
                         if ($matchURI !== '' && !preg_match("@^{$matchURI}\\b@", $uriString)) {
                             continue;
                         }
                         switch ($matchHostMethod) {
                             case 'strict':
                                 $hasHostMatch = $matchHost === $host;
                                 break;
                             case 'start':
                                 $hasHostMatch = strpos($host, $matchHost) === 0;
                                 break;
                             case 'end':
                                 $hasHostMatch = strstr($host, $matchHost) === $matchHost;
                                 break;
                             case 'part':
                                 $hasHostMatch = strpos($host, $matchHost) !== false;
                                 break;
                             default:
                                 $hasHostMatch = false;
                                 eZDebug::writeError("Unknown host_uri host match: {$matchHostMethod}", "access");
                                 break;
                         }
                         if ($hasHostMatch) {
                             if ($matchURI !== '') {
                                 $matchURIFolders = explode('/', $matchURI);
                                 $uri->increase(count($matchURIFolders));
                                 $uri->dropBase();
                                 $access['uri_part'] = $matchURIFolders;
                             }
                             $access['name'] = $matchAccess;
                             $access['type'] = $type;
                             return $access;
                         }
                     }
                 }
                 break;
             case 'index':
                 $type = eZSiteAccess::TYPE_INDEX_FILE;
                 $match_type = $ini->variable('SiteAccessSettings', 'IndexMatchType');
                 $match_item = $file;
                 if ($match_type == 'element') {
                     $match_index = $ini->variable('SiteAccessSettings', 'IndexMatchElement');
                     $match_pos = strpos($match_item, '.php');
                     if ($match_pos !== false) {
                         $match_item = substr($match_item, 0, $match_pos);
                         $match_arr = explode('_', $match_item);
                         $name = $match_arr[$match_index];
                     }
                 } else {
                     if ($match_type == 'text') {
                         $matcher_pre = $ini->variable('SiteAccessSettings', 'IndexMatchSubtextPre');
                         $matcher_post = $ini->variable('SiteAccessSettings', 'IndexMatchSubtextPost');
                     } else {
                         if ($match_type == 'regexp') {
                             $matcher = $ini->variable('SiteAccessSettings', 'IndexMatchRegexp');
                             $match_num = $ini->variable('SiteAccessSettings', 'IndexMatchRegexpItem');
                         } else {
                             continue;
                         }
                     }
                 }
                 break;
             default:
                 eZDebug::writeError("Unknown access match: {$matchprobe}", "access");
                 break;
         }
         if ($match_type == 'regexp') {
             $name = self::matchRegexp($match_item, $matcher, $match_num);
         } else {
             if ($match_type == 'text') {
                 $name = self::matchText($match_item, $matcher_pre, $matcher_post);
             }
         }
         if (isset($name) && $name != '') {
             $nameClean = self::washName($name);
             if (in_array($nameClean, $siteAccessList)) {
                 if ($nameClean !== $name) {
                     if (!$ini->hasVariable('SiteAccessSettings', 'NormalizeSANames') || $ini->variable('SiteAccessSettings', 'NormalizeSANames') == 'enabled') {
                         $name = $nameClean;
                         if ($ini->hasVariable('SiteAccessSettings', 'RedirectOnNormalize') && $ini->variable('SiteAccessSettings', 'RedirectOnNormalize') == 'enabled') {
                             header($_SERVER['SERVER_PROTOCOL'] . " 301 Moved Permanently");
                             header("Status: 301 Moved Permanently");
                             $uriSlice = $uri->URIArray;
                             array_shift($uriSlice);
                             $newUri = $name . '/' . implode('/', $uriSlice);
                             $location = eZSys::indexDir() . "/" . eZURI::encodeIRI($newUri);
                             header("Location: " . $location);
                             eZExecution::cleanExit();
                         }
                     }
                 }
                 if ($type == eZSiteAccess::TYPE_URI) {
                     if ($match_type == 'element') {
                         $uri->increase($match_index);
                         $uri->dropBase();
                     } else {
                         if ($match_type == 'regexp') {
                             $uri->setURIString($match_item);
                         } else {
                             if ($match_type == 'text') {
                                 $uri->setURIString($match_item);
                             }
                         }
                     }
                 }
                 $access['type'] = $type;
                 $access['name'] = $name;
                 $access['uri_part'] = $uri_part;
                 return $access;
             }
         }
     }
     return $access;
 }