PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/core/lib/website.lib.php

http://github.com/Dolibarr/dolibarr
PHP | 1205 lines | 798 code | 147 blank | 260 comment | 152 complexity | 42bb57bdd5a04a10107d8c60db63921c MD5 | raw file
Possible License(s): GPL-2.0, AGPL-3.0, LGPL-2.0, CC-BY-SA-4.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/lib/website.lib.php
  19. * \ingroup website
  20. * \brief Library for website module
  21. */
  22. /**
  23. * Remove PHP code part from a string.
  24. *
  25. * @param string $str String to clean
  26. * @param string $replacewith String to use as replacement
  27. * @return string Result string without php code
  28. * @see dolKeepOnlyPhpCode()
  29. */
  30. function dolStripPhpCode($str, $replacewith = '')
  31. {
  32. $newstr = '';
  33. //split on each opening tag
  34. $parts = explode('<?php', $str);
  35. if (!empty($parts)) {
  36. $i = 0;
  37. foreach ($parts as $part) {
  38. if ($i == 0) { // The first part is never php code
  39. $i++;
  40. $newstr .= $part;
  41. continue;
  42. }
  43. // The second part is the php code. We split on closing tag
  44. $partlings = explode('?>', $part);
  45. if (!empty($partlings)) {
  46. //$phppart = $partlings[0];
  47. //remove content before closing tag
  48. if (count($partlings) > 1) {
  49. $partlings[0] = ''; // Todo why a count > 1 and not >= 1 ?
  50. }
  51. //append to out string
  52. //$newstr .= '<span class="phptag" class="tooltip" title="'.dol_escape_htmltag(dolGetFirstLineOfText($phppart).'...').'">'.$replacewith.'<!-- '.$phppart.' --></span>'.implode('', $partlings);
  53. //$newstr .= '<span>'.$replacewith.'<!-- '.$phppart.' --></span>'.implode('', $partlings);
  54. $newstr .= '<span phptag>'.$replacewith.'</span>'.implode('', $partlings);
  55. //$newstr .= $replacewith.implode('', $partlings);
  56. }
  57. }
  58. }
  59. return $newstr;
  60. }
  61. /**
  62. * Keep only PHP code part from a HTML string page.
  63. *
  64. * @param string $str String to clean
  65. * @return string Result string with php code only
  66. * @see dolStripPhpCode()
  67. */
  68. function dolKeepOnlyPhpCode($str)
  69. {
  70. $newstr = '';
  71. //split on each opening tag
  72. $parts = explode('<?php', $str);
  73. if (!empty($parts)) {
  74. $i = 0;
  75. foreach ($parts as $part) {
  76. if ($i == 0) { // The first part is never php code
  77. $i++;
  78. continue;
  79. }
  80. $newstr .= '<?php';
  81. //split on closing tag
  82. $partlings = explode('?>', $part, 2);
  83. if (!empty($partlings)) {
  84. $newstr .= $partlings[0].'?>';
  85. } else {
  86. $newstr .= $part.'?>';
  87. }
  88. }
  89. }
  90. return $newstr;
  91. }
  92. /**
  93. * Convert a page content to have correct links (based on DOL_URL_ROOT) into an html content. It replaces also dynamic content with '...php...'
  94. * Used to ouput the page on the Preview from backoffice.
  95. *
  96. * @param Website $website Web site object
  97. * @param string $content Content to replace
  98. * @param int $removephppart 0=Replace PHP sections with a PHP badge. 1=Remove completely PHP sections.
  99. * @param string $contenttype Content type
  100. * @param int $containerid Contenair id
  101. * @return boolean True if OK
  102. * @see dolWebsiteOutput() for function used to replace content in a web server context
  103. */
  104. function dolWebsiteReplacementOfLinks($website, $content, $removephppart = 0, $contenttype = 'html', $containerid = '')
  105. {
  106. $nbrep = 0;
  107. dol_syslog('dolWebsiteReplacementOfLinks start (contenttype='.$contenttype." containerid=".$containerid." USEDOLIBARREDITOR=".(defined('USEDOLIBARREDITOR') ? '1' : '')." USEDOLIBARRSERVER=".(defined('USEDOLIBARRSERVER') ? '1' : '').')', LOG_DEBUG);
  108. //if ($contenttype == 'html') { print $content;exit; }
  109. // Replace php code. Note $content may come from database and does not contains body tags.
  110. $replacewith = '...php...';
  111. if ($removephppart) {
  112. $replacewith = '';
  113. }
  114. $content = preg_replace('/value="<\?php((?!\?>).)*\?>\n*/ims', 'value="'.$replacewith.'"', $content);
  115. $replacewith = '"callto=#';
  116. if ($removephppart) {
  117. $replacewith = '';
  118. }
  119. $content = preg_replace('/"callto:<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
  120. $replacewith = '"mailto=#';
  121. if ($removephppart) {
  122. $replacewith = '';
  123. }
  124. $content = preg_replace('/"mailto:<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
  125. $replacewith = 'src="php';
  126. if ($removephppart) {
  127. $replacewith = '';
  128. }
  129. $content = preg_replace('/src="<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
  130. $replacewith = 'href="php';
  131. if ($removephppart) {
  132. $replacewith = '';
  133. }
  134. $content = preg_replace('/href="<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
  135. //$replacewith='<span class="phptag">...php...</span>';
  136. $replacewith = '...php...';
  137. if ($removephppart) {
  138. $replacewith = '';
  139. }
  140. //$content = preg_replace('/<\?php((?!\?toremove>).)*\?toremove>\n*/ims', $replacewith, $content);
  141. /*if ($content === null) {
  142. if (preg_last_error() == PREG_JIT_STACKLIMIT_ERROR) $content = 'preg_replace error (when removing php tags) PREG_JIT_STACKLIMIT_ERROR';
  143. }*/
  144. $content = dolStripPhpCode($content, $replacewith);
  145. //var_dump($content);
  146. // Protect the link styles.css.php to any replacement that we make after.
  147. $content = str_replace('href="styles.css.php', 'href="!~!~!~styles.css.php', $content);
  148. $content = str_replace('href="http', 'href="!~!~!~http', $content);
  149. $content = str_replace('href="//', 'href="!~!~!~//', $content);
  150. $content = str_replace('src="viewimage.php', 'src="!~!~!~/viewimage.php', $content);
  151. $content = str_replace('src="/viewimage.php', 'src="!~!~!~/viewimage.php', $content);
  152. $content = str_replace('src="'.DOL_URL_ROOT.'/viewimage.php', 'src="!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content);
  153. $content = str_replace('href="document.php', 'href="!~!~!~/document.php', $content);
  154. $content = str_replace('href="/document.php', 'href="!~!~!~/document.php', $content);
  155. $content = str_replace('href="'.DOL_URL_ROOT.'/document.php', 'href="!~!~!~'.DOL_URL_ROOT.'/document.php', $content);
  156. // Replace relative link '/' with dolibarr URL
  157. $content = preg_replace('/(href=")\/(#[^\"<>]*)?\"/', '\1!~!~!~'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageid='.$website->fk_default_home.'\2"', $content, -1, $nbrep);
  158. // Replace relative link /xxx.php#aaa or /xxx.php with dolibarr URL (we discard param ?...)
  159. $content = preg_replace('/(href=")\/?([^:\"\!]*)\.php(#[^\"<>]*)?\"/', '\1!~!~!~'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageref=\2\3"', $content, -1, $nbrep);
  160. // Replace relative link /xxx.php?a=b&c=d#aaa or /xxx.php?a=b&c=d with dolibarr URL
  161. $content = preg_replace('/(href=")\/?([^:\"\!]*)\.php\?([^#\"<>]*)(#[^\"<>]*)?\"/', '\1!~!~!~'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageref=\2&\3\4"', $content, -1, $nbrep);
  162. // Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
  163. $content = preg_replace('/url\((["\']?)\/?medias\//', 'url(\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
  164. $content = preg_replace('/data-slide-bg=(["\']?)\/?medias\//', 'data-slide-bg=\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
  165. // <img src="medias/...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
  166. // <img src="...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
  167. $content = preg_replace('/(<img[^>]*src=")\/?medias\//', '\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
  168. // <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
  169. $content = preg_replace('/(<img[^>]*src=")\/?([^:\"\!]+)\"/', '\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=\2"', $content, -1, $nbrep);
  170. // <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
  171. $content = preg_replace('/(<img[^>]*src=")(\/?viewimage\.php)/', '\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content, -1, $nbrep);
  172. // action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
  173. $content = preg_replace('/(action=")\/?([^:\"]*)(\.php\")/', '\1!~!~!~'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
  174. // Fix relative link /document.php with correct URL after the DOL_URL_ROOT: ...href="/document.php?modulepart="
  175. $content = preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  176. $content = preg_replace('/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  177. // Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT: ...href="/viewimage.php?modulepart="
  178. $content = preg_replace('/(url\(")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  179. // Fix relative URL
  180. $content = str_replace('src="!~!~!~/viewimage.php', 'src="!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content);
  181. $content = str_replace('href="!~!~!~/document.php', 'href="!~!~!~'.DOL_URL_ROOT.'/document.php', $content);
  182. // Remove the protection tag !~!~!~
  183. $content = str_replace('!~!~!~', '', $content);
  184. dol_syslog('dolWebsiteReplacementOfLinks end', LOG_DEBUG);
  185. //if ($contenttype == 'html') { print $content;exit; }
  186. return $content;
  187. }
  188. /**
  189. * Render a string of an HTML content and output it.
  190. * Used to ouput the page when viewed from a server (Dolibarr or Apache).
  191. *
  192. * @param string $content Content string
  193. * @param string $contenttype Content type
  194. * @param int $containerid Contenair id
  195. * @return void
  196. * @see dolWebsiteReplacementOfLinks() for function used to replace content in the backoffice context.
  197. */
  198. function dolWebsiteOutput($content, $contenttype = 'html', $containerid = '')
  199. {
  200. global $db, $langs, $conf, $user;
  201. global $dolibarr_main_url_root, $dolibarr_main_data_root;
  202. global $website;
  203. global $includehtmlcontentopened;
  204. $nbrep = 0;
  205. dol_syslog("dolWebsiteOutput start - contenttype=".$contenttype." containerid=".$containerid." USEDOLIBARREDITOR=".(defined('USEDOLIBARREDITOR') ? '1' : '')." USEDOLIBARRSERVER=".(defined('USEDOLIBARRSERVER') ? '1' : '').' includehtmlcontentopened='.$includehtmlcontentopened);
  206. //print $containerid.' '.$content;
  207. // Define $urlwithroot
  208. $urlwithouturlroot = preg_replace('/'.preg_quote(DOL_URL_ROOT, '/').'$/i', '', trim($dolibarr_main_url_root));
  209. $urlwithroot = $urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  210. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  211. if (defined('USEDOLIBARREDITOR')) { // REPLACEMENT OF LINKS When page called from Dolibarr editor
  212. // We remove the <head> part of content
  213. if ($contenttype == 'html') {
  214. $content = preg_replace('/<head>.*<\/head>/ims', '', $content);
  215. $content = preg_replace('/^.*<body(\s[^>]*)*>/ims', '', $content);
  216. $content = preg_replace('/<\/body(\s[^>]*)*>.*$/ims', '', $content);
  217. }
  218. } elseif (defined('USEDOLIBARRSERVER')) { // REPLACEMENT OF LINKS When page called from Dolibarr server
  219. $content = str_replace('<link rel="stylesheet" href="/styles.css', '<link rel="stylesheet" href="styles.css', $content);
  220. // Protect the link styles.css.php to any replacement that we make after.
  221. $content = str_replace('href="styles.css.php', 'href="!~!~!~styles.css.php', $content);
  222. $content = str_replace('href="http', 'href="!~!~!~http', $content);
  223. $content = str_replace('href="//', 'href="!~!~!~//', $content);
  224. $content = str_replace(array('src="viewimage.php', 'src="/viewimage.php'), 'src="!~!~!~/viewimage.php', $content);
  225. $content = str_replace('src="'.DOL_URL_ROOT.'/viewimage.php', 'src="!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content);
  226. $content = str_replace(array('href="document.php', 'href="/document.php'), 'href="!~!~!~/document.php', $content);
  227. $content = str_replace('href="'.DOL_URL_ROOT.'/document.php', 'href="!~!~!~'.DOL_URL_ROOT.'/document.php', $content);
  228. // Replace relative link / with dolibarr URL: ...href="/"...
  229. $content = preg_replace('/(href=")\/\"/', '\1!~!~!~'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'"', $content, -1, $nbrep);
  230. // Replace relative link /xxx.php#aaa or /xxx.php with dolibarr URL: ...href="....php" (we discard param ?...)
  231. $content = preg_replace('/(href=")\/?([^:\"\!]*)\.php(#[^\"<>]*)?\"/', '\1!~!~!~'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2\3"', $content, -1, $nbrep);
  232. // Replace relative link /xxx.php?a=b&c=d#aaa or /xxx.php?a=b&c=d with dolibarr URL
  233. // Warning: we may replace twice if href="..." was inside an include (dolWebsiteOutput called by include and the by final page), that's why
  234. // at end we replace the '!~!~!~' only if we are in final parent page.
  235. $content = preg_replace('/(href=")\/?([^:\"\!]*)\.php\?([^#\"<>]*)(#[^\"<>]*)?\"/', '\1!~!~!~'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2&\3\4"', $content, -1, $nbrep);
  236. // Replace relative link without .php like /xxx#aaa or /xxx with dolibarr URL: ...href="....php"
  237. $content = preg_replace('/(href=")\/?([a-zA-Z0-9\-_#]+)(\"|\?)/', '\1!~!~!~'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2\3', $content, -1, $nbrep);
  238. // Fix relative link /document.php with correct URL after the DOL_URL_ROOT: href="/document.php?modulepart=" => href="/dolibarr/document.php?modulepart="
  239. $content = preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  240. $content = preg_replace('/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  241. // Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT: href="/viewimage.php?modulepart=" => href="/dolibarr/viewimage.php?modulepart="
  242. $content = preg_replace('/(href=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  243. $content = preg_replace('/(src=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  244. $content = preg_replace('/(url\(")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1!~!~!~'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
  245. // Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
  246. $content = preg_replace('/url\((["\']?)\/?medias\//', 'url(\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
  247. $content = preg_replace('/data-slide-bg=(["\']?)\/?medias\//', 'data-slide-bg=\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
  248. // <img src="medias/...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
  249. // <img src="...image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
  250. $content = preg_replace('/(<img[^>]*src=")\/?medias\//', '\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
  251. // <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
  252. $content = preg_replace('/(<img[^>]*src=")\/?([^:\"\!]+)\"/', '\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=\2"', $content, -1, $nbrep);
  253. // <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
  254. $content = preg_replace('/(<img[^>]*src=")(\/?viewimage\.php)/', '\1!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content, -1, $nbrep);
  255. // action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
  256. $content = preg_replace('/(action=")\/?([^:\"]*)(\.php\")/', '\1!~!~!~'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
  257. // Fix relative URL
  258. $content = str_replace('src="!~!~!~/viewimage.php', 'src="!~!~!~'.DOL_URL_ROOT.'/viewimage.php', $content);
  259. $content = str_replace('href="!~!~!~/document.php', 'href="!~!~!~'.DOL_URL_ROOT.'/document.php', $content);
  260. // Remove the protection tag !~!~!~, but only if this is the parent page and not an include
  261. if (empty($includehtmlcontentopened)) {
  262. $content = str_replace('!~!~!~', '', $content);
  263. }
  264. } else // REPLACEMENT OF LINKS When page called from virtual host web server
  265. {
  266. $symlinktomediaexists = 1;
  267. if ($website->virtualhost) {
  268. $content = preg_replace('/^(<link[^>]*rel="canonical" href=")\//m', '\1'.$website->virtualhost.'/', $content, -1, $nbrep);
  269. }
  270. //print 'rrrrrrrrr'.$website->virtualhost.$content;
  271. // Make a change into HTML code to allow to include images from medias directory correct with direct link for virtual server
  272. // <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
  273. // become
  274. // <img alt="" src="'.$urlwithroot.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
  275. if (!$symlinktomediaexists) {
  276. // <img src="image.png... => <img src="medias/image.png...
  277. $content = preg_replace('/(<img[^>]*src=")\/?image\//', '\1/wrapper.php?modulepart=medias&file=medias/image/', $content, -1, $nbrep);
  278. $content = preg_replace('/(url\(["\']?)\/?image\//', '\1/wrapper.php?modulepart=medias&file=medias/image/', $content, -1, $nbrep);
  279. $content = preg_replace('/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
  280. $content = preg_replace('/(<a[^>]*href=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
  281. $content = preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
  282. $content = preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
  283. $content = preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
  284. $content = preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
  285. $content = preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
  286. $content = preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)hashp=([^\)]*)(["\']?\))/', '\1/wrapper.php\2hashp\3\4', $content, -1, $nbrep);
  287. $content = preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=mycompany\3file=\4\5', $content, -1, $nbrep);
  288. // If some links to documents or viewimage remains, we replace with wrapper
  289. $content = preg_replace('/(<img[^>]*src=")\/?viewimage\.php/', '\1/wrapper.php', $content, -1, $nbrep);
  290. $content = preg_replace('/(<a[^>]*href=")\/?documents\.php/', '\1/wrapper.php', $content, -1, $nbrep);
  291. } else {
  292. // <img src="image.png... => <img src="medias/image.png...
  293. $content = preg_replace('/(<img[^>]*src=")\/?image\//', '\1/medias/image/', $content, -1, $nbrep);
  294. $content = preg_replace('/(url\(["\']?)\/?image\//', '\1/medias/image/', $content, -1, $nbrep);
  295. $content = preg_replace('/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/medias/\4\5', $content, -1, $nbrep);
  296. $content = preg_replace('/(<a[^>]*href=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/medias/\4\5', $content, -1, $nbrep);
  297. $content = preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/medias/\4\5', $content, -1, $nbrep);
  298. $content = preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/medias/\4\5', $content, -1, $nbrep);
  299. $content = preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1/medias/\4\5', $content, -1, $nbrep);
  300. $content = preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
  301. $content = preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
  302. $content = preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)hashp=([^\)]*)(["\']?\))/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
  303. $content = preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=mycompany\3file=\4\5', $content, -1, $nbrep);
  304. // If some links to documents or viewimage remains, we replace with wrapper
  305. $content = preg_replace('/(<img[^>]*src=")\/?viewimage\.php/', '\1/wrapper.php', $content, -1, $nbrep);
  306. $content = preg_replace('/(<a[^>]*href=")\/?document\.php/', '\1/wrapper.php', $content, -1, $nbrep);
  307. }
  308. }
  309. if (!defined('USEDOLIBARREDITOR')) {
  310. $content = str_replace(' contenteditable="true"', ' contenteditable="false"', $content);
  311. }
  312. if (!empty($conf->global->WEBSITE_ADD_CSS_TO_BODY)) {
  313. $content = str_replace('<body id="bodywebsite" class="bodywebsite', '<body id="bodywebsite" class="bodywebsite '.$conf->global->WEBSITE_ADD_CSS_TO_BODY, $content);
  314. }
  315. dol_syslog("dolWebsiteOutput end");
  316. print $content;
  317. }
  318. /**
  319. * Format img tags to introduce viewimage on img src.
  320. *
  321. * @param string $content Content string
  322. * @return void
  323. * @see dolWebsiteOutput()
  324. */
  325. /*
  326. function dolWebsiteSaveContent($content)
  327. {
  328. global $db, $langs, $conf, $user;
  329. global $dolibarr_main_url_root, $dolibarr_main_data_root;
  330. //dol_syslog("dolWebsiteSaveContent start (mode=".(defined('USEDOLIBARRSERVER')?'USEDOLIBARRSERVER':'').')');
  331. // Define $urlwithroot
  332. $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  333. $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
  334. //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
  335. //$content = preg_replace('/(<img.*src=")(?!(http|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
  336. return $content;
  337. }
  338. */
  339. /**
  340. * Make a redirect to another container.
  341. *
  342. * @param string $containerref Ref of container to redirect to (Example: 'mypage' or 'mypage.php').
  343. * @param string $containeraliasalt Ref of alternative aliases to redirect to.
  344. * @param int $containerid Id of container.
  345. * @param int $permanent 0=Use temporary redirect 302, 1=Use permanent redirect 301
  346. * @return void
  347. */
  348. function redirectToContainer($containerref, $containeraliasalt = '', $containerid = 0, $permanent = 0)
  349. {
  350. global $db, $website;
  351. $newurl = '';
  352. $result = 0;
  353. // We make redirect using the alternative alias, we must find the real $containerref
  354. if ($containeraliasalt) {
  355. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  356. $tmpwebsitepage = new WebsitePage($db);
  357. $result = $tmpwebsitepage->fetch(0, $website->id, '', $containeraliasalt);
  358. if ($result > 0) {
  359. $containerref = $tmpwebsitepage->pageurl;
  360. } else {
  361. print "Error, page contains a redirect to the alternative alias '".$containeraliasalt."' that does not exists in web site (".$website->id." / ".$website->ref.")";
  362. exit;
  363. }
  364. }
  365. if (defined('USEDOLIBARREDITOR')) {
  366. /*print '<div class="margintoponly marginleftonly">';
  367. print "This page contains dynamic code that make a redirect to '".$containerref."' in your current context. Redirect has been canceled as it is not supported in edition mode.";
  368. print '</div>';*/
  369. $text = "This page contains dynamic code that make a redirect to '".$containerref."' in your current context. Redirect has been canceled as it is not supported in edition mode.";
  370. setEventMessages($text, null, 'warnings', 'WEBSITEREDIRECTDISABLED'.$containerref);
  371. return;
  372. }
  373. if (defined('USEDOLIBARRSERVER')) { // When page called from Dolibarr server
  374. // Check new container exists
  375. if (!$containeraliasalt) { // If containeraliasalt set, we already did the test
  376. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  377. $tmpwebsitepage = new WebsitePage($db);
  378. $result = $tmpwebsitepage->fetch(0, $website->id, $containerref);
  379. unset($tmpwebsitepage);
  380. }
  381. if ($result > 0) {
  382. $currenturi = $_SERVER["REQUEST_URI"];
  383. $regtmp = array();
  384. if (preg_match('/&pageref=([^&]+)/', $currenturi, $regtmp)) {
  385. if ($regtmp[0] == $containerref) {
  386. print "Error, page with uri '.$currenturi.' try a redirect to the same alias page '".$containerref."' in web site '".$website->ref."'";
  387. exit;
  388. } else {
  389. $newurl = preg_replace('/&pageref=([^&]+)/', '&pageref='.$containerref, $currenturi);
  390. }
  391. } else {
  392. $newurl = $currenturi.'&pageref='.urlencode($containerref);
  393. }
  394. }
  395. } else // When page called from virtual host server
  396. {
  397. $newurl = '/'.$containerref.'.php';
  398. }
  399. if ($newurl) {
  400. if ($permanent) {
  401. header("Status: 301 Moved Permanently", false, 301);
  402. }
  403. header("Location: ".$newurl);
  404. exit;
  405. } else {
  406. print "Error, page contains a redirect to the alias page '".$containerref."' that does not exists in web site (".$website->id." / ".$website->ref.")";
  407. exit;
  408. }
  409. }
  410. /**
  411. * Clean an HTML page to report only content, so we can include it into another page.
  412. * It outputs content of file sanitized from html and body part.
  413. *
  414. * @param string $containerref Path to file to include (must be a page from website root. Example: 'mypage.php' means 'mywebsite/mypage.php')
  415. * @return void
  416. */
  417. function includeContainer($containerref)
  418. {
  419. global $conf, $db, $hookmanager, $langs, $mysoc, $user, $website, $websitepage, $weblangs; // Very important. Required to have var available when running included containers.
  420. global $includehtmlcontentopened;
  421. global $websitekey, $websitepagefile;
  422. $MAXLEVEL = 20;
  423. if (!preg_match('/\.php$/i', $containerref)) {
  424. $containerref .= '.php';
  425. }
  426. $fullpathfile = DOL_DATA_ROOT.'/website/'.$websitekey.'/'.$containerref;
  427. if (empty($includehtmlcontentopened)) {
  428. $includehtmlcontentopened = 0;
  429. }
  430. $includehtmlcontentopened++;
  431. if ($includehtmlcontentopened > $MAXLEVEL) {
  432. print 'ERROR: RECURSIVE CONTENT LEVEL. Depth of recursive call is more than the limit of '.((int) $MAXLEVEL).".\n";
  433. return;
  434. }
  435. //dol_syslog("Include container ".$containerref.' includehtmlcontentopened='.$includehtmlcontentopened);
  436. // file_get_contents is not possible. We must execute code with include
  437. //$content = file_get_contents($fullpathfile);
  438. //print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/
  439. ob_start();
  440. $res = include $fullpathfile; // Include because we want to execute code content
  441. $tmpoutput = ob_get_contents();
  442. ob_end_clean();
  443. print "\n".'<!-- include '.$websitekey.'/'.$containerref.(is_object($websitepage) ? ' parent id='.$websitepage->id : '').' level = '.$includehtmlcontentopened.' -->'."\n";
  444. print preg_replace(array('/^.*<body[^>]*>/ims', '/<\/body>.*$/ims'), array('', ''), $tmpoutput);
  445. if (!$res) {
  446. print 'ERROR: FAILED TO INCLUDE PAGE '.$containerref.".\n";
  447. }
  448. $includehtmlcontentopened--;
  449. }
  450. /**
  451. * Return HTML content to add structured data for an article, news or Blog Post. Use the json-ld format.
  452. * Example:
  453. * <?php getStructureData('blogpost'); ?>
  454. * <?php getStructureData('software', array('name'=>'Name', 'os'=>'Windows', 'price'=>10)); ?>
  455. *
  456. * @param string $type 'blogpost', 'product', 'software', 'organization', 'qa', ...
  457. * @param array $data Array of data parameters for structured data
  458. * @return string HTML content
  459. */
  460. function getStructuredData($type, $data = array())
  461. {
  462. global $conf, $db, $hookmanager, $langs, $mysoc, $user, $website, $websitepage, $weblangs, $pagelangs; // Very important. Required to have var available when running inluded containers.
  463. $type = strtolower($type);
  464. if ($type == 'software') {
  465. $ret = '<!-- Add structured data for entry in a software annuary -->'."\n";
  466. $ret .= '<script type="application/ld+json">'."\n";
  467. $ret .= '{
  468. "@context": "https://schema.org",
  469. "@type": "SoftwareApplication",
  470. "name": "'.dol_escape_json($data['name']).'",
  471. "operatingSystem": "'.dol_escape_json($data['os']).'",
  472. "applicationCategory": "https://schema.org/'.dol_escape_json($data['applicationCategory']).'",';
  473. if (!empty($data['ratingcount'])) {
  474. $ret .= '
  475. "aggregateRating": {
  476. "@type": "AggregateRating",
  477. "ratingValue": "'.dol_escape_json($data['ratingvalue']).'",
  478. "ratingCount": "'.dol_escape_json($data['ratingcount']).'"
  479. },';
  480. }
  481. $ret .= '
  482. "offers": {
  483. "@type": "Offer",
  484. "price": "'.dol_escape_json($data['price']).'",
  485. "priceCurrency": "'.dol_escape_json($data['currency'] ? $data['currency'] : $conf->currency).'"
  486. }
  487. }'."\n";
  488. $ret .= '</script>'."\n";
  489. } elseif ($type == 'organization') {
  490. $companyname = $mysoc->name;
  491. $url = $mysoc->url;
  492. $ret = '<!-- Add structured data for organization -->'."\n";
  493. $ret .= '<script type="application/ld+json">'."\n";
  494. $ret .= '{
  495. "@context": "https://schema.org",
  496. "@type": "Organization",
  497. "name": "'.dol_escape_json($data['name'] ? $data['name'] : $companyname).'",
  498. "url": "'.dol_escape_json($data['url'] ? $data['url'] : $url).'",
  499. "logo": "'.($data['logo'] ? dol_escape_json($data['logo']) : '/wrapper.php?modulepart=mycompany&file=logos%2F'.urlencode($mysoc->logo)).'",
  500. "contactPoint": {
  501. "@type": "ContactPoint",
  502. "contactType": "Contact",
  503. "email": "'.dol_escape_json($data['email'] ? $data['email'] : $mysoc->email).'"
  504. }'."\n";
  505. if (is_array($mysoc->socialnetworks) && count($mysoc->socialnetworks) > 0) {
  506. $ret .= ",\n";
  507. $ret .= '"sameAs": [';
  508. $i = 0;
  509. foreach ($mysoc->socialnetworks as $key => $value) {
  510. if ($key == 'linkedin') {
  511. $ret .= '"https://www.'.$key.'.com/company/'.dol_escape_json($value).'"';
  512. } elseif ($key == 'youtube') {
  513. $ret .= '"https://www.'.$key.'.com/user/'.dol_escape_json($value).'"';
  514. } else {
  515. $ret .= '"https://www.'.$key.'.com/'.dol_escape_json($value).'"';
  516. }
  517. $i++;
  518. if ($i < count($mysoc->socialnetworks)) {
  519. $ret .= ', ';
  520. }
  521. }
  522. $ret .= ']'."\n";
  523. }
  524. $ret .= '}'."\n";
  525. $ret .= '</script>'."\n";
  526. } elseif ($type == 'blogpost') {
  527. if (!empty($websitepage->author_alias)) {
  528. //include_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  529. //$tmpuser = new User($db);
  530. //$restmpuser = $tmpuser->fetch($websitepage->fk_user_creat);
  531. $pageurl = $websitepage->pageurl;
  532. $title = $websitepage->title;
  533. $image = $websitepage->image;
  534. $companyname = $mysoc->name;
  535. $description = $websitepage->description;
  536. $pageurl = str_replace('__WEBSITE_KEY__', $website->ref, $pageurl);
  537. $title = str_replace('__WEBSITE_KEY__', $website->ref, $title);
  538. $image = '/medias'.(preg_match('/^\//', $image) ? '' : '/').str_replace('__WEBSITE_KEY__', $website->ref, $image);
  539. $companyname = str_replace('__WEBSITE_KEY__', $website->ref, $companyname);
  540. $description = str_replace('__WEBSITE_KEY__', $website->ref, $description);
  541. $ret = '<!-- Add structured data for blog post -->'."\n";
  542. $ret .= '<script type="application/ld+json">'."\n";
  543. $ret .= '{
  544. "@context": "https://schema.org",
  545. "@type": "NewsArticle",
  546. "mainEntityOfPage": {
  547. "@type": "WebPage",
  548. "@id": "'.dol_escape_json($pageurl).'"
  549. },
  550. "headline": "'.dol_escape_json($title).'",
  551. "image": [
  552. "'.dol_escape_json($image).'"
  553. ],
  554. "dateCreated": "'.dol_print_date($websitepage->date_creation, 'dayhourrfc').'",
  555. "datePublished": "'.dol_print_date($websitepage->date_creation, 'dayhourrfc').'",
  556. "dateModified": "'.dol_print_date($websitepage->date_modification, 'dayhourrfc').'",
  557. "author": {
  558. "@type": "Person",
  559. "name": "'.dol_escape_json($websitepage->author_alias).'"
  560. },
  561. "publisher": {
  562. "@type": "Organization",
  563. "name": "'.dol_escape_json($companyname).'",
  564. "logo": {
  565. "@type": "ImageObject",
  566. "url": "/wrapper.php?modulepart=mycompany&file=logos%2F'.urlencode($mysoc->logo).'"
  567. }
  568. },'."\n";
  569. if ($websitepage->keywords) {
  570. $ret .= '"keywords": [';
  571. $i = 0;
  572. $arrayofkeywords = explode(',', $websitepage->keywords);
  573. foreach ($arrayofkeywords as $keyword) {
  574. $ret .= '"'.dol_escape_json($keyword).'"';
  575. $i++;
  576. if ($i < count($arrayofkeywords)) {
  577. $ret .= ', ';
  578. }
  579. }
  580. $ret .= '],'."\n";
  581. }
  582. $ret .= '"description": "'.dol_escape_json($description).'"';
  583. $ret .= "\n".'}'."\n";
  584. $ret .= '</script>'."\n";
  585. } else {
  586. $ret .= '<!-- no structured data inserted inline inside blogpost because no author_alias defined -->'."\n";
  587. }
  588. } elseif ($type == 'product') {
  589. $ret = '<!-- Add structured data for product -->'."\n";
  590. $ret .= '<script type="application/ld+json">'."\n";
  591. $ret .= '{
  592. "@context": "https://schema.org/",
  593. "@type": "Product",
  594. "name": "'.dol_escape_json($data['label']).'",
  595. "image": [
  596. "'.dol_escape_json($data['image']).'",
  597. ],
  598. "description": "'.dol_escape_json($data['description']).'",
  599. "sku": "'.dol_escape_json($data['ref']).'",
  600. "brand": {
  601. "@type": "Thing",
  602. "name": "'.dol_escape_json($data['brand']).'"
  603. },
  604. "author": {
  605. "@type": "Person",
  606. "name": "'.dol_escape_json($data['author']).'"
  607. }
  608. },
  609. "offers": {
  610. "@type": "Offer",
  611. "url": "https://example.com/anvil",
  612. "priceCurrency": "'.dol_escape_json($data['currency'] ? $data['currency'] : $conf->currency).'",
  613. "price": "'.dol_escape_json($data['price']).'",
  614. "itemCondition": "https://schema.org/UsedCondition",
  615. "availability": "https://schema.org/InStock",
  616. "seller": {
  617. "@type": "Organization",
  618. "name": "'.dol_escape_json($mysoc->name).'"
  619. }
  620. }
  621. }'."\n";
  622. $ret .= '</script>'."\n";
  623. } elseif ($type == 'qa') {
  624. $ret = '<!-- Add structured data for QA -->'."\n";
  625. $ret .= '<script type="application/ld+json">'."\n";
  626. $ret .= '{
  627. "@context": "https://schema.org/",
  628. "@type": "QAPage",
  629. "mainEntity": {
  630. "@type": "Question",
  631. "name": "'.dol_escape_json($data['name']).'",
  632. "text": "'.dol_escape_json($data['name']).'",
  633. "answerCount": 1,
  634. "author": {
  635. "@type": "Person",
  636. "name": "'.dol_escape_json($data['author']).'"
  637. }
  638. "acceptedAnswer": {
  639. "@type": "Answer",
  640. "text": "'.dol_escape_json(dol_string_nohtmltag(dolStripPhpCode($data['description']))).'",
  641. "author": {
  642. "@type": "Person",
  643. "name": "'.dol_escape_json($data['author']).'"
  644. }
  645. }
  646. }
  647. }'."\n";
  648. $ret .= '</script>'."\n";
  649. }
  650. return $ret;
  651. }
  652. /**
  653. * Return HTML content to add as header card for an article, news or Blog Post or home page.
  654. *
  655. * @param array $params Array of parameters
  656. * @return string HTML content
  657. */
  658. function getSocialNetworkHeaderCards($params = null)
  659. {
  660. global $conf, $db, $hookmanager, $langs, $mysoc, $user, $website, $websitepage, $weblangs; // Very important. Required to have var available when running inluded containers.
  661. $out = '';
  662. if ($website->virtualhost) {
  663. $pageurl = $websitepage->pageurl;
  664. $title = $websitepage->title;
  665. $image = $websitepage->image;
  666. $companyname = $mysoc->name;
  667. $description = $websitepage->description;
  668. $pageurl = str_replace('__WEBSITE_KEY__', $website->ref, $pageurl);
  669. $title = str_replace('__WEBSITE_KEY__', $website->ref, $title);
  670. $image = '/medias'.(preg_match('/^\//', $image) ? '' : '/').str_replace('__WEBSITE_KEY__', $website->ref, $image);
  671. $companyname = str_replace('__WEBSITE_KEY__', $website->ref, $companyname);
  672. $description = str_replace('__WEBSITE_KEY__', $website->ref, $description);
  673. $shortlangcode = '';
  674. if ($websitepage->lang) {
  675. $shortlangcode = substr($websitepage->lang, 0, 2); // en_US or en-US -> en
  676. }
  677. if (empty($shortlangcode)) {
  678. $shortlangcode = substr($website->lang, 0, 2); // en_US or en-US -> en
  679. }
  680. $fullurl = $website->virtualhost.'/'.$websitepage->pageurl.'.php';
  681. $canonicalurl = $website->virtualhost.(($websitepage->id == $website->fk_default_home) ? '/' : (($shortlangcode != substr($website->lang, 0, 2) ? '/'.$shortlangcode : '').'/'.$websitepage->pageurl.'.php'));
  682. $hashtags = trim(join(' #', array_map('trim', explode(',', $websitepage->keywords))));
  683. // Open Graph
  684. $out .= '<meta name="og:type" content="website">'."\n"; // TODO If blogpost, use type article
  685. $out .= '<meta name="og:title" content="'.$websitepage->title.'">'."\n";
  686. if ($websitepage->image) {
  687. $out .= '<meta name="og:image" content="'.$website->virtualhost.$image.'">'."\n";
  688. }
  689. $out .= '<meta name="og:url" content="'.$canonicalurl.'">'."\n";
  690. // Twitter
  691. $out .= '<meta name="twitter:card" content="summary">'."\n";
  692. if (!empty($params) && !empty($params['twitter_account'])) {
  693. $out .= '<meta name="twitter:site" content="@'.$params['twitter_account'].'">'."\n";
  694. $out .= '<meta name="twitter:creator" content="@'.$params['twitter_account'].'">'."\n";
  695. }
  696. $out .= '<meta name="twitter:title" content="'.$websitepage->title.'">'."\n";
  697. if ($websitepage->description) {
  698. $out .= '<meta name="twitter:description" content="'.$websitepage->description.'">'."\n";
  699. }
  700. if ($websitepage->image) {
  701. $out .= '<meta name="twitter:image" content="'.$website->virtualhost.$image.'">'."\n";
  702. }
  703. //$out .= '<meta name="twitter:domain" content="'.getDomainFromURL($website->virtualhost, 1).'">';
  704. /*
  705. $out .= '<meta name="twitter:app:name:iphone" content="">';
  706. $out .= '<meta name="twitter:app:name:ipad" content="">';
  707. $out .= '<meta name="twitter:app:name:googleplay" content="">';
  708. $out .= '<meta name="twitter:app:url:iphone" content="">';
  709. $out .= '<meta name="twitter:app:url:ipad" content="">';
  710. $out .= '<meta name="twitter:app:url:googleplay" content="">';
  711. $out .= '<meta name="twitter:app:id:iphone" content="">';
  712. $out .= '<meta name="twitter:app:id:ipad" content="">';
  713. $out .= '<meta name="twitter:app:id:googleplay" content="">';
  714. */
  715. }
  716. return $out;
  717. }
  718. /**
  719. * Return HTML content to add structured data for an article, news or Blog Post.
  720. *
  721. * @return string HTML content
  722. */
  723. function getSocialNetworkSharingLinks()
  724. {
  725. global $conf, $db, $hookmanager, $langs, $mysoc, $user, $website, $websitepage, $weblangs; // Very important. Required to have var available when running inluded containers.
  726. $out = '<!-- section for social network sharing of page -->'."\n";
  727. if ($website->virtualhost) {
  728. $fullurl = $website->virtualhost.'/'.$websitepage->pageurl.'.php';
  729. $hashtags = trim(join(' #', array_map('trim', explode(',', $websitepage->keywords))));
  730. $out .= '<div class="dol-social-share">'."\n";
  731. // Twitter
  732. $out .= '<div class="dol-social-share-tw">'."\n";
  733. $out .= '<a href="https://twitter.com/share" class="twitter-share-button" data-url="'.$fullurl.'" data-text="'.dol_escape_htmltag($websitepage->description).'" data-lang="'.$websitepage->lang.'" data-size="small" data-related="" data-hashtags="'.preg_replace('/^#/', '', $hashtags).'" data-count="horizontal">Tweet</a>';
  734. $out .= '<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?\'http\':\'https\';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+\'://platform.twitter.com/widgets.js\';fjs.parentNode.insertBefore(js,fjs);}}(document, \'script\', \'twitter-wjs\');</script>';
  735. $out .= '</div>'."\n";
  736. // Reddit
  737. $out .= '<div class="dol-social-share-reddit">'."\n";
  738. $out .= '<a href="https://www.reddit.com/submit" target="_blank" onclick="window.location = \'https://www.reddit.com/submit?url='.$fullurl.'\'; return false">';
  739. $out .= '<span class="dol-social-share-reddit-span">Reddit</span>';
  740. $out .= '</a>';
  741. $out .= '</div>'."\n";
  742. // Facebook
  743. $out .= '<div class="dol-social-share-fbl">'."\n";
  744. $out .= '<div id="fb-root"></div>'."\n";
  745. $out .= '<script>(function(d, s, id) {
  746. var js, fjs = d.getElementsByTagName(s)[0];
  747. if (d.getElementById(id)) return;
  748. js = d.createElement(s); js.id = id;
  749. js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0&amp;appId=dolibarr.org";
  750. fjs.parentNode.insertBefore(js, fjs);
  751. }(document, \'script\', \'facebook-jssdk\'));</script>
  752. <fb:like
  753. href="'.$fullurl.'"
  754. layout="button_count"
  755. show_faces="false"
  756. width="90"
  757. colorscheme="light"
  758. share="1"
  759. action="like" ></fb:like>'."\n";
  760. $out .= '</div>'."\n";
  761. $out .= "\n</div>\n";
  762. } else {
  763. $out .= '<!-- virtual host not defined in CMS. No way to add sharing buttons -->'."\n";
  764. }
  765. $out .= '<!-- section end for social network sharing of page -->'."\n";
  766. return $out;
  767. }
  768. /**
  769. * Return list of containers object that match a criteria.
  770. * WARNING: This function can be used by websites.
  771. *
  772. * @param string $type Type of container to search into (Example: '', 'page', 'blogpost', 'page,blogpost', ...)
  773. * @param string $algo Algorithm used for search (Example: 'meta' is searching into meta information like title and description, 'content', 'sitefiles', or any combination 'meta,content,...')
  774. * @param string $searchstring Search string
  775. * @param int $max Max number of answers
  776. * @param string $sortfield Sort Fields
  777. * @param string $sortorder Sort order ('DESC' or 'ASC')
  778. * @param string $langcode Language code ('' or 'en', 'fr', 'es', ...)
  779. * @param array $otherfilters Other filters
  780. * @param int $status 0 or 1, or -1 for both
  781. * @return string HTML content
  782. */
  783. function getPagesFromSearchCriterias($type, $algo, $searchstring, $max = 25, $sortfield = 'date_creation', $sortorder = 'DESC', $langcode = '', $otherfilters = 'null', $status = 1)
  784. {
  785. global $conf, $db, $hookmanager, $langs, $mysoc, $user, $website, $websitepage, $weblangs; // Very important. Required to have var available when running inluded containers.
  786. $error = 0;
  787. $arrayresult = array('code'=>'', 'list'=>array());
  788. if (!is_object($weblangs)) {
  789. $weblangs = $langs;
  790. }
  791. if (empty($searchstring) && empty($type) && empty($langcode) && empty($otherfilters)) {
  792. $error++;
  793. $arrayresult['code'] = 'KO';
  794. $arrayresult['message'] = $weblangs->trans("EmptySearchString");
  795. } elseif ($searchstring && dol_strlen($searchstring) < 2) {
  796. $weblangs->load("errors");
  797. $error++;
  798. $arrayresult['code'] = 'KO';
  799. $arrayresult['message'] = $weblangs->trans("ErrorSearchCriteriaTooSmall");
  800. } else {
  801. $tmparrayoftype = explode(',', $type);
  802. /*foreach ($tmparrayoftype as $tmptype) {
  803. if (!in_array($tmptype, array('', 'page', 'blogpost'))) {
  804. $error++;
  805. $arrayresult['code'] = 'KO';
  806. $arrayresult['message'] = 'Bad value for parameter type';
  807. break;
  808. }
  809. }*/
  810. }
  811. $searchdone = 0;
  812. $found = 0;
  813. if (!$error && (empty($max) || ($found < $max)) && (preg_match('/meta/', $algo) || preg_match('/content/', $algo))) {
  814. $sql = 'SELECT wp.rowid FROM '.MAIN_DB_PREFIX.'website_page as wp';
  815. if (is_array($otherfilters) && !empty($otherfilters['category'])) {
  816. $sql .= ', '.MAIN_DB_PREFIX.'categorie_website_page as cwp';
  817. }
  818. $sql .= " WHERE wp.fk_website = ".((int) $website->id);
  819. if ($status >= 0) {
  820. $sql .= " AND wp.status = ".((int) $status);
  821. }
  822. if ($langcode) {
  823. $sql .= " AND wp.lang ='".$db->escape($langcode)."'";
  824. }
  825. if ($type) {
  826. $tmparrayoftype = explode(',', $type);
  827. $typestring = '';
  828. foreach ($tmparrayoftype as $tmptype) {
  829. $typestring .= ($typestring ? ", " : "")."'".$db->escape(trim($tmptype))."'";
  830. }
  831. $sql .= " AND wp.type_container IN (".$db->sanitize($typestring, 1).")";
  832. }
  833. $sql .= " AND (";
  834. $searchalgo = '';
  835. if (preg_match('/meta/', $algo)) {
  836. $searchalgo .= ($searchalgo ? ' OR ' : '')."wp.title LIKE '%".$db->escapeunderscore($db->escape($searchstring))."%' OR wp.description LIKE '%".$db->escapeunderscore($db->escape($searchstring))."%'";
  837. $searchalgo .= ($searchalgo ? ' OR ' : '')."wp.keywords LIKE '".$db->escapeunderscore($db->escape($searchstring)).",%' OR wp.keywords LIKE '% ".$db->escapeunderscore($db->escape($searchstring))."%'"; // TODO Use a better way to scan keywords
  838. }
  839. if (preg_match('/content/', $algo)) {
  840. $searchalgo .= ($searchalgo ? ' OR ' : '')."wp.content LIKE '%".$db->escapeunderscore($db->escape($searchstring))."%'";
  841. }
  842. $sql .= $searchalgo;
  843. if (is_array($otherfilters) && !empty($otherfilters['category'])) {
  844. $sql .= ' AND cwp.fk_website_page = wp.rowid AND cwp.fk_categorie = '.((int) $otherfilters['category']);
  845. }
  846. $sql .= ")";
  847. $sql .= $db->order($sortfield, $sortorder);
  848. $sql .= $db->plimit($max);
  849. //print $sql;
  850. $resql = $db->query($sql);
  851. if ($resql) {
  852. $i = 0;
  853. while (($obj = $db->fetch_object($resql)) && ($i < $max || $max == 0)) {
  854. if ($obj->rowid > 0) {
  855. $tmpwebsitepage = new WebsitePage($db);
  856. $tmpwebsitepage->fetch($obj->rowid);
  857. if ($tmpwebsitepage->id > 0) {
  858. $arrayresult['list'][$obj->rowid] = $tmpwebsitepage;
  859. }
  860. $found++;
  861. }
  862. $i++;
  863. }
  864. } else {
  865. $error++;
  866. $arrayresult['code'] = $db->lasterrno();
  867. $arrayresult['message'] = $db->lasterror();
  868. }
  869. $searchdone = 1;
  870. }
  871. if (!$error && (empty($max) || ($found < $max)) && (preg_match('/sitefiles/', $algo))) {
  872. global $dolibarr_main_data_root;
  873. $pathofwebsite = $dolibarr_main_data_root.'/website/'.$website->ref;
  874. $filehtmlheader = $pathofwebsite.'/htmlheader.html';
  875. $filecss = $pathofwebsite.'/styles.css.php';
  876. $filejs = $pathofwebsite.'/javascript.js.php';
  877. $filerobot = $pathofwebsite.'/robots.txt';
  878. $filehtaccess = $pathofwebsite.'/.htaccess';
  879. $filemanifestjson = $pathofwebsite.'/manifest.json.php';
  880. $filereadme = $pathofwebsite.'/README.md';
  881. $filecontent = file_get_contents($filehtmlheader);
  882. if ((empty($max) || ($found < $max)) && preg_match('/'.preg_quote($searchstring, '/').'/', $filecontent)) {
  883. $arrayresult['list'][] = array('type'=>'website_htmlheadercontent');
  884. }
  885. $filecontent = file_get_contents($filecss);
  886. if ((empty($max) || ($found < $max)) && preg_match('/'.preg_quote($searchstring, '/').'/', $filecontent)) {
  887. $arrayresult['list'][] = array('type'=>'website_csscontent');
  888. }
  889. $filecontent = file_get_contents($filejs);
  890. if ((empty($max) || ($found < $max)) && preg_match('/'.preg_quote($searchstring, '/').'/', $filecontent)) {
  891. $arrayresult['list'][] = array('type'=>'website_jscontent');
  892. }
  893. $filerobot = file_get_contents($filerobot);
  894. if ((empty($max) || ($found < $max)) && preg_match('/'.preg_quote($searchstring, '/').'/', $filecontent)) {
  895. $arrayresult['list'][] = array('type'=>'website_robotcontent');
  896. }
  897. $searchdone = 1;
  898. }
  899. if (!$error) {
  900. if ($searchdone) {
  901. $arrayresult['code'] = 'OK';
  902. if (empty($arrayresult['list'])) {
  903. $arrayresult['code'] = 'KO';
  904. $arrayresult['message'] = $weblangs->trans("NoRecordFound");
  905. }
  906. } else {
  907. $error++;
  908. $arrayresult['code'] = 'KO';
  909. $arrayresult['message'] = 'No supported algorithm found';
  910. }
  911. }
  912. return $arrayresult;
  913. }
  914. /**
  915. * Download all images found into page content $tmp.
  916. * If $modifylinks is set, links to images will be replace with a link to viewimage wrapper.
  917. *
  918. * @param Website $object Object website
  919. * @param WebsitePage $objectpage Object website page
  920. * @param string $urltograb URL to grab (exemple: http://www.nltechno.com/ or http://www.nltechno.com/dir1/ or http://www.nltechno.com/dir1/mapage1)
  921. * @param string $tmp Content to parse
  922. * @param string $action Var $action
  923. * @param string $modifylinks 0=Do not modify content, 1=Replace links with a link to viewimage
  924. * @param int $grabimages 0=Do not grab images, 1=Grab images
  925. * @param string $grabimagesinto 'root' or 'subpage'
  926. * @return void
  927. */
  928. function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modifylinks = 0, $grabimages = 1, $grabimagesinto = 'subpage')
  929. {
  930. global $conf;
  931. $error = 0;
  932. dol_syslog("Call getAllImages with grabimagesinto=".$grabimagesinto);
  933. $alreadygrabbed = array();
  934. if (preg_match('/\/$/', $urltograb)) {
  935. $urltograb .= '.';
  936. }
  937. $urltograb = dirname($urltograb); // So urltograb is now http://www.nltechno.com or http://www.nltechno.com/dir1
  938. // Search X in "img...src=X"
  939. $regs = array();
  940. preg_match_all('/<img([^\.\/]+)src="([^>"]+)"([^>]*)>/i', $tmp, $regs);
  941. foreach ($regs[0] as $key => $val) {
  942. if (preg_match('/^data:image/i', $regs[2][$key])) {
  943. continue; // We do nothing for such images
  944. }
  945. if (preg_match('/^\//', $regs[2][$key])) {
  946. $urltograbdirrootwithoutslash = getRootURLFromURL($urltograb);
  947. $urltograbbis = $urltograbdirrootwithoutslash.$regs[2][$key]; // We use dirroot
  948. } else {
  949. $urltograbbis = $urltograb.'/'.$regs[2][$key]; // We use dir of grabbed file
  950. }
  951. $linkwithoutdomain = $regs[2][$key];
  952. $dirforimages = '/'.$objectpage->pageurl;
  953. if ($grabimagesinto == 'root') {
  954. $dirforimages = '';
  955. }
  956. // Define $filetosave and $filename
  957. $filetosave = $conf->medias->mu

Large files files are truncated, but you can click here to view the full file