PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/odtPHP/library/odf.php

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