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