config.class.inc.php 11 KB

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