xref: /plugin/siteexport/inc/filewriter.php (revision 7d101cc131696cb3a0de345d8044a69fb2ef70e9)
1*7d101cc1SGerry Weißbach<?php
2*7d101cc1SGerry Weißbach
3*7d101cc1SGerry Weißbachif(!defined('DOKU_PLUGIN')) die('meh');
4*7d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/pdfgenerator.php');
5*7d101cc1SGerry Weißbach
6*7d101cc1SGerry Weißbachclass siteexport_zipfilewriter
7*7d101cc1SGerry Weißbach{
8*7d101cc1SGerry Weißbach    /**
9*7d101cc1SGerry Weißbach     * further classes
10*7d101cc1SGerry Weißbach     */
11*7d101cc1SGerry Weißbach    private $pdfGenerator = false;
12*7d101cc1SGerry Weißbach    private $functions = null;
13*7d101cc1SGerry Weißbach
14*7d101cc1SGerry Weißbach    public function siteexport_zipfilewriter( $functions = null )
15*7d101cc1SGerry Weißbach    {
16*7d101cc1SGerry Weißbach        $this->functions = $functions;
17*7d101cc1SGerry Weißbach        if ( class_exists( 'siteexport_pdfgenerator' ) )
18*7d101cc1SGerry Weißbach        {
19*7d101cc1SGerry Weißbach            $this->pdfGenerator = new siteexport_pdfgenerator($functions);
20*7d101cc1SGerry Weißbach        }
21*7d101cc1SGerry Weißbach    }
22*7d101cc1SGerry Weißbach
23*7d101cc1SGerry Weißbach    public function canDoPDF()
24*7d101cc1SGerry Weißbach    {
25*7d101cc1SGerry Weißbach        return $this->pdfGenerator !== false;
26*7d101cc1SGerry Weißbach    }
27*7d101cc1SGerry Weißbach
28*7d101cc1SGerry Weißbach
29*7d101cc1SGerry Weißbach    /**
30*7d101cc1SGerry Weißbach     * Wrapper for fetching the Context or the TOC for Eclipse Documentation
31*7d101cc1SGerry Weißbach     * This also puts the file into the zip package
32*7d101cc1SGerry Weißbach     **/
33*7d101cc1SGerry Weißbach    public function __moveDataToZip($DATA, $FILENAME='toc.xml') {
34*7d101cc1SGerry Weißbach
35*7d101cc1SGerry Weißbach        if ( empty($DATA) ) { return false; }
36*7d101cc1SGerry Weißbach
37*7d101cc1SGerry Weißbach        $tmpFile = tempnam($this->functions->settings->tmpDir , 'siteexport__');
38*7d101cc1SGerry Weißbach
39*7d101cc1SGerry Weißbach        $fp = fopen( $tmpFile, "w");
40*7d101cc1SGerry Weißbach        if(!$fp) return false;
41*7d101cc1SGerry Weißbach
42*7d101cc1SGerry Weißbach        fwrite($fp,$DATA);
43*7d101cc1SGerry Weißbach        fclose($fp);
44*7d101cc1SGerry Weißbach
45*7d101cc1SGerry Weißbach        // Add to zip
46*7d101cc1SGerry Weißbach        $status = $this->__addFileToZip($tmpFile, $FILENAME);
47*7d101cc1SGerry Weißbach        @unlink($tmpFile);
48*7d101cc1SGerry Weißbach
49*7d101cc1SGerry Weißbach        return true;
50*7d101cc1SGerry Weißbach    }
51*7d101cc1SGerry Weißbach
52*7d101cc1SGerry Weißbach    /**
53*7d101cc1SGerry Weißbach     * Adds a file to the zip file
54*7d101cc1SGerry Weißbach     * @param $FILE file-name of the zip
55*7d101cc1SGerry Weißbach     * @param $NAME name of the file that is being added
56*7d101cc1SGerry Weißbach     * @param $ZIP name of the zip file to which we add
57*7d101cc1SGerry Weißbach     */
58*7d101cc1SGerry Weißbach    function __addFileToZip($FILE, $NAME, $ZIP=null) {
59*7d101cc1SGerry Weißbach
60*7d101cc1SGerry Weißbach        if ( $NAME[0] === "/" ) {
61*7d101cc1SGerry Weißbach            $this->functions->debug->message("Weird, the NAME for the ZIP started with a '/'. This may result in wrong links!", null, 3);
62*7d101cc1SGerry Weißbach            $NAME = substr($NAME, 1);
63*7d101cc1SGerry Weißbach        }
64*7d101cc1SGerry Weißbach
65*7d101cc1SGerry Weißbach        // check for mpdf
66*7d101cc1SGerry Weißbach        if ( $this->canDoPDF() ) {
67*7d101cc1SGerry Weißbach            $this->functions->debug->message("Trying to create PDF from File '$FILE' with name '$NAME' for ZIP '$ZIP'", null, 2);
68*7d101cc1SGerry Weißbach
69*7d101cc1SGerry Weißbach            if ( $this->functions->debug->debugLevel() <= 1 ) { // 2011-01-12 Write HTML to ZIP for Debug purpose
70*7d101cc1SGerry Weißbach                $this->__writeFileToZip($FILE, "_debug/$NAME.html", $ZIP);
71*7d101cc1SGerry Weißbach            }
72*7d101cc1SGerry Weißbach
73*7d101cc1SGerry Weißbach            if ( !$this->pdfGenerator->createPDFFromFile($FILE, $NAME) ) {
74*7d101cc1SGerry Weißbach                $this->functions->debug->message("Create PDF from File '$FILE' with name '$NAME' went wrong and is not being added!", null, 4);
75*7d101cc1SGerry Weißbach                return;
76*7d101cc1SGerry Weißbach            }
77*7d101cc1SGerry Weißbach        }
78*7d101cc1SGerry Weißbach
79*7d101cc1SGerry Weißbach        return $this->__writeFileToZip($FILE, $NAME, $ZIP);
80*7d101cc1SGerry Weißbach    }
81*7d101cc1SGerry Weißbach
82*7d101cc1SGerry Weißbach    /**
83*7d101cc1SGerry Weißbach     * This really writes a file to a zip-file
84*7d101cc1SGerry Weißbach     * @param $FILE file-name of the zip
85*7d101cc1SGerry Weißbach     * @param $NAME name of the file that is being added
86*7d101cc1SGerry Weißbach     * @param $ZIP name of the zip file to which we add
87*7d101cc1SGerry Weißbach     */
88*7d101cc1SGerry Weißbach    private function __writeFileToZip($FILE, $NAME, $ZIP) {
89*7d101cc1SGerry Weißbach        if ( empty( $ZIP ) ) $ZIP = $this->functions->settings->zipFile;
90*7d101cc1SGerry Weißbach
91*7d101cc1SGerry Weißbach        if ( !class_exists('ZipArchive') ) {
92*7d101cc1SGerry Weißbach            $this->functions->debug->runtimeException("PHP class 'ZipArchive' does not exist. Please make sure that you have the ziplib extension for PHP installed.");
93*7d101cc1SGerry Weißbach            return false;
94*7d101cc1SGerry Weißbach        }
95*7d101cc1SGerry Weißbach
96*7d101cc1SGerry Weißbach        $zip = new ZipArchive;
97*7d101cc1SGerry Weißbach        if ( !$zip ) {
98*7d101cc1SGerry Weißbach            $this->functions->debug->runtimeException("Can't create new instance of 'ZipArchive'. Please make sure that you have the ziplib extension for PHP installed.");
99*7d101cc1SGerry Weißbach            return false;
100*7d101cc1SGerry Weißbach        }
101*7d101cc1SGerry Weißbach
102*7d101cc1SGerry Weißbach        $code = $zip->open($ZIP, ZipArchive::CREATE);
103*7d101cc1SGerry Weißbach        if ($code === TRUE) {
104*7d101cc1SGerry Weißbach
105*7d101cc1SGerry Weißbach            $this->functions->debug->message("Adding file '$NAME' to ZIP $ZIP", null, 2);
106*7d101cc1SGerry Weißbach
107*7d101cc1SGerry Weißbach            $zip->addFile($FILE, $NAME);
108*7d101cc1SGerry Weißbach            $zip->close();
109*7d101cc1SGerry Weißbach
110*7d101cc1SGerry Weißbach            // If this has worked out, we may put this version into the cache ... ?
111*7d101cc1SGerry Weißbach
112*7d101cc1SGerry Weißbach            // ALibi Touching - 2011-09-13 wird nicht gebraucht nach Umstellung
113*7d101cc1SGerry Weißbach            // io_saveFile(mediaFN($this->origZipFile), "alibi file");
114*7d101cc1SGerry Weißbach
115*7d101cc1SGerry Weißbach            return true;
116*7d101cc1SGerry Weißbach        }
117*7d101cc1SGerry Weißbach
118*7d101cc1SGerry Weißbach        $this->functions->debug->runtimeException("Zip Error #$code");
119*7d101cc1SGerry Weißbach        return false;
120*7d101cc1SGerry Weißbach    }
121*7d101cc1SGerry Weißbach
122*7d101cc1SGerry Weißbach    /**
123*7d101cc1SGerry Weißbach     * check if a file exists allready
124*7d101cc1SGerry Weißbach     * @param $NAME name of the file in the zip
125*7d101cc1SGerry Weißbach     */
126*7d101cc1SGerry Weißbach    function fileExistsInZip($NAME)
127*7d101cc1SGerry Weißbach    {
128*7d101cc1SGerry Weißbach        $zip = new ZipArchive;
129*7d101cc1SGerry Weißbach        $code = $zip->open($this->functions->settings->zipFile, ZipArchive::CREATE);
130*7d101cc1SGerry Weißbach        if ($code === TRUE) {
131*7d101cc1SGerry Weißbach            return !($zip->statName($NAME) === FALSE);
132*7d101cc1SGerry Weißbach        }
133*7d101cc1SGerry Weißbach
134*7d101cc1SGerry Weißbach        return false;
135*7d101cc1SGerry Weißbach    }
136*7d101cc1SGerry Weißbach
137*7d101cc1SGerry Weißbach    /**
138*7d101cc1SGerry Weißbach     * Checks if a valid cache file exists for the given request parameters
139*7d101cc1SGerry Weißbach     * @param $requestData
140*7d101cc1SGerry Weißbach     */
141*7d101cc1SGerry Weißbach    function hasValidCacheFile($requestData, $depends=array())
142*7d101cc1SGerry Weißbach    {
143*7d101cc1SGerry Weißbach        $this->functions->settings->hasValidCacheFile = false; // reset the cache settings
144*7d101cc1SGerry Weißbach        $HASH = $this->functions->requestParametersToCacheHash($requestData);
145*7d101cc1SGerry Weißbach        $this->functions->debug->message("HASH for CacheFile: ", $HASH, 2);
146*7d101cc1SGerry Weißbach
147*7d101cc1SGerry Weißbach        $cacheFile = $this->functions->getCacheFileNameForPattern($HASH);
148*7d101cc1SGerry Weißbach
149*7d101cc1SGerry Weißbach        $mtime = @filemtime($cacheFile); // 0 if not exists
150*7d101cc1SGerry Weißbach
151*7d101cc1SGerry Weißbach        // Check if the file is expired - if so, just create a new one.
152*7d101cc1SGerry Weißbach        if ( $mtime == 0 || $mtime < time()-$this->functions->settings->getConf('cachetime') )
153*7d101cc1SGerry Weißbach        {
154*7d101cc1SGerry Weißbach            @unlink($cacheFile);
155*7d101cc1SGerry Weißbach            @unlink($this->functions->settings->zipFile);
156*7d101cc1SGerry Weißbach            $this->functions->debug->message("New CacheFile because the file was over the cachetime: ", $cacheFile, 2);
157*7d101cc1SGerry Weißbach            return false;
158*7d101cc1SGerry Weißbach        }
159*7d101cc1SGerry Weißbach
160*7d101cc1SGerry Weißbach        // Check for dependencies
161*7d101cc1SGerry Weißbach        if ( !empty($depends) )
162*7d101cc1SGerry Weißbach        {
163*7d101cc1SGerry Weißbach            foreach ($depends as $site) {
164*7d101cc1SGerry Weißbach
165*7d101cc1SGerry Weißbach                if ( !page_exists($site['id']) )
166*7d101cc1SGerry Weißbach                {
167*7d101cc1SGerry Weißbach                    continue;
168*7d101cc1SGerry Weißbach                }
169*7d101cc1SGerry Weißbach
170*7d101cc1SGerry Weißbach                if ($mtime < @filemtime(wikiFN($site['id']))) {
171*7d101cc1SGerry Weißbach                    @unlink($cacheFile);
172*7d101cc1SGerry Weißbach                    @unlink($this->functions->settings->zipFile);
173*7d101cc1SGerry Weißbach                    $this->functions->debug->message("New CacheFile, because a page changed: ", $cacheFile, 2);
174*7d101cc1SGerry Weißbach                    return false;         // cache older than files it depends on?
175*7d101cc1SGerry Weißbach                }
176*7d101cc1SGerry Weißbach            }
177*7d101cc1SGerry Weißbach        }
178*7d101cc1SGerry Weißbach
179*7d101cc1SGerry Weißbach        $this->functions->debug->message("CacheFile exists: ", $cacheFile, 2);
180*7d101cc1SGerry Weißbach        return $this->functions->settings->hasValidCacheFile = true;
181*7d101cc1SGerry Weißbach    }
182*7d101cc1SGerry Weißbach}
183*7d101cc1SGerry Weißbach
184*7d101cc1SGerry Weißbach?>