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