Show mod_rewrite.php syntax highlighted
<?php
/**
* This class rewrites all URL links to something more readable (SEO)
*/
class Rewrite {
public static function Start() {
//ini_set('zlib.output_compression', 'Off');
//ini_set('zlib.output_compression_level', 6);
ob_start();
}
public static function End() {
global $XCS;
$contents = ob_get_contents();
if (!Ajax :: sendNewContent()) {
try {
$contents = str_replace('<!--PAGENAME-->', Cache :: getObject('page', 'name'), $contents);
} catch (NoCacheException $e) {
// continue
}
if (Engine :: getFlag('separate_theme_header') === true && Engine :: file_exists("styles/" . $XCS->getDefaultTheme() . "/header.php"))
include_once ("styles/" . $XCS->getDefaultTheme() . "/header.php");
}
$contents = str_replace('<body>', '<body id="body"><div id="loader"></div>', $contents);
$contents = str_replace('</body>', file_get_contents('core/footer.html') . '</body>', $contents);
$contents = str_replace('<div id="author"></div>', '<div id="author">' . file_get_contents('core/author.html') . '</div>', $contents);
$contents = self :: RewriteURL($contents);
if (!Ajax :: sendNewContent()) {
Ajax :: Process();
if (Debug :: active()) {
Debug :: printDebug();
}
//$contents = String::cleanXHTMLDocument($contents);
} else {
$js = Ajax :: getScriptsOnDemand();
if (sizeof($js)) {
$val = $js[0];
$val = str_replace("|", "!!pipe!!", $val);
$val = str_replace("~", "!!tilde!!", $val);
$val = utf8_encode($val);
$contents = "+:TabJS|" . $val . "~" . $contents;
} else {
$contents = "+:" . $contents;
}
}
ob_end_clean();
ob_start();
echo $contents;
ob_end_flush();
}
/**
* This method rewrites all links on current page so it reflects to
* mod_rewrite links configured in .htaccess
*
* @param string $content (x)HTML source
* @return string rewritten (x)HTML source
*/
public static function RewriteURL($content) {
global $Config;
$urlin = array (
"'(?<!/)index\.php'",
"'(?<!/)index\.(.+)module=(\w+)(.+)op=read(.+)id=([0-9]+)(\w+)?'e",
"'(?<!/)index\.(.+)module=(\w+)(.+)op=section(.+)sid=([0-9]+)(\w+)?'e",
"'(?<!/)index\.(.+)module=(\w+)(.+)op=gameinfo(.+)id=([0-9]+)(\w+)?'e",
"'(?<!/)index\.(.+)module=(\w+)(.+)op=by_letter(.+)letter=([a-z0-9]+)(\w+)?'e",
"'(?<!/)index\.(.+)module=(\w+)(.+)op=info(.+)id=([0-9]+)(.+)file=([0-9]+)(\w+)?'e",
'/href="[\/]*([^#":\(\)]+)"/',
'/action="[\/]*([^#":\(\)]+)"/',
'/src="[\/]*([^#":\(\)]+)"/',
'/background="[\/]*([^#":\(\)]+)"/'
);
$urlout = array (
"index.html",
"self::outputSEO('\\2','\\5',\"heading\").'.html'",
"self::outputSEO('\\2','\\5',\"section\")",
"self::outputSEO('\\2','\\5',\"heading\").'.html'",
"self::outputSEO('\\2','\\5',\"by_letter\")",
"self::outputSEO('\\2','\\7-\\5',\"heading\").'.html'",
"href=\"" . $Config->getPath() . "\\1\"", "action=\"" .
$Config->getPath() . "\\1\"", "src=\"" .
$Config->getPath() . "\\1\"", "background=\"" .
$Config->getPath() . "\\1\"");
if (!User :: inAdmin())
$content = preg_replace($urlin, $urlout, $content);
$content = str_replace("html&", "html?", $content);
return $content;
}
public static function RepairURL($url, $delimiter = "&") {
return $url;
$url = str_replace("index.php", "index.html", $url);
if (!strpos($url, "?") && strpos($url, $delimiter)) {
$retval = "";
$repaired = false;
foreach (str_split($url) as $char) {
if ($char == $delimiter && $repaired === false) {
$char = "?";
$repaired = true;
}
$retval .= $char;
}
return $retval;
} else
return $url;
}
/**
* Tries to load desired module and execute his SEO method.
*
* @param string $module Module name
* @param string $source Source for replacement
* @param string $name SEO Type ID name
* @return string rewritten link
*/
public static function outputSEO($module, $source, $name) {
global $XCS, $Config;
$module = ModulesManager :: loadModule($module);
if ($name == "module") {
if (!@ file_exists(String :: TEXT2URL($module->getNick()) . "/"))
return String :: TEXT2URL($module->getNick()) . "/";
else
return $Config->getPath() . "index.html?module=" . $module->getInstance();
}
if (isset ($module) && method_exists($module, 'SEO')) {
$retval = $Config->getPath() . $module->SEO($source, $name);
} else
if (isset ($module->object) && $module->object->_getSEO($source, $name) !== false)
$retval = $Config->getPath() . $module->object->_getSEO($source, $name);
else
$retval = "index.php?module=" . $module->getInstance() . "&id=" . $source;
//$retval = self::RepairURL($retval);
return $retval;
}
}
Event :: registerHandler("onBoot", array (
"Rewrite",
"Start"
));
?>
See more files for this project here