Skip to content

Commit

Permalink
FIX a case of corrupted ODT by Word that insert <text:s> when it should
Browse files Browse the repository at this point in the history
not.
  • Loading branch information
eldy committed Jun 22, 2016
1 parent 386b19d commit f514400
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions htdocs/includes/odtphp/odf.php
Expand Up @@ -39,6 +39,7 @@ class Odf
public $userdefined=array();

const PIXEL_TO_CM = 0.026458333;

/**
* Class constructor
*
Expand Down Expand Up @@ -107,6 +108,7 @@ public function __construct($filename, $config = array())

copy($filename, $this->tmpfile);

// Clean file to have tags for line corrected
$this->_moveRowSegments();
}

Expand Down Expand Up @@ -201,7 +203,9 @@ public function preOdfToOdf($value)
public function phpEval()
{
preg_match_all('/[\{\<]\?(php)?\s+(?P<content>.+)\?[\}\>]/iU',$this->contentXml, $matches); // detecting all {?php code ?} or <?php code ? >
for ($i=0;$i < count($matches['content']);$i++) {
$nbfound=count($matches['content']);
for ($i=0; $i < $nbfound; $i++)
{
try {
$ob_output = ''; // flush the output for each code. This var will be filled in by the eval($code) and output buffering : any print or echo or output will be redirected into this variable
$code = $matches['content'][$i];
Expand Down Expand Up @@ -247,13 +251,18 @@ public function setImage($key, $value)

/**
* Move segment tags for lines of tables
* Called automatically within the constructor
* This function is called automatically within the constructor, so this->contentXml is clean before any other thing
*
* @return void
*/
private function _moveRowSegments()
{
// Search all possible rows in the document
// Replace BEGIN<text:s/>xxx into BEGIN xxx
$this->contentXml = preg_replace('/\[!--\sBEGIN<text:s[^>]>(row.[\S]*)\s--\]/sm', '[!-- BEGIN \\1 --]', $this->contentXml);
// Replace END<text:s/>xxx into END xxx
$this->contentXml = preg_replace('/\[!--\sEND<text:s[^>]>(row.[\S]*)\s--\]/sm', '[!-- END \\1 --]', $this->contentXml);

// Search all possible rows in the document
$reg1 = "#<table:table-row[^>]*>(.*)</table:table-row>#smU";
preg_match_all($reg1, $this->contentXml, $matches);
for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
Expand Down Expand Up @@ -333,7 +342,7 @@ private function _parse($type='content')
/**
* Add the merged segment to the document
*
* @param Segment $segment
* @param Segment $segment Segment
* @throws OdfException
* @return odf
*/
Expand Down Expand Up @@ -383,7 +392,7 @@ public function printDeclaredSegments()
/**
* Declare a segment in order to use it in a loop
*
* @param string $segment
* @param string $segment Segment
* @throws OdfException
* @return Segment
*/
Expand All @@ -395,7 +404,7 @@ public function setSegment($segment)
// $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)<text:p\s.*>\[!--\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");
throw new OdfException("'".$segment."' segment not found in the document. The tag [!-- BEGIN xxx --] or [!-- END xxx --] is not present into content file.");
}
$this->segments[$segment] = new Segment($segment, $m[1], $this);
return $this->segments[$segment];
Expand Down Expand Up @@ -674,9 +683,9 @@ private function _rrmdir($dir)

/**
* return the value present on odt in [valuename][/valuename]
* @param string $value name balise in the template
* @return string the value inside the balise
*
*
* @param string $value name balise in the template
* @return string the value inside the balise
*/
public function getvalue($valuename)
{
Expand All @@ -688,4 +697,3 @@ public function getvalue($valuename)

}

?>

0 comments on commit f514400

Please sign in to comment.