public function write_htaccess($exists = false, $update = false, $rewritebase = true)
{
$htaccess = $this->htaccess();
if ($rewritebase) {
$rewrite_base = trim(dirname($_SERVER['SCRIPT_NAME']), '/\\');
$htaccess['rewrite_base'] = 'RewriteBase "/' . $rewrite_base . '"';
}
$file_contents = "\n" . implode("\n", $htaccess) . "\n";
if (!$exists) {
if (!is_writable(HABARI_PATH)) {
// we can't create the file
return false;
}
} else {
if (!is_writable(HABARI_PATH . '/.htaccess')) {
// we can't update the file
return false;
}
}
if ($update) {
// we're updating an existing but incomplete .htaccess
// care must be take only to remove the Habari bits
$htaccess = file_get_contents(HABARI_PATH . '/.htaccess');
$file_contents = preg_replace('%### HABARI START.*?### HABARI END%ims', $file_contents, $htaccess);
// Overwrite the existing htaccess with one that includes the modified Habari rewrite block
$fmode = 'w';
} else {
// Append the Habari rewrite block to the existing file.
$fmode = 'a';
}
// Save the .htaccess
if ($fh = fopen(HABARI_PATH . '/.htaccess', $fmode)) {
if (false === fwrite($fh, $file_contents)) {
return false;
}
fclose($fh);
} else {
return false;
}
return true;
}