<?php
/**
* @package PhpOpenDocumentReports
* @author Eric Letard
* @copyright GPL License 2009
* @license http://www.gnu.org/copyleft/gpl.html GPL License
* @version 0.3
*/
/**
* Takes in charge the extraction of content.xml and after fetching, inclusion of the new content.xml and if necessary additionnal files
* Based on ODTPHP project.
*/
class PODRODT
{
protected static $__additionnalFiles=array();
public static function addFile($file_path,$localfile)
{
self::$__additionnalFiles[$file_path]=$localfile;
}
public static function load(PODRFileString $data)
{
/*It must be the first filter, to have the good filename*/
if(PODREngine::getExtension($data->getContext()->getConfig("InputFileName", ""))=="odt")
{
$file = new ZipArchive();
if($file->open($data->getFileName()) !== true) {
throw new Exception("Error while Opening the file '".$filename."' - Check your odt file");
}
if (($stream = $file->getFromName('content.xml')) === false) {
throw new Exception("Nothing to parse - check that the content.xml file is correctly formed");
}
$data->setContent($stream);
$file->close();
}
return $data;
}
public static function save(PODRFileString $data)
{
$inputfile=$data->getContext()->getConfig("InputFileName", "");
if(PODREngine::getExtension($inputfile)=="odt")
{
$outputodt=tempnam($data->getContext()->getConfig("TempDir", null), "outodt");
copy($inputfile,$outputodt);
$file = new ZipArchive();
$file->open($outputodt, ZIPARCHIVE::CREATE);
if (!$file->addFromString("content.xml",$data->getContent())) {
throw new Exception('Error during file export');
}
foreach(self::$__additionnalFiles as $fileKey => $fileValue) {
$file->addFile($fileKey, $fileValue);
}
$file->close();
$data->setFilename($outputodt);
}
return $data;
}
}
?>