master.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. require_once 'filefunc.inc.php'; // May have been already require by main.inc.php. But may not by scripts.
  34. /*
  35. * Create $conf object
  36. */
  37. require_once DOL_DOCUMENT_ROOT.'/core/class/conf.class.php';
  38. $conf = new Conf();
  39. // Set properties specific to database
  40. $conf->db->host = $dolibarr_main_db_host;
  41. $conf->db->port = $dolibarr_main_db_port;
  42. $conf->db->name = $dolibarr_main_db_name;
  43. $conf->db->user = $dolibarr_main_db_user;
  44. $conf->db->pass = empty($dolibarr_main_db_pass) ? '' : $dolibarr_main_db_pass;
  45. $conf->db->type = $dolibarr_main_db_type;
  46. $conf->db->prefix = $dolibarr_main_db_prefix;
  47. $conf->db->character_set = $dolibarr_main_db_character_set;
  48. $conf->db->dolibarr_main_db_collation = $dolibarr_main_db_collation;
  49. $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
  50. $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
  51. if (defined('TEST_DB_FORCE_TYPE')) $conf->db->type = constant('TEST_DB_FORCE_TYPE'); // Force db type (for test purpose, by PHP unit for example)
  52. // Set properties specific to conf file
  53. $conf->file->main_limit_users = $dolibarr_main_limit_users;
  54. $conf->file->mailing_limit_sendbyweb = $dolibarr_mailing_limit_sendbyweb;
  55. $conf->file->mailing_limit_sendbycli = $dolibarr_mailing_limit_sendbycli;
  56. $conf->file->main_authentication = empty($dolibarr_main_authentication) ? '' : $dolibarr_main_authentication; // Identification mode
  57. $conf->file->main_force_https = empty($dolibarr_main_force_https) ? '' : $dolibarr_main_force_https; // Force https
  58. $conf->file->strict_mode = empty($dolibarr_strict_mode) ? '' : $dolibarr_strict_mode; // Force php strict mode (for debug)
  59. $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
  60. $conf->file->dol_document_root = array('main' => (string) DOL_DOCUMENT_ROOT); // Define array of document root directories ('/home/htdocs')
  61. $conf->file->dol_url_root = array('main' => (string) DOL_URL_ROOT); // Define array of url root path ('' or '/dolibarr')
  62. if (!empty($dolibarr_main_document_root_alt))
  63. {
  64. // dolibarr_main_document_root_alt can contains several directories
  65. $values = preg_split('/[;,]/', $dolibarr_main_document_root_alt);
  66. $i = 0;
  67. foreach ($values as $value) $conf->file->dol_document_root['alt'.($i++)] = (string) $value;
  68. $values = preg_split('/[;,]/', $dolibarr_main_url_root_alt);
  69. $i = 0;
  70. foreach ($values as $value)
  71. {
  72. if (preg_match('/^http(s)?:/', $value))
  73. {
  74. // Show error message
  75. $correct_value = str_replace($dolibarr_main_url_root, '', $value);
  76. print '<b>Error:</b><br>'."\n";
  77. print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>'."\n";
  78. print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>'."\n";
  79. print 'Value found: '.$value.'<br>'."\n";
  80. print 'Should be replaced by: '.$correct_value.'<br>'."\n";
  81. print "Or something like following examples:<br>\n";
  82. print "\"/extensions\"<br>\n";
  83. print "\"/extensions1,/extensions2,...\"<br>\n";
  84. print "\"/../extensions\"<br>\n";
  85. print "\"/custom\"<br>\n";
  86. exit;
  87. }
  88. $conf->file->dol_url_root['alt'.($i++)] = (string) $value;
  89. }
  90. }
  91. // Chargement des includes principaux de librairies communes
  92. if (!defined('NOREQUIREUSER')) require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; // Need 500ko memory
  93. if (!defined('NOREQUIRETRAN')) require_once DOL_DOCUMENT_ROOT.'/core/class/translate.class.php';
  94. if (!defined('NOREQUIRESOC')) require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  95. /*
  96. * Creation objet $langs (must be before all other code)
  97. */
  98. if (!defined('NOREQUIRETRAN'))
  99. {
  100. $langs = new Translate('', $conf); // Must be after reading conf
  101. }
  102. /*
  103. * Object $db
  104. */
  105. if (!defined('NOREQUIREDB'))
  106. {
  107. $db = getDoliDBInstance($conf->db->type, $conf->db->host, $conf->db->user, $conf->db->pass, $conf->db->name, $conf->db->port);
  108. if ($db->error)
  109. {
  110. // If we were into a website context
  111. if (!defined('USEDOLIBARREDITOR') && !defined('USEDOLIBARRSERVER') && !empty($_SERVER['SCRIPT_FILENAME']) && (strpos($_SERVER['SCRIPT_FILENAME'], DOL_DATA_ROOT.'/website') === 0))
  112. {
  113. $sapi_type = php_sapi_name();
  114. if (substr($sapi_type, 0, 3) != 'cgi') http_response_code(503); // To tel search engine this is a temporary error
  115. print '<div class="center" style="text-align: center; margin: 100px;">';
  116. if (is_object($langs))
  117. {
  118. $langs->setDefaultLang('auto');
  119. $langs->load("website");
  120. print $langs->trans("SorryWebsiteIsCurrentlyOffLine");
  121. } else {
  122. print "SorryWebsiteIsCurrentlyOffLine";
  123. }
  124. print '</div>';
  125. exit;
  126. }
  127. dol_print_error($db, "host=".$conf->db->host.", port=".$conf->db->port.", user=".$conf->db->user.", databasename=".$conf->db->name.", ".$db->error);
  128. exit;
  129. }
  130. }
  131. // Now database connexion is known, so we can forget password
  132. //unset($dolibarr_main_db_pass); // We comment this because this constant is used in a lot of pages
  133. unset($conf->db->pass); // This is to avoid password to be shown in memory/swap dump
  134. /*
  135. * Object $user
  136. */
  137. if (!defined('NOREQUIREUSER')) {
  138. $user = new User($db);
  139. }
  140. /*
  141. * Load object $conf
  142. * After this, all parameters conf->global->CONSTANTS are loaded
  143. */
  144. // By default conf->entity is 1, but we change this if we ask another value.
  145. if (session_id() && !empty($_SESSION["dol_entity"])) {
  146. // Entity inside an opened session
  147. $conf->entity = $_SESSION["dol_entity"];
  148. } elseif (!empty($_ENV["dol_entity"])) {
  149. // Entity inside a CLI script
  150. $conf->entity = $_ENV["dol_entity"];
  151. } elseif (GETPOSTISSET("loginfunction") && GETPOST("entity", 'int')) {
  152. // Just after a login page
  153. $conf->entity = GETPOST("entity", 'int');
  154. } elseif (defined('DOLENTITY') && is_numeric(DOLENTITY)) {
  155. // For public page with MultiCompany module
  156. $conf->entity = DOLENTITY;
  157. }
  158. // Sanitize entity
  159. if (!is_numeric($conf->entity)) $conf->entity = 1;
  160. if (!defined('NOREQUIREDB'))
  161. {
  162. //print "Will work with data into entity instance number '".$conf->entity."'";
  163. // Here we read database (llx_const table) and define $conf->global->XXX var.
  164. $conf->setValues($db);
  165. }
  166. // Overwrite database value
  167. if (!empty($conf->file->mailing_limit_sendbyweb))
  168. {
  169. $conf->global->MAILING_LIMIT_SENDBYWEB = $conf->file->mailing_limit_sendbyweb;
  170. }
  171. if (empty($conf->global->MAILING_LIMIT_SENDBYWEB))
  172. {
  173. $conf->global->MAILING_LIMIT_SENDBYWEB = 25;
  174. }
  175. if (!empty($conf->file->mailing_limit_sendbycli))
  176. {
  177. $conf->global->MAILING_LIMIT_SENDBYCLI = $conf->file->mailing_limit_sendbycli;
  178. }
  179. if (empty($conf->global->MAILING_LIMIT_SENDBYCLI))
  180. {
  181. $conf->global->MAILING_LIMIT_SENDBYCLI = 0;
  182. }
  183. // If software has been locked. Only login $conf->global->MAIN_ONLY_LOGIN_ALLOWED is allowed.
  184. if (!empty($conf->global->MAIN_ONLY_LOGIN_ALLOWED))
  185. {
  186. $ok = 0;
  187. if ((!session_id() || !isset($_SESSION["dol_login"])) && !isset($_POST["username"]) && !empty($_SERVER["GATEWAY_INTERFACE"])) $ok = 1; // We let working pages if not logged and inside a web browser (login form, to allow login by admin)
  188. elseif (isset($_POST["username"]) && $_POST["username"] == $conf->global->MAIN_ONLY_LOGIN_ALLOWED) $ok = 1; // We let working pages that is a login submission (login submit, to allow login by admin)
  189. elseif (defined('NOREQUIREDB')) $ok = 1; // We let working pages that don't need database access (xxx.css.php)
  190. elseif (defined('EVEN_IF_ONLY_LOGIN_ALLOWED')) $ok = 1; // We let working pages that ask to work even if only login enabled (logout.php)
  191. elseif (session_id() && isset($_SESSION["dol_login"]) && $_SESSION["dol_login"] == $conf->global->MAIN_ONLY_LOGIN_ALLOWED) $ok = 1; // We let working if user is allowed admin
  192. if (!$ok)
  193. {
  194. if (session_id() && isset($_SESSION["dol_login"]) && $_SESSION["dol_login"] != $conf->global->MAIN_ONLY_LOGIN_ALLOWED)
  195. {
  196. print 'Sorry, your application is offline.'."\n";
  197. print 'You are logged with user "'.$_SESSION["dol_login"].'" and only administrator user "'.$conf->global->MAIN_ONLY_LOGIN_ALLOWED.'" is allowed to connect for the moment.'."\n";
  198. $nexturl = DOL_URL_ROOT.'/user/logout.php';
  199. print 'Please try later or <a href="'.$nexturl.'">click here to disconnect and change login user</a>...'."\n";
  200. } else {
  201. print 'Sorry, your application is offline. Only administrator user "'.$conf->global->MAIN_ONLY_LOGIN_ALLOWED.'" is allowed to connect for the moment.'."\n";
  202. $nexturl = DOL_URL_ROOT.'/';
  203. print 'Please try later or <a href="'.$nexturl.'">click here to change login user</a>...'."\n";
  204. }
  205. exit;
  206. }
  207. }
  208. // Create object $mysoc (A thirdparty object that contains properties of companies managed by Dolibarr.
  209. if (!defined('NOREQUIREDB') && !defined('NOREQUIRESOC'))
  210. {
  211. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  212. $mysoc = new Societe($db);
  213. $mysoc->setMysoc($conf);
  214. // For some countries, we need to invert our address with customer address
  215. if ($mysoc->country_code == 'DE' && !isset($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) $conf->global->MAIN_INVERT_SENDER_RECIPIENT = 1;
  216. }
  217. // 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)
  218. if (!defined('NOREQUIRETRAN'))
  219. {
  220. $langcode = (GETPOST('lang', 'aZ09') ?GETPOST('lang', 'aZ09', 1) : (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT));
  221. if (defined('MAIN_LANG_DEFAULT')) $langcode = constant('MAIN_LANG_DEFAULT');
  222. $langs->setDefaultLang($langcode);
  223. }
  224. // Create the global $hookmanager object
  225. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  226. $hookmanager = new HookManager($db);
  227. if (!defined('MAIN_LABEL_MENTION_NPR')) define('MAIN_LABEL_MENTION_NPR', 'NPR');
  228. //if (! defined('PCLZIP_TEMPORARY_DIR')) define('PCLZIP_TEMPORARY_DIR', $conf->user->dir_temp);