config.class.inc.php 15 KB

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