config.class.inc.php 7.8 KB

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