config.class.inc.php 9.7 KB

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