dolibarr 21.0.0-alpha
website2.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 <https://www.gnu.org/licenses/>.
16 */
17
31function dolSaveMasterFile($filemaster)
32{
33 // Now generate the master.inc.php page
34 dol_syslog("We regenerate the master.inc.php file");
35
36 dol_delete_file($filemaster);
37
38 $mastercontent = '<?php'."\n";
39 $mastercontent .= '// File generated to link to the master file - DO NOT MODIFY - It is just an include'."\n";
40 $mastercontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
41 $mastercontent .= " if (! defined('USEEXTERNALSERVER')) define('USEEXTERNALSERVER', 1);\n";
42 $mastercontent .= " require_once '".DOL_DOCUMENT_ROOT."/master.inc.php';\n";
43 $mastercontent .= "}\n";
44 $mastercontent .= '?>'."\n";
45 $result = file_put_contents($filemaster, $mastercontent);
46 dolChmod($filemaster);
47
48 return $result;
49}
50
61function dolSavePageAlias($filealias, $object, $objectpage)
62{
63 // Now create the .tpl file
64 dol_syslog("dolSavePageAlias We regenerate the alias page filealias=".$filealias." and a wrapper into all language subdirectories");
65
66 $aliascontent = '<?php'."\n";
67 $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
68 $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
69 $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->id.'.tpl.php\'; ';
70 $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
71 $aliascontent .= '?>'."\n";
72 $result = file_put_contents($filealias, $aliascontent);
73 if ($result === false) {
74 dol_syslog("Failed to write file ".$filealias, LOG_WARNING);
75 }
76 dolChmod($filealias);
77
78 // Save also alias into language subdirectory if it is not a main language
79 if ($objectpage->lang && in_array($objectpage->lang, explode(',', $object->otherlang))) {
80 $dirname = dirname($filealias);
81 $filename = basename($filealias);
82 $filealiassub = $dirname.'/'.$objectpage->lang.'/'.$filename;
83
84 dol_mkdir($dirname.'/'.$objectpage->lang, DOL_DATA_ROOT);
85
86 $aliascontent = '<?php'."\n";
87 $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
88 $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
89 $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'../page'.$objectpage->id.'.tpl.php\'; ';
90 $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
91 $aliascontent .= '?>'."\n";
92 $result = file_put_contents($filealiassub, $aliascontent);
93 if ($result === false) {
94 dol_syslog("Failed to write file ".$filealiassub, LOG_WARNING);
95 }
96 dolChmod($filealiassub);
97 } elseif (empty($objectpage->lang) || !in_array($objectpage->lang, explode(',', $object->otherlang))) {
98 // Save also alias into all language subdirectories if it is a main language
99 if (!getDolGlobalString('WEBSITE_DISABLE_MAIN_LANGUAGE_INTO_LANGSUBDIR') && !empty($object->otherlang)) {
100 $dirname = dirname($filealias);
101 $filename = basename($filealias);
102 foreach (explode(',', $object->otherlang) as $sublang) {
103 // Avoid to erase main alias file if $sublang is empty string
104 if (empty(trim($sublang))) {
105 continue;
106 }
107 $filealiassub = $dirname.'/'.$sublang.'/'.$filename;
108
109 $aliascontent = '<?php'."\n";
110 $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
111 $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
112 $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'../page'.$objectpage->id.'.tpl.php\'; ';
113 $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
114 $aliascontent .= '?>'."\n";
115
116 dol_mkdir($dirname.'/'.$sublang);
117 $result = file_put_contents($filealiassub, $aliascontent);
118 if ($result === false) {
119 dol_syslog("Failed to write file ".$filealiassub, LOG_WARNING);
120 }
121 dolChmod($filealiassub);
122 }
123 }
124 }
125
126 return ($result ? true : false);
127}
128
129
141function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage, $backupold = 0)
142{
143 global $db;
144
145 // Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
146 dol_syslog("dolSavePageContent We regenerate the tpl page filetpl=".$filetpl);
147
148 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
149
150 if (dol_is_file($filetpl)) {
151 if ($backupold) {
152 $result = archiveOrBackupFile($filetpl);
153 if (! $result) {
154 return false;
155 }
156 } else {
157 dol_delete_file($filetpl);
158 }
159 }
160
161 $shortlangcode = '';
162 if ($objectpage->lang) {
163 $shortlangcode = substr($objectpage->lang, 0, 2); // en_US or en-US -> en
164 }
165 if (empty($shortlangcode)) {
166 $shortlangcode = substr($object->lang, 0, 2); // en_US or en-US -> en
167 }
168
169 if (!empty($objectpage->type_container) && in_array($objectpage->type_container, array('library', 'service'))) {
170 $originalcontentonly = 1;
171 }
172
173 $tplcontent = '';
174 if (!isset($originalcontentonly)) {
175 $tplcontent .= "<?php // BEGIN PHP\n";
176 $tplcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
177 $tplcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
178 $tplcontent .= ' $pathdepth = count(explode(\'/\', $_SERVER[\'SCRIPT_NAME\'])) - 2;'."\n";
179 $tplcontent .= ' require_once ($pathdepth ? str_repeat(\'../\', $pathdepth) : \'./\').\'master.inc.php\';'."\n";
180 $tplcontent .= "} // Not already loaded\n";
181 $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
182 $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
183 $tplcontent .= "ob_start();\n";
184 $tplcontent .= "// END PHP ?>\n";
185 if (getDolGlobalString('WEBSITE_FORCE_DOCTYPE_HTML5')) {
186 $tplcontent .= "<!DOCTYPE html>\n";
187 }
188 $tplcontent .= '<html'.($shortlangcode ? ' lang="'.$shortlangcode.'"' : '').'>'."\n";
189 $tplcontent .= '<head>'."\n";
190 $tplcontent .= '<title>'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'</title>'."\n";
191 $tplcontent .= '<meta charset="utf-8">'."\n";
192 $tplcontent .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'."\n";
193 $tplcontent .= '<meta name="robots" content="index, follow" />'."\n";
194 $tplcontent .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\n";
195 $tplcontent .= '<meta name="keywords" content="'.dol_string_nohtmltag($objectpage->keywords).'" />'."\n";
196 $tplcontent .= '<meta name="title" content="'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'" />'."\n";
197 $tplcontent .= '<meta name="description" content="'.dol_string_nohtmltag($objectpage->description, 0, 'UTF-8').'" />'."\n";
198 $tplcontent .= '<meta name="generator" content="'.DOL_APPLICATION_TITLE.' '.DOL_VERSION.' (https://www.dolibarr.org)" />'."\n";
199 $tplcontent .= '<meta name="dolibarr:pageid" content="'.dol_string_nohtmltag($objectpage->id).'" />'."\n";
200
201 // Add favicon
202 if ($objectpage->id == $object->fk_default_home) {
203 $tplcontent .= '<link rel="icon" type="image/png" href="/favicon.png" />'."\n";
204 }
205
206 // Add canonical reference
207 if ($object->virtualhost) {
208 $tplcontent .= '<link rel="canonical" href="'.(($objectpage->id == $object->fk_default_home) ? '/' : (($shortlangcode != substr($object->lang, 0, 2) ? '/'.$shortlangcode : '').'/'.$objectpage->pageurl.'.php')).'" />'."\n";
209 }
210 // Add translation reference (main language)
211 if ($object->isMultiLang()) {
212 // Add page "translation of"
213 $translationof = $objectpage->fk_page;
214 if ($translationof) {
215 $tmppage = new WebsitePage($db);
216 $tmppage->fetch($translationof);
217 if ($tmppage->id > 0) {
218 $tmpshortlangcode = '';
219 if ($tmppage->lang) {
220 $tmpshortlangcode = preg_replace('/[_-].*$/', '', $tmppage->lang); // en_US or en-US -> en
221 }
222 if (empty($tmpshortlangcode)) {
223 $tmpshortlangcode = preg_replace('/[_-].*$/', '', $object->lang); // en_US or en-US -> en
224 }
225 if ($tmpshortlangcode != $shortlangcode) {
226 $tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="<?php echo $website->virtualhost; ?>'.($object->fk_default_home == $tmppage->id ? '/' : (($tmpshortlangcode != substr($object->lang, 0, 2)) ? '/'.$tmpshortlangcode : '').'/'.$tmppage->pageurl.'.php').'" />'."\n";
227 }
228 }
229 }
230
231 // Add "has translation pages"
232 $sql = "SELECT rowid as id, lang, pageurl from ".MAIN_DB_PREFIX.'website_page where fk_page IN ('.$db->sanitize($objectpage->id.($translationof ? ", ".$translationof : '')).")";
233 $resql = $db->query($sql);
234 if ($resql) {
235 $num_rows = $db->num_rows($resql);
236 if ($num_rows > 0) {
237 while ($obj = $db->fetch_object($resql)) {
238 $tmpshortlangcode = '';
239 if ($obj->lang) {
240 $tmpshortlangcode = preg_replace('/[_-].*$/', '', $obj->lang); // en_US or en-US -> en
241 }
242 if ($tmpshortlangcode != $shortlangcode) {
243 $tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="<?php echo $website->virtualhost; ?>'.($object->fk_default_home == $obj->id ? '/' : (($tmpshortlangcode != substr($object->lang, 0, 2) ? '/'.$tmpshortlangcode : '')).'/'.$obj->pageurl.'.php').'" />'."\n";
244 }
245 }
246 }
247 } else {
248 dol_print_error($db);
249 }
250
251 // Add myself
252 $tplcontent .= '<?php if ($_SERVER["PHP_SELF"] == "'.(($object->fk_default_home == $objectpage->id) ? '/' : (($shortlangcode != substr($object->lang, 0, 2)) ? '/'.$shortlangcode : '')).'/'.$objectpage->pageurl.'.php") { ?>'."\n";
253 $tplcontent .= '<link rel="alternate" hreflang="'.$shortlangcode.'" href="<?php echo $website->virtualhost; ?>'.(($object->fk_default_home == $objectpage->id) ? '/' : (($shortlangcode != substr($object->lang, 0, 2)) ? '/'.$shortlangcode : '').'/'.$objectpage->pageurl.'.php').'" />'."\n";
254
255 $tplcontent .= '<?php } ?>'."\n";
256 }
257 // Add manifest.json. Do we have to add it only on home page ?
258 $tplcontent .= '<?php if ($website->use_manifest) { print \'<link rel="manifest" href="/manifest.json.php" />\'."\n"; } ?>'."\n";
259 $tplcontent .= '<!-- Include link to CSS file -->'."\n";
260 // Add js
261 $tplcontent .= '<link rel="stylesheet" href="/styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
262 $tplcontent .= '<!-- Include link to JS file -->'."\n";
263 $tplcontent .= '<script nonce="'.getNonce().'" async src="/javascript.js.php?website=<?php echo $websitekey; ?>"></script>'."\n";
264 // Add headers
265 $tplcontent .= '<!-- Include HTML header from common file -->'."\n";
266 $tplcontent .= '<?php if (file_exists(DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html")) include DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html"; ?>'."\n";
267 $tplcontent .= '<!-- Include HTML header from page header block -->'."\n";
268 $tplcontent .= preg_replace('/<\/?html>/ims', '', $objectpage->htmlheader)."\n";
269 $tplcontent .= '</head>'."\n";
270
271 $tplcontent .= '<!-- File generated by Dolibarr website module editor -->'."\n";
272 $tplcontent .= '<body id="bodywebsite" class="bodywebsite bodywebpage-'.$objectpage->ref.'">'."\n";
273 $tplcontent .= $objectpage->content."\n";
274 $tplcontent .= '</body>'."\n";
275 $tplcontent .= '</html>'."\n";
276
277 $tplcontent .= '<?php // BEGIN PHP'."\n";
278 $tplcontent .= '$tmp = ob_get_contents(); ob_end_clean(); dolWebsiteOutput($tmp, "html", '.$objectpage->id.'); dolWebsiteIncrementCounter('.$object->id.', "'.$objectpage->type_container.'", '.$objectpage->id.');'."\n";
279 $tplcontent .= "// END PHP ?>\n";
280 } else {
281 $tplcontent .= "<?php // BEGIN PHP\n";
282 $tplcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
283 $tplcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
284 $tplcontent .= ' $pathdepth = count(explode(\'/\', $_SERVER[\'SCRIPT_NAME\'])) - 2;'."\n";
285 $tplcontent .= ' require_once ($pathdepth ? str_repeat(\'../\', $pathdepth) : \'./\').\'master.inc.php\';'."\n";
286 $tplcontent .= "} // Not already loaded\n";
287 $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
288 $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
289 $tplcontent .= "// END PHP ?>\n";
290
291 $tplcontent .= $objectpage->content;
292 }
293
294 //var_dump($filetpl);exit;
295 $result = file_put_contents($filetpl, $tplcontent);
296
297 dolChmod($filetpl);
298
299 return $result;
300}
301
302
313function dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper, $object = null)
314{
315 global $db;
316
317 $result1 = false;
318 $result2 = false;
319
320 dol_mkdir($pathofwebsite);
321
322 if ($fileindex) {
323 dol_delete_file($fileindex);
324 $indexcontent = '<?php'."\n";
325 $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";
326 $indexcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
327 $indexcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Load master if not already loaded\n";
328 $indexcontent .= 'if (!empty($_GET[\'pageref\']) || !empty($_GET[\'pagealiasalt\']) || !empty($_GET[\'pageid\'])) {'."\n";
329 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
330 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
331 $indexcontent .= ' redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
332 $indexcontent .= "}\n";
333 $indexcontent .= "include_once './".basename($filetpl)."'\n";
334 $indexcontent .= '// END PHP ?>'."\n";
335
336 $result1 = file_put_contents($fileindex, $indexcontent);
337
338 dolChmod($fileindex);
339
340 if (is_object($object) && $object->fk_default_home > 0) {
341 $objectpage = new WebsitePage($db);
342 $objectpage->fetch($object->fk_default_home);
343
344 // Create a version for sublanguages
345 if (empty($objectpage->lang) || !in_array($objectpage->lang, explode(',', $object->otherlang))) {
346 if (!getDolGlobalString('WEBSITE_DISABLE_MAIN_LANGUAGE_INTO_LANGSUBDIR') && is_object($object) && !empty($object->otherlang)) {
347 $dirname = dirname($fileindex);
348 foreach (explode(',', $object->otherlang) as $sublang) {
349 // Avoid to erase main alias file if $sublang is empty string
350 if (empty(trim($sublang))) {
351 continue;
352 }
353 $fileindexsub = $dirname.'/'.$sublang.'/index.php';
354
355 // Same indexcontent than previously but with ../ instead of ./ for master and tpl file include/require_once.
356 $relpath = '..';
357 $indexcontent = '<?php'."\n";
358 $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";
359 $indexcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
360 $indexcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once '".$relpath."/master.inc.php'; } // Load master if not already loaded\n";
361 $indexcontent .= 'if (!empty($_GET[\'pageref\']) || !empty($_GET[\'pagealiasalt\']) || !empty($_GET[\'pageid\'])) {'."\n";
362 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
363 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
364 $indexcontent .= ' redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
365 $indexcontent .= "}\n";
366 $indexcontent .= "include_once '".$relpath."/".basename($filetpl)."'\n"; // use .. instead of .
367 $indexcontent .= '// END PHP ?>'."\n";
368 $result = file_put_contents($fileindexsub, $indexcontent);
369 if ($result === false) {
370 dol_syslog("Failed to write file ".$fileindexsub, LOG_WARNING);
371 }
372 dolChmod($fileindexsub);
373 }
374 }
375 }
376 }
377 } else {
378 $result1 = true;
379 }
380
381 if ($filewrapper) {
382 dol_delete_file($filewrapper);
383 $wrappercontent = file_get_contents(DOL_DOCUMENT_ROOT.'/website/samples/wrapper.php');
384
385 $result2 = file_put_contents($filewrapper, $wrappercontent);
386 dolChmod($filewrapper);
387 } else {
388 $result2 = true;
389 }
390
391 return ($result1 && $result2);
392}
393
394
402function dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
403{
404 global $pathofwebsite;
405
406 dol_syslog("Save html header into ".$filehtmlheader);
407
408 dol_mkdir($pathofwebsite);
409 $result = file_put_contents($filehtmlheader, $htmlheadercontent);
410 dolChmod($filehtmlheader);
411
412 return $result;
413}
414
422function dolSaveCssFile($filecss, $csscontent)
423{
424 global $pathofwebsite;
425
426 dol_syslog("Save css file into ".$filecss);
427
428 dol_mkdir($pathofwebsite);
429 $result = file_put_contents($filecss, $csscontent);
430 dolChmod($filecss);
431
432 return $result;
433}
434
442function dolSaveJsFile($filejs, $jscontent)
443{
444 global $pathofwebsite;
445
446 dol_syslog("Save js file into ".$filejs);
447
448 dol_mkdir($pathofwebsite);
449 $result = file_put_contents($filejs, $jscontent);
450 dolChmod($filejs);
451
452 return $result;
453}
454
462function dolSaveRobotFile($filerobot, $robotcontent)
463{
464 global $pathofwebsite;
465
466 dol_syslog("Save robot file into ".$filerobot);
467
468 dol_mkdir($pathofwebsite);
469 $result = file_put_contents($filerobot, $robotcontent);
470 dolChmod($filerobot);
471
472 return $result;
473}
474
482function dolSaveHtaccessFile($filehtaccess, $htaccess)
483{
484 global $pathofwebsite;
485
486 dol_syslog("Save htaccess file into ".$filehtaccess);
487
488 dol_mkdir($pathofwebsite);
489 $result = file_put_contents($filehtaccess, $htaccess);
490 dolChmod($filehtaccess);
491
492 return $result;
493}
494
502function dolSaveManifestJson($file, $content)
503{
504 global $pathofwebsite;
505
506 dol_syslog("Save manifest.js.php file into ".$file);
507
508 dol_mkdir($pathofwebsite);
509 $result = file_put_contents($file, $content);
510 dolChmod($file);
511
512 return $result;
513}
514
522function dolSaveReadme($file, $content)
523{
524 global $pathofwebsite;
525
526 dol_syslog("Save README.md file into ".$file);
527
528 dol_mkdir($pathofwebsite);
529 $result = file_put_contents($file, $content);
530 dolChmod($file);
531
532 return $result;
533}
534
542function dolSaveLicense($file, $content)
543{
544 global $pathofwebsite;
545
546 dol_syslog("Save LICENSE file into ".$file);
547
548 dol_mkdir($pathofwebsite);
549 $result = file_put_contents($file, $content);
550 dolChmod($file);
551
552 return $result;
553}
554
562{
563 global $conf, $langs, $form, $user;
564
565 $dirthemes = array('/doctemplates/websites');
566 /*
567 if (!empty($conf->modules_parts['websitetemplates'])) {
568 foreach ($conf->modules_parts['websitetemplates'] as $reldir) {
569 $dirthemes = array_merge($dirthemes, (array) ($reldir.'doctemplates/websites'));
570 }
571 }
572 */
573 $dirthemes = array_unique($dirthemes);
574 // Now dir_themes=array('/themes') or dir_themes=array('/theme','/mymodule/theme')
575
576 $colspan = 2;
577
578 print '<!-- For website template import -->'."\n";
579 print '<table class="noborder centpercent">';
580
581 // Title
582 print '<tr class="liste_titre"><th class="titlefield">';
583 print $form->textwithpicto($langs->trans("Templates"), $langs->trans("ThemeDir").' : '.implode(", ", $dirthemes));
584 print ' ';
585 print '<a href="'.$_SERVER["PHP_SELF"].'?website='.urlencode($website->ref).'&importsite=1" rel="noopener noreferrer external">';
586 print img_picto('', 'refresh');
587 print '</a>';
588 print '</th>';
589 print '<th class="right">';
590 $url = 'https://www.dolistore.com/43-web-site-templates';
591 print '<a href="'.$url.'" target="_blank" rel="noopener noreferrer external">';
592 print img_picto('', 'globe', 'class="pictofixedwidth"').$langs->trans('DownloadMoreSkins');
593 print '</a>';
594 print '</th></tr>';
595
596 print '<tr><td colspan="'.$colspan.'">';
597
598 print '<table class="nobordernopadding centpercent"><tr><td><div class="display-flex">';
599
600 if (count($dirthemes)) {
601 $i = 0;
602 foreach ($dirthemes as $dir) {
603 if (preg_match('/^\/doctemplates\//', $dir)) {
604 $dirtheme = DOL_DATA_ROOT.$dir; // This include loop on $conf->file->dol_document_root
605 } else {
606 $dirtheme = dol_buildpath($dir); // This include loop on $conf->file->dol_document_root
607 }
608 if (is_dir($dirtheme)) {
609 $handle = opendir($dirtheme);
610 if (is_resource($handle)) {
611 while (($subdir = readdir($handle)) !== false) {
612 //var_dump($dirtheme.'/'.$subdir);
613 if (is_file($dirtheme."/".$subdir) && substr($subdir, 0, 1) != '.' && substr($subdir, 0, 3) != 'CVS' && preg_match('/\.zip$/i', $subdir)) {
614 $subdirwithoutzip = preg_replace('/\.zip$/i', '', $subdir);
615
616 // Disable not stable themes (dir ends with _exp or _dev)
617 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2 && preg_match('/_dev$/i', $subdir)) {
618 continue;
619 }
620 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1 && preg_match('/_exp$/i', $subdir)) {
621 continue;
622 }
623
624 print '<div class="inline-block center flex-item" style="min-width: 250px; max-width: 400px; margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;">';
625
626 $templatedir = $dirtheme."/".$subdir;
627 $file = $dirtheme."/".$subdirwithoutzip.".jpg";
628 $url = DOL_URL_ROOT.'/viewimage.php?modulepart=doctemplateswebsite&file='.$subdirwithoutzip.".jpg";
629
630 if (!file_exists($file)) {
631 $url = DOL_URL_ROOT.'/public/theme/common/nophoto.png';
632 }
633
634 $originalfile = basename($file);
635 $entity = $conf->entity;
636 $modulepart = 'doctemplateswebsite';
637 $cache = '';
638 $title = $file;
639
640 $ret = '';
641 $urladvanced = getAdvancedPreviewUrl($modulepart, $originalfile, 1, '&entity='.$entity);
642 if (!empty($urladvanced)) {
643 $ret .= '<a class="'.$urladvanced['css'].'" target="'.$urladvanced['target'].'" mime="'.$urladvanced['mime'].'" href="'.$urladvanced['url'].'">';
644 } else {
645 $ret .= '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.urlencode($modulepart).'&entity='.((int) $entity).'&file='.urlencode($originalfile).'&cache='.((int) $cache).'">';
646 }
647 print $ret;
648 print '<img class="img-skinthumb shadow" src="'.$url.'" border="0" alt="'.$title.'" title="'.$title.'" style="margin-bottom: 5px;">';
649 print '</a>';
650
651 print '<br>';
652 print $subdir;
653 print '<br>';
654 print '<span class="opacitymedium">'.dol_print_size(dol_filesize($dirtheme."/".$subdir), 1, 1).' - '.dol_print_date(dol_filemtime($templatedir), 'dayhour', 'tzuserrel').'</span>';
655 if ($user->hasRight('website', 'delete')) {
656 print ' <a href="'.$_SERVER["PHP_SELF"].'?action=deletetemplate&token='.newToken().'&website='.urlencode($website->ref).'&templateuserfile='.urlencode($subdir).'">'.img_picto('', 'delete').'</a>';
657 }
658 print '<br><a href="'.$_SERVER["PHP_SELF"].'?action=importsiteconfirm&token='.newToken().'&website='.urlencode($website->ref).'&templateuserfile='.urlencode($subdir).'" class="button">'.$langs->trans("Load").'</a>';
659 print '</div>';
660
661 $i++;
662 }
663 }
664 print '<div class="inline-block center flex-item" style="min-width: 250px; max-width: 400px;margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;"></div>';
665 print '<div class="inline-block center flex-item" style="min-width: 250px; max-width: 400px;margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;"></div>';
666 print '<div class="inline-block center flex-item" style="min-width: 250px; max-width: 400px;margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;"></div>';
667 print '<div class="inline-block center flex-item" style="min-width: 250px; max-width: 400px;margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;"></div>';
668 print '<div class="inline-block center flex-item" style="min-width: 250px; max-width: 400px;margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;"></div>';
669 }
670 }
671 }
672 } else {
673 print '<span class="opacitymedium">'.$langs->trans("None").'</span>';
674 }
675
676 print '</div></td></tr></table>';
677
678 print '</td></tr>';
679 print '</table>';
680}
681
682
693function checkPHPCode(&$phpfullcodestringold, &$phpfullcodestring)
694{
695 global $langs, $user;
696
697 $error = 0;
698
699 if (empty($phpfullcodestringold) && empty($phpfullcodestring)) {
700 return 0;
701 }
702
703 // First check permission
704 if ($phpfullcodestringold != $phpfullcodestring) {
705 if (!$error && !$user->hasRight('website', 'writephp')) {
706 $error++;
707 setEventMessages($langs->trans("NotAllowedToAddDynamicContent"), null, 'errors');
708 }
709 }
710
711 // Then check forbidden commands
712 if (!$error) {
713 $forbiddenphpcommands = array("override_function", "session_id", "session_create_id", "session_regenerate_id");
714 if (!getDolGlobalString('WEBSITE_PHP_ALLOW_EXEC')) { // If option is not on, we disallow functions to execute commands
715 $forbiddenphpcommands = array_merge($forbiddenphpcommands, array("exec", "passthru", "shell_exec", "system", "proc_open", "popen", "eval", "dol_eval", "executeCLI"));
716 }
717 if (!getDolGlobalString('WEBSITE_PHP_ALLOW_WRITE')) { // If option is not on, we disallow functions to write files
718 $forbiddenphpcommands = array_merge($forbiddenphpcommands, array("fopen", "file_put_contents", "fputs", "fputscsv", "fwrite", "fpassthru", "unlink", "mkdir", "rmdir", "symlink", "touch", "umask"));
719 }
720 foreach ($forbiddenphpcommands as $forbiddenphpcommand) {
721 if (preg_match('/'.$forbiddenphpcommand.'\s*\‍(/ms', $phpfullcodestring)) {
722 $error++;
723 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", $forbiddenphpcommand), null, 'errors');
724 break;
725 }
726 }
727 }
728
729 // This char can be used to execute RCE for example using with echo `ls`
730 if (!$error) {
731 $forbiddenphpchars = array();
732 if (!getDolGlobalString('WEBSITE_PHP_ALLOW_DANGEROUS_CHARS')) { // If option is not on, we disallow functions to execute commands
733 $forbiddenphpchars = array("`");
734 }
735 foreach ($forbiddenphpchars as $forbiddenphpchar) {
736 if (preg_match('/'.$forbiddenphpchar.'/ms', $phpfullcodestring)) {
737 $error++;
738 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", $forbiddenphpchar), null, 'errors');
739 break;
740 }
741 }
742 }
743
744 // Deny dynamic functions '${a}(' or '$a[b](' => So we refuse '}(' and ']('
745 if (!$error) {
746 if (preg_match('/[}\]]\‍(/ims', $phpfullcodestring)) {
747 $error++;
748 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", ']('), null, 'errors');
749 }
750 }
751
752 // Deny dynamic functions '$xxx('
753 if (!$error) {
754 if (preg_match('/\$[a-z0-9_\-\/\*]+\‍(/ims', $phpfullcodestring)) {
755 $error++;
756 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", '$...('), null, 'errors');
757 }
758 }
759
760 // No need to block $conf->global->aaa() because PHP try to run method aaa an not function into $conf->global->aaa.
761
762 // Then check if installmodules does not block dynamic PHP code change.
763 if ($phpfullcodestringold != $phpfullcodestring) {
764 if (!$error) {
765 $dolibarrdataroot = preg_replace('/([\\/]+)$/i', '', DOL_DATA_ROOT);
766 $allowimportsite = true;
767 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
768 if (dol_is_file($dolibarrdataroot.'/installmodules.lock')) {
769 $allowimportsite = false;
770 }
771
772 if (!$allowimportsite) {
773 $error++;
774 // Blocked by installmodules.lock
775 if (getDolGlobalString('MAIN_MESSAGE_INSTALL_MODULES_DISABLED_CONTACT_US')) {
776 // Show clean corporate message
777 $message = $langs->trans('InstallModuleFromWebHasBeenDisabledContactUs');
778 } else {
779 // Show technical generic message
780 $message = $langs->trans("InstallModuleFromWebHasBeenDisabledByFile", $dolibarrdataroot.'/installmodules.lock');
781 }
782 setEventMessages($message, null, 'errors');
783 }
784 }
785 }
786
787 return $error;
788}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
Class Website.
Class Websitepage.
dol_filemtime($pathoffile)
Return time of a file.
dol_filesize($pathoffile)
Return size of a file.
archiveOrBackupFile($srcfile, $max_versions=5, $archivedir='', $suffix="v", $moveorcopy='move')
Manage backup versions for a given file, ensuring only a maximum number of versions are kept.
dol_delete_file($file, $disableglob=0, $nophperrors=0, $nohook=0, $object=null, $allowdotdot=false, $indexdatabase=1, $nolog=0)
Remove a file or several files with a mask.
dol_is_file($pathoffile)
Return if path is a file.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getAdvancedPreviewUrl($modulepart, $relativepath, $alldata=0, $param='')
Return URL we can use for advanced preview links.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
dolSaveMasterFile($filemaster)
Save content of a page on disk.
showWebsiteTemplates(Website $website)
Show list of themes.
dolSaveLicense($file, $content)
Save content of a page on disk.
checkPHPCode(&$phpfullcodestringold, &$phpfullcodestring)
Check a new string containing only php code (including <php tag)
dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
Save content of a page on disk.
dolSaveReadme($file, $content)
Save content of a page on disk.
dolSaveManifestJson($file, $content)
Save content of a page on disk.
dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper, $object=null)
Save content of the index.php and/or the wrapper.php page.
dolSavePageAlias($filealias, $object, $objectpage)
Save an alias page on disk (A page that include the reference page).
dolSaveHtaccessFile($filehtaccess, $htaccess)
Save content of a page on disk.
dolSaveJsFile($filejs, $jscontent)
Save content of a page on disk.
dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage, $backupold=0)
Save content of a page on disk (page name is generally ID_of_page.php).
dolSaveCssFile($filecss, $csscontent)
Save content of a page on disk.
dolSaveRobotFile($filerobot, $robotcontent)
Save content of a page on disk.