From: <ja...@us...> - 2006-11-27 22:15:51
|
Revision: 15303 http://svn.sourceforge.net/gallery/?rev=15303&view=rev Author: jablko Date: 2006-11-27 14:15:51 -0800 (Mon, 27 Nov 2006) Log Message: ----------- Closes: http://sourceforge.net/tracker/index.php?func=detail&aid=1380292&group_id=7130&atid=107130 http://sourceforge.net/tracker/index.php?func=detail&aid=1415619&group_id=7130&atid=357130 Support rewrite rules without patterns, so rules can define restrict or conditions without also defining a pattern. Support rewrite rules which define conditions. Conditions can be used to match HTTP headers or the request method, for example. Used by the WebDAV module to redirect OPTIONS requests and the HTTP auth module to capture the Authorization HTTP header. Add more tests to RewriteParser::isValidRule. Checks that conditions and restrict are only supported by preGallery parsers. Avoid collapsing whitespace in Htaccess.tpl and Httpdini.tpl. Add unit tests for rules with invalid patterns, restrict, etc. Quote regular expression meta characters in pattern where pattern is used in context of a regular expression. Modified Paths: -------------- trunk/gallery2/modules/rewrite/AdminRewrite.inc trunk/gallery2/modules/rewrite/RewriteModuleExtras.inc trunk/gallery2/modules/rewrite/classes/RewriteApi.class trunk/gallery2/modules/rewrite/classes/RewriteHelper.class trunk/gallery2/modules/rewrite/classes/RewriteParser.class trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/IsapiRewriteHelper.class trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/parser.inc trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/ModRewriteHelper.class trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/parser.inc trunk/gallery2/modules/rewrite/classes/parsers/pathinfo/PathInfoHelper.class trunk/gallery2/modules/rewrite/module.inc trunk/gallery2/modules/rewrite/templates/AdminRewrite.tpl trunk/gallery2/modules/rewrite/templates/Htaccess.tpl trunk/gallery2/modules/rewrite/templates/Httpdini.tpl trunk/gallery2/modules/rewrite/test/phpunit/AdminRewriteControllerTest.class trunk/gallery2/modules/rewrite/test/phpunit/IsapiRewriteParserTest.class trunk/gallery2/modules/rewrite/test/phpunit/ModRewriteParserTest.class trunk/gallery2/modules/rewrite/test/phpunit/RewriteApiTest.class trunk/gallery2/modules/rewrite/test/phpunit/RewriteHelperTest.class trunk/gallery2/modules/rewrite/test/phpunit/RewriteParserTestCase.class Modified: trunk/gallery2/modules/rewrite/AdminRewrite.inc =================================================================== --- trunk/gallery2/modules/rewrite/AdminRewrite.inc 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/AdminRewrite.inc 2006-11-27 22:15:51 UTC (rev 15303) @@ -31,7 +31,7 @@ * @see GalleryController::handleRequest */ function handleRequest(&$form) { - $error = $status = $seen = array(); + $error = $status = $seenPatterns = $seenConditions = array(); $ret = GalleryCoreApi::assertUserIsSiteAdministrator(); if ($ret) { @@ -48,32 +48,57 @@ $activeRules = array(); $mode = 'rules'; - /* Build the activeRules array and check for emtpy / seen patterns */ + /* Build the activeRules array and check for emtpy / dupe patterns */ if (isset($form['rules'])) { foreach (array_keys($form['rules']) as $moduleId) { - foreach (array_keys($form['rules'][$moduleId]) as $ruleId) { - if (isset($form['rules'][$moduleId][$ruleId]['active'])) { - /* Empty or just '/'? */ - if (empty($form['rules'][$moduleId][$ruleId]['pattern']) || - $form['rules'][$moduleId][$ruleId]['pattern'] == '/') { - $error[] = 'form[error][empty][' . $moduleId . '][' . $ruleId . ']'; - break 2; - } + list ($ret, $module) = GalleryCoreApi::loadPlugin('module', $moduleId); + if ($ret) { + return array($ret, null); + } - /* Dupe? */ - if (isset($seen[$form['rules'][$moduleId][$ruleId]['pattern']])) { - $error[] = 'form[error][dupe][' . - $form['rules'][$moduleId][$ruleId]['pattern'] . ']'; - break 2; + $rules = $module->getRewriteRules(); + foreach ($form['rules'][$moduleId] as $ruleId => $rule) { + if (isset($rule['active'])) { + /* Activate the rule */ + $activeRules[$moduleId][$ruleId] = array(); + + if (isset($rules[$ruleId]['conditions'])) { + $condition = serialize($rules[$ruleId]['conditions']); + + /* Dupe? */ + if (isset($seenConditions[$condition])) { + $error[] = 'form[error][conditions][dupe][' + . $seenConditions[$condition]; + $error[] = 'form[error][conditions][dupe][' + . $moduleId . '][' . $ruleId . ']'; + break 2; + } + $seenConditions[$condition] = $moduleId . '][' . $ruleId . ']'; } - /* Activate the rule */ - $pattern = $form['rules'][$moduleId][$ruleId]['pattern']; - if (substr($pattern, 0, 1) == '/') { - $pattern = substr($pattern, 1); + if (isset($rule['pattern'])) { + $pattern = ltrim($rule['pattern'], '/'); + + /* Empty? */ + if (empty($pattern)) { + $error[] = 'form[error][pattern][empty][' + . $moduleId . '][' . $ruleId . ']'; + break 2; + } + + /* Dupe? */ + if (isset($seenPatterns[$pattern])) { + $error[] = 'form[error][pattern][dupe][' + . $seenPatterns[$pattern]; + $error[] = 'form[error][pattern][dupe][' + . $moduleId . '][' . $ruleId . ']'; + break 2; + } + $seenPatterns[$pattern] = $moduleId . '][' . $ruleId . ']'; + + /* Save the pattern */ + $activeRules[$moduleId][$ruleId]['pattern'] = $pattern; } - $activeRules[$moduleId][$ruleId] = $pattern; - $seen[$pattern] = 1; } } } @@ -242,14 +267,18 @@ /* If we dont have old form data, populate the array with db info */ if ($form['formName'] != 'AdminRewrite') { - $form['rules'][$moduleId][$ruleId]['pattern'] = - isset($activeRules[$moduleId][$ruleId]) - ? $activeRules[$moduleId][$ruleId] - : $rule['pattern']; - + $form['rules'][$moduleId][$ruleId] = array(); if (isset($activeRules[$moduleId][$ruleId])) { $form['rules'][$moduleId][$ruleId]['active'] = 1; } + if (isset($rule['pattern'])) { + $form['rules'][$moduleId][$ruleId]['pattern'] = + $rule['pattern']; + if (isset($activeRules[$moduleId][$ruleId]['pattern'])) { + $form['rules'][$moduleId][$ruleId]['pattern'] = + $activeRules[$moduleId][$ruleId]['pattern']; + } + } } /* If a dupe rule is detected we need to know it's 'match' */ Modified: trunk/gallery2/modules/rewrite/RewriteModuleExtras.inc =================================================================== --- trunk/gallery2/modules/rewrite/RewriteModuleExtras.inc 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/RewriteModuleExtras.inc 2006-11-27 22:15:51 UTC (rev 15303) @@ -231,6 +231,59 @@ case '1.1.6': /* Added activateRewriteRulesForModule() to Rewrite API */ + case '1.1.7': + /* Support rewrite rules without 'pattern' */ + GalleryCoreApi::requireOnce('modules/rewrite/classes/RewriteHelper.class'); + list ($ret, $activeRules) = $module->getParameter('activeRules'); + if ($ret) { + return $ret; + } + $activeRules = unserialize($activeRules); + + /* + * Convert active rules from array($moduleId => array($ruleId => $pattern)) to + * array($moduleId => array($ruleId => array('pattern' => $pattern))) + */ + foreach (array_keys($activeRules) as $moduleId) { + foreach ($activeRules[$moduleId] as $ruleId => $pattern) { + $activeRules[$moduleId][$ruleId] = array('pattern' => $pattern); + } + } + + /* + * Save active rules. There should be no change in the interpretation of active rules, + * only the representation, so don't call $rewriteParser->saveActiveRules. + */ + $ret = $module->setParameter('activeRules', serialize($activeRules)); + if ($ret) { + return $ret; + } + + /* + * Convert history parameters from array($moduleId => array($ruleId => $pattern)) to + * array($moduleId => array($ruleId => array('pattern' => $pattern))) + */ + list ($ret, $parameters) = $module->fetchParameters(); + if ($ret) { + return $ret; + } + + foreach ($parameters as $key => $value) { + if (strncmp($key, 'history.', 8) !== 0) { + continue; + } + + $history = unserialize($value); + foreach ($history as $ruleId => $pattern) { + $history[$ruleId] = array('pattern' => $pattern); + } + + $ret = $module->setParameter($key, serialize($history)); + if ($ret) { + return $ret; + } + } + case 'end of upgrade path': break; @@ -318,15 +371,22 @@ $seen = array(); foreach (array_keys($activeRules) as $moduleId) { - foreach (array_keys($activeRules[$moduleId]) as $ruleId) { - $seen[$activeRules[$moduleId][$ruleId]] = 1; + foreach ($activeRules[$moduleId] as $ruleId => $rule) { + if (isset($rule['pattern'])) { + $seen[$rule['pattern']] = true; + } } } - foreach (array_keys($history) as $ruleId) { - if (isset($rules[$ruleId]) && !isset($seen[$history[$ruleId]])) { - $activeRules[$pluginId][$ruleId] = $history[$ruleId]; - $seen[$history[$ruleId]] = 1; + foreach ($history as $ruleId => $rule) { + if (isset($rules[$ruleId])) { + if (isset($rule['pattern'])) { + if (isset($seen[$rule['pattern']])) { + continue; + } + $seen[$rule['pattern']] = true; + } + $activeRules[$pluginId][$ruleId] = $rule; } } Modified: trunk/gallery2/modules/rewrite/classes/RewriteApi.class =================================================================== --- trunk/gallery2/modules/rewrite/classes/RewriteApi.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/RewriteApi.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -180,12 +180,17 @@ foreach ($rules as $ruleId => $rule) { if ((!isset($activeRules[$moduleId][$ruleId]) || $replacePatterns) && (!isset($ruleIds) || in_array($ruleId, $ruleIds))) { - if (!$replacePatterns && isset($history[$ruleId])) { - $pattern = $history[$ruleId]; - } else { - $pattern = $rule['pattern']; + $activeRule = array(); + if (isset($rule['pattern'])) { + $activeRule['pattern'] = $rule['pattern']; + if (!$replacePatterns && isset($activeRules[$moduleId][$ruleId]['pattern'])) { + $activeRule['pattern'] = $activeRules[$moduleId][$ruleId]['pattern']; + } + if (!$replacePatterns && isset($history[$ruleId]['pattern'])) { + $activeRule['pattern'] = $history[$ruleId]['pattern']; + } } - $activeRules[$moduleId][$ruleId] = $pattern; + $activeRules[$moduleId][$ruleId] = $activeRule; unset($history[$ruleId]); } } Modified: trunk/gallery2/modules/rewrite/classes/RewriteHelper.class =================================================================== --- trunk/gallery2/modules/rewrite/classes/RewriteHelper.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/RewriteHelper.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -79,7 +79,7 @@ * Parse active rules into an array of regular expressions for parsing URLs during page requests * and build an array for the URL generator to use when generating short URLs. * - * @param array $activeRules active rules by reference + * @param array $activeRules array array ('pattern' => string pattern) active rules by reference * @param object RewriteParser $rewriteParser * @param object GalleryModule $upgradeModule (optional) passed in during activate/upgrade * @param array $flags optional definition of default and mandator flags @@ -126,7 +126,7 @@ } $rules = $module->getRewriteRules(); - foreach (array_keys($activeRules[$moduleId]) as $ruleId) { + foreach ($activeRules[$moduleId] as $ruleId => $rule) { /* Make sure this rule still exists, if not silently continue */ if (!isset($rules[$ruleId])) { unset($activeRules[$moduleId][$ruleId]); @@ -141,9 +141,9 @@ } /* Save the pattern for the URL generator to use */ - if (isset($rules[$ruleId]['match'])) { + if (isset($rules[$ruleId]['match']) && isset($rule['pattern'])) { $shortUrl = array('match' => $rules[$ruleId]['match'], - 'pattern' => $activeRules[$moduleId][$ruleId]); + 'pattern' => $rule['pattern']); /* Get custom function information */ if (isset($rules[$ruleId]['keywords'])) { @@ -207,7 +207,7 @@ /* Parse the rule */ list ($ret, $code) = RewriteHelper::_parseRule($activeRules[$moduleId][$ruleId], - $keywords, $queryString, $regexRules, $settings); + $keywords, $queryString, $regexRules, $settings, $rules[$ruleId]); if ($ret) { return array($ret, null, null, null, null); } @@ -225,44 +225,79 @@ /** * Replaces all keywords with an appropriate pattern and append to $regexRules. * - * @param string $pattern URL pattern + * @param array $activeRule array ('pattern' => string pattern) * @param array $keywords of keywords => regular expresion for the htaccess file * @param array $queryString of query string params (key => value) * @param array $regexRules of parsed rules * @param array $settings of settings + * @param array $rule rewrite rule + * @return array object GalleryStatus a status code + * int rewrite status code (REWRITE_STATUS_OK on success) * @access private */ - function _parseRule($pattern, $keywords, $queryString, &$regexRules, $settings) { + function _parseRule($activeRule, $keywords, $queryString, &$regexRules, $settings, $rule) { $reference = 1; - preg_match_all('/\%([^\%]+)\%/', $pattern, $regs); + $regexRule = array('settings' => $settings); - foreach ($regs[1] as $keyword) { - if (!isset($keywords[$keyword]['pattern'])) { - return array(null, REWRITE_STATUS_BAD_KEYWORD); + if (isset($rule['conditions'])) { + $regexRule['conditions'] = array(); + foreach ($rule['conditions'] as $condition) { + $code = RewriteHelper::_parseKeywords( + $reference, $condition['pattern'], $keywords, $queryString); + if ($code != REWRITE_STATUS_OK) { + return array(null, $code); + } + + $regexRule['conditions'][] = $condition; } + } - $pattern = str_replace('%' . $keyword . '%', - $keywords[$keyword]['pattern'], $pattern); + if (isset($activeRule['pattern'])) { + $pattern = preg_quote($activeRule['pattern']); + $code = RewriteHelper::_parseKeywords( + $reference, $pattern, $keywords, $queryString); + if ($code != REWRITE_STATUS_OK) { + return array(null, $code); + } - if (!isset($keywords[$keyword]['ignore'])) { - $queryString[$keyword] = '$' . $reference; - } - $reference++; + $regexRule['pattern'] = $pattern; } $args = array(); foreach ($queryString as $key => $value) { $args[] = GalleryUtilities::prefixFormVariable($key) . '=' . $value; } + $regexRule['queryString'] = join('&', $args); + $regexRules[] = $regexRule; - if (!empty($pattern)) { - $regexRules[] = array( - 'pattern' => $pattern, - 'queryString' => join('&', $args), - 'settings' => $settings); + return array(null, REWRITE_STATUS_OK); + } + + /** + * Replaces all keywords with an appropriate pattern. + * @param array $reference back reference to next keyword pattern + * @param array $pattern + * @param array $keywords of keywords => regular expresion for the htaccess file + * @param array $queryString of query string params (key => value) + * @return int rewrite status code (REWRITE_STATUS_OK on success) + * @access private + */ + function _parseKeywords(&$reference, &$pattern, $keywords, &$queryString) { + preg_match_all('/\%([^%]+)\%/', $pattern, $matches); + foreach ($matches[1] as $keyword) { + if (!isset($keywords[$keyword]['pattern'])) { + return REWRITE_STATUS_BAD_KEYWORD; + } + + $pattern = str_replace('%' . $keyword . '%', $keywords[$keyword]['pattern'], $pattern); + + if (!isset($keywords[$keyword]['ignore'])) { + $queryString[$keyword] = '$' . $reference; + } + $reference++; } - return array(null, REWRITE_STATUS_OK); + return REWRITE_STATUS_OK; } /** @@ -270,10 +305,21 @@ * @access private */ function _sortRules($a, $b) { + if (isset($a['conditions']) || isset($b['conditions'])) { + if (isset($a['conditions']) && isset($b['conditions'])) { + return count($a['conditions']) - count($b['conditions']); + } + + if (isset($a['conditions'])) { + return -1; + } + + return 1; + } + if (isset($a['settings']['restrict']) || isset($b['settings']['restrict'])) { if (isset($a['settings']['restrict']) && isset($b['settings']['restrict'])) { - $diff = count($a['settings']['restrict']) - count($b['settings']['restrict']); - return $diff; + return count($a['settings']['restrict']) - count($b['settings']['restrict']); } if (isset($a['settings']['restrict'])) { @@ -310,7 +356,7 @@ * Get the rewrite rule history for a specific module. * @param string $moduleId id of the module * @return array object GalleryStatus a status code - * array (mixed ruleId => string pattern) + * array (mixed ruleId => array ('pattern' => string pattern)) */ function getHistoryForModule($moduleId) { list ($ret, $history) = @@ -326,7 +372,7 @@ /** * Store the rewrite rule history for a specific module. * @param string $moduleId id of the module - * @param array $history array (mixed ruleId => string pattern) + * @param array $history array (mixed ruleId => array ('pattern' => string pattern)) * @return object GalleryStatus a status code */ function setHistoryForModule($moduleId, $history) { Modified: trunk/gallery2/modules/rewrite/classes/RewriteParser.class =================================================================== --- trunk/gallery2/modules/rewrite/classes/RewriteParser.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/RewriteParser.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -102,22 +102,39 @@ * a sanity check to minimize possible conflicts and bad regular expressions. * * @param array $rule rewrite rule - * @param string $pattern (optional) set to the custom pattern when called from saveActiveRules + * @param array $activeRule (optional) array ('pattern' => string pattern) set to the custom + * active rule when called from saveActiveRules * @return boolean true if the rule is valid */ - function isValidRule($rule, $pattern=null) { + function isValidRule($rule, $activeRule=null) { if (isset($rule['parser']) && $rule['parser'] != $this->getParserType()) { return false; } - if (isset($rule['locked']) && isset($pattern) && $rule['pattern'] != $pattern) { + /* Only supported by preGallery parsers */ + if (isset($rule['conditions']) && $this->getParserType() != 'preGallery') { return false; } - if (isset($pattern) && !preg_match('/^([a-zA-Z0-9\/\.\%\-]+)$/', $pattern)) { + /* Only supported by preGallery parsers */ + if (isset($rule['restrict']) && $this->getParserType() != 'preGallery') { return false; } + if (isset($activeRule) && (isset($activeRule['pattern']) != isset($rule['pattern']))) { + return false; + } + + if (isset($activeRule['pattern']) && isset($rule['pattern']) && isset($rule['locked']) + && $activeRule['pattern'] != $rule['pattern']) { + return false; + } + + if (isset($activeRule['pattern']) + && !preg_match('/^[a-z0-9\/\.\%\-]+$/i', $activeRule['pattern'])) { + return false; + } + return true; } Modified: trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/IsapiRewriteHelper.class =================================================================== --- trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/IsapiRewriteHelper.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/IsapiRewriteHelper.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -193,8 +193,96 @@ $Httpdini['mainPhp'] = GALLERY_MAIN_PHP; $Httpdini['galleryId'] = GALLERY_CONFIG_DIR; - foreach (array_keys($regexRules) as $ruleId) { - $count = preg_match_all('/\(([^\(\)]+)\)/', $regexRules[$ruleId]['pattern'], $tmp); + foreach ($regexRules as $ruleId => $rule) { + if (isset($rule['conditions'])) { + foreach ($rule['conditions'] as $conditionId => $condition) { + /* + * ISAPI_Rewrite supports only certain test verbs: + * http://www.isapirewrite.com/docs/#RewriteCond + */ + switch ($condition['test']) { + + /* + * The ISAPI_Rewrite URL test verb is actually the concatenation of the Apache + * mod_rewrite REQUEST_URI and QUERY_STRING variables. By replacing REQUEST_URI + * and QUERY_STRING with URL, we're testing the condition pattern against *both* + * REQUEST_URI and QUERY_STRING. This works for most condition patterns, but a + * more consistent solution should eventually be found. + */ + case 'REQUEST_URI': + case 'QUERY_STRING': + $condition['test'] = 'URL'; + break; + + case 'REQUEST_METHOD': + $condition['test'] = 'METHOD'; + break; + + default: + if (strncmp($condition['test'], 'HTTP:', 5) === 0) { + $condition['test'] = substr($condition['test'], 5) . ':'; + } + } + + /* + * Apache mod_rewrite and ISAPI_Rewrite share the concepts of strings which + * start with, end with, or contain a pattern, however they represent these + * patterns differently: + * + * | Apache mod_rewrite | ISAPI_Rewrite + * ------------------------------------------------ + * Starts with | ^blah | blah.* + * ------------------------------------------------ + * Ends with | blah$ | .*blah + * ------------------------------------------------ + * Contains | blah | .*blah.* + * + * Apache mod_rewrite and ISAPI_Rewrite also share the concept of a negative + * pattern: + * + * | Apache mod_rewrite | ISAPI_Rewrite + * ------------------------------------------------ + * Negative | !blah | (?!.*blah).* + * + * http://www.isapirewrite.com/docs/#RewriteCond + */ + $isNegative = false; + + /* Condition pattern starts with '!' */ + if (substr($condition['pattern'], 0, 1) == '!') { + $condition['pattern'] = substr($condition['pattern'], 1); + $isNegative = true; + } + + /* Condition pattern starts with '^' */ + if (substr($condition['pattern'], 0, 1) == '^') { + $condition['pattern'] = substr($condition['pattern'], 1); + } else { + $condition['pattern'] = '.*' . $condition['pattern']; + } + + /* Condition pattern ends with unescaped '$' */ + if (substr($condition['pattern'], -1) == '$' + && substr($condition['pattern'], -2) != '\\$') { + $condition['pattern'] = substr($condition['pattern'], 0, -1); + if ($isNegative) { + $condition['pattern'] = '(?!' . $condition['pattern'] . ')'; + } + } else { + if ($isNegative) { + $condition['pattern'] = '(?!' . $condition['pattern'] . ')'; + } + $condition['pattern'] = $condition['pattern'] . '.*'; + } + + $regexRules[$ruleId]['conditions'][$conditionId] = $condition; + } + } + + $count = 0; + if (isset($rule['pattern'])) { + $count = preg_match_all('/\(([^\(\)]+)\)/', $rule['pattern'], $tmp); + } $regexRules[$ruleId]['queryStringId'] = $count + 1; $regexRules[$ruleId]['queryString'] = str_replace('%', '$', $regexRules[$ruleId]['queryString']); @@ -226,7 +314,7 @@ } $Httpdini['baseFile'] .= (strpos($Httpdini['baseFile'], '?') === false) ? '?' : '&'; - $template = new GalleryTemplate(dirname(__FILE__) . '/../../../templates'); + $template = new GalleryTemplate(dirname(__FILE__) . '/../../../templates', true, false); $template->setVariable('Httpdini', $Httpdini); list ($ret, $content) = $template->fetch('Httpdini.tpl', 'modules_rewrite'); if ($ret) { Modified: trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/parser.inc =================================================================== --- trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/parser.inc 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/parsers/isapirewrite/parser.inc 2006-11-27 22:15:51 UTC (rev 15303) @@ -78,16 +78,46 @@ /** * @see RewriteParser::isValidRule */ - function isValidRule($rule, $pattern=null) { - $ret = parent::isValidRule($rule, $pattern); - if ($ret) { - if ((isset($rule['locked']) && substr($rule['pattern'], 0, 1) == '%') || - (isset($pattern) && substr($pattern, 0, 1) == '%')) { - return false; + function isValidRule($rule, $activeRule=null) { + $isValid = parent::isValidRule($rule, $activeRule); + if (!$isValid) { + return false; + } + + if (isset($rule['conditions'])) { + foreach ($rule['conditions'] as $condition) { + /* + * ISAPI_Rewrite supports only certain test strings: + * http://www.isapirewrite.com/docs/#RewriteCond + */ + if (!in_array($condition['test'], array('REQUEST_URI', + 'QUERY_STRING', + 'REQUEST_METHOD', + 'VERSION')) + && strncmp($condition['test'], 'HTTP:', 5) !== 0) { + return false; + } + + if (isset($condition['flags'])) { + foreach ($condition['flags'] as $flag) { + /* + * ISAPI_Rewrite supports only certain flags: + * http://www.isapirewrite.com/docs/#RewriteCond + */ + if (!in_array($flag, array('O'))) { + return false; + } + } + } } } - return $ret; + if ((isset($rule['pattern']) && substr($rule['pattern'], 0, 1) == '%') + || (isset($activeRule['pattern']) && substr($activeRule['pattern'], 0, 1) == '%')) { + return false; + } + + return true; } /** Modified: trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/ModRewriteHelper.class =================================================================== --- trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/ModRewriteHelper.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/ModRewriteHelper.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -268,7 +268,7 @@ $Htaccess['matchBaseFile'] = $Htaccess['directory'] . preg_quote( preg_replace('/(\?.*)/', '', $Htaccess['baseFile'])); - $template = new GalleryTemplate(dirname(__FILE__) . '/../../../templates'); + $template = new GalleryTemplate(dirname(__FILE__) . '/../../../templates', true, false); $template->setVariable('Htaccess', $Htaccess); list ($ret, $content) = $template->fetch('Htaccess.tpl', 'modules_rewrite'); if ($ret) { Modified: trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/parser.inc =================================================================== --- trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/parser.inc 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/parsers/modrewrite/parser.inc 2006-11-27 22:15:51 UTC (rev 15303) @@ -80,6 +80,34 @@ } /** + * @see RewriteParser::isValidRule + */ + function isValidRule($rule, $activeRule=null) { + $isValid = parent::isValidRule($rule, $activeRule); + if (!$isValid) { + return false; + } + + if (isset($rule['conditions'])) { + foreach ($rule['conditions'] as $condition) { + if (isset($condition['flags'])) { + foreach ($condition['flags'] as $flag) { + /* + * Apache mod_rewrite supports only certain flags: + * http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond + */ + if (!in_array($flag, array('NC', 'OR'))) { + return false; + } + } + } + } + } + + return true; + } + + /** * @see RewriteParser::getErrorMessage */ function getErrorMessage($code, $rewriteModule=null) { Modified: trunk/gallery2/modules/rewrite/classes/parsers/pathinfo/PathInfoHelper.class =================================================================== --- trunk/gallery2/modules/rewrite/classes/parsers/pathinfo/PathInfoHelper.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/classes/parsers/pathinfo/PathInfoHelper.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -87,7 +87,9 @@ foreach ($regexRules as $rule) { $queryString = 'Q' . $rule['queryString']; - if (strpos($rule['pattern'], '(') === false) { + if (!isset($rule['pattern'])) { + $static['/'] = $queryString; + } else if (strpos($rule['pattern'], '(') === false) { $static['/' . $rule['pattern']] = $queryString; } else { $find[] = '@^\/' . $rule['pattern'] . '$@'; Modified: trunk/gallery2/modules/rewrite/module.inc =================================================================== --- trunk/gallery2/modules/rewrite/module.inc 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/module.inc 2006-11-27 22:15:51 UTC (rev 15303) @@ -35,7 +35,7 @@ $this->setName($gallery->i18n('URL Rewrite')); $this->setDescription($gallery->i18n( 'Enables short URLs using Apache mod_rewrite, ISAPI Rewrite, or PathInfo')); - $this->setVersion('1.1.7'); /* Update RewriteModuleExtras::upgrade also! */ + $this->setVersion('1.1.8'); /* Update RewriteModuleExtras::upgrade also! */ $this->setGroup('gallery', $gallery->i18n('Gallery')); $this->setCallbacks('getSiteAdminViews|registerEventListeners'); $this->setRequiredCoreApi(array(7, 5)); @@ -135,13 +135,14 @@ $activeRules = unserialize($activeRules); if (empty($activeRules)) { - $activeRules = array('rewrite' => array(0 => 'v/%path%')); + $activeRules = array('rewrite' => array(0 => array('pattern' => 'v/%path%'))); /* * We'll break fast download cache if we activate the short URL for download items with * an inGallery parser */ if ($rewriteParser->getParserType() != 'inGallery') { - $activeRules['rewrite'][1] = 'd/%itemId%-%serialNumber%/%fileName%'; + $activeRules['rewrite'][1] = + array('pattern' => 'd/%itemId%-%serialNumber%/%fileName%'); } } Modified: trunk/gallery2/modules/rewrite/templates/AdminRewrite.tpl =================================================================== --- trunk/gallery2/modules/rewrite/templates/AdminRewrite.tpl 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/templates/AdminRewrite.tpl 2006-11-27 22:15:51 UTC (rev 15303) @@ -111,9 +111,12 @@ <tr class="{$rowClass}"> <td> {assign var="match" value=$AdminRewrite.info.$moduleId.$ruleId.match} - {if isset($form.error.dupe[$rule.pattern]) || isset($form.error.empty.$moduleId.$ruleId) || - isset($form.error.1.$moduleId.$ruleId) || isset($form.error.3[$match]) || - isset($form.error.4.$moduleId.$ruleId)} + {if isset($form.error.conditions.dupe.$moduleId.$ruleId) + || isset($form.error.pattern.dupe.$moduleId.$ruleId) + || isset($form.error.pattern.empty.$moduleId.$ruleId) + || isset($form.error.1.$moduleId.$ruleId) + || isset($form.error.3.$match) + || isset($form.error.4.$moduleId.$ruleId)} <img src="{g->url href="modules/core/data/module-inactive.gif"}" width="13" height="13" alt="{g->text text="Status: Error"}" /> {elseif isset($rule.active)} @@ -137,12 +140,16 @@ {$AdminRewrite.info.$moduleId.$ruleId.comment} </td> <td> + {if !isset($rule.pattern)} + {g->text text="No URL Pattern"} + {else} {if isset($AdminRewrite.info.$moduleId.$ruleId.locked)} <input type="hidden" name="{g->formVar var="form[rules][$moduleId][$ruleId][pattern]"}" value="{$rule.pattern}"/> <input type="text" size="40" name="dummy" value="{$rule.pattern}" disabled /> {else} <input type="text" size="40" name="{g->formVar var="form[rules][$moduleId][$ruleId][pattern]"}" value="{$rule.pattern}"/> {/if} + {/if} </td> <td> {foreach from=$AdminRewrite.info.$moduleId.$ruleId.keywords key=keyword item=tmp} Modified: trunk/gallery2/modules/rewrite/templates/Htaccess.tpl =================================================================== --- trunk/gallery2/modules/rewrite/templates/Htaccess.tpl 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/templates/Htaccess.tpl 2006-11-27 22:15:51 UTC (rev 15303) @@ -21,25 +21,31 @@ RewriteRule . - [L] {foreach from=$Htaccess.rules item=rule} +{if isset($rule.conditions)} +{foreach from=$rule.conditions item="condition"} + RewriteCond %{ldelim}{$condition.test}{rdelim} {$condition.pattern}{if !empty($condition.flags)} [{$condition.flags|@implode:','}]{/if} + +{/foreach} +{/if} {if isset($rule.settings.restrict)} - {foreach from=$rule.settings.restrict item=condition} +{foreach from=$rule.settings.restrict item=condition} RewriteCond %{ldelim}QUERY_STRING{rdelim} {$condition} - {/foreach} - {foreach from=$rule.settings.exempt item=host} +{/foreach} +{foreach from=$rule.settings.exempt item=host} RewriteCond %{ldelim}HTTP_REFERER{rdelim} !://{$host}/ [NC] - {/foreach} - {if $Htaccess.allowEmptyReferer} +{/foreach} +{if $Htaccess.allowEmptyReferer} RewriteCond %{ldelim}HTTP_REFERER{rdelim} !^$ - {/if} -{else} +{/if} +{elseif isset($rule.pattern)} RewriteCond %{ldelim}THE_REQUEST{rdelim} \ {$Htaccess.rewriteBase}{$rule.pattern}(\?.|\ .) RewriteCond %{ldelim}REQUEST_URI{rdelim} !{$Htaccess.matchBaseFile}$ {/if} {if strpos($rule.queryString, 'view=core.DownloadItem') !== false || strpos($rule.queryString, 'view=watermark.DownloadItem') !== false} - RewriteRule . {$Htaccess.galleryDirectory}{$Htaccess.mainPhp}?{$rule.queryString} [{$rule.settings.flags}] + RewriteRule . {$Htaccess.galleryDirectory}{$Htaccess.mainPhp}?{$rule.queryString}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} {else} - RewriteRule . {$Htaccess.directory}{$Htaccess.baseFile}{$rule.queryString} [{$rule.settings.flags}] + RewriteRule . {$Htaccess.directory}{$Htaccess.baseFile}{$rule.queryString}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} {/if} {/foreach} Modified: trunk/gallery2/modules/rewrite/templates/Httpdini.tpl =================================================================== --- trunk/gallery2/modules/rewrite/templates/Httpdini.tpl 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/templates/Httpdini.tpl 2006-11-27 22:15:51 UTC (rev 15303) @@ -11,34 +11,45 @@ RewriteRule {$Httpdini.galleryDirectory}modules/rewrite/data/isapi_rewrite/Rewrite.txt {$Httpdini.galleryDirectory}modules/rewrite/data/isapi_rewrite/Works.txt [O] {foreach from=$Httpdini.rules item=rule} +{if isset($rule.conditions)} +{foreach from=$rule.conditions item="condition"} +RewriteCond {$condition.test} {$condition.pattern}{if !empty($condition.flags)} [{$condition.flags|@implode:','}]{/if} + +{/foreach} +{/if} {if isset($rule.settings.restrict)} - {foreach from=$rule.settings.restrict item=condition} - RewriteCond URL .*\?.*{$condition}.* - {/foreach} +{foreach from=$rule.settings.restrict item=condition} +RewriteCond URL .*\?.*{$condition}.* +{/foreach} - {foreach from=$rule.settings.exempt item=host} - RewriteCond Referer: (?!.*://{$host}/.*) - {/foreach} +{foreach from=$rule.settings.exempt item=host} +RewriteCond Referer: (?!.*://{$host}/.*) +{/foreach} - {if $Httpdini.allowEmptyReferer && !empty($rule.settings.exempt)} - RewriteCond Referer: (?!^$) - {/if} +{if $Httpdini.allowEmptyReferer && !empty($rule.settings.exempt)} +RewriteCond Referer: (?!^$) +{/if} - RewriteCond Host: {$Httpdini.host} - RewriteRule {$Httpdini.rewriteBase}.* {$Httpdini.rewriteBase}{$Httpdini.baseFile}{$rule.queryString} [{$rule.settings.flags}] -{else} RewriteCond Host: {$Httpdini.host} +RewriteRule {$Httpdini.rewriteBase}.* {$Httpdini.rewriteBase}{$Httpdini.baseFile}{$rule.queryString}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} +{elseif isset($rule.pattern)} +RewriteCond Host: {$Httpdini.host} + {if strpos($rule.queryString, 'view=core.DownloadItem') !== false} - RewriteRule {$Httpdini.rewriteBase}{$rule.pattern} {$Httpdini.galleryDirectory}{$Httpdini.mainPhp}?{$rule.queryString} [{$rule.settings.flags}] +RewriteRule {$Httpdini.rewriteBase}{$rule.pattern} {$Httpdini.galleryDirectory}{$Httpdini.mainPhp}?{$rule.queryString}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} {else} - RewriteRule {$Httpdini.rewriteBase}{$rule.pattern} {$Httpdini.rewriteBase}{$Httpdini.baseFile}{$rule.queryString} [{$rule.settings.flags}] +RewriteRule {$Httpdini.rewriteBase}{$rule.pattern} {$Httpdini.rewriteBase}{$Httpdini.baseFile}{$rule.queryString}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} {/if} + RewriteCond Host: {$Httpdini.host} {if strpos($rule.queryString, 'view=core.DownloadItem') !== false} - RewriteRule {$Httpdini.rewriteBase}{$rule.pattern}\?(.*) {$Httpdini.galleryDirectory}{$Httpdini.mainPhp}?{$rule.queryString}&${$rule.queryStringId} [{$rule.settings.flags}] +RewriteRule {$Httpdini.rewriteBase}{$rule.pattern}\?(.*) {$Httpdini.galleryDirectory}{$Httpdini.mainPhp}?{$rule.queryString}&${$rule.queryStringId}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} {else} - RewriteRule {$Httpdini.rewriteBase}{$rule.pattern}\?(.*) {$Httpdini.rewriteBase}{$Httpdini.baseFile}{$rule.queryString}&${$rule.queryStringId} [{$rule.settings.flags}] +RewriteRule {$Httpdini.rewriteBase}{$rule.pattern}\?(.*) {$Httpdini.rewriteBase}{$Httpdini.baseFile}{$rule.queryString}&${$rule.queryStringId}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} {/if} +{else} +RewriteCond Host: {$Httpdini.host} +RewriteRule {$Httpdini.rewriteBase}.* {$Httpdini.rewriteBase}{$Httpdini.baseFile}{$rule.queryString}{if !empty($rule.settings.flags)} [{$rule.settings.flags}]{/if} {/if} {/foreach} Modified: trunk/gallery2/modules/rewrite/test/phpunit/AdminRewriteControllerTest.class =================================================================== --- trunk/gallery2/modules/rewrite/test/phpunit/AdminRewriteControllerTest.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/test/phpunit/AdminRewriteControllerTest.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -138,8 +138,8 @@ return $this->failWithStatus($ret); } $expected = array(); - $expected['rewrite'][0] = 'uv/%path%'; - $expected['rewrite'][1] = 'ud/%itemId%-%serialNumber%/%fileName%'; + $expected['rewrite'][0] = array('pattern' => 'uv/%path%'); + $expected['rewrite'][1] = array('pattern' => 'ud/%itemId%-%serialNumber%/%fileName%'); $this->assertEquals($expected, unserialize($value), 'activeRules'); list ($ret, $value) = GalleryCoreApi::getPluginParameter('module', 'rewrite', @@ -188,6 +188,35 @@ $this->assertEquals(array(), unserialize($value), 'shortUrls'); } + function testSaveDupeConditions() { + GalleryUtilities::putRequestVariable('form[action][rules]', true); + GalleryUtilities::putRequestVariable('form[rules][adminrewritemock][3][active]', true); + GalleryUtilities::putRequestVariable('form[rules][adminrewritemock][4][active]', true); + + $results = $this->handleRequest(); + $this->assertEquals(array('delegate' => array('view' => 'core.SiteAdmin', + 'subView' => 'rewrite.AdminRewrite', + 'mode' => 'rules'), + 'error' => array( + 'form[error][conditions][dupe][adminrewritemock][3]', + 'form[error][conditions][dupe][adminrewritemock][4]'), + 'status' => array()), $results, 'handleRequest'); + + list ($ret, $value) = GalleryCoreApi::getPluginParameter( + 'module', 'rewrite', 'activeRules'); + if ($ret) { + return $this->failWithStatus($ret); + } + $this->assertEquals(array(), unserialize($value), 'activeRules'); + + list ($ret, $value) = GalleryCoreApi::getPluginParameter( + 'module', 'rewrite', 'shortUrls'); + if ($ret) { + return $this->failWithStatus($ret); + } + $this->assertEquals(array(), unserialize($value), 'shortUrls'); + } + function testSaveEmptyPattern() { GalleryUtilities::putRequestVariable('form[action][rules]', 1); GalleryUtilities::putRequestVariable('form[rules][rewrite][0][active]', 1); @@ -197,7 +226,7 @@ $this->assertEquals(array('delegate' => array('view' => 'core.SiteAdmin', 'subView' => 'rewrite.AdminRewrite', 'mode' => 'rules'), - 'error' => array('form[error][empty][rewrite][0]'), + 'error' => array('form[error][pattern][empty][rewrite][0]'), 'status' => array()), $results, 'handleRequest'); @@ -216,7 +245,7 @@ $this->assertEquals(array(), unserialize($value), 'shortUrls'); } - function testSaveDupeRules() { + function testSaveDupePattern() { GalleryUtilities::putRequestVariable('form[action][rules]', 1); GalleryUtilities::putRequestVariable('form[rules][rewrite][0][active]', 1); GalleryUtilities::putRequestVariable('form[rules][rewrite][0][pattern]', 'dupe/'); @@ -227,7 +256,8 @@ $this->assertEquals(array('delegate' => array('view' => 'core.SiteAdmin', 'subView' => 'rewrite.AdminRewrite', 'mode' => 'rules'), - 'error' => array('form[error][dupe][dupe/]'), + 'error' => array('form[error][pattern][dupe][rewrite][0]', + 'form[error][pattern][dupe][rewrite][1]'), 'status' => array()), $results, 'handleRequest'); list ($ret, $value) = GalleryCoreApi::getPluginParameter('module', 'rewrite', @@ -302,8 +332,8 @@ $this->assertEquals(array(), unserialize($value), 'shortUrls'); } - /* It might be desirable to activate two rules for one Gallery view */ function testSaveDupeMatch() { + /* It might be desirable to activate two rules for one Gallery view */ GalleryUtilities::putRequestVariable('form[action][rules]', 1); GalleryUtilities::putRequestVariable('form[rules][adminrewritemock][0][active]', 1); GalleryUtilities::putRequestVariable('form[rules][adminrewritemock][0][pattern]', @@ -324,7 +354,8 @@ if ($ret) { return $this->failWithStatus($ret); } - $this->assertEquals(array('adminrewritemock' => array('phpunit/0/', 'phpunit/1/')), + $this->assertEquals(array('adminrewritemock' => array(array('pattern' => 'phpunit/0/'), + array('pattern' => 'phpunit/1/'))), unserialize($value), 'activeRules'); list ($ret, $value) = GalleryCoreApi::getPluginParameter('module', 'rewrite', @@ -460,18 +491,20 @@ class AdminRewriteMockParser extends RewriteParser { function AdminRewriteMockParser() { $this->_setParserId('adminrewritemock'); - $this->_setParserType('mock'); + $this->_setParserType('preGallery'); } function isValidRule($rule, $pattern=null) { - $ret = parent::isValidRule($rule, $pattern); - if ($ret) { - if ($rule['pattern'] == 'phpunit/invalid/') { - return false; - } + $isValid = parent::isValidRule($rule, $pattern); + if (!$isValid) { + return false; } - return $ret; + if (isset($rule['pattern']) && $rule['pattern'] == 'phpunit/invalid/') { + return false; + } + + return true; } function handleTestResultsRequest($form) { @@ -573,11 +606,21 @@ } function getRewriteRules() { - return array(array('pattern' => 'phpunit/dummy/%itemId%.htm', - 'match' => array('view' => 'phpunit.Test')), - array('pattern' => 'phpunit/conflict/', - 'match' => array('view' => 'phpunit.Test')), - array('pattern' => 'phpunit/invalid/')); + $rules = array(); + + $rules[0] = array('pattern' => 'phpunit/dummy/%itemId%.htm', + 'match' => array('view' => 'phpunit.Test')); + $rules[1] = array('pattern' => 'phpunit/conflict/', + 'match' => array('view' => 'phpunit.Test')); + $rules[2] = array('pattern' => 'phpunit/invalid/'); + + /* Rule 3 and 4: Dupe conditions */ + $rules[3] = array('conditions' => array(array('test' => 'REQUEST_METHOD', + 'pattern' => 'FOO'))); + $rules[4] = array('conditions' => array(array('test' => 'REQUEST_METHOD', + 'pattern' => 'FOO'))); + + return $rules; } } Modified: trunk/gallery2/modules/rewrite/test/phpunit/IsapiRewriteParserTest.class =================================================================== --- trunk/gallery2/modules/rewrite/test/phpunit/IsapiRewriteParserTest.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/test/phpunit/IsapiRewriteParserTest.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -437,5 +437,43 @@ $this->assert(!empty($errstr), 'errstr'); $this->_platform->_isWriteable = true; } + + function testSaveBadTestCondition() { + $testRules['rewriteparsermock'][4] = array(); + list ($ret, $code, $errno) = $this->_parser->saveActiveRules($testRules); + if ($ret) { + return $this->failWithStatus($ret); + } + + list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter( + 'module', 'rewrite', 'activeRules'); + if ($ret) { + return $this->failWithStatus($ret); + } + + $expected['rewriteparsermock'][0] = array('pattern' => 'bogus'); + $this->assertEquals(REWRITE_STATUS_INVALID_PATTERN, $code); + $this->assertEquals(array('rewriteparsermock', 4), $errno); + $this->assertEquals($expected, unserialize($activeRules)); + } + + function testSavePercentSignPattern() { + $testRules['rewriteparsermock'][0] = array('pattern' => '%foo'); + list ($ret, $code, $errno) = $this->_parser->saveActiveRules($testRules); + if ($ret) { + return $this->failWithStatus($ret); + } + + list ($ret, $activeRules) = GalleryCoreApi::getPluginParameter( + 'module', 'rewrite', 'activeRules'); + if ($ret) { + return $this->failWithStatus($ret); + } + + $expected['rewriteparsermock'][0] = array('pattern' => 'bogus'); + $this->assertEquals(REWRITE_STATUS_INVALID_PATTERN, $code); + $this->assertEquals(array('rewriteparsermock', 0), $errno); + $this->assertEquals($expected, unserialize($activeRules)); + } } ?> Modified: trunk/gallery2/modules/rewrite/test/phpunit/ModRewriteParserTest.class =================================================================== --- trunk/gallery2/modules/rewrite/test/phpunit/ModRewriteParserTest.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/test/phpunit/ModRewriteParserTest.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -81,7 +81,7 @@ function testSaveBadActiveRulesEmbedded() { GalleryDataCache::put('G2_EMBED', 1, true); - $testRules['rewriteparsermock'][0] = 'p/%bogus%'; + $testRules['rewriteparsermock'][0] = array('pattern' => 'p/%bogus%'); list ($ret, $code, $errno) = $this->_parser->saveActiveRules($testRules); if ($ret) { return $this->failWithStatus($ret); @@ -93,7 +93,7 @@ return $this->failWithStatus($ret); } - $expected['rewriteparsermock'][0] = 'bogus'; + $expected['rewriteparsermock'][0] = array('pattern' => 'bogus'); $this->assertEquals(REWRITE_STATUS_BAD_KEYWORD, $code); $this->assertEquals(array('rewriteparsermock', '0'), $errno); $this->assertEquals($expected, unserialize($activeRules)); Modified: trunk/gallery2/modules/rewrite/test/phpunit/RewriteApiTest.class =================================================================== --- trunk/gallery2/modules/rewrite/test/phpunit/RewriteApiTest.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/test/phpunit/RewriteApiTest.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -169,8 +169,8 @@ /* Verify the results */ $this->assert($success, 'should have succeeded'); - $expectedRules[$moduleId]['rule1'] = 'abc'; - $expectedRules[$moduleId]['rule2'] = '123'; + $expectedRules[$moduleId]['rule1'] = array('pattern' => 'abc'); + $expectedRules[$moduleId]['rule2'] = array('pattern' => '123'); $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules'); } @@ -189,7 +189,7 @@ /* Verify the results */ $this->assert($success, 'should have succeeded'); - $expectedRules[$moduleId]['rule2'] = '123'; + $expectedRules[$moduleId]['rule2'] = array('pattern' => '123'); $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules'); } @@ -227,15 +227,15 @@ /* Verify the results */ $this->assert($success, 'should have succeeded'); - $expectedRules[$moduleId]['rule1'] = 'existing pattern for r1'; - $expectedRules[$moduleId]['rule2'] = 'old pattern for r2'; + $expectedRules[$moduleId]['rule1'] = array('pattern' => 'existing pattern for r1'); + $expectedRules[$moduleId]['rule2'] = array('pattern' => 'old pattern for r2'); $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules'); list ($ret, $history) = RewriteHelper::getHistoryForModule($moduleId); if ($ret) { return $this->failWithStatus($ret); } - $this->assertEquals(array('rule0' => 'foo'), $history, + $this->assertEquals(array('rule0' => array('pattern' => 'foo')), $history, 'history for rule1 should have been removed'); } @@ -256,15 +256,15 @@ /* Verify the results */ $this->assert($success, 'should have succeeded'); - $expectedRules[$moduleId]['rule1'] = 'abc'; - $expectedRules[$moduleId]['rule2'] = '123'; + $expectedRules[$moduleId]['rule1'] = array('pattern' => 'abc'); + $expectedRules[$moduleId]['rule2'] = array('pattern' => '123'); $this->assertEquals($expectedRules, $_SERVER['saveActiveRules'], 'saved rules'); list ($ret, $history) = RewriteHelper::getHistoryForModule($moduleId); if ($ret) { return $this->failWithStatus($ret); } - $this->assertEquals(array('rule0' => 'foo'), $history, + $this->assertEquals(array('rule0' => array('pattern' => 'foo')), $history, 'history for rule1 should have been removed'); } @@ -274,10 +274,11 @@ $module = new RewriteApiMockModule(); $moduleId = $module->getId(); - $activeRules = array('a' => array('r1' => 'abc', 'r2' => '123'), - 'b' => array('r1' => '.*')); + $activeRules = array('a' => array('r1' => array('pattern' => 'abc'), + 'r2' => array('pattern' => '123')), + 'b' => array('r1' => array('pattern' => '.*'))); if ($hasActiveRulesForModule) { - $activeRules[$moduleId]['rule1'] = 'existing pattern for r1'; + $activeRules[$moduleId]['rule1'] = array('pattern' => 'existing pattern for r1'); } $expectedRules = $activeRules; @@ -292,8 +293,9 @@ if ($hasHistory) { /* Create a pattern history for that module */ - $ret = RewriteHelper::setHistoryForModule($moduleId, - array('rule2' => 'old pattern for r2', 'rule0' => 'foo')); + $ret = RewriteHelper::setHistoryForModule( + $moduleId, array('rule2' => array('pattern' => 'old pattern for r2'), + 'rule0' => array('pattern' => 'foo'))); if ($ret) { return array($ret, null, null); } Modified: trunk/gallery2/modules/rewrite/test/phpunit/RewriteHelperTest.class =================================================================== --- trunk/gallery2/modules/rewrite/test/phpunit/RewriteHelperTest.class 2006-11-27 17:48:53 UTC (rev 15302) +++ trunk/gallery2/modules/rewrite/test/phpunit/RewriteHelperTest.class 2006-11-27 22:15:51 UTC (rev 15303) @@ -141,7 +141,7 @@ /* Test basic rule without keyword */ $activeRules = array(); - $activeRules['rewritehelpermock'][0] = 'phpunit'; + $activeRules['rewritehelpermock'][0] = array('pattern' => 'phpunit'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(); $expectedRegexRules = array(array('pattern' => 'phpunit', @@ -150,9 +150,9 @@ $tests[] = array($activeRules, REWRITE_STATUS_OK, $expectedRegexRules, $expectedShortUrls, null, $activeRules, null); - /* Test rule with keyword */ + /* Test basic rule with keyword */ $activeRules = array(); - $activeRules['rewritehelpermock'][1] = 'php/%unit%'; + $activeRules['rewritehelpermock'][1] = array('pattern' => 'php/%unit%'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(); $expectedRegexRules = array(array('pattern' => 'php/(.*)', @@ -161,9 +161,9 @@ $tests[] = array($activeRules, REWRITE_STATUS_OK, $expectedRegexRules, $expectedShortUrls, null, $activeRules, null); - /* Test rule with custom functions */ + /* Test custom functions */ $activeRules = array(); - $activeRules['rewritehelpermock'][2] = 'php/%unit%'; + $activeRules['rewritehelpermock'][2] = array('pattern' => 'php/%unit%'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(array('match' => array('view' => 'php.Unit'), 'pattern' => 'php/%unit%', @@ -177,7 +177,7 @@ /* Test multiple keywords */ $activeRules = array(); - $activeRules['rewritehelpermock'][3] = 'p/%php%/%unit%'; + $activeRules['rewritehelpermock'][3] = array('pattern' => 'p/%php%/%unit%'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(); $expectedRegexRules = array(array('pattern' => 'p/([2])/([1])', @@ -188,7 +188,7 @@ /* Test query string and match combination */ $activeRules = array(); - $activeRules['rewritehelpermock'][4] = 'php/%unit%'; + $activeRules['rewritehelpermock'][4] = array('pattern' => 'php/%unit%'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(array('match' => array('view' => 'php.Unit', 'subView' => 'php.Sub'), @@ -201,7 +201,7 @@ /* Test default flags */ $activeRules = array(); - $activeRules['rewritehelpermock'][0] = 'phpunit'; + $activeRules['rewritehelpermock'][0] = array('pattern' => 'phpunit'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(); $expectedRegexRules = array(array('pattern' => 'phpunit', @@ -212,7 +212,7 @@ /* Test custom flags */ $activeRules = array(); - $activeRules['rewritehelpermock'][5] = 'phpunit'; + $activeRules['rewritehelpermock'][5] = array('pattern' => 'phpunit'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(); $expectedRegexRules = array(array('pattern' => 'phpunit', @@ -222,13 +222,27 @@ $expectedShortUrls, null, $activeRules, array('default' => array('F1', 'F2'), 'mandatory' => array('F1'))); + /* Test conditions */ + $activeRules = array(); + $activeRules['rewritehelpermock'][11] = array(); + $expectedActiveRules = $activeRules; + $expectedShortUrls = array(); + $expectedRegexRules = array(array( + 'conditions' => array(array('test' => 'REQUEST_METHOD', + 'pattern' => 'OPTIONS'), + array('test' => 'HTTP:Authorization', + 'pattern' => '(.+)')), + 'queryString' => 'g2_authorization=$1', + 'settings' => array('flags' => ''))); + $tests[] = array($activeRules, REWRITE_STATUS_OK, $expectedRegexRules, + $expectedShortUrls, null, $activeRules, null); + /* Test restrict */ $activeRules = array(); - $activeRules['rewritehelpermock'][6] = '.'; + $activeRules['rewritehelpermock'][6] = array(); $expectedActiveRules = $activeRules; $expectedShortUrls = array(); - $expectedRegexRules = array(array('pattern' => '.', - 'queryString' => 'g2_static=value', + $expectedRegexRules = array(array('queryString' => 'g2_static=value', 'settings' => array('restrict' => array('param=bogus'), 'exempt' => array('phpunit1', 'phpunit2', @@ -237,9 +251,19 @@ $tests[] = array($activeRules, REWRITE_STATUS_OK, $expectedRegexRules, $expectedShortUrls, null, $activeRules, null); + /* Test empty pattern */ + $activeRules = array(); + $activeRules['rewritehelpermock'][10] = array(); + $expectedActiveRules = $activeRules; + $expectedShortUrls = array(); + $expectedRegexRules = array(array('queryString' => 'g2_view=php.Unit', + 'settings' => array('flags' => ''))); + $tests[] = array($activeRules, REWRITE_STATUS_OK, $expectedRegexRules, + $expectedShortUrls, null, $activeRules, null); + /* Test parser */ $activeRules = array(); - $activeRules['rewritehelpermock'][7] = 'phpunit'; + $activeRules['rewritehelpermock'][7] = array('pattern' => 'phpunit'); $expectedActiveRules = $activeRules; $expectedShortUrls = array(); $expectedRegexRules = array(array('pattern' => 'phpunit', @@ -265,7 +289,7 @@ function testParseBadKeyword() { $activeRules = array(); - $activeRules['rewritehelpermock'][0] = 'p/%badKeyword%'; + $activeRules['rewritehelpermock'][0] = array('pattern' => 'p/%badKeyword%'); $expectedActiveRules = $activeRules; list ($ret, $code, $regexRules, $shortUrls, $errno) = RewriteHelper::parseActiveRules( @@ -283,7 +307,7 @@ function testParseBadParser() { $activeRules = array(); - $activeRules['rewritehelpermock'][8] = 'phpunit'; + $activeRules['rewritehelpermock'][8] = array('pattern' => 'phpunit'); $expectedActiveRules = $activeRules; list ($ret, $code, $regexRules, $shortUrls, $errno) = RewriteHelper::parseActiveRules( @@ -299,11 +323,11 @@ $this->assertEquals($expectedActiveRules, $activeRules, 'active rules'); } - /* It might be desirable to activate two rules for one Gallery view */ - function testParseDupeShortUrl() { + function testParseDupeMatch() { + /* It might be desirable to activate two rules for one Gallery view */ $activeRules = array(); - $activeRules['rewritehelpermock'][2] = 'phpunit1'; - $activeRules['rewritehelpermock'][9] = 'phpunit2'; + $activeRules['rewritehelpermock'][2] = array('pattern' => 'phpunit1'); + $activeRules['rewritehelpermock'][9] = array('pattern' => 'phpunit2'); $expectedActiveRules = $activeRules; list ($ret, $code, $regexRules, $shortUrls, $errno) = RewriteHelper::parseActiveRules( @@ -329,6 +353,28 @@ $this->assertEquals($expectedActiveRules, $activeRules, 'active rules'); } + function testParseQuoteMetaChars() { + $activeRules = array(); + $activeRules['rewritehelpermock'][1] = array('pattern' => 'php.-/%unit%'); + $expectedActiveRules = $activeRules; + $expectedShortUrls = array(); + $expectedRegexRules = array(array('pattern' => 'php\\.-/(.*)', + 'queryString' => 'g2_unit=$1', + 'settings' => array('flags' => ''))); + + list ($ret, $code, $regexRules, $shortUrls, $errno) = RewriteHelper::parseActiveRules( + $activeRules, $this->_parser); + if ($ret) { + $this->failWithStatus($ret); + } + + $this->assertEquals(REWRITE_STATUS_OK, $code, 'code'); + $this->assertEquals($expectedRegexRules, $regexRules, 'regex rules'); + $this->assertEquals($expectedShortUrls, $shortUrls, 'short urls'); + $this->assertEquals(null, $errno, 'errno'); + $this->assertEquals($expectedActiveRules, $activeRules, 'active rules'); + } + function testSetAndGetHistoryForModule() { $moduleId = 'test-' . rand(); $expected = array('r1' => 'abc', 1 => '.*'); @@ -366,7 +412,7 @@ class RewriteHelperMockParser extends RewriteParser { function RewriteHelperMockParser() { $this->_setParserId('rewritehelpermock'); - $this->_setParserType('mock'); + $this->_setParserType('preGallery'); } } @@ -387,34 +433,65 @@ } function getRewriteRules() { - return array( - array('pattern' => 'phpunit'), - array('pattern' => 'php/%unit%', - 'keywords' => array('unit' => array('pattern' => '(.*)'))), - array('match' => array('view' => 'php.Unit'), - 'pattern' => 'php/%unit%', - 'keywords' => array('unit' => array('pattern' => '(.*)', - 'function' => array('bogus', 'BogusFile', 'BogusFunction1'))), - 'onLoad' => array('bogus', 'BogusFile', 'BogusFunction2')), - array('pattern' => 'php/%unit%', - 'keywords' => array('unit' => array('pattern' => '([1])'), - 'php' => array('pattern' => '([2])'))), - array('match' => array('view' => 'php.Unit', 'subView' => 'php.Sub'), - 'pattern' => 'php/%unit%', - 'keywords' => array('unit' => array('pattern' => '(.*)')), - 'queryString' => array('static' => 'value')), - array('pattern' => 'phpunit', - 'flags' => array('C1', 'C2')), - array('pattern' => '.', - 'queryString' => array('static' => 'value'), - 'restrict' => array('param' => 'bogus'), - 'exemptReferer' => 1), - array('pattern' => 'phpunit', - 'parser' => 'mock'), - array('pattern' => 'phpunit', - 'parser' => 'bogus'), - array('match' => array('view' => 'php.Unit'), - 'pattern' => 'p')); + $rules = array(); + + /* Rule 0: Without keyword */ + $rules[0] = array('pattern' => 'phpunit'); ... [truncated message content] |