dolibarr 24.0.0-beta
website.inc.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2017-2024 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
5 *
6* This program is free software; you can redistribute it and/or modify
7* it under the terms of the GNU General Public License as published by
8* the Free Software Foundation; either version 3 of the License, or
9* (at your option) any later version.
10*
11* This program is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU General Public License for more details.
15*
16* You should have received a copy of the GNU General Public License
17* along with this program. If not, see <https://www.gnu.org/licenses/>.
18* or see https://www.gnu.org/
19*/
20
36// Load website class
37include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
38include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
39
40$website = null;
41$websitepage = null;
42$weblangs = null;
43$pagelangs = null;
44
45// Detection browser (copy of code from main.inc.php)
46if (isset($_SERVER["HTTP_USER_AGENT"]) && is_object($conf) && empty($conf->browser->name)) {
47 $tmp = getBrowserInfo($_SERVER["HTTP_USER_AGENT"]);
48 $conf->browser->name = $tmp['browsername'];
49 $conf->browser->os = $tmp['browseros'];
50 $conf->browser->version = $tmp['browserversion'];
51 $conf->browser->layout = $tmp['layout']; // 'classic', 'phone', 'tablet'
52 //var_dump($conf->browser);
53
54 if ($conf->browser->layout == 'phone') {
55 $conf->dol_no_mouse_hover = 1;
56 }
57}
58// Define $website
59if (is_null($website)) {
60 $website = new Website($db);
61 $website->fetch(0, $websitekey);
62}
63// Define $websitepage if we have $websitepagefile defined
64if (empty($pageid) && !empty($websitepagefile)) {
65 $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
66 if ($pageid == 'index.php') {
67 $pageid = $website->fk_default_home;
68 }
69}
70if (is_null($websitepage)) {
71 $websitepage = new WebsitePage($db);
72}
73// Define $weblangs
74if (is_null($weblangs)) {
75 $weblangs = new Translate('', $conf);
76}
77if (is_null($pagelangs)) {
78 $pagelangs = new Translate('', $conf);
79}
80if (!empty($pageid) && $pageid > 0) {
81 $websitepage->fetch($pageid);
82
83 // Rule to define weblang of visitor:
84 // 1 - Take parameter lang
85 // 2 - Cookie lang of website (set by a possible js lang selector)
86 // 3 - XX/... found in url page
87 // 4 - auto (so web browser lang)
88 $srclang = GETPOSTISSET('lang') ? GETPOST('lang', 'aZ09') : '';
89 if (empty($srclang)) {
90 $srclang = (empty($_COOKIE['weblangs-shortcode']) ? '' : preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['weblangs-shortcode']));
91 }
92 if (empty($srclang)) {
93 $reg = array();
94 // With Dolibarr server, url is in parameter pageref
95 if (defined('USEDOLIBARRSERVER') && !empty($_GET['pageref']) && preg_match('/^\/?(\w\w)\//', $_GET['pageref'], $reg) && $reg[1] != 'js') { // We reuse $_GET['pageref'] because $pageref may have been cleaned already from the language code.
96 $srclang = $reg[1];
97 }
98 // With External server, url is in parameter pageref
99 if (defined('USEEXTERNALSERVER') && !empty($_SERVER['PHP_SELF']) && preg_match('/^\/?(\w\w)\//', $_SERVER['PHP_SELF'], $reg) && $reg[1] != 'js') {
100 $srclang = $reg[1];
101 }
102 }
103 if (empty($srclang)) {
104 $srclang = 'auto';
105 }
106 $weblangs->setDefaultLang($srclang);
107
108 $pagelangs->setDefaultLang($websitepage->lang ? $websitepage->lang : $weblangs->shortlang);
109
110 if (!defined('USEDOLIBARREDITOR') && (in_array($websitepage->type_container, array('menu', 'setup', 'other')) || empty($websitepage->status) && !defined('USEDOLIBARRSERVER'))) {
111 $weblangs->load("website");
112
113 // Security options
114
115 // X-Content-Type-Options
116 header("X-Content-Type-Options: nosniff");
117
118 // X-Frame-Options
119 if (empty($websitepage->allowed_in_frames) && !getDolGlobalString('WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES')) {
120 header("X-Frame-Options: SAMEORIGIN");
121 }
122
123 //httponly_accessforbidden('<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>', 404, 1);
124 http_response_code(404);
125 print '<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>';
126 exit;
127 }
128}
129
130if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
131 // Security options
132
133 // X-Content-Type-Options
134 header("X-Content-Type-Options: nosniff");
135
136 // X-Frame-Options
137 if (empty($websitepage->allowed_in_frames) && !getDolGlobalString('WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES')) {
138 header("X-Frame-Options: SAMEORIGIN");
139 }
140
141 // X-XSS-Protection
142 //header("X-XSS-Protection: 1"); // XSS filtering protection of some browsers (note: use of Content-Security-Policy is more efficient). Disabled as deprecated.
143
144 // Content-Security-Policy-Report-Only
145 if (!defined('WEBSITE_'.$website->id.'_SECURITY_FORCECSPRO')) {
146 // A default security policy that keep usage of js external component like ckeditor, stripe, google, working
147 // For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
148 // 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: *;
149 // For example, to restrict everything to itself except img that can be on other servers:
150 // default-src 'self'; img-src *;
151 // Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
152 // default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
153 //
154 // $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;";
155 // $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';";
156 $contentsecuritypolicy = getDolGlobalString('WEBSITE_'.$website->id.'_SECURITY_FORCECSPRO');
157
158 if (!is_object($hookmanager)) {
159 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
160 $hookmanager = new HookManager($db);
161 }
162 $hookmanager->initHooks(array("main"));
163
164 $parameters = array('contentsecuritypolicy' => $contentsecuritypolicy, 'mode' => 'reportonly');
165 $result = $hookmanager->executeHooks('setContentSecurityPolicy', $parameters); // Note that $action and $object may have been modified by some hooks
166 if ($result > 0) {
167 $contentsecuritypolicy = $hookmanager->resPrint; // Replace CSP
168 } else {
169 $contentsecuritypolicy .= $hookmanager->resPrint; // Concat CSP
170 }
171
172 if (!empty($contentsecuritypolicy)) {
173 header("Content-Security-Policy-Report-Only: ".$contentsecuritypolicy);
174 }
175 }
176
177 // Content-Security-Policy
178 if (!defined('WEBSITE_'.$website->id.'_SECURITY_FORCECSP')) {
179 // A default security policy that keep usage of js external component like ckeditor, stripe, google, working
180 // For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
181 // 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: *;
182 // For example, to restrict everything to itself except img that can be on other servers:
183 // default-src 'self'; img-src *;
184 // Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
185 // default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
186 //
187 // $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;";
188 // $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';";
189 $contentsecuritypolicy = getDolGlobalString('WEBSITE_'.$website->id.'_SECURITY_FORCECSP');
190
191 if (!is_object($hookmanager)) {
192 include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
193 $hookmanager = new HookManager($db);
194 }
195 $hookmanager->initHooks(array("main"));
196
197 $parameters = array('contentsecuritypolicy' => $contentsecuritypolicy, 'mode' => 'active');
198 $result = $hookmanager->executeHooks('setContentSecurityPolicy', $parameters); // Note that $action and $object may have been modified by some hooks
199 if ($result > 0) {
200 $contentsecuritypolicy = $hookmanager->resPrint; // Replace CSP
201 } else {
202 $contentsecuritypolicy .= $hookmanager->resPrint; // Concat CSP
203 }
204
205 if (!empty($contentsecuritypolicy)) {
206 header("Content-Security-Policy: ".$contentsecuritypolicy);
207 }
208 }
209
210 // Referrer-Policy
211 if (!defined('WEBSITE_'.$website->id.'_SECURITY_FORCERP')) {
212 // The constant WEBSITE_MAIN_SECURITY_FORCERP should never be defined by page, but the variable used just after may be
213
214 // For public web sites, we use the same default value than "strict-origin-when-cross-origin"
215 $referrerpolicy = getDolGlobalString('WEBSITE_'.$website->id.'_SECURITY_FORCERP', "strict-origin-when-cross-origin");
216
217 header("Referrer-Policy: ".$referrerpolicy);
218 }
219
220 // Strict-Transport-Security
221 if (!defined('WEBSITE_'.$website->id.'_SECURITY_FORCESTS')) {
222 // The constant WEBSITE_MAIN_SECURITY_FORCESTS should never be defined by page, but the variable used just after may be
223
224 // Example: "max-age=31536000; includeSubDomains"
225 $sts = getDolGlobalString('WEBSITE_'.$website->id.'_SECURITY_FORCESTS');
226 if (!empty($sts)) {
227 header("Strict-Transport-Security: ".$sts);
228 }
229 }
230
231 // Permissions-Policy (old name was Feature-Policy)
232 if (!defined('WEBSITE_'.$website->id.'_SECURITY_FORCEPP')) {
233 // The constant WEBSITE_MAIN_SECURITY_FORCEPP should never be defined by page, but the variable used just after may be
234
235 // Example: "camera: 'none'; microphone: 'none';"
236 $pp = getDolGlobalString('WEBSITE_'.$website->id.'_SECURITY_FORCEPP');
237 if (!empty($pp)) {
238 header("Permissions-Policy: ".$pp);
239 }
240 }
241}
242
243// A lang was forced, so we change weblangs init
244if (GETPOST('l', 'aZ09')) {
245 $weblangs->setDefaultLang(GETPOST('l', 'aZ09'));
246}
247// A lang was forced, so we check to find if we must make a redirect on translation page
248if ($_SERVER['PHP_SELF'] != DOL_URL_ROOT.'/website/index.php') { // If we browsing page using Dolibarr server or a Native web server
249 //print_r(get_defined_constants(true));exit;
250 if (GETPOST('l', 'aZ09')) {
251 $sql = "SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
252 $sql .= " FROM ".MAIN_DB_PREFIX."website_page as wp";
253 $sql .= " WHERE wp.fk_website = ".((int) $website->id);
254 $sql .= " AND (wp.fk_page = ".((int) $pageid)." OR wp.rowid = ".((int) $pageid);
255 if (is_object($websitepage) && $websitepage->fk_page > 0) {
256 $sql .= " OR wp.fk_page = ".((int) $websitepage->fk_page)." OR wp.rowid = ".((int) $websitepage->fk_page);
257 }
258 $sql .= ")";
259 $sql .= " AND wp.lang = '".$db->escape(GETPOST('l', 'aZ09'))."'";
260
261 $resql = $db->query($sql);
262 if ($resql) {
263 $obj = $db->fetch_object($resql);
264 if ($obj) {
265 $newpageid = $obj->rowid;
266 if ($newpageid != $pageid) { // To avoid to make a redirect on same page (infinite loop)
267 if (defined('USEDOLIBARRSERVER')) {
268 header("Location: ".DOL_URL_ROOT.'/public/website/index.php?website='.$websitekey.'&pageid='.$newpageid.'&l='.GETPOST('l', 'aZ09'));
269 exit;
270 } else {
271 $newpageref = $obj->pageurl;
272 header("Location: ".(($obj->lang && $obj->lang != $website->lang) ? '/'.$obj->lang.'/' : '/').$newpageref.'.php?l='.GETPOST('l', 'aZ09'));
273 exit;
274 }
275 }
276 }
277 }
278 }
279}
280
281// Show off line message when all website is off
282if (!defined('USEDOLIBARREDITOR') && empty($website->status)) {
283 // Security options
284
285 // X-Content-Type-Options
286 header("X-Content-Type-Options: nosniff");
287
288 // X-Frame-Options
289 if (empty($websitepage->allowed_in_frames) && !getDolGlobalString('WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES')) {
290 header("X-Frame-Options: SAMEORIGIN");
291 }
292
293 $weblangs->load("website");
294
295 //httponly_accessforbidden('<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>', 503, 1);
296 http_response_code(503);
297 print '<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>';
298 exit;
299}
300
301
302// Get session info and obfuscate session cookie and other variables
303$prefix = dol_getprefix('');
304$sessionname = 'DOLSESSID_'.$prefix;
305//$savsessionid = $_COOKIE[$sessionname];
306
307
308// Add a protection if custom PHP is not allowed or allowed with conditions
309global $dolibarr_website_allow_custom_php;
310$notdisabledsystemfunction = '';
311$systemfunctions = array("exec", "passthru", "shell_exec", "system", "popen", "proc_open");
312foreach ($systemfunctions as $systemfunction) {
313 // @phpstan-ignore-next-line
314 if (function_exists($systemfunction)) {
315 $notdisabledsystemfunction .= ($notdisabledsystemfunction ? ', ' : '').$systemfunction;
316 }
317}
318
319if (empty($dolibarr_website_allow_custom_php) || ($dolibarr_website_allow_custom_php == 1 && $notdisabledsystemfunction)) {
320 print '<center><br><br>';
321 print 'Website features are DISABLED if the PHP system functions ('.implode(',', $systemfunctions).') are NOT disabled for the website context.<br>';
322 print 'The value "'.$notdisabledsystemfunction.'" has NOT been found into the php parameter <b>current disable_functions</b> ';
323 //print '<textarea cols="100" rows="5">';
324 $form = new Form($db);
325 print $form->textwithpicto('', 'Current value for disable_functions = '.ini_get('disable_functions'));
326 print '<br>';
327 //print ini_get('disable_functions'); // Warning, the real value may not be this one.Only the master initial value from php.ini is effective, not the local value set at virtualhost.
328 //print (implode(', ', explode(',', (string) ini_get('disable_functions'))));
329 //print '</textarea>';
330
331 print '<br><br>';
332 print 'You can fix this by changing setup of your PHP ini (changing this in a virtual host with php_admin_value is not effective):<br>';
333 print 'disable_functions="exec,passthru,shell_exec,system,popen,proc_open,..."<br>';
334 print 'but WARNING, this will break this 3 features for:<br>';
335 print '- 1) Cron tasks calling command line tools.<br>';
336 print '- 2) Internal backup feature running the database dump tool.<br>';
337 print '- 3) Command line antivirus check ran when uploading a file.<br>';
338 print '<br>';
339 print 'If you don\'t use this 3 feature, you can change your php.ini to exclude the exec functions.<br>If you need at least one, you can bypass this protection by setting $dolibarr_website_allow_custom_php to 2 in your dolibarr config file (Add RCE protection like AppArmor or SELinux in this case).';
340 print '</center>';
341 exit; // Stop here to PHP later won't be executed
342}
343
344
345$_COOKIE[$sessionname] = 'obfuscatedcookie';
346unset($conf->file->instance_unique_id);
347
348unset($dolibarr_main_instance_unique_id);
349unset($dolibarr_main_dolcrypt_key);
350
351unset($dolibarr_main_db_host);
352unset($dolibarr_main_db_port);
353unset($dolibarr_main_db_name);
354unset($dolibarr_main_db_user);
355unset($dolibarr_main_db_pass);
356unset($dolibarr_main_db_type);
357unset($dolibarr_main_document_root);
358unset($dolibarr_main_document_root_alt);
Class to manage generation of HTML components Only common components must be here.
Class to manage hooks.
Class to manage translations.
Class Website.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getBrowserInfo($user_agent)
Return information about user browser.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.