config.class.inc.php 12 KB

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