PHP Class for modifying old HTML static copies / mirrors

This article has been written before more than 24months, information might old.

Ok you made a static mirror(maybe with HTTrack, maybe with something else) and you still want to host it somewhere, but you also want to make a change to all the static pages without modifying each and every one of the files. In that case, you need to load that file using a script that reads the file at the time of loading then makes the changes you want and after that displays it. Sound simple enough so I made a PHP class that does just that and I even wrote 2 flavors of the script to work on ancient PHP version( and by ancient I mean around 2010 5.3.x).

The idea is simple, first redirect all request to the script using your web server configuration here is a simple apache .htaccess example:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.html$ HaCopyAn.php

Then here is the class for newer PHP Versions(it will be instantiated in the script too):

<?php

class HaCopyAn{
    
    private $base="";
    private $rep = [];
    
    public function __construct(string $base="", array $rep=[]){
       
       $this->base = $base;
       $this->rep = $rep;
       
    } 
    
    public function addSearchRep(string $search, string $replace){
        
        $this->rep[$search] = $replace;
        
    }
    
    
    public function serve(){

        $servedFile = str_replace($this->base, "", $_SERVER["REQUEST_URI"]);

        if(file_exists($servedFile)){
            $fileToServe = file_get_contents($servedFile);
        
            foreach ($this->rep as $key => $value) {
                $fileToServe = preg_replace($key, $value, $fileToServe);
            }
                
            echo $fileToServe;
            
        }else{
            $this->e404();
        }
        
    }
    
    private function e404(){
        http_response_code(404);
        echo " 
            <!DOCTYPE html>
            <html>
            <head>
            <title>404 - Not Found</title>
            </head>
            <body>

            <h1>404 - Not Found</h1>
            <p>Request could not resolve.</p>

            </body>
            </html>
            ";
        die();
        
    }

    
}

$a = new HaCopyAn("");
$a->addSearchRep('/<body.*?>/s', ' <body class="template-richdocument_view portaltype-richdocument site-ro section-index.shtml icons-on subsection0-index.shtml subsite-romania" dir="ltr"><style>.error {
  width: 100%;
  height: 30px;
  background-color: #e74c3c;
  line-height: 30px;
  padding: 5px 10px 5px 10px;
  font-family: arial;
  color: white;
  font-weight: bold;
  border-radius: 3px;
}</style><p class="error">
  Alert: This is an old page, no new updates will be made.
</p>');
$a->serve();


And now for an ancient PHP version:

<?php

class HaCopyAnOld{
    
    private $base="";
    private $rep = array();
    private $relative = false;
    
    public function __construct($base, $rep){
       
       $this->base = $base;
       $this->rep = $rep;
       
    } 
    
    public function addSearchRep($search, $replace){
        
        $this->rep[$search] = $replace;
        
    }
    
    public function serve(){

        if($relative){
        $servedFile = str_replace($this->base, "", $_SERVER["REQUEST_URI"]);
        }else{
         $servedFile = $this->base.$_SERVER["REQUEST_URI"];
        }
        if(file_exists($servedFile)){
            $fileToServe = file_get_contents($servedFile);
        
            foreach ($this->rep as $key => $value) {
                $fileToServe = preg_replace($key, $value, $fileToServe);
            }
                
            echo $fileToServe;
            
        }else{
            $this->e404();
        }
        
    }
    
    private function e404(){
        header('X-PHP-Response-Code: 404', true, 404);
        echo " 
            <!DOCTYPE html>
            <html>
            <head>
            <title>404 - Not Found</title>
            </head>
            <body>

            <h1>404 - Not Found</h1>
            <p>Request could not resolve.</p>

            </body>
            </html>
            ";
        die();
        
    }

    
}

$a = new HaCopyAnOld("/var/www/www1/example-website/",array(), false);
$a->addSearchRep('/<body.*?>/s', ' <body class="template-richdocument_view portaltype-richdocument site-ro section-index.shtml icons-on subsection0-index.shtml subsite-romania" dir="ltr"><style>.error {
  width: 100%;
  height: 30px;
  background-color: #e74c3c;
  line-height: 30px;
  padding: 5px 10px 5px 10px;
  font-family: arial;
  color: white;
  font-weight: bold;
  border-radius: 3px;
}</style><br><p class="error">
  <b>Alert:</b> This is an old page, no new updates will be made.</p>');
$a->serve();

As you can see, there isn’t a drastic difference between the syntax of both flavours.
Anyway here is a zip archive if you want to download: .

Share the joy

Leave a Reply