<?php
    
if (isset($_POST['submit']))
    {
        
// Import class
        
require_once ('classPak.php');

        
// Create a new PAK instance
        
$pak = new classPak();
        
        
// Add crosshair 1?
        
if ($_POST['ch1'] == '1')
        {
            
$pak->add_file ('./ch1.pcx''pics/ch1.pcx');
        }
        
        
// Add crosshair 2?
        
if ($_POST['ch2'] == '1')
        {
            
$pak->add_file ('./ch2.pcx''pics/ch2.pcx');
        }

        
// Add textures?
        
if ($_POST['textures'] == '1')
        {
            
$pak->add_directory ('./textures''textures/r1ch');
        }

        
// No optimization possible in this example, so pointless trying to do so.
        
$pakdata $pak->generate_pak (FALSE);

        
// HTTP headers so the .pak is saved properly
        
header ("Content-type: application/octet-stream");
        
header ("Content-disposition: attachment; filename=\"custom.pak\"");
        
        print 
$pakdata;
    }
    else
    {
    
?>
    <html>
    <head><title>quake pak files in php - classPak</title></head>
    <body>
    <h1>quake pak files in php - classPak</h1>
    <h2>Introduction</h2>
    <p>classPak is a PHP class for creating dynamic PAK files for the Quake I and II engines. Get classPak (<a href="classPak.txt">download</a>, <a href="classPak.phps">view</a>) and read below to see how to use it.</p>
    
    <h2>Synopsis</h2>
    <p><b>add_file (realpath, pakpath)</b><br>
    Add the physical file specified by <i>realpath</i> to the pak file as <i>pakpath</i>. Returns TRUE if successful, FALSE otherwise (eg, too long a pakpath). Adding more than one file with the same <i>pakpath</i> will result in undefined behaviour.</p>

    <p><b>add_directory (realpath, pakpath [, match])</b><br>
    A helper function that recurses through the directory <i>realpath</i> and adds every file found to the pak file under <i>pakpath</i>. The optional <i>match</i> parameter is a regular expression specifying the pattern of <u>files</u> to include. If not specified, all files will be included. Returns TRUE on success, FALSE on failure (eg bad dir).</p>

    <p><b>remove_file_by_path (realpath)</b><br>
    Removes the file <i>realpath</i> from the pak file by matching its real path. You cannot remove whole directories using this function.</p>

    <p><b>remove_file_by_pakpath (pakpath)</b><br>
    Removes the file <i>pakpath</i> from the pak file by matching its pak path. You cannot remove whole directories using this function.</p>

    <p><b>generate_pak (optimize)</b><br>
    Generate the pak file. Returns a binary string containing the entire pak file data if successful. If <i>optimize</i> is TRUE, the data will be optimized to remove duplicate file data (see <a href="http://www.r1ch.net/forum/index.php?topic=196">pakoptimz</a>). This may make the pak file smaller at the expense of being incompatible with some pak editors.</p>

    <p><b>save_pak (path, optimize)</b><br>
    A helper function that generates and writes the .pak to <i>path</i>. See above for details about <i>optimize</i>.

    <p>Example:</p>
    <?php
        $code 
"<?php
    require_once ('classPak.php');

    \$packfile = new classPak();
    \$packfile->add_file ('./data/map1.bsp', 'maps/map1.bsp');
    \$packfile->add_directory ('./data/textures/map1', 'textures/map1');

    \$pakdata = \$packfile->generate_pak (TRUE);
    header ('Content-type: application/octet-stream');
    header ('Content-disposition: attachment; filename=\"pak9.pak\"');
    print \$pakdata;
?>"
;
            
        
highlight_string ($code);
    
?>

    <h2>Example Implementation</h2>
    <p>See the <a href="pakgen.phps">source</a> for the details.</p>
    
    <form method="post" action="pakgen.php">
    <p>Select the files you want in your customised pak:</p>
    
    <p><input type="checkbox" name="ch1" value="1"> Crosshair 1</p>
    <p><input type="checkbox" name="ch2" value="1"> Crosshair 2</p>
    <p><input type="checkbox" name="textures" value="1"> Textures</p>
    
    <p><input type="submit" name="submit" value="Generate .pak"></p>
    </body>
    </html>
    <?
    
}
?>