<?
    
/* Usage - call base URL and add wordconv.php?name=xxx where xxx =  
       filename of .doc file to be converted (without .doc extension)
       For example: 
       http://mydomain/mydirectory/wordconv/wordconv.php?name=try
       to convert try.doc in mydirectory
       * Create a subdirectory called wordconv and make it readable and 
         writable by the web server (aka php) process.
       * Put this script into the wordconv subdirectory.  The parent
         directory should contain the .doc file.
       * To prevent a security problem make this script read only to the
         web server process.
       
       This script will create all .html, .png, etc. files in the wordconv 
       subdirectory.  The .html files will not be deleted after serving.
       The created html files are reconverted when the modification time 
       on the .doc file is newer than the .html file.  If the .html file
       is already present, it is served up directly (i.e. cached) without
       reconversion.

       Requires: 
       PHP - http://www.php.net
       wvWare - http://wvware.sourceforge.net

       Note:
       Change the path of wvWare in the $wvware variable below to match
       the path of your wvWare executable.
       
       Created and copyrighted by Mark Warburton - spam2@instruform.com
       11 Jan 2002
       Licensed under the GPL.
       Revision History:
       v1.01 15 Jan 2002 Moved the script to the wordconv subdirectory
                         to ensure that URL for created graphics is correct.
       v1.02 30 Jan 2002 Add a sanity check to ensure that the wvWare
                         executable is present and executable.

       I would like to dedicate this work to my wife for all of her support in my endeavours
       I wish her all the best in hers: <A HREF="www.cwarburton.com">
       http://www.cwarburton.com/</A>
    */

    /* Where is wvware installed?  Command line options? */
    
$wvware "/usr/bin/wvWare";
    
$wvware_options "-d";
       
    function 
updateword($wordfilename$htmlfilename) {
        global 
$wvware$wvware_options;
        
$htmldir dirname ($htmlfilename);
        
/* ensure that we get any images into the html directory */
        
exec("$wvware $wvware_options $htmldir $wordfilename > $htmlfilename");
        
readfile($htmlfilename);
    }
    
    
/* What absolute directory are we in on the file system? */
    
$basedir dirname (  getenv ("SCRIPT_FILENAME") ); 
    
    
/* Work relative to this directory */
    
$wordfilename $basedir "/../" escapeshellcmd($name) . ".doc";
    
    
$htmldir $basedir "/";
    
$htmlfilename $htmldir escapeshellcmd($name) . ".html";
    
    
/* Sanity checks */
    
if (! file_exists($wordfilename)) 
        die(
"Unable to open file $name ($wordfilename). Set URL variable " .
            
"?name=xxx to the base filename of the word file (e.g. try " .
            
"for try.doc).  Do not include path!  This script should be in " .
            
"the wordconv subdirectory to the .doc file.");
    if (! 
is_dir($htmldir))
        die(
"Directory $htmldir does not exist.  It must be " .
            
"created and readable and writable by your web server.");
    if ((! 
is_writeable($htmldir)) || (! is_readable($htmldir)))
        die(
"Directory $htmldir must be readable and writable by your " .
            
"web server.");
    if (
file_exists($htmlfilename) && (! is_writeable($htmlfilename)))
        die(
"The html file ($htmlfilename) exists already but is not " .
            
"writable by the web server.");
    if (! 
file_exists($wvware))
        die(
"The wvWare executable file $wvware cannot be found.  Please " .
            
"ensure that the \$wvware variable in the script is pointed " .
            
"to your wvware executable.");
    if (! 
is_executable($wvware))
        die(
"The wvWare executable file $wvware is not " .
            
"executable by the web server process.  Please change the file " .
            
"permissions to make it executable.");

    
/* Do the work */
    
if (file_exists($htmlfilename)) {
        
/* Do we need to update the html file? */
        
if (filectime($wordfilename) > filectime($htmlfilename))
            
updateword($wordfilename$htmlfilename);
        else 
readfile ($htmlfilename);
    }
    else  
updateword($wordfilename$htmlfilename);
?>