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