Config.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. /**
  3. * PEAR_Command_Config (config-show, config-get, config-set, config-help, config-create commands)
  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. * @version CVS: $Id: Config.php 313024 2011-07-06 19:51:24Z dufuz $
  14. * @link http://pear.php.net/package/PEAR
  15. * @since File available since Release 0.1
  16. */
  17. /**
  18. * base class
  19. */
  20. require_once 'PEAR/Command/Common.php';
  21. /**
  22. * PEAR commands for managing configuration data.
  23. *
  24. * @category pear
  25. * @package PEAR
  26. * @author Stig Bakken <ssb@php.net>
  27. * @author Greg Beaver <cellog@php.net>
  28. * @copyright 1997-2009 The Authors
  29. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  30. * @version Release: 1.9.4
  31. * @link http://pear.php.net/package/PEAR
  32. * @since Class available since Release 0.1
  33. */
  34. class PEAR_Command_Config extends PEAR_Command_Common
  35. {
  36. var $commands = array(
  37. 'config-show' => array(
  38. 'summary' => 'Show All Settings',
  39. 'function' => 'doConfigShow',
  40. 'shortcut' => 'csh',
  41. 'options' => array(
  42. 'channel' => array(
  43. 'shortopt' => 'c',
  44. 'doc' => 'show configuration variables for another channel',
  45. 'arg' => 'CHAN',
  46. ),
  47. ),
  48. 'doc' => '[layer]
  49. Displays all configuration values. An optional argument
  50. may be used to tell which configuration layer to display. Valid
  51. configuration layers are "user", "system" and "default". To display
  52. configurations for different channels, set the default_channel
  53. configuration variable and run config-show again.
  54. ',
  55. ),
  56. 'config-get' => array(
  57. 'summary' => 'Show One Setting',
  58. 'function' => 'doConfigGet',
  59. 'shortcut' => 'cg',
  60. 'options' => array(
  61. 'channel' => array(
  62. 'shortopt' => 'c',
  63. 'doc' => 'show configuration variables for another channel',
  64. 'arg' => 'CHAN',
  65. ),
  66. ),
  67. 'doc' => '<parameter> [layer]
  68. Displays the value of one configuration parameter. The
  69. first argument is the name of the parameter, an optional second argument
  70. may be used to tell which configuration layer to look in. Valid configuration
  71. layers are "user", "system" and "default". If no layer is specified, a value
  72. will be picked from the first layer that defines the parameter, in the order
  73. just specified. The configuration value will be retrieved for the channel
  74. specified by the default_channel configuration variable.
  75. ',
  76. ),
  77. 'config-set' => array(
  78. 'summary' => 'Change Setting',
  79. 'function' => 'doConfigSet',
  80. 'shortcut' => 'cs',
  81. 'options' => array(
  82. 'channel' => array(
  83. 'shortopt' => 'c',
  84. 'doc' => 'show configuration variables for another channel',
  85. 'arg' => 'CHAN',
  86. ),
  87. ),
  88. 'doc' => '<parameter> <value> [layer]
  89. Sets the value of one configuration parameter. The first argument is
  90. the name of the parameter, the second argument is the new value. Some
  91. parameters are subject to validation, and the command will fail with
  92. an error message if the new value does not make sense. An optional
  93. third argument may be used to specify in which layer to set the
  94. configuration parameter. The default layer is "user". The
  95. configuration value will be set for the current channel, which
  96. is controlled by the default_channel configuration variable.
  97. ',
  98. ),
  99. 'config-help' => array(
  100. 'summary' => 'Show Information About Setting',
  101. 'function' => 'doConfigHelp',
  102. 'shortcut' => 'ch',
  103. 'options' => array(),
  104. 'doc' => '[parameter]
  105. Displays help for a configuration parameter. Without arguments it
  106. displays help for all configuration parameters.
  107. ',
  108. ),
  109. 'config-create' => array(
  110. 'summary' => 'Create a Default configuration file',
  111. 'function' => 'doConfigCreate',
  112. 'shortcut' => 'coc',
  113. 'options' => array(
  114. 'windows' => array(
  115. 'shortopt' => 'w',
  116. 'doc' => 'create a config file for a windows install',
  117. ),
  118. ),
  119. 'doc' => '<root path> <filename>
  120. Create a default configuration file with all directory configuration
  121. variables set to subdirectories of <root path>, and save it as <filename>.
  122. This is useful especially for creating a configuration file for a remote
  123. PEAR installation (using the --remoteconfig option of install, upgrade,
  124. and uninstall).
  125. ',
  126. ),
  127. );
  128. /**
  129. * PEAR_Command_Config constructor.
  130. *
  131. * @access public
  132. */
  133. function PEAR_Command_Config(&$ui, &$config)
  134. {
  135. parent::PEAR_Command_Common($ui, $config);
  136. }
  137. function doConfigShow($command, $options, $params)
  138. {
  139. $layer = null;
  140. if (is_array($params)) {
  141. $layer = isset($params[0]) ? $params[0] : null;
  142. }
  143. // $params[0] -> the layer
  144. if ($error = $this->_checkLayer($layer)) {
  145. return $this->raiseError("config-show:$error");
  146. }
  147. $keys = $this->config->getKeys();
  148. sort($keys);
  149. $channel = isset($options['channel']) ? $options['channel'] :
  150. $this->config->get('default_channel');
  151. $reg = &$this->config->getRegistry();
  152. if (!$reg->channelExists($channel)) {
  153. return $this->raiseError('Channel "' . $channel . '" does not exist');
  154. }
  155. $channel = $reg->channelName($channel);
  156. $data = array('caption' => 'Configuration (channel ' . $channel . '):');
  157. foreach ($keys as $key) {
  158. $type = $this->config->getType($key);
  159. $value = $this->config->get($key, $layer, $channel);
  160. if ($type == 'password' && $value) {
  161. $value = '********';
  162. }
  163. if ($value === false) {
  164. $value = 'false';
  165. } elseif ($value === true) {
  166. $value = 'true';
  167. }
  168. $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
  169. }
  170. foreach ($this->config->getLayers() as $layer) {
  171. $data['data']['Config Files'][] = array(ucfirst($layer) . ' Configuration File', 'Filename' , $this->config->getConfFile($layer));
  172. }
  173. $this->ui->outputData($data, $command);
  174. return true;
  175. }
  176. function doConfigGet($command, $options, $params)
  177. {
  178. $args_cnt = is_array($params) ? count($params) : 0;
  179. switch ($args_cnt) {
  180. case 1:
  181. $config_key = $params[0];
  182. $layer = null;
  183. break;
  184. case 2:
  185. $config_key = $params[0];
  186. $layer = $params[1];
  187. if ($error = $this->_checkLayer($layer)) {
  188. return $this->raiseError("config-get:$error");
  189. }
  190. break;
  191. case 0:
  192. default:
  193. return $this->raiseError("config-get expects 1 or 2 parameters");
  194. }
  195. $reg = &$this->config->getRegistry();
  196. $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
  197. if (!$reg->channelExists($channel)) {
  198. return $this->raiseError('Channel "' . $channel . '" does not exist');
  199. }
  200. $channel = $reg->channelName($channel);
  201. $this->ui->outputData($this->config->get($config_key, $layer, $channel), $command);
  202. return true;
  203. }
  204. function doConfigSet($command, $options, $params)
  205. {
  206. // $param[0] -> a parameter to set
  207. // $param[1] -> the value for the parameter
  208. // $param[2] -> the layer
  209. $failmsg = '';
  210. if (count($params) < 2 || count($params) > 3) {
  211. $failmsg .= "config-set expects 2 or 3 parameters";
  212. return PEAR::raiseError($failmsg);
  213. }
  214. if (isset($params[2]) && ($error = $this->_checkLayer($params[2]))) {
  215. $failmsg .= $error;
  216. return PEAR::raiseError("config-set:$failmsg");
  217. }
  218. $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
  219. $reg = &$this->config->getRegistry();
  220. if (!$reg->channelExists($channel)) {
  221. return $this->raiseError('Channel "' . $channel . '" does not exist');
  222. }
  223. $channel = $reg->channelName($channel);
  224. if ($params[0] == 'default_channel' && !$reg->channelExists($params[1])) {
  225. return $this->raiseError('Channel "' . $params[1] . '" does not exist');
  226. }
  227. if ($params[0] == 'preferred_mirror'
  228. && (
  229. !$reg->mirrorExists($channel, $params[1]) &&
  230. (!$reg->channelExists($params[1]) || $channel != $params[1])
  231. )
  232. ) {
  233. $msg = 'Channel Mirror "' . $params[1] . '" does not exist';
  234. $msg .= ' in your registry for channel "' . $channel . '".';
  235. $msg .= "\n" . 'Attempt to run "pear channel-update ' . $channel .'"';
  236. $msg .= ' if you believe this mirror should exist as you may';
  237. $msg .= ' have outdated channel information.';
  238. return $this->raiseError($msg);
  239. }
  240. if (count($params) == 2) {
  241. array_push($params, 'user');
  242. $layer = 'user';
  243. } else {
  244. $layer = $params[2];
  245. }
  246. array_push($params, $channel);
  247. if (!call_user_func_array(array(&$this->config, 'set'), $params)) {
  248. array_pop($params);
  249. $failmsg = "config-set (" . implode(", ", $params) . ") failed, channel $channel";
  250. } else {
  251. $this->config->store($layer);
  252. }
  253. if ($failmsg) {
  254. return $this->raiseError($failmsg);
  255. }
  256. $this->ui->outputData('config-set succeeded', $command);
  257. return true;
  258. }
  259. function doConfigHelp($command, $options, $params)
  260. {
  261. if (empty($params)) {
  262. $params = $this->config->getKeys();
  263. }
  264. $data['caption'] = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
  265. $data['headline'] = array('Name', 'Type', 'Description');
  266. $data['border'] = true;
  267. foreach ($params as $name) {
  268. $type = $this->config->getType($name);
  269. $docs = $this->config->getDocs($name);
  270. if ($type == 'set') {
  271. $docs = rtrim($docs) . "\nValid set: " .
  272. implode(' ', $this->config->getSetValues($name));
  273. }
  274. $data['data'][] = array($name, $type, $docs);
  275. }
  276. $this->ui->outputData($data, $command);
  277. }
  278. function doConfigCreate($command, $options, $params)
  279. {
  280. if (count($params) != 2) {
  281. return PEAR::raiseError('config-create: must have 2 parameters, root path and ' .
  282. 'filename to save as');
  283. }
  284. $root = $params[0];
  285. // Clean up the DIRECTORY_SEPARATOR mess
  286. $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
  287. $root = preg_replace(array('!\\\\+!', '!/+!', "!$ds2+!"),
  288. array('/', '/', '/'),
  289. $root);
  290. if ($root{0} != '/') {
  291. if (!isset($options['windows'])) {
  292. return PEAR::raiseError('Root directory must be an absolute path beginning ' .
  293. 'with "/", was: "' . $root . '"');
  294. }
  295. if (!preg_match('/^[A-Za-z]:/', $root)) {
  296. return PEAR::raiseError('Root directory must be an absolute path beginning ' .
  297. 'with "\\" or "C:\\", was: "' . $root . '"');
  298. }
  299. }
  300. $windows = isset($options['windows']);
  301. if ($windows) {
  302. $root = str_replace('/', '\\', $root);
  303. }
  304. if (!file_exists($params[1]) && !@touch($params[1])) {
  305. return PEAR::raiseError('Could not create "' . $params[1] . '"');
  306. }
  307. $params[1] = realpath($params[1]);
  308. $config = &new PEAR_Config($params[1], '#no#system#config#', false, false);
  309. if ($root{strlen($root) - 1} == '/') {
  310. $root = substr($root, 0, strlen($root) - 1);
  311. }
  312. $config->noRegistry();
  313. $config->set('php_dir', $windows ? "$root\\pear\\php" : "$root/pear/php", 'user');
  314. $config->set('data_dir', $windows ? "$root\\pear\\data" : "$root/pear/data");
  315. $config->set('www_dir', $windows ? "$root\\pear\\www" : "$root/pear/www");
  316. $config->set('cfg_dir', $windows ? "$root\\pear\\cfg" : "$root/pear/cfg");
  317. $config->set('ext_dir', $windows ? "$root\\pear\\ext" : "$root/pear/ext");
  318. $config->set('doc_dir', $windows ? "$root\\pear\\docs" : "$root/pear/docs");
  319. $config->set('test_dir', $windows ? "$root\\pear\\tests" : "$root/pear/tests");
  320. $config->set('cache_dir', $windows ? "$root\\pear\\cache" : "$root/pear/cache");
  321. $config->set('download_dir', $windows ? "$root\\pear\\download" : "$root/pear/download");
  322. $config->set('temp_dir', $windows ? "$root\\pear\\temp" : "$root/pear/temp");
  323. $config->set('bin_dir', $windows ? "$root\\pear" : "$root/pear");
  324. $config->writeConfigFile();
  325. $this->_showConfig($config);
  326. $this->ui->outputData('Successfully created default configuration file "' . $params[1] . '"',
  327. $command);
  328. }
  329. function _showConfig(&$config)
  330. {
  331. $params = array('user');
  332. $keys = $config->getKeys();
  333. sort($keys);
  334. $channel = 'pear.php.net';
  335. $data = array('caption' => 'Configuration (channel ' . $channel . '):');
  336. foreach ($keys as $key) {
  337. $type = $config->getType($key);
  338. $value = $config->get($key, 'user', $channel);
  339. if ($type == 'password' && $value) {
  340. $value = '********';
  341. }
  342. if ($value === false) {
  343. $value = 'false';
  344. } elseif ($value === true) {
  345. $value = 'true';
  346. }
  347. $data['data'][$config->getGroup($key)][] =
  348. array($config->getPrompt($key) , $key, $value);
  349. }
  350. foreach ($config->getLayers() as $layer) {
  351. $data['data']['Config Files'][] =
  352. array(ucfirst($layer) . ' Configuration File', 'Filename' ,
  353. $config->getConfFile($layer));
  354. }
  355. $this->ui->outputData($data, 'config-show');
  356. return true;
  357. }
  358. /**
  359. * Checks if a layer is defined or not
  360. *
  361. * @param string $layer The layer to search for
  362. * @return mixed False on no error or the error message
  363. */
  364. function _checkLayer($layer = null)
  365. {
  366. if (!empty($layer) && $layer != 'default') {
  367. $layers = $this->config->getLayers();
  368. if (!in_array($layer, $layers)) {
  369. return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";
  370. }
  371. }
  372. return false;
  373. }
  374. }