dolibarr  x.y.z
website.inc.php
Go to the documentation of this file.
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 
26 // Load website class
27 include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
28 include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
29 
30 $website = null;
31 $websitepage = null;
32 $weblangs = null;
33 $pagelangs = null;
34 
35 // Detection browser (copy of code from main.inc.php)
36 if (isset($_SERVER["HTTP_USER_AGENT"]) && is_object($conf) && empty($conf->browser->name)) {
37  $tmp = getBrowserInfo($_SERVER["HTTP_USER_AGENT"]);
38  $conf->browser->name = $tmp['browsername'];
39  $conf->browser->os = $tmp['browseros'];
40  $conf->browser->version = $tmp['browserversion'];
41  $conf->browser->layout = $tmp['layout']; // 'classic', 'phone', 'tablet'
42  //var_dump($conf->browser);
43 
44  if ($conf->browser->layout == 'phone') {
45  $conf->dol_no_mouse_hover = 1;
46  }
47 }
48 // Define $website
49 if (!is_object($website)) {
50  $website = new Website($db);
51  $website->fetch(0, $websitekey);
52 }
53 // Define $websitepage if we have $websitepagefile defined
54 if (!$pageid && !empty($websitepagefile)) {
55  $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
56  if ($pageid == 'index.php') {
57  $pageid = $website->fk_default_home;
58  }
59 }
60 if (!is_object($websitepage)) {
61  $websitepage = new WebsitePage($db);
62 }
63 // Define $weblangs
64 if (!is_object($weblangs)) {
65  $weblangs = new Translate('', $conf);
66 }
67 if (!is_object($pagelangs)) {
68  $pagelangs = new Translate('', $conf);
69 }
70 if ($pageid > 0) {
71  $websitepage->fetch($pageid);
72 
73  $weblangs->setDefaultLang(GETPOSTISSET('lang') ? GETPOST('lang', 'aZ09') : (empty($_COOKIE['weblangs-shortcode']) ? 'auto' : preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['weblangs-shortcode'])));
74  $pagelangs->setDefaultLang($websitepage->lang ? $websitepage->lang : $weblangs->shortlang);
75 
76  if (!defined('USEDOLIBARREDITOR') && (in_array($websitepage->type_container, array('menu', 'other')) || empty($websitepage->status) && !defined('USEDOLIBARRSERVER'))) {
77  $weblangs->load("website");
78 
79  // Security options
80 
81  // X-Content-Type-Options
82  header("X-Content-Type-Options: nosniff");
83 
84  // X-Frame-Options
85  if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
86  header("X-Frame-Options: SAMEORIGIN");
87  }
88 
89  //httponly_accessforbidden('<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>', 404, 1);
90  http_response_code(404);
91  print '<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>';
92  exit;
93  }
94 }
95 
96 if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
97  // Security options
98 
99  // X-Content-Type-Options
100  header("X-Content-Type-Options: nosniff");
101 
102  // X-Frame-Options
103  if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
104  header("X-Frame-Options: SAMEORIGIN");
105  }
106 
107  // X-XSS-Protection
108  //header("X-XSS-Protection: 1"); // XSS filtering protection of some browsers (note: use of Content-Security-Policy is more efficient). Disabled as deprecated.
109 
110  // Content-Security-Policy
111  if (!defined('WEBSITE_MAIN_SECURITY_FORCECSP')) {
112  // The constant WEBSITE_MAIN_SECURITY_FORCECSP should never be defined by page, but the variable used just after may be
113 
114  // A default security policy that keep usage of js external component like ckeditor, stripe, google, working
115  // $contentsecuritypolicy = "font-src *; img-src *; style-src * 'unsafe-inline' 'unsafe-eval'; default-src 'self' *.stripe.com 'unsafe-inline' 'unsafe-eval'; script-src 'self' *.stripe.com 'unsafe-inline' 'unsafe-eval'; frame-src 'self' *.stripe.com; connect-src 'self';";
116  $contentsecuritypolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCECSP');
117 
118  if (!is_object($hookmanager)) {
119  $hookmanager = new HookManager($db);
120  }
121  $hookmanager->initHooks(array("main"));
122 
123  $parameters = array('contentsecuritypolicy'=>$contentsecuritypolicy);
124  $result = $hookmanager->executeHooks('setContentSecurityPolicy', $parameters); // Note that $action and $object may have been modified by some hooks
125  if ($result > 0) {
126  $contentsecuritypolicy = $hookmanager->resPrint; // Replace CSP
127  } else {
128  $contentsecuritypolicy .= $hookmanager->resPrint; // Concat CSP
129  }
130 
131  if (!empty($contentsecuritypolicy)) {
132  // For example: to restrict to only local resources, except for css (cloudflare+google), and js (transifex + google tags) and object/iframe (youtube)
133  // 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: *;
134  // For example, to restrict everything to itself except img that can be on other servers:
135  // default-src 'self'; img-src *;
136  // Pre-existing site that uses too much js code to fix but wants to ensure resources are loaded only over https and disable plugins:
137  // default-src https: 'unsafe-inline' 'unsafe-eval'; object-src 'none'
138  header("Content-Security-Policy: ".$contentsecuritypolicy);
139  }
140  }
141 
142  // Referrer-Policy
143  if (!defined('WEBSITE_MAIN_SECURITY_FORCERP')) {
144  // The constant WEBSITE_MAIN_SECURITY_FORCERP should never be defined by page, but the variable used just after may be
145 
146  // For public web sites, we use the same default value than "strict-origin-when-cross-origin"
147  $referrerpolicy = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCERP', "strict-origin-when-cross-origin");
148 
149  header("Referrer-Policy: ".$referrerpolicy);
150  }
151 
152  // Strict-Transport-Security
153  if (!defined('WEBSITE_MAIN_SECURITY_FORCESTS')) {
154  // The constant WEBSITE_MAIN_SECURITY_FORCESTS should never be defined by page, but the variable used just after may be
155 
156  // Example: "max-age=31536000; includeSubDomains"
157  $sts = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCESTS');
158  if (!empty($sts)) {
159  header("Strict-Transport-Security: ".$sts);
160  }
161  }
162 
163  // Permissions-Policy (old name was Feature-Policy)
164  if (!defined('WEBSITE_MAIN_SECURITY_FORCEPP')) {
165  // The constant WEBSITE_MAIN_SECURITY_FORCEPP should never be defined by page, but the variable used just after may be
166 
167  // Example: "camera: 'none'; microphone: 'none';"
168  $pp = getDolGlobalString('WEBSITE_MAIN_SECURITY_FORCEPP');
169  if (!empty($pp)) {
170  header("Permissions-Policy: ".$pp);
171  }
172  }
173 }
174 
175 // A lang was forced, so we change weblangs init
176 if (GETPOST('l', 'aZ09')) {
177  $weblangs->setDefaultLang(GETPOST('l', 'aZ09'));
178 }
179 // A lang was forced, so we check to find if we must make a redirect on translation page
180 if ($_SERVER['PHP_SELF'] != DOL_URL_ROOT.'/website/index.php') { // If we browsing page using Dolibarr server or a Native web server
181  //print_r(get_defined_constants(true));exit;
182  if (GETPOST('l', 'aZ09')) {
183  $sql = "SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
184  $sql .= " FROM ".MAIN_DB_PREFIX."website_page as wp";
185  $sql .= " WHERE wp.fk_website = ".((int) $website->id);
186  $sql .= " AND (wp.fk_page = ".((int) $pageid)." OR wp.rowid = ".((int) $pageid);
187  if (is_object($websitepage) && $websitepage->fk_page > 0) {
188  $sql .= " OR wp.fk_page = ".((int) $websitepage->fk_page)." OR wp.rowid = ".((int) $websitepage->fk_page);
189  }
190  $sql .= ")";
191  $sql .= " AND wp.lang = '".$db->escape(GETPOST('l', 'aZ09'))."'";
192 
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 
213 // Show off line message when all website is off
214 if (!defined('USEDOLIBARREDITOR') && empty($website->status)) {
215  // Security options
216 
217  // X-Content-Type-Options
218  header("X-Content-Type-Options: nosniff");
219 
220  // X-Frame-Options
221  if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
222  header("X-Frame-Options: SAMEORIGIN");
223  }
224 
225  $weblangs->load("website");
226 
227  //httponly_accessforbidden('<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>', 503, 1);
228  http_response_code(503);
229  print '<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>';
230  exit;
231 }
Class to manage hooks.
Class to manage translations.
Class Website.
Class Websitepage.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
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.
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.