<?php
    # $Id: benchmarksArchive_DOWNLOAD.php,v 1.4 2007/01/14 15:19:02 jarda Exp $

    /* Page that downloads archive of benchmarks matching the specified searching criteria
    */

    require_once('simplePage.inc.php');


    class PageClass extends ViewSimplePage {


        function createPage()
        {
            if ($_GET['fileNameBase'])
            {
                if (file_exists($this->getExistenceFileName()) &&
                    file_exists($this->getArchiveFileName())) {
                    header("Content-Type: application/force-download");
                    header("Content-Disposition: attachment; filename=\"benchmarksArchive.". BenchConfig::$archiveFileExtension . "\"");
                    readfile($this->getArchiveFileName());
                }
                else
                    $this->refresh($_GET['fileNameBase']);

            }
            elseif ($_POST['query'])
            {
                $fileNameBase = md5(session_id() . time());
                
                $file = fopen(BenchConfig::$tmpDirectory . "/$fileNameBase.query", "w");
                fwrite($file, $_POST['query']);
                fclose($file);

                exec(BenchConfig::$pythonBinary . " " . BenchConfig::$execDirectory . 
                     "/CreateBenchmarksArchive.py $fileNameBase");

                $this->refresh($fileNameBase);
            }
            else
            {
                header("Location: index.php"); /* Redirect browser */ 
            }
        }


        /* Inserts refresh data to the page
        */
        function refresh($fileNameBase)
        {
            header("Content-type: text/html; charset=iso8859-2");
            header("Refresh: 10; url=benchmarksArchive_DOWNLOAD.php?fileNameBase=$fileNameBase");

            echo "The archive is being prepared, wait a moment please...<p>";

            echo "<a href=\"index.php\">Return to the main page</a>";
        }

        
        /* Returns the name of the file that indicates existence of the prepared archive
        */
        function getExistenceFileName()
        {
            return BenchConfig::$tmpDirectory . "/$_GET[fileNameBase]." . BenchConfig::$existenceFileExtension;
        }
        

        /* Returns the name of the archive file
        */
        function getArchiveFileName()
        {
            return BenchConfig::$tmpDirectory . "/$_GET[fileNameBase]." . BenchConfig::$archiveFileExtension;
        }
    
    }

    launchPage();

    # vim:enc=iso8859-2:fenc=iso8859-2:
?>
