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