dolibarr  9.0.0
website.lib.php
Go to the documentation of this 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 <http://www.gnu.org/licenses/>.
16  */
17 
36 function dolWebsiteReplacementOfLinks($website, $content, $removephppart=0)
37 {
38  $nbrep = 0;
39 
40  // Replace php code. Note $content may come from database and does not contains body tags.
41  $replacewith='...php...';
42  if ($removephppart) $replacewith='';
43  $content = preg_replace('/value="<\?php((?!\?>).)*\?>\n*/ims', 'value="'.$replacewith.'"', $content);
44 
45  $replacewith='"callto=#';
46  if ($removephppart) $replacewith='';
47  $content = preg_replace('/"callto:<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
48 
49  $replacewith='"mailto=#';
50  if ($removephppart) $replacewith='';
51  $content = preg_replace('/"mailto:<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
52 
53  $replacewith='src="php';
54  if ($removephppart) $replacewith='';
55  $content = preg_replace('/src="<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
56 
57  $replacewith='href="php';
58  if ($removephppart) $replacewith='';
59  $content = preg_replace('/href="<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
60 
61  //$replacewith='<span class="phptag">...php...</span>';
62  $replacewith='<span class="phptag">...php...</span>';
63  if ($removephppart) $replacewith='';
64  //$content = preg_replace('/<\?php((?!\?toremove>).)*\?toremove>\n*/ims', $replacewith, $content);
65  /*if ($content === null) {
66  if (preg_last_error() == PREG_JIT_STACKLIMIT_ERROR) $content = 'preg_replace error (when removing php tags) PREG_JIT_STACKLIMIT_ERROR';
67  }*/
68  $content = dolStripPhpCode($content, $replacewith);
69  //var_dump($content);
70 
71  // Replace relative link / with dolibarr URL
72  $content = preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageid='.$website->fk_default_home.'"', $content, -1, $nbrep);
73  // Replace relative link /xxx.php with dolibarr URL
74  $content = preg_replace('/(href=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
75 
76  // Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
77  $content = preg_replace('/url\((["\']?)medias\//', 'url(\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
78  $content = preg_replace('/data-slide-bg=(["\']?)medias\//', 'data-slide-bg=\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
79 
80  // <img src="medias/image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
81  $content = preg_replace('/(<img[^>]*src=")(medias\/)/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
82  // <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
83  $content = preg_replace('/(<img[^>]*src=")(?!(http|\/?viewimage|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
84  // <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
85  $content = preg_replace('/(<img[^>]*src=")(\/?viewimage\.php)/', '\1'.DOL_URL_ROOT.'/viewimage.php', $content, -1, $nbrep);
86 
87  // action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
88  $content = preg_replace('/(action=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
89 
90  // Fix relative link /document.php with correct URL after the DOL_URL_ROOT: ...href="/document.php?modulepart="
91  $content=preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
92  $content=preg_replace('/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
93 
94  return $content;
95 }
96 
97 
105 function dolStripPhpCode($str, $replacewith='')
106 {
107  $newstr = '';
108 
109  //split on each opening tag
110  $parts = explode('<?php',$str);
111  if (!empty($parts))
112  {
113  $i=0;
114  foreach($parts as $part)
115  {
116  if ($i == 0) // The first part is never php code
117  {
118  $i++;
119  $newstr .= $part;
120  continue;
121  }
122  //split on closing tag
123  $partlings = explode('?>', $part);
124  if (!empty($partlings))
125  {
126  //remove content before closing tag
127  if (count($partlings) > 1) $partlings[0] = '';
128  //append to out string
129  $newstr .= $replacewith.implode('',$partlings);
130  }
131  }
132  }
133  return $newstr;
134 }
135 
136 
145 function dolWebsiteOutput($content)
146 {
147  global $db, $langs, $conf, $user;
148  global $dolibarr_main_url_root, $dolibarr_main_data_root;
149 
150  dol_syslog("dolWebsiteOutput start (USEDOLIBARRSERVER=".(defined('USEDOLIBARRSERVER')?'1':'')." (USEDOLIBARREDITOR=".(defined('USEDOLIBARREDITOR')?'1':'').')');
151 
152  // Define $urlwithroot
153  $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
154  $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
155  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
156 
157  if (defined('USEDOLIBARREDITOR')) // REPLACEMENT OF LINKS When page called from Dolibarr editor
158  {
159  // We remove the <head> part of content
160  $content = preg_replace('/<head>.*<\/head>/ims', '', $content);
161  $content = preg_replace('/^.*<body(\s[^>]*)*>/ims', '', $content);
162  $content = preg_replace('/<\/body(\s[^>]*)*>.*$/ims', '', $content);
163  }
164  elseif (defined('USEDOLIBARRSERVER')) // REPLACEMENT OF LINKS When page called from Dolibarr server
165  {
166  global $website;
167 
168  // Replace relative link / with dolibarr URL: ...href="/"...
169  $content=preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'"', $content, -1, $nbrep);
170  // Replace relative link /xxx.php with dolibarr URL: ...href="....php"
171  $content=preg_replace('/(href=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
172  // Replace relative link /xxx with dolibarr URL: ...href="....php"
173  $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);
174  $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);
175 
176  // Fix relative link /document.php with correct URL after the DOL_URL_ROOT: href="/document.php?modulepart=" => href="/dolibarr/document.php?modulepart="
177  $content=preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
178  $content=preg_replace('/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
179 
180  // Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT: href="/viewimage.php?modulepart=" => href="/dolibarr/viewimage.php?modulepart="
181  $content=preg_replace('/(href=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
182  $content=preg_replace('/(src=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
183 
184  // Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
185  $content=preg_replace('/url\((["\']?)medias\//', 'url(\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
186  $content=preg_replace('/data-slide-bg=(["\']?)medias\//', 'data-slide-bg=\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
187 
188  // <img src="medias/image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
189  $content = preg_replace('/(<img[^>]*src=")(medias\/)/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
190  // <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
191  $content = preg_replace('/(<img[^>]*src=")(?!(http|\/?viewimage|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
192  // <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
193  $content = preg_replace('/(<img[^>]*src=")(\/?viewimage\.php)/', '\1'.DOL_URL_ROOT.'/viewimage.php', $content, -1, $nbrep);
194 
195  // action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
196  $content = preg_replace('/(action=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
197  }
198  else // REPLACEMENT OF LINKS When page called from virtual host
199  {
200  $symlinktomediaexists=1;
201 
202  // Make a change into HTML code to allow to include images from medias directory correct with direct link for virtual server
203  // <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&amp;entity=1&amp;file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
204  // become
205  // <img alt="" src="'.$urlwithroot.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
206  $nbrep=0;
207  if (! $symlinktomediaexists)
208  {
209  $content=preg_replace('/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
210 
211  $content=preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
212  $content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
213  $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
214 
215  $content=preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
216  $content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
217  $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)hashp=([^\)]*)(["\']?\))/', '\1/wrapper.php\2hashp\3\4', $content, -1, $nbrep);
218 
219  $content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=mycompany\3file=\4\5', $content, -1, $nbrep);
220  }
221  else
222  {
223  $content=preg_replace('/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1medias/\4\5', $content, -1, $nbrep);
224 
225  $content=preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1medias/\4\5', $content, -1, $nbrep);
226  $content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1medias/\4\5', $content, -1, $nbrep);
227  $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1medias/\4\5', $content, -1, $nbrep);
228 
229  $content=preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
230  $content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)hashp=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
231  $content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)hashp=([^\)]*)(["\']?\))/', '\1/wrapper.php\2hashp=\3\4', $content, -1, $nbrep);
232 
233  $content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=mycompany\3file=\4\5', $content, -1, $nbrep);
234  }
235  }
236 
237  $content=preg_replace('/ contenteditable="true"/', ' contenteditable="false"', $content, -1, $nbrep);
238 
239  dol_syslog("dolWebsiteOutput end");
240 
241  print $content;
242 }
243 
244 
252 /*
253 function dolWebsiteSaveContent($content)
254 {
255  global $db, $langs, $conf, $user;
256  global $dolibarr_main_url_root, $dolibarr_main_data_root;
257 
258  //dol_syslog("dolWebsiteSaveContent start (mode=".(defined('USEDOLIBARRSERVER')?'USEDOLIBARRSERVER':'').')');
259 
260  // Define $urlwithroot
261  $urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
262  $urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
263  //$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
264 
265  //$content = preg_replace('/(<img.*src=")(?!(http|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
266 
267  return $content;
268 }
269 */
270 
271 
280 function redirectToContainer($containerref, $containeraliasalt='',$containerid=0)
281 {
282  global $db, $website;
283 
284  $newurl = '';
285  $result=0;
286 
287  // We make redirect using the alternative alias, we must find the real $containerref
288  if ($containeraliasalt)
289  {
290  include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
291  $tmpwebsitepage=new WebsitePage($db);
292  $result = $tmpwebsitepage->fetch(0, $website->id, '', $containeraliasalt);
293  if ($result > 0)
294  {
295  $containerref = $tmpwebsitepage->pageurl;
296  }
297  else
298  {
299  print "Error, page contains a redirect to the alternative alias '".$containeraliasalt."' that does not exists in web site (".$website->id." / ".$website->ref.")";
300  exit;
301  }
302  }
303 
304  if (defined('USEDOLIBARRSERVER')) // When page called from Dolibarr server
305  {
306  // Check new container exists
307  if (! $containeraliasalt) // If containeraliasalt set, we already did the test
308  {
309  include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
310  $tmpwebsitepage=new WebsitePage($db);
311  $result = $tmpwebsitepage->fetch(0, $website->id, $containerref);
312  unset($tmpwebsitepage);
313  }
314  if ($result > 0)
315  {
316  $currenturi = $_SERVER["REQUEST_URI"];
317  if (preg_match('/&pageref=([^&]+)/', $currenturi, $regtmp))
318  {
319  if ($regtmp[0] == $containerref)
320  {
321  print "Error, page with uri '.$currenturi.' try a redirect to the same alias page '".$containerref."' in web site '".$website->ref."'";
322  exit;
323  }
324  else
325  {
326  $newurl = preg_replace('/&pageref=([^&]+)/', '&pageref='.$containerref, $currenturi);
327  }
328  }
329  else
330  {
331  $newurl = $currenturi.'&pageref='.urlencode($containerref);
332  }
333  }
334  }
335  else // When page called from virtual host server
336  {
337  $newurl = '/'.$containerref.'.php';
338  }
339 
340  if ($newurl)
341  {
342  header("Location: ".$newurl);
343  exit;
344  }
345  else
346  {
347  print "Error, page contains a redirect to the alias page '".$containerref."' that does not exists in web site (".$website->id." / ".$website->ref.")";
348  exit;
349  }
350 }
351 
352 
360 function includeContainer($containerref)
361 {
362  global $conf, $db, $hookmanager, $langs, $mysoc, $user, $website, $websitepage, $weblangs; // Very important. Required to have var available when running inluded containers.
363  global $includehtmlcontentopened;
364  global $websitekey, $websitepagefile;
365 
366  $MAXLEVEL=20;
367 
368  if (! preg_match('/\.php$/i', $containerref)) $containerref.='.php';
369 
370  $fullpathfile=DOL_DATA_ROOT.'/website/'.$websitekey.'/'.$containerref;
371 
372  if (empty($includehtmlcontentopened)) $includehtmlcontentopened=0;
373  $includehtmlcontentopened++;
374  if ($includehtmlcontentopened > $MAXLEVEL)
375  {
376  print 'ERROR: RECURSIVE CONTENT LEVEL. Depth of recursive call is more than the limit of '.$MAXLEVEL.".\n";
377  return;
378  }
379  // file_get_contents is not possible. We must execute code with include
380  //$content = file_get_contents($fullpathfile);
381  //print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/
382 
383  ob_start();
384  $res = include $fullpathfile; // Include because we want to execute code content
385  $tmpoutput = ob_get_contents();
386  ob_end_clean();
387 
388  print "\n".'<!-- include '.$fullpathfile.' level = '.$includehtmlcontentopened.' -->'."\n";
389  print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $tmpoutput);
390 
391  if (! $res)
392  {
393  print 'ERROR: FAILED TO INCLUDE PAGE '.$containerref.".\n";
394  }
395 
396  $includehtmlcontentopened--;
397 }
398 
399 
400 
415 function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modifylinks=0, $grabimages=1, $grabimagesinto='subpage')
416 {
417  global $conf;
418 
419  $error=0;
420 
421  dol_syslog("Call getAllImages with grabimagesinto=".$grabimagesinto);
422 
423  $alreadygrabbed=array();
424 
425  if (preg_match('/\/$/', $urltograb)) $urltograb.='.';
426  $urltograb = dirname($urltograb); // So urltograb is now http://www.nltechno.com or http://www.nltechno.com/dir1
427 
428  // Search X in "img...src=X"
429  preg_match_all('/<img([^\.\/]+)src="([^>"]+)"([^>]*)>/i', $tmp, $regs);
430 
431  foreach ($regs[0] as $key => $val)
432  {
433  if (preg_match('/^data:image/i', $regs[2][$key])) continue; // We do nothing for such images
434 
435  if (preg_match('/^\//', $regs[2][$key]))
436  {
437  $urltograbdirrootwithoutslash = getRootURLFromURL($urltograb);
438  $urltograbbis = $urltograbdirrootwithoutslash.$regs[2][$key]; // We use dirroot
439  }
440  else
441  {
442  $urltograbbis = $urltograb.'/'.$regs[2][$key]; // We use dir of grabbed file
443  }
444 
445  $linkwithoutdomain = $regs[2][$key];
446  $dirforimages = '/'.$objectpage->pageurl;
447  if ($grabimagesinto == 'root') $dirforimages='';
448 
449  // Define $filetosave and $filename
450  $filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $regs[2][$key])?'':'/').$regs[2][$key];
451  if (preg_match('/^http/', $regs[2][$key]))
452  {
453  $urltograbbis = $regs[2][$key];
454  $linkwithoutdomain = preg_replace('/^https?:\/\/[^\/]+\//i', '', $regs[2][$key]);
455  $filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
456  }
457  $filename = 'image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
458 
459  // Clean the aa/bb/../cc into aa/cc
460  $filetosave = preg_replace('/\/[^\/]+\/\.\./', '', $filetosave);
461  $filename = preg_replace('/\/[^\/]+\/\.\./', '', $filename);
462 
463  //var_dump($filetosave);
464  //var_dump($filename);
465  //exit;
466 
467  if (empty($alreadygrabbed[$urltograbbis]))
468  {
469  if ($grabimages)
470  {
471  $tmpgeturl = getURLContent($urltograbbis);
472  if ($tmpgeturl['curl_error_no'])
473  {
474  $error++;
475  setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['curl_error_msg'], null, 'errors');
476  $action='create';
477  }
478  elseif ($tmpgeturl['http_code'] != '200')
479  {
480  $error++;
481  setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['http_code'], null, 'errors');
482  $action='create';
483  }
484  else
485  {
486  $alreadygrabbed[$urltograbbis]=1; // Track that file was alreay grabbed.
487 
488  dol_mkdir(dirname($filetosave));
489 
490  $fp = fopen($filetosave, "w");
491  fputs($fp, $tmpgeturl['content']);
492  fclose($fp);
493  if (! empty($conf->global->MAIN_UMASK))
494  @chmod($filetosave, octdec($conf->global->MAIN_UMASK));
495  }
496  }
497  }
498 
499  if ($modifylinks)
500  {
501  $tmp = preg_replace('/'.preg_quote($regs[0][$key],'/').'/i', '<img'.$regs[1][$key].'src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file='.$filename.'"'.$regs[3][$key].'>', $tmp);
502  }
503  }
504 
505  // Search X in "background...url(X)"
506  preg_match_all('/background([^\.\/\(;]+)url\([\"\']?([^\)\"\']*)[\"\']?\)/i', $tmp, $regs);
507 
508  foreach ($regs[0] as $key => $val)
509  {
510  if (preg_match('/^data:image/i', $regs[2][$key])) continue; // We do nothing for such images
511 
512  if (preg_match('/^\//', $regs[2][$key]))
513  {
514  $urltograbdirrootwithoutslash = getRootURLFromURL($urltograb);
515  $urltograbbis = $urltograbdirrootwithoutslash.$regs[2][$key]; // We use dirroot
516  }
517  else
518  {
519  $urltograbbis = $urltograb.'/'.$regs[2][$key]; // We use dir of grabbed file
520  }
521 
522  $linkwithoutdomain = $regs[2][$key];
523 
524  $dirforimages = '/'.$objectpage->pageurl;
525  if ($grabimagesinto == 'root') $dirforimages='';
526 
527  $filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $regs[2][$key])?'':'/').$regs[2][$key];
528 
529  if (preg_match('/^http/', $regs[2][$key]))
530  {
531  $urltograbbis = $regs[2][$key];
532  $linkwithoutdomain = preg_replace('/^https?:\/\/[^\/]+\//i', '', $regs[2][$key]);
533  $filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
534  }
535 
536  $filename = 'image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
537 
538  // Clean the aa/bb/../cc into aa/cc
539  $filetosave = preg_replace('/\/[^\/]+\/\.\./', '', $filetosave);
540  $filename = preg_replace('/\/[^\/]+\/\.\./', '', $filename);
541 
542  //var_dump($filetosave);
543  //var_dump($filename);
544  //exit;
545 
546  if (empty($alreadygrabbed[$urltograbbis]))
547  {
548  if ($grabimages)
549  {
550  $tmpgeturl = getURLContent($urltograbbis);
551  if ($tmpgeturl['curl_error_no'])
552  {
553  $error++;
554  setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['curl_error_msg'], null, 'errors');
555  $action='create';
556  }
557  elseif ($tmpgeturl['http_code'] != '200')
558  {
559  $error++;
560  setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['http_code'], null, 'errors');
561  $action='create';
562  }
563  else
564  {
565  $alreadygrabbed[$urltograbbis]=1; // Track that file was alreay grabbed.
566 
567  dol_mkdir(dirname($filetosave));
568 
569  $fp = fopen($filetosave, "w");
570  fputs($fp, $tmpgeturl['content']);
571  fclose($fp);
572  if (! empty($conf->global->MAIN_UMASK))
573  @chmod($filetosave, octdec($conf->global->MAIN_UMASK));
574  }
575  }
576  }
577 
578  if ($modifylinks)
579  {
580  $tmp = preg_replace('/'.preg_quote($regs[0][$key],'/').'/i', 'background'.$regs[1][$key].'url("'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file='.$filename.'")', $tmp);
581  }
582  }
583 }
584 
585 
586 
593 function dolSaveMasterFile($filemaster)
594 {
595  global $conf;
596 
597  // Now generate the master.inc.php page
598  dol_syslog("We regenerate the master file");
599  dol_delete_file($filemaster);
600 
601  $mastercontent = '<?php'."\n";
602  $mastercontent.= '// File generated to link to the master file - DO NOT MODIFY - It is just an include'."\n";
603  $mastercontent.= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) require_once '".DOL_DOCUMENT_ROOT."/master.inc.php';\n";
604  $mastercontent.= '?>'."\n";
605  $result = file_put_contents($filemaster, $mastercontent);
606  if (! empty($conf->global->MAIN_UMASK))
607  @chmod($filemaster, octdec($conf->global->MAIN_UMASK));
608 
609  return $result;
610 }
611 
620 function dolSavePageAlias($filealias, $object, $objectpage)
621 {
622  global $conf;
623 
624  // Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
625  dol_syslog("We regenerate the alias page filealias=".$filealias);
626 
627  $aliascontent = '<?php'."\n";
628  $aliascontent.= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
629  $aliascontent.= 'global $dolibarr_main_data_root;'."\n";
630  $aliascontent.= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->id.'.tpl.php\'; ';
631  $aliascontent.= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
632  $aliascontent.= '?>'."\n";
633  $result = file_put_contents($filealias, $aliascontent);
634  if (! empty($conf->global->MAIN_UMASK)) {
635  @chmod($filealias, octdec($conf->global->MAIN_UMASK));
636  }
637 
638  return ($result?true:false);
639 }
640 
641 
650 function dolSavePageContent($filetpl, $object, $objectpage)
651 {
652  global $conf;
653 
654  // Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
655  dol_syslog("We regenerate the tpl page filetpl=".$filetpl);
656 
657  dol_delete_file($filetpl);
658 
659  $shortlangcode = '';
660  if ($objectpage->lang) $shortlangcode=preg_replace('/[_-].*$/', '', $objectpage->lang); // en_US or en-US -> en
661 
662  $tplcontent ='';
663  $tplcontent.= "<?php // BEGIN PHP\n";
664  $tplcontent.= '$websitekey=basename(dirname(__FILE__)); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
665  $tplcontent.= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Not already loaded"."\n";
666  $tplcontent.= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
667  $tplcontent.= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
668  $tplcontent.= "ob_start();\n";
669  $tplcontent.= "// END PHP ?>\n";
670  $tplcontent.= '<html'.($shortlangcode ? ' lang="'.$shortlangcode.'"':'').'>'."\n";
671  $tplcontent.= '<head>'."\n";
672  $tplcontent.= '<title>'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'</title>'."\n";
673  $tplcontent.= '<meta charset="UTF-8">'."\n";
674  $tplcontent.= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'."\n";
675  $tplcontent.= '<meta name="robots" content="index, follow" />'."\n";
676  $tplcontent.= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\n";
677  $tplcontent.= '<meta name="keywords" content="'.dol_string_nohtmltag($objectpage->keywords).'" />'."\n";
678  $tplcontent.= '<meta name="title" content="'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'" />'."\n";
679  $tplcontent.= '<meta name="description" content="'.dol_string_nohtmltag($objectpage->description, 0, 'UTF-8').'" />'."\n";
680  $tplcontent.= '<meta name="generator" content="'.DOL_APPLICATION_TITLE.' '.DOL_VERSION.' (https://www.dolibarr.org)" />'."\n";
681  $tplcontent.= '<link href="/'.(($objectpage->id == $object->fk_default_home) ? '' : ($objectpage->pageurl.'.php')).'" rel="canonical" />'."\n";
682  $tplcontent.= '<!-- Include link to CSS file -->'."\n";
683  $tplcontent.= '<link rel="stylesheet" href="styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
684  $tplcontent.= '<!-- Include HTML header from common file -->'."\n";
685  $tplcontent.= '<?php print preg_replace(\'/<\/?html>/ims\', \'\', file_get_contents(DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html")); ?>'."\n";
686  $tplcontent.= '<!-- Include HTML header from page header block -->'."\n";
687  $tplcontent.= preg_replace('/<\/?html>/ims', '', $objectpage->htmlheader)."\n";
688  $tplcontent.= '</head>'."\n";
689 
690  $tplcontent.= '<!-- File generated by Dolibarr website module editor -->'."\n";
691  $tplcontent.= '<body id="bodywebsite" class="bodywebsite">'."\n";
692  $tplcontent.= $objectpage->content."\n";
693  $tplcontent.= '</body>'."\n";
694  $tplcontent.= '</html>'."\n";
695 
696  $tplcontent.= '<?php // BEGIN PHP'."\n";
697  $tplcontent.= '$tmp = ob_get_contents(); ob_end_clean(); dolWebsiteOutput($tmp);'."\n";
698  $tplcontent.= "// END PHP ?>"."\n";
699 
700  //var_dump($filetpl);exit;
701  $result = file_put_contents($filetpl, $tplcontent);
702  if (! empty($conf->global->MAIN_UMASK))
703  @chmod($filetpl, octdec($conf->global->MAIN_UMASK));
704 
705  return $result;
706 }
707 
708 
718 function dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper)
719 {
720  global $conf;
721 
722  $result1=false;
723  $result2=false;
724 
725  dol_mkdir($pathofwebsite);
726 
727  dol_delete_file($fileindex);
728  $indexcontent = '<?php'."\n";
729  $indexcontent.= "// BEGIN PHP File generated to provide an index.php as Home Page or alias redirector - DO NOT MODIFY - It is just a generated wrapper.\n";
730  $indexcontent.= '$websitekey=basename(dirname(__FILE__)); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
731  $indexcontent.= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Load master if not already loaded\n";
732  $indexcontent.= 'if (! empty($_GET[\'pageref\']) || ! empty($_GET[\'pagealiasalt\']) || ! empty($_GET[\'pageid\'])) {'."\n";
733  $indexcontent.= " require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
734  $indexcontent.= " require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
735  $indexcontent.= ' redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
736  $indexcontent.= "}\n";
737  $indexcontent.= "include_once './".basename($filetpl)."'\n";
738  $indexcontent.= '// END PHP ?>'."\n";
739  $result1 = file_put_contents($fileindex, $indexcontent);
740  if (! empty($conf->global->MAIN_UMASK))
741  @chmod($fileindex, octdec($conf->global->MAIN_UMASK));
742 
743  dol_delete_file($filewrapper);
744 
745  $wrappercontent=file_get_contents(DOL_DOCUMENT_ROOT.'/website/samples/wrapper.html');
746 
747  $result2 = file_put_contents($filewrapper, $wrappercontent);
748  if (! empty($conf->global->MAIN_UMASK))
749  @chmod($filewrapper, octdec($conf->global->MAIN_UMASK));
750 
751  return ($result1 && $result2);
752 }
753 
754 
762 function dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
763 {
764  global $conf, $pathofwebsite;
765 
766  dol_syslog("Save html header into ".$filehtmlheader);
767 
768  dol_mkdir($pathofwebsite);
769  $result = file_put_contents($filehtmlheader, $htmlheadercontent);
770  if (! empty($conf->global->MAIN_UMASK))
771  @chmod($filehtmlheader, octdec($conf->global->MAIN_UMASK));
772 
773  if (! $result)
774  {
775  setEventMessages('Failed to write file '.$filehtmlheader, null, 'errors');
776  return false;
777  }
778 
779  return true;
780 }
781 
789 function dolSaveCssFile($filecss, $csscontent)
790 {
791  global $conf, $pathofwebsite;
792 
793  dol_syslog("Save css file into ".$filecss);
794 
795  dol_mkdir($pathofwebsite);
796  $result = file_put_contents($filecss, $csscontent);
797  if (! empty($conf->global->MAIN_UMASK))
798  @chmod($filecss, octdec($conf->global->MAIN_UMASK));
799 
800  if (! $result)
801  {
802  setEventMessages('Failed to write file '.$filecss, null, 'errors');
803  return false;
804  }
805 
806  return true;
807 }
808 
816 function dolSaveJsFile($filejs, $jscontent)
817 {
818  global $conf, $pathofwebsite;
819 
820  dol_syslog("Save js file into ".$filejs);
821 
822  dol_mkdir($pathofwebsite);
823  $result = file_put_contents($filejs, $jscontent);
824  if (! empty($conf->global->MAIN_UMASK))
825  @chmod($filejs, octdec($conf->global->MAIN_UMASK));
826 
827  if (! $result)
828  {
829  setEventMessages('Failed to write file '.$filejs, null, 'errors');
830  return false;
831  }
832 
833  return true;
834 }
835 
843 function dolSaveRobotFile($filerobot, $robotcontent)
844 {
845  global $conf, $pathofwebsite;
846 
847  dol_syslog("Save robot file into ".$filerobot);
848 
849  dol_mkdir($pathofwebsite);
850  $result = file_put_contents($filerobot, $robotcontent);
851  if (! empty($conf->global->MAIN_UMASK))
852  @chmod($filerobot, octdec($conf->global->MAIN_UMASK));
853 
854  if (! $result)
855  {
856  setEventMessages('Failed to write file '.$filerobot, null, 'errors');
857  return false;
858  }
859 
860  return true;
861 }
862 
870 function dolSaveHtaccessFile($filehtaccess, $htaccess)
871 {
872  global $conf, $pathofwebsite;
873 
874  dol_syslog("Save htaccess file into ".$filehtaccess);
875 
876  dol_mkdir($pathofwebsite);
877  $result = file_put_contents($filehtaccess, $htaccess);
878  if (! empty($conf->global->MAIN_UMASK))
879  @chmod($filehtaccess, octdec($conf->global->MAIN_UMASK));
880 
881  if (! $result)
882  {
883  setEventMessages('Failed to write file '.$filehtaccess, null, 'errors');
884  return false;
885  }
886 
887  return true;
888 }
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modifylinks=0, $grabimages=1, $grabimagesinto='subpage')
Download all images found into page content $tmp.
dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
Save content of a page on disk.
dolWebsiteOutput($content)
Render a string of an HTML content and output it.
dolSavePageAlias($filealias, $object, $objectpage)
Save content of a page on disk.
dolSaveRobotFile($filerobot, $robotcontent)
Save content of a page on disk.
dolSaveMasterFile($filemaster)
Save content of a page on disk.
getRootURLFromURL($url)
Function root url from a long url For example: https://www.abc.mydomain.com/dir/page.html return &#39;https://www.abc.mydomain.com&#39; For example: http://www.abc.mydomain.com/ return &#39;https://www.abc.mydomain.com&#39;.
Definition: geturl.lib.php:189
dolWebsiteReplacementOfLinks($website, $content, $removephppart=0)
Convert a page content to have correct links (based on DOL_URL_ROOT) into an html content...
Definition: website.lib.php:36
includeContainer($containerref)
Clean an HTML page to report only content, so we can include it into another page.
Class Websitepage.
redirectToContainer($containerref, $containeraliasalt='', $containerid=0)
Format img tags to introduce viewimage on img src.
dolSavePageContent($filetpl, $object, $objectpage)
Save content of a page on disk.
dolStripPhpCode($str, $replacewith='')
Remove PHP code part from a string.
dolSaveJsFile($filejs, $jscontent)
Save content of a page on disk.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array())
Function get content from an URL (use proxy if proxy defined)
Definition: geturl.lib.php:34
dolSaveHtaccessFile($filehtaccess, $htaccess)
Save content of a page on disk.
dolSaveCssFile($filecss, $csscontent)
Save content of a page on disk.
dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper)
Save content of the index.php and wrapper.php page.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1)
Remove a file or several files with a mask.
Definition: files.lib.php:1139
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)