Odt2xhtml.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. class Odt2xhtml {
  3. /**
  4. * Extract 'content.xml' from odt file (zip archive).
  5. */
  6. public function oo_unzip($file, $path = false) {
  7. if (!function_exists('zip_open')) {
  8. throw new Exception('NO ZIP FUNCTIONS DETECTED. Do you have the PECL ZIP extensions loaded?');
  9. }
  10. if (!is_file($file)) {
  11. throw new Exception('Can\'t find file: '.$file);
  12. }
  13. if ($zip = zip_open($file)) {
  14. $content = null;
  15. $img = array();
  16. while ($zip_entry = zip_read($zip)) {
  17. $filename = zip_entry_name($zip_entry);
  18. if (zip_entry_name($zip_entry) == 'content.xml' && zip_entry_open($zip, $zip_entry, "r")) {
  19. $content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  20. zip_entry_close($zip_entry);
  21. }
  22. if (substr($filename, 0, strlen('Pictures/')) == 'Pictures/' && false === strpos('Object', $filename) && zip_entry_open($zip, $zip_entry, "r")) {
  23. $img[$filename] = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  24. zip_entry_close($zip_entry);
  25. }
  26. }
  27. if (isset($content)) {
  28. if (is_array($img) && !empty($img)) {
  29. if (!is_dir($path.'Pictures')) {
  30. mkdir($path.'Pictures');
  31. }
  32. foreach ($img as $key => $val) {
  33. file_put_contents($path.$key, $val);
  34. }
  35. }
  36. return $content;
  37. }
  38. }
  39. }
  40. /**
  41. * Convert xml content using xsl template file.
  42. */
  43. public function oo_convert($xml, $xsl_file = 'template.xsl') {
  44. $xls = new DOMDocument;
  45. $xls->load($xsl_file);
  46. $xslt = new XSLTProcessor;
  47. $xslt->importStylesheet($xls);
  48. $x = preg_replace('#<draw:image xlink:href="Pictures/([a-z .A-Z_0-9]*)" (.*?)/>#es', "ODT2XHTML::makeImage('\\1')", $xml);
  49. $xml = new DOMDocument;
  50. $xml->loadXML($x);
  51. return html_entity_decode($xslt->transformToXML($xml));
  52. }
  53. public function makeImage($img) {
  54. return '&lt;img src="Pictures/'.$img.'" border="0" /&gt;';
  55. }
  56. }