config.class.inc.php 11 KB

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