]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
$reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU';
$this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml);
return $this;
}
/**
* Display all the current template variables
*
* @return string
*/
public function printVars()
{
return print_r('' . print_r($this->vars, true) . '
', true);
}
/**
* Display the XML content of the file from odt document
* as it is at the moment
*
* @return string
*/
public function __toString()
{
return $this->contentXml;
}
/**
* Display loop segments declared with setSegment()
*
* @return string
*/
public function printDeclaredSegments()
{
return '' . print_r(implode(' ', array_keys($this->segments)), true) . '
';
}
/**
* Declare a segment in order to use it in a loop
*
* @param string $segment
* @throws OdfException
* @return Segment
*/
public function setSegment($segment)
{
if (array_key_exists($segment, $this->segments)) {
return $this->segments[$segment];
}
// $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)\[!--\sEND\s$segment\s--\]#sm";
$reg = "#\[!--\sBEGIN\s$segment\s--\](.*)\[!--\sEND\s$segment\s--\]#sm";
if (preg_match($reg, html_entity_decode($this->contentXml), $m) == 0) {
throw new OdfException("'$segment' segment not found in the document");
}
$this->segments[$segment] = new Segment($segment, $m[1], $this);
return $this->segments[$segment];
}
/**
* Save the odt file on the disk
*
* @param string $file name of the desired file
* @throws OdfException
* @return void
*/
public function saveToDisk($file = null)
{
if ($file !== null && is_string($file)) {
if (file_exists($file) && !(is_file($file) && is_writable($file))) {
throw new OdfException('Permission denied : can\'t create ' . $file);
}
$this->_save();
copy($this->tmpfile, $file);
} else {
$this->_save();
}
}
/**
* Write output file onto disk
*
* @throws OdfException
* @return void
*/
private function _save()
{
$res=$this->file->open($this->tmpfile); // tmpfile is odt template
$this->_parse('content');
$this->_parse('styles');
if (! $this->file->addFromString('content.xml', $this->contentXml)) {
throw new OdfException('Error during file export addFromString');
}
if (! $this->file->addFromString('styles.xml', $this->stylesXml)) {
throw new OdfException('Error during file export addFromString');
}
foreach ($this->images as $imageKey => $imageValue) {
// Add the image inside the ODT document
$this->file->addFile($imageKey, 'Pictures/' . $imageValue);
// Add the image to the Manifest (which maintains a list of images, necessary to avoid "Corrupt ODT file. Repair?" when opening the file with LibreOffice)
$this->addImageToManifest($imageValue);
}
if (! $this->file->addFromString('./META-INF/manifest.xml', $this->manifestXml)) {
throw new OdfException('Error during file export: manifest.xml');
}
$this->file->close();
}
/**
* Update Manifest file according to added image files
*
* @param string $file Image file to add into manifest content
*/
public function addImageToManifest($file)
{
// Get the file extension
$ext = substr(strrchr($val, '.'), 1);
// Create the correct image XML entry to add to the manifest (this is necessary because ODT format requires that we keep a list of the images in the manifest.xml)
$add = ' '."\n";
// Append the image to the manifest
$this->manifestXml = str_replace('', $add.'', $this->manifestXml); // we replace the manifest closing tag by the image XML entry + manifest closing tag (this results in appending the data, we do not overwrite anything)
}
/**
* Export the file as attached file by HTTP
*
* @param string $name (optional)
* @throws OdfException
* @return void
*/
public function exportAsAttachedFile($name="")
{
$this->_save();
if (headers_sent($filename, $linenum)) {
throw new OdfException("headers already sent ($filename at $linenum)");
}
if( $name == "" )
{
$name = md5(uniqid()) . ".odt";
}
header('Content-type: application/vnd.oasis.opendocument.text');
header('Content-Disposition: attachment; filename="'.$name.'"');
header('Content-Length: '.filesize($this->tmpfile));
readfile($this->tmpfile);
}
/**
* Convert the ODT file to PDF and export the file as attached file by HTTP
* Note: you need to have JODConverter and OpenOffice or LibreOffice installed and executable on the same system as where this php script will be executed. You also need to chmod +x odt2pdf.sh
*
* @param string $name (optional)
* @throws OdfException
* @return void
*/
public function exportAsAttachedPDF($name="")
{
global $conf;
if( $name == "" ) $name = md5(uniqid());
dol_syslog(get_class($this).'::exportAsAttachedPDF $name='.$name, LOG_DEBUG);
$this->saveToDisk($name);
$execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2
$name=preg_replace('/\.odt/i', '', $name);
if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT))
{
$command = $conf->global->MAIN_DOL_SCRIPTS_ROOT.'/scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
}
else
{
$command = '../../scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($name).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
}
//$dirname=dirname($name);
//$command = DOL_DOCUMENT_ROOT.'/includes/odtphp/odt2pdf.sh '.$name.' '.$dirname;
dol_syslog(get_class($this).'::exportAsAttachedPDF $execmethod='.$execmethod.' Run command='.$command,LOG_DEBUG);
if ($execmethod == 1)
{
exec($command, $output_arr, $retval);
}
if ($execmethod == 2)
{
$ok=0;
$handle = fopen($outputfile, 'w');
if ($handle)
{
dol_syslog(get_class($this)."Run command ".$command,LOG_DEBUG);
$handlein = popen($command, 'r');
while (!feof($handlein))
{
$read = fgets($handlein);
fwrite($handle,$read);
$output_arr[]=$read;
}
pclose($handlein);
fclose($handle);
}
if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
}
if($retval == 0)
{
dol_syslog(get_class($this).'::exportAsAttachedPDF $ret_val='.$retval, LOG_DEBUG);
if (headers_sent($filename, $linenum)) {
throw new OdfException("headers already sent ($filename at $linenum)");
}
if (!empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$name.'.pdf"');
readfile("$name.pdf");
}
if (!empty($conf->global->MAIN_ODT_AS_PDF_DEL_SOURCE))
unlink("$name.odt");
} else {
dol_syslog(get_class($this).'::exportAsAttachedPDF $ret_val='.$retval, LOG_DEBUG);
dol_syslog(get_class($this).'::exportAsAttachedPDF $output_arr='.var_export($output_arr,true), LOG_DEBUG);
if ($retval==126) {
throw new OdfException('Permission execute convert script : ' . $command);
}
else {
foreach($output_arr as $line) {
$errors.= $line."
";
}
throw new OdfException('ODT to PDF convert fail : ' . $errors);
}
}
}
/**
* Returns a variable of configuration
*
* @return string The requested variable of configuration
*/
public function getConfig($configKey)
{
if (array_key_exists($configKey, $this->config)) {
return $this->config[$configKey];
}
return false;
}
/**
* Returns the temporary working file
*
* @return string le chemin vers le fichier temporaire de travail
*/
public function getTmpfile()
{
return $this->tmpfile;
}
/**
* Delete the temporary file when the object is destroyed
*/
public function __destruct()
{
if (file_exists($this->tmpfile)) {
unlink($this->tmpfile);
}
if (file_exists($this->tmpdir)) {
$this->_rrmdir($this->tmpdir);
rmdir($this->tmpdir);
}
}
/**
* Empty the temporary working directory recursively
* @param $dir the temporary working directory
* @return void
*/
private function _rrmdir($dir)
{
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
if (is_dir($dir . '/' . $file)) {
$this->_rrmdir($dir . '/' . $file);
rmdir($dir . '/' . $file);
} else {
unlink($dir . '/' . $file);
}
}
}
closedir($handle);
}
}
/**
* return the value present on odt in [valuename][/valuename]
* @param string $value name balise in the template
* @return string the value inside the balise
*
*/
public function getvalue($valuename)
{
$searchreg="/\\[".$valuename."\\](.*)\\[\\/".$valuename."\\]/";
preg_match($searchreg, $this->contentXml, $matches);
$this->contentXml = preg_replace($searchreg, "", $this->contentXml);
return $matches[1];
}
}
?>