website.inc.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. /**
  19. * \file htdocs/core/website.inc.php
  20. * \brief Common file loaded by all website pages (after master.inc.php). It set the new object $weblangs, using parameter 'l'.
  21. * This file is included in top of all container pages and is run only when a web page is called.
  22. * The global variable $websitekey must be defined.
  23. */
  24. // Load website class
  25. include_once DOL_DOCUMENT_ROOT.'/website/class/website.class.php';
  26. include_once DOL_DOCUMENT_ROOT.'/website/class/websitepage.class.php';
  27. $website = null;
  28. $websitepage = null;
  29. $weblangs = null;
  30. $pagelangs = null;
  31. // Detection browser (copy of code from main.inc.php)
  32. if (isset($_SERVER["HTTP_USER_AGENT"]) && is_object($conf) && empty($conf->browser->name)) {
  33. $tmp = getBrowserInfo($_SERVER["HTTP_USER_AGENT"]);
  34. $conf->browser->name = $tmp['browsername'];
  35. $conf->browser->os = $tmp['browseros'];
  36. $conf->browser->version = $tmp['browserversion'];
  37. $conf->browser->layout = $tmp['layout']; // 'classic', 'phone', 'tablet'
  38. //var_dump($conf->browser);
  39. if ($conf->browser->layout == 'phone') {
  40. $conf->dol_no_mouse_hover = 1;
  41. }
  42. }
  43. // Define $website
  44. if (!is_object($website)) {
  45. $website = new Website($db);
  46. $website->fetch(0, $websitekey);
  47. }
  48. // Define $websitepage if we have $websitepagefile defined
  49. if (!$pageid && !empty($websitepagefile)) {
  50. $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
  51. if ($pageid == 'index.php') {
  52. $pageid = $website->fk_default_home;
  53. }
  54. }
  55. if (!is_object($websitepage)) {
  56. $websitepage = new WebsitePage($db);
  57. }
  58. // Define $weblangs
  59. if (!is_object($weblangs)) {
  60. $weblangs = new Translate('', $conf);
  61. }
  62. if (!is_object($pagelangs)) {
  63. $pagelangs = new Translate('', $conf);
  64. }
  65. if ($pageid > 0) {
  66. $websitepage->fetch($pageid);
  67. $weblangs->setDefaultLang(GETPOSTISSET('lang') ? GETPOST('lang', 'aZ09') : (empty($_COOKIE['weblangs-shortcode']) ? 'auto' : preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['weblangs-shortcode'])));
  68. $pagelangs->setDefaultLang($websitepage->lang ? $websitepage->lang : $weblangs->shortlang);
  69. if (!defined('USEDOLIBARREDITOR') && (in_array($websitepage->type_container, array('menu', 'other')) || empty($websitepage->status) && !defined('USEDOLIBARRSERVER'))) {
  70. $weblangs->load("website");
  71. http_response_code(404);
  72. print '<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage", $websitepage->pageurl, $websitepage->type_container, $websitepage->status).'</center>';
  73. exit;
  74. }
  75. }
  76. if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
  77. header("X-Content-Type-Options: nosniff");
  78. if (empty($websitepage->allowed_in_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
  79. header("X-Frame-Options: SAMEORIGIN");
  80. }
  81. }
  82. // A lang was forced, so we change weblangs init
  83. if (GETPOST('l', 'aZ09')) {
  84. $weblangs->setDefaultLang(GETPOST('l', 'aZ09'));
  85. }
  86. // A lang was forced, so we check to find if we must make a redirect on translation page
  87. if ($_SERVER['PHP_SELF'] != DOL_URL_ROOT.'/website/index.php') { // If we browsing page using Dolibarr server or a Native web server
  88. //print_r(get_defined_constants(true));exit;
  89. if (GETPOST('l', 'aZ09')) {
  90. $sql = "SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
  91. $sql .= " FROM ".MAIN_DB_PREFIX."website_page as wp";
  92. $sql .= " WHERE wp.fk_website = ".((int) $website->id);
  93. $sql .= " AND (wp.fk_page = ".((int) $pageid)." OR wp.rowid = ".((int) $pageid);
  94. if (is_object($websitepage) && $websitepage->fk_page > 0) {
  95. $sql .= " OR wp.fk_page = ".((int) $websitepage->fk_page)." OR wp.rowid = ".((int) $websitepage->fk_page);
  96. }
  97. $sql .= ")";
  98. $sql .= " AND wp.lang = '".$db->escape(GETPOST('l', 'aZ09'))."'";
  99. $resql = $db->query($sql);
  100. if ($resql) {
  101. $obj = $db->fetch_object($resql);
  102. if ($obj) {
  103. $newpageid = $obj->rowid;
  104. if ($newpageid != $pageid) { // To avoid to make a redirect on same page (infinite loop)
  105. if (defined('USEDOLIBARRSERVER')) {
  106. header("Location: ".DOL_URL_ROOT.'/public/website/index.php?website='.$websitekey.'&pageid='.$newpageid.'&l='.GETPOST('l', 'aZ09'));
  107. exit;
  108. } else {
  109. $newpageref = $obj->pageurl;
  110. header("Location: ".(($obj->lang && $obj->lang != $website->lang) ? '/'.$obj->lang.'/' : '/').$newpageref.'.php?l='.GETPOST('l', 'aZ09'));
  111. exit;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. // Show off line message
  119. if (!defined('USEDOLIBARREDITOR') && empty($website->status)) {
  120. $weblangs->load("website");
  121. http_response_code(503);
  122. print '<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>';
  123. exit;
  124. }