config.class.inc.php 13 KB

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