Skip to content

Commit

Permalink
htaccess patching
Browse files Browse the repository at this point in the history
  • Loading branch information
EsdertCO committed Aug 28, 2020
1 parent 5cc5791 commit 319caf6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ or add the following to your composer.json file and run `composer update`.
Supports the patching of the following things:
- Language tags
- Replaces a language tag with another language tag for all languages.
- Htaccess
- Adds security things to the default htaccess. See the config for the options.

#### Publish
- ##### publish:config
Expand Down
38 changes: 38 additions & 0 deletions src/Config/laravel-stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@
*/
'language_tags' => [
'passwords.user'
],
'htaccess' => [
'headers' => [
'needsModule' => false,
'values' => [
'Header always set X-Frame-Options SAMEORIGIN',
'Header always set X-XSS-Protection 1;mode=block',
'Header always set X-Content-Type-Options nosniff',
'Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" env=HTTPS',
]
],
'mod_expires.c' => [
'needsModule' => true,
'values' => [
'ExpiresActive on',
'AddType application/font-sfnt otf ttf',
'AddType application/font-woff woff',
'AddType application/font-woff2 woff2',
'AddType application/vnd.ms-fontobject eot',
'ExpiresDefault "access plus 2 days"',
'ExpiresByType image/jpg "access plus 2 months"',
'ExpiresByType image/svg+xml "access 2 months"',
'ExpiresByType image/gif "access plus 2 months"',
'ExpiresByType image/jpeg "access plus 2 months"',
'ExpiresByType image/png "access plus 2 months"',
'ExpiresByType text/css "access plus 2 months"',
'ExpiresByType text/javascript "access plus 2 months"',
'ExpiresByType application/javascript "access plus 2 months"',
'ExpiresByType application/x-shockwave-flash "access plus 2 months"',
'ExpiresByType image/ico "access plus 2 months"',
'ExpiresByType image/x-icon "access plus 2 months"',
'ExpiresByType text/html "access plus 600 seconds"',
'ExpiresByType application/font-woff "access plus 2 months"',
'ExpiresByType application/font-woff2 "access plus 2 months"',
'ExpiresByType application/font-sfnt "access plus 2 months"',
'ExpiresByType application/vnd.ms-fontobject "access plus 2 months"',
]
]
]
]
];
50 changes: 49 additions & 1 deletion src/Console/Patch.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;

class Patch extends Command
{
Expand All @@ -31,7 +32,8 @@ class Patch extends Command
*/
public function handle()
{
$this->handleLanguagePatching();
// $this->handleLanguagePatching();
$this->handleHtaccessPatching();

$this->info('Successfully patched Laravel :)');
}
Expand Down Expand Up @@ -68,4 +70,50 @@ private function handleLanguagePatching()
}
}
}

private function handleHtaccessPatching()
{
foreach (\config('laravel-stubs.patch.htaccess') as $groupName => $options) {
$fileContents = \file_get_contents(\base_path('public/.htaccess'));

$extraContents = "";
$tabChar = "";
$needsCloser = false;
if ($options['needsModule']) {
$tabChar = "\t";
if (!Str::contains($fileContents, '<IfModule ' . $groupName . '>')) {
$needsCloser = true;
$groupName = "<IfModule " . $groupName . ">\n";
$extraContents .= $groupName;
}
}

$prevFirstChar = null;
foreach ($options['values'] as $value) {
if (!Str::contains($fileContents, $value)) {
if (!\is_null($prevFirstChar) && $prevFirstChar !== $value[0]) {
$extraContents .= "\n";
}

$extraContents .= $tabChar . $value . "\n";
$prevFirstChar = $value[0];
}
}

if ($needsCloser) {
$extraContents .= "</IfModule>\n";
}

if (!empty($extraContents)) {
if ($options['needsModule'] && !$needsCloser) {
$fileContents = \preg_replace('/(' . $groupName . ')([\s\S]+)(<\/IfModule>)/', "$1$2\n" . $extraContents . "$3", $fileContents);
}
else {
$fileContents .= "\n" . $extraContents;
}
}

\file_put_contents(\base_path('public/.htaccess'), $fileContents);
}
}
}

0 comments on commit 319caf6

Please sign in to comment.