17d101cc1SGerry Weißbach<?php 27d101cc1SGerry Weißbach 37d101cc1SGerry Weißbachif(!defined('DOKU_PLUGIN')) die('meh'); 47d101cc1SGerry Weißbachrequire_once(DOKU_PLUGIN.'siteexport/inc/pdfgenerator.php'); 57d101cc1SGerry Weißbach 67d101cc1SGerry Weißbachclass siteexport_zipfilewriter 77d101cc1SGerry Weißbach{ 87d101cc1SGerry Weißbach /** 97d101cc1SGerry Weißbach * further classes 107d101cc1SGerry Weißbach */ 117d101cc1SGerry Weißbach private $pdfGenerator = false; 127d101cc1SGerry Weißbach private $functions = null; 137d101cc1SGerry Weißbach 147d101cc1SGerry Weißbach public function siteexport_zipfilewriter( $functions = null ) 157d101cc1SGerry Weißbach { 167d101cc1SGerry Weißbach $this->functions = $functions; 177d101cc1SGerry Weißbach if ( class_exists( 'siteexport_pdfgenerator' ) ) 187d101cc1SGerry Weißbach { 197d101cc1SGerry Weißbach $this->pdfGenerator = new siteexport_pdfgenerator($functions); 207d101cc1SGerry Weißbach } 217d101cc1SGerry Weißbach } 227d101cc1SGerry Weißbach 237d101cc1SGerry Weißbach public function canDoPDF() 247d101cc1SGerry Weißbach { 257d101cc1SGerry Weißbach return $this->pdfGenerator !== false; 267d101cc1SGerry Weißbach } 277d101cc1SGerry Weißbach 287d101cc1SGerry Weißbach 297d101cc1SGerry Weißbach /** 307d101cc1SGerry Weißbach * Wrapper for fetching the Context or the TOC for Eclipse Documentation 317d101cc1SGerry Weißbach * This also puts the file into the zip package 327d101cc1SGerry Weißbach **/ 337d101cc1SGerry Weißbach public function __moveDataToZip($DATA, $FILENAME='toc.xml') { 347d101cc1SGerry Weißbach 357d101cc1SGerry Weißbach if ( empty($DATA) ) { return false; } 367d101cc1SGerry Weißbach 377d101cc1SGerry Weißbach $tmpFile = tempnam($this->functions->settings->tmpDir , 'siteexport__'); 387d101cc1SGerry Weißbach 397d101cc1SGerry Weißbach $fp = fopen( $tmpFile, "w"); 407d101cc1SGerry Weißbach if(!$fp) return false; 417d101cc1SGerry Weißbach 427d101cc1SGerry Weißbach fwrite($fp,$DATA); 437d101cc1SGerry Weißbach fclose($fp); 447d101cc1SGerry Weißbach 457d101cc1SGerry Weißbach // Add to zip 467d101cc1SGerry Weißbach $status = $this->__addFileToZip($tmpFile, $FILENAME); 477d101cc1SGerry Weißbach @unlink($tmpFile); 487d101cc1SGerry Weißbach 497d101cc1SGerry Weißbach return true; 507d101cc1SGerry Weißbach } 517d101cc1SGerry Weißbach 527d101cc1SGerry Weißbach /** 537d101cc1SGerry Weißbach * Adds a file to the zip file 547d101cc1SGerry Weißbach * @param $FILE file-name of the zip 557d101cc1SGerry Weißbach * @param $NAME name of the file that is being added 567d101cc1SGerry Weißbach * @param $ZIP name of the zip file to which we add 577d101cc1SGerry Weißbach */ 587d101cc1SGerry Weißbach function __addFileToZip($FILE, $NAME, $ZIP=null) { 597d101cc1SGerry Weißbach 607d101cc1SGerry Weißbach if ( $NAME[0] === "/" ) { 617d101cc1SGerry Weißbach $this->functions->debug->message("Weird, the NAME for the ZIP started with a '/'. This may result in wrong links!", null, 3); 627d101cc1SGerry Weißbach $NAME = substr($NAME, 1); 637d101cc1SGerry Weißbach } 647d101cc1SGerry Weißbach 657d101cc1SGerry Weißbach // check for mpdf 667d101cc1SGerry Weißbach if ( $this->canDoPDF() ) { 677d101cc1SGerry Weißbach $this->functions->debug->message("Trying to create PDF from File '$FILE' with name '$NAME' for ZIP '$ZIP'", null, 2); 687d101cc1SGerry Weißbach 697d101cc1SGerry Weißbach if ( $this->functions->debug->debugLevel() <= 1 ) { // 2011-01-12 Write HTML to ZIP for Debug purpose 707d101cc1SGerry Weißbach $this->__writeFileToZip($FILE, "_debug/$NAME.html", $ZIP); 717d101cc1SGerry Weißbach } 727d101cc1SGerry Weißbach 737d101cc1SGerry Weißbach if ( !$this->pdfGenerator->createPDFFromFile($FILE, $NAME) ) { 74*8da901a0SGerry Weißbach $this->functions->debug->runtimeException("Create PDF from File '$FILE' with name '$NAME' went wrong and is not being added!"); 75*8da901a0SGerry Weißbach return false; 767d101cc1SGerry Weißbach } 777d101cc1SGerry Weißbach } 787d101cc1SGerry Weißbach 797d101cc1SGerry Weißbach return $this->__writeFileToZip($FILE, $NAME, $ZIP); 807d101cc1SGerry Weißbach } 817d101cc1SGerry Weißbach 827d101cc1SGerry Weißbach /** 837d101cc1SGerry Weißbach * This really writes a file to a zip-file 847d101cc1SGerry Weißbach * @param $FILE file-name of the zip 857d101cc1SGerry Weißbach * @param $NAME name of the file that is being added 867d101cc1SGerry Weißbach * @param $ZIP name of the zip file to which we add 877d101cc1SGerry Weißbach */ 887d101cc1SGerry Weißbach private function __writeFileToZip($FILE, $NAME, $ZIP) { 897d101cc1SGerry Weißbach if ( empty( $ZIP ) ) $ZIP = $this->functions->settings->zipFile; 907d101cc1SGerry Weißbach 917d101cc1SGerry Weißbach if ( !class_exists('ZipArchive') ) { 927d101cc1SGerry Weißbach $this->functions->debug->runtimeException("PHP class 'ZipArchive' does not exist. Please make sure that you have the ziplib extension for PHP installed."); 937d101cc1SGerry Weißbach return false; 947d101cc1SGerry Weißbach } 957d101cc1SGerry Weißbach 967d101cc1SGerry Weißbach $zip = new ZipArchive; 977d101cc1SGerry Weißbach if ( !$zip ) { 987d101cc1SGerry 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."); 997d101cc1SGerry Weißbach return false; 1007d101cc1SGerry Weißbach } 1017d101cc1SGerry Weißbach 1027d101cc1SGerry Weißbach $code = $zip->open($ZIP, ZipArchive::CREATE); 1037d101cc1SGerry Weißbach if ($code === TRUE) { 1047d101cc1SGerry Weißbach 1057d101cc1SGerry Weißbach $this->functions->debug->message("Adding file '$NAME' to ZIP $ZIP", null, 2); 1067d101cc1SGerry Weißbach 1077d101cc1SGerry Weißbach $zip->addFile($FILE, $NAME); 1087d101cc1SGerry Weißbach $zip->close(); 1097d101cc1SGerry Weißbach 1107d101cc1SGerry Weißbach // If this has worked out, we may put this version into the cache ... ? 1117d101cc1SGerry Weißbach 1127d101cc1SGerry Weißbach // ALibi Touching - 2011-09-13 wird nicht gebraucht nach Umstellung 1137d101cc1SGerry Weißbach // io_saveFile(mediaFN($this->origZipFile), "alibi file"); 1147d101cc1SGerry Weißbach 1157d101cc1SGerry Weißbach return true; 1167d101cc1SGerry Weißbach } 1177d101cc1SGerry Weißbach 1187d101cc1SGerry Weißbach $this->functions->debug->runtimeException("Zip Error #$code"); 1197d101cc1SGerry Weißbach return false; 1207d101cc1SGerry Weißbach } 1217d101cc1SGerry Weißbach 1227d101cc1SGerry Weißbach /** 1237d101cc1SGerry Weißbach * check if a file exists allready 1247d101cc1SGerry Weißbach * @param $NAME name of the file in the zip 1257d101cc1SGerry Weißbach */ 1267d101cc1SGerry Weißbach function fileExistsInZip($NAME) 1277d101cc1SGerry Weißbach { 1287d101cc1SGerry Weißbach $zip = new ZipArchive; 1297d101cc1SGerry Weißbach $code = $zip->open($this->functions->settings->zipFile, ZipArchive::CREATE); 1307d101cc1SGerry Weißbach if ($code === TRUE) { 1317d101cc1SGerry Weißbach return !($zip->statName($NAME) === FALSE); 1327d101cc1SGerry Weißbach } 1337d101cc1SGerry Weißbach 1347d101cc1SGerry Weißbach return false; 1357d101cc1SGerry Weißbach } 1367d101cc1SGerry Weißbach 1377d101cc1SGerry Weißbach /** 1387d101cc1SGerry Weißbach * Checks if a valid cache file exists for the given request parameters 1397d101cc1SGerry Weißbach * @param $requestData 1407d101cc1SGerry Weißbach */ 1417d101cc1SGerry Weißbach function hasValidCacheFile($requestData, $depends=array()) 1427d101cc1SGerry Weißbach { 1437d101cc1SGerry Weißbach $this->functions->settings->hasValidCacheFile = false; // reset the cache settings 1447d101cc1SGerry Weißbach $HASH = $this->functions->requestParametersToCacheHash($requestData); 1457d101cc1SGerry Weißbach $this->functions->debug->message("HASH for CacheFile: ", $HASH, 2); 1467d101cc1SGerry Weißbach 1477d101cc1SGerry Weißbach $cacheFile = $this->functions->getCacheFileNameForPattern($HASH); 1487d101cc1SGerry Weißbach 1497d101cc1SGerry Weißbach $mtime = @filemtime($cacheFile); // 0 if not exists 1507d101cc1SGerry Weißbach 1517d101cc1SGerry Weißbach // Check if the file is expired - if so, just create a new one. 1527d101cc1SGerry Weißbach if ( $mtime == 0 || $mtime < time()-$this->functions->settings->getConf('cachetime') ) 1537d101cc1SGerry Weißbach { 1547d101cc1SGerry Weißbach @unlink($cacheFile); 1557d101cc1SGerry Weißbach @unlink($this->functions->settings->zipFile); 1567d101cc1SGerry Weißbach $this->functions->debug->message("New CacheFile because the file was over the cachetime: ", $cacheFile, 2); 1577d101cc1SGerry Weißbach return false; 1587d101cc1SGerry Weißbach } 1597d101cc1SGerry Weißbach 1607d101cc1SGerry Weißbach // Check for dependencies 1617d101cc1SGerry Weißbach if ( !empty($depends) ) 1627d101cc1SGerry Weißbach { 1637d101cc1SGerry Weißbach foreach ($depends as $site) { 1647d101cc1SGerry Weißbach 1657d101cc1SGerry Weißbach if ( !page_exists($site['id']) ) 1667d101cc1SGerry Weißbach { 1677d101cc1SGerry Weißbach continue; 1687d101cc1SGerry Weißbach } 1697d101cc1SGerry Weißbach 1707d101cc1SGerry Weißbach if ($mtime < @filemtime(wikiFN($site['id']))) { 1717d101cc1SGerry Weißbach @unlink($cacheFile); 1727d101cc1SGerry Weißbach @unlink($this->functions->settings->zipFile); 1737d101cc1SGerry Weißbach $this->functions->debug->message("New CacheFile, because a page changed: ", $cacheFile, 2); 1747d101cc1SGerry Weißbach return false; // cache older than files it depends on? 1757d101cc1SGerry Weißbach } 1767d101cc1SGerry Weißbach } 1777d101cc1SGerry Weißbach } 1787d101cc1SGerry Weißbach 1797d101cc1SGerry Weißbach $this->functions->debug->message("CacheFile exists: ", $cacheFile, 2); 1807d101cc1SGerry Weißbach return $this->functions->settings->hasValidCacheFile = true; 1817d101cc1SGerry Weißbach } 1827d101cc1SGerry Weißbach} 1837d101cc1SGerry Weißbach 1847d101cc1SGerry Weißbach?>