Browse Source

Fix corrupt files when inserting images - http://www.odtphp.com/forum/viewtopic.php?f=5&t=147

Yves Chedemois 13 years ago
parent
commit
b46863c465
3 changed files with 134 additions and 19 deletions
  1. 9 6
      odtphp/library/Segment.php
  2. 41 13
      odtphp/library/odf.php
  3. 84 0
      patch_for_odtphp_1.0.patch

+ 9 - 6
odtphp/library/Segment.php

@@ -38,7 +38,7 @@ class Segment implements IteratorAggregate, Countable
         $this->xml = (string) $xml;
 		$this->odf = $odf;
         $zipHandler = $this->odf->getConfig('ZIP_PROXY');
-        $this->file = new $zipHandler();	
+        $this->file = new $zipHandler();
         $this->_analyseChildren($this->xml);
     }
     /**
@@ -96,11 +96,14 @@ class Segment implements IteratorAggregate, Countable
         $this->xmlParsed = preg_replace($reg, '$1', $this->xmlParsed);
         $this->file->open($this->odf->getTmpfile());
         foreach ($this->images as $imageKey => $imageValue) {
-			if ($this->file->getFromName('Pictures/' . $imageValue) === false) {
-				$this->file->addFile($imageKey, 'Pictures/' . $imageValue);
-			}
+    			if ($this->file->getFromName('Pictures/' . $imageValue) === false) {
+    				$this->file->addFile($imageKey, 'Pictures/' . $imageValue);
+    			}
+    			// MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
+			    $this->odf->addImageToManifest($imageValue);
+			    // /MODIF
         }
-        $this->file->close();		
+        $this->file->close();
         return $this->xmlParsed;
     }
     /**
@@ -166,7 +169,7 @@ IMG;
         $this->images[$value] = $file;
         $this->setVars($key, $xml, false);
         return $this;
-    }	
+    }
     /**
      * Shortcut to retrieve a child
      *

+ 41 - 13
odtphp/library/odf.php

@@ -32,6 +32,10 @@ class Odf
     protected $images = array();
     protected $vars = array();
     protected $segments = array();
+    // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
+    // http://www.odtphp.com/forum/viewtopic.php?f=5&t=147
+    protected $manifestXml;
+    // /MODIF
     const PIXEL_TO_CM = 0.026458333;
     /**
      * Class constructor
@@ -60,9 +64,15 @@ class Odf
         if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
             throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed");
         }
+
+        // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images
+        if (($this->manifestXml = $this->file->getFromName('META-INF/manifest.xml')) === false) {
+          throw new OdfException("Something is wrong with META-INF/manifest.xml");
+        }
+        // /MODIF
 
-        $this->file->close();
-        
+        $this->file->close();
+
         $tmp = tempnam($this->config['PATH_TO_TMP'], md5(uniqid()));
         copy($filename, $tmp);
         $this->tmpfile = $tmp;
@@ -118,7 +128,7 @@ IMG;
      * Called automatically within the constructor
      *
      * @return void
-     */    
+     */
     private function _moveRowSegments()
     {
     	// Search all possible rows in the document
@@ -171,7 +181,7 @@ IMG;
     }
     /**
      * Display all the current template variables
-     * 
+     *
      * @return string
      */
     public function printVars()
@@ -190,7 +200,7 @@ IMG;
     }
     /**
      * Display loop segments declared with setSegment()
-     * 
+     *
      * @return string
      */
     public function printDeclaredSegments()
@@ -219,7 +229,7 @@ IMG;
     }
     /**
      * Save the odt file on the disk
-     * 
+     *
      * @param string $file name of the desired file
      * @throws OdfException
      * @return void
@@ -231,7 +241,7 @@ IMG;
             	throw new OdfException('Permission denied : can\'t create ' . $file);
         	}
             $this->_save();
-            copy($this->tmpfile, $file);     
+            copy($this->tmpfile, $file);
         } else {
             $this->_save();
         }
@@ -251,9 +261,27 @@ IMG;
         }
         foreach ($this->images as $imageKey => $imageValue) {
             $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
+            // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
+            $this->addImageToManifest($imageValue);
+            // /MODIF
+        }
+        // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
+        if (! $this->file->addFromString('./META-INF/manifest.xml', $this->manifestXml)) {
+            throw new OdfException('Error during file export: manifest.xml');
         }
+        // /MODIF
+
         $this->file->close(); // seems to bug on windows CLI sometimes
     }
+    // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
+    public function addImageToManifest($file)
+    {
+        $extension = explode('.', $file);
+        $replace = '<manifest:file-entry manifest:media-type="image/'.$extension[1].'" manifest:full-path="Pictures/'.$file.'"/></manifest:manifest>';
+
+        $this->manifestXml = str_replace('</manifest:manifest>', $replace, $this->manifestXml);
+    }
+    // /MODIF
     /**
      * Export the file as attached file by HTTP
      *
@@ -267,19 +295,19 @@ IMG;
         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.'"');
         readfile($this->tmpfile);
     }
     /**
-     * Returns a variable of configuration 
-     * 
+     * Returns a variable of configuration
+     *
      * @return string The requested variable of configuration
      */
     public function getConfig($configKey)
