<?php
/**
 * DokuWiki Plugin gtime (Renderer Component)
 *
 * @license Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
 * @author  Michael Kirchner <michael.kirchner@uni-due.de>
 */
/*
 * Copyright (C) Michael Kirchner <michael.kirchner@uni-due.de>
 *
 * This file is part of the GuardTime dokuwiki plugin.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 


// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
require_once DOKU_INC.'inc/parser/renderer.php';
require_once(DOKU_INC.'inc/pluginutils.php'); 

class renderer_plugin_gtime extends Doku_Renderer {

    /**
     * The format this renderer produces
     */
    public function getFormat(){
        return 'gtime';
    }


    /**
     * This function exports page and timestamp and makes it available as ZIP download.
     *
     * @return void
     */
    function document_start() {
        global $ID;
        global $INFO;
        global $conf;
        global $ACT;
        global $lang;

        $this->setupLocale();

        // Hoping to get at the caching problem
	    $this->info['cache'] = false; 
        p_set_metadata($ID,  array('cache' => 'expire'), false, false);



        $pagefile = $INFO['filepath'];
        $filerev = basename($INFO['filepath']);

$GT_README= <<<EOT
{$this->getLang('README')}
gtime -xv -f {$filerev} -i {$filerev}.gtts

EOT;

    
    
        $filename = "GuardTime_Timestamp_Attic_{$filerev}.zip";
        $filetemp = tempnam(DOKU_INC.'data/tmp/', "GUARDTIME");
    
        $zip = new ZipArchive();
    
        $result = $zip->open($filetemp, ZipArchive::CREATE);
    
        if ($result !== true) {
            die("Unable to create zip file: {$filetemp}, error code: {$result}");
        }
    
        $zip->addFile($pagefile,$filerev);
        $zip->addFile($pagefile.".gtts",$filerev.".gtts");
        $zip->addfromString("README.txt",$GT_README);
        $zip->close();
    
        $fp = fopen($filetemp, 'r');
    
        if (!$fp) {
            die("Unable to open ZIP file: {$filetemp}");
        }
    
        header("Content-Type: archive/zip");
        header("Content-Disposition: attachment; filename={$filename}");

        $headers = array(
            'Content-Type' => 'archive/zip',
            'Content-Disposition' => "attachment; filename={$filename}.zip"
        );
        p_set_metadata($ID,array('format' => array('zip' => $headers) ));
    
        fpassthru($fp);
        fclose($fp);
        unlink($filetemp);
    
    } 
}

// vim:ts=4:sw=4:et:
