Config.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131
  1. <?php
  2. /**
  3. * PEAR_Config, customized configuration handling for the PEAR Installer
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Stig Bakken <ssb@php.net>
  10. * @author Greg Beaver <cellog@php.net>
  11. * @copyright 1997-2009 The Authors
  12. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  13. * @link http://pear.php.net/package/PEAR
  14. * @since File available since Release 0.1
  15. */
  16. /**
  17. * Required for error handling
  18. */
  19. require_once 'PEAR.php';
  20. require_once 'PEAR/Registry.php';
  21. require_once 'PEAR/Installer/Role.php';
  22. require_once 'System.php';
  23. /**
  24. * Last created PEAR_Config instance.
  25. * @var object
  26. */
  27. $GLOBALS['_PEAR_Config_instance'] = null;
  28. if (!defined('PEAR_INSTALL_DIR') || !PEAR_INSTALL_DIR) {
  29. $PEAR_INSTALL_DIR = PHP_LIBDIR . DIRECTORY_SEPARATOR . 'pear';
  30. } else {
  31. $PEAR_INSTALL_DIR = PEAR_INSTALL_DIR;
  32. }
  33. // Below we define constants with default values for all configuration
  34. // parameters except username/password. All of them can have their
  35. // defaults set through environment variables. The reason we use the
  36. // PHP_ prefix is for some security, PHP protects environment
  37. // variables starting with PHP_*.
  38. // default channel and preferred mirror is based on whether we are invoked through
  39. // the "pear" or the "pecl" command
  40. if (!defined('PEAR_RUNTYPE')) {
  41. define('PEAR_RUNTYPE', 'pear');
  42. }
  43. if (PEAR_RUNTYPE == 'pear') {
  44. define('PEAR_CONFIG_DEFAULT_CHANNEL', 'pear.php.net');
  45. } else {
  46. define('PEAR_CONFIG_DEFAULT_CHANNEL', 'pecl.php.net');
  47. }
  48. if (getenv('PHP_PEAR_SYSCONF_DIR')) {
  49. define('PEAR_CONFIG_SYSCONFDIR', getenv('PHP_PEAR_SYSCONF_DIR'));
  50. } elseif (getenv('SystemRoot')) {
  51. define('PEAR_CONFIG_SYSCONFDIR', getenv('SystemRoot'));
  52. } else {
  53. define('PEAR_CONFIG_SYSCONFDIR', PHP_SYSCONFDIR);
  54. }
  55. // Default for master_server
  56. if (getenv('PHP_PEAR_MASTER_SERVER')) {
  57. define('PEAR_CONFIG_DEFAULT_MASTER_SERVER', getenv('PHP_PEAR_MASTER_SERVER'));
  58. } else {
  59. define('PEAR_CONFIG_DEFAULT_MASTER_SERVER', 'pear.php.net');
  60. }
  61. // Default for http_proxy
  62. if (getenv('PHP_PEAR_HTTP_PROXY')) {
  63. define('PEAR_CONFIG_DEFAULT_HTTP_PROXY', getenv('PHP_PEAR_HTTP_PROXY'));
  64. } elseif (getenv('http_proxy')) {
  65. define('PEAR_CONFIG_DEFAULT_HTTP_PROXY', getenv('http_proxy'));
  66. } else {
  67. define('PEAR_CONFIG_DEFAULT_HTTP_PROXY', '');
  68. }
  69. // Default for php_dir
  70. if (getenv('PHP_PEAR_INSTALL_DIR')) {
  71. define('PEAR_CONFIG_DEFAULT_PHP_DIR', getenv('PHP_PEAR_INSTALL_DIR'));
  72. } else {
  73. if (@file_exists($PEAR_INSTALL_DIR) && is_dir($PEAR_INSTALL_DIR)) {
  74. define('PEAR_CONFIG_DEFAULT_PHP_DIR', $PEAR_INSTALL_DIR);
  75. } else {
  76. define('PEAR_CONFIG_DEFAULT_PHP_DIR', $PEAR_INSTALL_DIR);
  77. }
  78. }
  79. // Default for metadata_dir
  80. if (getenv('PHP_PEAR_METADATA_DIR')) {
  81. define('PEAR_CONFIG_DEFAULT_METADATA_DIR', getenv('PHP_PEAR_METADATA_DIR'));
  82. } else {
  83. define('PEAR_CONFIG_DEFAULT_METADATA_DIR', '');
  84. }
  85. // Default for ext_dir
  86. if (getenv('PHP_PEAR_EXTENSION_DIR')) {
  87. define('PEAR_CONFIG_DEFAULT_EXT_DIR', getenv('PHP_PEAR_EXTENSION_DIR'));
  88. } else {
  89. if (ini_get('extension_dir')) {
  90. define('PEAR_CONFIG_DEFAULT_EXT_DIR', ini_get('extension_dir'));
  91. } elseif (defined('PEAR_EXTENSION_DIR') &&
  92. file_exists(PEAR_EXTENSION_DIR) && is_dir(PEAR_EXTENSION_DIR)) {
  93. define('PEAR_CONFIG_DEFAULT_EXT_DIR', PEAR_EXTENSION_DIR);
  94. } elseif (defined('PHP_EXTENSION_DIR')) {
  95. define('PEAR_CONFIG_DEFAULT_EXT_DIR', PHP_EXTENSION_DIR);
  96. } else {
  97. define('PEAR_CONFIG_DEFAULT_EXT_DIR', '.');
  98. }
  99. }
  100. // Default for doc_dir
  101. if (getenv('PHP_PEAR_DOC_DIR')) {
  102. define('PEAR_CONFIG_DEFAULT_DOC_DIR', getenv('PHP_PEAR_DOC_DIR'));
  103. } else {
  104. define('PEAR_CONFIG_DEFAULT_DOC_DIR',
  105. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'docs');
  106. }
  107. // Default for bin_dir
  108. if (getenv('PHP_PEAR_BIN_DIR')) {
  109. define('PEAR_CONFIG_DEFAULT_BIN_DIR', getenv('PHP_PEAR_BIN_DIR'));
  110. } else {
  111. define('PEAR_CONFIG_DEFAULT_BIN_DIR', PHP_BINDIR);
  112. }
  113. // Default for data_dir
  114. if (getenv('PHP_PEAR_DATA_DIR')) {
  115. define('PEAR_CONFIG_DEFAULT_DATA_DIR', getenv('PHP_PEAR_DATA_DIR'));
  116. } else {
  117. define('PEAR_CONFIG_DEFAULT_DATA_DIR',
  118. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'data');
  119. }
  120. // Default for cfg_dir
  121. if (getenv('PHP_PEAR_CFG_DIR')) {
  122. define('PEAR_CONFIG_DEFAULT_CFG_DIR', getenv('PHP_PEAR_CFG_DIR'));
  123. } else {
  124. define('PEAR_CONFIG_DEFAULT_CFG_DIR',
  125. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'cfg');
  126. }
  127. // Default for www_dir
  128. if (getenv('PHP_PEAR_WWW_DIR')) {
  129. define('PEAR_CONFIG_DEFAULT_WWW_DIR', getenv('PHP_PEAR_WWW_DIR'));
  130. } else {
  131. define('PEAR_CONFIG_DEFAULT_WWW_DIR',
  132. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'www');
  133. }
  134. // Default for man_dir
  135. if (getenv('PHP_PEAR_MAN_DIR')) {
  136. define('PEAR_CONFIG_DEFAULT_MAN_DIR', getenv('PHP_PEAR_MAN_DIR'));
  137. } else {
  138. if (defined('PHP_MANDIR')) { // Added in PHP5.3.7
  139. define('PEAR_CONFIG_DEFAULT_MAN_DIR', PHP_MANDIR);
  140. } else {
  141. define('PEAR_CONFIG_DEFAULT_MAN_DIR', PHP_PREFIX . DIRECTORY_SEPARATOR .
  142. 'local' . DIRECTORY_SEPARATOR .'man');
  143. }
  144. }
  145. // Default for test_dir
  146. if (getenv('PHP_PEAR_TEST_DIR')) {
  147. define('PEAR_CONFIG_DEFAULT_TEST_DIR', getenv('PHP_PEAR_TEST_DIR'));
  148. } else {
  149. define('PEAR_CONFIG_DEFAULT_TEST_DIR',
  150. $PEAR_INSTALL_DIR.DIRECTORY_SEPARATOR.'tests');
  151. }
  152. // Default for temp_dir
  153. if (getenv('PHP_PEAR_TEMP_DIR')) {
  154. define('PEAR_CONFIG_DEFAULT_TEMP_DIR', getenv('PHP_PEAR_TEMP_DIR'));
  155. } else {
  156. define('PEAR_CONFIG_DEFAULT_TEMP_DIR',
  157. System::tmpdir() . DIRECTORY_SEPARATOR . 'pear' .
  158. DIRECTORY_SEPARATOR . 'temp');
  159. }
  160. // Default for cache_dir
  161. if (getenv('PHP_PEAR_CACHE_DIR')) {
  162. define('PEAR_CONFIG_DEFAULT_CACHE_DIR', getenv('PHP_PEAR_CACHE_DIR'));
  163. } else {
  164. define('PEAR_CONFIG_DEFAULT_CACHE_DIR',
  165. System::tmpdir() . DIRECTORY_SEPARATOR . 'pear' .
  166. DIRECTORY_SEPARATOR . 'cache');
  167. }
  168. // Default for download_dir
  169. if (getenv('PHP_PEAR_DOWNLOAD_DIR')) {
  170. define('PEAR_CONFIG_DEFAULT_DOWNLOAD_DIR', getenv('PHP_PEAR_DOWNLOAD_DIR'));
  171. } else {
  172. define('PEAR_CONFIG_DEFAULT_DOWNLOAD_DIR',
  173. System::tmpdir() . DIRECTORY_SEPARATOR . 'pear' .
  174. DIRECTORY_SEPARATOR . 'download');
  175. }
  176. // Default for php_bin
  177. if (getenv('PHP_PEAR_PHP_BIN')) {
  178. define('PEAR_CONFIG_DEFAULT_PHP_BIN', getenv('PHP_PEAR_PHP_BIN'));
  179. } else {
  180. define('PEAR_CONFIG_DEFAULT_PHP_BIN', PEAR_CONFIG_DEFAULT_BIN_DIR.
  181. DIRECTORY_SEPARATOR.'php'.(OS_WINDOWS ? '.exe' : ''));
  182. }
  183. // Default for verbose
  184. if (getenv('PHP_PEAR_VERBOSE')) {
  185. define('PEAR_CONFIG_DEFAULT_VERBOSE', getenv('PHP_PEAR_VERBOSE'));
  186. } else {
  187. define('PEAR_CONFIG_DEFAULT_VERBOSE', 1);
  188. }
  189. // Default for preferred_state
  190. if (getenv('PHP_PEAR_PREFERRED_STATE')) {
  191. define('PEAR_CONFIG_DEFAULT_PREFERRED_STATE', getenv('PHP_PEAR_PREFERRED_STATE'));
  192. } else {
  193. define('PEAR_CONFIG_DEFAULT_PREFERRED_STATE', 'stable');
  194. }
  195. // Default for umask
  196. if (getenv('PHP_PEAR_UMASK')) {
  197. define('PEAR_CONFIG_DEFAULT_UMASK', getenv('PHP_PEAR_UMASK'));
  198. } else {
  199. define('PEAR_CONFIG_DEFAULT_UMASK', decoct(umask()));
  200. }
  201. // Default for cache_ttl
  202. if (getenv('PHP_PEAR_CACHE_TTL')) {
  203. define('PEAR_CONFIG_DEFAULT_CACHE_TTL', getenv('PHP_PEAR_CACHE_TTL'));
  204. } else {
  205. define('PEAR_CONFIG_DEFAULT_CACHE_TTL', 3600);
  206. }
  207. // Default for sig_type
  208. if (getenv('PHP_PEAR_SIG_TYPE')) {
  209. define('PEAR_CONFIG_DEFAULT_SIG_TYPE', getenv('PHP_PEAR_SIG_TYPE'));
  210. } else {
  211. define('PEAR_CONFIG_DEFAULT_SIG_TYPE', 'gpg');
  212. }
  213. // Default for sig_bin
  214. if (getenv('PHP_PEAR_SIG_BIN')) {
  215. define('PEAR_CONFIG_DEFAULT_SIG_BIN', getenv('PHP_PEAR_SIG_BIN'));
  216. } else {
  217. define('PEAR_CONFIG_DEFAULT_SIG_BIN',
  218. System::which(
  219. 'gpg', OS_WINDOWS ? 'c:\gnupg\gpg.exe' : '/usr/local/bin/gpg'));
  220. }
  221. // Default for sig_keydir
  222. if (getenv('PHP_PEAR_SIG_KEYDIR')) {
  223. define('PEAR_CONFIG_DEFAULT_SIG_KEYDIR', getenv('PHP_PEAR_SIG_KEYDIR'));
  224. } else {
  225. define('PEAR_CONFIG_DEFAULT_SIG_KEYDIR',
  226. PEAR_CONFIG_SYSCONFDIR . DIRECTORY_SEPARATOR . 'pearkeys');
  227. }
  228. /**
  229. * This is a class for storing configuration data, keeping track of
  230. * which are system-defined, user-defined or defaulted.
  231. * @category pear
  232. * @package PEAR
  233. * @author Stig Bakken <ssb@php.net>
  234. * @author Greg Beaver <cellog@php.net>
  235. * @copyright 1997-2009 The Authors
  236. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  237. * @version Release: 1.10.10
  238. * @link http://pear.php.net/package/PEAR
  239. * @since Class available since Release 0.1
  240. */
  241. class PEAR_Config extends PEAR
  242. {
  243. /**
  244. * Array of config files used.
  245. *
  246. * @var array layer => config file
  247. */
  248. var $files = array(
  249. 'system' => '',
  250. 'user' => '',
  251. );
  252. var $layers = array();
  253. /**
  254. * Configuration data, two-dimensional array where the first
  255. * dimension is the config layer ('user', 'system' and 'default'),
  256. * and the second dimension is keyname => value.
  257. *
  258. * The order in the first dimension is important! Earlier
  259. * layers will shadow later ones when a config value is
  260. * requested (if a 'user' value exists, it will be returned first,
  261. * then 'system' and finally 'default').
  262. *
  263. * @var array layer => array(keyname => value, ...)
  264. */
  265. var $configuration = array(
  266. 'user' => array(),
  267. 'system' => array(),
  268. 'default' => array(),
  269. );
  270. /**
  271. * Configuration values that can be set for a channel
  272. *
  273. * All other configuration values can only have a global value
  274. * @var array
  275. * @access private
  276. */
  277. var $_channelConfigInfo = array(
  278. 'php_dir', 'ext_dir', 'doc_dir', 'bin_dir', 'data_dir', 'cfg_dir',
  279. 'test_dir', 'www_dir', 'php_bin', 'php_prefix', 'php_suffix', 'username',
  280. 'password', 'verbose', 'preferred_state', 'umask', 'preferred_mirror', 'php_ini'
  281. );
  282. /**
  283. * Channels that can be accessed
  284. * @see setChannels()
  285. * @var array
  286. * @access private
  287. */
  288. var $_channels = array('pear.php.net', 'pecl.php.net', '__uri');
  289. /**
  290. * This variable is used to control the directory values returned
  291. * @see setInstallRoot();
  292. * @var string|false
  293. * @access private
  294. */
  295. var $_installRoot = false;
  296. /**
  297. * If requested, this will always refer to the registry
  298. * contained in php_dir
  299. * @var PEAR_Registry
  300. */
  301. var $_registry = array();
  302. /**
  303. * @var array
  304. * @access private
  305. */
  306. var $_regInitialized = array();
  307. /**
  308. * @var bool
  309. * @access private
  310. */
  311. var $_noRegistry = false;
  312. /**
  313. * amount of errors found while parsing config
  314. * @var integer
  315. * @access private
  316. */
  317. var $_errorsFound = 0;
  318. var $_lastError = null;
  319. /**
  320. * Information about the configuration data. Stores the type,
  321. * default value and a documentation string for each configuration
  322. * value.
  323. *
  324. * @var array layer => array(infotype => value, ...)
  325. */
  326. var $configuration_info = array(
  327. // Channels/Internet Access
  328. 'default_channel' => array(
  329. 'type' => 'string',
  330. 'default' => PEAR_CONFIG_DEFAULT_CHANNEL,
  331. 'doc' => 'the default channel to use for all non explicit commands',
  332. 'prompt' => 'Default Channel',
  333. 'group' => 'Internet Access',
  334. ),
  335. 'preferred_mirror' => array(
  336. 'type' => 'string',
  337. 'default' => PEAR_CONFIG_DEFAULT_CHANNEL,
  338. 'doc' => 'the default server or mirror to use for channel actions',
  339. 'prompt' => 'Default Channel Mirror',
  340. 'group' => 'Internet Access',
  341. ),
  342. 'remote_config' => array(
  343. 'type' => 'password',
  344. 'default' => '',
  345. 'doc' => 'ftp url of remote configuration file to use for synchronized install',
  346. 'prompt' => 'Remote Configuration File',
  347. 'group' => 'Internet Access',
  348. ),
  349. 'auto_discover' => array(
  350. 'type' => 'integer',
  351. 'default' => 0,
  352. 'doc' => 'whether to automatically discover new channels',
  353. 'prompt' => 'Auto-discover new Channels',
  354. 'group' => 'Internet Access',
  355. ),
  356. // Internet Access
  357. 'master_server' => array(
  358. 'type' => 'string',
  359. 'default' => 'pear.php.net',
  360. 'doc' => 'name of the main PEAR server [NOT USED IN THIS VERSION]',
  361. 'prompt' => 'PEAR server [DEPRECATED]',
  362. 'group' => 'Internet Access',
  363. ),
  364. 'http_proxy' => array(
  365. 'type' => 'string',
  366. 'default' => PEAR_CONFIG_DEFAULT_HTTP_PROXY,
  367. 'doc' => 'HTTP proxy (host:port) to use when downloading packages',
  368. 'prompt' => 'HTTP Proxy Server Address',
  369. 'group' => 'Internet Access',
  370. ),
  371. // File Locations
  372. 'php_dir' => array(
  373. 'type' => 'directory',
  374. 'default' => PEAR_CONFIG_DEFAULT_PHP_DIR,
  375. 'doc' => 'directory where .php files are installed',
  376. 'prompt' => 'PEAR directory',
  377. 'group' => 'File Locations',
  378. ),
  379. 'ext_dir' => array(
  380. 'type' => 'directory',
  381. 'default' => PEAR_CONFIG_DEFAULT_EXT_DIR,
  382. 'doc' => 'directory where loadable extensions are installed',
  383. 'prompt' => 'PHP extension directory',
  384. 'group' => 'File Locations',
  385. ),
  386. 'doc_dir' => array(
  387. 'type' => 'directory',
  388. 'default' => PEAR_CONFIG_DEFAULT_DOC_DIR,
  389. 'doc' => 'directory where documentation is installed',
  390. 'prompt' => 'PEAR documentation directory',
  391. 'group' => 'File Locations',
  392. ),
  393. 'bin_dir' => array(
  394. 'type' => 'directory',
  395. 'default' => PEAR_CONFIG_DEFAULT_BIN_DIR,
  396. 'doc' => 'directory where executables are installed',
  397. 'prompt' => 'PEAR executables directory',
  398. 'group' => 'File Locations',
  399. ),
  400. 'data_dir' => array(
  401. 'type' => 'directory',
  402. 'default' => PEAR_CONFIG_DEFAULT_DATA_DIR,
  403. 'doc' => 'directory where data files are installed',
  404. 'prompt' => 'PEAR data directory',
  405. 'group' => 'File Locations (Advanced)',
  406. ),
  407. 'cfg_dir' => array(
  408. 'type' => 'directory',
  409. 'default' => PEAR_CONFIG_DEFAULT_CFG_DIR,
  410. 'doc' => 'directory where modifiable configuration files are installed',
  411. 'prompt' => 'PEAR configuration file directory',
  412. 'group' => 'File Locations (Advanced)',
  413. ),
  414. 'www_dir' => array(
  415. 'type' => 'directory',
  416. 'default' => PEAR_CONFIG_DEFAULT_WWW_DIR,
  417. 'doc' => 'directory where www frontend files (html/js) are installed',
  418. 'prompt' => 'PEAR www files directory',
  419. 'group' => 'File Locations (Advanced)',
  420. ),
  421. 'man_dir' => array(
  422. 'type' => 'directory',
  423. 'default' => PEAR_CONFIG_DEFAULT_MAN_DIR,
  424. 'doc' => 'directory where unix manual pages are installed',
  425. 'prompt' => 'Systems manpage files directory',
  426. 'group' => 'File Locations (Advanced)',
  427. ),
  428. 'test_dir' => array(
  429. 'type' => 'directory',
  430. 'default' => PEAR_CONFIG_DEFAULT_TEST_DIR,
  431. 'doc' => 'directory where regression tests are installed',
  432. 'prompt' => 'PEAR test directory',
  433. 'group' => 'File Locations (Advanced)',
  434. ),
  435. 'cache_dir' => array(
  436. 'type' => 'directory',
  437. 'default' => PEAR_CONFIG_DEFAULT_CACHE_DIR,
  438. 'doc' => 'directory which is used for web service cache',
  439. 'prompt' => 'PEAR Installer cache directory',
  440. 'group' => 'File Locations (Advanced)',
  441. ),
  442. 'temp_dir' => array(
  443. 'type' => 'directory',
  444. 'default' => PEAR_CONFIG_DEFAULT_TEMP_DIR,
  445. 'doc' => 'directory which is used for all temp files',
  446. 'prompt' => 'PEAR Installer temp directory',
  447. 'group' => 'File Locations (Advanced)',
  448. ),
  449. 'download_dir' => array(
  450. 'type' => 'directory',
  451. 'default' => PEAR_CONFIG_DEFAULT_DOWNLOAD_DIR,
  452. 'doc' => 'directory which is used for all downloaded files',
  453. 'prompt' => 'PEAR Installer download directory',
  454. 'group' => 'File Locations (Advanced)',
  455. ),
  456. 'php_bin' => array(
  457. 'type' => 'file',
  458. 'default' => PEAR_CONFIG_DEFAULT_PHP_BIN,
  459. 'doc' => 'PHP CLI/CGI binary for executing scripts',
  460. 'prompt' => 'PHP CLI/CGI binary',
  461. 'group' => 'File Locations (Advanced)',
  462. ),
  463. 'php_prefix' => array(
  464. 'type' => 'string',
  465. 'default' => '',
  466. 'doc' => '--program-prefix for php_bin\'s ./configure, used for pecl installs',
  467. 'prompt' => '--program-prefix passed to PHP\'s ./configure',
  468. 'group' => 'File Locations (Advanced)',
  469. ),
  470. 'php_suffix' => array(
  471. 'type' => 'string',
  472. 'default' => '',
  473. 'doc' => '--program-suffix for php_bin\'s ./configure, used for pecl installs',
  474. 'prompt' => '--program-suffix passed to PHP\'s ./configure',
  475. 'group' => 'File Locations (Advanced)',
  476. ),
  477. 'php_ini' => array(
  478. 'type' => 'file',
  479. 'default' => '',
  480. 'doc' => 'location of php.ini in which to enable PECL extensions on install',
  481. 'prompt' => 'php.ini location',
  482. 'group' => 'File Locations (Advanced)',
  483. ),
  484. 'metadata_dir' => array(
  485. 'type' => 'directory',
  486. 'default' => PEAR_CONFIG_DEFAULT_METADATA_DIR,
  487. 'doc' => 'directory where metadata files are installed (registry, filemap, channels, ...)',
  488. 'prompt' => 'PEAR metadata directory',
  489. 'group' => 'File Locations (Advanced)',
  490. ),
  491. // Maintainers
  492. 'username' => array(
  493. 'type' => 'string',
  494. 'default' => '',
  495. 'doc' => '(maintainers) your PEAR account name',
  496. 'prompt' => 'PEAR username (for maintainers)',
  497. 'group' => 'Maintainers',
  498. ),
  499. 'password' => array(
  500. 'type' => 'password',
  501. 'default' => '',
  502. 'doc' => '(maintainers) your PEAR account password',
  503. 'prompt' => 'PEAR password (for maintainers)',
  504. 'group' => 'Maintainers',
  505. ),
  506. // Advanced
  507. 'verbose' => array(
  508. 'type' => 'integer',
  509. 'default' => PEAR_CONFIG_DEFAULT_VERBOSE,
  510. 'doc' => 'verbosity level
  511. 0: really quiet
  512. 1: somewhat quiet
  513. 2: verbose
  514. 3: debug',
  515. 'prompt' => 'Debug Log Level',
  516. 'group' => 'Advanced',
  517. ),
  518. 'preferred_state' => array(
  519. 'type' => 'set',
  520. 'default' => PEAR_CONFIG_DEFAULT_PREFERRED_STATE,
  521. 'doc' => 'the installer will prefer releases with this state when installing packages without a version or state specified',
  522. 'valid_set' => array(
  523. 'stable', 'beta', 'alpha', 'devel', 'snapshot'),
  524. 'prompt' => 'Preferred Package State',
  525. 'group' => 'Advanced',
  526. ),
  527. 'umask' => array(
  528. 'type' => 'mask',
  529. 'default' => PEAR_CONFIG_DEFAULT_UMASK,
  530. 'doc' => 'umask used when creating files (Unix-like systems only)',
  531. 'prompt' => 'Unix file mask',
  532. 'group' => 'Advanced',
  533. ),
  534. 'cache_ttl' => array(
  535. 'type' => 'integer',
  536. 'default' => PEAR_CONFIG_DEFAULT_CACHE_TTL,
  537. 'doc' => 'amount of secs where the local cache is used and not updated',
  538. 'prompt' => 'Cache TimeToLive',
  539. 'group' => 'Advanced',
  540. ),
  541. 'sig_type' => array(
  542. 'type' => 'set',
  543. 'default' => PEAR_CONFIG_DEFAULT_SIG_TYPE,
  544. 'doc' => 'which package signature mechanism to use',
  545. 'valid_set' => array('gpg'),
  546. 'prompt' => 'Package Signature Type',
  547. 'group' => 'Maintainers',
  548. ),
  549. 'sig_bin' => array(
  550. 'type' => 'string',
  551. 'default' => PEAR_CONFIG_DEFAULT_SIG_BIN,
  552. 'doc' => 'which package signature mechanism to use',
  553. 'prompt' => 'Signature Handling Program',
  554. 'group' => 'Maintainers',
  555. ),
  556. 'sig_keyid' => array(
  557. 'type' => 'string',
  558. 'default' => '',
  559. 'doc' => 'which key to use for signing with',
  560. 'prompt' => 'Signature Key Id',
  561. 'group' => 'Maintainers',
  562. ),
  563. 'sig_keydir' => array(
  564. 'type' => 'directory',
  565. 'default' => PEAR_CONFIG_DEFAULT_SIG_KEYDIR,
  566. 'doc' => 'directory where signature keys are located',
  567. 'prompt' => 'Signature Key Directory',
  568. 'group' => 'Maintainers',
  569. ),
  570. // __channels is reserved - used for channel-specific configuration
  571. );
  572. /**
  573. * Constructor.
  574. *
  575. * @param string file to read user-defined options from
  576. * @param string file to read system-wide defaults from
  577. * @param bool determines whether a registry object "follows"
  578. * the value of php_dir (is automatically created
  579. * and moved when php_dir is changed)
  580. * @param bool if true, fails if configuration files cannot be loaded
  581. *
  582. * @access public
  583. *
  584. * @see PEAR_Config::singleton
  585. */
  586. function __construct($user_file = '', $system_file = '', $ftp_file = false,
  587. $strict = true)
  588. {
  589. parent::__construct();
  590. PEAR_Installer_Role::initializeConfig($this);
  591. $sl = DIRECTORY_SEPARATOR;
  592. if (empty($user_file)) {
  593. if (OS_WINDOWS) {
  594. $user_file = PEAR_CONFIG_SYSCONFDIR . $sl . 'pear.ini';
  595. } else {
  596. $user_file = getenv('HOME') . $sl . '.pearrc';
  597. }
  598. }
  599. if (empty($system_file)) {
  600. $system_file = PEAR_CONFIG_SYSCONFDIR . $sl;
  601. if (OS_WINDOWS) {
  602. $system_file .= 'pearsys.ini';
  603. } else {
  604. $system_file .= 'pear.conf';
  605. }
  606. }
  607. $this->layers = array_keys($this->configuration);
  608. $this->files['user'] = $user_file;
  609. $this->files['system'] = $system_file;
  610. if ($user_file && file_exists($user_file)) {
  611. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  612. $this->readConfigFile($user_file, 'user', $strict);
  613. $this->popErrorHandling();
  614. if ($this->_errorsFound > 0) {
  615. return;
  616. }
  617. }
  618. if ($system_file && @file_exists($system_file)) {
  619. $this->mergeConfigFile($system_file, false, 'system', $strict);
  620. if ($this->_errorsFound > 0) {
  621. return;
  622. }
  623. }
  624. if (!$ftp_file) {
  625. $ftp_file = $this->get('remote_config');
  626. }
  627. if ($ftp_file && defined('PEAR_REMOTEINSTALL_OK')) {
  628. $this->readFTPConfigFile($ftp_file);
  629. }
  630. foreach ($this->configuration_info as $key => $info) {
  631. $this->configuration['default'][$key] = $info['default'];
  632. }
  633. $this->_registry['default'] = new PEAR_Registry(
  634. $this->configuration['default']['php_dir'], false, false,
  635. $this->configuration['default']['metadata_dir']);
  636. $this->_registry['default']->setConfig($this, false);
  637. $this->_regInitialized['default'] = false;
  638. //$GLOBALS['_PEAR_Config_instance'] = &$this;
  639. }
  640. /**
  641. * Return the default locations of user and system configuration files
  642. */
  643. public static function getDefaultConfigFiles()
  644. {
  645. $sl = DIRECTORY_SEPARATOR;
  646. if (OS_WINDOWS) {
  647. return array(
  648. 'user' => PEAR_CONFIG_SYSCONFDIR . $sl . 'pear.ini',
  649. 'system' => PEAR_CONFIG_SYSCONFDIR . $sl . 'pearsys.ini'
  650. );
  651. }
  652. return array(
  653. 'user' => getenv('HOME') . $sl . '.pearrc',
  654. 'system' => PEAR_CONFIG_SYSCONFDIR . $sl . 'pear.conf'
  655. );
  656. }
  657. /**
  658. * Static singleton method. If you want to keep only one instance
  659. * of this class in use, this method will give you a reference to
  660. * the last created PEAR_Config object if one exists, or create a
  661. * new object.
  662. *
  663. * @param string (optional) file to read user-defined options from
  664. * @param string (optional) file to read system-wide defaults from
  665. *
  666. * @return object an existing or new PEAR_Config instance
  667. *
  668. * @see PEAR_Config::PEAR_Config
  669. */
  670. public static function &singleton($user_file = '', $system_file = '', $strict = true)
  671. {
  672. if (is_object($GLOBALS['_PEAR_Config_instance'])) {
  673. return $GLOBALS['_PEAR_Config_instance'];
  674. }
  675. $t_conf = new PEAR_Config($user_file, $system_file, false, $strict);
  676. if ($t_conf->_errorsFound > 0) {
  677. return $t_conf->lastError;
  678. }
  679. $GLOBALS['_PEAR_Config_instance'] = &$t_conf;
  680. return $GLOBALS['_PEAR_Config_instance'];
  681. }
  682. /**
  683. * Determine whether any configuration files have been detected, and whether a
  684. * registry object can be retrieved from this configuration.
  685. * @return bool
  686. * @since PEAR 1.4.0a1
  687. */
  688. function validConfiguration()
  689. {
  690. if ($this->isDefinedLayer('user') || $this->isDefinedLayer('system')) {
  691. return true;
  692. }
  693. return false;
  694. }
  695. /**
  696. * Reads configuration data from a file. All existing values in
  697. * the config layer are discarded and replaced with data from the
  698. * file.
  699. * @param string file to read from, if NULL or not specified, the
  700. * last-used file for the same layer (second param) is used
  701. * @param string config layer to insert data into ('user' or 'system')
  702. * @return bool TRUE on success or a PEAR error on failure
  703. */
  704. function readConfigFile($file = null, $layer = 'user', $strict = true)
  705. {
  706. if (empty($this->files[$layer])) {
  707. return $this->raiseError("unknown config layer `$layer'");
  708. }
  709. if ($file === null) {
  710. $file = $this->files[$layer];
  711. }
  712. $data = $this->_readConfigDataFrom($file);
  713. if (PEAR::isError($data)) {
  714. if (!$strict) {
  715. return true;
  716. }
  717. $this->_errorsFound++;
  718. $this->lastError = $data;
  719. return $data;
  720. }
  721. $this->files[$layer] = $file;
  722. $this->_decodeInput($data);
  723. $this->configuration[$layer] = $data;
  724. $this->_setupChannels();
  725. if (!$this->_noRegistry && ($phpdir = $this->get('php_dir', $layer, 'pear.php.net'))) {
  726. $this->_registry[$layer] = new PEAR_Registry(
  727. $phpdir, false, false,
  728. $this->get('metadata_dir', $layer, 'pear.php.net'));
  729. $this->_registry[$layer]->setConfig($this, false);
  730. $this->_regInitialized[$layer] = false;
  731. } else {
  732. unset($this->_registry[$layer]);
  733. }
  734. return true;
  735. }
  736. /**
  737. * @param string url to the remote config file, like ftp://www.example.com/pear/config.ini
  738. * @return true|PEAR_Error
  739. */
  740. function readFTPConfigFile($path)
  741. {
  742. do { // poor man's try
  743. if (!class_exists('PEAR_FTP')) {
  744. if (!class_exists('PEAR_Common')) {
  745. require_once 'PEAR/Common.php';
  746. }
  747. if (PEAR_Common::isIncludeable('PEAR/FTP.php')) {
  748. require_once 'PEAR/FTP.php';
  749. }
  750. }
  751. if (!class_exists('PEAR_FTP')) {
  752. return PEAR::raiseError('PEAR_RemoteInstaller must be installed to use remote config');
  753. }
  754. $this->_ftp = new PEAR_FTP;
  755. $this->_ftp->pushErrorHandling(PEAR_ERROR_RETURN);
  756. $e = $this->_ftp->init($path);
  757. if (PEAR::isError($e)) {
  758. $this->_ftp->popErrorHandling();
  759. return $e;
  760. }
  761. $tmp = System::mktemp('-d');
  762. PEAR_Common::addTempFile($tmp);
  763. $e = $this->_ftp->get(basename($path), $tmp . DIRECTORY_SEPARATOR .
  764. 'pear.ini', false, FTP_BINARY);
  765. if (PEAR::isError($e)) {
  766. $this->_ftp->popErrorHandling();
  767. return $e;
  768. }
  769. PEAR_Common::addTempFile($tmp . DIRECTORY_SEPARATOR . 'pear.ini');
  770. $this->_ftp->disconnect();
  771. $this->_ftp->popErrorHandling();
  772. $this->files['ftp'] = $tmp . DIRECTORY_SEPARATOR . 'pear.ini';
  773. $e = $this->readConfigFile(null, 'ftp');
  774. if (PEAR::isError($e)) {
  775. return $e;
  776. }
  777. $fail = array();
  778. foreach ($this->configuration_info as $key => $val) {
  779. if (in_array($this->getGroup($key),
  780. array('File Locations', 'File Locations (Advanced)')) &&
  781. $this->getType($key) == 'directory') {
  782. // any directory configs must be set for this to work
  783. if (!isset($this->configuration['ftp'][$key])) {
  784. $fail[] = $key;
  785. }
  786. }
  787. }
  788. if (!count($fail)) {
  789. return true;
  790. }
  791. $fail = '"' . implode('", "', $fail) . '"';
  792. unset($this->files['ftp']);
  793. unset($this->configuration['ftp']);
  794. return PEAR::raiseError('ERROR: Ftp configuration file must set all ' .
  795. 'directory configuration variables. These variables were not set: ' .
  796. $fail);
  797. } while (false); // poor man's catch
  798. unset($this->files['ftp']);
  799. return PEAR::raiseError('no remote host specified');
  800. }
  801. /**
  802. * Reads the existing configurations and creates the _channels array from it
  803. */
  804. function _setupChannels()
  805. {
  806. $set = array_flip(array_values($this->_channels));
  807. foreach ($this->configuration as $layer => $data) {
  808. $i = 1000;
  809. if (isset($data['__channels']) && is_array($data['__channels'])) {
  810. foreach ($data['__channels'] as $channel => $info) {
  811. $set[$channel] = $i++;
  812. }
  813. }
  814. }
  815. $this->_channels = array_values(array_flip($set));
  816. $this->setChannels($this->_channels);
  817. }
  818. function deleteChannel($channel)
  819. {
  820. $ch = strtolower($channel);
  821. foreach ($this->configuration as $layer => $data) {
  822. if (isset($data['__channels']) && isset($data['__channels'][$ch])) {
  823. unset($this->configuration[$layer]['__channels'][$ch]);
  824. }
  825. }
  826. $this->_channels = array_flip($this->_channels);
  827. unset($this->_channels[$ch]);
  828. $this->_channels = array_flip($this->_channels);
  829. }
  830. /**
  831. * Merges data into a config layer from a file. Does the same
  832. * thing as readConfigFile, except it does not replace all
  833. * existing values in the config layer.
  834. * @param string file to read from
  835. * @param bool whether to overwrite existing data (default TRUE)
  836. * @param string config layer to insert data into ('user' or 'system')
  837. * @param string if true, errors are returned if file opening fails
  838. * @return bool TRUE on success or a PEAR error on failure
  839. */
  840. function mergeConfigFile($file, $override = true, $layer = 'user', $strict = true)
  841. {
  842. if (empty($this->files[$layer])) {
  843. return $this->raiseError("unknown config layer `$layer'");
  844. }
  845. if ($file === null) {
  846. $file = $this->files[$layer];
  847. }
  848. $data = $this->_readConfigDataFrom($file);
  849. if (PEAR::isError($data)) {
  850. if (!$strict) {
  851. return true;
  852. }
  853. $this->_errorsFound++;
  854. $this->lastError = $data;
  855. return $data;
  856. }
  857. $this->_decodeInput($data);
  858. if ($override) {
  859. $this->configuration[$layer] =
  860. PEAR_Config::arrayMergeRecursive($this->configuration[$layer], $data);
  861. } else {
  862. $this->configuration[$layer] =
  863. PEAR_Config::arrayMergeRecursive($data, $this->configuration[$layer]);
  864. }
  865. $this->_setupChannels();
  866. if (!$this->_noRegistry && ($phpdir = $this->get('php_dir', $layer, 'pear.php.net'))) {
  867. $this->_registry[$layer] = new PEAR_Registry(
  868. $phpdir, false, false,
  869. $this->get('metadata_dir', $layer, 'pear.php.net'));
  870. $this->_registry[$layer]->setConfig($this, false);
  871. $this->_regInitialized[$layer] = false;
  872. } else {
  873. unset($this->_registry[$layer]);
  874. }
  875. return true;
  876. }
  877. /**
  878. * @param array
  879. * @param array
  880. * @return array
  881. */
  882. public static function arrayMergeRecursive($arr2, $arr1)
  883. {
  884. $ret = array();
  885. foreach ($arr2 as $key => $data) {
  886. if (!isset($arr1[$key])) {
  887. $ret[$key] = $data;
  888. unset($arr1[$key]);
  889. continue;
  890. }
  891. if (is_array($data)) {
  892. if (!is_array($arr1[$key])) {
  893. $ret[$key] = $arr1[$key];
  894. unset($arr1[$key]);
  895. continue;
  896. }
  897. $ret[$key] = PEAR_Config::arrayMergeRecursive($arr1[$key], $arr2[$key]);
  898. unset($arr1[$key]);
  899. }
  900. }
  901. return array_merge($ret, $arr1);
  902. }
  903. /**
  904. * Writes data into a config layer from a file.
  905. *
  906. * @param string|null file to read from, or null for default
  907. * @param string config layer to insert data into ('user' or
  908. * 'system')
  909. * @param string|null data to write to config file or null for internal data [DEPRECATED]
  910. * @return bool TRUE on success or a PEAR error on failure
  911. */
  912. function writeConfigFile($file = null, $layer = 'user', $data = null)
  913. {
  914. $this->_lazyChannelSetup($layer);
  915. if ($layer == 'both' || $layer == 'all') {
  916. foreach ($this->files as $type => $file) {
  917. $err = $this->writeConfigFile($file, $type, $data);
  918. if (PEAR::isError($err)) {
  919. return $err;
  920. }
  921. }
  922. return true;
  923. }
  924. if (empty($this->files[$layer])) {
  925. return $this->raiseError("unknown config file type `$layer'");
  926. }
  927. if ($file === null) {
  928. $file = $this->files[$layer];
  929. }
  930. $data = ($data === null) ? $this->configuration[$layer] : $data;
  931. $this->_encodeOutput($data);
  932. $opt = array('-p', dirname($file));
  933. if (!@System::mkDir($opt)) {
  934. return $this->raiseError("could not create directory: " . dirname($file));
  935. }
  936. if (file_exists($file) && is_file($file) && !is_writeable($file)) {
  937. return $this->raiseError("no write access to $file!");
  938. }
  939. $fp = @fopen($file, "w");
  940. if (!$fp) {
  941. return $this->raiseError("PEAR_Config::writeConfigFile fopen('$file','w') failed ($php_errormsg)");
  942. }
  943. $contents = "#PEAR_Config 0.9\n" . serialize($data);
  944. if (!@fwrite($fp, $contents)) {
  945. return $this->raiseError("PEAR_Config::writeConfigFile: fwrite failed ($php_errormsg)");
  946. }
  947. return true;
  948. }
  949. /**
  950. * Reads configuration data from a file and returns the parsed data
  951. * in an array.
  952. *
  953. * @param string file to read from
  954. * @return array configuration data or a PEAR error on failure
  955. * @access private
  956. */
  957. function _readConfigDataFrom($file)
  958. {
  959. $fp = false;
  960. if (file_exists($file)) {
  961. $fp = @fopen($file, "r");
  962. }
  963. if (!$fp) {
  964. return $this->raiseError("PEAR_Config::readConfigFile fopen('$file','r') failed");
  965. }
  966. $size = filesize($file);
  967. fclose($fp);
  968. $contents = file_get_contents($file);
  969. if (empty($contents)) {
  970. return $this->raiseError('Configuration file "' . $file . '" is empty');
  971. }
  972. $version = false;
  973. if (preg_match('/^#PEAR_Config\s+(\S+)\s+/si', $contents, $matches)) {
  974. $version = $matches[1];
  975. $contents = substr($contents, strlen($matches[0]));
  976. } else {
  977. // Museum config file
  978. if (substr($contents,0,2) == 'a:') {
  979. $version = '0.1';
  980. }
  981. }
  982. if ($version && version_compare("$version", '1', '<')) {
  983. // no '@', it is possible that unserialize
  984. // raises a notice but it seems to block IO to
  985. // STDOUT if a '@' is used and a notice is raise
  986. $data = unserialize($contents);
  987. if (!is_array($data) && !$data) {
  988. if ($contents == serialize(false)) {
  989. $data = array();
  990. } else {
  991. $err = $this->raiseError("PEAR_Config: bad data in $file");
  992. return $err;
  993. }
  994. }
  995. if (!is_array($data)) {
  996. if (strlen(trim($contents)) > 0) {
  997. $error = "PEAR_Config: bad data in $file";
  998. $err = $this->raiseError($error);
  999. return $err;
  1000. }
  1001. $data = array();
  1002. }
  1003. // add parsing of newer formats here...
  1004. } else {
  1005. $err = $this->raiseError("$file: unknown version `$version'");
  1006. return $err;
  1007. }
  1008. return $data;
  1009. }
  1010. /**
  1011. * Gets the file used for storing the config for a layer
  1012. *
  1013. * @param string $layer 'user' or 'system'
  1014. */
  1015. function getConfFile($layer)
  1016. {
  1017. return $this->files[$layer];
  1018. }
  1019. /**
  1020. * @param string Configuration class name, used for detecting duplicate calls
  1021. * @param array information on a role as parsed from its xml file
  1022. * @return true|PEAR_Error
  1023. * @access private
  1024. */
  1025. function _addConfigVars($class, $vars)
  1026. {
  1027. static $called = array();
  1028. if (isset($called[$class])) {
  1029. return;
  1030. }
  1031. $called[$class] = 1;
  1032. if (count($vars) > 3) {
  1033. return $this->raiseError('Roles can only define 3 new config variables or less');
  1034. }
  1035. foreach ($vars as $name => $var) {
  1036. if (!is_array($var)) {
  1037. return $this->raiseError('Configuration information must be an array');
  1038. }
  1039. if (!isset($var['type'])) {
  1040. return $this->raiseError('Configuration information must contain a type');
  1041. } elseif (!in_array($var['type'],
  1042. array('string', 'mask', 'password', 'directory', 'file', 'set'))) {
  1043. return $this->raiseError(
  1044. 'Configuration type must be one of directory, file, string, ' .
  1045. 'mask, set, or password');
  1046. }
  1047. if (!isset($var['default'])) {
  1048. return $this->raiseError(
  1049. 'Configuration information must contain a default value ("default" index)');
  1050. }
  1051. if (is_array($var['default'])) {
  1052. $real_default = '';
  1053. foreach ($var['default'] as $config_var => $val) {
  1054. if (strpos($config_var, 'text') === 0) {
  1055. $real_default .= $val;
  1056. } elseif (strpos($config_var, 'constant') === 0) {
  1057. if (!defined($val)) {
  1058. return $this->raiseError(
  1059. 'Unknown constant "' . $val . '" requested in ' .
  1060. 'default value for configuration variable "' .
  1061. $name . '"');
  1062. }
  1063. $real_default .= constant($val);
  1064. } elseif (isset($this->configuration_info[$config_var])) {
  1065. $real_default .=
  1066. $this->configuration_info[$config_var]['default'];
  1067. } else {
  1068. return $this->raiseError(
  1069. 'Unknown request for "' . $config_var . '" value in ' .
  1070. 'default value for configuration variable "' .
  1071. $name . '"');
  1072. }
  1073. }
  1074. $var['default'] = $real_default;
  1075. }
  1076. if ($var['type'] == 'integer') {
  1077. $var['default'] = (integer) $var['default'];
  1078. }
  1079. if (!isset($var['doc'])) {
  1080. return $this->raiseError(
  1081. 'Configuration information must contain a summary ("doc" index)');
  1082. }
  1083. if (!isset($var['prompt'])) {
  1084. return $this->raiseError(
  1085. 'Configuration information must contain a simple prompt ("prompt" index)');
  1086. }
  1087. if (!isset($var['group'])) {
  1088. return $this->raiseError(
  1089. 'Configuration information must contain a simple group ("group" index)');
  1090. }
  1091. if (isset($this->configuration_info[$name])) {
  1092. return $this->raiseError('Configuration variable "' . $name .
  1093. '" already exists');
  1094. }
  1095. $this->configuration_info[$name] = $var;
  1096. // fix bug #7351: setting custom config variable in a channel fails
  1097. $this->_channelConfigInfo[] = $name;
  1098. }
  1099. return true;
  1100. }
  1101. /**
  1102. * Encodes/scrambles configuration data before writing to files.
  1103. * Currently, 'password' values will be base64-encoded as to avoid
  1104. * that people spot cleartext passwords by accident.
  1105. *
  1106. * @param array (reference) array to encode values in
  1107. * @return bool TRUE on success
  1108. * @access private
  1109. */
  1110. function _encodeOutput(&$data)
  1111. {
  1112. foreach ($data as $key => $value) {
  1113. if ($key == '__channels') {
  1114. foreach ($data['__channels'] as $channel => $blah) {
  1115. $this->_encodeOutput($data['__channels'][$channel]);
  1116. }
  1117. }
  1118. if (!isset($this->configuration_info[$key])) {
  1119. continue;
  1120. }
  1121. $type = $this->configuration_info[$key]['type'];
  1122. switch ($type) {
  1123. // we base64-encode passwords so they are at least
  1124. // not shown in plain by accident
  1125. case 'password': {
  1126. $data[$key] = base64_encode($data[$key]);
  1127. break;
  1128. }
  1129. case 'mask': {
  1130. $data[$key] = octdec($data[$key]);
  1131. break;
  1132. }
  1133. }
  1134. }
  1135. return true;
  1136. }
  1137. /**
  1138. * Decodes/unscrambles configuration data after reading from files.
  1139. *
  1140. * @param array (reference) array to encode values in
  1141. * @return bool TRUE on success
  1142. * @access private
  1143. *
  1144. * @see PEAR_Config::_encodeOutput
  1145. */
  1146. function _decodeInput(&$data)
  1147. {
  1148. if (!is_array($data)) {
  1149. return true;
  1150. }
  1151. foreach ($data as $key => $value) {
  1152. if ($key == '__channels') {
  1153. foreach ($data['__channels'] as $channel => $blah) {
  1154. $this->_decodeInput($data['__channels'][$channel]);
  1155. }
  1156. }
  1157. if (!isset($this->configuration_info[$key])) {
  1158. continue;
  1159. }
  1160. $type = $this->configuration_info[$key]['type'];
  1161. switch ($type) {
  1162. case 'password': {
  1163. $data[$key] = base64_decode($data[$key]);
  1164. break;
  1165. }
  1166. case 'mask': {
  1167. $data[$key] = decoct($data[$key]);
  1168. break;
  1169. }
  1170. }
  1171. }
  1172. return true;
  1173. }
  1174. /**
  1175. * Retrieve the default channel.
  1176. *
  1177. * On startup, channels are not initialized, so if the default channel is not
  1178. * pear.php.net, then initialize the config.
  1179. * @param string registry layer
  1180. * @return string|false
  1181. */
  1182. function getDefaultChannel($layer = null)
  1183. {
  1184. $ret = false;
  1185. if ($layer === null) {
  1186. foreach ($this->layers as $layer) {
  1187. if (isset($this->configuration[$layer]['default_channel'])) {
  1188. $ret = $this->configuration[$layer]['default_channel'];
  1189. break;
  1190. }
  1191. }
  1192. } elseif (isset($this->configuration[$layer]['default_channel'])) {
  1193. $ret = $this->configuration[$layer]['default_channel'];
  1194. }
  1195. if ($ret == 'pear.php.net' && defined('PEAR_RUNTYPE') && PEAR_RUNTYPE == 'pecl') {
  1196. $ret = 'pecl.php.net';
  1197. }
  1198. if ($ret) {
  1199. if ($ret != 'pear.php.net') {
  1200. $this->_lazyChannelSetup();
  1201. }
  1202. return $ret;
  1203. }
  1204. return PEAR_CONFIG_DEFAULT_CHANNEL;
  1205. }
  1206. /**
  1207. * Returns a configuration value, prioritizing layers as per the
  1208. * layers property.
  1209. *
  1210. * @param string config key
  1211. * @return mixed the config value, or NULL if not found
  1212. * @access public
  1213. */
  1214. function get($key, $layer = null, $channel = false)
  1215. {
  1216. if (!isset($this->configuration_info[$key])) {
  1217. return null;
  1218. }
  1219. if ($key == '__channels') {
  1220. return null;
  1221. }
  1222. if ($key == 'default_channel') {
  1223. return $this->getDefaultChannel($layer);
  1224. }
  1225. if (!$channel) {
  1226. $channel = $this->getDefaultChannel();
  1227. } elseif ($channel != 'pear.php.net') {
  1228. $this->_lazyChannelSetup();
  1229. }
  1230. $channel = strtolower($channel);
  1231. $test = (in_array($key, $this->_channelConfigInfo)) ?
  1232. $this->_getChannelValue($key, $layer, $channel) :
  1233. null;
  1234. if ($test !== null) {
  1235. if ($this->_installRoot) {
  1236. if (in_array($this->getGroup($key),
  1237. array('File Locations', 'File Locations (Advanced)')) &&
  1238. $this->getType($key) == 'directory') {
  1239. return $this->_prependPath($test, $this->_installRoot);
  1240. }
  1241. }
  1242. return $test;
  1243. }
  1244. if ($layer === null) {
  1245. foreach ($this->layers as $layer) {
  1246. if (isset($this->configuration[$layer][$key])) {
  1247. $test = $this->configuration[$layer][$key];
  1248. if ($this->_installRoot) {
  1249. if (in_array($this->getGroup($key),
  1250. array('File Locations', 'File Locations (Advanced)')) &&
  1251. $this->getType($key) == 'directory') {
  1252. return $this->_prependPath($test, $this->_installRoot);
  1253. }
  1254. }
  1255. if ($key == 'preferred_mirror') {
  1256. $reg = &$this->getRegistry();
  1257. if (is_object($reg)) {
  1258. $chan = $reg->getChannel($channel);
  1259. if (PEAR::isError($chan)) {
  1260. return $channel;
  1261. }
  1262. if (!$chan->getMirror($test) && $chan->getName() != $test) {
  1263. return $channel; // mirror does not exist
  1264. }
  1265. }
  1266. }
  1267. return $test;
  1268. }
  1269. }
  1270. } elseif (isset($this->configuration[$layer][$key])) {
  1271. $test = $this->configuration[$layer][$key];
  1272. if ($this->_installRoot) {
  1273. if (in_array($this->getGroup($key),
  1274. array('File Locations', 'File Locations (Advanced)')) &&
  1275. $this->getType($key) == 'directory') {
  1276. return $this->_prependPath($test, $this->_installRoot);
  1277. }
  1278. }
  1279. if ($key == 'preferred_mirror') {
  1280. $reg = &$this->getRegistry();
  1281. if (is_object($reg)) {
  1282. $chan = $reg->getChannel($channel);
  1283. if (PEAR::isError($chan)) {
  1284. return $channel;
  1285. }
  1286. if (!$chan->getMirror($test) && $chan->getName() != $test) {
  1287. return $channel; // mirror does not exist
  1288. }
  1289. }
  1290. }
  1291. return $test;
  1292. }
  1293. return null;
  1294. }
  1295. /**
  1296. * Returns a channel-specific configuration value, prioritizing layers as per the
  1297. * layers property.
  1298. *
  1299. * @param string config key
  1300. * @return mixed the config value, or NULL if not found
  1301. * @access private
  1302. */
  1303. function _getChannelValue($key, $layer, $channel)
  1304. {
  1305. if ($key == '__channels' || $channel == 'pear.php.net') {
  1306. return null;
  1307. }
  1308. $ret = null;
  1309. if ($layer === null) {
  1310. foreach ($this->layers as $ilayer) {
  1311. if (isset($this->configuration[$ilayer]['__channels'][$channel][$key])) {
  1312. $ret = $this->configuration[$ilayer]['__channels'][$channel][$key];
  1313. break;
  1314. }
  1315. }
  1316. } elseif (isset($this->configuration[$layer]['__channels'][$channel][$key])) {
  1317. $ret = $this->configuration[$layer]['__channels'][$channel][$key];
  1318. }
  1319. if ($key != 'preferred_mirror') {
  1320. return $ret;
  1321. }
  1322. if ($ret !== null) {
  1323. $reg = &$this->getRegistry($layer);
  1324. if (is_object($reg)) {
  1325. $chan = $reg->getChannel($channel);
  1326. if (PEAR::isError($chan)) {
  1327. return $channel;
  1328. }
  1329. if (!$chan->getMirror($ret) && $chan->getName() != $ret) {
  1330. return $channel; // mirror does not exist
  1331. }
  1332. }
  1333. return $ret;
  1334. }
  1335. if ($channel != $this->getDefaultChannel($layer)) {
  1336. return $channel; // we must use the channel name as the preferred mirror
  1337. // if the user has not chosen an alternate
  1338. }
  1339. return $this->getDefaultChannel($layer);
  1340. }
  1341. /**
  1342. * Set a config value in a specific layer (defaults to 'user').
  1343. * Enforces the types defined in the configuration_info array. An
  1344. * integer config variable will be cast to int, and a set config
  1345. * variable will be validated against its legal values.
  1346. *
  1347. * @param string config key
  1348. * @param string config value
  1349. * @param string (optional) config layer
  1350. * @param string channel to set this value for, or null for global value
  1351. * @return bool TRUE on success, FALSE on failure
  1352. */
  1353. function set($key, $value, $layer = 'user', $channel = false)
  1354. {
  1355. if ($key == '__channels') {
  1356. return false;
  1357. }
  1358. if (!isset($this->configuration[$layer])) {
  1359. return false;
  1360. }
  1361. if ($key == 'default_channel') {
  1362. // can only set this value globally
  1363. $channel = 'pear.php.net';
  1364. if ($value != 'pear.php.net') {
  1365. $this->_lazyChannelSetup($layer);
  1366. }
  1367. }
  1368. if ($key == 'preferred_mirror') {
  1369. if ($channel == '__uri') {
  1370. return false; // can't set the __uri pseudo-channel's mirror
  1371. }
  1372. $reg = &$this->getRegistry($layer);
  1373. if (is_object($reg)) {
  1374. $chan = $reg->getChannel($channel ? $channel : 'pear.php.net');
  1375. if (PEAR::isError($chan)) {
  1376. return false;
  1377. }
  1378. if (!$chan->getMirror($value) && $chan->getName() != $value) {
  1379. return false; // mirror does not exist
  1380. }
  1381. }
  1382. }
  1383. if (!isset($this->configuration_info[$key])) {
  1384. return false;
  1385. }
  1386. extract($this->configuration_info[$key]);
  1387. switch ($type) {
  1388. case 'integer':
  1389. $value = (int)$value;
  1390. break;
  1391. case 'set': {
  1392. // If a valid_set is specified, require the value to
  1393. // be in the set. If there is no valid_set, accept
  1394. // any value.
  1395. if ($valid_set) {
  1396. reset($valid_set);
  1397. if ((key($valid_set) === 0 && !in_array($value, $valid_set)) ||
  1398. (key($valid_set) !== 0 && empty($valid_set[$value])))
  1399. {
  1400. return false;
  1401. }
  1402. }
  1403. break;
  1404. }
  1405. }
  1406. if (!$channel) {
  1407. $channel = $this->get('default_channel', null, 'pear.php.net');
  1408. }
  1409. if (!in_array($channel, $this->_channels)) {
  1410. $this->_lazyChannelSetup($layer);
  1411. $reg = &$this->getRegistry($layer);
  1412. if ($reg) {
  1413. $channel = $reg->channelName($channel);
  1414. }
  1415. if (!in_array($channel, $this->_channels)) {
  1416. return false;
  1417. }
  1418. }
  1419. if ($channel != 'pear.php.net') {
  1420. if (in_array($key, $this->_channelConfigInfo)) {
  1421. $this->configuration[$layer]['__channels'][$channel][$key] = $value;
  1422. return true;
  1423. }
  1424. return false;
  1425. }
  1426. if ($key == 'default_channel') {
  1427. if (!isset($reg)) {
  1428. $reg = &$this->getRegistry($layer);
  1429. if (!$reg) {
  1430. $reg = &$this->getRegistry();
  1431. }
  1432. }
  1433. if ($reg) {
  1434. $value = $reg->channelName($value);
  1435. }
  1436. if (!$value) {
  1437. return false;
  1438. }
  1439. }
  1440. $this->configuration[$layer][$key] = $value;
  1441. if ($key == 'php_dir' && !$this->_noRegistry) {
  1442. if (!isset($this->_registry[$layer]) ||
  1443. $value != $this->_registry[$layer]->install_dir) {
  1444. $this->_registry[$layer] = new PEAR_Registry($value);
  1445. $this->_regInitialized[$layer] = false;
  1446. $this->_registry[$layer]->setConfig($this, false);
  1447. }
  1448. }
  1449. return true;
  1450. }
  1451. function _lazyChannelSetup($uselayer = false)
  1452. {
  1453. if ($this->_noRegistry) {
  1454. return;
  1455. }
  1456. $merge = false;
  1457. foreach ($this->_registry as $layer => $p) {
  1458. if ($uselayer && $uselayer != $layer) {
  1459. continue;
  1460. }
  1461. if (!$this->_regInitialized[$layer]) {
  1462. if ($layer == 'default' && isset($this->_registry['user']) ||
  1463. isset($this->_registry['system'])) {
  1464. // only use the default registry if there are no alternatives
  1465. continue;
  1466. }
  1467. if (!is_object($this->_registry[$layer])) {
  1468. if ($phpdir = $this->get('php_dir', $layer, 'pear.php.net')) {
  1469. $this->_registry[$layer] = new PEAR_Registry(
  1470. $phpdir, false, false,
  1471. $this->get('metadata_dir', $layer, 'pear.php.net'));
  1472. $this->_registry[$layer]->setConfig($this, false);
  1473. $this->_regInitialized[$layer] = false;
  1474. } else {
  1475. unset($this->_registry[$layer]);
  1476. return;
  1477. }
  1478. }
  1479. $this->setChannels($this->_registry[$layer]->listChannels(), $merge);
  1480. $this->_regInitialized[$layer] = true;
  1481. $merge = true;
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * Set the list of channels.
  1487. *
  1488. * This should be set via a call to {@link PEAR_Registry::listChannels()}
  1489. * @param array
  1490. * @param bool
  1491. * @return bool success of operation
  1492. */
  1493. function setChannels($channels, $merge = false)
  1494. {
  1495. if (!is_array($channels)) {
  1496. return false;
  1497. }
  1498. if ($merge) {
  1499. $this->_channels = array_merge($this->_channels, $channels);
  1500. } else {
  1501. $this->_channels = $channels;
  1502. }
  1503. foreach ($channels as $channel) {
  1504. $channel = strtolower($channel);
  1505. if ($channel == 'pear.php.net') {
  1506. continue;
  1507. }
  1508. foreach ($this->layers as $layer) {
  1509. if (!isset($this->configuration[$layer]['__channels'])) {
  1510. $this->configuration[$layer]['__channels'] = array();
  1511. }
  1512. if (!isset($this->configuration[$layer]['__channels'][$channel])
  1513. || !is_array($this->configuration[$layer]['__channels'][$channel])) {
  1514. $this->configuration[$layer]['__channels'][$channel] = array();
  1515. }
  1516. }
  1517. }
  1518. return true;
  1519. }
  1520. /**
  1521. * Get the type of a config value.
  1522. *
  1523. * @param string config key
  1524. *
  1525. * @return string type, one of "string", "integer", "file",
  1526. * "directory", "set" or "password".
  1527. *
  1528. * @access public
  1529. *
  1530. */
  1531. function getType($key)
  1532. {
  1533. if (isset($this->configuration_info[$key])) {
  1534. return $this->configuration_info[$key]['type'];
  1535. }
  1536. return false;
  1537. }
  1538. /**
  1539. * Get the documentation for a config value.
  1540. *
  1541. * @param string config key
  1542. * @return string documentation string
  1543. *
  1544. * @access public
  1545. *
  1546. */
  1547. function getDocs($key)
  1548. {
  1549. if (isset($this->configuration_info[$key])) {
  1550. return $this->configuration_info[$key]['doc'];
  1551. }
  1552. return false;
  1553. }
  1554. /**
  1555. * Get the short documentation for a config value.
  1556. *
  1557. * @param string config key
  1558. * @return string short documentation string
  1559. *
  1560. * @access public
  1561. *
  1562. */
  1563. function getPrompt($key)
  1564. {
  1565. if (isset($this->configuration_info[$key])) {
  1566. return $this->configuration_info[$key]['prompt'];
  1567. }
  1568. return false;
  1569. }
  1570. /**
  1571. * Get the parameter group for a config key.
  1572. *
  1573. * @param string config key
  1574. * @return string parameter group
  1575. *
  1576. * @access public
  1577. *
  1578. */
  1579. function getGroup($key)
  1580. {
  1581. if (isset($this->configuration_info[$key])) {
  1582. return $this->configuration_info[$key]['group'];
  1583. }
  1584. return false;
  1585. }
  1586. /**
  1587. * Get the list of parameter groups.
  1588. *
  1589. * @return array list of parameter groups
  1590. *
  1591. * @access public
  1592. *
  1593. */
  1594. function getGroups()
  1595. {
  1596. $tmp = array();
  1597. foreach ($this->configuration_info as $key => $info) {
  1598. $tmp[$info['group']] = 1;
  1599. }
  1600. return array_keys($tmp);
  1601. }
  1602. /**
  1603. * Get the list of the parameters in a group.
  1604. *
  1605. * @param string $group parameter group
  1606. * @return array list of parameters in $group
  1607. *
  1608. * @access public
  1609. *
  1610. */
  1611. function getGroupKeys($group)
  1612. {
  1613. $keys = array();
  1614. foreach ($this->configuration_info as $key => $info) {
  1615. if ($info['group'] == $group) {
  1616. $keys[] = $key;
  1617. }
  1618. }
  1619. return $keys;
  1620. }
  1621. /**
  1622. * Get the list of allowed set values for a config value. Returns
  1623. * NULL for config values that are not sets.
  1624. *
  1625. * @param string config key
  1626. * @return array enumerated array of set values, or NULL if the
  1627. * config key is unknown or not a set
  1628. *
  1629. * @access public
  1630. *
  1631. */
  1632. function getSetValues($key)
  1633. {
  1634. if (isset($this->configuration_info[$key]) &&
  1635. isset($this->configuration_info[$key]['type']) &&
  1636. $this->configuration_info[$key]['type'] == 'set')
  1637. {
  1638. $valid_set = $this->configuration_info[$key]['valid_set'];
  1639. reset($valid_set);
  1640. if (key($valid_set) === 0) {
  1641. return $valid_set;
  1642. }
  1643. return array_keys($valid_set);
  1644. }
  1645. return null;
  1646. }
  1647. /**
  1648. * Get all the current config keys.
  1649. *
  1650. * @return array simple array of config keys
  1651. *
  1652. * @access public
  1653. */
  1654. function getKeys()
  1655. {
  1656. $keys = array();
  1657. foreach ($this->layers as $layer) {
  1658. $test = $this->configuration[$layer];
  1659. if (isset($test['__channels'])) {
  1660. foreach ($test['__channels'] as $channel => $configs) {
  1661. $keys = array_merge($keys, $configs);
  1662. }
  1663. }
  1664. unset($test['__channels']);
  1665. $keys = array_merge($keys, $test);
  1666. }
  1667. return array_keys($keys);
  1668. }
  1669. /**
  1670. * Remove the a config key from a specific config layer.
  1671. *
  1672. * @param string config key
  1673. * @param string (optional) config layer
  1674. * @param string (optional) channel (defaults to default channel)
  1675. * @return bool TRUE on success, FALSE on failure
  1676. *
  1677. * @access public
  1678. */
  1679. function remove($key, $layer = 'user', $channel = null)
  1680. {
  1681. if ($channel === null) {
  1682. $channel = $this->getDefaultChannel();
  1683. }
  1684. if ($channel !== 'pear.php.net') {
  1685. if (isset($this->configuration[$layer]['__channels'][$channel][$key])) {
  1686. unset($this->configuration[$layer]['__channels'][$channel][$key]);
  1687. return true;
  1688. }
  1689. }
  1690. if (isset($this->configuration[$layer][$key])) {
  1691. unset($this->configuration[$layer][$key]);
  1692. return true;
  1693. }
  1694. return false;
  1695. }
  1696. /**
  1697. * Temporarily remove an entire config layer. USE WITH CARE!
  1698. *
  1699. * @param string config key
  1700. * @param string (optional) config layer
  1701. * @return bool TRUE on success, FALSE on failure
  1702. *
  1703. * @access public
  1704. */
  1705. function removeLayer($layer)
  1706. {
  1707. if (isset($this->configuration[$layer])) {
  1708. $this->configuration[$layer] = array();
  1709. return true;
  1710. }
  1711. return false;
  1712. }
  1713. /**
  1714. * Stores configuration data in a layer.
  1715. *
  1716. * @param string config layer to store
  1717. * @return bool TRUE on success, or PEAR error on failure
  1718. *
  1719. * @access public
  1720. */
  1721. function store($layer = 'user', $data = null)
  1722. {
  1723. return $this->writeConfigFile(null, $layer, $data);
  1724. }
  1725. /**
  1726. * Tells what config layer that gets to define a key.
  1727. *
  1728. * @param string config key
  1729. * @param boolean return the defining channel
  1730. *
  1731. * @return string|array the config layer, or an empty string if not found.
  1732. *
  1733. * if $returnchannel, the return is an array array('layer' => layername,
  1734. * 'channel' => channelname), or an empty string if not found
  1735. *
  1736. * @access public
  1737. */
  1738. function definedBy($key, $returnchannel = false)
  1739. {
  1740. foreach ($this->layers as $layer) {
  1741. $channel = $this->getDefaultChannel();
  1742. if ($channel !== 'pear.php.net') {
  1743. if (isset($this->configuration[$layer]['__channels'][$channel][$key])) {
  1744. if ($returnchannel) {
  1745. return array('layer' => $layer, 'channel' => $channel);
  1746. }
  1747. return $layer;
  1748. }
  1749. }
  1750. if (isset($this->configuration[$layer][$key])) {
  1751. if ($returnchannel) {
  1752. return array('layer' => $layer, 'channel' => 'pear.php.net');
  1753. }
  1754. return $layer;
  1755. }
  1756. }
  1757. return '';
  1758. }
  1759. /**
  1760. * Tells whether a given key exists as a config value.
  1761. *
  1762. * @param string config key
  1763. * @return bool whether <config key> exists in this object
  1764. *
  1765. * @access public
  1766. */
  1767. function isDefined($key)
  1768. {
  1769. foreach ($this->layers as $layer) {
  1770. if (isset($this->configuration[$layer][$key])) {
  1771. return true;
  1772. }
  1773. }
  1774. return false;
  1775. }
  1776. /**
  1777. * Tells whether a given config layer exists.
  1778. *
  1779. * @param string config layer
  1780. * @return bool whether <config layer> exists in this object
  1781. *
  1782. * @access public
  1783. */
  1784. function isDefinedLayer($layer)
  1785. {
  1786. return isset($this->configuration[$layer]);
  1787. }
  1788. /**
  1789. * Returns the layers defined (except the 'default' one)
  1790. *
  1791. * @return array of the defined layers
  1792. */
  1793. function getLayers()
  1794. {
  1795. $cf = $this->configuration;
  1796. unset($cf['default']);
  1797. return array_keys($cf);
  1798. }
  1799. function apiVersion()
  1800. {
  1801. return '1.1';
  1802. }
  1803. /**
  1804. * @return PEAR_Registry
  1805. */
  1806. function &getRegistry($use = null)
  1807. {
  1808. $layer = $use === null ? 'user' : $use;
  1809. if (isset($this->_registry[$layer])) {
  1810. return $this->_registry[$layer];
  1811. } elseif ($use === null && isset($this->_registry['system'])) {
  1812. return $this->_registry['system'];
  1813. } elseif ($use === null && isset($this->_registry['default'])) {
  1814. return $this->_registry['default'];
  1815. } elseif ($use) {
  1816. $a = false;
  1817. return $a;
  1818. }
  1819. // only go here if null was passed in
  1820. echo "CRITICAL ERROR: Registry could not be initialized from any value";
  1821. exit(1);
  1822. }
  1823. /**
  1824. * This is to allow customization like the use of installroot
  1825. * @param PEAR_Registry
  1826. * @return bool
  1827. */
  1828. function setRegistry(&$reg, $layer = 'user')
  1829. {
  1830. if ($this->_noRegistry) {
  1831. return false;
  1832. }
  1833. if (!in_array($layer, array('user', 'system'))) {
  1834. return false;
  1835. }
  1836. $this->_registry[$layer] = &$reg;
  1837. if (is_object($reg)) {
  1838. $this->_registry[$layer]->setConfig($this, false);
  1839. }
  1840. return true;
  1841. }
  1842. function noRegistry()
  1843. {
  1844. $this->_noRegistry = true;
  1845. }
  1846. /**
  1847. * @return PEAR_REST
  1848. */
  1849. function &getREST($version, $options = array())
  1850. {
  1851. $version = str_replace('.', '', $version);
  1852. if (!class_exists($class = 'PEAR_REST_' . $version)) {
  1853. require_once 'PEAR/REST/' . $version . '.php';
  1854. }
  1855. $remote = new $class($this, $options);
  1856. return $remote;
  1857. }
  1858. /**
  1859. * The ftp server is set in {@link readFTPConfigFile()}. It exists only if a
  1860. * remote configuration file has been specified
  1861. * @return PEAR_FTP|false
  1862. */
  1863. function &getFTP()
  1864. {
  1865. if (isset($this->_ftp)) {
  1866. return $this->_ftp;
  1867. }
  1868. $a = false;
  1869. return $a;
  1870. }
  1871. static function _prependPath($path, $prepend)
  1872. {
  1873. if (strlen($prepend) > 0) {
  1874. if (OS_WINDOWS && preg_match('/^[a-z]:/i', $path)) {
  1875. if (preg_match('/^[a-z]:/i', $prepend)) {
  1876. $prepend = substr($prepend, 2);
  1877. } elseif ($prepend[0] != '\\') {
  1878. $prepend = "\\$prepend";
  1879. }
  1880. $path = substr($path, 0, 2) . $prepend . substr($path, 2);
  1881. } else {
  1882. $path = $prepend . $path;
  1883. }
  1884. }
  1885. return $path;
  1886. }
  1887. /**
  1888. * @param string|false installation directory to prepend to all _dir variables, or false to
  1889. * disable
  1890. */
  1891. function setInstallRoot($root)
  1892. {
  1893. if (substr($root, -1) == DIRECTORY_SEPARATOR) {
  1894. $root = substr($root, 0, -1);
  1895. }
  1896. $old = $this->_installRoot;
  1897. $this->_installRoot = $root;
  1898. if (($old != $root) && !$this->_noRegistry) {
  1899. foreach (array_keys($this->_registry) as $layer) {
  1900. if ($layer == 'ftp' || !isset($this->_registry[$layer])) {
  1901. continue;
  1902. }
  1903. $this->_registry[$layer] =
  1904. new PEAR_Registry(
  1905. $this->get('php_dir', $layer, 'pear.php.net'), false, false,
  1906. $this->get('metadata_dir', $layer, 'pear.php.net'));
  1907. $this->_registry[$layer]->setConfig($this, false);
  1908. $this->_regInitialized[$layer] = false;
  1909. }
  1910. }
  1911. }
  1912. }