@@ -291,7 +319,7 @@ IMG;
     }
     /**
      * Returns the temporary working file
-     * 
+     *
      * @return string le chemin vers le fichier temporaire de travail
      */
     public function getTmpfile()
@@ -300,7 +328,7 @@ IMG;
     }
     /**
      * Delete the temporary file when the object is destroyed
-     */    
+     */
     public function __destruct() {
           if (file_exists($this->tmpfile)) {
         	unlink($this->tmpfile);

+ 84 - 0
patch_for_odtphp_1.0.patch

@@ -0,0 +1,84 @@
+diff --git odtphp/library/Segment.php odtphp/library/Segment.php
+index c0ff220..20f4760 100644
+--- odtphp/library/Segment.php
++++ odtphp/library/Segment.php
+@@ -96,11 +96,14 @@ class Segment implements IteratorAggregate, Countable
+         $this->xmlParsed = preg_replace($reg, '$1', $this->xmlParsed);
+         $this->file->open($this->odf->getTmpfile());
+         foreach ($this->images as $imageKey => $imageValue) {
+-			if ($this->file->getFromName('Pictures/' . $imageValue) === false) {
+-				$this->file->addFile($imageKey, 'Pictures/' . $imageValue);
+-			}
++    			if ($this->file->getFromName('Pictures/' . $imageValue) === false) {
++    				$this->file->addFile($imageKey, 'Pictures/' . $imageValue);
++    			}
++    			// MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
++			    $this->odf->addImageToManifest($imageValue);
++			    // /MODIF
+         }
+-        $this->file->close();		
++        $this->file->close();
+         return $this->xmlParsed;
+     }
+     /**
+diff --git odtphp/library/odf.php odtphp/library/odf.php
+index 36f894e..c7423e2 100644
+--- odtphp/library/odf.php
++++ odtphp/library/odf.php
+@@ -32,6 +32,10 @@ class Odf
+     protected $images = array();
+     protected $vars = array();
+     protected $segments = array();
++    // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
++    // http://www.odtphp.com/forum/viewtopic.php?f=5&t=147
++    protected $manifestXml;
++    // /MODIF
+     const PIXEL_TO_CM = 0.026458333;
+     /**
+      * Class constructor
+@@ -60,9 +64,15 @@ class Odf
+         if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
+             throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed");
+         }
++
++        // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images
++        if (($this->manifestXml = $this->file->getFromName('META-INF/manifest.xml')) === false) {
++          throw new OdfException("Something is wrong with META-INF/manifest.xml");
++        }
++        // /MODIF
+ 
+-        $this->file->close();
+-        
++        $this->file->close();
++
+         $tmp = tempnam($this->config['PATH_TO_TMP'], md5(uniqid()));
+         copy($filename, $tmp);
+         $this->tmpfile = $tmp;
+@@ -251,9 +261,27 @@ IMG;
+         }
+         foreach ($this->images as $imageKey => $imageValue) {
+             $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
++            // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
++            $this->addImageToManifest($imageValue);
++            // /MODIF
++        }
++        // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
++        if (! $this->file->addFromString('./META-INF/manifest.xml', $this->manifestXml)) {
++            throw new OdfException('Error during file export: manifest.xml');
+         }
++        // /MODIF
++
+         $this->file->close(); // seems to bug on windows CLI sometimes
+     }
++    // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
++    public function addImageToManifest($file)
++    {
++        $extension = explode('.', $file);
++        $replace = '<manifest:file-entry manifest:media-type="image/'.$extension[1].'" manifest:full-path="Pictures/'.$file.'"/></manifest:manifest>';
++
++        $this->manifestXml = str_replace('</manifest:manifest>', $replace, $this->manifestXml);
++    }
++    // /MODIF
+     /**
+      * Export the file as attached file by HTTP
+      *