-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
website.lib.php
833 lines (692 loc) · 33.5 KB
/
website.lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
<?php
/* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/core/lib/website.lib.php
* \ingroup website
* \brief Library for website module
*/
/**
* Convert a page content to have correct links (based on DOL_URL_ROOT) into an html content.
* Used to ouput the page on the Preview from backoffice.
*
* @param Website $website Web site object
* @param string $content Content to replace
* @param int $removephppart 0=Replace PHP sections with a PHP badge. 1=Remove completely PHP sections.
* @return boolean True if OK
* @see dolWebsiteOutput for function used to replace content in a web server context
*/
function dolWebsiteReplacementOfLinks($website, $content, $removephppart=0)
{
// Replace php code. Note $content may come from database and does not contains body tags.
$replacewith='...php...';
if ($removephppart) $replacewith='';
$content = preg_replace('/value="<\?php((?!\?>).)*\?>\n*/ims', 'value="'.$replacewith.'"', $content);
$replacewith='"callto=#';
if ($removephppart) $replacewith='';
$content = preg_replace('/"callto:<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
$replacewith='"mailto=#';
if ($removephppart) $replacewith='';
$content = preg_replace('/"mailto:<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
$replacewith='src="php';
if ($removephppart) $replacewith='';
$content = preg_replace('/src="<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
$replacewith='<span class="phptag">...php...</span>';
if ($removephppart) $replacewith='';
$content = preg_replace('/<\?php((?!\?>).)*\?>\n*/ims', $replacewith, $content);
// Replace relative link / with dolibarr URL
$content = preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageid='.$website->fk_default_home.'"', $content, -1, $nbrep);
// Replace relative link /xxx.php with dolibarr URL
$content = preg_replace('/(href=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
// Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
$content = preg_replace('/url\((["\']?)medias\//', 'url(\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
$content = preg_replace('/data-slide-bg=(["\']?)medias\//', 'data-slide-bg=\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
// <img src="medias/image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
$content = preg_replace('/(<img[^>]*src=")(medias\/)/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
// <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
$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);
// <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
$content = preg_replace('/(<img[^>]*src=")(\/?viewimage\.php)/', '\1'.DOL_URL_ROOT.'/viewimage.php', $content, -1, $nbrep);
// action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
$content = preg_replace('/(action=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
// Fix relative link /document.php with correct URL after the DOL_URL_ROOT: ...href="/document.php?modulepart="
$content=preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
$content=preg_replace('/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
return $content;
}
/**
* Render a string of an HTML content and output it.
* Used to ouput the page when viewed from server (Dolibarr or Apache).
*
* @param string $content Content string
* @return void
* @see dolWebsiteReplacementOfLinks for function used to replace content in the backoffice editor context
*/
function dolWebsiteOutput($content)
{
global $db, $langs, $conf, $user;
global $dolibarr_main_url_root, $dolibarr_main_data_root;
dol_syslog("dolWebsiteOutput start (USEDOLIBARRSERVER=".(defined('USEDOLIBARRSERVER')?'1':'')." (USEDOLIBARREDITOR=".(defined('USEDOLIBARREDITOR')?'1':'').')');
// Define $urlwithroot
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
if (defined('USEDOLIBARREDITOR')) // REPLACEMENT OF LINKS When page called from Dolibarr editor
{
// We remove the <head> part of content
$content = preg_replace('/<head>.*<\/head>/ims', '', $content);
}
elseif (defined('USEDOLIBARRSERVER')) // REPLACEMENT OF LINKS When page called from Dolibarr server
{
global $website;
// Replace relative link / with dolibarr URL: ...href="/"...
$content=preg_replace('/(href=")\/\"/', '\1'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'"', $content, -1, $nbrep);
// Replace relative link /xxx.php with dolibarr URL: ...href="....php"
$content=preg_replace('/(href=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
// Replace relative link /xxx with dolibarr URL: ...href="....php"
$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);
$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);
// Fix relative link /document.php with correct URL after the DOL_URL_ROOT: href="/document.php?modulepart=" => href="/dolibarr/document.php?modulepart="
$content=preg_replace('/(href=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
$content=preg_replace('/(src=")(\/?document\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
// Fix relative link /viewimage.php with correct URL after the DOL_URL_ROOT: href="/viewimage.php?modulepart=" => href="/dolibarr/viewimage.php?modulepart="
$content=preg_replace('/(href=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
$content=preg_replace('/(src=")(\/?viewimage\.php\?[^\"]*modulepart=[^\"]*)(\")/', '\1'.DOL_URL_ROOT.'\2\3', $content, -1, $nbrep);
// Fix relative link into medias with correct URL after the DOL_URL_ROOT: ../url("medias/
$content=preg_replace('/url\((["\']?)medias\//', 'url(\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
$content=preg_replace('/data-slide-bg=(["\']?)medias\//', 'data-slide-bg=\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
// <img src="medias/image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
$content = preg_replace('/(<img[^>]*src=")(medias\/)/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
// <img src="image.png... => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png...
$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);
// <img src="viewimage.php/modulepart=medias&file=image.png" => <img src="dolibarr/viewimage.php/modulepart=medias&file=image.png"
$content = preg_replace('/(<img[^>]*src=")(\/?viewimage\.php)/', '\1'.DOL_URL_ROOT.'/viewimage.php', $content, -1, $nbrep);
// action="newpage.php" => action="dolibarr/website/index.php?website=...&pageref=newpage
$content = preg_replace('/(action=")\/?([^:\"]*)(\.php\")/', '\1'.DOL_URL_ROOT.'/public/website/index.php?website='.$website->ref.'&pageref=\2"', $content, -1, $nbrep);
}
else // REPLACEMENT OF LINKS When page called from virtual host
{
$symlinktomediaexists=1;
// Make a change into HTML code to allow to include images from medias directory correct with direct link for virtual server
// <img alt="" src="/dolibarr_dev/htdocs/viewimage.php?modulepart=medias&entity=1&file=image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
// become
// <img alt="" src="'.$urlwithroot.'/medias/image/ldestailleur_166x166.jpg" style="height:166px; width:166px" />
$nbrep=0;
if (! $symlinktomediaexists)
{
$content=preg_replace('/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
$content=preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
$content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
$content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1/wrapper.php\2modulepart=medias\3file=\4\5', $content, -1, $nbrep);
$content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=mycompany\3file=\4\5', $content, -1, $nbrep);
}
else
{
$content=preg_replace('/(<script[^>]*src=")[^\"]*document\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1medias/\4\5', $content, -1, $nbrep);
$content=preg_replace('/(<a[^>]*href=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1medias/\4\5', $content, -1, $nbrep);
$content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=medias([^\"]*)file=([^\"]*)("[^>]*>)/', '\1medias/\4\5', $content, -1, $nbrep);
$content=preg_replace('/(url\(["\']?)[^\)]*viewimage\.php([^\)]*)modulepart=medias([^\)]*)file=([^\)]*)(["\']?\))/', '\1medias/\4\5', $content, -1, $nbrep);
$content=preg_replace('/(<img[^>]*src=")[^\"]*viewimage\.php([^\"]*)modulepart=mycompany([^\"]*)file=([^\"]*)("[^>]*>)/', '\1/wrapper.php\2modulepart=mycompany\3file=\4\5', $content, -1, $nbrep);
}
}
$content=preg_replace('/ contenteditable="true"/', ' contenteditable="false"', $content, -1, $nbrep);
dol_syslog("dolWebsiteOutput end");
print $content;
}
/**
* Format img tags to introduce viewimage on img src.
*
* @param string $content Content string
* @return void
* @see dolWebsiteOutput
*/
/*
function dolWebsiteSaveContent($content)
{
global $db, $langs, $conf, $user;
global $dolibarr_main_url_root, $dolibarr_main_data_root;
//dol_syslog("dolWebsiteSaveContent start (mode=".(defined('USEDOLIBARRSERVER')?'USEDOLIBARRSERVER':'').')');
// Define $urlwithroot
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
//$content = preg_replace('/(<img.*src=")(?!(http|'.preg_quote(DOL_URL_ROOT,'/').'\/viewimage))/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $content, -1, $nbrep);
return $content;
}
*/
/**
* Make a redirect to another container.
*
* @param string $containerref Ref of container to redirect to (must be a page from website root. Example: 'mypage.php' means 'mywebsite/mypage.php').
* @param string $containeraliasalt Ref of alternative aliases to redirect to.
* @param int $containerid Id of container.
* @return void
*/
function redirectToContainer($containerref, $containeraliasalt='',$containerid=0)
{
global $db, $website;
$newurl = '';
$result=0;
// We make redirect using the alternative alias, we must find the real $containerref
if ($containeraliasalt)
{
include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
$tmpwebsitepage=new WebsitePage($db);
$result = $tmpwebsitepage->fetch(0, $website->id, '', $containeraliasalt);
if ($result > 0)
{
$containerref = $tmpwebsitepage->pageurl;
}
else
{
print "Error, page contains a redirect to the alternative alias '".$containeraliasalt."' that does not exists in web site (".$website->id." / ".$website->ref.")";
exit;
}
}
if (defined('USEDOLIBARRSERVER')) // When page called from Dolibarr server
{
// Check new container exists
if (! $containeraliasalt) // If containeraliasalt set, we already did the test
{
include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
$tmpwebsitepage=new WebsitePage($db);
$result = $tmpwebsitepage->fetch(0, $website->id, $containerref);
unset($tmpwebsitepage);
}
if ($result > 0)
{
$currenturi = $_SERVER["REQUEST_URI"];
if (preg_match('/&pageref=([^&]+)/', $currenturi, $regtmp))
{
if ($regtmp[0] == $containerref)
{
print "Error, page with uri '.$currenturi.' try a redirect to the same alias page '".$containerref."' in web site '".$website->ref."'";
exit;
}
else
{
$newurl = preg_replace('/&pageref=([^&]+)/', '&pageref='.$containerref, $currenturi);
}
}
else
{
$newurl = $currenturi.'&pageref='.urlencode($containerref);
}
}
}
else // When page called from virtual host server
{
$newurl = '/'.$containerref.'.php';
}
if ($newurl)
{
header("Location: ".$newurl);
exit;
}
else
{
print "Error, page contains a redirect to the alias page '".$containerref."' that does not exists in web site (".$website->id." / ".$website->ref.")";
exit;
}
}
/**
* Clean an HTML page to report only content, so we can include it into another page.
* It outputs content of file sanitized from html and body part.
*
* @param string $containerref Path to file to include (must be a page from website root. Example: 'mypage.php' means 'mywebsite/mypage.php')
* @return void
*/
function includeContainer($containerref)
{
global $conf, $db, $langs, $mysoc, $user, $website;
global $includehtmlcontentopened;
global $websitekey;
$MAXLEVEL=20;
if (! preg_match('/\.php$/i', $containerref)) $containerref.='.php';
$fullpathfile=DOL_DATA_ROOT.'/website/'.$websitekey.'/'.$containerref;
if (empty($includehtmlcontentopened)) $includehtmlcontentopened=0;
$includehtmlcontentopened++;
if ($includehtmlcontentopened > $MAXLEVEL)
{
print 'ERROR: RECURSIVE CONTENT LEVEL. Depth of recursive call is more than the limit of '.$MAXLEVEL.".\n";
return;
}
// file_get_contents is not possible. We must execute code with include
//$content = file_get_contents($fullpathfile);
//print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/
ob_start();
$res = include $fullpathfile; // Include because we want to execute code content
$tmpoutput = ob_get_contents();
ob_end_clean();
print "\n".'<!-- include '.$fullpathfile.' level = '.$includehtmlcontentopened.' -->'."\n";
print preg_replace(array('/^.*<body[^>]*>/ims','/<\/body>.*$/ims'), array('', ''), $tmpoutput);
if (! $res)
{
print 'ERROR: FAILED TO INCLUDE PAGE '.$containerref.".\n";
}
$includehtmlcontentopened--;
}
/**
* Download all images found into page content $tmp.
* If $modifylinks is set, links to images will be replace with a link to viewimage wrapper.
*
* @param Website $object Object website
* @param WebsitePage $objectpage Object website page
* @param string $urltograb URL to grab (exemple: http://www.nltechno.com/ or http://www.nltechno.com/dir1/ or http://www.nltechno.com/dir1/mapage1)
* @param string $tmp Content to parse
* @param string $action Var $action
* @param string $modifylinks 0=Do not modify content, 1=Replace links with a link to viewimage
* @param int $grabimages 0=Do not grab images, 1=Grab images
* @param string $grabimagesinto 'root' or 'subpage'
* @return void
*/
function getAllImages($object, $objectpage, $urltograb, &$tmp, &$action, $modifylinks=0, $grabimages=1, $grabimagesinto='subpage')
{
global $conf;
$error=0;
dol_syslog("Call getAllImages with grabimagesinto=".$grabimagesinto);
$alreadygrabbed=array();
if (preg_match('/\/$/', $urltograb)) $urltograb.='.';
$urltograb = dirname($urltograb); // So urltograb is now http://www.nltechno.com or http://www.nltechno.com/dir1
// Search X in "img...src=X"
preg_match_all('/<img([^\.\/]+)src="([^>"]+)"([^>]*)>/i', $tmp, $regs);
foreach ($regs[0] as $key => $val)
{
if (preg_match('/^data:image/i', $regs[2][$key])) continue; // We do nothing for such images
if (preg_match('/^\//', $regs[2][$key]))
{
$urltograbdirrootwithoutslash = getRootURLFromURL($urltograb);
$urltograbbis = $urltograbdirrootwithoutslash.$regs[2][$key]; // We use dirroot
}
else
{
$urltograbbis = $urltograb.'/'.$regs[2][$key]; // We use dir of grabbed file
}
$linkwithoutdomain = $regs[2][$key];
$dirforimages = '/'.$objectpage->pageurl;
if ($grabimagesinto == 'root') $dirforimages='';
// Define $filetosave and $filename
$filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $regs[2][$key])?'':'/').$regs[2][$key];
if (preg_match('/^http/', $regs[2][$key]))
{
$urltograbbis = $regs[2][$key];
$linkwithoutdomain = preg_replace('/^https?:\/\/[^\/]+\//i', '', $regs[2][$key]);
$filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
}
$filename = 'image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
// Clean the aa/bb/../cc into aa/cc
$filetosave = preg_replace('/\/[^\/]+\/\.\./', '', $filetosave);
$filename = preg_replace('/\/[^\/]+\/\.\./', '', $filename);
//var_dump($filetosave);
//var_dump($filename);
//exit;
if (empty($alreadygrabbed[$urltograbbis]))
{
if ($grabimages)
{
$tmpgeturl = getURLContent($urltograbbis);
if ($tmpgeturl['curl_error_no'])
{
$error++;
setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['curl_error_msg'], null, 'errors');
$action='create';
}
elseif ($tmpgeturl['http_code'] != '200')
{
$error++;
setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['http_code'], null, 'errors');
$action='create';
}
else
{
$alreadygrabbed[$urltograbbis]=1; // Track that file was alreay grabbed.
dol_mkdir(dirname($filetosave));
$fp = fopen($filetosave, "w");
fputs($fp, $tmpgeturl['content']);
fclose($fp);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filetosave, octdec($conf->global->MAIN_UMASK));
}
}
}
if ($modifylinks)
{
$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);
}
}
// Search X in "background...url(X)"
preg_match_all('/background([^\.\/\(;]+)url\([\"\']?([^\)\"\']*)[\"\']?\)/i', $tmp, $regs);
foreach ($regs[0] as $key => $val)
{
if (preg_match('/^data:image/i', $regs[2][$key])) continue; // We do nothing for such images
if (preg_match('/^\//', $regs[2][$key]))
{
$urltograbdirrootwithoutslash = getRootURLFromURL($urltograb);
$urltograbbis = $urltograbdirrootwithoutslash.$regs[2][$key]; // We use dirroot
}
else
{
$urltograbbis = $urltograb.'/'.$regs[2][$key]; // We use dir of grabbed file
}
$linkwithoutdomain = $regs[2][$key];
$dirforimages = '/'.$objectpage->pageurl;
if ($grabimagesinto == 'root') $dirforimages='';
$filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $regs[2][$key])?'':'/').$regs[2][$key];
if (preg_match('/^http/', $regs[2][$key]))
{
$urltograbbis = $regs[2][$key];
$linkwithoutdomain = preg_replace('/^https?:\/\/[^\/]+\//i', '', $regs[2][$key]);
$filetosave = $conf->medias->multidir_output[$conf->entity].'/image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
}
$filename = 'image/'.$object->ref.$dirforimages.(preg_match('/^\//', $linkwithoutdomain)?'':'/').$linkwithoutdomain;
// Clean the aa/bb/../cc into aa/cc
$filetosave = preg_replace('/\/[^\/]+\/\.\./', '', $filetosave);
$filename = preg_replace('/\/[^\/]+\/\.\./', '', $filename);
//var_dump($filetosave);
//var_dump($filename);
//exit;
if (empty($alreadygrabbed[$urltograbbis]))
{
if ($grabimages)
{
$tmpgeturl = getURLContent($urltograbbis);
if ($tmpgeturl['curl_error_no'])
{
$error++;
setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['curl_error_msg'], null, 'errors');
$action='create';
}
elseif ($tmpgeturl['http_code'] != '200')
{
$error++;
setEventMessages('Error getting '.$urltograbbis.': '.$tmpgeturl['http_code'], null, 'errors');
$action='create';
}
else
{
$alreadygrabbed[$urltograbbis]=1; // Track that file was alreay grabbed.
dol_mkdir(dirname($filetosave));
$fp = fopen($filetosave, "w");
fputs($fp, $tmpgeturl['content']);
fclose($fp);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filetosave, octdec($conf->global->MAIN_UMASK));
}
}
}
if ($modifylinks)
{
$tmp = preg_replace('/'.preg_quote($regs[0][$key],'/').'/i', 'background'.$regs[1][$key].'url("'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file='.$filename.'")', $tmp);
}
}
}
/**
* Save content of a page on disk
*
* @param string $filemaster Full path of filename master.inc.php for website to generate
* @return boolean True if OK
*/
function dolSaveMasterFile($filemaster)
{
global $conf;
// Now generate the master.inc.php page
dol_syslog("We regenerate the master file");
dol_delete_file($filemaster);
$mastercontent = '<?php'."\n";
$mastercontent.= '// File generated to link to the master file - DO NOT MODIFY - It is just an include'."\n";
$mastercontent.= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) require_once '".DOL_DOCUMENT_ROOT."/master.inc.php';\n";
$mastercontent.= '?>'."\n";
$result = file_put_contents($filemaster, $mastercontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filemaster, octdec($conf->global->MAIN_UMASK));
return $result;
}
/**
* Save content of a page on disk
*
* @param string $filealias Full path of filename to generate
* @param Website $object Object website
* @param WebsitePage $objectpage Object websitepage
* @return boolean True if OK
*/
function dolSavePageAlias($filealias, $object, $objectpage)
{
global $conf;
// Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
dol_syslog("We regenerate the alias page filealias=".$filealias);
$aliascontent = '<?php'."\n";
$aliascontent.= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
$aliascontent.= 'global $dolibarr_main_data_root;'."\n";
$aliascontent.= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->id.'.tpl.php\'; ';
$aliascontent.= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
$aliascontent.= '?>'."\n";
$result = file_put_contents($filealias, $aliascontent);
if (! empty($conf->global->MAIN_UMASK)) {
@chmod($filealias, octdec($conf->global->MAIN_UMASK));
}
return ($result?true:false);
}
/**
* Save content of a page on disk
*
* @param string $filetpl Full path of filename to generate
* @param Website $object Object website
* @param WebsitePage $objectpage Object websitepage
* @return boolean True if OK
*/
function dolSavePageContent($filetpl, $object, $objectpage)
{
global $conf;
// Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
dol_syslog("We regenerate the tpl page filetpl=".$filetpl);
dol_delete_file($filetpl);
$shortlangcode = '';
if ($objectpage->lang) $shortlangcode=preg_replace('/[_-].*$/', '', $objectpage->lang); // en_US or en-US -> en
$tplcontent ='';
$tplcontent.= "<?php // BEGIN PHP\n";
$tplcontent.= '$websitekey=basename(dirname(__FILE__));'."\n";
$tplcontent.= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Not already loaded"."\n";
$tplcontent.= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
$tplcontent.= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
$tplcontent.= "ob_start();\n";
$tplcontent.= "// END PHP ?>\n";
$tplcontent.= '<html'.($shortlangcode ? ' lang="'.$shortlangcode.'"':'').'>'."\n";
$tplcontent.= '<head>'."\n";
$tplcontent.= '<title>'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'</title>'."\n";
$tplcontent.= '<meta charset="UTF-8">'."\n";
$tplcontent.= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'."\n";
$tplcontent.= '<meta name="robots" content="index, follow" />'."\n";
$tplcontent.= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\n";
$tplcontent.= '<meta name="keywords" content="'.dol_string_nohtmltag($objectpage->keywords).'" />'."\n";
$tplcontent.= '<meta name="title" content="'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'" />'."\n";
$tplcontent.= '<meta name="description" content="'.dol_string_nohtmltag($objectpage->description, 0, 'UTF-8').'" />'."\n";
$tplcontent.= '<meta name="generator" content="'.DOL_APPLICATION_TITLE.' '.DOL_VERSION.' (https://www.dolibarr.org)" />'."\n";
$tplcontent.= '<!-- Include link to CSS file -->'."\n";
$tplcontent.= '<link rel="stylesheet" href="styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
$tplcontent.= '<!-- Include HTML header from common file -->'."\n";
$tplcontent.= '<?php print preg_replace(\'/<\/?html>/ims\', \'\', file_get_contents(DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html")); ?>'."\n";
$tplcontent.= '<!-- Include HTML header from page header block -->'."\n";
$tplcontent.= preg_replace('/<\/?html>/ims', '', $objectpage->htmlheader)."\n";
$tplcontent.= '</head>'."\n";
$tplcontent.= '<!-- File generated by Dolibarr website module editor -->'."\n";
$tplcontent.= '<body id="bodywebsite" class="bodywebsite">'."\n";
$tplcontent.= $objectpage->content."\n";
$tplcontent.= '</body>'."\n";
$tplcontent.= '</html>'."\n";
$tplcontent.= '<?php // BEGIN PHP'."\n";
$tplcontent.= '$tmp = ob_get_contents(); ob_end_clean(); dolWebsiteOutput($tmp);'."\n";
$tplcontent.= "// END PHP ?>"."\n";
//var_dump($filetpl);exit;
$result = file_put_contents($filetpl, $tplcontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filetpl, octdec($conf->global->MAIN_UMASK));
return $result;
}
/**
* Save content of the index.php and wrapper.php page
*
* @param string $pathofwebsite Path of website root
* @param string $fileindex Full path of file index.php
* @param string $filetpl File tpl to index.php page redirect to
* @param string $filewrapper Full path of file wrapper.php
* @return boolean True if OK
*/
function dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper)
{
global $conf;
$result1=false;
$result2=false;
dol_mkdir($pathofwebsite);
dol_delete_file($fileindex);
$indexcontent = '<?php'."\n";
$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";
$indexcontent.= '$websitekey=basename(dirname(__FILE__));'."\n";
$indexcontent.= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Load master if not already loaded\n";
$indexcontent.= 'if (! empty($_GET[\'pageref\']) || ! empty($_GET[\'pagealiasalt\']) || ! empty($_GET[\'pageid\'])) {'."\n";
$indexcontent.= " require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
$indexcontent.= " require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
$indexcontent.= ' redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
$indexcontent.= "}\n";
$indexcontent.= "include_once './".basename($filetpl)."'\n";
$indexcontent.= '// END PHP ?>'."\n";
$result1 = file_put_contents($fileindex, $indexcontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($fileindex, octdec($conf->global->MAIN_UMASK));
dol_delete_file($filewrapper);
$wrappercontent = '<?php'."\n";
$wrappercontent.= "// BEGIN PHP File generated to provide a wrapper.php - DO NOT MODIFY - It is just a generated wrapper.\n";
$wrappercontent.= '$websitekey=basename(dirname(__FILE__));'."\n";
$wrappercontent.= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Load master if not already loaded\n";
$wrappercontent.= '$original_file=str_replace("../","/", GETPOST("file","alpha"));'."\n";
$wrappercontent.= 'if ($_GET["modulepart"] == "mycompany" && preg_match(\'/^\/?logos\//\', $original_file)) readfile(dol_osencode($conf->mycompany->dir_output."/".$original_file));'."\n";
$wrappercontent.= "else print 'Bad value for modulepart or file';\n";
$wrappercontent.= 'if (is_object($db)) $db->close();'."\n";
$wrappercontent.= '// END PHP ?>'."\n";
$result2 = file_put_contents($filewrapper, $wrappercontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filewrapper, octdec($conf->global->MAIN_UMASK));
return ($result1 && $result2);
}
/**
* Save content of a page on disk
*
* @param string $filehtmlheader Full path of filename to generate
* @param string $htmlheadercontent Content of file
* @return boolean True if OK
*/
function dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save html header into ".$filehtmlheader);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filehtmlheader, $htmlheadercontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filehtmlheader, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filehtmlheader, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filecss Full path of filename to generate
* @param string $csscontent Content of file
* @return boolean True if OK
*/
function dolSaveCssFile($filecss, $csscontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save css file into ".$filecss);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filecss, $csscontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filecss, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filecss, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filejs Full path of filename to generate
* @param string $jscontent Content of file
* @return boolean True if OK
*/
function dolSaveJsFile($filejs, $jscontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save js file into ".$filejs);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filejs, $jscontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filejs, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filejs, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filerobot Full path of filename to generate
* @param string $robotcontent Content of file
* @return boolean True if OK
*/
function dolSaveRobotFile($filerobot, $robotcontent)
{
global $conf, $pathofwebsite;
dol_syslog("Save robot file into ".$filerobot);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filerobot, $robotcontent);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filerobot, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filerobot, null, 'errors');
return false;
}
return true;
}
/**
* Save content of a page on disk
*
* @param string $filehtaccess Full path of filename to generate
* @param string $htaccess Content of file
* @return boolean True if OK
*/
function dolSaveHtaccessFile($filehtaccess, $htaccess)
{
global $conf, $pathofwebsite;
dol_syslog("Save htaccess file into ".$filehtaccess);
dol_mkdir($pathofwebsite);
$result = file_put_contents($filehtaccess, $htaccess);
if (! empty($conf->global->MAIN_UMASK))
@chmod($filehtaccess, octdec($conf->global->MAIN_UMASK));
if (! $result)
{
setEventMessages('Failed to write file '.$filehtaccess, null, 'errors');
return false;
}
return true;
}