public function check_htaccess()
{
// default is assume we have mod_rewrite
$this->handler_vars['no_mod_rewrite'] = false;
// If this is the mod_rewrite check request, then bounce it as a success.
if (strpos($_SERVER['REQUEST_URI'], 'check_mod_rewrite') !== false) {
echo 'ok';
exit;
}
if (false === strpos($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
// .htaccess is only needed on Apache
// @TODO: add support for IIS and lighttpd rewrites
return true;
}
$result = false;
if (file_exists(HABARI_PATH . '/.htaccess')) {
$htaccess = file_get_contents(HABARI_PATH . '/.htaccess');
if (false === strpos($htaccess, 'HABARI')) {
// the Habari block does not exist in this file
// so try to create it
$result = $this->write_htaccess(true);
} else {
// the Habari block exists
$result = true;
}
} else {
// no .htaccess exists. Try to create one
$result = $this->write_htaccess();
}
if ($result) {
// the Habari block exists, but we need to make sure
// it is correct.
// Check that the rewrite rules actually do the job.
$test_ajax_url = Site::get_url('site') . '/check_mod_rewrite';
$rr = new RemoteRequest($test_ajax_url, 'POST', 5);
try {
$rr_result = $rr->execute();
} catch (\Exception $e) {
$result = $this->write_htaccess(true, true, true);
}
}
return $result;
}