diff --git a/htdocs/includes/odtphp/Segment.php b/htdocs/includes/odtphp/Segment.php index 7a6e15ee48b..9bcfa0f65d1 100644 --- a/htdocs/includes/odtphp/Segment.php +++ b/htdocs/includes/odtphp/Segment.php @@ -136,9 +136,15 @@ class Segment implements IteratorAggregate, Countable if (strpos($this->xml, $this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')) === false) { throw new SegmentException("var $key not found in {$this->getName()}"); } + + $value=$this->odf->htmlToUTFAndPreOdf($value); + $value = $encode ? htmlspecialchars($value) : $value; $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value; - $this->vars[$this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')] = str_replace("\n", "", $value); + + $value=$this->odf->preOdfToOdf($value); + + $this->vars[$this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')] = $value; return $this; } /** diff --git a/htdocs/includes/odtphp/odf.php b/htdocs/includes/odtphp/odf.php index d2cb8e0b6e1..623fa083ad1 100644 --- a/htdocs/includes/odtphp/odf.php +++ b/htdocs/includes/odtphp/odf.php @@ -122,12 +122,68 @@ class Odf throw new OdfException("var $key not found in the document"); //} } + + $value=$this->htmlToUTFAndPreOdf($value); + $value = $encode ? htmlspecialchars($value) : $value; $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value; - $this->vars[$tag] = str_replace("\n", "", $value); + + $value=$this->preOdfToOdf($value); + + $this->vars[$tag] = $value; return $this; } + + /** + * Function to convert a HTML string into an ODT string + * + * @param string $value String to convert + */ + public function htmlToUTFAndPreOdf($value) + { + // We decode into utf8, entities + $value=dol_html_entity_decode($value, ENT_QUOTES); + + // We convert html tags + $ishtml=dol_textishtml($value); + if ($ishtml) + { + // If string is "MYPODUCT - Desc bold with é accent
\n
\nUn texto en español ?" + // Result after clean must be "MYPODUCT - Desc bold with é accent\n\nUn texto en español ?" + + // We want to ignore \n and we want all
to be \n + $value=preg_replace('/(\r\n|\r|\n)/i','',$value); + $value=preg_replace('/
/i',"\n",$value); + $value=preg_replace('/\/]*>/i',"\n",$value); + $value=preg_replace('/\/]*\/>/i',"\n",$value); + + //$value=preg_replace('//','__lt__text:p text:style-name=__quot__bold__quot____gt__',$value); + //$value=preg_replace('/<\/strong>/','__lt__/text:p__gt__',$value); + + $value=dol_string_nohtmltag($value, 0); + } + + return $value; + } + + + /** + * Function to convert a HTML string into an ODT string + * + * @param string $value String to convert + */ + public function preOdfToOdf($value) + { + $value = str_replace("\n", "", $value); + + //$value = str_replace("__lt__", "<", $value); + //$value = str_replace("__gt__", ">", $value); + //$value = str_replace("__quot__", '"', $value); + + return $value; + } + /** * Evaluating php codes inside the ODT and output the buffer (print, echo) inplace of the code * @@ -422,25 +478,25 @@ IMG; 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=str_replace('.odt', '', $name); if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT)) { $command = $conf->global->MAIN_DOL_SCRIPTS_ROOT.'/scripts/odt2pdf/odt2pdf.sh '.$name; }else { $command = '../../scripts/odt2pdf/odt2pdf.sh '.$name; } - - + + //$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) { @@ -482,7 +538,7 @@ IMG; } 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); }