config.class.inc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  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; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. define('ITOP_VERSION', '$ITOP_VERSION$');
  17. define('ITOP_REVISION', '$WCREV$');
  18. define('ITOP_BUILD_DATE', '$WCNOW$');
  19. /**
  20. * Configuration read/write
  21. *
  22. * @author Erwan Taloc <erwan.taloc@combodo.com>
  23. * @author Romain Quetiez <romain.quetiez@combodo.com>
  24. * @author Denis Flaven <denis.flaven@combodo.com>
  25. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  26. */
  27. require_once('coreexception.class.inc.php');
  28. class ConfigException extends CoreException
  29. {
  30. }
  31. define ('DEFAULT_LOG_GLOBAL', true);
  32. define ('DEFAULT_LOG_NOTIFICATION', true);
  33. define ('DEFAULT_LOG_ISSUE', true);
  34. define ('DEFAULT_LOG_WEB_SERVICE', true);
  35. define ('DEFAULT_MIN_DISPLAY_LIMIT', 10);
  36. define ('DEFAULT_MAX_DISPLAY_LIMIT', 15);
  37. define ('DEFAULT_STANDARD_RELOAD_INTERVAL', 5*60);
  38. define ('DEFAULT_FAST_RELOAD_INTERVAL', 1*60);
  39. define ('DEFAULT_SECURE_CONNECTION_REQUIRED', false);
  40. /**
  41. * Config
  42. * configuration data (this class cannot not be localized, because it is responsible for loading the dictionaries)
  43. *
  44. * @package iTopORM
  45. */
  46. class Config
  47. {
  48. //protected $m_bIsLoaded = false;
  49. protected $m_sFile = '';
  50. protected $m_aAppModules;
  51. protected $m_aDataModels;
  52. protected $m_aAddons;
  53. protected $m_aDictionaries;
  54. protected $m_aModuleSettings;
  55. protected $m_sDBHost;
  56. protected $m_sDBUser;
  57. protected $m_sDBPwd;
  58. protected $m_sDBName;
  59. protected $m_sDBSubname;
  60. /**
  61. * @var integer Event log options (see LOG_... definition)
  62. */
  63. protected $m_bLogGlobal;
  64. protected $m_bLogNotification;
  65. protected $m_bLogIssue;
  66. protected $m_bLogWebService;
  67. /**
  68. * @var integer Number of elements to be displayed when there are more than m_iMaxDisplayLimit elements
  69. */
  70. protected $m_iMinDisplayLimit;
  71. /**
  72. * @var integer Max number of elements before truncating the display
  73. */
  74. protected $m_iMaxDisplayLimit;
  75. /**
  76. * @var integer Number of seconds between two reloads of the display (standard)
  77. */
  78. protected $m_iStandardReloadInterval;
  79. /**
  80. * @var integer Number of seconds between two reloads of the display (fast)
  81. */
  82. protected $m_iFastReloadInterval;
  83. /**
  84. * @var boolean Whether or not a secure connection is required for using the application
  85. */
  86. protected $m_bSecureConnectionRequired;
  87. /**
  88. * @var string Langage code, default if the user language is undefined
  89. */
  90. protected $m_sDefaultLanguage;
  91. public function __construct($sConfigFile, $bLoadConfig = true)
  92. {
  93. $this->m_sFile = $sConfigFile;
  94. $this->m_aAppModules = array(
  95. // Some default modules, always present can be move to an official iTop Module later if needed
  96. '../application/transaction.class.inc.php',
  97. '../application/menunode.class.inc.php',
  98. '../application/user.preferences.class.inc.php',
  99. '../application/audit.rule.class.inc.php',
  100. // Romain - That's dirty, because those 3 classes are in fact part of the core
  101. // but I needed those classes to be derived from cmdbAbstractObject
  102. // (to be managed via the GUI) and this class in not really known from
  103. // the core, PLUS I needed the includes to be there also for the setup
  104. // to create the tables.
  105. '../core/event.class.inc.php',
  106. '../core/action.class.inc.php',
  107. '../core/trigger.class.inc.php',
  108. );
  109. $this->m_aDataModels = array();
  110. $this->m_aAddons = array(
  111. // Default AddOn, always present can be moved to an official iTop Module later if needed
  112. 'user rights' => '../addons/userrights/userrightsprofile.class.inc.php',
  113. );
  114. $this->m_aDictionaries = array(
  115. // Default dictionaries, always present can be moved to an official iTop Module later if needed
  116. '../dictionaries/dictionary.itop.core.php',
  117. '../dictionaries/dictionary.itop.ui.php', // Support for English
  118. '../dictionaries/fr.dictionary.itop.ui.php', // Support for French
  119. );
  120. $this->m_sDBHost = '';
  121. $this->m_sDBUser = '';
  122. $this->m_sDBPwd = '';
  123. $this->m_sDBName = '';
  124. $this->m_sDBSubname = '';
  125. $this->m_bLogGlobal = DEFAULT_LOG_GLOBAL;
  126. $this->m_bLogNotification = DEFAULT_LOG_NOTIFICATION;
  127. $this->m_bLogIssue = DEFAULT_LOG_ISSUE;
  128. $this->m_bLogWebService = DEFAULT_LOG_WEB_SERVICE;
  129. $this->m_iMinDisplayLimit = DEFAULT_MIN_DISPLAY_LIMIT;
  130. $this->m_iMaxDisplayLimit = DEFAULT_MAX_DISPLAY_LIMIT;
  131. $this->m_iStandardReloadInterval = DEFAULT_STANDARD_RELOAD_INTERVAL;
  132. $this->m_iFastReloadInterval = DEFAULT_FAST_RELOAD_INTERVAL;
  133. $this->m_bSecureConnectionRequired = DEFAULT_SECURE_CONNECTION_REQUIRED;
  134. $this->m_sDefaultLanguage = 'EN US';
  135. $this->m_aModuleSettings = array();
  136. if ($bLoadConfig)
  137. {
  138. $this->Load($sConfigFile);
  139. $this->Verify();
  140. }
  141. }
  142. protected function CheckFile($sPurpose, $sFileName)
  143. {
  144. if (!file_exists($sFileName))
  145. {
  146. throw new ConfigException("Could not find $sPurpose file", array('file' => $sFileName));
  147. }
  148. }
  149. protected function Load($sConfigFile)
  150. {
  151. $this->CheckFile('configuration', $sConfigFile);
  152. $sConfigCode = trim(file_get_contents($sConfigFile));
  153. // This does not work on several lines
  154. // preg_match('/^<\\?php(.*)\\?'.'>$/', $sConfigCode, $aMatches)...
  155. // So, I've implemented a solution suggested in the PHP doc (search for phpWrapper)
  156. try
  157. {
  158. ob_start();
  159. eval('?'.'>'.trim($sConfigCode));
  160. $sNoise = trim(ob_get_contents());
  161. ob_end_clean();
  162. }
  163. catch (Exception $e)
  164. {
  165. // well, never reach in case of parsing error :-(
  166. // will be improved in PHP 6 ?
  167. throw new ConfigException('Error in configuration file', array('file' => $sConfigFile, 'error' => $e->getMessage()));
  168. }
  169. if (strlen($sNoise) > 0)
  170. {
  171. // Note: sNoise is an html output, but so far it was ok for me (e.g. showing the entire call stack)
  172. throw new ConfigException('Syntax error in configuration file', array('file' => $sConfigFile, 'error' => '<tt>'.htmlentities($sNoise).'</tt>'));
  173. }
  174. if (!isset($MySettings) || !is_array($MySettings))
  175. {
  176. throw new ConfigException('Missing array in configuration file', array('file' => $sConfigFile, 'expected' => '$MySettings'));
  177. }
  178. if (!isset($MyModules) || !is_array($MyModules))
  179. {
  180. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules'));
  181. }
  182. if (!array_key_exists('application', $MyModules))
  183. {
  184. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'application\']'));
  185. }
  186. if (!array_key_exists('business', $MyModules))
  187. {
  188. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'business\']'));
  189. }
  190. if (!array_key_exists('addons', $MyModules))
  191. {
  192. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'addons\']'));
  193. }
  194. if (!array_key_exists('user rights', $MyModules['addons']))
  195. {
  196. $MyModules['addons']['user rights'] = '../addons/userrights/userrightsnull.class.inc.php';
  197. }
  198. if (!array_key_exists('dictionaries', $MyModules))
  199. {
  200. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'dictionaries\']'));
  201. }
  202. $this->m_aAppModules = $MyModules['application'];
  203. $this->m_aDataModels = $MyModules['business'];
  204. $this->m_aAddons = $MyModules['addons'];
  205. $this->m_aDictionaries = $MyModules['dictionaries'];
  206. $this->m_sDBHost = trim($MySettings['db_host']);
  207. $this->m_sDBUser = trim($MySettings['db_user']);
  208. $this->m_sDBPwd = trim($MySettings['db_pwd']);
  209. $this->m_sDBName = trim($MySettings['db_name']);
  210. $this->m_sDBSubname = trim($MySettings['db_subname']);
  211. $this->m_bLogGlobal = isset($MySettings['log_global']) ? trim($MySettings['log_global']) : DEFAULT_LOG_GLOBAL;
  212. $this->m_bLogNotification = isset($MySettings['log_notification']) ? trim($MySettings['log_notification']) : DEFAULT_LOG_NOTIFICATION;
  213. $this->m_bLogIssue = isset($MySettings['log_issue']) ? trim($MySettings['log_issue']) : DEFAULT_LOG_ISSUE;
  214. $this->m_bLogWebService = isset($MySettings['log_web_service']) ? trim($MySettings['log_web_service']) : DEFAULT_LOG_WEB_SERVICE;
  215. $this->m_iMinDisplayLimit = isset($MySettings['min_display_limit']) ? trim($MySettings['min_display_limit']) : DEFAULT_MIN_DISPLAY_LIMIT;
  216. $this->m_iMaxDisplayLimit = isset($MySettings['max_display_limit']) ? trim($MySettings['max_display_limit']) : DEFAULT_MAX_DISPLAY_LIMIT;
  217. $this->m_iStandardReloadInterval = isset($MySettings['standard_reload_interval']) ? trim($MySettings['standard_reload_interval']) : DEFAULT_STANDARD_RELOAD_INTERVAL;
  218. $this->m_iFastReloadInterval = isset($MySettings['fast_reload_interval']) ? trim($MySettings['fast_reload_interval']) : DEFAULT_FAST_RELOAD_INTERVAL;
  219. $this->m_bSecureConnectionRequired = isset($MySettings['secure_connection_required']) ? trim($MySettings['secure_connection_required']) : DEFAULT_SECURE_CONNECTION_REQUIRED;
  220. $this->m_aModuleSettings = isset($MyModuleSettings) ? $MyModuleSettings : array();
  221. $this->m_sDefaultLanguage = isset($MySettings['default_language']) ? trim($MySettings['default_language']) : 'EN US';
  222. }
  223. protected function Verify()
  224. {
  225. foreach ($this->m_aAppModules as $sModule => $sToInclude)
  226. {
  227. $this->CheckFile('application module', $sToInclude);
  228. }
  229. foreach ($this->m_aDataModels as $sModule => $sToInclude)
  230. {
  231. $this->CheckFile('business model', $sToInclude);
  232. }
  233. foreach ($this->m_aAddons as $sModule => $sToInclude)
  234. {
  235. $this->CheckFile('addon module', $sToInclude);
  236. }
  237. foreach ($this->m_aDictionaries as $sModule => $sToInclude)
  238. {
  239. $this->CheckFile('dictionary', $sToInclude);
  240. }
  241. }
  242. public function GetModuleSetting($sModule, $sProperty, $defaultvalue = null)
  243. {
  244. if (isset($this->m_aModuleSettings[$sModule][$sProperty]))
  245. {
  246. return $this->m_aModuleSettings[$sModule][$sProperty];
  247. }
  248. return $defaultvalue;
  249. }
  250. public function SetModuleSetting($sModule, $sProperty, $value)
  251. {
  252. $this->m_aModuleSettings[$sModule][$sProperty] = $value;
  253. }
  254. public function GetAppModules()
  255. {
  256. return $this->m_aAppModules;
  257. }
  258. public function SetAppModules($aAppModules)
  259. {
  260. $this->m_aAppModules = $aAppModules;
  261. }
  262. public function GetDataModels()
  263. {
  264. return $this->m_aDataModels;
  265. }
  266. public function SetDataModels($aDataModels)
  267. {
  268. $this->m_aDataModels = $aDataModels;
  269. }
  270. public function GetAddons()
  271. {
  272. return $this->m_aAddons;
  273. }
  274. public function SetAddons($aAddons)
  275. {
  276. $this->m_aAddons = $aAddons;
  277. }
  278. public function GetDictionaries()
  279. {
  280. return $this->m_aDictionaries;
  281. }
  282. public function SetDictionaries($aDictionaries)
  283. {
  284. $this->m_aDictionaries = $aDictionaries;
  285. }
  286. public function GetDBHost()
  287. {
  288. return $this->m_sDBHost;
  289. }
  290. public function GetDBName()
  291. {
  292. return $this->m_sDBName;
  293. }
  294. public function GetDBSubname()
  295. {
  296. return $this->m_sDBSubname;
  297. }
  298. public function GetDBUser()
  299. {
  300. return $this->m_sDBUser;
  301. }
  302. public function GetDBPwd()
  303. {
  304. return $this->m_sDBPwd;
  305. }
  306. public function GetLogGlobal()
  307. {
  308. return $this->m_bLogGlobal;
  309. }
  310. public function GetLogNotification()
  311. {
  312. return $this->m_bLogNotification;
  313. }
  314. public function GetLogIssue()
  315. {
  316. return $this->m_bLogIssue;
  317. }
  318. public function GetLogWebService()
  319. {
  320. return $this->m_bLogWebService;
  321. }
  322. public function GetMinDisplayLimit()
  323. {
  324. return $this->m_iMinDisplayLimit;
  325. }
  326. public function GetMaxDisplayLimit()
  327. {
  328. return $this->m_iMaxDisplayLimit;
  329. }
  330. public function GetStandardReloadInterval()
  331. {
  332. return $this->m_iStandardReloadInterval;
  333. }
  334. public function GetFastReloadInterval()
  335. {
  336. return $this->m_iFastReloadInterval;
  337. }
  338. public function GetSecureConnectionRequired()
  339. {
  340. return $this->m_bSecureConnectionRequired;
  341. }
  342. public function GetDefaultLanguage()
  343. {
  344. return $this->m_sDefaultLanguage;
  345. }
  346. public function SetDBHost($sDBHost)
  347. {
  348. $this->m_sDBHost = $sDBHost;
  349. }
  350. public function SetDBName($sDBName)
  351. {
  352. $this->m_sDBName = $sDBName;
  353. }
  354. public function SetDBSubname($sDBSubName)
  355. {
  356. $this->m_sDBSubname = $sDBSubName;
  357. }
  358. public function SetDBUser($sUser)
  359. {
  360. $this->m_sDBUser = $sUser;
  361. }
  362. public function SetDBPwd($sPwd)
  363. {
  364. $this->m_sDBPwd = $sPwd;
  365. }
  366. public function SetLogGlobal($iLogGlobal)
  367. {
  368. $this->m_iLogGlobal = $iLogGlobal;
  369. }
  370. public function SetLogNotification($iLogNotification)
  371. {
  372. $this->m_iLogNotification = $iLogNotification;
  373. }
  374. public function SetLogIssue($iLogIssue)
  375. {
  376. $this->m_iLogIssue = $iLogIssue;
  377. }
  378. public function SetLogWebService($iLogWebService)
  379. {
  380. $this->m_iLogWebService = $iLogWebService;
  381. }
  382. public function SetMinDisplayLimit($iMinDisplayLimit)
  383. {
  384. $this->m_iMinDisplayLimit = $iMinDisplayLimit;
  385. }
  386. public function SetMaxDisplayLimit($iMaxDisplayLimit)
  387. {
  388. $this->m_iMaxDisplayLimit = $iMaxDisplayLimit;
  389. }
  390. public function SetStandardReloadInterval($iStandardReloadInterval)
  391. {
  392. $this->m_iStandardReloadInterval = $iStandardReloadInterval;
  393. }
  394. public function SetFastReloadInterval($iFastReloadInterval)
  395. {
  396. $this->m_iFastReloadInterval = $iFastReloadInterval;
  397. }
  398. public function SetSecureConnectionRequired($bSecureConnectionRequired)
  399. {
  400. $this->m_bSecureConnectionRequired = $bSecureConnectionRequired;
  401. }
  402. public function SetDefaultLanguage($sLanguageCode)
  403. {
  404. $this->m_sDefaultLanguage = $sLanguageCode;
  405. }
  406. public function FileIsWritable()
  407. {
  408. return is_writable($this->m_sFile);
  409. }
  410. /**
  411. * Write the configuration to a file (php format) that can be reloaded later
  412. * By default write to the same file that was specified when constructing the object
  413. * @param $sFileName string Name of the file to write to (emtpy to write to the same file)
  414. * @return boolean True otherwise throws an Exception
  415. */
  416. public function WriteToFile($sFileName = '')
  417. {
  418. if (empty($sFileName))
  419. {
  420. $sFileName = $this->m_sFile;
  421. }
  422. $hFile = @fopen($sFileName, 'w');
  423. if ($hFile !== false)
  424. {
  425. fwrite($hFile, "<?php\n");
  426. fwrite($hFile, "\n/**\n");
  427. fwrite($hFile, " *\n");
  428. fwrite($hFile, " * phpMyORM configuration file, generated by the iTop configuration wizard\n");
  429. fwrite($hFile, " *\n");
  430. fwrite($hFile, " * The file is used in MetaModel::LoadConfig() which does all the necessary initialization job\n");
  431. fwrite($hFile, " *\n");
  432. fwrite($hFile, " */\n");
  433. fwrite($hFile, "\n");
  434. fwrite($hFile, "\$MySettings = array(\n");
  435. fwrite($hFile, "\t'db_host' => '{$this->m_sDBHost}',\n");
  436. fwrite($hFile, "\t'db_user' => '{$this->m_sDBUser}',\n");
  437. fwrite($hFile, "\t'db_pwd' => '".addslashes($this->m_sDBPwd)."',\n");
  438. fwrite($hFile, "\t'db_name' => '{$this->m_sDBName}',\n");
  439. fwrite($hFile, "\t'db_subname' => '{$this->m_sDBSubname}',\n");
  440. fwrite($hFile, "\n");
  441. fwrite($hFile, "\t'log_global' => {$this->m_bLogGlobal},\n");
  442. fwrite($hFile, "\t'log_notification' => {$this->m_bLogNotification},\n");
  443. fwrite($hFile, "\t'log_issue' => {$this->m_bLogIssue},\n");
  444. fwrite($hFile, "\t'log_web_service' => {$this->m_bLogWebService},\n");
  445. fwrite($hFile, "\t'min_display_limit' => {$this->m_iMinDisplayLimit},\n");
  446. fwrite($hFile, "\t'max_display_limit' => {$this->m_iMaxDisplayLimit},\n");
  447. fwrite($hFile, "\t'standard_reload_interval' => {$this->m_iStandardReloadInterval},\n");
  448. fwrite($hFile, "\t'fast_reload_interval' => {$this->m_iFastReloadInterval},\n");
  449. fwrite($hFile, "\t'secure_connection_required' => ".($this->m_bSecureConnectionRequired ? 'true' : 'false').",\n");
  450. fwrite($hFile, "\t'default_language' => '{$this->m_sDefaultLanguage}',\n");
  451. fwrite($hFile, ");\n");
  452. fwrite($hFile, "\n");
  453. fwrite($hFile, "\$MyModuleSettings = array(\n");
  454. foreach ($this->m_aModuleSettings as $sModule => $aProperties)
  455. {
  456. fwrite($hFile, "\t'$sModule' => array (\n");
  457. foreach ($aProperties as $sProperty => $value)
  458. {
  459. $sExport = var_export($value, true);
  460. fwrite($hFile, "\t\t'$sProperty' => $sExport,\n");
  461. }
  462. fwrite($hFile, "\t),\n");
  463. }
  464. fwrite($hFile, ");\n");
  465. fwrite($hFile, "\n/**\n");
  466. fwrite($hFile, " *\n");
  467. fwrite($hFile, " * Data model modules to be loaded. Names should be specified as absolute paths\n");
  468. fwrite($hFile, " *\n");
  469. fwrite($hFile, " */\n");
  470. fwrite($hFile, "\$MyModules = array(\n");
  471. fwrite($hFile, "\t'application' => array (\n");
  472. foreach($this->m_aAppModules as $sFile)
  473. {
  474. fwrite($hFile, "\t\t'$sFile',\n");
  475. }
  476. fwrite($hFile, "\t),\n");
  477. fwrite($hFile, "\t'business' => array (\n");
  478. foreach($this->m_aDataModels as $sFile)
  479. {
  480. fwrite($hFile, "\t\t'$sFile',\n");
  481. }
  482. fwrite($hFile, "\t),\n");
  483. fwrite($hFile, "\t'addons' => array (\n");
  484. foreach($this->m_aAddons as $sKey => $sFile)
  485. {
  486. fwrite($hFile, "\t\t'$sKey' => '$sFile',\n");
  487. }
  488. fwrite($hFile, "\t),\n");
  489. fwrite($hFile, "\t'dictionaries' => array (\n");
  490. foreach($this->m_aDictionaries as $sFile)
  491. {
  492. fwrite($hFile, "\t\t'$sFile',\n");
  493. }
  494. fwrite($hFile, "\t),\n");
  495. fwrite($hFile, ");\n");
  496. fwrite($hFile, '?'.'>'); // Avoid perturbing the syntax highlighting !
  497. return fclose($hFile);
  498. }
  499. else
  500. {
  501. throw new ConfigException("Could not write to configuration file", array('file' => $sFileName));
  502. }
  503. }
  504. }
  505. ?>