<?php

define
('XSLT_SABOPT_PARSE_PUBLIC_ENTITIES'0);
define('XSLT_SABOPT_DISABLE_ADDING_META'1);

function 
writeToFile($filename$somecontent)
{
    
// Open the file 
    
if (!$handle fopen($filename'w')) {
         print 
"Cannot open file ($filename)";
         exit;
    }

    
// Write $somecontent to our opened file.
    
if (!fwrite($handle$somecontent)) {
        print 
"Cannot write to file ($filename)";
        exit;
    }
    
    print 
"Success, wrote the file $filename <br>";
    
fclose($handle);
}

function 
xsltTransform($from$how$to='')
{
    
$xmlHandle fopen ('baw/original/' $from"r");
    
$xml fread ($xmlHandlefilesize ('baw/original/' $from));
    
fclose ($xmlHandle);
    
    
$xml str_replace(' xmlns="http://www.w3.org/1999/xhtml"'''$xml);    
    
$xml str_replace('&''^_'$xml);
    
    
$xslHandle fopen ($how"r");
    
$xsl fread ($xslHandlefilesize ($how));
    
fclose ($xslHandle);
    
    
$arguments = array(
         
'/_xml' => $xml,
         
'/_xsl' => $xsl
    
);
    
    
// Allocate a new XSLT processor
    
$xh xslt_create();
    
    
// Process the document
    
$result xslt_process($xh'arg:/_xml''arg:/_xsl'NULL$arguments);
    
    
// Free the processor
    
xslt_free($xh);

    
// Recreate the mangled entities    
    
$result str_replace('^_''&'$result);

    
// Reput the Doctype declaration in place
    
$result str_replace('<html xml:lang="en" lang="en" id="html-#position#">''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml" id="html-#position#">'
$result);    

    
// Generate the ids
    
$lines explode("#position#"$result);
    
$i = -5;
    
$isLastLine false;
    foreach (
$lines as $line) {
        
$i += 5;
        if (
$line == end($lines)) {
            
$isLastLine true;
        }
        if (
$isLastLine == false) {
            
$output .= $line $i;
        } else {
            
$output .= $line;
        }    
    }

    
// Return the transformed document or write it to disk
    
writeToFile('baw/with-id/' $from$output);
    return;
}

?>