config.class.inc.php 7.9 KB

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