website.inc.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /* Copyright (C) 2017-2019 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. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/website.inc.php
  20. * \brief Common file loaded by all website pages (after master.inc.php). It set the new object $weblangs, using parameter 'l'.
  21. * This file is included in top of all container pages and is run only when a web page is called.
  22. * The global variable $websitekey must be defined.
  23. */
  24. // Load website class
  25. include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  27. $website = null;
  28. $websitepage = null;
  29. $weblangs = null;
  30. $pagelangs = null;
  31. // Detection browser (copy of code from main.inc.php)
  32. if (isset($_SERVER["HTTP_USER_AGENT"]) && is_object($conf) && empty($conf->browser->name)) {
  33. $tmp = getBrowserInfo($_SERVER["HTTP_USER_AGENT"]);
  34. $conf->browser->name = $tmp['browsername'];
  35. $conf->browser->os = $tmp['browseros'];
  36. $conf->browser->version = $tmp['browserversion'];
  37. $conf->browser->layout = $tmp['layout']; // 'classic', 'phone', 'tablet'
  38. //var_dump($conf->browser);
  39. if ($conf->browser->layout == 'phone') {
  40. $conf->dol_no_mouse_hover = 1;
  41. }
  42. }
  43. // Define $website
  44. if (!is_object($website)) {
  45. $website = new Website($db);
  46. $website->fetch(0, $websitekey);
  47. }
  48. // Define $websitepage if we have $websitepagefile defined
  49. if (!$pageid && !empty($websitepagefile)) {
  50. $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
  51. if ($pageid == 'index.php') {
  52. $pageid = $website->fk_default_home;
  53. }
  54. }
  55. if (!is_object($websitepage)) {
  56. $websitepage = new WebsitePage($db);
  57. }
  58. // Define $weblangs
  59. if (!is_object($weblangs)) {
  60. $weblangs = new Translate('', $conf);
  61. }
  62. if (!is_object($pagelangs)) {
  63. $pagelangs = new Translate('', $conf);
  64. }
  65. if ($pageid > 0) {
  66. $websitepage->fetch($pageid);
  67. $weblangs->setDefaultLang(GETPOSTISSET('lang') ? GETPOST('lang', 'aZ09') : (empty($_COOKIE['weblangs-shortcode']) ? 'auto' : preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['weblangs-shortcode'])));
  68. $pagelangs->setDefaultLang($websitepage->lang ? $websitepage->lang : $weblangs->shortlang);
  69. if (!defined('USEDOLIBARREDITOR') && (in_array($websitepage->type_container, array('menu', 'other')) || empty($websitepage->status) && !defined('USEDOLIBARRSERVER'))) {
  70. $weblangs->load("website");
  71. http_response_code(404);
  72. print '<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>';
  73. exit;
  74. }
  75. }
  76. if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
  77. // Security options
  78. // X-Content-Type-Options
  79. header("X-Content-Type-Options: nosniff");
  80. // X-Frame-Options
  81. if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
  82. header("X-Frame-Options: SAMEORIGIN");
  83. }
  84. // X-XSS-Protection
  85. //header("X-XSS-Protection: 1"); // XSS filtering protection of some browsers (note: use of Content-Security-Policy is more efficient). Disabled as deprecated.
  86. // Content-Security-Policy
  87. if (!defined('WEBSITE_MAIN_SECURITY_FORCECSP')) {
  88. // The constant WEBSITE_MAIN_SECURITY_FORCECSP should never be defined by page, but the variable used just after may be
  89. // A default security policy that keep usage of js external component like ckeditor, stripe, google, working
  90. // $contentsecuritypolicy = "font-src *; img-src *; style-src * 'unsafe-inline' 'unsafe-eval'; default-src 'self' *.stripe.com 'unsafe-inline' 'unsafe-eval'; script-src 'self' *.stripe.com 'unsafe-inline' 'unsafe-eval'; frame-src 'self' *.stripe.com; connect-src 'self';";
  91. $contentsecuritypolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSP');
  92. if (!is_object($hookmanager)) {
  93. $hookmanager = new HookManager($db);
  94. }
  95. $hookmanager->initHooks(array("main"));
  96. $parameters = array('contentsecuritypolicy'=>$contentsecuritypolicy);
  97. $result = $hookmanager->executeHooks('setContentSecurityPolicy', $parameters); // Note that $action and $object may have been modified by some hooks
  98. if ($result > 0) {
  99. $contentsecuritypolicy = $hookmanager->resPrint; // Replace CSP
  100. } else {
  101. $contentsecuritypolicy .= $hookmanager->resPrint; // Concat CSP
  102. }
  103. if (!empty($contentsecuritypolicy)) {
  104. // For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
  105. // default-src 'self'; style-src: https://cdnjs.cloudflare.com https://fonts.googleapis.com; script-src: https://cdn.transifex.com https://www.googletagmanager.com; object-src https://youtube.com; frame-src https://youtube.com; img-src: *;
  106. // For example, to restrict everything to itself except img that can be on other servers:
  107. // default-src 'self'; img-src *;
  108. // Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
  109. // default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
  110. header("Content-Security-Policy: ".$contentsecuritypolicy);
  111. }
  112. }
  113. }
  114. // A lang was forced, so we change weblangs init
  115. if (GETPOST('l', 'aZ09')) {
  116. $weblangs->setDefaultLang(GETPOST('l', 'aZ09'));
  117. }
  118. // A lang was forced, so we check to find if we must make a redirect on translation page
  119. if ($_SERVER['PHP_SELF'] != DOL_URL_ROOT.'/website/index.php') { // If we browsing page using Dolibarr server or a Native web server
  120. //print_r(get_defined_constants(true));exit;
  121. if (GETPOST('l', 'aZ09')) {
  122. $sql = "SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
  123. $sql .= " FROM ".MAIN_DB_PREFIX."website_page as wp";
  124. $sql .= " WHERE wp.fk_website = ".((int) $website->id);
  125. $sql .= " AND (wp.fk_page = ".((int) $pageid)." OR wp.rowid = ".((int) $pageid);
  126. if (is_object($websitepage) && $websitepage->fk_page > 0) {
  127. $sql .= " OR wp.fk_page = ".((int) $websitepage->fk_page)." OR wp.rowid = ".((int) $websitepage->fk_page);
  128. }
  129. $sql .= ")";
  130. $sql .= " AND wp.lang = '".$db->escape(GETPOST('l', 'aZ09'))."'";
  131. $resql = $db->query($sql);
  132. if ($resql) {
  133. $obj = $db->fetch_object($resql);
  134. if ($obj) {
  135. $newpageid = $obj->rowid;
  136. if ($newpageid != $pageid) { // To avoid to make a redirect on same page (infinite loop)
  137. if (defined('USEDOLIBARRSERVER')) {
  138. header("Location: ".DOL_URL_ROOT.'/public/website/index.php?website='.$websitekey.'&pageid='.$newpageid.'&l='.GETPOST('l', 'aZ09'));
  139. exit;
  140. } else {
  141. $newpageref = $obj->pageurl;
  142. header("Location: ".(($obj->lang && $obj->lang != $website->lang) ? '/'.$obj->lang.'/' : '/').$newpageref.'.php?l='.GETPOST('l', 'aZ09'));
  143. exit;
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. // Show off line message
  151. if (!defined('USEDOLIBARREDITOR') && empty($website->status)) {
  152. $weblangs->load("website");
  153. http_response_code(503);
  154. print '<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>';
  155. exit;
  156. }