#!/usr/bin/php
<?php
$path_to_html_files = "PUT YOUR PATH IN HERE";
$files = traverse_html($path_to_html_files);
foreach ($files as $file)
{
echo "Loading $file.\n";
$stream = fopen($file, "r");
$contents = fread($stream, filesize($file));
fclose($stream);
if ($contents)
{
$new_contents = $contents;
$matches = array();
/*
$header_hook = '';
if (matches header)
{
$new_contents = str_replace($matches[0], '<?php include "header.php"; ?>', $new_contents);
}
*/
$footer_hook = 'Ranger Station is in no way affiliated with the Ford Motor';
$footer_hook = preg_replace('/\s+/', '\s+', $footer_hook);
if (preg_match('/<table((?!\/table).)*' . $footer_hook . '.*?<\/table>/sx', $contents, $matches))
{
$new_contents = str_replace($matches[0], '<?php include "footer.php"; ?>', $new_contents);
}
$php_file = preg_replace('/\.html$/', '.php', $file);
$stream = fopen($php_file, 'w');
fwrite($stream, $new_contents);
fclose($stream);
}
else
{
echo "!!! $file did not read any data!! \n";
}
break; // Remove this to make this global, bails after first html file.
}
function traverse_html($dir)
{
$dir = escapeshellcmd($dir);
$files = glob("$dir/*.html");
foreach (glob("$dir/{.[^.]*,*}", GLOB_BRACE|GLOB_ONLYDIR) as $sub)
{
$files = array_merge($files, traverse_html($sub));
}
return $files;
}
?>