dolibarr 18.0.6
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
32function dolSaveMasterFile($filemaster)
33{
34 global $conf;
35
36 // Now generate the master.inc.php page
37 dol_syslog("We regenerate the master file");
38 dol_delete_file($filemaster);
39
40 $mastercontent = '<?php'."\n";
41 $mastercontent .= '// File generated to link to the master file - DO NOT MODIFY - It is just an include'."\n";
42 $mastercontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
43 $mastercontent .= " if (! defined('USEEXTERNALSERVER')) define('USEEXTERNALSERVER', 1);\n";
44 $mastercontent .= " require_once '".DOL_DOCUMENT_ROOT."/master.inc.php';\n";
45 $mastercontent .= "}\n";
46 $mastercontent .= '?>'."\n";
47 $result = file_put_contents($filemaster, $mastercontent);
48 dolChmod($filemaster);
49
50 return $result;
51}
52
63function dolSavePageAlias($filealias, $object, $objectpage)
64{
65 global $conf;
66
67 // Now create the .tpl file
68 dol_syslog("dolSavePageAlias We regenerate the alias page filealias=".$filealias);
69
70 $aliascontent = '<?php'."\n";
71 $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
72 $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
73 $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->id.'.tpl.php\'; ';
74 $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
75 $aliascontent .= '?>'."\n";
76 $result = file_put_contents($filealias, $aliascontent);
77 if ($result === false) {
78 dol_syslog("Failed to write file ".$filealias, LOG_WARNING);
79 }
80 dolChmod($filealias);
81
82 // Save also alias into language subdirectory if it is not a main language
83 if ($objectpage->lang && in_array($objectpage->lang, explode(',', $object->otherlang))) {
84 $dirname = dirname($filealias);
85 $filename = basename($filealias);
86 $filealiassub = $dirname.'/'.$objectpage->lang.'/'.$filename;
87
88 $aliascontent = '<?php'."\n";
89 $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
90 $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
91 $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'../page'.$objectpage->id.'.tpl.php\'; ';
92 $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
93 $aliascontent .= '?>'."\n";
94 $result = file_put_contents($filealiassub, $aliascontent);
95 if ($result === false) {
96 dol_syslog("Failed to write file ".$filealiassub, LOG_WARNING);
97 }
98 dolChmod($filealiassub);
99 } elseif (empty($objectpage->lang) || !in_array($objectpage->lang, explode(',', $object->otherlang))) {
100 // Save also alias into all language subdirectories if it is a main language
101 if (empty($conf->global->WEBSITE_DISABLE_MAIN_LANGUAGE_INTO_LANGSUBDIR) && !empty($object->otherlang)) {
102 $dirname = dirname($filealias);
103 $filename = basename($filealias);
104 foreach (explode(',', $object->otherlang) as $sublang) {
105 // Avoid to erase main alias file if $sublang is empty string
106 if (empty(trim($sublang))) continue;
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 $result = file_put_contents($filealiassub, $aliascontent);
116 if ($result === false) {
117 dol_syslog("Failed to write file ".$filealiassub, LOG_WARNING);
118 }
119 dolChmod($filealiassub);
120 }
121 }
122 }
123
124 return ($result ?true:false);
125}
126
127
139function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage, $backupold = 0)
140{
141 global $conf, $db;
142
143 // Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
144 dol_syslog("dolSavePageContent We regenerate the tpl page filetpl=".$filetpl);
145
146 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
147
148 if (dol_is_file($filetpl)) {
149 if ($backupold) {
150 dol_delete_file($filetpl.'.old');
151 $result = dol_move($filetpl, $filetpl.'.old', 0, 1, 0, 0);
152 if (! $result) {
153 return false;
154 }
155 } else {
156 dol_delete_file($filetpl);
157 }
158 }
159
160 $shortlangcode = '';
161 if ($objectpage->lang) {
162 $shortlangcode = substr($objectpage->lang, 0, 2); // en_US or en-US -> en
163 }
164 if (empty($shortlangcode)) {
165 $shortlangcode = substr($object->lang, 0, 2); // en_US or en-US -> en
166 }
167
168 $tplcontent = '';
169 $tplcontent .= "<?php // BEGIN PHP\n";
170 $tplcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
171 $tplcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
172 $tplcontent .= ' $pathdepth = count(explode(\'/\', $_SERVER[\'SCRIPT_NAME\'])) - 2;'."\n";
173 $tplcontent .= ' require_once ($pathdepth ? str_repeat(\'../\', $pathdepth) : \'./\').\'master.inc.php\';'."\n";
174 $tplcontent .= "} // Not already loaded\n";
175 $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
176 $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
177 $tplcontent .= "ob_start();\n";
178 $tplcontent .= "// END PHP ?>\n";
179 if (!empty($conf->global->WEBSITE_FORCE_DOCTYPE_HTML5)) {
180 $tplcontent .= "<!DOCTYPE html>\n";
181 }
182 $tplcontent .= '<html'.($shortlangcode ? ' lang="'.$shortlangcode.'"' : '').'>'."\n";
183 $tplcontent .= '<head>'."\n";
184 $tplcontent .= '<title>'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'</title>'."\n";
185 $tplcontent .= '<meta charset="utf-8">'."\n";
186 $tplcontent .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'."\n";
187 $tplcontent .= '<meta name="robots" content="index, follow" />'."\n";
188 $tplcontent .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\n";
189 $tplcontent .= '<meta name="keywords" content="'.dol_string_nohtmltag($objectpage->keywords).'" />'."\n";
190 $tplcontent .= '<meta name="title" content="'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'" />'."\n";
191 $tplcontent .= '<meta name="description" content="'.dol_string_nohtmltag($objectpage->description, 0, 'UTF-8').'" />'."\n";
192 $tplcontent .= '<meta name="generator" content="'.DOL_APPLICATION_TITLE.' '.DOL_VERSION.' (https://www.dolibarr.org)" />'."\n";
193 $tplcontent .= '<meta name="dolibarr:pageid" content="'.dol_string_nohtmltag($objectpage->id).'" />'."\n";
194 // Add canonical reference
195 if ($object->virtualhost) {
196 $tplcontent .= '<link rel="canonical" href="'.(($objectpage->id == $object->fk_default_home) ? '/' : (($shortlangcode != substr($object->lang, 0, 2) ? '/'.$shortlangcode : '').'/'.$objectpage->pageurl.'.php')).'" />'."\n";
197 }
198 // Add translation reference (main language)
199 if ($object->isMultiLang()) {
200 // Add page "translation of"
201 $translationof = $objectpage->fk_page;
202 if ($translationof) {
203 $tmppage = new WebsitePage($db);
204 $tmppage->fetch($translationof);
205 if ($tmppage->id > 0) {
206 $tmpshortlangcode = '';
207 if ($tmppage->lang) {
208 $tmpshortlangcode = preg_replace('/[_-].*$/', '', $tmppage->lang); // en_US or en-US -> en
209 }
210 if (empty($tmpshortlangcode)) {
211 $tmpshortlangcode = preg_replace('/[_-].*$/', '', $object->lang); // en_US or en-US -> en
212 }
213 if ($tmpshortlangcode != $shortlangcode) {
214 $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";
215 }
216 }
217 }
218
219 // Add "has translation pages"
220 $sql = "SELECT rowid as id, lang, pageurl from ".MAIN_DB_PREFIX.'website_page where fk_page IN ('.$db->sanitize($objectpage->id.($translationof ? ", ".$translationof : '')).")";
221 $resql = $db->query($sql);
222 if ($resql) {
223 $num_rows = $db->num_rows($resql);
224 if ($num_rows > 0) {
225 while ($obj = $db->fetch_object($resql)) {
226 $tmpshortlangcode = '';
227 if ($obj->lang) {
228 $tmpshortlangcode = preg_replace('/[_-].*$/', '', $obj->lang); // en_US or en-US -> en
229 }
230 if ($tmpshortlangcode != $shortlangcode) {
231 $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";
232 }
233 }
234 }
235 } else {
236 dol_print_error($db);
237 }
238
239 // Add myself
240 $tplcontent .= '<?php if ($_SERVER["PHP_SELF"] == "'.(($object->fk_default_home == $objectpage->id) ? '/' : (($shortlangcode != substr($object->lang, 0, 2)) ? '/'.$shortlangcode : '')).'/'.$objectpage->pageurl.'.php") { ?>'."\n";
241 $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";
242
243 $tplcontent .= '<?php } ?>'."\n";
244 }
245 // Add manifest.json. Do we have to add it only on home page ?
246 $tplcontent .= '<?php if ($website->use_manifest) { print \'<link rel="manifest" href="/manifest.json.php" />\'."\n"; } ?>'."\n";
247 $tplcontent .= '<!-- Include link to CSS file -->'."\n";
248 // Add js
249 $tplcontent .= '<link rel="stylesheet" href="/styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
250 $tplcontent .= '<!-- Include link to JS file -->'."\n";
251 $tplcontent .= '<script nonce="'.getNonce().'" async src="/javascript.js.php"></script>'."\n";
252 // Add headers
253 $tplcontent .= '<!-- Include HTML header from common file -->'."\n";
254 $tplcontent .= '<?php if (file_exists(DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html")) include DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html"; ?>'."\n";
255 $tplcontent .= '<!-- Include HTML header from page header block -->'."\n";
256 $tplcontent .= preg_replace('/<\/?html>/ims', '', $objectpage->htmlheader)."\n";
257 $tplcontent .= '</head>'."\n";
258
259 $tplcontent .= '<!-- File generated by Dolibarr website module editor -->'."\n";
260 $tplcontent .= '<body id="bodywebsite" class="bodywebsite bodywebpage-'.$objectpage->ref.'">'."\n";
261 $tplcontent .= $objectpage->content."\n";
262 $tplcontent .= '</body>'."\n";
263 $tplcontent .= '</html>'."\n";
264
265 $tplcontent .= '<?php // BEGIN PHP'."\n";
266 $tplcontent .= '$tmp = ob_get_contents(); ob_end_clean(); dolWebsiteOutput($tmp, "html", '.$objectpage->id.'); dolWebsiteIncrementCounter('.$object->id.', "'.$objectpage->type_container.'", '.$objectpage->id.');'."\n";
267 $tplcontent .= "// END PHP ?>\n";
268
269 //var_dump($filetpl);exit;
270 $result = file_put_contents($filetpl, $tplcontent);
271
272 dolChmod($filetpl);
273
274 return $result;
275}
276
277
288function dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper, $object = null)
289{
290 global $conf, $db;
291
292 $result1 = false;
293 $result2 = false;
294
295 dol_mkdir($pathofwebsite);
296
297 if ($fileindex) {
298 dol_delete_file($fileindex);
299 $indexcontent = '<?php'."\n";
300 $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";
301 $indexcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
302 $indexcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Load master if not already loaded\n";
303 $indexcontent .= 'if (!empty($_GET[\'pageref\']) || !empty($_GET[\'pagealiasalt\']) || !empty($_GET[\'pageid\'])) {'."\n";
304 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
305 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
306 $indexcontent .= ' redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
307 $indexcontent .= "}\n";
308 $indexcontent .= "include_once './".basename($filetpl)."'\n";
309 $indexcontent .= '// END PHP ?>'."\n";
310
311 $result1 = file_put_contents($fileindex, $indexcontent);
312
313 dolChmod($fileindex);
314
315 if (is_object($object) && $object->fk_default_home > 0) {
316 $objectpage = new WebsitePage($db);
317 $objectpage->fetch($object->fk_default_home);
318
319 // Create a version for sublanguages
320 if (empty($objectpage->lang) || !in_array($objectpage->lang, explode(',', $object->otherlang))) {
321 if (empty($conf->global->WEBSITE_DISABLE_MAIN_LANGUAGE_INTO_LANGSUBDIR) && is_object($object) && !empty($object->otherlang)) {
322 $dirname = dirname($fileindex);
323 foreach (explode(',', $object->otherlang) as $sublang) {
324 // Avoid to erase main alias file if $sublang is empty string
325 if (empty(trim($sublang))) continue;
326 $fileindexsub = $dirname.'/'.$sublang.'/index.php';
327
328 // Same indexcontent than previously but with ../ instead of ./ for master and tpl file include/require_once.
329 $relpath = '..';
330 $indexcontent = '<?php'."\n";
331 $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";
332 $indexcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
333 $indexcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once '".$relpath."/master.inc.php'; } // Load master if not already loaded\n";
334 $indexcontent .= 'if (!empty($_GET[\'pageref\']) || !empty($_GET[\'pagealiasalt\']) || !empty($_GET[\'pageid\'])) {'."\n";
335 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
336 $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
337 $indexcontent .= ' redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
338 $indexcontent .= "}\n";
339 $indexcontent .= "include_once '".$relpath."/".basename($filetpl)."'\n"; // use .. instead of .
340 $indexcontent .= '// END PHP ?>'."\n";
341 $result = file_put_contents($fileindexsub, $indexcontent);
342 if ($result === false) {
343 dol_syslog("Failed to write file ".$fileindexsub, LOG_WARNING);
344 }
345 dolChmod($fileindexsub);
346 }
347 }
348 }
349 }
350 } else {
351 $result1 = true;
352 }
353
354 if ($filewrapper) {
355 dol_delete_file($filewrapper);
356 $wrappercontent = file_get_contents(DOL_DOCUMENT_ROOT.'/website/samples/wrapper.php');
357
358 $result2 = file_put_contents($filewrapper, $wrappercontent);
359 dolChmod($filewrapper);
360 } else {
361 $result2 = true;
362 }
363
364 return ($result1 && $result2);
365}
366
367
375function dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
376{
377 global $conf, $pathofwebsite;
378
379 dol_syslog("Save html header into ".$filehtmlheader);
380
381 dol_mkdir($pathofwebsite);
382 $result = file_put_contents($filehtmlheader, $htmlheadercontent);
383 dolChmod($filehtmlheader);
384
385 return $result;
386}
387
395function dolSaveCssFile($filecss, $csscontent)
396{
397 global $conf, $pathofwebsite;
398
399 dol_syslog("Save css file into ".$filecss);
400
401 dol_mkdir($pathofwebsite);
402 $result = file_put_contents($filecss, $csscontent);
403 dolChmod($filecss);
404
405 return $result;
406}
407
415function dolSaveJsFile($filejs, $jscontent)
416{
417 global $conf, $pathofwebsite;
418
419 dol_syslog("Save js file into ".$filejs);
420
421 dol_mkdir($pathofwebsite);
422 $result = file_put_contents($filejs, $jscontent);
423 dolChmod($filejs);
424
425 return $result;
426}
427
435function dolSaveRobotFile($filerobot, $robotcontent)
436{
437 global $conf, $pathofwebsite;
438
439 dol_syslog("Save robot file into ".$filerobot);
440
441 dol_mkdir($pathofwebsite);
442 $result = file_put_contents($filerobot, $robotcontent);
443 dolChmod($filerobot);
444
445 return $result;
446}
447
455function dolSaveHtaccessFile($filehtaccess, $htaccess)
456{
457 global $conf, $pathofwebsite;
458
459 dol_syslog("Save htaccess file into ".$filehtaccess);
460
461 dol_mkdir($pathofwebsite);
462 $result = file_put_contents($filehtaccess, $htaccess);
463 dolChmod($filehtaccess);
464
465 return $result;
466}
467
475function dolSaveManifestJson($file, $content)
476{
477 global $conf, $pathofwebsite;
478
479 dol_syslog("Save manifest.js.php file into ".$file);
480
481 dol_mkdir($pathofwebsite);
482 $result = file_put_contents($file, $content);
483 dolChmod($file);
484
485 return $result;
486}
487
495function dolSaveReadme($file, $content)
496{
497 global $conf, $pathofwebsite;
498
499 dol_syslog("Save README.md file into ".$file);
500
501 dol_mkdir($pathofwebsite);
502 $result = file_put_contents($file, $content);
503 dolChmod($file);
504
505 return $result;
506}
507
515function dolSaveLicense($file, $content)
516{
517 global $conf, $pathofwebsite;
518
519 dol_syslog("Save LICENSE file into ".$file);
520
521 dol_mkdir($pathofwebsite);
522 $result = file_put_contents($file, $content);
523 dolChmod($file);
524
525 return $result;
526}
527
535{
536 global $conf, $langs, $db, $form, $user;
537
538 $dirthemes = array('/doctemplates/websites');
539 if (!empty($conf->modules_parts['websitetemplates'])) { // Using this feature slow down application
540 foreach ($conf->modules_parts['websitetemplates'] as $reldir) {
541 $dirthemes = array_merge($dirthemes, (array) ($reldir.'doctemplates/websites'));
542 }
543 }
544 $dirthemes = array_unique($dirthemes);
545 // Now dir_themes=array('/themes') or dir_themes=array('/theme','/mymodule/theme')
546
547 $colspan = 2;
548
549 print '<!-- For website template import -->'."\n";
550 print '<table class="noborder centpercent">';
551
552 // Title
553 print '<tr class="liste_titre"><th class="titlefield">';
554 print $form->textwithpicto($langs->trans("Templates"), $langs->trans("ThemeDir").' : '.join(", ", $dirthemes));
555 print ' ';
556 print '<a href="'.$_SERVER["PHP_SELF"].'?website='.urlencode($website->ref).'&importsite=1" target="_blank" rel="noopener noreferrer external">';
557 print img_picto('', 'refresh');
558 print '</a>';
559 print '</th>';
560 print '<th class="right">';
561 $url = 'https://www.dolistore.com/43-web-site-templates';
562 print '<a href="'.$url.'" target="_blank" rel="noopener noreferrer external">';
563 print img_picto('', 'globe', 'class="pictofixedwidth"').$langs->trans('DownloadMoreSkins');
564 print '</a>';
565 print '</th></tr>';
566
567 print '<tr><td colspan="'.$colspan.'">';
568
569 print '<table class="nobordernopadding centpercent"><tr><td><div class="center">';
570
571 if (count($dirthemes)) {
572 $i = 0;
573 foreach ($dirthemes as $dir) {
574 //print $dirroot.$dir;exit;
575 $dirtheme = DOL_DATA_ROOT.$dir; // This include loop on $conf->file->dol_document_root
576 if (is_dir($dirtheme)) {
577 $handle = opendir($dirtheme);
578 if (is_resource($handle)) {
579 while (($subdir = readdir($handle)) !== false) {
580 if (is_file($dirtheme."/".$subdir) && substr($subdir, 0, 1) <> '.' && substr($subdir, 0, 3) <> 'CVS' && preg_match('/\.zip$/i', $subdir)) {
581 $subdirwithoutzip = preg_replace('/\.zip$/i', '', $subdir);
582
583 // Disable not stable themes (dir ends with _exp or _dev)
584 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 2 && preg_match('/_dev$/i', $subdir)) {
585 continue;
586 }
587 if (getDolGlobalInt('MAIN_FEATURES_LEVEL') < 1 && preg_match('/_exp$/i', $subdir)) {
588 continue;
589 }
590
591 print '<div class="inline-block" style="margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;">';
592
593 $templatedir = $dirtheme."/".$subdir;
594 $file = $dirtheme."/".$subdirwithoutzip.".jpg";
595 $url = DOL_URL_ROOT.'/viewimage.php?modulepart=doctemplateswebsite&file='.$subdirwithoutzip.".jpg";
596
597 if (!file_exists($file)) {
598 $url = DOL_URL_ROOT.'/public/theme/common/nophoto.png';
599 }
600
601 $originalfile = basename($file);
602 $entity = $conf->entity;
603 $modulepart = 'doctemplateswebsite';
604 $cache = '';
605 $title = $file;
606
607 $ret = '';
608 $urladvanced = getAdvancedPreviewUrl($modulepart, $originalfile, 1, '&entity='.$entity);
609 if (!empty($urladvanced)) {
610 $ret .= '<a class="'.$urladvanced['css'].'" target="'.$urladvanced['target'].'" mime="'.$urladvanced['mime'].'" href="'.$urladvanced['url'].'">';
611 } else {
612 $ret .= '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.urlencode($modulepart).'&entity='.((int) $entity).'&file='.urlencode($originalfile).'&cache='.((int) $cache).'">';
613 }
614 print $ret;
615 print '<img class="img-skinthumb shadow" src="'.$url.'" border="0" alt="'.$title.'" title="'.$title.'" style="margin-bottom: 5px;">';
616 print '</a>';
617
618 print '<br>';
619 print $subdir;
620 print '<br>';
621 print '<span class="opacitymedium">'.dol_print_size(dol_filesize($dirtheme."/".$subdir), 1, 1).' - '.dol_print_date(dol_filemtime($templatedir), 'dayhour', 'tzuserrel').'</span>';
622 if ($user->hasRight('website', 'delete')) {
623 print ' <a href="'.$_SERVER["PHP_SELF"].'?action=deletetemplate&token='.newToken().'&website='.urlencode($website->ref).'&templateuserfile='.urlencode($subdir).'">'.img_picto('', 'delete').'</a>';
624 }
625 print '<br><a href="'.$_SERVER["PHP_SELF"].'?action=importsiteconfirm&token='.newToken().'&website='.urlencode($website->ref).'&templateuserfile='.urlencode($subdir).'" class="button">'.$langs->trans("Load").'</a>';
626 print '</div>';
627
628 $i++;
629 }
630 }
631 }
632 }
633 }
634 } else {
635 print '<span class="opacitymedium">'.$langs->trans("None").'</span>';
636 }
637
638 print '</div></td></tr></table>';
639
640 print '</td></tr>';
641 print '</table>';
642}
643
644
655function checkPHPCode($phpfullcodestringold, $phpfullcodestring)
656{
657 global $conf, $langs, $user;
658
659 $error = 0;
660
661 if (empty($phpfullcodestringold) && empty($phpfullcodestring)) {
662 return 0;
663 }
664
665 // First check forbidden commands
666 $forbiddenphpcommands = array();
667 if (empty($conf->global->WEBSITE_PHP_ALLOW_EXEC)) { // If option is not on, we disallow functions to execute commands
668 $forbiddenphpcommands = array("exec", "passthru", "shell_exec", "system", "proc_open", "popen", "eval", "dol_eval", "executeCLI");
669 }
670 if (empty($conf->global->WEBSITE_PHP_ALLOW_WRITE)) { // If option is not on, we disallow functions to write files
671 $forbiddenphpcommands = array_merge($forbiddenphpcommands, array("fopen", "file_put_contents", "fputs", "fputscsv", "fwrite", "fpassthru", "unlink", "mkdir", "rmdir", "symlink", "touch", "umask"));
672 }
673 foreach ($forbiddenphpcommands as $forbiddenphpcommand) {
674 if (preg_match('/'.$forbiddenphpcommand.'\s*\‍(/ms', $phpfullcodestring)) {
675 $error++;
676 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", $forbiddenphpcommand), null, 'errors');
677 break;
678 }
679 }
680 // This char can be used to execute RCE for example using with echo `ls`
681 $forbiddenphpchars = array();
682 if (empty($conf->global->WEBSITE_PHP_ALLOW_DANGEROUS_CHARS)) { // If option is not on, we disallow functions to execute commands
683 $forbiddenphpchars = array("`");
684 }
685 foreach ($forbiddenphpchars as $forbiddenphpchar) {
686 if (preg_match('/'.$forbiddenphpchar.'/ms', $phpfullcodestring)) {
687 $error++;
688 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", $forbiddenphpchar), null, 'errors');
689 break;
690 }
691 }
692 // Deny dynamic functions '${a}(' or '$a[b](' - So we refuse '}(' and ']('
693 if (preg_match('/[}\]]\‍(/ims', $phpfullcodestring)) {
694 $error++;
695 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", ']('), null, 'errors');
696 }
697 // Deny dynamic functions $xxx(
698 if (preg_match('/\$[a-z0-9_]+\‍(/ims', $phpfullcodestring)) {
699 $error++;
700 setEventMessages($langs->trans("DynamicPHPCodeContainsAForbiddenInstruction", '$...('), null, 'errors');
701 }
702
703 if ($phpfullcodestringold != $phpfullcodestring) {
704 if (!$error && empty($user->rights->website->writephp)) {
705 $error++;
706 setEventMessages($langs->trans("NotAllowedToAddDynamicContent"), null, 'errors');
707 }
708 if (!$error) {
709 $dolibarrdataroot = preg_replace('/([\\/]+)$/i', '', DOL_DATA_ROOT);
710 $allowimportsite = true;
711 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
712 if (dol_is_file($dolibarrdataroot.'/installmodules.lock')) {
713 $allowimportsite = false;
714 }
715
716 if (!$allowimportsite) {
717 $error++;
718 // Blocked by installmodules.lock
719 if (getDolGlobalString('MAIN_MESSAGE_INSTALL_MODULES_DISABLED_CONTACT_US')) {
720 // Show clean corporate message
721 $message = $langs->trans('InstallModuleFromWebHasBeenDisabledContactUs');
722 } else {
723 // Show technical generic message
724 $message = $langs->trans("InstallModuleFromWebHasBeenDisabledByFile", $dolibarrdataroot.'/installmodules.lock');
725 }
726 setEventMessages($message, null, 'errors');
727 }
728 }
729 }
730
731 return $error;
732}
Class Website.
Class Websitepage.
dol_move($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=1, $moreinfo=array())
Move a file into another name.
dol_filemtime($pathoffile)
Return time of a file.
dol_filesize($pathoffile)
Return size of a file.
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.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolChmod($filepath, $newmask='')
Change mod of a file.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
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.
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)
checkPHPCode($phpfullcodestringold, $phpfullcodestring)
Check a new string containing only php code (including <php tag)
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.
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.