website.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // Security options
  72. // X-Content-Type-Options
  73. header("X-Content-Type-Options: nosniff");
  74. // X-Frame-Options
  75. if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
  76. header("X-Frame-Options: SAMEORIGIN");
  77. }
  78. //httponly_accessforbidden('<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>', 404, 1);
  79. http_response_code(404);
  80. print '<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>';
  81. exit;
  82. }
  83. }
  84. if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
  85. // Security options
  86. // X-Content-Type-Options
  87. header("X-Content-Type-Options: nosniff");
  88. // X-Frame-Options
  89. if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
  90. header("X-Frame-Options: SAMEORIGIN");
  91. }
  92. // X-XSS-Protection
  93. //header("X-XSS-Protection: 1"); // XSS filtering protection of some browsers (note: use of Content-Security-Policy is more efficient). Disabled as deprecated.
  94. // Content-Security-Policy-Report-Only
  95. if (!defined('WEBSITE_MAIN_SECURITY_FORCECSPRO')) {
  96. // A default security policy that keep usage of js external component like ckeditor, stripe, google, working
  97. // For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
  98. // 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: *;
  99. // For example, to restrict everything to itself except img that can be on other servers:
  100. // default-src 'self'; img-src *;
  101. // Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
  102. // default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
  103. //
  104. // $contentsecuritypolicy = "frame-ancestors 'self'; img-src * data:; font-src *; default-src 'self' 'unsafe-inline' 'unsafe-eval' *.paypal.com *.stripe.com *.google.com *.googleapis.com *.google-analytics.com *.googletagmanager.com;";
  105. // $contentsecuritypolicy = "frame-ancestors 'self'; img-src * data:; font-src *; default-src *; script-src 'self' 'unsafe-inline' *.paypal.com *.stripe.com *.google.com *.googleapis.com *.google-analytics.com *.googletagmanager.com; style-src 'self' 'unsafe-inline'; connect-src 'self';";
  106. $contentsecuritypolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSPRO');
  107. if (!is_object($hookmanager)) {
  108. $hookmanager = new HookManager($db);
  109. }
  110. $hookmanager->initHooks(array("main"));
  111. $parameters = array('contentsecuritypolicy'=>$contentsecuritypolicy, 'mode'=>'reportonly');
  112. $result = $hookmanager->executeHooks('setContentSecurityPolicy', $parameters); // Note that $action and $object may have been modified by some hooks
  113. if ($result > 0) {
  114. $contentsecuritypolicy = $hookmanager->resPrint; // Replace CSP
  115. } else {
  116. $contentsecuritypolicy .= $hookmanager->resPrint; // Concat CSP
  117. }
  118. if (!empty($contentsecuritypolicy)) {
  119. header("Content-Security-Policy-Report-Only: ".$contentsecuritypolicy);
  120. }
  121. }
  122. // Content-Security-Policy
  123. if (!defined('WEBSITE_MAIN_SECURITY_FORCECSP')) {
  124. // A default security policy that keep usage of js external component like ckeditor, stripe, google, working
  125. // For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
  126. // 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: *;
  127. // For example, to restrict everything to itself except img that can be on other servers:
  128. // default-src 'self'; img-src *;
  129. // Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
  130. // default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
  131. //
  132. // $contentsecuritypolicy = "frame-ancestors 'self'; img-src * data:; font-src *; default-src 'self' 'unsafe-inline' 'unsafe-eval' *.paypal.com *.stripe.com *.google.com *.googleapis.com *.google-analytics.com *.googletagmanager.com;";
  133. // $contentsecuritypolicy = "frame-ancestors 'self'; img-src * data:; font-src *; default-src *; script-src 'self' 'unsafe-inline' *.paypal.com *.stripe.com *.google.com *.googleapis.com *.google-analytics.com *.googletagmanager.com; style-src 'self' 'unsafe-inline'; connect-src 'self';";
  134. $contentsecuritypolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSP');
  135. if (!is_object($hookmanager)) {
  136. $hookmanager = new HookManager($db);
  137. }
  138. $hookmanager->initHooks(array("main"));
  139. $parameters = array('contentsecuritypolicy'=>$contentsecuritypolicy, 'mode'=>'active');
  140. $result = $hookmanager->executeHooks('setContentSecurityPolicy', $parameters); // Note that $action and $object may have been modified by some hooks
  141. if ($result > 0) {
  142. $contentsecuritypolicy = $hookmanager->resPrint; // Replace CSP
  143. } else {
  144. $contentsecuritypolicy .= $hookmanager->resPrint; // Concat CSP
  145. }
  146. if (!empty($contentsecuritypolicy)) {
  147. header("Content-Security-Policy: ".$contentsecuritypolicy);
  148. }
  149. }
  150. // Referrer-Policy
  151. if (!defined('WEBSITE_MAIN_SECURITY_FORCERP')) {
  152. // The constant WEBSITE_MAIN_SECURITY_FORCERP should never be defined by page, but the variable used just after may be
  153. // For public web sites, we use the same default value than "strict-origin-when-cross-origin"
  154. $referrerpolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCERP', "strict-origin-when-cross-origin");
  155. header("Referrer-Policy: ".$referrerpolicy);
  156. }
  157. // Strict-Transport-Security
  158. if (!defined('WEBSITE_MAIN_SECURITY_FORCESTS')) {
  159. // The constant WEBSITE_MAIN_SECURITY_FORCESTS should never be defined by page, but the variable used just after may be
  160. // Example: "max-age=31536000; includeSubDomains"
  161. $sts = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCESTS');
  162. if (!empty($sts)) {
  163. header("Strict-Transport-Security: ".$sts);
  164. }
  165. }
  166. // Permissions-Policy (old name was Feature-Policy)
  167. if (!defined('WEBSITE_MAIN_SECURITY_FORCEPP')) {
  168. // The constant WEBSITE_MAIN_SECURITY_FORCEPP should never be defined by page, but the variable used just after may be
  169. // Example: "camera: 'none'; microphone: 'none';"
  170. $pp = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCEPP');
  171. if (!empty($pp)) {
  172. header("Permissions-Policy: ".$pp);
  173. }
  174. }
  175. }
  176. // A lang was forced, so we change weblangs init
  177. if (GETPOST('l', 'aZ09')) {
  178. $weblangs->setDefaultLang(GETPOST('l', 'aZ09'));
  179. }
  180. // A lang was forced, so we check to find if we must make a redirect on translation page
  181. if ($_SERVER['PHP_SELF'] != DOL_URL_ROOT.'/website/index.php') { // If we browsing page using Dolibarr server or a Native web server
  182. //print_r(get_defined_constants(true));exit;
  183. if (GETPOST('l', 'aZ09')) {
  184. $sql = "SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
  185. $sql .= " FROM ".MAIN_DB_PREFIX."website_page as wp";
  186. $sql .= " WHERE wp.fk_website = ".((int) $website->id);
  187. $sql .= " AND (wp.fk_page = ".((int) $pageid)." OR wp.rowid = ".((int) $pageid);
  188. if (is_object($websitepage) && $websitepage->fk_page > 0) {
  189. $sql .= " OR wp.fk_page = ".((int) $websitepage->fk_page)." OR wp.rowid = ".((int) $websitepage->fk_page);
  190. }
  191. $sql .= ")";
  192. $sql .= " AND wp.lang = '".$db->escape(GETPOST('l', 'aZ09'))."'";
  193. $resql = $db->query($sql);
  194. if ($resql) {
  195. $obj = $db->fetch_object($resql);
  196. if ($obj) {
  197. $newpageid = $obj->rowid;
  198. if ($newpageid != $pageid) { // To avoid to make a redirect on same page (infinite loop)
  199. if (defined('USEDOLIBARRSERVER')) {
  200. header("Location: ".DOL_URL_ROOT.'/public/website/index.php?website='.$websitekey.'&pageid='.$newpageid.'&l='.GETPOST('l', 'aZ09'));
  201. exit;
  202. } else {
  203. $newpageref = $obj->pageurl;
  204. header("Location: ".(($obj->lang && $obj->lang != $website->lang) ? '/'.$obj->lang.'/' : '/').$newpageref.'.php?l='.GETPOST('l', 'aZ09'));
  205. exit;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. // Show off line message when all website is off
  213. if (!defined('USEDOLIBARREDITOR') && empty($website->status)) {
  214. // Security options
  215. // X-Content-Type-Options
  216. header("X-Content-Type-Options: nosniff");
  217. // X-Frame-Options
  218. if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
  219. header("X-Frame-Options: SAMEORIGIN");
  220. }
  221. $weblangs->load("website");
  222. //httponly_accessforbidden('<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>', 503, 1);
  223. http_response_code(503);
  224. print '<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>';
  225. exit;
  226. }