PageRenderTime 70ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/master.inc.php

https://github.com/asterix14/dolibarr
PHP | 293 lines | 182 code | 33 blank | 78 comment | 44 complexity | f62ec3e9341ef8600f74b13fb8022397 MD5 | raw file
Possible License(s): LGPL-2.0
  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-2010 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-2011 Regis Houssin <regis@dolibarr.fr>
  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 2 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. // Identifiant propres au serveur base de donnee
  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. $conf->file->main_limit_users = $dolibarr_main_limit_users;
  51. $conf->file->mailing_limit_sendbyweb = $dolibarr_mailing_limit_sendbyweb;
  52. if (defined('TEST_DB_FORCE_TYPE')) $conf->db->type=constant('TEST_DB_FORCE_TYPE'); // For test purpose
  53. // Identifiant autres
  54. $conf->file->main_authentication = empty($dolibarr_main_authentication)?'':$dolibarr_main_authentication;
  55. // Force https
  56. $conf->file->main_force_https = empty($dolibarr_main_force_https)?'':$dolibarr_main_force_https;
  57. // Define charset for HTML Output (can set hidden value force_charset in conf file)
  58. $conf->file->character_set_client = strtoupper($force_charset_do_notuse);
  59. // Cookie cryptkey
  60. $conf->file->cookie_cryptkey = empty($dolibarr_main_cookie_cryptkey)?'':$dolibarr_main_cookie_cryptkey;
  61. // Define array of document root directories
  62. $conf->file->dol_document_root = array('main' => DOL_DOCUMENT_ROOT);
  63. if (! empty($dolibarr_main_document_root_alt))
  64. {
  65. // dolibarr_main_document_root_alt contains several directories
  66. $values=preg_split('/[;,]/',$dolibarr_main_document_root_alt);
  67. foreach($values as $value)
  68. {
  69. $conf->file->dol_document_root['alt']=$value;
  70. }
  71. }
  72. // Multi-Company transverse mode
  73. $conf->multicompany->transverse_mode = empty($multicompany_transverse_mode)?'':$multicompany_transverse_mode;
  74. // Chargement des includes principaux de librairies communes
  75. if (! defined('NOREQUIREUSER')) require_once(DOL_DOCUMENT_ROOT ."/user/class/user.class.php"); // Need 500ko memory
  76. if (! defined('NOREQUIRETRAN')) require_once(DOL_DOCUMENT_ROOT ."/core/class/translate.class.php");
  77. if (! defined('NOREQUIRESOC')) require_once(DOL_DOCUMENT_ROOT ."/societe/class/societe.class.php");
  78. /*
  79. * Creation objet $langs (must be before all other code)
  80. */
  81. if (! defined('NOREQUIRETRAN'))
  82. {
  83. $langs = new Translate("",$conf); // A mettre apres lecture de la conf
  84. }
  85. /*
  86. * Creation objet $db
  87. */
  88. if (! defined('NOREQUIREDB'))
  89. {
  90. $db=getDoliDBInstance($conf->db->type,$conf->db->host,$conf->db->user,$conf->db->pass,$conf->db->name,$conf->db->port);
  91. if ($db->error)
  92. {
  93. dol_print_error($db,"host=".$conf->db->host.", port=".$conf->db->port.", user=".$conf->db->user.", databasename=".$conf->db->name.", ".$db->error);
  94. exit;
  95. }
  96. }
  97. // Now database connexion is known, so we can forget password
  98. //$dolibarr_main_db_pass=''; // Comment this because this constant is used in a lot of pages
  99. $conf->db->pass=''; // This is to avoid password to be shown in memory/swap dump
  100. /*
  101. * Creation objet $user
  102. */
  103. if (! defined('NOREQUIREUSER'))
  104. {
  105. $user = new User($db);
  106. }
  107. /*
  108. * Load object $conf
  109. * After this, all parameters conf->global->CONSTANTS are loaded
  110. */
  111. if (! defined('NOREQUIREDB'))
  112. {
  113. // By default conf->entity is 1, but we change this if we ask another value.
  114. if (session_id() && ! empty($_SESSION["dol_entity"])) // Entity inside an opened session
  115. {
  116. $conf->entity = $_SESSION["dol_entity"];
  117. }
  118. elseif (! empty($_ENV["dol_entity"])) // Entity inside a CLI script
  119. {
  120. $conf->entity = $_ENV["dol_entity"];
  121. }
  122. elseif (isset($_POST["loginfunction"]) && ! empty($_POST["entity"])) // Just after a login page
  123. {
  124. $conf->entity = $_POST["entity"];
  125. }
  126. else
  127. {
  128. $prefix=dol_getprefix();
  129. $entityCookieName = 'DOLENTITYID_'.$prefix;
  130. if (! empty($_COOKIE[$entityCookieName]) && ! empty($conf->file->cookie_cryptkey)) // Just for view specific login page
  131. {
  132. include_once(DOL_DOCUMENT_ROOT."/core/class/cookie.class.php");
  133. $lastuser = '';
  134. $lastentity = '';
  135. $entityCookie = new DolCookie($conf->file->cookie_cryptkey);
  136. $cookieValue = $entityCookie->_getCookie($entityCookieName);
  137. list($lastuser, $lastentity) = explode('|', $cookieValue);
  138. $conf->entity = $lastentity;
  139. }
  140. }
  141. //print "Will work with data into entity instance number '".$conf->entity."'";
  142. // Here we read database (llx_const table) and define $conf->global->XXX var.
  143. $conf->setValues($db);
  144. }
  145. // Overwrite database value
  146. if (! empty($conf->file->mailing_limit_sendbyweb))
  147. {
  148. $conf->global->MAILING_LIMIT_SENDBYWEB = $conf->file->mailing_limit_sendbyweb;
  149. }
  150. // If software has been locked. Only login $conf->global->MAIN_ONLY_LOGIN_ALLOWED is allowed.
  151. if (! empty($conf->global->MAIN_ONLY_LOGIN_ALLOWED))
  152. {
  153. /*print '$_SERVER["GATEWAY_INTERFACE"]='.$_SERVER["GATEWAY_INTERFACE"].'<br>';
  154. print 'session_id()='.session_id().'<br>';
  155. print '$_SESSION["dol_login"]='.$_SESSION["dol_login"].'<br>';
  156. print '$conf->global->MAIN_ONLY_LOGIN_ALLOWED='.$conf->global->MAIN_ONLY_LOGIN_ALLOWED.'<br>';
  157. exit;*/
  158. $ok=0;
  159. 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)
  160. 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)
  161. elseif (defined('NOREQUIREDB')) $ok=1; // We let working pages that don't need database access (xxx.css.php)
  162. elseif (defined('EVEN_IF_ONLY_LOGIN_ALLOWED')) $ok=1; // We let working pages that ask to work even if only login enabled (logout.php)
  163. 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
  164. if (! $ok)
  165. {
  166. if (session_id() && isset($_SESSION["dol_login"]) && $_SESSION["dol_login"] != $conf->global->MAIN_ONLY_LOGIN_ALLOWED)
  167. {
  168. print 'Sorry, your application is offline.'."\n";
  169. 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";
  170. $nexturl=DOL_URL_ROOT.'/user/logout.php';
  171. print 'Please try later or <a href="'.$nexturl.'">click here to disconnect and change login user</a>...'."\n";
  172. }
  173. else
  174. {
  175. print 'Sorry, your application is offline. Only administrator user "'.$conf->global->MAIN_ONLY_LOGIN_ALLOWED.'" is allowed to connect for the moment.'."\n";
  176. $nexturl=DOL_URL_ROOT.'/';
  177. print 'Please try later or <a href="'.$nexturl.'">click here to change login user</a>...'."\n";
  178. }
  179. exit;
  180. }
  181. }
  182. /*
  183. * Create object $mysoc (A thirdparty object that contains properties of companies managed by Dolibarr.
  184. */
  185. if (! defined('NOREQUIREDB') && ! defined('NOREQUIRESOC'))
  186. {
  187. require_once(DOL_DOCUMENT_ROOT ."/societe/class/societe.class.php");
  188. $mysoc=new Societe($db);
  189. $mysoc->id=0;
  190. $mysoc->nom=$conf->global->MAIN_INFO_SOCIETE_NOM; // TODO deprecated
  191. $mysoc->name=$conf->global->MAIN_INFO_SOCIETE_NOM;
  192. $mysoc->adresse=$conf->global->MAIN_INFO_SOCIETE_ADRESSE; // TODO deprecated
  193. $mysoc->address=$conf->global->MAIN_INFO_SOCIETE_ADRESSE;
  194. $mysoc->cp=$conf->global->MAIN_INFO_SOCIETE_CP; // TODO deprecated
  195. $mysoc->zip=$conf->global->MAIN_INFO_SOCIETE_CP;
  196. $mysoc->ville=$conf->global->MAIN_INFO_SOCIETE_VILLE; // TODO deprecated
  197. $mysoc->town=$conf->global->MAIN_INFO_SOCIETE_VILLE;
  198. $mysoc->state_id=$conf->global->MAIN_INFO_SOCIETE_DEPARTEMENT;
  199. $mysoc->note=empty($conf->global->MAIN_INFO_SOCIETE_NOTE)?'':$conf->global->MAIN_INFO_SOCIETE_NOTE;
  200. // We define pays_id, pays_code and pays_label
  201. $tmp=explode(':',$conf->global->MAIN_INFO_SOCIETE_PAYS);
  202. $country_id=$tmp[0];
  203. if (! empty($tmp[1])) // If $conf->global->MAIN_INFO_SOCIETE_PAYS is "id:code:label"
  204. {
  205. $country_code=$tmp[1];
  206. $country_label=$tmp[2];
  207. }
  208. else // For backward compatibility
  209. {
  210. dol_syslog("Your country setup use an old syntax. Reedit it in setup area.", LOG_WARNING);
  211. include_once(DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php');
  212. $country_code=getCountry($country_id,2,$db); // This need a SQL request, but it's the old feature
  213. $country_label=getCountry($country_id,0,$db); // This need a SQL request, but it's the old feature
  214. }
  215. $mysoc->pays_id=$country_id; // TODO deprecated
  216. $mysoc->country_id=$country_id;
  217. $mysoc->pays_code=$country_code; // TODO deprecated
  218. $mysoc->country_code=$country_code;
  219. $mysoc->country=$country_label;
  220. if (is_object($langs)) $mysoc->country=($langs->trans('Country'.$country_code)!='Country'.$country_code)?$langs->trans('Country'.$country_code):$country_label;
  221. $mysoc->pays=$mysoc->country; // TODO deprecated
  222. $mysoc->tel=empty($conf->global->MAIN_INFO_SOCIETE_TEL)?'':$conf->global->MAIN_INFO_SOCIETE_TEL; // TODO deprecated
  223. $mysoc->phone=empty($conf->global->MAIN_INFO_SOCIETE_TEL)?'':$conf->global->MAIN_INFO_SOCIETE_TEL;
  224. $mysoc->fax=empty($conf->global->MAIN_INFO_SOCIETE_FAX)?'':$conf->global->MAIN_INFO_SOCIETE_FAX;
  225. $mysoc->url=empty($conf->global->MAIN_INFO_SOCIETE_WEB)?'':$conf->global->MAIN_INFO_SOCIETE_WEB;
  226. // Anciens id prof
  227. $mysoc->siren=empty($conf->global->MAIN_INFO_SIREN)?'':$conf->global->MAIN_INFO_SIREN;
  228. $mysoc->siret=empty($conf->global->MAIN_INFO_SIRET)?'':$conf->global->MAIN_INFO_SIRET;
  229. $mysoc->ape=empty($conf->global->MAIN_INFO_APE)?'':$conf->global->MAIN_INFO_APE;
  230. $mysoc->rcs=empty($conf->global->MAIN_INFO_RCS)?'':$conf->global->MAIN_INFO_RCS;
  231. // Id prof generiques
  232. $mysoc->idprof1=empty($conf->global->MAIN_INFO_SIREN)?'':$conf->global->MAIN_INFO_SIREN;
  233. $mysoc->idprof2=empty($conf->global->MAIN_INFO_SIRET)?'':$conf->global->MAIN_INFO_SIRET;
  234. $mysoc->idprof3=empty($conf->global->MAIN_INFO_APE)?'':$conf->global->MAIN_INFO_APE;
  235. $mysoc->idprof4=empty($conf->global->MAIN_INFO_RCS)?'':$conf->global->MAIN_INFO_RCS;
  236. $mysoc->tva_intra=$conf->global->MAIN_INFO_TVAINTRA; // VAT number, not necessarly INTRA.
  237. $mysoc->idtrainer=empty($conf->global->MAIN_INFO_TRAINER)?'':$conf->global->MAIN_INFO_TRAINER;
  238. $mysoc->capital=$conf->global->MAIN_INFO_CAPITAL;
  239. $mysoc->forme_juridique_code=$conf->global->MAIN_INFO_SOCIETE_FORME_JURIDIQUE;
  240. $mysoc->email=$conf->global->MAIN_INFO_SOCIETE_MAIL;
  241. $mysoc->logo=$conf->global->MAIN_INFO_SOCIETE_LOGO;
  242. $mysoc->logo_small=$conf->global->MAIN_INFO_SOCIETE_LOGO_SMALL;
  243. $mysoc->logo_mini=$conf->global->MAIN_INFO_SOCIETE_LOGO_MINI;
  244. // Define if company use vat or not (Do not use conf->global->FACTURE_TVAOPTION anymore)
  245. $mysoc->tva_assuj=((isset($conf->global->FACTURE_TVAOPTION) && $conf->global->FACTURE_TVAOPTION=='franchise')?0:1);
  246. // Define if company use local taxes
  247. $mysoc->localtax1_assuj=((isset($conf->global->FACTURE_LOCAL_TAX1_OPTION) && $conf->global->FACTURE_LOCAL_TAX1_OPTION=='localtax1on')?1:0);
  248. $mysoc->localtax2_assuj=((isset($conf->global->FACTURE_LOCAL_TAX2_OPTION) && $conf->global->FACTURE_LOCAL_TAX2_OPTION=='localtax2on')?1:0);
  249. // For some countries, we need to invert our address with customer address
  250. if ($mysoc->pays_code == 'DE' && ! isset($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) $conf->global->MAIN_INVERT_SENDER_RECIPIENT=1;
  251. }
  252. // Set default language (must be after the setValues of $conf)
  253. if (! defined('NOREQUIRETRAN'))
  254. {
  255. $langs->setDefaultLang($conf->global->MAIN_LANG_DEFAULT);
  256. }
  257. if (! defined('MAIN_LABEL_MENTION_NPR') ) define('MAIN_LABEL_MENTION_NPR','NPR');
  258. // We force feature to help debug
  259. $conf->global->MAIN_JS_ON_PAYMENT=1;
  260. ?>