static function headerOverrideArray( $uri ) { $headerArray = array(); if ( !eZHTTPHeader::enabled() ) { return $headerArray; } $contentView = false; $uriString = eZURLAliasML::cleanURL( $uri->uriString() ); // If content/view used, get url alias for node if ( strpos( $uriString, 'content/view/' ) === 0 ) { $urlParts = explode( '/', $uriString ); $nodeID = $urlParts[3]; if ( !$nodeID ) { return $headerArray; } $node = eZContentObjectTreeNode::fetch( $nodeID ); if ( !$node ) { return $headerArray; } $uriString = $node->pathWithNames(); $contentView = true; } else { $uriCopy = clone $uri; eZURLAliasML::translate( $uriCopy ); if ( strpos( $uriCopy->uriString(), 'content/view' ) === 0 ) { $contentView = true; } } $uriString = '/' . eZURLAliasML::cleanURL( $uriString ); $ini = eZINI::instance(); foreach( $ini->variable( 'HTTPHeaderSettings', 'HeaderList' ) as $header ) { foreach( $ini->variable( 'HTTPHeaderSettings', $header ) as $path => $value ) { $path = '/' . eZURLAliasML::cleanURL( $path ); if ( strlen( $path ) == 1 && ( !$contentView && ( $ini->variable( 'HTTPHeaderSettings', 'OnlyForContent' ) === 'enabled' ) ) && $uriString != '/' ) { continue; } if ( strpos( $uriString, $path ) === 0 ) { @list( $headerValue, $depth, $level ) = explode( ';', $value ); if ( $header == 'Expires' ) { $headerValue = gmdate( 'D, d M Y H:i:s', time() + $headerValue ) . ' GMT'; } if ( $depth === null ) { $headerArray[$header] = $headerValue; } else { $pathLevel = count( explode( '/', $path ) ); $uriLevel = count( explode( '/', $uriString ) ); if ( $level === null ) { if ( $uriLevel <= $pathLevel + $depth ) { $headerArray[$header] = $headerValue; } } else { if ( $uriLevel <= $pathLevel + $depth && $uriLevel >= $pathLevel + $level ) { $headerArray[$header] = $headerValue; } } } } } } return $headerArray; }
/** * 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; }
function fetchParentNodeByTranslation( $nodePathString ) { // Strip extensions. E.g. .jpg $nodePathString = $this->fileBasename( $nodePathString ); // Strip away last slash if ( strlen( $nodePathString ) > 0 and $nodePathString[strlen( $nodePathString ) - 1] == '/' ) { $nodePathString = substr( $nodePathString, 0, strlen( $nodePathString ) - 1 ); } $nodePathString = $this->splitLastPathElement( $nodePathString, $element ); if ( strlen( $nodePathString ) == 0 ) $nodePathString = '/'; $nodePathString = eZURLAliasML::convertPathToAlias( $nodePathString ); // Attempt to translate the URL to something like "/content/view/full/84". $translateResult = eZURLAliasML::translate( $nodePathString ); // handle redirects while ( $nodePathString == 'error/301' ) { $nodePathString = $translateResult; $translateResult = eZURLAliasML::translate( $nodePathString ); } // Get the ID of the node (which is the last part of the translated path). if ( preg_match( "#^content/view/full/([0-9]+)$#", $nodePathString, $matches ) ) { $nodeID = $matches[1]; $this->appendLogEntry( "NodeID: $nodeID", 'CS:fetchParentNodeByTranslation' ); } else { $this->appendLogEntry( "Root node", 'CS:fetchParentNodeByTranslation' ); $nodeID = 2; } // Attempt to fetch the node. $node = eZContentObjectTreeNode::fetch( $nodeID ); // Return the node. return $node; }
/** * Attempts to fetch a possible node by translating the provided * string/path to a node-number. * * The last section of the path is removed before the actual * translation: hence, the PARENT node is returned. * * @param string $nodePathString Eg. 'Folder1/file1.txt' * @return eZContentObject Eg. the node of 'Folder1' */ protected function fetchParentNodeByTranslation($nodePathString) { // Strip extensions. E.g. .jpg $nodePathString = $this->fileBasename($nodePathString); // Strip away last slash if (strlen($nodePathString) > 0 and $nodePathString[strlen($nodePathString) - 1] === '/') { $nodePathString = substr($nodePathString, 0, strlen($nodePathString) - 1); } $nodePathString = $this->splitLastPathElement($nodePathString, $element); if (strlen($nodePathString) === 0) { $nodePathString = '/'; } $nodePathString = eZURLAliasML::convertPathToAlias($nodePathString); // Attempt to translate the URL to something like "/content/view/full/84". $translateResult = eZURLAliasML::translate($nodePathString); // handle redirects while ($nodePathString === 'error/301') { $nodePathString = $translateResult; $translateResult = eZURLAliasML::translate($nodePathString); } // Get the ID of the node (which is the last part of the translated path). if (preg_match("#^content/view/full/([0-9]+)\$#", $nodePathString, $matches)) { $nodeID = $matches[1]; } else { $ini = eZINI::instance('webdav.ini'); if ($ini->hasVariable('GeneralSettings', 'StartNode')) { $nodeID = $ini->variable('GeneralSettings', 'StartNode'); } } // Attempt to fetch the node. $node = eZContentObjectTreeNode::fetch($nodeID); // Return the node. return $node; }
/** * Performs a redirection */ protected function redirect() { $GLOBALS['eZRedirection'] = true; $ini = eZINI::instance(); $automaticRedirect = true; if ($GLOBALS['eZDebugAllowed'] && ($redirUri = $ini->variable('DebugSettings', 'DebugRedirection')) !== 'disabled') { if ($redirUri == "enabled") { $automaticRedirect = false; } else { $uri = eZURI::instance(eZSys::requestURI()); $uri->toBeginning(); foreach ($ini->variableArray("DebugSettings", "DebugRedirection") as $redirUri) { $redirUri = new eZURI($redirUri); if ($redirUri->matchBase($uri)) { $automaticRedirect = false; break; } } } } $redirectURI = eZSys::indexDir(); $moduleRedirectUri = $this->module->redirectURI(); if ($ini->variable('URLTranslator', 'Translation') === 'enabled' && eZURLAliasML::urlTranslationEnabledByUri(new eZURI($moduleRedirectUri))) { $translatedModuleRedirectUri = $moduleRedirectUri; if (eZURLAliasML::translate($translatedModuleRedirectUri, true)) { $moduleRedirectUri = $translatedModuleRedirectUri; if (strlen($moduleRedirectUri) > 0 && $moduleRedirectUri[0] !== '/') { $moduleRedirectUri = '/' . $moduleRedirectUri; } } } if (preg_match('#^(\\w+:)|^//#', $moduleRedirectUri)) { $redirectURI = $moduleRedirectUri; } else { $leftSlash = strlen($redirectURI) > 0 && $redirectURI[strlen($redirectURI) - 1] === '/'; $rightSlash = strlen($moduleRedirectUri) > 0 && $moduleRedirectUri[0] === '/'; if (!$leftSlash && !$rightSlash) { // Both are without a slash, so add one $moduleRedirectUri = '/' . $moduleRedirectUri; } else { if ($leftSlash && $rightSlash) { // Both are with a slash, so we remove one $moduleRedirectUri = substr($moduleRedirectUri, 1); } } $redirectURI .= $moduleRedirectUri; } eZStaticCache::executeActions(); eZDB::checkTransactionCounter(); if ($automaticRedirect) { eZHTTPTool::redirect($redirectURI, array(), $this->module->redirectStatus()); } else { // Make sure any errors or warnings are reported if ($ini->variable('DebugSettings', 'DisplayDebugWarnings') === 'enabled') { if (isset($GLOBALS['eZDebugError']) && $GLOBALS['eZDebugError']) { eZAppendWarningItem(array('error' => array('type' => 'error', 'number' => 1, 'count' => $GLOBALS['eZDebugErrorCount']), 'identifier' => 'ezdebug-first-error', 'text' => ezpI18n::tr('index.php', 'Some errors occurred, see debug for more information.'))); } if (isset($GLOBALS['eZDebugWarning']) && $GLOBALS['eZDebugWarning']) { eZAppendWarningItem(array('error' => array('type' => 'warning', 'number' => 1, 'count' => $GLOBALS['eZDebugWarningCount']), 'identifier' => 'ezdebug-first-warning', 'text' => ezpI18n::tr('index.php', 'Some general warnings occured, see debug for more information.'))); } } $tpl = eZTemplate::factory(); $tpl->setVariable('site', $this->site); $tpl->setVariable('warning_list', !empty($this->warningList) ? $this->warningList : false); $tpl->setVariable('redirect_uri', eZURI::encodeURL($redirectURI)); $templateResult = $tpl->fetch('design:redirect.tpl'); eZDebug::addTimingPoint("Script end"); eZDisplayResult($templateResult); } eZExecution::cleanExit(); }
/** * @see http://issues.ez.no/19062 * @group issue19062 * @covers eZURLAliasML::translate */ public function testTranslateWildcardNopUri() { $folder = new ezpObject('folder', 2); $folder->name = 'foo'; $folder->publish(); // By creating following URL alias, "test/single-page" will be registered as a NOP URI segment $uriFirstSegment = 'test19062'; $uriWildcard = "{$uriFirstSegment}/single-page"; $res = eZURLAliasML::storePath("{$uriWildcard}/article.html", "eznode:{$folder->mainNode->node_id}", $this->englishLanguage, 0, true); $wildcard = eZURLWildcardTest::createWildcard($uriWildcard, 'foo', eZURLWildcard::TYPE_FORWARD); // Translating the wildcard URL should return false in order to be then translated by eZURLWildcard in index.php self::assertFalse(eZURLAliasML::translate($uriWildcard)); // Here no wildcard and test19062 should be a nop segment (points to nothing. // Default behaviour is to return true and redirect to root ("/") self::assertTrue(eZURLAliasML::translate($uriFirstSegment)); $folder->remove(); $wildcard->remove(); }
$uri->toBeginning(); foreach ($redirUris as $redirUri) { $redirUri = new eZURI($redirUri); if ($redirUri->matchBase($uri)) { $automatic_redir = false; break; } } } } $redirectURI = eZSys::indexDir(); $moduleRedirectUri = $module->redirectURI(); $redirectStatus = $module->redirectStatus(); $translatedModuleRedirectUri = $moduleRedirectUri; if ($ini->variable('URLTranslator', 'Translation') == 'enabled' && eZURLAliasML::urlTranslationEnabledByUri(new eZURI($moduleRedirectUri))) { if (eZURLAliasML::translate($translatedModuleRedirectUri, true)) { $moduleRedirectUri = $translatedModuleRedirectUri; if (strlen($moduleRedirectUri) > 0 and $moduleRedirectUri[0] != '/') { $moduleRedirectUri = '/' . $moduleRedirectUri; } } } if (preg_match('#^(\\w+:)|^//#', $moduleRedirectUri)) { $redirectURI = $moduleRedirectUri; } else { $leftSlash = false; $rightSlash = false; if (strlen($redirectURI) > 0 and $redirectURI[strlen($redirectURI) - 1] == '/') { $leftSlash = true; } if (strlen($moduleRedirectUri) > 0 and $moduleRedirectUri[0] == '/') {
if ($isValid) { eZURL::setIsValid($linkID, false); } $cli->output($cli->stylize('warning', "invalid")); } else { if (!$isValid) { eZURL::setIsValid($linkID, true); } $cli->output($cli->stylize('success', "valid")); } } else { $cli->output("Couldn't check https protocol"); } } } else { $translateResult = eZURLAliasML::translate($url); if (!$translateResult) { $isInternal = false; // Check if it is a valid internal link. foreach ($siteURLs as $siteURL) { $siteURL = preg_replace("/\\/\$/e", "", $siteURL); $fp = @fopen($siteURL . "/" . $url, "r"); if (!$fp) { // do nothing } else { $isInternal = true; fclose($fp); } } $translateResult = $isInternal; }
/** * @param string $nodePath * @param eZContentObjectTreeNode[] $publisherFolderNodes * @return eZContentObjectTreeNode */ static protected function getNodeFromPath ( $nodePath, $publisherFolderNodes ) { // Remove view parameters $nodePath = preg_replace( '#/\([^\)]+\)/.*$#', '', $nodePath ); $node = false; foreach ( $publisherFolderNodes as $publisherFolderNode ) { $nodeFullPath = '/' . $publisherFolderNode->attribute('url_alias') . '/' . $nodePath; $node = eZContentObjectTreeNode::fetchByURLPath( $nodeFullPath ); if (!$node) { $node = eZContentObjectTreeNode::fetchByPath($nodeFullPath); if (!$node) { $cleanableHref = $nodeFullPath; $aliasTranslate = eZURLAliasML::translate($cleanableHref); while ( $aliasTranslate !== true && $aliasTranslate !== false ) { $cleanableHref = $aliasTranslate; $aliasTranslate = eZURLAliasML::translate($cleanableHref); } if ( $aliasTranslate === true ) { $lastSlashPosition = strrpos($cleanableHref, '/'); if( $lastSlashPosition !== false ) $node = eZContentObjectTreeNode::fetch(substr($cleanableHref, $lastSlashPosition+1)); } } if (!$node) { $tabNodePath = explode('/', substr($nodeFullPath, 1)); $remoteId = end($tabNodePath); $nodeRemote = eZContentObjectTreeNode::fetchByRemoteID($remoteId); if($nodeRemote && $publisherFolderNode) { $rootNodeID = $publisherFolderNode->attribute('node_id'); $tabNodeRemote = $nodeRemote->pathArray(); $nodeId = $nodeRemote->attribute('node_id'); if(in_array($rootNodeID, $tabNodeRemote) && $rootNodeID != $nodeId) $node = $nodeRemote; /******************************************************************************* * TODO : Delete after PF Refactor *******************************************************************************/ else { $publisherFolderPathArray = $publisherFolderNode->pathArray(); foreach ( $nodeRemote->object()->assignedNodes() as $location ) { if ( in_array( $publisherFolderPathArray[2], $location->pathArray() ) ) { $node = $location; break; } } } /******************************************************************************* * TODO : END : Delete after PF Refactor *******************************************************************************/ } } } if ($node instanceof eZContentObjectTreeNode) break; } // if we still do not have a node, we try to look in the old locations if ( !($node instanceof eZContentObjectTreeNode) ) { $pfrLocation = null; if ( preg_match('#node_(?P<node_id>\d+)/?$#', $nodePath, $m) ) { $nodeId = $m['node_id']; $pfrLocation = PfrLocation::fetchByNodeId( $nodeId ); } else { $remoteId = basename( rtrim($nodePath, '/') ); $pfrLocation = PfrLocation::fetchByRemoteId( $remoteId ); } if ( $pfrLocation instanceof PfrLocation ) { $node = $pfrLocation->node( $publisherFolderNodes ); } } return $node; }