function ApplyRedirects($req, $rules) { $doRedirect = false; foreach ($rules as $rule) { //if (!empty($rule->match)) { // FIXME - Never ever upgrade to PHP 5.2.6. It breaks empty() on SimpleXML objects. if ($rule->match) { $ismatch = true; $isexcept = false; $matchvars = array(NULL); // Force first element to NULL to start array indexing at 1 (regex-style) foreach ($rule->match->attributes() as $matchkey => $matchstr) { $checkstr = array_get($req, $matchkey); if ($checkstr !== NULL) { $m = NULL; if (substr($matchstr, 0, 1) == "!") { $ismatch &= !preg_match("#" . substr($matchstr, 1) . "#", $checkstr, $m); } else { $ismatch &= preg_match("#" . $matchstr . "#", $checkstr, $m); } //Logger::Debug("Check rewrite (%s): '%s' =~ '%s' ? %s", $matchkey, $checkstr, $matchstr, ($ismatch ? "YES" : "NO")); if (is_array($m) && count($m) > 0) { if (count($m) > 1) { for ($i = 1; $i < count($m); $i++) { $matchvars[] = $m[$i]; } } } } else { if (substr($matchstr, 0, 1) != "!") { $ismatch = false; } } } if ($ismatch && $rule->except) { $exceptflag = true; foreach ($rule->except->attributes() as $exceptkey => $exceptstr) { $checkstr = array_get($req, $exceptkey); if ($checkstr !== NULL) { $m = NULL; if (substr($exceptstr, 0, 1) == "!") { $exceptflag &= !preg_match("#" . substr($exceptstr, 1) . "#", $checkstr, $m); } else { $exceptflag &= preg_match("#" . $exceptstr . "#", $checkstr, $m); } } } if ($exceptflag) { $isexcept = true; } } if ($ismatch && !$isexcept) { // Apply nested rules first... if ($rule->rule) { $req = $this->ApplyRedirects($req, $rule->rule); } // Then process "set" command if ($rule->set) { Logger::Info("Applying redirect:\n " . $rule->asXML()); if (!empty($req["args"]["testredir"])) { print "<pre>" . htmlspecialchars($rule->asXML()) . "</pre><hr />"; } foreach ($rule->set->attributes() as $rewritekey => $rewritestr) { if (count($matchvars) > 1 && strpos($rewritestr, "%") !== false) { $find = array(NULL); for ($i = 1; $i < count($matchvars); $i++) { $find[] = "%{$i}"; } $rewritestr = str_replace($find, $matchvars, $rewritestr); } array_set($req, (string) $rewritekey, (string) $rewritestr); } if ($rule["type"] == "redirect") { $doRedirect = 301; } else { if ($rule["type"] == "bounce") { $doRedirect = 302; } } } // And finally process "unset" if ($rule->unset) { $unset = false; foreach ($rule->unset->attributes() as $unsetkey => $unsetval) { if ($unsetkey == "_ALL_" && $unsetval == "ALL") { $req["args"] = array(); } else { if (!empty($unsetval)) { $reqval = array_get($req, $unsetkey); if ($reqval !== NULL) { array_unset($req, $unsetkey); $unset = true; } } } } if ($unset) { if ($rule["type"] == "redirect") { $doRedirect = 301; } else { if ($rule["type"] == "bounce") { $doRedirect = 302; } } } } if ($doRedirect !== false) { break; } } } } if ($doRedirect !== false) { $origscheme = "http" . ($req["ssl"] ? "s" : ""); if ($req["host"] != $_SERVER["HTTP_HOST"] || $req["scheme"] != $origscheme) { $newurl = sprintf("%s://%s%s", $req["scheme"], $req["host"], $req["path"]); } else { $newurl = $req["path"]; } if (empty($req["args"]["testredir"])) { if (empty($req["friendly"])) { $querystr = makeQueryString($req["args"]); $newurl = http_build_url($newurl, array("query" => $querystr)); } else { $newurl = makeFriendlyURL($newurl, $req["args"]); } if ($newurl != $req["url"]) { http_redirect($newurl, NULL, true, $doRedirect); } } else { print_pre($req); } } return $req; }
function makeFriendlyURL($pre, $suff, $path) { $this->loadExtension('DeprecatedAPI'); return makeFriendlyURL($pre, $suff, $path); }