$mtime_htaccess) {
$no_changes_needed = false;
break;
}
}
if ($no_changes_needed) {
throw new XHTMLCompiler_Exception(503, false,
'No changes detected in xhtml-compiler/config.php,
xhtml-compiler/config.default.php or
xhtml-compiler/htaccess.php.');
}
if (!file_exists('.htaccess.in')) {
$contents = file_get_contents('.htaccess');
} else {
$contents = file_get_contents('.htaccess.in');
}
// do writeability check
if (
(
strpos($contents, $identifier_begin) === false ||
strpos($contents, $identifier_end) === false
) &&
strpos($contents, $identifier_here) === false
) {
throw new XHTMLCompiler_Exception(503, false,
'Pre-existing htaccess not configured to accept new rules');
}
// replace old rules with new set
$regex =
'/' .
preg_quote($identifier_begin, '/') .
'.+?' .
preg_quote($identifier_end, '/') .
'/s';
$contents = preg_replace($regex, $identifier_here, $contents);
} else {
$contents = $identifier_here;
}
// build the new htaccess
$new_contents = array();
$new_contents[] = $identifier_begin;
$new_contents[] = 'RewriteEngine on';
$new_contents[] = 'RewriteBase ' . $xc->getConf('web_path') . '/';
// create permanent redirects
if (file_exists('redirects.txt')) {
$redirects = explode("\n", file_get_contents('redirects.txt'));
foreach ($redirects as $redirect) {
$redirect = trim($redirect) . ' ';
if ($redirect === ' ') continue;
if ($redirect[0] === '#') continue;
list($src, $dest, $p) = explode(' ', $redirect);
if ($p !== '[P]') $src = '^' . preg_quote($src) . '$';
// We use rewrite to prevent the appending of ?f= querystring
$new_contents[] = 'RewriteRule ' . $src . ' ' . $dest . ' [R=permanent,L]';
}
}
$big_exp = array();
$directory_index = $xc->getConf('directory_index');
$indexed_dirs = $xc->getConf('indexed_dirs');
$allowed_dirs = $xc->getConf('allowed_dirs');
foreach ($allowed_dirs as $dir => $recursive) {
$r = '';
if ($recursive) {
$r = "([^/]+/)*"; // escaped slashes not necessary
}
$len = strlen($dir);
$slash = (!$len || $dir[$len-1] === '/') ? '' : '/';
$dir_exp = preg_quote($dir) . $slash . $r;
if (is_array($indexed_dirs)) {
$intercept = isset($indexed_dirs[$dir]) ? $indexed_dirs[$dir] : true;
} else {
$intercept = $indexed_dirs;
}
if (is_string($directory_index) && $intercept) {
// setup index rewrite
$new_contents[] = "RewriteRule ^($dir_exp)$ \$1$directory_index";
}
$big_exp[] = $dir_exp;
}
$full_dir_exp = implode('|', $big_exp);
$new_contents[] = 'RewriteCond %{REQUEST_FILENAME} !-f [OR]';
$new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1 [OR]';
$new_contents[] = 'RewriteCond %{HTTP_COOKIE} purgatory=1';
$new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
// if purge is set, also handle directories
$new_contents[] = 'RewriteCond %{QUERY_STRING} purge=1';
$new_contents[] = "RewriteRule ^($full_dir_exp)$ xhtml-compiler/main.php?f=\$1 [L,QSA]";
// add application/xhtml+xml if the browser supports it
$new_contents[] = 'RewriteCond %{HTTP_ACCEPT} application/xhtml\\+xml';
$new_contents[] = "RewriteRule ^(($full_dir_exp)[^/]+\.html)$ - \"[T=application/xhtml+xml,L]\"";
// xc-deps are forbidden to outside world
$new_contents[] = '