PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/app/siteinvoice/lib/PEAR/PEAR/Command/Config.php

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