From b785b0e7486d88b7ac200cd87cbda72014bdbe4d Mon Sep 17 00:00:00 2001 From: Jonathan Miller Date: Sun, 14 Jun 2026 21:22:30 -0500 Subject: [PATCH] feat: add .htaccess reset option to MokoRestore Add checkbox in Step 4 to reset .htaccess to Joomla defaults. Backs up existing as .htaccess.bak before overwriting. Optional and unchecked by default to preserve custom rules. --- .../src/Engine/MokoRestore.php | 134 +++++++++++++++++- 1 file changed, 133 insertions(+), 1 deletion(-) diff --git a/source/packages/com_mokosuitebackup/src/Engine/MokoRestore.php b/source/packages/com_mokosuitebackup/src/Engine/MokoRestore.php index 44809cb..0fd2eba 100644 --- a/source/packages/com_mokosuitebackup/src/Engine/MokoRestore.php +++ b/source/packages/com_mokosuitebackup/src/Engine/MokoRestore.php @@ -452,6 +452,11 @@ function actionConfig(array $data): array @unlink($bakPath); } + // Reset .htaccess to Joomla defaults if requested + if (($data['reset_htaccess'] ?? '0') === '1') { + writeDefaultHtaccess(RESTORE_DIR); + } + return ['success' => true, 'message' => 'Joomla configuration rebuilt with fresh credentials and secret']; } @@ -505,7 +510,122 @@ JCONFIG; @mkdir($tmpPath, 0755, true); @mkdir($logPath, 0755, true); - return ['success' => true, 'message' => 'configuration.php created from scratch with fresh secret']; + // Reset .htaccess to Joomla defaults if requested + if (($data['reset_htaccess'] ?? '0') === '1') { + writeDefaultHtaccess(RESTORE_DIR); + } + + return ['success' => true, 'message' => 'Joomla configuration created from scratch with fresh secret']; +} + +/** + * Write a clean Joomla default .htaccess file. + * Backs up the existing one as .htaccess.bak first. + */ +function writeDefaultHtaccess(string $siteRoot): void +{ + $htaccess = $siteRoot . '/.htaccess'; + + // Backup existing .htaccess before overwriting + if (is_file($htaccess)) { + copy($htaccess, $htaccess . '.bak'); + } + + $default = <<<'HTACCESS' +## +# @package Joomla +# @copyright (C) 2005 Open Source Matters, Inc. +# @license GNU General Public License version 2 or later; see LICENSE.txt +## + +## +# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE! +# +# The line 'Options +FollowSymLinks' may cause problems with some server +# configurations. It is required for the use of Apache mod_rewrite, but +# it may have already been set by your server administrator in a way that +# disallows changing it in this .htaccess file. If using it causes your +# server to report an error, comment it out, reload your site in your +# browser and test your SEF URLs. If they work, then it has been set by +# your server administrator and you do not need to set it here. +## + +## No directory listings + + IndexIgnore * + + +## Suppress mime type detection in browsers for unknown types + + Header always set X-Content-Type-Options "nosniff" + + +## Can be commented out if causes errors, see notes above. +Options +FollowSymLinks +Options -Indexes + +## Disable inline JavaScript when directly opening SVG files or embedding them with the object-tag + + + Header always set Content-Security-Policy "script-src 'none'" + + + +## Mod_rewrite in use. +RewriteEngine On + +## Begin - Rewrite rules to block out some common exploits. +# If you experience problems on your site then comment out the operations listed +# below by adding a # to the beginning of the line. +# This attempts to block the most common type of exploit `attempts` on Joomla! +# +# Block any script trying to base64_encode data within the URL. +RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR] +# Block any script that includes a