Segment.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. require 'SegmentIterator.php';
  3. class SegmentException extends Exception
  4. {}
  5. /**
  6. * Class for handling templating segments with odt files
  7. * You need PHP 5.2 at least
  8. * You need Zip Extension or PclZip library
  9. * Encoding : ISO-8859-1
  10. * Last commit by $Author: neveldo $
  11. * Date - $Date: 2009-06-17 12:12:59 +0200 (mer., 17 juin 2009) $
  12. * SVN Revision - $Rev: 44 $
  13. * Id : $Id: Segment.php 44 2009-06-17 10:12:59Z neveldo $
  14. *
  15. * @copyright GPL License 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  16. * @license http://www.gnu.org/copyleft/gpl.html GPL License
  17. * @version 1.3
  18. */
  19. class Segment implements IteratorAggregate, Countable
  20. {
  21. protected $xml;
  22. protected $xmlParsed = '';
  23. protected $name;
  24. protected $children = array();
  25. protected $vars = array();
  26. protected $images = array();
  27. protected $odf;
  28. protected $file;
  29. /**
  30. * Constructor
  31. *
  32. * @param string $name name of the segment to construct
  33. * @param string $xml XML tree of the segment
  34. */
  35. public function __construct($name, $xml, $odf)
  36. {
  37. $this->name = (string) $name;
  38. $this->xml = (string) $xml;
  39. $this->odf = $odf;
  40. $zipHandler = $this->odf->getConfig('ZIP_PROXY');
  41. $this->file = new $zipHandler();
  42. $this->_analyseChildren($this->xml);
  43. }
  44. /**
  45. * Returns the name of the segment
  46. *
  47. * @return string
  48. */
  49. public function getName()
  50. {
  51. return $this->name;
  52. }
  53. /**
  54. * Does the segment have children ?
  55. *
  56. * @return bool
  57. */
  58. public function hasChildren()
  59. {
  60. return $this->getIterator()->hasChildren();
  61. }
  62. /**
  63. * Countable interface
  64. *
  65. * @return int
  66. */
  67. public function count()
  68. {
  69. return count($this->children);
  70. }
  71. /**
  72. * IteratorAggregate interface
  73. *
  74. * @return Iterator
  75. */
  76. public function getIterator()
  77. {
  78. return new RecursiveIteratorIterator(new SegmentIterator($this->children), 1);
  79. }
  80. /**
  81. * Replace variables of the template in the XML code
  82. * All the children are also called
  83. *
  84. * @return string
  85. */
  86. public function merge()
  87. {
  88. $this->xmlParsed .= str_replace(array_keys($this->vars), array_values($this->vars), $this->xml);
  89. if ($this->hasChildren()) {
  90. foreach ($this->children as $child) {
  91. $this->xmlParsed = str_replace($child->xml, ($child->xmlParsed=="")?$child->merge():$child->xmlParsed, $this->xmlParsed);
  92. $child->xmlParsed = '';
  93. }
  94. }
  95. $reg = "/\[!--\sBEGIN\s$this->name\s--\](.*)\[!--\sEND\s$this->name\s--\]/sm";
  96. $this->xmlParsed = preg_replace($reg, '$1', $this->xmlParsed);
  97. $this->file->open($this->odf->getTmpfile());
  98. foreach ($this->images as $imageKey => $imageValue) {
  99. if ($this->file->getFromName('Pictures/' . $imageValue) === false) {
  100. $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
  101. }
  102. // MODIF YC : fix 'corrupt odt file in OpenOffice 3.2' when inserting images.
  103. $this->odf->addImageToManifest($imageValue);
  104. // /MODIF
  105. }
  106. $this->file->close();
  107. return $this->xmlParsed;
  108. }
  109. /**
  110. * Analyse the XML code in order to find children
  111. *
  112. * @param string $xml
  113. * @return Segment
  114. */
  115. protected function _analyseChildren($xml)
  116. {
  117. // $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](?:<\/text:p>)?(.*)(?:<text:p\s.*>)?\[!--\sEND\s(\\1)\s--\]#sm";
  118. $reg2 = "#\[!--\sBEGIN\s([\S]*)\s--\](.*)\[!--\sEND\s(\\1)\s--\]#sm";
  119. preg_match_all($reg2, $xml, $matches);
  120. for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
  121. if ($matches[1][$i] != $this->name) {
  122. $this->children[$matches[1][$i]] = new self($matches[1][$i], $matches[0][$i], $this->odf);
  123. } else {
  124. $this->_analyseChildren($matches[2][$i]);
  125. }
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Assign a template variable to replace
  131. *
  132. * @param string $key
  133. * @param string $value
  134. * @throws SegmentException
  135. * @return Segment
  136. */
  137. public function setVars($key, $value, $encode = true, $charset = 'ISO-8859')
  138. {
  139. if (strpos($this->xml, $this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')) === false) {
  140. throw new SegmentException("var $key not found in {$this->getName()}");
  141. }
  142. $value = $encode ? htmlspecialchars($value) : $value;
  143. $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
  144. $this->vars[$this->odf->getConfig('DELIMITER_LEFT') . $key . $this->odf->getConfig('DELIMITER_RIGHT')] = str_replace("\n", "<text:line-break/>", $value);
  145. return $this;
  146. }
  147. /**
  148. * Assign a template variable as a picture
  149. *
  150. * @param string $key name of the variable within the template
  151. * @param string $value path to the picture
  152. * @throws OdfException
  153. * @return Segment
  154. */
  155. public function setImage($key, $value)
  156. {
  157. $filename = strtok(strrchr($value, '/'), '/.');
  158. $file = substr(strrchr($value, '/'), 1);
  159. $size = @getimagesize($value);
  160. if ($size === false) {
  161. throw new OdfException("Invalid image");
  162. }
  163. list ($width, $height) = $size;
  164. $width *= Odf::PIXEL_TO_CM;
  165. $height *= Odf::PIXEL_TO_CM;
  166. $xml = <<<IMG
  167. <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>
  168. IMG;
  169. $this->images[$value] = $file;
  170. $this->setVars($key, $xml, false);
  171. return $this;
  172. }
  173. /**
  174. * Shortcut to retrieve a child
  175. *
  176. * @param string $prop
  177. * @return Segment
  178. * @throws SegmentException
  179. */
  180. public function __get($prop)
  181. {
  182. if (array_key_exists($prop, $this->children)) {
  183. return $this->children[$prop];
  184. } else {
  185. throw new SegmentException('child ' . $prop . ' does not exist');
  186. }
  187. }
  188. /**
  189. * Proxy for setVars
  190. *
  191. * @param string $meth
  192. * @param array $args
  193. * @return Segment
  194. */
  195. public function __call($meth, $args)
  196. {
  197. try {
  198. return $this->setVars($meth, $args[0]);
  199. } catch (SegmentException $e) {
  200. throw new SegmentException("method $meth nor var $meth exist");
  201. }
  202. }
  203. /**
  204. * Returns the parsed XML
  205. *
  206. * @return string
  207. */
  208. public function getXmlParsed()
  209. {
  210. return $this->xmlParsed;
  211. }
  212. }
  213. ?>