odf.php.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. require 'zip/PclZipProxy.php';
  3. require 'zip/PhpZipProxy.php';
  4. require 'Segment.php';
  5. class OdfException extends Exception
  6. {}
  7. /**
  8. * Templating class for odt file
  9. * You need PHP 5.2 at least
  10. * You need Zip Extension or PclZip library
  11. * Encoding : ISO-8859-1
  12. * Last commit by $Author$
  13. * Date - $Date$
  14. * SVN Revision - $Rev$
  15. * Id : $Id$
  16. *
  17. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  18. * @license http://www.gnu.org/copyleft/gpl.html GPL License
  19. * @version 1.3
  20. */
  21. class Odf
  22. {
  23. protected $config = array(
  24. 'ZIP_PROXY' => 'PclZipProxy',
  25. 'DELIMITER_LEFT' => '{',
  26. 'DELIMITER_RIGHT' => '}'
  27. );
  28. protected $file;
  29. protected $contentXml;
  30. protected $tmpfile;
  31. protected $images = array();
  32. protected $vars = array();
  33. protected $segments = array();
  34. const PIXEL_TO_CM = 0.026458333;
  35. /**
  36. * Class constructor
  37. *
  38. * @param string $filename the name of the odt file
  39. * @throws OdfException
  40. */
  41. public function __construct($filename, $config = array())
  42. {
  43. if (! is_array($config)) {
  44. throw new OdfException('Configuration data must be provided as array');
  45. }
  46. foreach ($config as $configKey => $configValue) {
  47. if (array_key_exists($configKey, $this->config)) {
  48. $this->config[$configKey] = $configValue;
  49. }
  50. }
  51. if (! class_exists($this->config['ZIP_PROXY'])) {
  52. throw new OdfException($this->config['ZIP_PROXY'] . ' class not found - check your php settings');
  53. }
  54. $zipHandler = $this->config['ZIP_PROXY'];
  55. $this->file = new $zipHandler();
  56. if ($this->file->open($filename) !== true) {
  57. throw new OdfException("Error while Opening the file '$filename' - Check your odt file");
  58. }
  59. if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
  60. throw new OdfException("Nothing to parse - check that the content.xml file is correctly formed");
  61. }
  62. $this->file->close();
  63. $tmp = tempnam(null, md5(uniqid()));
  64. copy($filename, $tmp);
  65. $this->tmpfile = $tmp;
  66. $this->_moveRowSegments();
  67. }
  68. /**
  69. * Assing a template variable
  70. *
  71. * @param string $key name of the variable within the template
  72. * @param string $value replacement value
  73. * @param bool $encode if true, special XML characters are encoded
  74. * @throws OdfException
  75. * @return odf
  76. */
  77. public function setVars($key, $value, $encode = true, $charset = 'ISO-8859')
  78. {
  79. if (strpos($this->contentXml, $this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']) === false) {
  80. throw new OdfException("var $key not found in the document");
  81. }
  82. $value = $encode ? htmlspecialchars($value) : $value;
  83. $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
  84. $this->vars[$this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']] = str_replace("\n", "<text:line-break/>", $value);
  85. return $this;
  86. }
  87. /**
  88. * Assign a template variable as a picture
  89. *
  90. * @param string $key name of the variable within the template
  91. * @param string $value path to the picture
  92. * @throws OdfException
  93. * @return odf
  94. */
  95. public function setImage($key, $value)
  96. {
  97. $filename = strtok(strrchr($value, '/'), '/.');
  98. $file = substr(strrchr($value, '/'), 1);
  99. $size = @getimagesize($value);
  100. if ($size === false) {
  101. throw new OdfException("Invalid image");
  102. }
  103. list ($width, $height) = $size;
  104. $width *= self::PIXEL_TO_CM;
  105. $height *= self::PIXEL_TO_CM;
  106. $xml = <<<IMG
  107. <draw:frame draw:style-name="fr1" draw:name="$filename" text:anchor-type="char" svg:width="{$width}cm" svg:height="{$height}cm" draw:z-index="3"><draw:image xlink:href="Pictures/$file" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad"/></draw:frame>
  108. IMG;
  109. $this->images[$value] = $file;
  110. $this->setVars($key, $xml, false);
  111. return $this;
  112. }
  113. /**
  114. * Move segment tags for lines of tables
  115. * Called automatically within the constructor
  116. *
  117. * @return void
  118. */
  119. private function _moveRowSegments()
  120. {
  121. // Search all possible rows in the document
  122. $reg1 = "#<table:table-row[^>]*>(.*)</table:table-row>#smU";
  123. preg_match_all($reg1, $this->contentXml, $matches);
  124. for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
  125. // Check if the current row contains a segment row.*
  126. $reg2 = '#\[!--\sBEGIN\s(row.[\S]*)\s--\](.*)\[!--\sEND\s\\1\s--\]#sm';
  127. if (preg_match($reg2, $matches[0][$i], $matches2)) {
  128. $balise = str_replace('row.', '', $matches2[1]);
  129. // Move segment tags around the row
  130. $replace = array(
  131. '[!-- BEGIN ' . $matches2[1] . ' --]' => '',
  132. '[!-- END ' . $matches2[1] . ' --]' => '',
  133. '<table:table-row' => '[!-- BEGIN ' . $balise . ' --]<table:table-row',
  134. '</table:table-row>' => '</table:table-row>[!-- END ' . $balise . ' --]'
  135. );
  136. $replacedXML = str_replace(array_keys($replace), array_values($replace), $matches[0][$i]);
  137. $this->contentXml = str_replace($matches[0][$i], $replacedXML, $this->contentXml);
  138. }
  139. }
  140. }
  141. /**
  142. * Merge template variables
  143. * Called automatically for a save
  144. *
  145. * @return void
  146. */
  147. private function _parse()
  148. {
  149. $this->contentXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->contentXml);
  150. }
  151. /**
  152. * Add the merged segment to the document
  153. *
  154. * @param Segment $segment
  155. * @throws OdfException
  156. * @return odf
  157. */
  158. public function mergeSegment(Segment $segment)
  159. {
  160. if (! array_key_exists($segment->getName(), $this->segments)) {
  161. throw new OdfException($segment->getName() . 'cannot be parsed, has it been set yet ?');
  162. }
  163. $string = $segment->getName();
  164. // $reg = '@<text:p[^>]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
  165. $reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU';
  166. $this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml);
  167. return $this;
  168. }
  169. /**
  170. * Display all the current template variables
  171. *
  172. * @return string
  173. */
  174. public function printVars()
  175. {
  176. return print_r('<pre>' . print_r($this->vars, true) . '</pre>', true);
  177. }
  178. /**
  179. * Display the XML content of the file from odt document
  180. * as it is at the moment
  181. *
  182. * @return string
  183. */
  184. public function __toString()
  185. {
  186. return $this->contentXml;
  187. }
  188. /**
  189. * Display loop segments declared with setSegment()
  190. *
  191. * @return string
  192. */
  193. public function printDeclaredSegments()
  194. {
  195. return '<pre>' . print_r(implode(' ', array_keys($this->segments)), true) . '</pre>';
  196. }
  197. /**
  198. * Declare a segment in order to use it in a loop
  199. *
  200. * @param string $segment
  201. * @throws OdfException
  202. * @return Segment
  203. */
  204. public function setSegment($segment)
  205. {
  206. if (array_key_exists($segment, $this->segments)) {
  207. return $this->segments[$segment];
  208. }
  209. // $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)<text:p\s.*>\[!--\sEND\s$segment\s--\]#sm";
  210. $reg = "#\[!--\sBEGIN\s$segment\s--\](.*)\[!--\sEND\s$segment\s--\]#sm";
  211. if (preg_match($reg, html_entity_decode($this->contentXml), $m) == 0) {
  212. throw new OdfException("'$segment' segment not found in the document");
  213. }
  214. $this->segments[$segment] = new Segment($segment, $m[1], $this);
  215. return $this->segments[$segment];
  216. }
  217. /**
  218. * Save the odt file on the disk
  219. *
  220. * @param string $file name of the desired file
  221. * @throws OdfException
  222. * @return void
  223. */
  224. public function saveToDisk($file = null)
  225. {
  226. if ($file !== null && is_string($file)) {
  227. if (file_exists($file) && !(is_file($file) && is_writable($file))) {
  228. throw new OdfException('Permission denied : can\'t create ' . $file);
  229. }
  230. $this->_save();
  231. copy($this->tmpfile, $file);
  232. } else {
  233. $this->_save();
  234. }
  235. }
  236. /**
  237. * Internal save
  238. *
  239. * @throws OdfException
  240. * @return void
  241. */
  242. private function _save()
  243. {
  244. $this->file->open($this->tmpfile);
  245. $this->_parse();
  246. if (! $this->file->addFromString('content.xml', $this->contentXml)) {
  247. throw new OdfException('Error during file export');
  248. }
  249. foreach ($this->images as $imageKey => $imageValue) {
  250. $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
  251. }
  252. $this->file->close(); // seems to bug on windows CLI sometimes
  253. }
  254. /**
  255. * Export the file as attached file by HTTP
  256. *
  257. * @param string $name (optionnal)
  258. * @throws OdfException
  259. * @return void
  260. */
  261. public function exportAsAttachedFile($name="")
  262. {
  263. $this->_save();
  264. if (headers_sent($filename, $linenum)) {
  265. throw new OdfException("headers already sent ($filename at $linenum)");
  266. }
  267. if( $name == "" )
  268. {
  269. $name = md5(uniqid()) . ".odt";
  270. }
  271. header('Content-type: application/vnd.oasis.opendocument.text');
  272. header('Content-Disposition: attachment; filename="'.$name.'"');
  273. readfile($this->tmpfile);
  274. }
  275. /**
  276. * Returns a variable of configuration
  277. *
  278. * @return string The requested variable of configuration
  279. */
  280. public function getConfig($configKey)
  281. {
  282. if (array_key_exists($configKey, $this->config)) {
  283. return $this->config[$configKey];
  284. }
  285. return false;
  286. }
  287. /**
  288. * Returns the temporary working file
  289. *
  290. * @return string le chemin vers le fichier temporaire de travail
  291. */
  292. public function getTmpfile()
  293. {
  294. return $this->tmpfile;
  295. }
  296. /**
  297. * Delete the temporary file when the object is destroyed
  298. */
  299. public function __destruct() {
  300. if (file_exists($this->tmpfile)) {
  301. unlink($this->tmpfile);
  302. }
  303. }
  304. }
  305. ?>