Skip to content

Instantly share code, notes, and snippets.

@katgirl
Created August 8, 2013 16:23
Show Gist options
  • Save katgirl/6186140 to your computer and use it in GitHub Desktop.
Save katgirl/6186140 to your computer and use it in GitHub Desktop.
sucht alle .htaccess Dateien und ersetzt den Inhalt gegen Anweisungen, die vom Apache 2.4 interpretiert werden können.
<?php
function rglob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}
foreach (rglob('.htaccess') as $filename)
{
$filename = realpath ($filename);
$tmpfilename = str_replace('.htaccess', '.htaccess.backup', $filename);
if (!copy($filename, $tmpfilename)) {
echo 'can\'t copy $filename ...' . PHP_EOL;
}
echo PHP_EOL . ' read $filename ' . PHP_EOL;
$arrZeilen = file($filename);
$content = '';
foreach ($arrZeilen as $zeile)
{
$zeile = preg_replace ( '/[^a-z]/', '', strtolower($zeile) );
$zeile = preg_replace ( '/orderallowdeny/', '<IfModule !mod_authz_core.c>' . PHP_EOL . ' Order allow,deny', $zeile );
$zeile = preg_replace ( '/orderdenyallow/', '<IfModule !mod_authz_core.c>' . PHP_EOL . ' Order deny,allow', $zeile );
$zeile = preg_replace ( '/denyfromall/', ' Deny from all' . PHP_EOL . '</IfModule>' . PHP_EOL . '<IfModule mod_authz_core.c>' . PHP_EOL . ' Require all denied' . PHP_EOL . '</IfModule>', $zeile );
$zeile = preg_replace ( '/allowfromall/', ' Allow from all' . PHP_EOL . '</IfModule>' . PHP_EOL . '<IfModule mod_authz_core.c>' . PHP_EOL . ' Require all granted' . PHP_EOL . '</IfModule>', $zeile );
$content .= $zeile . PHP_EOL;
}
if (is_writable($filename))
{
if (!$handle = fopen($filename, 'w')) {
print 'Can\'t open $filename';
}
if (!fwrite($handle, $content)) {
print 'Can\'t write $filename';
}
fclose($handle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment