2017-07-20 16:49:20 +02:00
< ? php
2019-11-12 13:55:46 +01:00
/* Copyright ( C ) 2017 - 2019 Laurent Destailleur < eldy @ users . sourceforge . net >
2017-07-20 16:49:20 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2017-07-20 16:49:20 +02:00
*/
/**
* \file htdocs / core / website . inc . php
2018-10-03 19:53:33 +02:00
* \brief Common file loaded by all website pages ( after master . inc . php ) . It set the new object $weblangs , using parameter 'l' .
2020-05-06 04:32:48 +02:00
* This file is included in top of all container pages and is run only when a web page is called .
2018-09-25 16:35:59 +02:00
* The global variable $websitekey must be defined .
2017-07-20 16:49:20 +02:00
*/
2018-10-03 20:58:45 +02:00
// Load website class
2017-10-20 23:48:42 +02:00
include_once DOL_DOCUMENT_ROOT . '/website/class/website.class.php' ;
2019-04-30 13:50:03 +02:00
include_once DOL_DOCUMENT_ROOT . '/website/class/websitepage.class.php' ;
2022-03-16 15:56:39 +01:00
$website = null ;
$websitepage = null ;
$weblangs = null ;
$pagelangs = null ;
2020-05-15 18:41:46 +02:00
// Detection browser (copy of code from main.inc.php)
2021-02-23 22:03:23 +01:00
if ( isset ( $_SERVER [ " HTTP_USER_AGENT " ]) && is_object ( $conf ) && empty ( $conf -> browser -> name )) {
2020-05-15 18:41:46 +02:00
$tmp = getBrowserInfo ( $_SERVER [ " HTTP_USER_AGENT " ]);
$conf -> browser -> name = $tmp [ 'browsername' ];
$conf -> browser -> os = $tmp [ 'browseros' ];
$conf -> browser -> version = $tmp [ 'browserversion' ];
$conf -> browser -> layout = $tmp [ 'layout' ]; // 'classic', 'phone', 'tablet'
//var_dump($conf->browser);
2021-02-23 22:03:23 +01:00
if ( $conf -> browser -> layout == 'phone' ) {
$conf -> dol_no_mouse_hover = 1 ;
}
2020-05-15 18:41:46 +02:00
}
2018-10-16 01:39:04 +02:00
// Define $website
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $website )) {
2019-12-09 10:35:58 +01:00
$website = new Website ( $db );
2019-01-27 11:55:16 +01:00
$website -> fetch ( 0 , $websitekey );
2018-10-03 20:58:45 +02:00
}
2018-10-16 01:39:04 +02:00
// Define $websitepage if we have $websitepagefile defined
2021-02-23 22:03:23 +01:00
if ( ! $pageid && ! empty ( $websitepagefile )) {
2018-10-16 01:39:04 +02:00
$pageid = str_replace ( array ( '.tpl.php' , 'page' ), array ( '' , '' ), basename ( $websitepagefile ));
2021-02-23 22:03:23 +01:00
if ( $pageid == 'index.php' ) {
$pageid = $website -> fk_default_home ;
}
2019-04-30 13:50:03 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $websitepage )) {
2020-10-31 14:32:18 +01:00
$websitepage = new WebsitePage ( $db );
2018-10-16 01:39:04 +02:00
}
2020-05-08 14:25:49 +02:00
// Define $weblangs
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $weblangs )) {
2020-05-08 14:35:03 +02:00
$weblangs = new Translate ( '' , $conf );
2020-05-08 14:25:49 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! is_object ( $pagelangs )) {
2020-05-15 19:01:12 +02:00
$pagelangs = new Translate ( '' , $conf );
}
2021-02-23 22:03:23 +01:00
if ( $pageid > 0 ) {
2018-10-16 01:39:04 +02:00
$websitepage -> fetch ( $pageid );
2019-11-12 13:55:46 +01:00
2020-09-18 16:00:47 +02:00
$weblangs -> setDefaultLang ( GETPOSTISSET ( 'lang' ) ? GETPOST ( 'lang' , 'aZ09' ) : ( empty ( $_COOKIE [ 'weblangs-shortcode' ]) ? 'auto' : preg_replace ( '/[^a-zA-Z0-9_\-]/' , '' , $_COOKIE [ 'weblangs-shortcode' ])));
2020-05-15 19:01:12 +02:00
$pagelangs -> setDefaultLang ( $websitepage -> lang ? $websitepage -> lang : $weblangs -> shortlang );
2020-05-08 14:25:49 +02:00
2022-03-16 15:56:39 +01:00
if ( ! defined ( 'USEDOLIBARREDITOR' ) && ( in_array ( $websitepage -> type_container , array ( 'menu' , 'other' )) || empty ( $websitepage -> status ) && ! defined ( 'USEDOLIBARRSERVER' ))) {
2019-11-12 13:55:46 +01:00
$weblangs -> load ( " website " );
2022-09-09 13:58:54 +02:00
// Security options
// X-Content-Type-Options
header ( " X-Content-Type-Options: nosniff " );
// X-Frame-Options
if ( empty ( $websitepage -> allowed_in_frames ) && empty ( $conf -> global -> WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES )) {
header ( " X-Frame-Options: SAMEORIGIN " );
}
//httponly_accessforbidden('<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>', 404, 1);
2019-11-12 13:55:46 +01:00
http_response_code ( 404 );
2020-06-09 22:43:37 +02:00
print '<center><br><br>' . $weblangs -> trans ( " YouTryToAccessToAFileThatIsNotAWebsitePage " , $websitepage -> pageurl , $websitepage -> type_container , $websitepage -> status ) . '</center>' ;
2019-11-12 13:55:46 +01:00
exit ;
}
2018-10-16 01:39:04 +02:00
}
2018-10-14 18:44:29 +02:00
2019-12-09 10:35:58 +01:00
if ( ! defined ( 'USEDOLIBARRSERVER' ) && ! defined ( 'USEDOLIBARREDITOR' )) {
2022-08-16 15:19:21 +02:00
// Security options
// X-Content-Type-Options
2019-12-07 11:51:21 +01:00
header ( " X-Content-Type-Options: nosniff " );
2022-08-16 15:19:21 +02:00
// X-Frame-Options
2020-11-26 11:09:35 +01:00
if ( empty ( $websitepage -> allowed_in_frames ) && empty ( $conf -> global -> WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES )) {
2019-12-07 15:46:14 +01:00
header ( " X-Frame-Options: SAMEORIGIN " );
}
2022-08-16 15:19:21 +02:00
// X-XSS-Protection
//header("X-XSS-Protection: 1"); // XSS filtering protection of some browsers (note: use of Content-Security-Policy is more efficient). Disabled as deprecated.
2023-02-28 10:39:39 +01:00
// Content-Security-Policy-Report-Only
if ( ! defined ( 'WEBSITE_MAIN_SECURITY_FORCECSPRO' )) {
// A default security policy that keep usage of js external component like ckeditor, stripe, google, working
// For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
// 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: *;
// For example, to restrict everything to itself except img that can be on other servers:
// default-src 'self'; img-src *;
// Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
// default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
//
2023-03-01 23:50:02 +01:00
// $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;";
// $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';";
2023-02-28 10:39:39 +01:00
$contentsecuritypolicy = getDolGlobalString ( 'WEBSITE_MAIN_SECURITY_FORCECSPRO' );
if ( ! is_object ( $hookmanager )) {
$hookmanager = new HookManager ( $db );
}
$hookmanager -> initHooks ( array ( " main " ));
$parameters = array ( 'contentsecuritypolicy' => $contentsecuritypolicy , 'mode' => 'reportonly' );
$result = $hookmanager -> executeHooks ( 'setContentSecurityPolicy' , $parameters ); // Note that $action and $object may have been modified by some hooks
if ( $result > 0 ) {
$contentsecuritypolicy = $hookmanager -> resPrint ; // Replace CSP
} else {
$contentsecuritypolicy .= $hookmanager -> resPrint ; // Concat CSP
}
if ( ! empty ( $contentsecuritypolicy )) {
header ( " Content-Security-Policy-Report-Only: " . $contentsecuritypolicy );
}
}
2022-08-16 15:19:21 +02:00
// Content-Security-Policy
if ( ! defined ( 'WEBSITE_MAIN_SECURITY_FORCECSP' )) {
// A default security policy that keep usage of js external component like ckeditor, stripe, google, working
2023-02-28 10:39:39 +01:00
// For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
// 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: *;
// For example, to restrict everything to itself except img that can be on other servers:
// default-src 'self'; img-src *;
// Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
// default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
//
2023-03-01 23:50:02 +01:00
// $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;";
// $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';";
2022-08-16 15:19:21 +02:00
$contentsecuritypolicy = getDolGlobalString ( 'WEBSITE_MAIN_SECURITY_FORCECSP' );
if ( ! is_object ( $hookmanager )) {
$hookmanager = new HookManager ( $db );
}
$hookmanager -> initHooks ( array ( " main " ));
2023-02-28 10:39:39 +01:00
$parameters = array ( 'contentsecuritypolicy' => $contentsecuritypolicy , 'mode' => 'active' );
2022-08-16 15:19:21 +02:00
$result = $hookmanager -> executeHooks ( 'setContentSecurityPolicy' , $parameters ); // Note that $action and $object may have been modified by some hooks
if ( $result > 0 ) {
$contentsecuritypolicy = $hookmanager -> resPrint ; // Replace CSP
} else {
$contentsecuritypolicy .= $hookmanager -> resPrint ; // Concat CSP
}
if ( ! empty ( $contentsecuritypolicy )) {
header ( " Content-Security-Policy: " . $contentsecuritypolicy );
}
}
2022-08-16 15:49:29 +02:00
// Referrer-Policy
if ( ! defined ( 'WEBSITE_MAIN_SECURITY_FORCERP' )) {
2022-08-16 16:06:09 +02:00
// The constant WEBSITE_MAIN_SECURITY_FORCERP should never be defined by page, but the variable used just after may be
2022-08-16 15:49:29 +02:00
// For public web sites, we use the same default value than "strict-origin-when-cross-origin"
$referrerpolicy = getDolGlobalString ( 'WEBSITE_MAIN_SECURITY_FORCERP' , " strict-origin-when-cross-origin " );
header ( " Referrer-Policy: " . $referrerpolicy );
}
2022-08-16 16:06:09 +02:00
// Strict-Transport-Security
if ( ! defined ( 'WEBSITE_MAIN_SECURITY_FORCESTS' )) {
// The constant WEBSITE_MAIN_SECURITY_FORCESTS should never be defined by page, but the variable used just after may be
2022-08-16 16:58:28 +02:00
// Example: "max-age=31536000; includeSubDomains"
2022-08-16 16:06:09 +02:00
$sts = getDolGlobalString ( 'WEBSITE_MAIN_SECURITY_FORCESTS' );
if ( ! empty ( $sts )) {
header ( " Strict-Transport-Security: " . $sts );
}
}
2022-08-16 16:58:28 +02:00
// Permissions-Policy (old name was Feature-Policy)
if ( ! defined ( 'WEBSITE_MAIN_SECURITY_FORCEPP' )) {
// The constant WEBSITE_MAIN_SECURITY_FORCEPP should never be defined by page, but the variable used just after may be
// Example: "camera: 'none'; microphone: 'none';"
$pp = getDolGlobalString ( 'WEBSITE_MAIN_SECURITY_FORCEPP' );
if ( ! empty ( $pp )) {
header ( " Permissions-Policy: " . $pp );
}
}
2019-12-07 11:51:21 +01:00
}
2018-10-14 18:44:29 +02:00
// A lang was forced, so we change weblangs init
2021-02-23 22:03:23 +01:00
if ( GETPOST ( 'l' , 'aZ09' )) {
$weblangs -> setDefaultLang ( GETPOST ( 'l' , 'aZ09' ));
}
2018-10-14 18:44:29 +02:00
// A lang was forced, so we check to find if we must make a redirect on translation page
2021-02-23 22:03:23 +01:00
if ( $_SERVER [ 'PHP_SELF' ] != DOL_URL_ROOT . '/website/index.php' ) { // If we browsing page using Dolibarr server or a Native web server
2018-10-14 20:04:25 +02:00
//print_r(get_defined_constants(true));exit;
2021-02-23 22:03:23 +01:00
if ( GETPOST ( 'l' , 'aZ09' )) {
2019-12-09 10:35:58 +01:00
$sql = " SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " website_page as wp " ;
2021-03-30 17:53:25 +02:00
$sql .= " WHERE wp.fk_website = " . (( int ) $website -> id );
2021-04-24 20:18:11 +02:00
$sql .= " AND (wp.fk_page = " . (( int ) $pageid ) . " OR wp.rowid = " . (( int ) $pageid );
2021-02-23 22:03:23 +01:00
if ( is_object ( $websitepage ) && $websitepage -> fk_page > 0 ) {
2021-08-23 19:33:24 +02:00
$sql .= " OR wp.fk_page = " . (( int ) $websitepage -> fk_page ) . " OR wp.rowid = " . (( int ) $websitepage -> fk_page );
2021-02-23 22:03:23 +01:00
}
2019-12-09 10:35:58 +01:00
$sql .= " ) " ;
$sql .= " AND wp.lang = ' " . $db -> escape ( GETPOST ( 'l' , 'aZ09' )) . " ' " ;
2018-10-14 23:26:57 +02:00
2018-10-16 01:39:04 +02:00
$resql = $db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( $resql ) {
2018-10-16 01:39:04 +02:00
$obj = $db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $obj ) {
2018-10-16 01:39:04 +02:00
$newpageid = $obj -> rowid ;
2021-02-23 22:03:23 +01:00
if ( $newpageid != $pageid ) { // To avoid to make a redirect on same page (infinite loop)
2018-10-16 01:39:04 +02:00
if ( defined ( 'USEDOLIBARRSERVER' )) {
2019-01-27 11:55:16 +01:00
header ( " Location: " . DOL_URL_ROOT . '/public/website/index.php?website=' . $websitekey . '&pageid=' . $newpageid . '&l=' . GETPOST ( 'l' , 'aZ09' ));
2018-10-16 01:39:04 +02:00
exit ;
2020-05-21 15:05:19 +02:00
} else {
2018-10-16 01:39:04 +02:00
$newpageref = $obj -> pageurl ;
2020-06-06 20:37:17 +02:00
header ( " Location: " . (( $obj -> lang && $obj -> lang != $website -> lang ) ? '/' . $obj -> lang . '/' : '/' ) . $newpageref . '.php?l=' . GETPOST ( 'l' , 'aZ09' ));
2018-10-16 01:39:04 +02:00
exit ;
2018-10-14 20:04:25 +02:00
}
2018-10-14 18:44:29 +02:00
}
}
}
}
}
2019-08-13 05:03:45 +02:00
2022-09-09 13:58:54 +02:00
// Show off line message when all website is off
2021-02-23 22:03:23 +01:00
if ( ! defined ( 'USEDOLIBARREDITOR' ) && empty ( $website -> status )) {
2022-09-09 13:58:54 +02:00
// Security options
// X-Content-Type-Options
header ( " X-Content-Type-Options: nosniff " );
// X-Frame-Options
if ( empty ( $websitepage -> allowed_in_frames ) && empty ( $conf -> global -> WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES )) {
header ( " X-Frame-Options: SAMEORIGIN " );
}
2019-08-13 05:03:45 +02:00
$weblangs -> load ( " website " );
2022-09-09 13:58:54 +02:00
//httponly_accessforbidden('<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>', 503, 1);
2019-08-13 06:37:06 +02:00
http_response_code ( 503 );
2019-08-13 05:03:45 +02:00
print '<center><br><br>' . $weblangs -> trans ( " SorryWebsiteIsCurrentlyOffLine " ) . '</center>' ;
exit ;
}