website.inc.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // Detection browser (copy of code from main.inc.php)
  28. if (isset($_SERVER["HTTP_USER_AGENT"]) && is_object($conf) && empty($conf->browser->name))
  29. {
  30. $tmp = getBrowserInfo($_SERVER["HTTP_USER_AGENT"]);
  31. $conf->browser->name = $tmp['browsername'];
  32. $conf->browser->os = $tmp['browseros'];
  33. $conf->browser->version = $tmp['browserversion'];
  34. $conf->browser->layout = $tmp['layout']; // 'classic', 'phone', 'tablet'
  35. //var_dump($conf->browser);
  36. if ($conf->browser->layout == 'phone') $conf->dol_no_mouse_hover = 1;
  37. }
  38. // Define $website
  39. if (!is_object($website))
  40. {
  41. $website = new Website($db);
  42. $website->fetch(0, $websitekey);
  43. }
  44. // Define $websitepage if we have $websitepagefile defined
  45. if (!$pageid && !empty($websitepagefile))
  46. {
  47. $pageid = str_replace(array('.tpl.php', 'page'), array('', ''), basename($websitepagefile));
  48. if ($pageid == 'index.php') $pageid = $website->fk_default_home;
  49. }
  50. if (!is_object($websitepage))
  51. {
  52. $websitepage = new WebsitePage($db);
  53. }
  54. // Define $weblangs
  55. if (!is_object($weblangs))
  56. {
  57. $weblangs = new Translate('', $conf);
  58. }
  59. if (!is_object($pagelangs))
  60. {
  61. $pagelangs = new Translate('', $conf);
  62. }
  63. if ($pageid > 0)
  64. {
  65. $websitepage->fetch($pageid);
  66. $weblangs->setDefaultLang(GETPOSTISSET('lang') ? GETPOST('lang', 'aZ09') : (empty($_COOKIE['weblangs-shortcode']) ? 'auto' : $_COOKIE['weblangs-shortcode']));
  67. $pagelangs->setDefaultLang($websitepage->lang ? $websitepage->lang : $weblangs->shortlang);
  68. if (!defined('USEDOLIBARREDITOR') && in_array($websitepage->type_container, array('menu', 'other')))
  69. {
  70. $weblangs->load("website");
  71. http_response_code(404);
  72. print '<center><br><br>'.$weblangs->trans("YouTryToAccessToAFileThatIsNotAWebsitePage").'</center>';
  73. exit;
  74. }
  75. }
  76. if (!defined('USEDOLIBARRSERVER') && !defined('USEDOLIBARREDITOR')) {
  77. header("X-Content-Type-Options: nosniff");
  78. /* TODO Manage allow_frames flag on websitepage.
  79. if (empty($websitepage->allow_frames) && empty($conf->global->WEBSITE_ALLOW_FRAMES_ON_ALL_PAGES)) {
  80. header("X-Frame-Options: SAMEORIGIN");
  81. }
  82. */
  83. }
  84. // A lang was forced, so we change weblangs init
  85. if (GETPOST('l', 'aZ09')) $weblangs->setDefaultLang(GETPOST('l', 'aZ09'));
  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. {
  89. //print_r(get_defined_constants(true));exit;
  90. if (GETPOST('l', 'aZ09'))
  91. {
  92. $sql = "SELECT wp.rowid, wp.lang, wp.pageurl, wp.fk_page";
  93. $sql .= " FROM ".MAIN_DB_PREFIX."website_page as wp";
  94. $sql .= " WHERE wp.fk_website = ".$website->id;
  95. $sql .= " AND (wp.fk_page = ".$pageid." OR wp.rowid = ".$pageid;
  96. if (is_object($websitepage) && $websitepage->fk_page > 0) $sql .= " OR wp.fk_page = ".$websitepage->fk_page." OR wp.rowid = ".$websitepage->fk_page;
  97. $sql .= ")";
  98. $sql .= " AND wp.lang = '".$db->escape(GETPOST('l', 'aZ09'))."'";
  99. $resql = $db->query($sql);
  100. if ($resql)
  101. {
  102. $obj = $db->fetch_object($resql);
  103. if ($obj)
  104. {
  105. $newpageid = $obj->rowid;
  106. if ($newpageid != $pageid) // To avoid to make a redirect on same page (infinite loop)
  107. {
  108. if (defined('USEDOLIBARRSERVER')) {
  109. header("Location: ".DOL_URL_ROOT.'/public/website/index.php?website='.$websitekey.'&pageid='.$newpageid.'&l='.GETPOST('l', 'aZ09'));
  110. exit;
  111. }
  112. else
  113. {
  114. $newpageref = $obj->pageurl;
  115. header("Location: ".(($obj->lang && $obj->lang != $website->lang) ? '/'.$obj->lang.'/' : '/').$newpageref.'.php?l='.GETPOST('l', 'aZ09'));
  116. exit;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. // Show off line message
  124. if (!defined('USEDOLIBARREDITOR') && empty($website->status))
  125. {
  126. $weblangs->load("website");
  127. http_response_code(503);
  128. print '<center><br><br>'.$weblangs->trans("SorryWebsiteIsCurrentlyOffLine").'</center>';
  129. exit;
  130. }