config.class.inc.php 16 KB

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