master.inc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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-2012 Regis Houssin <regis.houssin@capnetworks.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. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. /**
  27. * \file htdocs/master.inc.php
  28. * \ingroup core
  29. * \brief File that defines environment for all Dolibarr process (pages or scripts)
  30. * This script reads the conf file, init $lang, $db and and empty $user
  31. */
  32. require_once 'filefunc.inc.php'; // May have been already require by main.inc.php. But may not by scripts.
  33. /*
  34. * Create $conf object
  35. */
  36. require_once DOL_DOCUMENT_ROOT.'/core/class/conf.class.php';
  37. $conf = new Conf();
  38. // Set properties specific to database
  39. $conf->db->host = $dolibarr_main_db_host;
  40. $conf->db->port = $dolibarr_main_db_port;
  41. $conf->db->name = $dolibarr_main_db_name;
  42. $conf->db->user = $dolibarr_main_db_user;
  43. $conf->db->pass = $dolibarr_main_db_pass;
  44. $conf->db->type = $dolibarr_main_db_type;
  45. $conf->db->prefix = $dolibarr_main_db_prefix;
  46. $conf->db->character_set = $dolibarr_main_db_character_set;
  47. $conf->db->dolibarr_main_db_collation = $dolibarr_main_db_collation;
  48. $conf->db->dolibarr_main_db_encryption = $dolibarr_main_db_encryption;
  49. $conf->db->dolibarr_main_db_cryptkey = $dolibarr_main_db_cryptkey;
  50. 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)
  51. // Set properties specific to conf file
  52. $conf->file->main_limit_users = $dolibarr_main_limit_users;
  53. $conf->file->mailing_limit_sendbyweb = $dolibarr_mailing_limit_sendbyweb;
  54. $conf->file->main_authentication = empty($dolibarr_main_authentication)?'':$dolibarr_main_authentication; // Identification mode
  55. $conf->file->main_force_https = empty($dolibarr_main_force_https)?'':$dolibarr_main_force_https; // Force https
  56. $conf->file->strict_mode = empty($dolibarr_strict_mode)?'':$dolibarr_strict_mode; // Force php strict mode (for debug)
  57. $conf->file->cookie_cryptkey = empty($dolibarr_main_cookie_cryptkey)?'':$dolibarr_main_cookie_cryptkey; // Cookie cryptkey
  58. $conf->file->dol_document_root = array('main' => (string) DOL_DOCUMENT_ROOT); // Define array of document root directories ('/home/htdocs')
  59. $conf->file->dol_url_root = array('main' => (string) DOL_URL_ROOT); // Define array of url root path ('' or '/dolibarr')
  60. if (! empty($dolibarr_main_document_root_alt))
  61. {
  62. // dolibarr_main_document_root_alt can contains several directories
  63. $values=preg_split('/[;,]/',$dolibarr_main_document_root_alt);
  64. $i=0;
  65. foreach($values as $value) $conf->file->dol_document_root['alt'.($i++)]=(string) $value;
  66. $values=preg_split('/[;,]/',$dolibarr_main_url_root_alt);
  67. $i=0;
  68. foreach($values as $value)
  69. {
  70. if (preg_match('/^http(s)?:/',$value))
  71. {
  72. // TODO: Make this a warning rather than an error since the correct value can be derived in most cases
  73. $correct_value = str_replace($dolibarr_main_url_root, '', $value);
  74. print '<b>Error:</b><br>'."\n";
  75. print 'Wrong <b>$dolibarr_main_url_root_alt</b> value in <b>conf.php</b> file.<br>'."\n";
  76. print 'We now use a relative path to $dolibarr_main_url_root to build alternate URLs.<br>'."\n";
  77. print 'Value found: '.$value.'<br>'."\n";
  78. print 'Should be replaced by: '.$correct_value.'<br>'."\n";
  79. print "Or something like following examples:<br>\n";
  80. print "\"/extensions\"<br>\n";
  81. print "\"/extensions1,/extensions2,...\"<br>\n";
  82. print "\"/../extensions\"<br>\n";
  83. print "\"/custom\"<br>\n";
  84. exit;
  85. }
  86. $conf->file->dol_url_root['alt'.($i++)]=(string) $value;
  87. }
  88. }
  89. // Set properties specific to multicompany
  90. // TODO Multicompany Remove this. Useless. Var should be read when required.
  91. $conf->multicompany->transverse_mode = empty($multicompany_transverse_mode)?'':$multicompany_transverse_mode; // Force Multi-Company transverse mode
  92. $conf->multicompany->force_entity = empty($multicompany_force_entity)?'':(int) $multicompany_force_entity; // Force entity in login page
  93. // Chargement des includes principaux de librairies communes
  94. if (! defined('NOREQUIREUSER')) require_once DOL_DOCUMENT_ROOT .'/user/class/user.class.php'; // Need 500ko memory
  95. if (! defined('NOREQUIRETRAN')) require_once DOL_DOCUMENT_ROOT .'/core/class/translate.class.php';
  96. if (! defined('NOREQUIRESOC')) require_once DOL_DOCUMENT_ROOT .'/societe/class/societe.class.php';
  97. /*
  98. * Creation objet $langs (must be before all other code)
  99. */
  100. if (! defined('NOREQUIRETRAN'))
  101. {
  102. $langs = new Translate('',$conf); // A mettre apres lecture de la conf
  103. }
  104. /*
  105. * Object $db
  106. */
  107. if (! defined('NOREQUIREDB'))
  108. {
  109. $db=getDoliDBInstance($conf->db->type,$conf->db->host,$conf->db->user,$conf->db->pass,$conf->db->name,$conf->db->port);
  110. if ($db->error)
  111. {
  112. dol_print_error($db,"host=".$conf->db->host.", port=".$conf->db->port.", user=".$conf->db->user.", databasename=".$conf->db->name.", ".$db->error);
  113. exit;
  114. }
  115. }
  116. // Now database connexion is known, so we can forget password
  117. //unset($dolibarr_main_db_pass); // We comment this because this constant is used in a lot of pages
  118. unset($conf->db->pass); // This is to avoid password to be shown in memory/swap dump
  119. /*
  120. * Object $user
  121. */
  122. if (! defined('NOREQUIREUSER'))
  123. {
  124. $user = new User($db);
  125. }
  126. /*
  127. * Load object $conf
  128. * After this, all parameters conf->global->CONSTANTS are loaded
  129. */
  130. if (! defined('NOREQUIREDB'))
  131. {
  132. // By default conf->entity is 1, but we change this if we ask another value.
  133. if (session_id() && ! empty($_SESSION["dol_entity"])) // Entity inside an opened session
  134. {
  135. $conf->entity = $_SESSION["dol_entity"];
  136. }
  137. else if (! empty($_ENV["dol_entity"])) // Entity inside a CLI script
  138. {
  139. $conf->entity = $_ENV["dol_entity"];
  140. }
  141. else if (isset($_POST["loginfunction"]) && GETPOST("entity")) // Just after a login page
  142. {
  143. $conf->entity = GETPOST("entity",'int');
  144. }
  145. else if (defined('DOLENTITY') && is_int(DOLENTITY)) // For public page with MultiCompany module
  146. {
  147. $conf->entity = DOLENTITY;
  148. }
  149. else if (!empty($_COOKIE['DOLENTITY'])) // For other application with MultiCompany module
  150. {
  151. $conf->entity = $_COOKIE['DOLENTITY'];
  152. }
  153. else if (! empty($conf->multicompany->force_entity) && is_int($conf->multicompany->force_entity)) // To force entity in login page
  154. {
  155. $conf->entity = $conf->multicompany->force_entity;
  156. }
  157. //print "Will work with data into entity instance number '".$conf->entity."'";
  158. // Here we read database (llx_const table) and define $conf->global->XXX var.
  159. $conf->setValues($db);
  160. }
  161. // Overwrite database value
  162. if (! empty($conf->file->mailing_limit_sendbyweb))
  163. {
  164. $conf->global->MAILING_LIMIT_SENDBYWEB = $conf->file->mailing_limit_sendbyweb;
  165. }
  166. // If software has been locked. Only login $conf->global->MAIN_ONLY_LOGIN_ALLOWED is allowed.
  167. if (! empty($conf->global->MAIN_ONLY_LOGIN_ALLOWED))
  168. {
  169. $ok=0;
  170. 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)
  171. 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)
  172. elseif (defined('NOREQUIREDB')) $ok=1; // We let working pages that don't need database access (xxx.css.php)
  173. elseif (defined('EVEN_IF_ONLY_LOGIN_ALLOWED')) $ok=1; // We let working pages that ask to work even if only login enabled (logout.php)
  174. 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
  175. if (! $ok)
  176. {
  177. if (session_id() && isset($_SESSION["dol_login"]) && $_SESSION["dol_login"] != $conf->global->MAIN_ONLY_LOGIN_ALLOWED)
  178. {
  179. print 'Sorry, your application is offline.'."\n";
  180. 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";
  181. $nexturl=DOL_URL_ROOT.'/user/logout.php';
  182. print 'Please try later or <a href="'.$nexturl.'">click here to disconnect and change login user</a>...'."\n";
  183. }
  184. else
  185. {
  186. print 'Sorry, your application is offline. Only administrator user "'.$conf->global->MAIN_ONLY_LOGIN_ALLOWED.'" is allowed to connect for the moment.'."\n";
  187. $nexturl=DOL_URL_ROOT.'/';
  188. print 'Please try later or <a href="'.$nexturl.'">click here to change login user</a>...'."\n";
  189. }
  190. exit;
  191. }
  192. }
  193. // Create object $mysoc (A thirdparty object that contains properties of companies managed by Dolibarr.
  194. if (! defined('NOREQUIREDB') && ! defined('NOREQUIRESOC'))
  195. {
  196. require_once DOL_DOCUMENT_ROOT .'/societe/class/societe.class.php';
  197. $mysoc=new Societe($db);
  198. $mysoc->setMysoc($conf);
  199. // For some countries, we need to invert our address with customer address
  200. if ($mysoc->country_code == 'DE' && ! isset($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) $conf->global->MAIN_INVERT_SENDER_RECIPIENT=1;
  201. }
  202. // 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)
  203. if (! defined('NOREQUIRETRAN'))
  204. {
  205. $langcode=(GETPOST('lang')?GETPOST('lang','alpha',1):(empty($conf->global->MAIN_LANG_DEFAULT)?'auto':$conf->global->MAIN_LANG_DEFAULT));
  206. $langs->setDefaultLang($langcode);
  207. }
  208. // Create the global $hookmanager object
  209. include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
  210. $hookmanager=new HookManager($db);
  211. if (! defined('MAIN_LABEL_MENTION_NPR') ) define('MAIN_LABEL_MENTION_NPR','NPR');
  212. // We force feature to help debug
  213. //$conf->global->MAIN_JS_ON_PAYMENT=0;
  214. // We force FPDF
  215. if (! empty($dolibarr_pdf_force_fpdf)) $conf->global->MAIN_USE_FPDF=$dolibarr_pdf_force_fpdf;