simplecheck.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Simple Check file : checking your system about php-zip module
  4. * The output file checkresult.odt must be readable by an oasis document application as openoffice.org
  5. * You need PHP 5.2 at least
  6. * You need Zip Xml Extensions (or PclZip library instead if Zip extension)
  7. * Encoding : ISO-8859-1
  8. *
  9. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  10. * @license http://www.gnu.org/copyleft/gpl.html GPL License
  11. * SVN Revision - $Rev: 36 $
  12. * Last commit by $Author: neveldo $
  13. * Date - $Date: 2009-06-04 15:45:12 +0200 (jeu., 04 juin 2009) $
  14. * SVN Revision - $Rev: 36 $
  15. * Id : $Id: simplecheck.php 36 2009-06-04 13:45:12Z neveldo $
  16. */
  17. // instantiate the Exception class
  18. class OdfException extends Exception{}
  19. // checking php-zip module
  20. if ( !class_exists('ZipArchive') )
  21. {
  22. throw new OdfException('ZipArchive extension not loaded - check your php settings, PHP5.2 minimum with php-zip extension is required');
  23. }
  24. // checking php-xml module
  25. if ( !class_exists('DOMDocument') )
  26. {
  27. throw new OdfException('DOMDocument extension not loaded - check your php settings, PHP5.2 minimum with php-xml extension is required');
  28. }
  29. // start code ...
  30. $filename = "simplecheck.odt"; // must exist in same path as this script
  31. // load the oasis document via php-lib library
  32. $file = new ZipArchive();
  33. if ( $file->open($filename) !== true )
  34. {
  35. throw new OdfException("Error while Opening the file '$filename' - Check your odt file");
  36. }
  37. // read content.xml from the oasis document
  38. if (($contentXml = $file->getFromName('content.xml')) === false)
  39. {
  40. throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed");
  41. }
  42. // close the original oasis document
  43. $file->close();
  44. // for futur use, with load content.xml via DOMDocument library :
  45. $odt_content = new DOMDocument('1.0', 'utf-8');
  46. if ($odt_content->loadXML( $contentXml ) == FALSE)
  47. {
  48. throw new OdfException('Unable to load content.xml by DOMDocument library ', __METHOD__);
  49. }
  50. // here, we dont use the temp function but local temporary file
  51. $tmpfile = md5(uniqid()).'.odt';
  52. if( !@copy($filename, $tmpfile) );
  53. {
  54. // we do not test, because sometime it return false anyway !!
  55. // $errors = error_get_last();
  56. // throw new OdfException("Can not copy the tempfile in $tmpfile :[".$errors['message'] ."]/[".$errors['type']."]");
  57. }
  58. // futur use here : $odt_content modifications ...
  59. // open the temporary zipfile
  60. if( $file->open($tmpfile, ZIPARCHIVE::CREATE) != TRUE )
  61. {
  62. @unlink($tmpfile); // erase temporary file
  63. throw new OdfException("Error while Opening the tempfile '$tmpfile' - Check your odt file");
  64. }
  65. // for futur use here : with overwrite content.xml in zip file via DOMDocument library :
  66. if (! $file->addFromString('content.xml', $odt_content->saveXML()) )
  67. {
  68. @unlink($tmpfile); // erase temporary file
  69. throw new OdfException('Error during file export');
  70. }
  71. // close the temporary zipfile
  72. $file->close();
  73. // send the new checkresult.odt file via http :
  74. $name = "checkresult.odt";
  75. $size = filesize($tmpfile);
  76. header('Content-type: application/vnd.oasis.opendocument.text');
  77. header('Content-Disposition: attachment; filename="'.$name.'"');
  78. header("Content-Length: ".$size);
  79. readfile($tmpfile); // output
  80. @unlink($tmpfile); // erase temporary file
  81. exit; // be sure nothing else is write after
  82. ?>