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