Config.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at the following url: |
  11. // | http://www.php.net/license/3_0.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | [email protected] so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <[email protected]> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Config.php,v 1.52 2004/01/08 17:33:12 sniper Exp $
  20. require_once 'PEAR.php';
  21. require_once 'System.php';
  22. /**
  23. * Last created PEAR_Config instance.
  24. * @var object
  25. */
  26. $GLOBALS['_PEAR_Config_instance'] = null;
  27. if (!defined('PEAR_INSTALL_DIR') || !PEAR_INSTALL_DIR) {
  28. $PEAR_INSTALL_DIR = PHP_LIBDIR . DIRECTORY_SEPARATOR . 'pear';
  29. } else {
  30. $PEAR_INSTALL_DIR = PEAR_INSTALL_DIR;
  31. }
  32. // Below we define constants with default values for all configuration
  33. // parameters except username/password. All of them can have their
  34. // defaults set through environment variables. The reason we use the
  35. // PHP_ prefix is for some security, PHP protects environment
  36. // variables starting with PHP_*.
  37. if (getenv('PHP_PEAR_SYSCONF_DIR')) {
  38. define('PEAR_CONFIG_SYSCONFDIR', getenv('PHP_PEAR_SYSCONF_DIR'));
  39. } elseif (getenv('SystemRoot')) {
  40. define('PEAR_CONFIG_SYSCONFDIR', getenv('SystemRoot'));
  41. } else {
  42. define('PEAR_CONFIG_SYSCONFDIR', PHP_SYSCONFDIR);
  43. }
  44. // Default for master_server
  45. if (getenv('PHP_PEAR_MASTER_SERVER')) {
  46. define('PEAR_CONFIG_DEFAULT_MASTER_SERVER', getenv('PHP_PEAR_MASTER_SERVER'));
  47. } else {
  48. define('PEAR_CONFIG_DEFAULT_MASTER_SERVER', 'pear.php.net');
  49. }
  50. // Default for http_proxy
  51. if (getenv('PHP_PEAR_HTTP_PROXY')) {
  52. define('PEAR_CONFIG_DEFAULT_HTTP_PROXY', getenv('PHP_PEAR_HTTP_PROXY'));
  53. } elseif (getenv('http_proxy')) {
  54. define('PEAR_CONFIG_DEFAULT_HTTP_PROXY', getenv('http_proxy'));
  55. } else {
  56. define('PEAR_CONFIG_DEFAULT_HTTP_PROXY', '');
  57. }
  58. // Default for php_dir
  59. if (getenv('PHP_PEAR_INSTALL_DIR')) {
  60. define('PEAR_CONFIG_DEFAULT_PHP_DIR', getenv('PHP_PEAR_INSTALL_DIR'));
  61. } else {
  62. if (@is_dir($PEAR_INSTALL_DIR)) {
  63. define('PEAR_CONFIG_DEFAULT_PHP_DIR',
  64. $PEAR_INSTALL_DIR);
  65. } else {
  66. define('PEAR_CONFIG_DEFAULT_PHP_DIR', $PEAR_INSTALL_DIR);
  67. }
  68. }
  69. // Default for ext_dir
  70. if (getenv('PHP_PEAR_EXTENSION_DIR')) {
  71. define('PEAR_CONFIG_DEFAULT_EXT_DIR', getenv('PHP_PEAR_EXTENSION_DIR'));
  72. } else {
  73. if (ini_get('extension_dir')) {
  74. define('PEAR_CONFIG_DEFAULT_EXT_DIR', ini_get('extension_dir'));
  75. } elseif (defined('PEAR_EXTENSION_DIR') && @is_dir(PEAR_EXTENSION_DIR)) {
  76. define('PEAR_CONFIG_DEFAULT_EXT_DIR', PEAR_EXTENSION_DIR);
  77. } elseif (defined('PHP_EXTENSION_DIR')) {
  78. define('PEAR_CONFIG_DEFAULT_EXT_DIR', PHP_EXTENSION_DIR);
  79. } else {
  80. define('PEAR_CONFIG_DEFAULT_EXT_DIR', '.');
  81. }
  82. }
  83. // Default for doc_dir
  84. if (getenv('PHP_PEAR_DOC_DIR')) {
  85. define('PEAR_CONFIG_DEFAULT_DOC_DIR', getenv('PHP_PEAR_DOC_DIR'));
  86. } else {
  87. define('PEAR_CONFIG_DEFAULT_DOC_DIR',
  88. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'docs');
  89. }
  90. // Default for bin_dir
  91. if (getenv('PHP_PEAR_BIN_DIR')) {
  92. define('PEAR_CONFIG_DEFAULT_BIN_DIR', getenv('PHP_PEAR_BIN_DIR'));
  93. } else {
  94. define('PEAR_CONFIG_DEFAULT_BIN_DIR', PHP_BINDIR);
  95. }
  96. // Default for data_dir
  97. if (getenv('PHP_PEAR_DATA_DIR')) {
  98. define('PEAR_CONFIG_DEFAULT_DATA_DIR', getenv('PHP_PEAR_DATA_DIR'));
  99. } else {
  100. define('PEAR_CONFIG_DEFAULT_DATA_DIR',
  101. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'data');
  102. }
  103. // Default for test_dir
  104. if (getenv('PHP_PEAR_TEST_DIR')) {
  105. define('PEAR_CONFIG_DEFAULT_TEST_DIR', getenv('PHP_PEAR_TEST_DIR'));
  106. } else {
  107. define('PEAR_CONFIG_DEFAULT_TEST_DIR',
  108. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'tests');
  109. }
  110. // Default for cache_dir
  111. if (getenv('PHP_PEAR_CACHE_DIR')) {
  112. define('PEAR_CONFIG_DEFAULT_CACHE_DIR', getenv('PHP_PEAR_CACHE_DIR'));
  113. } else {
  114. define('PEAR_CONFIG_DEFAULT_CACHE_DIR',
  115. System::tmpdir() . DIRECTORY_SEPARATOR . 'pear' .
  116. DIRECTORY_SEPARATOR . 'cache');
  117. }
  118. // Default for php_bin
  119. if (getenv('PHP_PEAR_PHP_BIN')) {
  120. define('PEAR_CONFIG_DEFAULT_PHP_BIN', getenv('PHP_PEAR_PHP_BIN'));
  121. } else {
  122. define('PEAR_CONFIG_DEFAULT_PHP_BIN', PEAR_CONFIG_DEFAULT_BIN_DIR.
  123. DIRECTORY_SEPARATOR.'php'.(OS_WINDOWS ? '.exe' : ''));
  124. }
  125. // Default for verbose
  126. if (getenv('PHP_PEAR_VERBOSE')) {
  127. define('PEAR_CONFIG_DEFAULT_VERBOSE', getenv('PHP_PEAR_VERBOSE'));
  128. } else {
  129. define('PEAR_CONFIG_DEFAULT_VERBOSE', 1);
  130. }
  131. // Default for preferred_state
  132. if (getenv('PHP_PEAR_PREFERRED_STATE')) {
  133. define('PEAR_CONFIG_DEFAULT_PREFERRED_STATE', getenv('PHP_PEAR_PREFERRED_STATE'));
  134. } else {
  135. define('PEAR_CONFIG_DEFAULT_PREFERRED_STATE', 'stable');
  136. }
  137. // Default for umask
  138. if (getenv('PHP_PEAR_UMASK')) {
  139. define('PEAR_CONFIG_DEFAULT_UMASK', getenv('PHP_PEAR_UMASK'));
  140. } else {
  141. define('PEAR_CONFIG_DEFAULT_UMASK', decoct(umask()));
  142. }
  143. // Default for cache_ttl
  144. if (getenv('PHP_PEAR_CACHE_TTL')) {
  145. define('PEAR_CONFIG_DEFAULT_CACHE_TTL', getenv('PHP_PEAR_CACHE_TTL'));
  146. } else {
  147. define('PEAR_CONFIG_DEFAULT_CACHE_TTL', 3600);
  148. }
  149. // Default for sig_type
  150. if (getenv('PHP_PEAR_SIG_TYPE')) {
  151. define('PEAR_CONFIG_DEFAULT_SIG_TYPE', getenv('PHP_PEAR_SIG_TYPE'));
  152. } else {
  153. define('PEAR_CONFIG_DEFAULT_SIG_TYPE', 'gpg');
  154. }
  155. // Default for sig_bin
  156. if (getenv('PHP_PEAR_SIG_BIN')) {
  157. define('PEAR_CONFIG_DEFAULT_SIG_BIN', getenv('PHP_PEAR_SIG_BIN'));
  158. } else {
  159. define('PEAR_CONFIG_DEFAULT_SIG_BIN',
  160. System::which(
  161. 'gpg', OS_WINDOWS ? 'c:\gnupg\gpg.exe' : '/usr/local/bin/gpg'));
  162. }
  163. // Default for sig_keydir
  164. if (getenv('PHP_PEAR_SIG_KEYDIR')) {
  165. define('PEAR_CONFIG_DEFAULT_SIG_KEYDIR', getenv('PHP_PEAR_SIG_KEYDIR'));
  166. } else {
  167. define('PEAR_CONFIG_DEFAULT_SIG_KEYDIR',
  168. PEAR_CONFIG_SYSCONFDIR . DIRECTORY_SEPARATOR . 'pearkeys');
  169. }
  170. /**
  171. * This is a class for storing configuration data, keeping track of
  172. * which are system-defined, user-defined or defaulted.
  173. */
  174. class PEAR_Config extends PEAR
  175. {
  176. // {{{ properties
  177. /**
  178. * Array of config files used.
  179. *
  180. * @var array layer => config file
  181. */
  182. var $files = array(
  183. 'system' => '',
  184. 'user' => '',
  185. );
  186. var $layers = array();
  187. /**
  188. * Configuration data, two-dimensional array where the first
  189. * dimension is the config layer ('user', 'system' and 'default'),
  190. * and the second dimension is keyname => value.
  191. *
  192. * The order in the first dimension is important! Earlier
  193. * layers will shadow later ones when a config value is
  194. * requested (if a 'user' value exists, it will be returned first,
  195. * then 'system' and finally 'default').
  196. *
  197. * @var array layer => array(keyname => value, ...)
  198. */
  199. var $configuration = array(
  200. 'user' => array(),
  201. 'system' => array(),
  202. 'default' => array(),
  203. );
  204. /**
  205. * Information about the configuration data. Stores the type,
  206. * default value and a documentation string for each configuration
  207. * value.
  208. *
  209. * @var array layer => array(infotype => value, ...)
  210. */
  211. var $configuration_info = array(
  212. // Internet Access
  213. 'master_server' => array(
  214. 'type' => 'string',
  215. 'default' => 'pear.php.net',
  216. 'doc' => 'name of the main PEAR server',
  217. 'prompt' => 'PEAR server',
  218. 'group' => 'Internet Access',
  219. ),
  220. 'http_proxy' => array(
  221. 'type' => 'string',
  222. 'default' => PEAR_CONFIG_DEFAULT_HTTP_PROXY,
  223. 'doc' => 'HTTP proxy (host:port) to use when downloading packages',
  224. 'prompt' => 'HTTP Proxy Server Address',
  225. 'group' => 'Internet Access',
  226. ),
  227. // File Locations
  228. 'php_dir' => array(
  229. 'type' => 'directory',
  230. 'default' => PEAR_CONFIG_DEFAULT_PHP_DIR,
  231. 'doc' => 'directory where .php files are installed',
  232. 'prompt' => 'PEAR directory',
  233. 'group' => 'File Locations',
  234. ),
  235. 'ext_dir' => array(
  236. 'type' => 'directory',
  237. 'default' => PEAR_CONFIG_DEFAULT_EXT_DIR,
  238. 'doc' => 'directory where loadable extensions are installed',
  239. 'prompt' => 'PHP extension directory',
  240. 'group' => 'File Locations',
  241. ),
  242. 'doc_dir' => array(
  243. 'type' => 'directory',
  244. 'default' => PEAR_CONFIG_DEFAULT_DOC_DIR,
  245. 'doc' => 'directory where documentation is installed',
  246. 'prompt' => 'PEAR documentation directory',
  247. 'group' => 'File Locations',
  248. ),
  249. 'bin_dir' => array(
  250. 'type' => 'directory',
  251. 'default' => PEAR_CONFIG_DEFAULT_BIN_DIR,
  252. 'doc' => 'directory where executables are installed',
  253. 'prompt' => 'PEAR executables directory',
  254. 'group' => 'File Locations',
  255. ),
  256. 'data_dir' => array(
  257. 'type' => 'directory',
  258. 'default' => PEAR_CONFIG_DEFAULT_DATA_DIR,
  259. 'doc' => 'directory where data files are installed',
  260. 'prompt' => 'PEAR data directory',
  261. 'group' => 'File Locations (Advanced)',
  262. ),
  263. 'test_dir' => array(
  264. 'type' => 'directory',
  265. 'default' => PEAR_CONFIG_DEFAULT_TEST_DIR,
  266. 'doc' => 'directory where regression tests are installed',
  267. 'prompt' => 'PEAR test directory',
  268. 'group' => 'File Locations (Advanced)',
  269. ),
  270. 'cache_dir' => array(
  271. 'type' => 'directory',
  272. 'default' => PEAR_CONFIG_DEFAULT_CACHE_DIR,
  273. 'doc' => 'directory which is used for XMLRPC cache',
  274. 'prompt' => 'PEAR Installer cache directory',
  275. 'group' => 'File Locations (Advanced)',
  276. ),
  277. 'php_bin' => array(
  278. 'type' => 'file',
  279. 'default' => PEAR_CONFIG_DEFAULT_PHP_BIN,
  280. 'doc' => 'PHP CLI/CGI binary for executing scripts',
  281. 'prompt' => 'PHP CLI/CGI binary',
  282. 'group' => 'File Locations (Advanced)',
  283. ),
  284. // Maintainers
  285. 'username' => array(
  286. 'type' => 'string',
  287. 'default' => '',
  288. 'doc' => '(maintainers) your PEAR account name',
  289. 'prompt' => 'PEAR username (for maintainers)',
  290. 'group' => 'Maintainers',
  291. ),
  292. 'password' => array(
  293. 'type' => 'password',
  294. 'default' => '',
  295. 'doc' => '(maintainers) your PEAR account password',
  296. 'prompt' => 'PEAR password (for maintainers)',
  297. 'group' => 'Maintainers',
  298. ),
  299. // Advanced
  300. 'verbose' => array(
  301. 'type' => 'integer',
  302. 'default' => PEAR_CONFIG_DEFAULT_VERBOSE,
  303. 'doc' => 'verbosity level
  304. 0: really quiet
  305. 1: somewhat quiet
  306. 2: verbose
  307. 3: debug',
  308. 'prompt' => 'Debug Log Level',
  309. 'group' => 'Advanced',
  310. ),
  311. 'preferred_state' => array(
  312. 'type' => 'set',
  313. 'default' => PEAR_CONFIG_DEFAULT_PREFERRED_STATE,
  314. 'doc' => 'the installer will prefer releases with this state when installing packages without a version or state specified',
  315. 'valid_set' => array(
  316. 'stable', 'beta', 'alpha', 'devel', 'snapshot'),
  317. 'prompt' => 'Preferred Package State',
  318. 'group' => 'Advanced',
  319. ),
  320. 'umask' => array(
  321. 'type' => 'mask',
  322. 'default' => PEAR_CONFIG_DEFAULT_UMASK,
  323. 'doc' => 'umask used when creating files (Unix-like systems only)',
  324. 'prompt' => 'Unix file mask',
  325. 'group' => 'Advanced',
  326. ),
  327. 'cache_ttl' => array(
  328. 'type' => 'integer',
  329. 'default' => PEAR_CONFIG_DEFAULT_CACHE_TTL,
  330. 'doc' => 'amount of secs where the local cache is used and not updated',
  331. 'prompt' => 'Cache TimeToLive',
  332. 'group' => 'Advanced',
  333. ),
  334. 'sig_type' => array(
  335. 'type' => 'set',
  336. 'default' => PEAR_CONFIG_DEFAULT_SIG_TYPE,
  337. 'doc' => 'which package signature mechanism to use',
  338. 'valid_set' => array('gpg'),
  339. 'prompt' => 'Package Signature Type',
  340. 'group' => 'Maintainers',
  341. ),
  342. 'sig_bin' => array(
  343. 'type' => 'string',
  344. 'default' => PEAR_CONFIG_DEFAULT_SIG_BIN,
  345. 'doc' => 'which package signature mechanism to use',
  346. 'prompt' => 'Signature Handling Program',
  347. 'group' => 'Maintainers',
  348. ),
  349. 'sig_keyid' => array(
  350. 'type' => 'string',
  351. 'default' => '',
  352. 'doc' => 'which key to use for signing with',
  353. 'prompt' => 'Signature Key Id',
  354. 'group' => 'Maintainers',
  355. ),
  356. 'sig_keydir' => array(
  357. 'type' => 'string',
  358. 'default' => PEAR_CONFIG_DEFAULT_SIG_KEYDIR,
  359. 'doc' => 'which package signature mechanism to use',
  360. 'prompt' => 'Signature Key Directory',
  361. 'group' => 'Maintainers',
  362. ),
  363. );
  364. // }}}
  365. // {{{ PEAR_Config([file], [defaults_file])
  366. /**
  367. * Constructor.
  368. *
  369. * @param string (optional) file to read user-defined options from
  370. * @param string (optional) file to read system-wide defaults from
  371. *
  372. * @access public
  373. *
  374. * @see PEAR_Config::singleton
  375. */
  376. function PEAR_Config($user_file = '', $system_file = '')
  377. {
  378. $this->PEAR();
  379. $sl = DIRECTORY_SEPARATOR;
  380. if (empty($user_file)) {
  381. if (OS_WINDOWS) {
  382. $user_file = PEAR_CONFIG_SYSCONFDIR . $sl . 'pear.ini';
  383. } else {
  384. $user_file = getenv('HOME') . $sl . '.pearrc';
  385. }
  386. }
  387. if (empty($system_file)) {
  388. if (OS_WINDOWS) {
  389. $system_file = PEAR_CONFIG_SYSCONFDIR . $sl . 'pearsys.ini';
  390. } else {
  391. $system_file = PEAR_CONFIG_SYSCONFDIR . $sl . 'pear.conf';
  392. }
  393. }
  394. $this->layers = array_keys($this->configuration);
  395. $this->files['user'] = $user_file;
  396. $this->files['system'] = $system_file;
  397. if ($user_file && file_exists($user_file)) {
  398. $this->readConfigFile($user_file);
  399. }
  400. if ($system_file && file_exists($system_file)) {
  401. $this->mergeConfigFile($system_file, false, 'system');
  402. }
  403. foreach ($this->configuration_info as $key => $info) {
  404. $this->configuration['default'][$key] = $info['default'];
  405. }
  406. //$GLOBALS['_PEAR_Config_instance'] = &$this;
  407. }
  408. // }}}
  409. // {{{ singleton([file], [defaults_file])
  410. /**
  411. * Static singleton method. If you want to keep only one instance
  412. * of this class in use, this method will give you a reference to
  413. * the last created PEAR_Config object if one exists, or create a
  414. * new object.
  415. *
  416. * @param string (optional) file to read user-defined options from
  417. * @param string (optional) file to read system-wide defaults from
  418. *
  419. * @return object an existing or new PEAR_Config instance
  420. *
  421. * @access public
  422. *
  423. * @see PEAR_Config::PEAR_Config
  424. */
  425. function &singleton($user_file = '', $system_file = '')
  426. {
  427. if (is_object($GLOBALS['_PEAR_Config_instance'])) {
  428. return $GLOBALS['_PEAR_Config_instance'];
  429. }
  430. $GLOBALS['_PEAR_Config_instance'] =
  431. &new PEAR_Config($user_file, $system_file);
  432. return $GLOBALS['_PEAR_Config_instance'];
  433. }
  434. // }}}
  435. // {{{ readConfigFile([file], [layer])
  436. /**
  437. * Reads configuration data from a file. All existing values in
  438. * the config layer are discarded and replaced with data from the
  439. * file.
  440. *
  441. * @param string (optional) file to read from, if NULL or not
  442. * specified, the last-used file for the same layer (second param)
  443. * is used
  444. *
  445. * @param string (optional) config layer to insert data into
  446. * ('user' or 'system')
  447. *
  448. * @return bool TRUE on success or a PEAR error on failure
  449. *
  450. * @access public
  451. */
  452. function readConfigFile($file = null, $layer = 'user')
  453. {
  454. if (empty($this->files[$layer])) {
  455. return $this->raiseError("unknown config file type `$layer'");
  456. }
  457. if ($file === null) {
  458. $file = $this->files[$layer];
  459. }
  460. $data = $this->_readConfigDataFrom($file);
  461. if (PEAR::isError($data)) {
  462. return $data;
  463. }
  464. $this->_decodeInput($data);
  465. $this->configuration[$layer] = $data;
  466. return true;
  467. }
  468. // }}}
  469. // {{{ mergeConfigFile(file, [override], [layer])
  470. /**
  471. * Merges data into a config layer from a file. Does the same
  472. * thing as readConfigFile, except it does not replace all
  473. * existing values in the config layer.
  474. *
  475. * @param string file to read from
  476. *
  477. * @param bool (optional) whether to overwrite existing data
  478. * (default TRUE)
  479. *
  480. * @param string config layer to insert data into ('user' or
  481. * 'system')
  482. *
  483. * @return bool TRUE on success or a PEAR error on failure
  484. *
  485. * @access public.
  486. */
  487. function mergeConfigFile($file, $override = true, $layer = 'user')
  488. {
  489. if (empty($this->files[$layer])) {
  490. return $this->raiseError("unknown config file type `$layer'");
  491. }
  492. if ($file === null) {
  493. $file = $this->files[$layer];
  494. }
  495. $data = $this->_readConfigDataFrom($file);
  496. if (PEAR::isError($data)) {
  497. return $data;
  498. }
  499. $this->_decodeInput($data);
  500. if ($override) {
  501. $this->configuration[$layer] = array_merge($this->configuration[$layer], $data);
  502. } else {
  503. $this->configuration[$layer] = array_merge($data, $this->configuration[$layer]);
  504. }
  505. return true;
  506. }
  507. // }}}
  508. // {{{ writeConfigFile([file], [layer])
  509. /**
  510. * Writes data into a config layer from a file.
  511. *
  512. * @param string file to read from
  513. *
  514. * @param bool (optional) whether to overwrite existing data
  515. * (default TRUE)
  516. *
  517. * @param string config layer to insert data into ('user' or
  518. * 'system')
  519. *
  520. * @return bool TRUE on success or a PEAR error on failure
  521. *
  522. * @access public.
  523. */
  524. function writeConfigFile($file = null, $layer = 'user', $data = null)
  525. {
  526. if ($layer == 'both' || $layer == 'all') {
  527. foreach ($this->files as $type => $file) {
  528. $err = $this->writeConfigFile($file, $type, $data);
  529. if (PEAR::isError($err)) {
  530. return $err;
  531. }
  532. }
  533. return true;
  534. }
  535. if (empty($this->files[$layer])) {
  536. return $this->raiseError("unknown config file type `$layer'");
  537. }
  538. if ($file === null) {
  539. $file = $this->files[$layer];
  540. }
  541. $data = ($data === null) ? $this->configuration[$layer] : $data;
  542. $this->_encodeOutput($data);
  543. $opt = array('-p', dirname($file));
  544. if (!@System::mkDir($opt)) {
  545. return $this->raiseError("could not create directory: " . dirname($file));
  546. }
  547. if (@is_file($file) && !@is_writeable($file)) {
  548. return $this->raiseError("no write access to $file!");
  549. }
  550. $fp = @fopen($file, "w");
  551. if (!$fp) {
  552. return $this->raiseError("PEAR_Config::writeConfigFile fopen('$file','w') failed");
  553. }
  554. $contents = "#PEAR_Config 0.9\n" . serialize($data);
  555. if (!@fwrite($fp, $contents)) {
  556. return $this->raiseError("PEAR_Config::writeConfigFile: fwrite failed");
  557. }
  558. return true;
  559. }
  560. // }}}
  561. // {{{ _readConfigDataFrom(file)
  562. /**
  563. * Reads configuration data from a file and returns the parsed data
  564. * in an array.
  565. *
  566. * @param string file to read from
  567. *
  568. * @return array configuration data or a PEAR error on failure
  569. *
  570. * @access private
  571. */
  572. function _readConfigDataFrom($file)
  573. {
  574. $fp = @fopen($file, "r");
  575. if (!$fp) {
  576. return $this->raiseError("PEAR_Config::readConfigFile fopen('$file','r') failed");
  577. }
  578. $size = filesize($file);
  579. $rt = get_magic_quotes_runtime();
  580. set_magic_quotes_runtime(0);
  581. $contents = fread($fp, $size);
  582. set_magic_quotes_runtime($rt);
  583. fclose($fp);
  584. $version = '0.1';
  585. if (preg_match('/^#PEAR_Config\s+(\S+)\s+/si', $contents, $matches)) {
  586. $version = $matches[1];
  587. $contents = substr($contents, strlen($matches[0]));
  588. }
  589. if (version_compare("$version", '1', '<')) {
  590. $data = unserialize($contents);
  591. if (!is_array($data)) {
  592. if (strlen(trim($contents)) > 0) {
  593. $error = "PEAR_Config: bad data in $file";
  594. // if (isset($this)) {
  595. return $this->raiseError($error);
  596. // } else {
  597. // return PEAR::raiseError($error);
  598. } else {
  599. $data = array();
  600. }
  601. }
  602. // add parsing of newer formats here...
  603. } else {
  604. return $this->raiseError("$file: unknown version `$version'");
  605. }
  606. return $data;
  607. }
  608. // }}}
  609. // {{{ getConfFile(layer)
  610. /**
  611. * Gets the file used for storing the config for a layer
  612. *
  613. * @param string $layer 'user' or 'system'
  614. */
  615. function getConfFile($layer)
  616. {
  617. return $this->files[$layer];
  618. }
  619. // }}}
  620. // {{{ _encodeOutput(&data)
  621. /**
  622. * Encodes/scrambles configuration data before writing to files.
  623. * Currently, 'password' values will be base64-encoded as to avoid
  624. * that people spot cleartext passwords by accident.
  625. *
  626. * @param array (reference) array to encode values in
  627. *
  628. * @return bool TRUE on success
  629. *
  630. * @access private
  631. */
  632. function _encodeOutput(&$data)
  633. {
  634. foreach ($data as $key => $value) {
  635. if (!isset($this->configuration_info[$key])) {
  636. continue;
  637. }
  638. $type = $this->configuration_info[$key]['type'];
  639. switch ($type) {
  640. // we base64-encode passwords so they are at least
  641. // not shown in plain by accident
  642. case 'password': {
  643. $data[$key] = base64_encode($data[$key]);
  644. break;
  645. }
  646. case 'mask': {
  647. $data[$key] = octdec($data[$key]);
  648. break;
  649. }
  650. }
  651. }
  652. return true;
  653. }
  654. // }}}
  655. // {{{ _decodeInput(&data)
  656. /**
  657. * Decodes/unscrambles configuration data after reading from files.
  658. *
  659. * @param array (reference) array to encode values in
  660. *
  661. * @return bool TRUE on success
  662. *
  663. * @access private
  664. *
  665. * @see PEAR_Config::_encodeOutput
  666. */
  667. function _decodeInput(&$data)
  668. {
  669. if (!is_array($data)) {
  670. return true;
  671. }
  672. foreach ($data as $key => $value) {
  673. if (!isset($this->configuration_info[$key])) {
  674. continue;
  675. }
  676. $type = $this->configuration_info[$key]['type'];
  677. switch ($type) {
  678. case 'password': {
  679. $data[$key] = base64_decode($data[$key]);
  680. break;
  681. }
  682. case 'mask': {
  683. $data[$key] = decoct($data[$key]);
  684. break;
  685. }
  686. }
  687. }
  688. return true;
  689. }
  690. // }}}
  691. // {{{ get(key, [layer])
  692. /**
  693. * Returns a configuration value, prioritizing layers as per the
  694. * layers property.
  695. *
  696. * @param string config key
  697. *
  698. * @return mixed the config value, or NULL if not found
  699. *
  700. * @access public
  701. */
  702. function get($key, $layer = null)
  703. {
  704. if ($layer === null) {
  705. foreach ($this->layers as $layer) {
  706. if (isset($this->configuration[$layer][$key])) {
  707. return $this->configuration[$layer][$key];
  708. }
  709. }
  710. } elseif (isset($this->configuration[$layer][$key])) {
  711. return $this->configuration[$layer][$key];
  712. }
  713. return null;
  714. }
  715. // }}}
  716. // {{{ set(key, value, [layer])
  717. /**
  718. * Set a config value in a specific layer (defaults to 'user').
  719. * Enforces the types defined in the configuration_info array. An
  720. * integer config variable will be cast to int, and a set config
  721. * variable will be validated against its legal values.
  722. *
  723. * @param string config key
  724. *
  725. * @param string config value
  726. *
  727. * @param string (optional) config layer
  728. *
  729. * @return bool TRUE on success, FALSE on failure
  730. *
  731. * @access public
  732. */
  733. function set($key, $value, $layer = 'user')
  734. {
  735. if (empty($this->configuration_info[$key])) {
  736. return false;
  737. }
  738. extract($this->configuration_info[$key]);
  739. switch ($type) {
  740. case 'integer':
  741. $value = (int)$value;
  742. break;
  743. case 'set': {
  744. // If a valid_set is specified, require the value to
  745. // be in the set. If there is no valid_set, accept
  746. // any value.
  747. if ($valid_set) {
  748. reset($valid_set);
  749. if ((key($valid_set) === 0 && !in_array($value, $valid_set)) ||
  750. (key($valid_set) !== 0 && empty($valid_set[$value])))
  751. {
  752. return false;
  753. }
  754. }
  755. break;
  756. }
  757. }
  758. $this->configuration[$layer][$key] = $value;
  759. return true;
  760. }
  761. // }}}
  762. // {{{ getType(key)
  763. /**
  764. * Get the type of a config value.
  765. *
  766. * @param string config key
  767. *
  768. * @return string type, one of "string", "integer", "file",
  769. * "directory", "set" or "password".
  770. *
  771. * @access public
  772. *
  773. */
  774. function getType($key)
  775. {
  776. if (isset($this->configuration_info[$key])) {
  777. return $this->configuration_info[$key]['type'];
  778. }
  779. return false;
  780. }
  781. // }}}
  782. // {{{ getDocs(key)
  783. /**
  784. * Get the documentation for a config value.
  785. *
  786. * @param string config key
  787. *
  788. * @return string documentation string
  789. *
  790. * @access public
  791. *
  792. */
  793. function getDocs($key)
  794. {
  795. if (isset($this->configuration_info[$key])) {
  796. return $this->configuration_info[$key]['doc'];
  797. }
  798. return false;
  799. }
  800. // }}}
  801. // {{{ getPrompt(key)
  802. /**
  803. * Get the short documentation for a config value.
  804. *
  805. * @param string config key
  806. *
  807. * @return string short documentation string
  808. *
  809. * @access public
  810. *
  811. */
  812. function getPrompt($key)
  813. {
  814. if (isset($this->configuration_info[$key])) {
  815. return $this->configuration_info[$key]['prompt'];
  816. }
  817. return false;
  818. }
  819. // }}}
  820. // {{{ getGroup(key)
  821. /**
  822. * Get the parameter group for a config key.
  823. *
  824. * @param string config key
  825. *
  826. * @return string parameter group
  827. *
  828. * @access public
  829. *
  830. */
  831. function getGroup($key)
  832. {
  833. if (isset($this->configuration_info[$key])) {
  834. return $this->configuration_info[$key]['group'];
  835. }
  836. return false;
  837. }
  838. // }}}
  839. // {{{ getGroups()
  840. /**
  841. * Get the list of parameter groups.
  842. *
  843. * @return array list of parameter groups
  844. *
  845. * @access public
  846. *
  847. */
  848. function getGroups()
  849. {
  850. $tmp = array();
  851. foreach ($this->configuration_info as $key => $info) {
  852. $tmp[$info['group']] = 1;
  853. }
  854. return array_keys($tmp);
  855. }
  856. // }}}
  857. // {{{ getGroupKeys()
  858. /**
  859. * Get the list of the parameters in a group.
  860. *
  861. * @param string $group parameter group
  862. *
  863. * @return array list of parameters in $group
  864. *
  865. * @access public
  866. *
  867. */
  868. function getGroupKeys($group)
  869. {
  870. $keys = array();
  871. foreach ($this->configuration_info as $key => $info) {
  872. if ($info['group'] == $group) {
  873. $keys[] = $key;
  874. }
  875. }
  876. return $keys;
  877. }
  878. // }}}
  879. // {{{ getSetValues(key)
  880. /**
  881. * Get the list of allowed set values for a config value. Returns
  882. * NULL for config values that are not sets.
  883. *
  884. * @param string config key
  885. *
  886. * @return array enumerated array of set values, or NULL if the
  887. * config key is unknown or not a set
  888. *
  889. * @access public
  890. *
  891. */
  892. function getSetValues($key)
  893. {
  894. if (isset($this->configuration_info[$key]) &&
  895. isset($this->configuration_info[$key]['type']) &&
  896. $this->configuration_info[$key]['type'] == 'set')
  897. {
  898. $valid_set = $this->configuration_info[$key]['valid_set'];
  899. reset($valid_set);
  900. if (key($valid_set) === 0) {
  901. return $valid_set;
  902. }
  903. return array_keys($valid_set);
  904. }
  905. return false;
  906. }
  907. // }}}
  908. // {{{ getKeys()
  909. /**
  910. * Get all the current config keys.
  911. *
  912. * @return array simple array of config keys
  913. *
  914. * @access public
  915. */
  916. function getKeys()
  917. {
  918. $keys = array();
  919. foreach ($this->layers as $layer) {
  920. $keys = array_merge($keys, $this->configuration[$layer]);
  921. }
  922. return array_keys($keys);
  923. }
  924. // }}}
  925. // {{{ remove(key, [layer])
  926. /**
  927. * Remove the a config key from a specific config layer.
  928. *
  929. * @param string config key
  930. *
  931. * @param string (optional) config layer
  932. *
  933. * @return bool TRUE on success, FALSE on failure
  934. *
  935. * @access public
  936. */
  937. function remove($key, $layer = 'user')
  938. {
  939. if (isset($this->configuration[$layer][$key])) {
  940. unset($this->configuration[$layer][$key]);
  941. return true;
  942. }
  943. return false;
  944. }
  945. // }}}
  946. // {{{ removeLayer(layer)
  947. /**
  948. * Temporarily remove an entire config layer. USE WITH CARE!
  949. *
  950. * @param string config key
  951. *
  952. * @param string (optional) config layer
  953. *
  954. * @return bool TRUE on success, FALSE on failure
  955. *
  956. * @access public
  957. */
  958. function removeLayer($layer)
  959. {
  960. if (isset($this->configuration[$layer])) {
  961. $this->configuration[$layer] = array();
  962. return true;
  963. }
  964. return false;
  965. }
  966. // }}}
  967. // {{{ store([layer])
  968. /**
  969. * Stores configuration data in a layer.
  970. *
  971. * @param string config layer to store
  972. *
  973. * @return bool TRUE on success, or PEAR error on failure
  974. *
  975. * @access public
  976. */
  977. function store($layer = 'user', $data = null)
  978. {
  979. return $this->writeConfigFile(null, $layer, $data);
  980. }
  981. // }}}
  982. // {{{ toDefault(key)
  983. /**
  984. * Unset the user-defined value of a config key, reverting the
  985. * value to the system-defined one.
  986. *
  987. * @param string config key
  988. *
  989. * @return bool TRUE on success, FALSE on failure
  990. *
  991. * @access public
  992. */
  993. function toDefault($key)
  994. {
  995. trigger_error("PEAR_Config::toDefault() deprecated, use PEAR_Config::remove() instead", E_USER_NOTICE);
  996. return $this->remove($key, 'user');
  997. }
  998. // }}}
  999. // {{{ definedBy(key)
  1000. /**
  1001. * Tells what config layer that gets to define a key.
  1002. *
  1003. * @param string config key
  1004. *
  1005. * @return string the config layer, or an empty string if not found
  1006. *
  1007. * @access public
  1008. */
  1009. function definedBy($key)
  1010. {
  1011. foreach ($this->layers as $layer) {
  1012. if (isset($this->configuration[$layer][$key])) {
  1013. return $layer;
  1014. }
  1015. }
  1016. return '';
  1017. }
  1018. // }}}
  1019. // {{{ isDefaulted(key)
  1020. /**
  1021. * Tells whether a config value has a system-defined value.
  1022. *
  1023. * @param string config key
  1024. *
  1025. * @return bool
  1026. *
  1027. * @access public
  1028. *
  1029. * @deprecated
  1030. */
  1031. function isDefaulted($key)
  1032. {
  1033. trigger_error("PEAR_Config::isDefaulted() deprecated, use PEAR_Config::definedBy() instead", E_USER_NOTICE);
  1034. return $this->definedBy($key) == 'system';
  1035. }
  1036. // }}}
  1037. // {{{ isDefined(key)
  1038. /**
  1039. * Tells whether a given key exists as a config value.
  1040. *
  1041. * @param string config key
  1042. *
  1043. * @return bool whether <config key> exists in this object
  1044. *
  1045. * @access public
  1046. */
  1047. function isDefined($key)
  1048. {
  1049. foreach ($this->layers as $layer) {
  1050. if (isset($this->configuration[$layer][$key])) {
  1051. return true;
  1052. }
  1053. }
  1054. return false;
  1055. }
  1056. // }}}
  1057. // {{{ isDefinedLayer(key)
  1058. /**
  1059. * Tells whether a given config layer exists.
  1060. *
  1061. * @param string config layer
  1062. *
  1063. * @return bool whether <config layer> exists in this object
  1064. *
  1065. * @access public
  1066. */
  1067. function isDefinedLayer($layer)
  1068. {
  1069. return isset($this->configuration[$layer]);
  1070. }
  1071. // }}}
  1072. // {{{ getLayers()
  1073. /**
  1074. * Returns the layers defined (except the 'default' one)
  1075. *
  1076. * @return array of the defined layers
  1077. */
  1078. function getLayers()
  1079. {
  1080. $cf = $this->configuration;
  1081. unset($cf['default']);
  1082. return array_keys($cf);
  1083. }
  1084. // }}}
  1085. }
  1086. ?>