master.inc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /* Copyright (C) 2002-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2003 Xavier Dutoit <doli@sydesy.com>
  4. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  6. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  7. * Copyright (C) 2005-2017 Regis Houssin <regis.houssin@inodbox.com>
  8. * Copyright (C) 2005 Simon Tosser <simon@kornog-computing.com>
  9. * Copyright (C) 2006 Andre Cianfarani <andre.cianfarani@acdeveloppement.net>
  10. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  11. * Copyright (C) 2011 Philippe Grand <philippe.grand@atoo-net.com>
  12. * Copyright (C) 2014 Teddy Andreotti <125155@supinfo.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  26. */
  27. /**
  28. * \file htdocs/master.inc.php
  29. * \ingroup core
  30. * \brief File that defines environment for all Dolibarr process (pages or scripts)
  31. * This script reads the conf file, init $lang, $db and and empty $user
  32. */
  33. // Include the conf.php and functions.lib.php and security.lib.php. This defined the constants like DOL_DOCUMENT_ROOT, DOL_DATA_ROOT, DOL_URL_ROOT...
  34. // This file may have been already required by main.inc.php. But may not by scripts. So, here the require_once must be kept.
  35. require_once 'filefunc.inc.php';
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/conf.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  38. if (!function_exists('is_countable')) {
  39. /**
  40. * function is_countable (to remove when php version supported will be >= 7.3)
  41. * @param mixed $c data to check if countable
  42. * @return bool
  43. */
  44. function is_countable($c)
  45. {
  46. return is_array($c) || $c instanceof Countable;
  47. }
  48. }
  49. /*
  50. * Create $conf object
  51. */
  52. $conf = new Conf();
  53. // Set properties specific to database
  54. $conf->db->host = empty($dolibarr_main_db_host) ? '' : $dolibarr_main_db_host;
  55. $conf->db->port = empty($dolibarr_main_db_port) ? '' : $dolibarr_main_db_port;
  56. $conf->db->name = empty($dolibarr_main_db_name) ? '' : $dolibarr_main_db_name;
  57. $conf->db->user = empty($dolibarr_main_db_user) ? '' : $dolibarr_main_db_user;
  58. $conf->db->pass = empty($dolibarr_main_db_pass) ? '' : $dolibarr_main_db_pass;
  59. $conf->db->type = $dolibarr_main_db_type;
  60. $conf->db->prefix = $dolibarr_main_db_prefix;
  61. $conf->db->character_set = $dolibarr_main_db_character_set;
  62. $conf->db->dolibarr_main_db_collation = $dolibarr_main_db_collation;
  63. $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
  64. $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
  65. if (defined('TEST_DB_FORCE_TYPE')) {
  66. $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example)
  67. }
  68. // Set properties specific to conf file
  69. $conf->file->main_limit_users = $dolibarr_main_limit_users;
  70. $conf->file->mailing_limit_sendbyweb = empty($dolibarr_mailing_limit_sendbyweb) ? 0 : $dolibarr_mailing_limit_sendbyweb;
  71. $conf->file->mailing_limit_sendbycli = empty($dolibarr_mailing_limit_sendbycli) ? 0 : $dolibarr_mailing_limit_sendbycli;
  72. $conf->file->mailing_limit_sendbyday = empty($dolibarr_mailing_limit_sendbyday) ? 0 : $dolibarr_mailing_limit_sendbyday;
  73. $conf->file->main_authentication = empty($dolibarr_main_authentication) ? 'dolibarr' : $dolibarr_main_authentication; // Identification mode
  74. $conf->file->main_force_https = empty($dolibarr_main_force_https) ? '' : $dolibarr_main_force_https; // Force https
  75. $conf->file->strict_mode = empty($dolibarr_strict_mode) ? '' : $dolibarr_strict_mode; // Force php strict mode (for debug)
  76. $conf->file->instance_unique_id = empty($dolibarr_main_instance_unique_id) ? (empty($dolibarr_main_cookie_cryptkey) ? '' : $dolibarr_main_cookie_cryptkey) : $dolibarr_main_instance_unique_id; // Unique id of instance
  77. $conf->file->dol_main_url_root = $dolibarr_main_url_root; // Define url inside the config file
  78. $conf->file->dol_document_root = array('main' => (string) DOL_DOCUMENT_ROOT); // Define array of document root directories ('/home/htdocs')
  79. $conf->file->dol_url_root = array('main' => (string) DOL_URL_ROOT); // Define array of url root path ('' or '/dolibarr')
  80. if (!empty($dolibarr_main_document_root_alt)) {
  81. // dolibarr_main_document_root_alt can contains several directories
  82. $values = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
  83. $i = 0;
  84. foreach ($values as $value) {
  85. $conf->file->dol_document_root['alt'.($i++)] = (string) $value;
  86. }
  87. $values = preg_split('/[;,]/', $dolibarr_main_url_root_alt);
  88. $i = 0;
  89. foreach ($values as $value) {
  90. if (preg_match('/^http(s)?:/', $value)) {
  91. // Show error message
  92. $correct_value = str_replace($dolibarr_main_url_root, '', $value);
  93. print '<b>Error:</b><br>'."\n";
  94. print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>'."\n";
  95. print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>'."\n";
  96. print 'Value found: '.$value.'<br>'."\n";
  97. print 'Should be replaced by: '.$correct_value.'<br>'."\n";
  98. print "Or something like following examples:<br>\n";
  99. print "\"/extensions\"<br>\n";
  100. print "\"/extensions1,/extensions2,...\"<br>\n";
  101. print "\"/../extensions\"<br>\n";
  102. print "\"/custom\"<br>\n";
  103. exit;
  104. }
  105. $conf->file->dol_url_root['alt'.($i++)] = (string) $value;
  106. }
  107. }
  108. // Chargement des includes principaux de librairies communes
  109. if (!defined('NOREQUIREUSER')) {
  110. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; // Need 500ko memory
  111. }
  112. if (!defined('NOREQUIRETRAN')) {
  113. require_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
  114. }
  115. if (!defined('NOREQUIRESOC')) {
  116. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  117. }
  118. /*
  119. * Creation objet $langs (must be before all other code)
  120. */
  121. if (!defined('NOREQUIRETRAN')) {
  122. $langs = new Translate('', $conf); // Must be after reading conf
  123. }
  124. /*
  125. * Object $db
  126. */
  127. $db = null;
  128. if (!defined('NOREQUIREDB')) {
  129. $db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, (int) $conf->db->port);
  130. if ($db->error) {
  131. // If we were into a website context
  132. if (!defined('USEDOLIBARREDITOR') && !defined('USEDOLIBARRSERVER') && !empty($_SERVER['SCRIPT_FILENAME']) && (strpos($_SERVER['SCRIPT_FILENAME'], DOL_DATA_ROOT.'/website') === 0)) {
  133. $sapi_type = php_sapi_name();
  134. if (substr($sapi_type, 0, 3) != 'cgi') {
  135. http_response_code(503); // To tel search engine this is a temporary error
  136. }
  137. print '<div class="center" style="text-align: center; margin: 100px;">';
  138. if (is_object($langs)) {
  139. $langs->setDefaultLang('auto');
  140. $langs->load("website");
  141. print $langs->trans("SorryWebsiteIsCurrentlyOffLine");
  142. } else {
  143. print "SorryWebsiteIsCurrentlyOffLine";
  144. }
  145. print '</div>';
  146. exit;
  147. }
  148. dol_print_error($db, "host=".$conf->db->host.", port=".$conf->db->port.", user=".$conf->db->user.", databasename=".$conf->db->name.", ".$db->error);
  149. exit;
  150. }
  151. }
  152. // Now database connexion is known, so we can forget password
  153. //unset($dolibarr_main_db_pass); // We comment this because this constant is used in some other pages
  154. unset($conf->db->pass); // This is to avoid password to be shown in memory/swap dump
  155. /*
  156. * Object $user
  157. */
  158. if (!defined('NOREQUIREUSER')) {
  159. $user = new User($db);
  160. }
  161. /*
  162. * Create the global $hookmanager object
  163. */
  164. $hookmanager = new HookManager($db);
  165. /*
  166. * Load object $conf
  167. */
  168. // By default conf->entity is 1, but we change this if we ask another value.
  169. if (session_id() && !empty($_SESSION["dol_entity"])) {
  170. // Entity inside an opened session
  171. $conf->entity = $_SESSION["dol_entity"];
  172. } elseif (!empty($_ENV["dol_entity"])) {
  173. // Entity inside a CLI script
  174. $conf->entity = $_ENV["dol_entity"];
  175. } elseif (GETPOSTISSET("loginfunction") && (GETPOST("entity", 'int') || GETPOST("switchentity", 'int'))) {
  176. // Just after a login page
  177. $conf->entity = (GETPOSTISSET("entity") ? GETPOST("entity", 'int') : GETPOST("switchentity", 'int'));
  178. } elseif (defined('DOLENTITY') && is_numeric(constant('DOLENTITY'))) {
  179. // For public page with MultiCompany module
  180. $conf->entity = constant('DOLENTITY');
  181. }
  182. // Sanitize entity
  183. if (!is_numeric($conf->entity)) {
  184. $conf->entity = 1;
  185. }
  186. // Here we read database (llx_const table) and define $conf->global->XXX var.
  187. //print "We work with data into entity instance number '".$conf->entity."'";
  188. $conf->setValues($db);
  189. // Create object $mysoc (A thirdparty object that contains properties of companies managed by Dolibarr.
  190. if (!defined('NOREQUIREDB') && !defined('NOREQUIRESOC')) {
  191. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  192. $mysoc = new Societe($db);
  193. $mysoc->setMysoc($conf);
  194. // We set some specific default values according to country
  195. if ($mysoc->country_code == 'DE' && !isset($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
  196. // For DE, we need to invert our address with customer address
  197. $conf->global->MAIN_INVERT_SENDER_RECIPIENT = 1;
  198. }
  199. if ($mysoc->country_code == 'FR' && !isset($conf->global->INVOICE_CATEGORY_OF_OPERATION)) {
  200. // For FR, default value of option to show category of operations is on by default. Decret n°2099-1299 2022-10-07
  201. $conf->global->INVOICE_CATEGORY_OF_OPERATION = 1;
  202. }
  203. if ($mysoc->country_code == 'FR' && !isset($conf->global->INVOICE_DISABLE_REPLACEMENT)) {
  204. // For FR, the replacement invoice type is not allowed.
  205. // From an accounting point of view, this creates holes in the numbering of the invoice.
  206. // This is very problematic during a fiscal control.
  207. $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
  208. }
  209. if ($mysoc->country_code == 'GR' && !isset($conf->global->INVOICE_DISABLE_REPLACEMENT)) {
  210. // The replacement invoice type is not allowed in Greece.
  211. $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
  212. }
  213. if ($mysoc->country_code == 'GR' && !isset($conf->global->INVOICE_DISABLE_DEPOSIT)) {
  214. // The deposit invoice type is not allowed in Greece.
  215. $conf->global->INVOICE_DISABLE_DEPOSIT = 1;
  216. }
  217. if ($mysoc->country_code == 'GR' && !isset($conf->global->INVOICE_CREDIT_NOTE_STANDALONE)) {
  218. // Standalone credit note is compulsory in Greece.
  219. $conf->global->INVOICE_CREDIT_NOTE_STANDALONE = 1;
  220. }
  221. if ($mysoc->country_code == 'GR' && !isset($conf->global->INVOICE_SUBTYPE_ENABLED)) {
  222. // Invoice subtype is a requirement for Greece.
  223. $conf->global->INVOICE_SUBTYPE_ENABLED = 1;
  224. }
  225. if (($mysoc->localtax1_assuj || $mysoc->localtax2_assuj) && !isset($conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX)) {
  226. // For countries using the 2nd or 3rd tax, we disable input/edit of lines using the price including tax (because 2nb and 3rd tax not yet taken into account).
  227. // Work In Progress to support all taxes into unit price entry when MAIN_UNIT_PRICE_WITH_TAX_IS_FOR_ALL_TAXES is set.
  228. $conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX = 1;
  229. }
  230. }
  231. // Set default language (must be after the setValues setting global $conf->global->MAIN_LANG_DEFAULT. Page main.inc.php will overwrite langs->defaultlang with user value later)
  232. if (!defined('NOREQUIRETRAN')) {
  233. $langcode = (GETPOST('lang', 'aZ09') ? GETPOST('lang', 'aZ09', 1) : getDolGlobalString('MAIN_LANG_DEFAULT', 'auto'));
  234. if (defined('MAIN_LANG_DEFAULT')) { // So a page can force the language whatever is setup and parameters in URL
  235. $langcode = constant('MAIN_LANG_DEFAULT');
  236. }
  237. $langs->setDefaultLang($langcode);
  238. }
  239. if (!defined('MAIN_LABEL_MENTION_NPR')) {
  240. define('MAIN_LABEL_MENTION_NPR', 'NPR');
  241. }