config.class.inc.php 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. define('ITOP_APPLICATION', 'iTop');
  17. define('ITOP_VERSION', '$ITOP_VERSION$');
  18. define('ITOP_REVISION', '$WCREV$');
  19. define('ITOP_BUILD_DATE', '$WCNOW$');
  20. define('ACCESS_USER_WRITE', 1);
  21. define('ACCESS_ADMIN_WRITE', 2);
  22. define('ACCESS_FULL', ACCESS_USER_WRITE | ACCESS_ADMIN_WRITE);
  23. define('ACCESS_READONLY', 0);
  24. /**
  25. * Configuration read/write
  26. *
  27. * @author Erwan Taloc <erwan.taloc@combodo.com>
  28. * @author Romain Quetiez <romain.quetiez@combodo.com>
  29. * @author Denis Flaven <denis.flaven@combodo.com>
  30. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  31. */
  32. require_once('coreexception.class.inc.php');
  33. class ConfigException extends CoreException
  34. {
  35. }
  36. define ('DEFAULT_CHARACTER_SET', 'utf8');
  37. define ('DEFAULT_COLLATION', 'utf8_unicode_ci');
  38. define ('DEFAULT_LOG_GLOBAL', true);
  39. define ('DEFAULT_LOG_NOTIFICATION', true);
  40. define ('DEFAULT_LOG_ISSUE', true);
  41. define ('DEFAULT_LOG_WEB_SERVICE', true);
  42. define ('DEFAULT_LOG_KPI_DURATION', false);
  43. define ('DEFAULT_LOG_KPI_MEMORY', false);
  44. define ('DEFAULT_DEBUG_QUERIES', false);
  45. define ('DEFAULT_QUERY_CACHE_ENABLED', true);
  46. define ('DEFAULT_MIN_DISPLAY_LIMIT', 10);
  47. define ('DEFAULT_MAX_DISPLAY_LIMIT', 15);
  48. define ('DEFAULT_STANDARD_RELOAD_INTERVAL', 5*60);
  49. define ('DEFAULT_FAST_RELOAD_INTERVAL', 1*60);
  50. define ('DEFAULT_SECURE_CONNECTION_REQUIRED', false);
  51. define ('DEFAULT_HTTPS_HYPERLINKS', false);
  52. define ('DEFAULT_ALLOWED_LOGIN_TYPES', 'form|basic|external');
  53. define ('DEFAULT_EXT_AUTH_VARIABLE', '$_SERVER[\'REMOTE_USER\']');
  54. define ('DEFAULT_ENCRYPTION_KEY', '@iT0pEncr1pti0n!'); // We'll use a random value, later...
  55. /**
  56. * Config
  57. * configuration data (this class cannot not be localized, because it is responsible for loading the dictionaries)
  58. *
  59. * @package iTopORM
  60. */
  61. class Config
  62. {
  63. //protected $m_bIsLoaded = false;
  64. protected $m_sFile = '';
  65. protected $m_aAppModules;
  66. protected $m_aDataModels;
  67. protected $m_aWebServiceCategories;
  68. protected $m_aAddons;
  69. protected $m_aDictionaries;
  70. protected $m_aModuleSettings;
  71. // New way to store the settings !
  72. //
  73. protected $m_aSettings = array(
  74. 'skip_check_to_write' => array(
  75. 'type' => 'bool',
  76. 'description' => 'Disable data format and integrity checks to boost up data load (insert or update)',
  77. 'default' => false,
  78. 'value' => false,
  79. 'source_of_value' => '',
  80. 'show_in_conf_sample' => false,
  81. ),
  82. 'skip_check_ext_keys' => array(
  83. 'type' => 'bool',
  84. 'description' => 'Disable external key check when checking the value of attributes',
  85. 'default' => false,
  86. 'value' => false,
  87. 'source_of_value' => '',
  88. 'show_in_conf_sample' => false,
  89. ),
  90. 'skip_strong_security' => array(
  91. 'type' => 'bool',
  92. 'description' => 'Disable strong security - TEMPORY: this flag should be removed when we are more confident in the recent change in security',
  93. 'default' => true,
  94. 'value' => true,
  95. 'source_of_value' => '',
  96. 'show_in_conf_sample' => false,
  97. ),
  98. 'graphviz_path' => array(
  99. 'type' => 'string',
  100. 'description' => 'Path to the Graphviz "dot" executable for graphing objects lifecycle',
  101. 'default' => '/usr/bin/dot',
  102. 'value' => '',
  103. 'source_of_value' => '',
  104. 'show_in_conf_sample' => false,
  105. ),
  106. 'session_name' => array(
  107. 'type' => 'string',
  108. 'description' => 'The name of the cookie used to store the PHP session id',
  109. 'default' => 'iTop',
  110. 'value' => '',
  111. 'source_of_value' => '',
  112. 'show_in_conf_sample' => true,
  113. ),
  114. 'max_combo_length' => array(
  115. 'type' => 'integer',
  116. 'description' => 'The maximum number of elements in a drop-down list. If more then an autocomplete will be used',
  117. 'default' => 50,
  118. 'value' => 50,
  119. 'source_of_value' => '',
  120. 'show_in_conf_sample' => false,
  121. ),
  122. 'min_autocomplete_chars' => array(
  123. 'type' => 'integer',
  124. 'description' => 'The minimum number of characters to type in order to trigger the "autocomplete" behavior',
  125. 'default' => 3,
  126. 'value' => 3,
  127. 'source_of_value' => '',
  128. 'show_in_conf_sample' => false,
  129. ),
  130. 'allow_target_creation' => array(
  131. 'type' => 'bool',
  132. 'description' => 'Displays the + button on external keys to create target objects',
  133. 'default' => true,
  134. 'value' => true,
  135. 'source_of_value' => '',
  136. 'show_in_conf_sample' => false,
  137. ),
  138. // Levels that trigger a confirmation in the CSV import/synchro wizard
  139. 'csv_import_min_object_confirmation' => array(
  140. 'type' => 'integer',
  141. 'description' => 'Minimum number of objects to check for the confirmation percentages',
  142. 'default' => 3,
  143. 'value' => 3,
  144. 'source_of_value' => '',
  145. 'show_in_conf_sample' => false,
  146. ),
  147. 'csv_import_errors_percentage' => array(
  148. 'type' => 'integer',
  149. 'description' => 'Percentage of errors that trigger a confirmation in the CSV import',
  150. 'default' => 50,
  151. 'value' => 50,
  152. 'source_of_value' => '',
  153. 'show_in_conf_sample' => false,
  154. ),
  155. 'csv_import_modifications_percentage' => array(
  156. 'type' => 'integer',
  157. 'description' => 'Percentage of modifications that trigger a confirmation in the CSV import',
  158. 'default' => 50,
  159. 'value' => 50,
  160. 'source_of_value' => '',
  161. 'show_in_conf_sample' => false,
  162. ),
  163. 'csv_import_creations_percentage' => array(
  164. 'type' => 'integer',
  165. 'description' => 'Percentage of creations that trigger a confirmation in the CSV import',
  166. 'default' => 50,
  167. 'value' => 50,
  168. 'source_of_value' => '',
  169. 'show_in_conf_sample' => false,
  170. ),
  171. 'access_mode' => array(
  172. 'type' => 'integer',
  173. 'description' => 'Combination of flags (ACCESS_USER_WRITE | ACCESS_ADMIN_WRITE, or ACCESS_FULL)',
  174. 'default' => ACCESS_FULL,
  175. 'value' => ACCESS_FULL,
  176. 'source_of_value' => '',
  177. 'show_in_conf_sample' => true,
  178. ),
  179. 'access_message' => array(
  180. 'type' => 'string',
  181. 'description' => 'Message displayed to the users when there is any access restriction',
  182. 'default' => 'iTop is temporarily frozen, please wait... (the admin team)',
  183. 'value' => '',
  184. 'source_of_value' => '',
  185. 'show_in_conf_sample' => true,
  186. ),
  187. 'online_help' => array(
  188. 'type' => 'string',
  189. 'description' => 'Hyperlink to the online-help web page',
  190. 'default' => 'http://www.combodo.com/itop-help',
  191. 'value' => '',
  192. 'source_of_value' => '',
  193. 'show_in_conf_sample' => true,
  194. ),
  195. 'log_usage' => array(
  196. 'type' => 'bool',
  197. 'description' => 'Log the usage of the application (i.e. the date/time and the user name of each login)',
  198. 'default' => false,
  199. 'value' => false,
  200. 'source_of_value' => '',
  201. 'show_in_conf_sample' => false,
  202. ),
  203. 'synchro_trace' => array(
  204. 'type' => 'string',
  205. 'description' => 'Synchronization details: none, display, save (includes \'display\')',
  206. 'default' => 'none',
  207. 'value' => 'none',
  208. 'source_of_value' => '',
  209. 'show_in_conf_sample' => true,
  210. ),
  211. 'link_set_item_separator' => array(
  212. 'type' => 'string',
  213. 'description' => 'Link set from string: line separator',
  214. 'default' => '|',
  215. 'value' => '|',
  216. 'source_of_value' => '',
  217. 'show_in_conf_sample' => true,
  218. ),
  219. 'link_set_attribute_separator' => array(
  220. 'type' => 'string',
  221. 'description' => 'Link set from string: attribute separator',
  222. 'default' => ';',
  223. 'value' => ';',
  224. 'source_of_value' => '',
  225. 'show_in_conf_sample' => true,
  226. ),
  227. 'link_set_value_separator' => array(
  228. 'type' => 'string',
  229. 'description' => 'Link set from string: value separator (between the attcode and the value itself',
  230. 'default' => ':',
  231. 'value' => ':',
  232. 'source_of_value' => '',
  233. 'show_in_conf_sample' => true,
  234. ),
  235. 'link_set_attribute_qualifier' => array(
  236. 'type' => 'string',
  237. 'description' => 'Link set from string: attribute qualifier (encloses both the attcode and the value)',
  238. 'default' => "'",
  239. 'value' => "'",
  240. 'source_of_value' => '',
  241. 'show_in_conf_sample' => true,
  242. ),
  243. 'cron_max_execution_time' => array(
  244. 'type' => 'integer',
  245. 'description' => 'Duration (seconds) of the page cron.php, must be shorter than php setting max_execution_time and shorter than the web server response timeout',
  246. 'default' => 60,
  247. 'value' => 60,
  248. 'source_of_value' => '',
  249. 'show_in_conf_sample' => true,
  250. ),
  251. 'cron_sleep' => array(
  252. 'type' => 'integer',
  253. 'description' => 'Duration (seconds) before cron.php checks again if something must be done',
  254. 'default' => 2,
  255. 'value' => 2,
  256. 'source_of_value' => '',
  257. 'show_in_conf_sample' => false,
  258. ),
  259. 'email_asynchronous' => array(
  260. 'type' => 'bool',
  261. 'description' => 'If set, the emails are sent off line, which requires cron.php to be activated. Exception: some features like the email test utility will force the serialized mode',
  262. 'default' => false,
  263. 'value' => false,
  264. 'source_of_value' => '',
  265. 'show_in_conf_sample' => true,
  266. ),
  267. );
  268. public function IsProperty($sPropCode)
  269. {
  270. return (array_key_exists($sPropCode, $this->m_aSettings));
  271. }
  272. public function Set($sPropCode, $value, $sSourceDesc = 'unknown')
  273. {
  274. $sType = $this->m_aSettings[$sPropCode]['type'];
  275. switch($sType)
  276. {
  277. case 'bool':
  278. $value = (bool) $value;
  279. break;
  280. case 'string':
  281. $value = (string) $value;
  282. break;
  283. case 'integer':
  284. $value = (integer) $value;
  285. break;
  286. case 'float':
  287. $value = (float) $value;
  288. break;
  289. default:
  290. throw new CoreException('Unknown type for setting', array('property' => $sPropCode, 'type' => $sType));
  291. }
  292. $this->m_aSettings[$sPropCode]['value'] = $value;
  293. $this->m_aSettings[$sPropCode]['source_of_value'] = $sSourceDesc;
  294. }
  295. public function Get($sPropCode)
  296. {
  297. return $this->m_aSettings[$sPropCode]['value'];
  298. }
  299. // Those variables will be deprecated later, when the transition to ...Get('my_setting') will be done
  300. protected $m_sDBHost;
  301. protected $m_sDBUser;
  302. protected $m_sDBPwd;
  303. protected $m_sDBName;
  304. protected $m_sDBSubname;
  305. protected $m_sDBCharacterSet;
  306. protected $m_sDBCollation;
  307. /**
  308. * Event log options (see LOG_... definition)
  309. */
  310. // Those variables will be deprecated later, when the transition to ...Get('my_setting') will be done
  311. protected $m_bLogGlobal;
  312. protected $m_bLogNotification;
  313. protected $m_bLogIssue;
  314. protected $m_bLogWebService;
  315. protected $m_bLogKpiDuration; // private setting
  316. protected $m_bLogKpiMemory; // private setting
  317. protected $m_bDebugQueries; // private setting
  318. protected $m_bQueryCacheEnabled; // private setting
  319. /**
  320. * @var integer Number of elements to be displayed when there are more than m_iMaxDisplayLimit elements
  321. */
  322. protected $m_iMinDisplayLimit;
  323. /**
  324. * @var integer Max number of elements before truncating the display
  325. */
  326. protected $m_iMaxDisplayLimit;
  327. /**
  328. * @var integer Number of seconds between two reloads of the display (standard)
  329. */
  330. protected $m_iStandardReloadInterval;
  331. /**
  332. * @var integer Number of seconds between two reloads of the display (fast)
  333. */
  334. protected $m_iFastReloadInterval;
  335. /**
  336. * @var boolean Whether or not a secure connection is required for using the application.
  337. * If set, any attempt to connect to an iTop page with http:// will be redirected
  338. * to https://
  339. */
  340. protected $m_bSecureConnectionRequired;
  341. /**
  342. * @var boolean Forces iTop to output hyperlinks starting with https:// even
  343. * if the current page is not using https. This can be useful when
  344. * the application runs behind a SSL gateway
  345. */
  346. protected $m_bHttpsHyperlinks;
  347. /**
  348. * @var string Langage code, default if the user language is undefined
  349. */
  350. protected $m_sDefaultLanguage;
  351. /**
  352. * @var string Type of login process allowed: form|basic|url|external
  353. */
  354. protected $m_sAllowedLoginTypes;
  355. /**
  356. * @var string Name of the PHP variable in which external authentication information is passed by the web server
  357. */
  358. protected $m_sExtAuthVariable;
  359. /**
  360. * @var string Encryption key used for all attributes of type "encrypted string". Can be set to a random value
  361. * unless you want to import a database from another iTop instance, in which case you must use
  362. * the same encryption key in order to properly decode the encrypted fields
  363. */
  364. protected $m_sEncryptionKey;
  365. /**
  366. * @var array Additional character sets to be supported by the interactive CSV import
  367. * 'iconv_code' => 'display name'
  368. */
  369. protected $m_aCharsets;
  370. public function __construct($sConfigFile, $bLoadConfig = true)
  371. {
  372. $this->m_sFile = $sConfigFile;
  373. $this->m_aAppModules = array(
  374. // Some default modules, always present can be move to an official iTop Module later if needed
  375. 'application/transaction.class.inc.php',
  376. 'application/menunode.class.inc.php',
  377. 'application/user.preferences.class.inc.php',
  378. 'application/audit.rule.class.inc.php',
  379. // Romain - That's dirty, because those classes are in fact part of the core
  380. // but I needed those classes to be derived from cmdbAbstractObject
  381. // (to be managed via the GUI) and this class in not really known from
  382. // the core, PLUS I needed the includes to be there also for the setup
  383. // to create the tables.
  384. 'core/event.class.inc.php',
  385. 'core/action.class.inc.php',
  386. 'core/trigger.class.inc.php',
  387. 'synchro/synchrodatasource.class.inc.php',
  388. );
  389. $this->m_aDataModels = array();
  390. $this->m_aWebServiceCategories = array(
  391. 'webservices/webservices.basic.php',
  392. );
  393. $this->m_aAddons = array(
  394. // Default AddOn, always present can be moved to an official iTop Module later if needed
  395. 'user rights' => 'addons/userrights/userrightsprofile.class.inc.php',
  396. );
  397. $this->m_aDictionaries = array(
  398. // Default dictionaries, always present can be moved to an official iTop Module later if needed
  399. 'dictionaries/dictionary.itop.core.php',
  400. 'dictionaries/dictionary.itop.ui.php', // Support for English
  401. 'dictionaries/fr.dictionary.itop.ui.php', // Support for French
  402. 'dictionaries/fr.dictionary.itop.core.php', // Support for French
  403. 'dictionaries/es_cr.dictionary.itop.ui.php', // Support for Spanish (from Costa Rica)
  404. 'dictionaries/es_cr.dictionary.itop.core.php', // Support for Spanish (from Costa Rica)
  405. 'dictionaries/de.dictionary.itop.ui.php', // Support for German
  406. 'dictionaries/de.dictionary.itop.core.php', // Support for German
  407. 'dictionaries/pt_br.dictionary.itop.ui.php', // Support for Brazilian Portuguese
  408. 'dictionaries/pt_br.dictionary.itop.core.php', // Support for Brazilian Portuguese
  409. 'dictionaries/ru.dictionary.itop.ui.php', // Support for Russian
  410. 'dictionaries/ru.dictionary.itop.core.php', // Support for Russian
  411. 'dictionaries/tr.dictionary.itop.ui.php', // Support for Turkish
  412. 'dictionaries/tr.dictionary.itop.core.php', // Support for Turkish
  413. 'dictionaries/zh.dictionary.itop.ui.php', // Support for Chinese
  414. 'dictionaries/zh.dictionary.itop.core.php', // Support for Chinese
  415. );
  416. foreach($this->m_aSettings as $sPropCode => $aSettingInfo)
  417. {
  418. $this->m_aSettings[$sPropCode]['value'] = $aSettingInfo['default'];
  419. }
  420. $this->m_sDBHost = '';
  421. $this->m_sDBUser = '';
  422. $this->m_sDBPwd = '';
  423. $this->m_sDBName = '';
  424. $this->m_sDBSubname = '';
  425. $this->m_sDBCharacterSet = DEFAULT_CHARACTER_SET;
  426. $this->m_sDBCollation = DEFAULT_COLLATION;
  427. $this->m_bLogGlobal = DEFAULT_LOG_GLOBAL;
  428. $this->m_bLogNotification = DEFAULT_LOG_NOTIFICATION;
  429. $this->m_bLogIssue = DEFAULT_LOG_ISSUE;
  430. $this->m_bLogWebService = DEFAULT_LOG_WEB_SERVICE;
  431. $this->m_bLogKPIDuration = DEFAULT_LOG_KPI_DURATION;
  432. $this->m_bLogKPIDuration = DEFAULT_LOG_KPI_DURATION;
  433. $this->m_iMinDisplayLimit = DEFAULT_MIN_DISPLAY_LIMIT;
  434. $this->m_iMaxDisplayLimit = DEFAULT_MAX_DISPLAY_LIMIT;
  435. $this->m_iStandardReloadInterval = DEFAULT_STANDARD_RELOAD_INTERVAL;
  436. $this->m_iFastReloadInterval = DEFAULT_FAST_RELOAD_INTERVAL;
  437. $this->m_bSecureConnectionRequired = DEFAULT_SECURE_CONNECTION_REQUIRED;
  438. $this->m_bHttpsHyperlinks = DEFAULT_HTTPS_HYPERLINKS;
  439. $this->m_sDefaultLanguage = 'EN US';
  440. $this->m_sAllowedLoginTypes = DEFAULT_ALLOWED_LOGIN_TYPES;
  441. $this->m_sExtAuthVariable = DEFAULT_EXT_AUTH_VARIABLE;
  442. $this->m_sEncryptionKey = DEFAULT_ENCRYPTION_KEY;
  443. $this->m_aCharsets = array();
  444. $this->m_aModuleSettings = array();
  445. if ($bLoadConfig)
  446. {
  447. $this->Load($sConfigFile);
  448. $this->Verify();
  449. }
  450. }
  451. protected function CheckFile($sPurpose, $sFileName)
  452. {
  453. if (!file_exists($sFileName))
  454. {
  455. throw new ConfigException("Could not find $sPurpose file", array('file' => $sFileName));
  456. }
  457. }
  458. protected function Load($sConfigFile)
  459. {
  460. $this->CheckFile('configuration', $sConfigFile);
  461. $sConfigCode = trim(file_get_contents($sConfigFile));
  462. // This does not work on several lines
  463. // preg_match('/^<\\?php(.*)\\?'.'>$/', $sConfigCode, $aMatches)...
  464. // So, I've implemented a solution suggested in the PHP doc (search for phpWrapper)
  465. try
  466. {
  467. ob_start();
  468. eval('?'.'>'.trim($sConfigCode));
  469. $sNoise = trim(ob_get_contents());
  470. ob_end_clean();
  471. }
  472. catch (Exception $e)
  473. {
  474. // well, never reach in case of parsing error :-(
  475. // will be improved in PHP 6 ?
  476. throw new ConfigException('Error in configuration file', array('file' => $sConfigFile, 'error' => $e->getMessage()));
  477. }
  478. if (strlen($sNoise) > 0)
  479. {
  480. // Note: sNoise is an html output, but so far it was ok for me (e.g. showing the entire call stack)
  481. throw new ConfigException('Syntax error in configuration file', array('file' => $sConfigFile, 'error' => '<tt>'.htmlentities($sNoise, ENT_QUOTES, 'UTF-8').'</tt>'));
  482. }
  483. if (!isset($MySettings) || !is_array($MySettings))
  484. {
  485. throw new ConfigException('Missing array in configuration file', array('file' => $sConfigFile, 'expected' => '$MySettings'));
  486. }
  487. if (!isset($MyModules) || !is_array($MyModules))
  488. {
  489. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules'));
  490. }
  491. if (!array_key_exists('application', $MyModules))
  492. {
  493. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'application\']'));
  494. }
  495. if (!array_key_exists('business', $MyModules))
  496. {
  497. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'business\']'));
  498. }
  499. if (!array_key_exists('addons', $MyModules))
  500. {
  501. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'addons\']'));
  502. }
  503. if (!array_key_exists('user rights', $MyModules['addons']))
  504. {
  505. // Add one, by default
  506. $MyModules['addons']['user rights'] = '/addons/userrights/userrightsnull.class.inc.php';
  507. }
  508. if (!array_key_exists('dictionaries', $MyModules))
  509. {
  510. throw new ConfigException('Missing item in configuration file', array('file' => $sConfigFile, 'expected' => '$MyModules[\'dictionaries\']'));
  511. }
  512. $this->m_aAppModules = $MyModules['application'];
  513. $this->m_aDataModels = $MyModules['business'];
  514. if (isset($MyModules['webservices']))
  515. {
  516. $this->m_aWebServiceCategories = $MyModules['webservices'];
  517. }
  518. $this->m_aAddons = $MyModules['addons'];
  519. $this->m_aDictionaries = $MyModules['dictionaries'];
  520. foreach($MySettings as $sPropCode => $rawvalue)
  521. {
  522. if ($this->IsProperty($sPropCode))
  523. {
  524. $value = trim($rawvalue);
  525. $this->Set($sPropCode, $value, $sConfigFile);
  526. }
  527. }
  528. $this->m_sDBHost = trim($MySettings['db_host']);
  529. $this->m_sDBUser = trim($MySettings['db_user']);
  530. $this->m_sDBPwd = trim($MySettings['db_pwd']);
  531. $this->m_sDBName = trim($MySettings['db_name']);
  532. $this->m_sDBSubname = trim($MySettings['db_subname']);
  533. $this->m_sDBCharacterSet = isset($MySettings['db_character_set']) ? trim($MySettings['db_character_set']) : DEFAULT_CHARACTER_SET;
  534. $this->m_sDBCollation = isset($MySettings['db_collation']) ? trim($MySettings['db_collation']) : DEFAULT_COLLATION;
  535. $this->m_bLogGlobal = isset($MySettings['log_global']) ? (bool) trim($MySettings['log_global']) : DEFAULT_LOG_GLOBAL;
  536. $this->m_bLogNotification = isset($MySettings['log_notification']) ? (bool) trim($MySettings['log_notification']) : DEFAULT_LOG_NOTIFICATION;
  537. $this->m_bLogIssue = isset($MySettings['log_issue']) ? (bool) trim($MySettings['log_issue']) : DEFAULT_LOG_ISSUE;
  538. $this->m_bLogWebService = isset($MySettings['log_web_service']) ? (bool) trim($MySettings['log_web_service']) : DEFAULT_LOG_WEB_SERVICE;
  539. $this->m_bLogKPIDuration = isset($MySettings['log_kpi_duration']) ? (bool) trim($MySettings['log_kpi_duration']) : DEFAULT_LOG_KPI_DURATION;
  540. $this->m_bLogKPIMemory = isset($MySettings['log_kpi_memory']) ? (bool) trim($MySettings['log_kpi_memory']) : DEFAULT_LOG_KPI_MEMORY;
  541. $this->m_bDebugQueries = isset($MySettings['debug_queries']) ? (bool) trim($MySettings['debug_queries']) : DEFAULT_DEBUG_QUERIES;
  542. $this->m_bQueryCacheEnabled = isset($MySettings['query_cache_enabled']) ? (bool) trim($MySettings['query_cache_enabled']) : DEFAULT_QUERY_CACHE_ENABLED;
  543. $this->m_iMinDisplayLimit = isset($MySettings['min_display_limit']) ? trim($MySettings['min_display_limit']) : DEFAULT_MIN_DISPLAY_LIMIT;
  544. $this->m_iMaxDisplayLimit = isset($MySettings['max_display_limit']) ? trim($MySettings['max_display_limit']) : DEFAULT_MAX_DISPLAY_LIMIT;
  545. $this->m_iStandardReloadInterval = isset($MySettings['standard_reload_interval']) ? trim($MySettings['standard_reload_interval']) : DEFAULT_STANDARD_RELOAD_INTERVAL;
  546. $this->m_iFastReloadInterval = isset($MySettings['fast_reload_interval']) ? trim($MySettings['fast_reload_interval']) : DEFAULT_FAST_RELOAD_INTERVAL;
  547. $this->m_bSecureConnectionRequired = isset($MySettings['secure_connection_required']) ? (bool) trim($MySettings['secure_connection_required']) : DEFAULT_SECURE_CONNECTION_REQUIRED;
  548. $this->m_bHttpsHyperlinks = isset($MySettings['https_hyperlinks']) ? (bool) trim($MySettings['https_hyperlinks']) : DEFAULT_HTTPS_HYPERLINKS;
  549. $this->m_aModuleSettings = isset($MyModuleSettings) ? $MyModuleSettings : array();
  550. $this->m_sDefaultLanguage = isset($MySettings['default_language']) ? trim($MySettings['default_language']) : 'EN US';
  551. $this->m_sAllowedLoginTypes = isset($MySettings['allowed_login_types']) ? trim($MySettings['allowed_login_types']) : DEFAULT_ALLOWED_LOGIN_TYPES;
  552. $this->m_sExtAuthVariable = isset($MySettings['ext_auth_variable']) ? trim($MySettings['ext_auth_variable']) : DEFAULT_EXT_AUTH_VARIABLE;
  553. $this->m_sEncryptionKey = isset($MySettings['encryption_key']) ? trim($MySettings['encryption_key']) : DEFAULT_ENCRYPTION_KEY;
  554. $this->m_aCharsets = isset($MySettings['csv_import_charsets']) ? $MySettings['csv_import_charsets'] : array();
  555. }
  556. protected function Verify()
  557. {
  558. // Files are verified later on, just before using them -see MetaModel::Plugin()
  559. // (we have their final path at that point)
  560. }
  561. public function GetModuleSetting($sModule, $sProperty, $defaultvalue = null)
  562. {
  563. if (isset($this->m_aModuleSettings[$sModule][$sProperty]))
  564. {
  565. return $this->m_aModuleSettings[$sModule][$sProperty];
  566. }
  567. return $defaultvalue;
  568. }
  569. public function SetModuleSetting($sModule, $sProperty, $value)
  570. {
  571. $this->m_aModuleSettings[$sModule][$sProperty] = $value;
  572. }
  573. public function GetAppModules()
  574. {
  575. return $this->m_aAppModules;
  576. }
  577. public function SetAppModules($aAppModules)
  578. {
  579. $this->m_aAppModules = $aAppModules;
  580. }
  581. public function GetDataModels()
  582. {
  583. return $this->m_aDataModels;
  584. }
  585. public function SetDataModels($aDataModels)
  586. {
  587. $this->m_aDataModels = $aDataModels;
  588. }
  589. public function GetWebServiceCategories()
  590. {
  591. return $this->m_aWebServiceCategories;
  592. }
  593. public function SetWebServiceCategories($aWebServiceCategories)
  594. {
  595. $this->m_aWebServiceCategories = $aWebServiceCategories;
  596. }
  597. public function GetAddons()
  598. {
  599. return $this->m_aAddons;
  600. }
  601. public function SetAddons($aAddons)
  602. {
  603. $this->m_aAddons = $aAddons;
  604. }
  605. public function GetDictionaries()
  606. {
  607. return $this->m_aDictionaries;
  608. }
  609. public function SetDictionaries($aDictionaries)
  610. {
  611. $this->m_aDictionaries = $aDictionaries;
  612. }
  613. public function GetDBHost()
  614. {
  615. return $this->m_sDBHost;
  616. }
  617. public function GetDBName()
  618. {
  619. return $this->m_sDBName;
  620. }
  621. public function GetDBSubname()
  622. {
  623. return $this->m_sDBSubname;
  624. }
  625. public function GetDBCharacterSet()
  626. {
  627. return $this->m_sDBCharacterSet;
  628. }
  629. public function GetDBCollation()
  630. {
  631. return $this->m_sDBCollation;
  632. }
  633. public function GetDBUser()
  634. {
  635. return $this->m_sDBUser;
  636. }
  637. public function GetDBPwd()
  638. {
  639. return $this->m_sDBPwd;
  640. }
  641. public function GetLogGlobal()
  642. {
  643. return $this->m_bLogGlobal;
  644. }
  645. public function GetLogNotification()
  646. {
  647. return $this->m_bLogNotification;
  648. }
  649. public function GetLogIssue()
  650. {
  651. return $this->m_bLogIssue;
  652. }
  653. public function GetLogWebService()
  654. {
  655. return $this->m_bLogWebService;
  656. }
  657. public function GetLogKPIDuration()
  658. {
  659. return $this->m_bLogKPIDuration;
  660. }
  661. public function GetLogKPIMemory()
  662. {
  663. return $this->m_bLogKPIMemory;
  664. }
  665. public function GetDebugQueries()
  666. {
  667. return $this->m_bDebugQueries;
  668. }
  669. public function GetQueryCacheEnabled()
  670. {
  671. return $this->m_bQueryCacheEnabled;
  672. }
  673. public function GetMinDisplayLimit()
  674. {
  675. return $this->m_iMinDisplayLimit;
  676. }
  677. public function GetMaxDisplayLimit()
  678. {
  679. return $this->m_iMaxDisplayLimit;
  680. }
  681. public function GetStandardReloadInterval()
  682. {
  683. return $this->m_iStandardReloadInterval;
  684. }
  685. public function GetFastReloadInterval()
  686. {
  687. return $this->m_iFastReloadInterval;
  688. }
  689. public function GetSecureConnectionRequired()
  690. {
  691. return $this->m_bSecureConnectionRequired;
  692. }
  693. public function GetHttpsHyperlinks()
  694. {
  695. return $this->m_bHttpsHyperlinks;
  696. }
  697. public function GetDefaultLanguage()
  698. {
  699. return $this->m_sDefaultLanguage;
  700. }
  701. public function GetEncryptionKey()
  702. {
  703. return $this->m_sEncryptionKey;
  704. }
  705. public function GetAllowedLoginTypes()
  706. {
  707. return explode('|', $this->m_sAllowedLoginTypes);
  708. }
  709. public function GetExternalAuthenticationVariable()
  710. {
  711. return $this->m_sExtAuthVariable;
  712. }
  713. public function GetCSVImportCharsets()
  714. {
  715. return $this->m_aCharsets;
  716. }
  717. public function SetDBHost($sDBHost)
  718. {
  719. $this->m_sDBHost = $sDBHost;
  720. }
  721. public function SetDBName($sDBName)
  722. {
  723. $this->m_sDBName = $sDBName;
  724. }
  725. public function SetDBSubname($sDBSubName)
  726. {
  727. $this->m_sDBSubname = $sDBSubName;
  728. }
  729. public function SetDBCharacterSet($sDBCharacterSet)
  730. {
  731. $this->m_sDBCharacterSet = $sDBCharacterSet;
  732. }
  733. public function SetDBCollation($sDBCollation)
  734. {
  735. $this->m_sDBCollation = $sDBCollation;
  736. }
  737. public function SetDBUser($sUser)
  738. {
  739. $this->m_sDBUser = $sUser;
  740. }
  741. public function SetDBPwd($sPwd)
  742. {
  743. $this->m_sDBPwd = $sPwd;
  744. }
  745. public function SetLogGlobal($iLogGlobal)
  746. {
  747. $this->m_iLogGlobal = $iLogGlobal;
  748. }
  749. public function SetLogNotification($iLogNotification)
  750. {
  751. $this->m_iLogNotification = $iLogNotification;
  752. }
  753. public function SetLogIssue($iLogIssue)
  754. {
  755. $this->m_iLogIssue = $iLogIssue;
  756. }
  757. public function SetLogWebService($iLogWebService)
  758. {
  759. $this->m_iLogWebService = $iLogWebService;
  760. }
  761. public function SetMinDisplayLimit($iMinDisplayLimit)
  762. {
  763. $this->m_iMinDisplayLimit = $iMinDisplayLimit;
  764. }
  765. public function SetMaxDisplayLimit($iMaxDisplayLimit)
  766. {
  767. $this->m_iMaxDisplayLimit = $iMaxDisplayLimit;
  768. }
  769. public function SetStandardReloadInterval($iStandardReloadInterval)
  770. {
  771. $this->m_iStandardReloadInterval = $iStandardReloadInterval;
  772. }
  773. public function SetFastReloadInterval($iFastReloadInterval)
  774. {
  775. $this->m_iFastReloadInterval = $iFastReloadInterval;
  776. }
  777. public function SetSecureConnectionRequired($bSecureConnectionRequired)
  778. {
  779. $this->m_bSecureConnectionRequired = $bSecureConnectionRequired;
  780. }
  781. public function SetHttpsHyperlinks($bHttpsHyperlinks)
  782. {
  783. $this->m_bHttpsHyperlinks = $bHttpsHyperlinks;
  784. }
  785. public function SetDefaultLanguage($sLanguageCode)
  786. {
  787. $this->m_sDefaultLanguage = $sLanguageCode;
  788. }
  789. public function SetAllowedLoginTypes($aAllowedLoginTypes)
  790. {
  791. $this->m_sAllowedLoginTypes = implode('|', $aAllowedLoginTypes);
  792. }
  793. public function SetExternalAuthenticationVariable($sExtAuthVariable)
  794. {
  795. $this->m_sExtAuthVariable = $sExtAuthVariable;
  796. }
  797. public function SetEncryptionKey($sKey)
  798. {
  799. $this->m_sEncryptionKey = $sKey;
  800. }
  801. public function SetCSVImportCharsets($aCharsets)
  802. {
  803. $this->m_aCharsets = $aCharsets;
  804. }
  805. public function AddCSVImportCharset($sIconvCode, $sDisplayName)
  806. {
  807. $this->m_aCharsets[$sIconvCode] = $sDisplayName;
  808. }
  809. public function FileIsWritable()
  810. {
  811. return is_writable($this->m_sFile);
  812. }
  813. public function GetLoadedFile()
  814. {
  815. return $this->m_sFile;
  816. }
  817. /**
  818. * Render the configuration as an associative array
  819. * @return boolean True otherwise throws an Exception
  820. */
  821. public function ToArray()
  822. {
  823. $aSettings = array();
  824. foreach($this->m_aSettings as $sPropCode => $aSettingInfo)
  825. {
  826. $aSettings[$sPropCode] = $aSettingInfo['value'];
  827. }
  828. $aSettings['db_host'] = $this->m_sDBHost;
  829. $aSettings['db_user'] = $this->m_sDBUser;
  830. $aSettings['db_pwd'] = $this->m_sDBPwd;
  831. $aSettings['db_name'] = $this->m_sDBName;
  832. $aSettings['db_subname'] = $this->m_sDBSubname;
  833. $aSettings['db_character_set'] = $this->m_sDBCharacterSet;
  834. $aSettings['db_collation'] = $this->m_sDBCollation;
  835. $aSettings['log_global'] = $this->m_bLogGlobal;
  836. $aSettings['log_notification'] = $this->m_bLogNotification;
  837. $aSettings['log_issue'] = $this->m_bLogIssue;
  838. $aSettings['log_web_service'] = $this->m_bLogWebService;
  839. $aSettings['min_display_limit'] = $this->m_iMinDisplayLimit;
  840. $aSettings['max_display_limit'] = $this->m_iMaxDisplayLimit;
  841. $aSettings['standard_reload_interval'] = $this->m_iStandardReloadInterval;
  842. $aSettings['fast_reload_interval'] = $this->m_iFastReloadInterval;
  843. $aSettings['secure_connection_required'] = $this->m_bSecureConnectionRequired;
  844. $aSettings['https_hyperlinks'] = $this->m_bHttpsHyperlinks;
  845. $aSettings['default_language'] = $this->m_sDefaultLanguage;
  846. $aSettings['allowed_login_types'] = $this->m_sAllowedLoginTypes;
  847. $aSettings['encryption_key'] = $this->m_sEncryptionKey;
  848. $aSettings['csv_import_charsets'] = $this->m_aCharsets;
  849. foreach ($this->m_aModuleSettings as $sModule => $aProperties)
  850. {
  851. foreach ($aProperties as $sProperty => $value)
  852. {
  853. $aSettings['module_settings'][$sModule][$sProperty] = $value;
  854. }
  855. }
  856. foreach($this->m_aAppModules as $sFile)
  857. {
  858. $aSettings['application_list'][] = $sFile;
  859. }
  860. foreach($this->m_aDataModels as $sFile)
  861. {
  862. $aSettings['datamodel_list'][] = $sFile;
  863. }
  864. foreach($this->m_aWebServiceCategories as $sFile)
  865. {
  866. $aSettings['webservice_list'][] = $sFile;
  867. }
  868. foreach($this->m_aAddons as $sKey => $sFile)
  869. {
  870. $aSettings['addon_list'][] = $sFile;
  871. }
  872. foreach($this->m_aDictionaries as $sFile)
  873. {
  874. $aSettings['dictionary_list'][] = $sFile;
  875. }
  876. return $aSettings;
  877. }
  878. /**
  879. * Write the configuration to a file (php format) that can be reloaded later
  880. * By default write to the same file that was specified when constructing the object
  881. * @param $sFileName string Name of the file to write to (emtpy to write to the same file)
  882. * @return boolean True otherwise throws an Exception
  883. */
  884. public function WriteToFile($sFileName = '')
  885. {
  886. if (empty($sFileName))
  887. {
  888. $sFileName = $this->m_sFile;
  889. }
  890. $hFile = @fopen($sFileName, 'w');
  891. if ($hFile !== false)
  892. {
  893. fwrite($hFile, "<?php\n");
  894. fwrite($hFile, "\n/**\n");
  895. fwrite($hFile, " *\n");
  896. fwrite($hFile, " * phpMyORM configuration file, generated by the iTop configuration wizard\n");
  897. fwrite($hFile, " *\n");
  898. fwrite($hFile, " * The file is used in MetaModel::LoadConfig() which does all the necessary initialization job\n");
  899. fwrite($hFile, " *\n");
  900. fwrite($hFile, " */\n");
  901. fwrite($hFile, "\n");
  902. fwrite($hFile, "\$MySettings = array(\n");
  903. foreach($this->m_aSettings as $sPropCode => $aSettingInfo)
  904. {
  905. if ($aSettingInfo['show_in_conf_sample'])
  906. {
  907. $sType = $this->m_aSettings[$sPropCode]['type'];
  908. switch($sType)
  909. {
  910. case 'bool':
  911. $sSeenAs = $aSettingInfo['value'] ? '1' : '0';
  912. break;
  913. default:
  914. $sSeenAs = "'".addslashes($aSettingInfo['value'])."'";
  915. }
  916. fwrite($hFile, "\t'$sPropCode' => $sSeenAs,\n");
  917. }
  918. }
  919. fwrite($hFile, "\t'db_host' => '{$this->m_sDBHost}',\n");
  920. fwrite($hFile, "\t'db_user' => '{$this->m_sDBUser}',\n");
  921. fwrite($hFile, "\t'db_pwd' => '".addslashes($this->m_sDBPwd)."',\n");
  922. fwrite($hFile, "\t'db_name' => '{$this->m_sDBName}',\n");
  923. fwrite($hFile, "\t'db_subname' => '{$this->m_sDBSubname}',\n");
  924. fwrite($hFile, "\t'db_character_set' => '{$this->m_sDBCharacterSet}',\n");
  925. fwrite($hFile, "\t'db_collation' => '{$this->m_sDBCollation}',\n");
  926. fwrite($hFile, "\n");
  927. fwrite($hFile, "\t'log_global' => {$this->m_bLogGlobal},\n");
  928. fwrite($hFile, "\t'log_notification' => {$this->m_bLogNotification},\n");
  929. fwrite($hFile, "\t'log_issue' => {$this->m_bLogIssue},\n");
  930. fwrite($hFile, "\t'log_web_service' => {$this->m_bLogWebService},\n");
  931. fwrite($hFile, "\t'min_display_limit' => {$this->m_iMinDisplayLimit},\n");
  932. fwrite($hFile, "\t'max_display_limit' => {$this->m_iMaxDisplayLimit},\n");
  933. fwrite($hFile, "\t'standard_reload_interval' => {$this->m_iStandardReloadInterval},\n");
  934. fwrite($hFile, "\t'fast_reload_interval' => {$this->m_iFastReloadInterval},\n");
  935. fwrite($hFile, "\t'secure_connection_required' => ".($this->m_bSecureConnectionRequired ? 'true' : 'false').",\n");
  936. fwrite($hFile, "\t'https_hyperlinks' => ".($this->m_bHttpsHyperlinks ? 'true' : 'false').",\n");
  937. fwrite($hFile, "\t'default_language' => '{$this->m_sDefaultLanguage}',\n");
  938. fwrite($hFile, "\t'allowed_login_types' => '{$this->m_sAllowedLoginTypes}',\n");
  939. fwrite($hFile, "\t'encryption_key' => '{$this->m_sEncryptionKey}',\n");
  940. $sExport = var_export($this->m_aCharsets, true);
  941. fwrite($hFile, "\t'csv_import_charsets' => $sExport,\n");
  942. fwrite($hFile, ");\n");
  943. fwrite($hFile, "\n");
  944. fwrite($hFile, "\$MyModuleSettings = array(\n");
  945. foreach ($this->m_aModuleSettings as $sModule => $aProperties)
  946. {
  947. fwrite($hFile, "\t'$sModule' => array (\n");
  948. foreach ($aProperties as $sProperty => $value)
  949. {
  950. $sExport = var_export($value, true);
  951. fwrite($hFile, "\t\t'$sProperty' => $sExport,\n");
  952. }
  953. fwrite($hFile, "\t),\n");
  954. }
  955. fwrite($hFile, ");\n");
  956. fwrite($hFile, "\n/**\n");
  957. fwrite($hFile, " *\n");
  958. fwrite($hFile, " * Data model modules to be loaded. Names should be specified as absolute paths\n");
  959. fwrite($hFile, " *\n");
  960. fwrite($hFile, " */\n");
  961. fwrite($hFile, "\$MyModules = array(\n");
  962. fwrite($hFile, "\t'application' => array (\n");
  963. foreach($this->m_aAppModules as $sFile)
  964. {
  965. fwrite($hFile, "\t\t'$sFile',\n");
  966. }
  967. fwrite($hFile, "\t),\n");
  968. fwrite($hFile, "\t'business' => array (\n");
  969. foreach($this->m_aDataModels as $sFile)
  970. {
  971. fwrite($hFile, "\t\t'$sFile',\n");
  972. }
  973. fwrite($hFile, "\t),\n");
  974. fwrite($hFile, "\t'webservices' => array (\n");
  975. foreach($this->m_aWebServiceCategories as $sFile)
  976. {
  977. fwrite($hFile, "\t\t'$sFile',\n");
  978. }
  979. fwrite($hFile, "\t),\n");
  980. fwrite($hFile, "\t'addons' => array (\n");
  981. foreach($this->m_aAddons as $sKey => $sFile)
  982. {
  983. fwrite($hFile, "\t\t'$sKey' => '$sFile',\n");
  984. }
  985. fwrite($hFile, "\t),\n");
  986. fwrite($hFile, "\t'dictionaries' => array (\n");
  987. foreach($this->m_aDictionaries as $sFile)
  988. {
  989. fwrite($hFile, "\t\t'$sFile',\n");
  990. }
  991. fwrite($hFile, "\t),\n");
  992. fwrite($hFile, ");\n");
  993. fwrite($hFile, '?'.'>'); // Avoid perturbing the syntax highlighting !
  994. return fclose($hFile);
  995. }
  996. else
  997. {
  998. throw new ConfigException("Could not write to configuration file", array('file' => $sFileName));
  999. }
  1000. }
  1001. }
  1002. ?>