odf.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. <?php
  2. require 'Segment.php';
  3. /**
  4. * Class of ODT Exception
  5. */
  6. class OdfException extends Exception
  7. {
  8. }
  9. /**
  10. * Templating class for odt file
  11. * You need PHP 5.2 at least
  12. * You need Zip Extension or PclZip library
  13. *
  14. * @copyright 2008 - Julien Pauli - Cyril PIERRE de GEYER - Anaska (http://www.anaska.com)
  15. * @copyright 2010-2015 - Laurent Destailleur - eldy@users.sourceforge.net
  16. * @copyright 2010 - Vikas Mahajan - http://vikasmahajan.wordpress.com
  17. * @copyright 2012 - Stephen Larroque - lrq3000@gmail.com
  18. * @license https://www.gnu.org/copyleft/gpl.html GPL License
  19. * @version 1.5.0
  20. */
  21. class Odf
  22. {
  23. protected $config = array(
  24. 'ZIP_PROXY' => 'PclZipProxy', // PclZipProxy, PhpZipProxy
  25. 'DELIMITER_LEFT' => '{',
  26. 'DELIMITER_RIGHT' => '}',
  27. 'PATH_TO_TMP' => '/tmp'
  28. );
  29. protected $file;
  30. protected $contentXml; // To store content of content.xml file
  31. protected $metaXml; // To store content of meta.xml file
  32. protected $stylesXml; // To store content of styles.xml file
  33. protected $manifestXml; // To store content of META-INF/manifest.xml file
  34. protected $tmpfile;
  35. protected $tmpdir='';
  36. protected $images = array();
  37. protected $vars = array();
  38. protected $segments = array();
  39. public $creator;
  40. public $title;
  41. public $subject;
  42. public $userdefined=array();
  43. const PIXEL_TO_CM = 0.026458333;
  44. /**
  45. * Class constructor
  46. *
  47. * @param string $filename The name of the odt file
  48. * @param string $config Array of config data
  49. * @throws OdfException
  50. */
  51. public function __construct($filename, $config = array())
  52. {
  53. clearstatcache();
  54. if (! is_array($config)) {
  55. throw new OdfException('Configuration data must be provided as array');
  56. }
  57. foreach ($config as $configKey => $configValue) {
  58. if (array_key_exists($configKey, $this->config)) {
  59. $this->config[$configKey] = $configValue;
  60. }
  61. }
  62. $md5uniqid = md5(uniqid());
  63. if ($this->config['PATH_TO_TMP']) $this->tmpdir = preg_replace('|[\/]$|', '', $this->config['PATH_TO_TMP']); // Remove last \ or /
  64. $this->tmpdir .= ($this->tmpdir?'/':'').$md5uniqid;
  65. $this->tmpfile = $this->tmpdir.'/'.$md5uniqid.'.odt'; // We keep .odt extension to allow OpenOffice usage during debug.
  66. // A working directory is required for some zip proxy like PclZipProxy
  67. if (in_array($this->config['ZIP_PROXY'], array('PclZipProxy')) && ! is_dir($this->config['PATH_TO_TMP'])) {
  68. throw new OdfException('Temporary directory '.$this->config['PATH_TO_TMP'].' must exists');
  69. }
  70. // Create tmp direcoty (will be deleted in destructor)
  71. if (!file_exists($this->tmpdir)) {
  72. $result = mkdir($this->tmpdir);
  73. }
  74. // Load zip proxy
  75. $zipHandler = $this->config['ZIP_PROXY'];
  76. if (!defined('PCLZIP_TEMPORARY_DIR')) define('PCLZIP_TEMPORARY_DIR', $this->tmpdir);
  77. include_once 'zip/'.$zipHandler.'.php';
  78. if (! class_exists($this->config['ZIP_PROXY'])) {
  79. throw new OdfException($this->config['ZIP_PROXY'] . ' class not found - check your php settings');
  80. }
  81. $this->file = new $zipHandler($this->tmpdir);
  82. if ($this->file->open($filename) !== true) { // This also create the tmpdir directory
  83. throw new OdfException("Error while Opening the file '$filename' - Check your odt filename");
  84. }
  85. if (($this->contentXml = $this->file->getFromName('content.xml')) === false) {
  86. throw new OdfException("Nothing to parse - Check that the content.xml file is correctly formed in source file '$filename'");
  87. }
  88. if (($this->manifestXml = $this->file->getFromName('META-INF/manifest.xml')) === false) {
  89. throw new OdfException("Something is wrong with META-INF/manifest.xml in source file '$filename'");
  90. }
  91. if (($this->metaXml = $this->file->getFromName('meta.xml')) === false) {
  92. throw new OdfException("Nothing to parse - Check that the meta.xml file is correctly formed in source file '$filename'");
  93. }
  94. if (($this->stylesXml = $this->file->getFromName('styles.xml')) === false) {
  95. throw new OdfException("Nothing to parse - Check that the styles.xml file is correctly formed in source file '$filename'");
  96. }
  97. $this->file->close();
  98. //print "tmpdir=".$tmpdir;
  99. //print "filename=".$filename;
  100. //print "tmpfile=".$tmpfile;
  101. copy($filename, $this->tmpfile);
  102. // Now file has been loaded, we must move the [!-- BEGIN and [!-- END tags outside the
  103. // <table:table-row tag and clean bad lines tags.
  104. $this->_moveRowSegments();
  105. }
  106. /**
  107. * Assing a template variable into ->vars.
  108. * For example, key is {object_date} and value is '2021-01-01'
  109. *
  110. * @param string $key Name of the variable within the template
  111. * @param string $value Replacement value
  112. * @param bool $encode If true, special XML characters are encoded
  113. * @param string $charset Charset
  114. * @throws OdfException
  115. * @return odf
  116. */
  117. public function setVars($key, $value, $encode = true, $charset = 'ISO-8859')
  118. {
  119. $tag = $this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT'];
  120. // TODO Warning string may be:
  121. // <text:span text:style-name="T13">{</text:span><text:span text:style-name="T12">aaa</text:span><text:span text:style-name="T13">}</text:span>
  122. // instead of {aaa} so we should enhance this function.
  123. //print $key.'-'.$value.'-'.strpos($this->contentXml, $this->config['DELIMITER_LEFT'] . $key . $this->config['DELIMITER_RIGHT']).'<br>';
  124. if (strpos($this->contentXml, $tag) === false && strpos($this->stylesXml, $tag) === false) {
  125. // Add the throw only for development. In most cases, it is normal to not having the key into the document (only few keys are presents).
  126. //throw new OdfException("var $key not found in the document");
  127. return $this;
  128. }
  129. $this->vars[$tag] = $this->convertVarToOdf($value, $encode, $charset);
  130. return $this;
  131. }
  132. /**
  133. * Replaces html tags in odt tags and returns a compatible string
  134. *
  135. * @param string $value Replacement value
  136. * @param bool $encode If true, special XML characters are encoded
  137. * @param string $charset Charset
  138. * @return string
  139. */
  140. public function convertVarToOdf($value, $encode = true, $charset = 'ISO-8859')
  141. {
  142. $value = $encode ? htmlspecialchars($value) : $value;
  143. $value = ($charset == 'ISO-8859') ? utf8_encode($value) : $value;
  144. $convertedValue = $value;
  145. // Check if the value includes html tags
  146. if ($this->_hasHtmlTag($value) === true) {
  147. // Default styles for strong/b, i/em, u, s, sub & sup
  148. $automaticStyles = array(
  149. '<style:style style:name="boldText" style:family="text"><style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold" /></style:style>',
  150. '<style:style style:name="italicText" style:family="text"><style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic" /></style:style>',
  151. '<style:style style:name="underlineText" style:family="text"><style:text-properties style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color" /></style:style>',
  152. '<style:style style:name="strikethroughText" style:family="text"><style:text-properties style:text-line-through-style="solid" style:text-line-through-type="single" /></style:style>',
  153. '<style:style style:name="subText" style:family="text"><style:text-properties style:text-position="sub 58%" /></style:style>',
  154. '<style:style style:name="supText" style:family="text"><style:text-properties style:text-position="super 58%" /></style:style>'
  155. );
  156. $customStyles = array();
  157. $fontDeclarations = array();
  158. $convertedValue = $this->_replaceHtmlWithOdtTag($this->_getDataFromHtml($value), $customStyles, $fontDeclarations);
  159. foreach ($customStyles as $key => $val) {
  160. array_push($automaticStyles, '<style:style style:name="customStyle' . $key . '" style:family="text">' . $val . '</style:style>');
  161. }
  162. // Join the styles and add them to the content xml
  163. $styles = '';
  164. foreach ($automaticStyles as $style) {
  165. if (strpos($this->contentXml, $style) === false) {
  166. $styles .= $style;
  167. }
  168. }
  169. $this->contentXml = str_replace('</office:automatic-styles>', $styles . '</office:automatic-styles>', $this->contentXml);
  170. // Join the font declarations and add them to the content xml
  171. $fonts = '';
  172. foreach ($fontDeclarations as $font) {
  173. if (strpos($this->contentXml, 'style:name="' . $font . '"') === false) {
  174. $fonts .= '<style:font-face style:name="' . $font . '" svg:font-family="\'' . $font . '\'" />';
  175. }
  176. }
  177. $this->contentXml = str_replace('</office:font-face-decls>', $fonts . '</office:font-face-decls>', $this->contentXml);
  178. } else $convertedValue = preg_replace('/(\r\n|\r|\n)/i', "<text:line-break/>", $value);
  179. return $convertedValue;
  180. }
  181. /**
  182. * Replaces html tags in with odt tags and returns an odt string
  183. * @param array $tags An array with html tags generated by the getDataFromHtml() function
  184. * @param array $customStyles An array of style defenitions that should be included inside the odt file
  185. * @param array $fontDeclarations An array of font declarations that should be included inside the odt file
  186. * @return string
  187. */
  188. private function _replaceHtmlWithOdtTag($tags, &$customStyles, &$fontDeclarations)
  189. {
  190. if ($customStyles == null) $customStyles = array();
  191. if ($fontDeclarations == null) $fontDeclarations = array();
  192. $odtResult = '';
  193. foreach ((array) $tags as $tag) {
  194. // Check if the current item is a tag or just plain text
  195. if (isset($tag['text'])) {
  196. $odtResult .= $tag['text'];
  197. } elseif (isset($tag['name'])) {
  198. switch ($tag['name']) {
  199. case 'br':
  200. $odtResult .= '<text:line-break/>';
  201. break;
  202. case 'strong':
  203. case 'b':
  204. $odtResult .= '<text:span text:style-name="boldText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
  205. break;
  206. case 'a':
  207. $odtResult = '<text:a xlink:type="simple" xlink:href="'.(is_numeric(strpos($tag['innerText'], '@')) ?'mailto:'.$tag['innerText'] :$tag['innerText']).'" office:name="">'.$tag['innerText'].'</text:a>';
  208. break;
  209. case 'i':
  210. case 'em':
  211. $odtResult .= '<text:span text:style-name="italicText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
  212. break;
  213. case 'u':
  214. $odtResult .= '<text:span text:style-name="underlineText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
  215. break;
  216. case 's':
  217. $odtResult .= '<text:span text:style-name="strikethroughText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
  218. break;
  219. case 'sub':
  220. $odtResult .= '<text:span text:style-name="subText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
  221. break;
  222. case 'sup':
  223. $odtResult .= '<text:span text:style-name="supText">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:span>';
  224. break;
  225. case 'span':
  226. case 'p':
  227. if (isset($tag['attributes']['style'])) {
  228. $odtStyles = '';
  229. foreach ($tag['attributes']['style'] as $styleName => $styleValue) {
  230. switch ($styleName) {
  231. case 'font-family':
  232. $fontName = $styleValue;
  233. if (strpos($fontName, ',') !== false) {
  234. $fontName = explode(',', $fontName)[0];
  235. }
  236. if (!in_array($fontName, $fontDeclarations)) {
  237. array_push($fontDeclarations, $fontName);
  238. }
  239. $odtStyles .= '<style:text-properties style:font-name="' . $fontName . '" />';
  240. break;
  241. case 'font-size':
  242. if (preg_match('/([0-9]+)\s?(px|pt)/', $styleValue, $matches)) {
  243. $fontSize = intval($matches[1]);
  244. if ($matches[2] == 'px') {
  245. $fontSize = round($fontSize * 0.75);
  246. }
  247. $odtStyles .= '<style:text-properties fo:font-size="' . $fontSize . 'pt" style:font-size-asian="' . $fontSize . 'pt" style:font-size-complex="' . $fontSize . 'pt" />';
  248. }
  249. break;
  250. case 'color':
  251. if (preg_match('/#[0-9A-Fa-f]{3}(?:[0-9A-Fa-f]{3})?/', $styleValue)) {
  252. $odtStyles .= '<style:text-properties fo:color="' . $styleValue . '" />';
  253. }
  254. break;
  255. case 'background-color':
  256. if (preg_match('/#[0-9A-Fa-f]{3}(?:[0-9A-Fa-f]{3})?/', $styleValue)) {
  257. $odtStyles .= '<style:text-properties fo:background-color="' . $styleValue . '" />';
  258. }
  259. break;
  260. }
  261. }
  262. if (strlen($odtStyles) > 0) {
  263. // Generate a unique id for the style (using microtime and random because some CPUs are really fast...)
  264. $key = floatval(str_replace('.', '', microtime(true)))+rand(0, 10);
  265. $customStyles[$key] = $odtStyles;
  266. $odtResult .= '<text:'.($tag['name']).' text:style-name="customStyle' . $key . '">' . ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']) . '</text:'.($tag['name']).'>';
  267. }
  268. else {
  269. $odtResult .= ($tag['children'] != null ? $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations) : $tag['innerText']);
  270. }
  271. }
  272. break;
  273. default:
  274. $odtResult .= $this->_replaceHtmlWithOdtTag($tag['children'], $customStyles, $fontDeclarations);
  275. break;
  276. }
  277. }
  278. }
  279. return $odtResult;
  280. }
  281. /**
  282. * Checks if the given text is a html string
  283. * @param string $text The text to check
  284. * @return bool
  285. */
  286. private function _isHtmlTag($text)
  287. {
  288. return preg_match('/<([A-Za-z]+)(?:\s([A-Za-z]+(?:\-[A-Za-z]+)?(?:=(?:".*?")|(?:[0-9]+))))*(?:(?:\s\/>)|(?:>(.*)<\/\1>))/', $text);
  289. }
  290. /**
  291. * Checks if the given text includes a html string
  292. * @param string $text The text to check
  293. * @return bool
  294. */
  295. private function _hasHtmlTag($text)
  296. {
  297. $result = preg_match_all('/<([A-Za-z]+)(?:\s([A-Za-z]+(?:\-[A-Za-z]+)?(?:=(?:".*?")|(?:[0-9]+))))*(?:(?:\s\/>)|(?:>(.*)<\/\1>))/', $text);
  298. return is_numeric($result) && $result > 0;
  299. }
  300. /**
  301. * Returns an array of html elements
  302. * @param string $html A string with html tags
  303. * @return array
  304. */
  305. private function _getDataFromHtml($html)
  306. {
  307. $tags = array();
  308. $tempHtml = $html;
  309. while (strlen($tempHtml) > 0) {
  310. $matches = array();
  311. // Check if the string includes a html tag
  312. if (preg_match_all('/<([A-Za-z]+)(?:\s([A-Za-z]+(?:\-[A-Za-z]+)?(?:=(?:".*?")|(?:[0-9]+))))*(?:(?:\s\/>)|(?:>(.*)<\/\1>))/', $tempHtml, $matches)) {
  313. $tagOffset = strpos($tempHtml, $matches[0][0]);
  314. // Check if the string starts with the html tag
  315. if ($tagOffset > 0) {
  316. // Push the text infront of the html tag to the result array
  317. array_push($tags, array(
  318. 'text' => substr($tempHtml, 0, $tagOffset)
  319. ));
  320. // Remove the text from the string
  321. $tempHtml = substr($tempHtml, $tagOffset);
  322. }
  323. // Extract the attribute data from the html tag
  324. $explodedAttributes = array();
  325. preg_match_all('/([0-9A-Za-z]+(?:="[0-9A-Za-z\:\-\s\,\;\#]*")?)+/', $matches[2][0], $explodedAttributes);
  326. $explodedAttributes = array_filter($explodedAttributes[0]);
  327. $attributes = array();
  328. // Store each attribute with its name in the $attributes array
  329. $explodedAttributesCount = count($explodedAttributes);
  330. for ($i=0; $i<$explodedAttributesCount; $i++) {
  331. $attribute = trim($explodedAttributes[$i]);
  332. // Check if the attribute has a value (like style="") or has no value (like required)
  333. if (strpos($attribute, '=') !== false) {
  334. $splitAttribute = explode('=', $attribute);
  335. $attrName = trim($splitAttribute[0]);
  336. $attrValue = trim(str_replace('"', '', $splitAttribute[1]));
  337. // check if the current attribute is a style attribute
  338. if (strtolower($attrName) == 'style') {
  339. $attributes[$attrName] = array();
  340. if (strpos($attrValue, ';') !== false) {
  341. // Split the style properties and store them in an array
  342. $explodedStyles = explode(';', $attrValue);
  343. $explodedStylesCount = count($explodedStyles);
  344. for ($n=0; $n<$explodedStylesCount; $n++) {
  345. $splitStyle = explode(':', $explodedStyles[$n]);
  346. $attributes[$attrName][trim($splitStyle[0])] = trim($splitStyle[1]);
  347. }
  348. } else {
  349. $splitStyle = explode(':', $attrValue);
  350. $attributes[$attrName][trim($splitStyle[0])] = trim($splitStyle[1]);
  351. }
  352. } else {
  353. // Store the value directly in the $attributes array if this is not the style attribute
  354. $attributes[$attrName] = $attrValue;
  355. }
  356. } else {
  357. $attributes[trim($attribute)] = true;
  358. }
  359. }
  360. // Push the html tag data to the result array
  361. array_push($tags, array(
  362. 'name' => $matches[1][0],
  363. 'attributes' => $attributes,
  364. 'innerText' => strip_tags($matches[3][0]),
  365. 'children' => $this->_hasHtmlTag($matches[3][0]) ? $this->_getDataFromHtml($matches[3][0]) : null
  366. ));
  367. // Remove the processed html tag from the html string
  368. $tempHtml = substr($tempHtml, strlen($matches[0][0]));
  369. } else {
  370. array_push($tags, array(
  371. 'text' => $tempHtml
  372. ));
  373. $tempHtml = '';
  374. }
  375. }
  376. return $tags;
  377. }
  378. /**
  379. * Function to convert a HTML string into an ODT string
  380. *
  381. * @param string $value String to convert
  382. * @return string String converted
  383. */
  384. public function htmlToUTFAndPreOdf($value)
  385. {
  386. // We decode into utf8, entities
  387. $value=dol_html_entity_decode($value, ENT_QUOTES|ENT_HTML5);
  388. // We convert html tags
  389. $ishtml=dol_textishtml($value);
  390. if ($ishtml) {
  391. // If string is "MYPODUCT - Desc <strong>bold</strong> with &eacute; accent<br />\n<br />\nUn texto en espa&ntilde;ol ?"
  392. // Result after clean must be "MYPODUCT - Desc bold with é accent\n\nUn texto en espa&ntilde;ol ?"
  393. // We want to ignore \n and we want all <br> to be \n
  394. $value=preg_replace('/(\r\n|\r|\n)/i', '', $value);
  395. $value=preg_replace('/<br>/i', "\n", $value);
  396. $value=preg_replace('/<br\s+[^<>\/]*>/i', "\n", $value);
  397. $value=preg_replace('/<br\s+[^<>\/]*\/>/i', "\n", $value);
  398. //$value=preg_replace('/<strong>/','__lt__text:p text:style-name=__quot__bold__quot____gt__',$value);
  399. //$value=preg_replace('/<\/strong>/','__lt__/text:p__gt__',$value);
  400. $value=dol_string_nohtmltag($value, 0);
  401. }
  402. return $value;
  403. }
  404. /**
  405. * Function to convert a HTML string into an ODT string
  406. *
  407. * @param string $value String to convert
  408. * @return string String converted
  409. */
  410. public function preOdfToOdf($value)
  411. {
  412. $value = str_replace("\n", "<text:line-break/>", $value);
  413. //$value = str_replace("__lt__", "<", $value);
  414. //$value = str_replace("__gt__", ">", $value);
  415. //$value = str_replace("__quot__", '"', $value);
  416. return $value;
  417. }
  418. /**
  419. * Assign a template variable as a picture
  420. *
  421. * @param string $key name of the variable within the template
  422. * @param string $value path to the picture
  423. * @throws OdfException
  424. * @return odf
  425. */
  426. public function setImage($key, $value)
  427. {
  428. $filename = strtok(strrchr($value, '/'), '/.');
  429. $file = substr(strrchr($value, '/'), 1);
  430. $size = @getimagesize($value);
  431. if ($size === false) {
  432. throw new OdfException("Invalid image");
  433. }
  434. list ($width, $height) = $size;
  435. $width *= self::PIXEL_TO_CM;
  436. $height *= self::PIXEL_TO_CM;
  437. $xml = <<<IMG
  438. <draw:frame draw:style-name="fr1" draw:name="$filename" text:anchor-type="aschar" 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>
  439. IMG;
  440. $this->images[$value] = $file;
  441. $this->setVars($key, $xml, false);
  442. return $this;
  443. }
  444. /**
  445. * Move segment tags for lines of tables
  446. * This function is called automatically within the constructor, so this->contentXml is clean before any other thing
  447. *
  448. * @return void
  449. */
  450. private function _moveRowSegments()
  451. {
  452. // Replace BEGIN<text:s/>xxx into BEGIN xxx
  453. $this->contentXml = preg_replace('/\[!--\sBEGIN<text:s[^>]>(row.[\S]*)\s--\]/sm', '[!-- BEGIN \\1 --]', $this->contentXml);
  454. // Replace END<text:s/>xxx into END xxx
  455. $this->contentXml = preg_replace('/\[!--\sEND<text:s[^>]>(row.[\S]*)\s--\]/sm', '[!-- END \\1 --]', $this->contentXml);
  456. // Search all possible rows in the document
  457. $reg1 = "#<table:table-row[^>]*>(.*)</table:table-row>#smU";
  458. $matches = array();
  459. preg_match_all($reg1, $this->contentXml, $matches);
  460. for ($i = 0, $size = count($matches[0]); $i < $size; $i++) {
  461. // Check if the current row contains a segment row.*
  462. $reg2 = '#\[!--\sBEGIN\s(row.[\S]*)\s--\](.*)\[!--\sEND\s\\1\s--\]#sm';
  463. $matches2 = array();
  464. if (preg_match($reg2, $matches[0][$i], $matches2)) {
  465. $balise = str_replace('row.', '', $matches2[1]);
  466. // Move segment tags around the row
  467. $replace = array(
  468. '[!-- BEGIN ' . $matches2[1] . ' --]' => '',
  469. '[!-- END ' . $matches2[1] . ' --]' => '',
  470. '<table:table-row' => '[!-- BEGIN ' . $balise . ' --]<table:table-row',
  471. '</table:table-row>' => '</table:table-row>[!-- END ' . $balise . ' --]'
  472. );
  473. $replacedXML = str_replace(array_keys($replace), array_values($replace), $matches[0][$i]);
  474. $this->contentXml = str_replace($matches[0][$i], $replacedXML, $this->contentXml);
  475. }
  476. }
  477. }
  478. /**
  479. * Merge template variables
  480. * Called at the beginning of the _save function
  481. *
  482. * @param string $type 'content', 'styles' or 'meta'
  483. * @return void
  484. */
  485. private function _parse($type = 'content')
  486. {
  487. // Search all tags found into condition to complete $this->vars, so we will proceed all tests even if not defined
  488. $reg='@\[!--\sIF\s([{}a-zA-Z0-9\.\,_]+)\s--\]@smU';
  489. $matches = array();
  490. preg_match_all($reg, $this->contentXml, $matches, PREG_SET_ORDER);
  491. //var_dump($this->vars);exit;
  492. foreach ($matches as $match) { // For each match, if there is no entry into this->vars, we add it
  493. if (! empty($match[1]) && ! isset($this->vars[$match[1]])) {
  494. $this->vars[$match[1]] = ''; // Not defined, so we set it to '', we just need entry into this->vars for next loop
  495. }
  496. }
  497. //var_dump($this->vars);exit;
  498. // Conditionals substitution
  499. // Note: must be done before static substitution, else the variable will be replaced by its value and the conditional won't work anymore
  500. foreach ($this->vars as $key => $value) {
  501. // If value is true (not 0 nor false nor null nor empty string)
  502. if ($value) {
  503. //dol_syslog("Var ".$key." is defined, we remove the IF, ELSE and ENDIF ");
  504. //$sav=$this->contentXml;
  505. // Remove the IF tag
  506. $this->contentXml = str_replace('[!-- IF '.$key.' --]', '', $this->contentXml);
  507. // Remove everything between the ELSE tag (if it exists) and the ENDIF tag
  508. $reg = '@(\[!--\sELSE\s' . $key . '\s--\](.*))?\[!--\sENDIF\s' . $key . '\s--\]@smU'; // U modifier = all quantifiers are non-greedy
  509. $this->contentXml = preg_replace($reg, '', $this->contentXml);
  510. /*if ($sav != $this->contentXml)
  511. {
  512. dol_syslog("We found a IF and it was processed");
  513. //var_dump($sav);exit;
  514. }*/
  515. } else {
  516. // Else the value is false, then two cases: no ELSE and we're done, or there is at least one place where there is an ELSE clause, then we replace it
  517. //dol_syslog("Var ".$key." is not defined, we remove the IF, ELSE and ENDIF ");
  518. //$sav=$this->contentXml;
  519. // Find all conditional blocks for this variable: from IF to ELSE and to ENDIF
  520. $reg = '@\[!--\sIF\s' . $key . '\s--\](.*)(\[!--\sELSE\s' . $key . '\s--\](.*))?\[!--\sENDIF\s' . $key . '\s--\]@smU'; // U modifier = all quantifiers are non-greedy
  521. preg_match_all($reg, $this->contentXml, $matches, PREG_SET_ORDER);
  522. foreach ($matches as $match) { // For each match, if there is an ELSE clause, we replace the whole block by the value in the ELSE clause
  523. if (!empty($match[3])) $this->contentXml = str_replace($match[0], $match[3], $this->contentXml);
  524. }
  525. // Cleanup the other conditional blocks (all the others where there were no ELSE clause, we can just remove them altogether)
  526. $this->contentXml = preg_replace($reg, '', $this->contentXml);
  527. /*if ($sav != $this->contentXml)
  528. {
  529. dol_syslog("We found a IF and it was processed");
  530. //var_dump($sav);exit;
  531. }*/
  532. }
  533. }
  534. // Static substitution
  535. if ($type == 'content') $this->contentXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->contentXml);
  536. if ($type == 'styles') $this->stylesXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->stylesXml);
  537. if ($type == 'meta') $this->metaXml = str_replace(array_keys($this->vars), array_values($this->vars), $this->metaXml);
  538. }
  539. /**
  540. * Add the merged segment to the document
  541. *
  542. * @param Segment $segment Segment
  543. * @throws OdfException
  544. * @return odf
  545. */
  546. public function mergeSegment(Segment $segment)
  547. {
  548. if (! array_key_exists($segment->getName(), $this->segments)) {
  549. throw new OdfException($segment->getName() . 'cannot be parsed, has it been set yet ?');
  550. }
  551. $string = $segment->getName();
  552. // $reg = '@<text:p[^>]*>\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]<\/text:p>@smU';
  553. $reg = '@\[!--\sBEGIN\s' . $string . '\s--\](.*)\[!--.+END\s' . $string . '\s--\]@smU';
  554. $this->contentXml = preg_replace($reg, $segment->getXmlParsed(), $this->contentXml);
  555. return $this;
  556. }
  557. /**
  558. * Display all the current template variables
  559. *
  560. * @return string
  561. */
  562. public function printVars()
  563. {
  564. return print_r('<pre>' . print_r($this->vars, true) . '</pre>', true);
  565. }
  566. /**
  567. * Display the XML content of the file from odt document
  568. * as it is at the moment
  569. *
  570. * @return string
  571. */
  572. public function __toString()
  573. {
  574. return $this->contentXml;
  575. }
  576. /**
  577. * Display loop segments declared with setSegment()
  578. *
  579. * @return string
  580. */
  581. public function printDeclaredSegments()
  582. {
  583. return '<pre>' . print_r(implode(' ', array_keys($this->segments)), true) . '</pre>';
  584. }
  585. /**
  586. * Declare a segment in order to use it in a loop.
  587. * Extract the segment and store it into $this->segments[]. Return it for next call.
  588. *
  589. * @param string $segment Segment
  590. * @throws OdfException
  591. * @return Segment
  592. */
  593. public function setSegment($segment)
  594. {
  595. if (array_key_exists($segment, $this->segments)) {
  596. return $this->segments[$segment];
  597. }
  598. // $reg = "#\[!--\sBEGIN\s$segment\s--\]<\/text:p>(.*)<text:p\s.*>\[!--\sEND\s$segment\s--\]#sm";
  599. $reg = "#\[!--\sBEGIN\s$segment\s--\](.*)\[!--\sEND\s$segment\s--\]#sm";
  600. $m = array();
  601. if (preg_match($reg, html_entity_decode($this->contentXml), $m) == 0) {
  602. throw new OdfException("'".$segment."' segment not found in the document. The tag [!-- BEGIN xxx --] or [!-- END xxx --] is not present into content file.");
  603. }
  604. $this->segments[$segment] = new Segment($segment, $m[1], $this);
  605. return $this->segments[$segment];
  606. }
  607. /**
  608. * Save the odt file on the disk
  609. *
  610. * @param string $file name of the desired file
  611. * @throws OdfException
  612. * @return void
  613. */
  614. public function saveToDisk($file = null)
  615. {
  616. if ($file !== null && is_string($file)) {
  617. if (file_exists($file) && !(is_file($file) && is_writable($file))) {
  618. throw new OdfException('Permission denied : can\'t create ' . $file);
  619. }
  620. $this->_save();
  621. copy($this->tmpfile, $file);
  622. } else {
  623. $this->_save();
  624. }
  625. }
  626. /**
  627. * Write output file onto disk
  628. *
  629. * @throws OdfException
  630. * @return void
  631. */
  632. private function _save()
  633. {
  634. $res=$this->file->open($this->tmpfile); // tmpfile is odt template
  635. $this->_parse('content');
  636. $this->_parse('styles');
  637. $this->_parse('meta');
  638. $this->setMetaData();
  639. //print $this->metaXml;exit;
  640. if (! $this->file->addFromString('content.xml', $this->contentXml)) {
  641. throw new OdfException('Error during file export addFromString content');
  642. }
  643. if (! $this->file->addFromString('meta.xml', $this->metaXml)) {
  644. throw new OdfException('Error during file export addFromString meta');
  645. }
  646. if (! $this->file->addFromString('styles.xml', $this->stylesXml)) {
  647. throw new OdfException('Error during file export addFromString styles');
  648. }
  649. foreach ($this->images as $imageKey => $imageValue) {
  650. // Add the image inside the ODT document
  651. $this->file->addFile($imageKey, 'Pictures/' . $imageValue);
  652. // Add the image to the Manifest (which maintains a list of images, necessary to avoid "Corrupt ODT file. Repair?" when opening the file with LibreOffice)
  653. $this->addImageToManifest($imageValue);
  654. }
  655. if (! $this->file->addFromString('./META-INF/manifest.xml', $this->manifestXml)) {
  656. throw new OdfException('Error during file export: manifest.xml');
  657. }
  658. $this->file->close();
  659. }
  660. /**
  661. * Update Meta information
  662. * <dc:date>2013-03-16T14:06:25</dc:date>
  663. *
  664. * @return void
  665. */
  666. public function setMetaData()
  667. {
  668. if (empty($this->creator)) $this->creator='';
  669. $this->metaXml = preg_replace('/<dc:date>.*<\/dc:date>/', '<dc:date>'.gmdate("Y-m-d\TH:i:s").'</dc:date>', $this->metaXml);
  670. $this->metaXml = preg_replace('/<dc:creator>.*<\/dc:creator>/', '<dc:creator>'.htmlspecialchars($this->creator).'</dc:creator>', $this->metaXml);
  671. $this->metaXml = preg_replace('/<dc:title>.*<\/dc:title>/', '<dc:title>'.htmlspecialchars($this->title).'</dc:title>', $this->metaXml);
  672. $this->metaXml = preg_replace('/<dc:subject>.*<\/dc:subject>/', '<dc:subject>'.htmlspecialchars($this->subject).'</dc:subject>', $this->metaXml);
  673. if (count($this->userdefined)) {
  674. foreach ($this->userdefined as $key => $val) {
  675. $this->metaXml = preg_replace('<meta:user-defined meta:name="'.$key.'"/>', '', $this->metaXml);
  676. $this->metaXml = preg_replace('/<meta:user-defined meta:name="'.$key.'">.*<\/meta:user-defined>/', '', $this->metaXml);
  677. $this->metaXml = str_replace('</office:meta>', '<meta:user-defined meta:name="'.$key.'">'.htmlspecialchars($val).'</meta:user-defined></office:meta>', $this->metaXml);
  678. }
  679. }
  680. }
  681. /**
  682. * Update Manifest file according to added image files
  683. *
  684. * @param string $file Image file to add into manifest content
  685. * @return void
  686. */
  687. public function addImageToManifest($file)
  688. {
  689. // Get the file extension
  690. $ext = substr(strrchr($file, '.'), 1);
  691. // Create the correct image XML entry to add to the manifest (this is necessary because ODT format requires that we keep a list of the images in the manifest.xml)
  692. $add = ' <manifest:file-entry manifest:media-type="image/'.$ext.'" manifest:full-path="Pictures/'.$file.'"/>'."\n";
  693. // Append the image to the manifest
  694. $this->manifestXml = str_replace('</manifest:manifest>', $add.'</manifest:manifest>', $this->manifestXml); // we replace the manifest closing tag by the image XML entry + manifest closing tag (this results in appending the data, we do not overwrite anything)
  695. }
  696. /**
  697. * Export the file as attached file by HTTP
  698. *
  699. * @param string $name (optional)
  700. * @throws OdfException
  701. * @return void
  702. */
  703. public function exportAsAttachedFile($name = "")
  704. {
  705. $this->_save();
  706. if (headers_sent($filename, $linenum)) {
  707. throw new OdfException("headers already sent ($filename at $linenum)");
  708. }
  709. if ( $name == "" ) {
  710. $name = md5(uniqid()) . ".odt";
  711. }
  712. header('Content-type: application/vnd.oasis.opendocument.text');
  713. header('Content-Disposition: attachment; filename="'.$name.'"');
  714. header('Content-Length: '.filesize($this->tmpfile));
  715. readfile($this->tmpfile);
  716. }
  717. /**
  718. * Convert the ODT file to PDF and export the file as attached file by HTTP
  719. * Note: you need to have JODConverter and OpenOffice or LibreOffice installed and executable on the same system as where this php script will be executed. You also need to chmod +x odt2pdf.sh
  720. *
  721. * @param string $name Name of ODT file to generate before generating PDF
  722. * @throws OdfException
  723. * @return void
  724. */
  725. public function exportAsAttachedPDF($name = "")
  726. {
  727. global $conf;
  728. if ( $name == "" ) $name = "temp".md5(uniqid());
  729. dol_syslog(get_class($this).'::exportAsAttachedPDF $name='.$name, LOG_DEBUG);
  730. $this->saveToDisk($name);
  731. $execmethod=(empty($conf->global->MAIN_EXEC_USE_POPEN)?1:2); // 1 or 2
  732. // Method 1 sometimes hang the server.
  733. // Export to PDF using LibreOffice
  734. if ($conf->global->MAIN_ODT_AS_PDF == 'libreoffice') {
  735. dol_mkdir($conf->user->dir_temp); // We must be sure the directory exists and is writable
  736. // We delete and recreate a subdir because the soffice may have change pemrissions on it
  737. dol_delete_dir_recursive($conf->user->dir_temp.'/odtaspdf');
  738. dol_mkdir($conf->user->dir_temp.'/odtaspdf');
  739. // Install prerequisites: apt install soffice libreoffice-common libreoffice-writer
  740. // using windows libreoffice that must be in path
  741. // using linux/mac libreoffice that must be in path
  742. // Note PHP Config "fastcgi.impersonate=0" must set to 0 - Default is 1
  743. $command ='soffice --headless -env:UserInstallation=file:\''.$conf->user->dir_temp.'/odtaspdf\' --convert-to pdf --outdir '. escapeshellarg(dirname($name)). " ".escapeshellarg($name);
  744. } elseif (preg_match('/unoconv/', $conf->global->MAIN_ODT_AS_PDF)) {
  745. // If issue with unoconv, see https://github.com/dagwieers/unoconv/issues/87
  746. // MAIN_ODT_AS_PDF should be "sudo -u unoconv /usr/bin/unoconv" and userunoconv must have sudo to be root by adding file /etc/sudoers.d/unoconv with content www-data ALL=(unoconv) NOPASSWD: /usr/bin/unoconv .
  747. // Try this with www-data user: /usr/bin/unoconv -vvvv -f pdf /tmp/document-example.odt
  748. // It must return:
  749. //Verbosity set to level 4
  750. //Using office base path: /usr/lib/libreoffice
  751. //Using office binary path: /usr/lib/libreoffice/program
  752. //DEBUG: Connection type: socket,host=127.0.0.1,port=2002;urp;StarOffice.ComponentContext
  753. //DEBUG: Existing listener not found.
  754. //DEBUG: Launching our own listener using /usr/lib/libreoffice/program/soffice.bin.
  755. //LibreOffice listener successfully started. (pid=9287)
  756. //Input file: /tmp/document-example.odt
  757. //unoconv: file `/tmp/document-example.odt' does not exist.
  758. //unoconv: RuntimeException during import phase:
  759. //Office probably died. Unsupported URL <file:///tmp/document-example.odt>: "type detection failed"
  760. //DEBUG: Terminating LibreOffice instance.
  761. //DEBUG: Waiting for LibreOffice instance to exit
  762. // If it fails:
  763. // - set shell of user to bash instead of nologin.
  764. // - set permission to read/write to user on home directory /var/www so user can create the libreoffice , dconf and .cache dir and files then set permission back
  765. $command = $conf->global->MAIN_ODT_AS_PDF.' '.escapeshellcmd($name);
  766. //$command = '/usr/bin/unoconv -vvv '.escapeshellcmd($name);
  767. } else {
  768. // deprecated old method using odt2pdf.sh (native, jodconverter, ...)
  769. $tmpname=preg_replace('/\.odt/i', '', $name);
  770. if (!empty($conf->global->MAIN_DOL_SCRIPTS_ROOT)) {
  771. $command = $conf->global->MAIN_DOL_SCRIPTS_ROOT.'/scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($tmpname).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
  772. } else {
  773. dol_syslog(get_class($this).'::exportAsAttachedPDF is used but the constant MAIN_DOL_SCRIPTS_ROOT with path to script directory was not defined.', LOG_WARNING);
  774. $command = '../../scripts/odt2pdf/odt2pdf.sh '.escapeshellcmd($tmpname).' '.(is_numeric($conf->global->MAIN_ODT_AS_PDF)?'jodconverter':$conf->global->MAIN_ODT_AS_PDF);
  775. }
  776. }
  777. //$dirname=dirname($name);
  778. //$command = DOL_DOCUMENT_ROOT.'/includes/odtphp/odt2pdf.sh '.$name.' '.$dirname;
  779. dol_syslog(get_class($this).'::exportAsAttachedPDF $execmethod='.$execmethod.' Run command='.$command, LOG_DEBUG);
  780. // TODO Use:
  781. // $outputfile = DOL_DATA_ROOT.'/odt2pdf.log';
  782. // $result = $utils->executeCLI($command, $outputfile); and replace test on $execmethod.
  783. // $retval will be $result['result']
  784. // $errorstring will be $result['output']
  785. $retval=0; $output_arr=array();
  786. if ($execmethod == 1) {
  787. exec($command, $output_arr, $retval);
  788. }
  789. if ($execmethod == 2) {
  790. $outputfile = DOL_DATA_ROOT.'/odt2pdf.log';
  791. $ok=0;
  792. $handle = fopen($outputfile, 'w');
  793. if ($handle) {
  794. dol_syslog(get_class($this)."Run command ".$command, LOG_DEBUG);
  795. fwrite($handle, $command."\n");
  796. $handlein = popen($command, 'r');
  797. while (!feof($handlein)) {
  798. $read = fgets($handlein);
  799. fwrite($handle, $read);
  800. $output_arr[]=$read;
  801. }
  802. pclose($handlein);
  803. fclose($handle);
  804. }
  805. if (! empty($conf->global->MAIN_UMASK)) @chmod($outputfile, octdec($conf->global->MAIN_UMASK));
  806. }
  807. if ($retval == 0) {
  808. dol_syslog(get_class($this).'::exportAsAttachedPDF $ret_val='.$retval, LOG_DEBUG);
  809. $filename=''; $linenum=0;
  810. if (php_sapi_name() != 'cli') { // If we are in a web context (not into CLI context)
  811. if (headers_sent($filename, $linenum)) {
  812. throw new OdfException("headers already sent ($filename at $linenum)");
  813. }
  814. if (!empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  815. $name=preg_replace('/\.od(x|t)/i', '', $name);
  816. header('Content-type: application/pdf');
  817. header('Content-Disposition: attachment; filename="'.$name.'.pdf"');
  818. readfile($name.".pdf");
  819. }
  820. }
  821. if (!empty($conf->global->MAIN_ODT_AS_PDF_DEL_SOURCE)) {
  822. unlink($name);
  823. }
  824. } else {
  825. dol_syslog(get_class($this).'::exportAsAttachedPDF $ret_val='.$retval, LOG_DEBUG);
  826. dol_syslog(get_class($this).'::exportAsAttachedPDF $output_arr='.var_export($output_arr, true), LOG_DEBUG);
  827. if ($retval == 126) {
  828. throw new OdfException('Permission execute convert script : ' . $command);
  829. } else {
  830. $errorstring='';
  831. foreach ($output_arr as $line) {
  832. $errorstring.= $line."<br>";
  833. }
  834. throw new OdfException('ODT to PDF convert fail (option MAIN_ODT_AS_PDF is '.$conf->global->MAIN_ODT_AS_PDF.', command was '.$command.', retval='.$retval.') : ' . $errorstring);
  835. }
  836. }
  837. }
  838. /**
  839. * Returns a variable of configuration
  840. *
  841. * @param string $configKey Config key
  842. * @return string The requested variable of configuration
  843. */
  844. public function getConfig($configKey)
  845. {
  846. if (array_key_exists($configKey, $this->config)) {
  847. return $this->config[$configKey];
  848. }
  849. return false;
  850. }
  851. /**
  852. * Returns the temporary working file
  853. *
  854. * @return string le chemin vers le fichier temporaire de travail
  855. */
  856. public function getTmpfile()
  857. {
  858. return $this->tmpfile;
  859. }
  860. /**
  861. * Delete the temporary file when the object is destroyed
  862. */
  863. public function __destruct()
  864. {
  865. if (file_exists($this->tmpfile)) {
  866. unlink($this->tmpfile);
  867. }
  868. if (file_exists($this->tmpdir)) {
  869. $this->_rrmdir($this->tmpdir);
  870. rmdir($this->tmpdir);
  871. }
  872. }
  873. /**
  874. * Empty the temporary working directory recursively
  875. *
  876. * @param string $dir The temporary working directory
  877. * @return void
  878. */
  879. private function _rrmdir($dir)
  880. {
  881. if ($handle = opendir($dir)) {
  882. while (($file = readdir($handle)) !== false) {
  883. if ($file != '.' && $file != '..') {
  884. if (is_dir($dir . '/' . $file)) {
  885. $this->_rrmdir($dir . '/' . $file);
  886. rmdir($dir . '/' . $file);
  887. } else {
  888. unlink($dir . '/' . $file);
  889. }
  890. }
  891. }
  892. closedir($handle);
  893. }
  894. }
  895. /**
  896. * return the value present on odt in [valuename][/valuename]
  897. *
  898. * @param string $valuename Balise in the template
  899. * @return string The value inside the balise
  900. */
  901. public function getvalue($valuename)
  902. {
  903. $searchreg="/\\[".$valuename."\\](.*)\\[\\/".$valuename."\\]/";
  904. $matches = array();
  905. preg_match($searchreg, $this->contentXml, $matches);
  906. $this->contentXml = preg_replace($searchreg, "", $this->contentXml);
  907. return $matches[1];
  908. }
  909. }