Skip to content

Commit

Permalink
fix: Rules added multiple times to /site/templates/.htaccess
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Nov 15, 2023
1 parent de06831 commit 6123c1a
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions RockFrontend.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2206,17 +2206,25 @@ public function svg($filename, $replacements = [])
private function updateHtaccess()
{
$file = $this->wire->config->paths->templates . ".htaccess";

// if htaccess file does not exist, create it
if (!is_file($file)) $this->wire->files->filePutContents($file, "");

// get content of files and content of rules to apply
$content = $this->wire->files->fileGetContents($file);
$rules = $this->wire->files->fileGetContents(__DIR__ . "/stubs/htaccess.txt");

// prepare error message
$err = "/site/templates/.htaccess not writeable - some template files might be publicly accessible!";
if (!strpos($content, "# RockFrontend: ")) {
// add rockfrontend rules

// check if rules are added to htaccess file
if (strpos($content, "# RockFrontend: ") === false) {
// no rockfrontend rules in the htaccess file, try to add them
if (is_writable($file)) {
$this->wire->files->filePutContents($file, $rules, FILE_APPEND);
} else $this->error($err);
} elseif (!strpos($content, $rules)) {
// update rockfrontend rules
} elseif (strpos($content, $rules) === false) {
// rockfrontend rules found, but outdated
$newcontent = preg_replace(
"/# RockFrontend: (.*)# End RockFrontend/s",
$rules,
Expand Down

0 comments on commit 6123c1a

Please sign in to comment.