Subversion Repositories Applications.gtt

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
Line 2... Line 2...
2
/**
2
/**
3
 * PEAR_Command_Config (config-show, config-get, config-set, config-help, config-create commands)
3
 * PEAR_Command_Config (config-show, config-get, config-set, config-help, config-create commands)
4
 *
4
 *
5
 * PHP versions 4 and 5
5
 * PHP versions 4 and 5
6
 *
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
7
 * @category   pear
14
 * @package    PEAR
8
 * @package    PEAR
15
 * @author     Stig Bakken <ssb@php.net>
9
 * @author     Stig Bakken <ssb@php.net>
16
 * @author     Greg Beaver <cellog@php.net>
10
 * @author     Greg Beaver <cellog@php.net>
17
 * @copyright  1997-2006 The PHP Group
11
 * @copyright  1997-2009 The Authors
18
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
12
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
19
 * @version    CVS: $Id: Config.php,v 1.52 2006/03/05 21:32:47 cellog Exp $
-
 
20
 * @link       http://pear.php.net/package/PEAR
13
 * @link       http://pear.php.net/package/PEAR
21
 * @since      File available since Release 0.1
14
 * @since      File available since Release 0.1
22
 */
15
 */
Line 23... Line 16...
23
 
16
 
Line 31... Line 24...
31
 *
24
 *
32
 * @category   pear
25
 * @category   pear
33
 * @package    PEAR
26
 * @package    PEAR
34
 * @author     Stig Bakken <ssb@php.net>
27
 * @author     Stig Bakken <ssb@php.net>
35
 * @author     Greg Beaver <cellog@php.net>
28
 * @author     Greg Beaver <cellog@php.net>
36
 * @copyright  1997-2006 The PHP Group
29
 * @copyright  1997-2009 The Authors
37
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
30
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
38
 * @version    Release: 1.5.1
31
 * @version    Release: 1.10.1
39
 * @link       http://pear.php.net/package/PEAR
32
 * @link       http://pear.php.net/package/PEAR
40
 * @since      Class available since Release 0.1
33
 * @since      Class available since Release 0.1
41
 */
34
 */
42
class PEAR_Command_Config extends PEAR_Command_Common
35
class PEAR_Command_Config extends PEAR_Command_Common
43
{
36
{
44
    // {{{ properties
-
 
45
 
-
 
46
    var $commands = array(
37
    var $commands = array(
47
        'config-show' => array(
38
        'config-show' => array(
48
            'summary' => 'Show All Settings',
39
            'summary' => 'Show All Settings',
49
            'function' => 'doConfigShow',
40
            'function' => 'doConfigShow',
50
            'shortcut' => 'csh',
41
            'shortcut' => 'csh',
Line 134... Line 125...
134
and uninstall).
125
and uninstall).
135
',
126
',
136
            ),
127
            ),
137
        );
128
        );
Line 138... Line -...
138
 
-
 
139
    // }}}
-
 
140
    // {{{ constructor
-
 
141
 
129
 
142
    /**
130
    /**
143
     * PEAR_Command_Config constructor.
131
     * PEAR_Command_Config constructor.
144
     *
132
     *
145
     * @access public
133
     * @access public
146
     */
134
     */
147
    function PEAR_Command_Config(&$ui, &$config)
135
    function __construct(&$ui, &$config)
148
    {
136
    {
149
        parent::PEAR_Command_Common($ui, $config);
137
        parent::__construct($ui, $config);
Line 150... Line -...
150
    }
-
 
151
 
-
 
152
    // }}}
-
 
153
 
-
 
154
    // {{{ doConfigShow()
138
    }
155
 
139
 
-
 
140
    function doConfigShow($command, $options, $params)
156
    function doConfigShow($command, $options, $params)
141
    {
157
    {
142
        $layer = null;
158
        if (is_array($params)) {
-
 
159
            $layer = isset($params[0]) ? $params[0] : NULL;
-
 
160
        } else {
143
        if (is_array($params)) {
Line 161... Line 144...
161
            $layer = NULL;
144
            $layer = isset($params[0]) ? $params[0] : null;
162
        }
145
        }
163
 
146
 
164
        // $params[0] -> the layer
147
        // $params[0] -> the layer
-
 
148
        if ($error = $this->_checkLayer($layer)) {
165
        if ($error = $this->_checkLayer($layer)) {
149
            return $this->raiseError("config-show:$error");
166
            return $this->raiseError("config-show:$error");
150
        }
167
        }
151
 
168
        $keys = $this->config->getKeys();
152
        $keys = $this->config->getKeys();
169
        sort($keys);
153
        sort($keys);
170
        $channel = isset($options['channel']) ? $options['channel'] :
154
        $channel = isset($options['channel']) ? $options['channel'] :
171
            $this->config->get('default_channel');
155
            $this->config->get('default_channel');
172
        $reg = &$this->config->getRegistry();
156
        $reg = &$this->config->getRegistry();
-
 
157
        if (!$reg->channelExists($channel)) {
-
 
158
            return $this->raiseError('Channel "' . $channel . '" does not exist');
173
        if (!$reg->channelExists($channel)) {
159
        }
174
            return $this->raiseError('Channel "' . $channel . '" does not exist');
160
 
175
        }
161
        $channel = $reg->channelName($channel);
176
        $data = array('caption' => 'Configuration (channel ' . $channel . '):');
162
        $data = array('caption' => 'Configuration (channel ' . $channel . '):');
177
        foreach ($keys as $key) {
163
        foreach ($keys as $key) {
178
            $type = $this->config->getType($key);
164
            $type = $this->config->getType($key);
179
            $value = $this->config->get($key, $layer, $channel);
165
            $value = $this->config->get($key, $layer, $channel);
-
 
166
            if ($type == 'password' && $value) {
180
            if ($type == 'password' && $value) {
167
                $value = '********';
181
                $value = '********';
168
            }
182
            }
169
 
183
            if ($value === false) {
170
            if ($value === false) {
184
                $value = 'false';
171
                $value = 'false';
-
 
172
            } elseif ($value === true) {
185
            } elseif ($value === true) {
173
                $value = 'true';
186
                $value = 'true';
174
            }
-
 
175
 
187
            }
176
            $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
188
            $data['data'][$this->config->getGroup($key)][] = array($this->config->getPrompt($key) , $key, $value);
177
        }
189
        }
178
 
Line 190... Line 179...
190
        foreach ($this->config->getLayers() as $layer) {
179
        foreach ($this->config->getLayers() as $layer) {
191
            $data['data']['Config Files'][] = array(ucfirst($layer) . ' Configuration File', 'Filename' , $this->config->getConfFile($layer));
180
            $data['data']['Config Files'][] = array(ucfirst($layer) . ' Configuration File', 'Filename' , $this->config->getConfFile($layer));
192
        }
181
        }
Line 193... Line -...
193
 
-
 
194
        $this->ui->outputData($data, $command);
-
 
195
        return true;
-
 
196
    }
182
 
197
 
183
        $this->ui->outputData($data, $command);
198
    // }}}
-
 
199
    // {{{ doConfigGet()
-
 
200
 
-
 
201
    function doConfigGet($command, $options, $params)
184
        return true;
202
    {
-
 
203
        if (!is_array($params)) {
-
 
204
            $args_cnt = 0;
185
    }
205
        } else {
186
 
206
            $args_cnt  = count($params);
187
    function doConfigGet($command, $options, $params)
207
        }
188
    {
208
 
189
        $args_cnt = is_array($params) ? count($params) : 0;
209
        switch ($args_cnt) {
190
        switch ($args_cnt) {
210
            case 1:
191
            case 1:
211
                $config_key = $params[0];
192
                $config_key = $params[0];
212
                $layer = NULL;
193
                $layer = null;
Line 221... Line 202...
221
            case 0:
202
            case 0:
222
            default:
203
            default:
223
                return $this->raiseError("config-get expects 1 or 2 parameters");
204
                return $this->raiseError("config-get expects 1 or 2 parameters");
224
        }
205
        }
Line 225... Line -...
225
 
-
 
226
        $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
206
 
227
        $reg = &$this->config->getRegistry();
-
 
-
 
207
        $reg = &$this->config->getRegistry();
228
 
208
        $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
229
        if (!$reg->channelExists($channel)) {
209
        if (!$reg->channelExists($channel)) {
230
            return $this->raiseError('Channel "' . $channel . '" does not exist');
210
            return $this->raiseError('Channel "' . $channel . '" does not exist');
Line -... Line 211...
-
 
211
        }
231
        }
212
 
232
 
-
 
233
        $this->ui->outputData($this->config->get($config_key, $layer, $channel), $command);
213
        $channel = $reg->channelName($channel);
234
 
214
        $this->ui->outputData($this->config->get($config_key, $layer, $channel), $command);
Line 235... Line -...
235
        return true;
-
 
236
    }
-
 
237
 
-
 
238
    // }}}
215
        return true;
239
    // {{{ doConfigSet()
216
    }
240
 
217
 
241
    function doConfigSet($command, $options, $params)
218
    function doConfigSet($command, $options, $params)
242
    {
219
    {
243
        // $param[0] -> a parameter to set
220
        // $param[0] -> a parameter to set
244
        // $param[1] -> the value for the parameter
221
        // $param[1] -> the value for the parameter
245
        // $param[2] -> the layer
222
        // $param[2] -> the layer
246
        $failmsg = '';
223
        $failmsg = '';
247
        if (sizeof($params) < 2 || sizeof($params) > 3) {
224
        if (count($params) < 2 || count($params) > 3) {
-
 
225
            $failmsg .= "config-set expects 2 or 3 parameters";
248
            $failmsg .= "config-set expects 2 or 3 parameters";
226
            return PEAR::raiseError($failmsg);
249
            return PEAR::raiseError($failmsg);
227
        }
250
        }
228
 
251
        if (isset($params[2]) && ($error = $this->_checkLayer($params[2]))) {
229
        if (isset($params[2]) && ($error = $this->_checkLayer($params[2]))) {
-
 
230
            $failmsg .= $error;
252
            $failmsg .= $error;
231
            return PEAR::raiseError("config-set:$failmsg");
253
            return PEAR::raiseError("config-set:$failmsg");
-
 
254
        }
232
        }
255
        $channel = isset($options['channel']) ? $options['channel'] :
233
 
256
            $this->config->get('default_channel');
234
        $channel = isset($options['channel']) ? $options['channel'] : $this->config->get('default_channel');
257
        $reg = &$this->config->getRegistry();
235
        $reg = &$this->config->getRegistry();
-
 
236
        if (!$reg->channelExists($channel)) {
258
        if (!$reg->channelExists($channel)) {
237
            return $this->raiseError('Channel "' . $channel . '" does not exist');
259
            return $this->raiseError('Channel "' . $channel . '" does not exist');
238
        }
260
        }
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'
261
        if ($params[0] == 'default_channel') {
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 .'"';
262
            if (!$reg->channelExists($params[1])) {
254
            $msg .= ' if you believe this mirror should exist as you may';
-
 
255
            $msg .= ' have outdated channel information.';
263
                return $this->raiseError('Channel "' . $params[1] . '" does not exist');
256
            return $this->raiseError($msg);
264
            }
257
        }
265
        }
258
 
266
        if (count($params) == 2) {
259
        if (count($params) == 2) {
267
            array_push($params, 'user');
260
            array_push($params, 'user');
268
            $layer = 'user';
261
            $layer = 'user';
-
 
262
        } else {
269
        } else {
263
            $layer = $params[2];
270
            $layer = $params[2];
264
        }
271
        }
-
 
272
        array_push($params, $channel);
265
 
273
        if (!call_user_func_array(array(&$this->config, 'set'), $params))
266
        array_push($params, $channel);
274
        {
267
        if (!call_user_func_array(array(&$this->config, 'set'), $params)) {
275
            array_pop($params);
268
            array_pop($params);
276
            $failmsg = "config-set (" . implode(", ", $params) . ") failed, channel $channel";
269
            $failmsg = "config-set (" . implode(", ", $params) . ") failed, channel $channel";
-
 
270
        } else {
277
        } else {
271
            $this->config->store($layer);
278
            $this->config->store($layer);
272
        }
279
        }
273
 
-
 
274
        if ($failmsg) {
280
        if ($failmsg) {
275
            return $this->raiseError($failmsg);
281
            return $this->raiseError($failmsg);
276
        }
282
        }
277
 
Line 283... Line -...
283
        $this->ui->outputData('config-set succeeded', $command);
-
 
284
        return true;
-
 
285
    }
-
 
286
 
278
        $this->ui->outputData('config-set succeeded', $command);
287
    // }}}
279
        return true;
288
    // {{{ doConfigHelp()
280
    }
289
 
281
 
290
    function doConfigHelp($command, $options, $params)
282
    function doConfigHelp($command, $options, $params)
-
 
283
    {
291
    {
284
        if (empty($params)) {
292
        if (empty($params)) {
285
            $params = $this->config->getKeys();
293
            $params = $this->config->getKeys();
286
        }
294
        }
287
 
295
        $data['caption']  = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
288
        $data['caption']  = "Config help" . ((count($params) == 1) ? " for $params[0]" : '');
296
        $data['headline'] = array('Name', 'Type', 'Description');
289
        $data['headline'] = array('Name', 'Type', 'Description');
297
        $data['border']   = true;
290
        $data['border']   = true;
298
        foreach ($params as $name) {
291
        foreach ($params as $name) {
299
            $type = $this->config->getType($name);
292
            $type = $this->config->getType($name);
300
            $docs = $this->config->getDocs($name);
293
            $docs = $this->config->getDocs($name);
-
 
294
            if ($type == 'set') {
301
            if ($type == 'set') {
295
                $docs = rtrim($docs) . "\nValid set: " .
302
                $docs = rtrim($docs) . "\nValid set: " .
296
                    implode(' ', $this->config->getSetValues($name));
-
 
297
            }
303
                    implode(' ', $this->config->getSetValues($name));
298
 
304
            }
299
            $data['data'][] = array($name, $type, $docs);
Line 305... Line -...
305
            $data['data'][] = array($name, $type, $docs);
-
 
306
        }
-
 
307
        $this->ui->outputData($data, $command);
-
 
308
    }
300
        }
309
 
301
 
310
    // }}}
302
        $this->ui->outputData($data, $command);
311
    // {{{ doConfigCreate()
303
    }
312
 
304
 
313
    function doConfigCreate($command, $options, $params)
305
    function doConfigCreate($command, $options, $params)
-
 
306
    {
314
    {
307
        if (count($params) != 2) {
315
        if (count($params) != 2) {
308
            return PEAR::raiseError('config-create: must have 2 parameters, root path and ' .
316
            return PEAR::raiseError('config-create: must have 2 parameters, root path and ' .
309
                'filename to save as');
317
                'filename to save as');
310
        }
318
        }
311
 
319
        $root = $params[0];
312
        $root = $params[0];
320
        // Clean up the DIRECTORY_SEPARATOR mess
313
        // Clean up the DIRECTORY_SEPARATOR mess
321
        $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
314
        $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
322
        $root = preg_replace(array('!\\\\+!', '!/+!', "!$ds2+!"),
-
 
323
                             array('/', '/', '/'),
-
 
324
                            $root);
-
 
325
        if ($root{0} != '/') {
-
 
326
            if (isset($options['windows'])) {
-
 
327
                if (!preg_match('/^[A-Za-z]:/', $root)) {
315
        $root = preg_replace(array('!\\\\+!', '!/+!', "!$ds2+!"),
328
                    return PEAR::raiseError('Root directory must be an absolute path beginning ' .
316
                             array('/', '/', '/'),
329
                        'with "\\" or "C:\\", was: "' . $root . '"');
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
            }
330
                }
323
 
-
 
324
            if (!preg_match('/^[A-Za-z]:/', $root)) {
331
            } else {
325
                return PEAR::raiseError('Root directory must be an absolute path beginning ' .
332
                return PEAR::raiseError('Root directory must be an absolute path beginning ' .
326
                    'with "\\" or "C:\\", was: "' . $root . '"');
333
                    'with "/", was: "' . $root . '"');
327
            }
334
            }
328
        }
-
 
329
 
335
        }
330
        $windows = isset($options['windows']);
336
        $windows = isset($options['windows']);
-
 
337
        if ($windows) {
331
        if ($windows) {
338
            $root = str_replace('/', '\\', $root);
-
 
339
        }
332
            $root = str_replace('/', '\\', $root);
-
 
333
        }
340
        if (!file_exists($params[1])) {
334
 
341
            if (!@touch($params[1])) {
335
        if (!file_exists($params[1]) && !@touch($params[1])) {
342
                return PEAR::raiseError('Could not create "' . $params[1] . '"');
336
            return PEAR::raiseError('Could not create "' . $params[1] . '"');
343
            }
337
        }
344
        }
338
 
-
 
339
        $params[1] = realpath($params[1]);
345
        $params[1] = realpath($params[1]);
340
        $config = new PEAR_Config($params[1], '#no#system#config#', false, false);
346
        $config = &new PEAR_Config($params[1], '#no#system#config#', false, false);
341
        if ($root{strlen($root) - 1} == '/') {
347
        if ($root{strlen($root) - 1} == '/') {
342
            $root = substr($root, 0, strlen($root) - 1);
-
 
343
        }
-
 
344
 
348
            $root = substr($root, 0, strlen($root) - 1);
345
        $config->noRegistry();
349
        }
346
        $config->set('php_dir', $windows ? "$root\\pear\\php" : "$root/pear/php", 'user');
350
        $config->noRegistry();
347
        $config->set('data_dir', $windows ? "$root\\pear\\data" : "$root/pear/data");
351
        $config->set('php_dir', $windows ? "$root\\pear\\php" : "$root/pear/php", 'user');
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");
352
        $config->set('data_dir', $windows ? "$root\\pear\\data" : "$root/pear/data");
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('ext_dir', $windows ? "$root\\pear\\ext" : "$root/pear/ext");
353
        $config->set('cache_dir', $windows ? "$root\\pear\\cache" : "$root/pear/cache");
354
        $config->set('doc_dir', $windows ? "$root\\pear\\docs" : "$root/pear/docs");
354
        $config->set('download_dir', $windows ? "$root\\pear\\download" : "$root/pear/download");
355
        $config->set('test_dir', $windows ? "$root\\pear\\tests" : "$root/pear/tests");
355
        $config->set('temp_dir', $windows ? "$root\\pear\\temp" : "$root/pear/temp");
356
        $config->set('cache_dir', $windows ? "$root\\pear\\cache" : "$root/pear/cache");
356
        $config->set('bin_dir', $windows ? "$root\\pear" : "$root/pear");
357
        $config->set('bin_dir', $windows ? "$root\\pear" : "$root/pear");
357
        $config->set('man_dir', $windows ? "$root\\pear\\man" : "$root/pear/man");
Line 358... Line -...
358
        $config->writeConfigFile();
-
 
359
        $this->_showConfig($config);
-
 
360
        $this->ui->outputData('Successfully created default configuration file "' . $params[1] . '"',
358
        $config->writeConfigFile();
361
            $command);
359
        $this->_showConfig($config);
362
    }
360
        $this->ui->outputData('Successfully created default configuration file "' . $params[1] . '"',
363
 
361
            $command);
364
    // }}}
362
    }
Line 374... Line 372...
374
            $type = $config->getType($key);
372
            $type = $config->getType($key);
375
            $value = $config->get($key, 'user', $channel);
373
            $value = $config->get($key, 'user', $channel);
376
            if ($type == 'password' && $value) {
374
            if ($type == 'password' && $value) {
377
                $value = '********';
375
                $value = '********';
378
            }
376
            }
-
 
377
 
379
            if ($value === false) {
378
            if ($value === false) {
380
                $value = 'false';
379
                $value = 'false';
381
            } elseif ($value === true) {
380
            } elseif ($value === true) {
382
                $value = 'true';
381
                $value = 'true';
383
            }
382
            }
384
            $data['data'][$config->getGroup($key)][] =
383
            $data['data'][$config->getGroup($key)][] =
385
                array($config->getPrompt($key) , $key, $value);
384
                array($config->getPrompt($key) , $key, $value);
386
        }
385
        }
-
 
386
 
387
        foreach ($config->getLayers() as $layer) {
387
        foreach ($config->getLayers() as $layer) {
388
            $data['data']['Config Files'][] =
388
            $data['data']['Config Files'][] =
389
                array(ucfirst($layer) . ' Configuration File', 'Filename' ,
389
                array(ucfirst($layer) . ' Configuration File', 'Filename' ,
390
                    $config->getConfFile($layer));
390
                    $config->getConfFile($layer));
391
        }
391
        }
Line 392... Line 392...
392
 
392
 
393
        $this->ui->outputData($data, 'config-show');
393
        $this->ui->outputData($data, 'config-show');
394
        return true;
394
        return true;
395
    }
-
 
Line 396... Line 395...
396
    // {{{ _checkLayer()
395
    }
397
 
396
 
398
    /**
397
    /**
399
     * Checks if a layer is defined or not
398
     * Checks if a layer is defined or not
Line 407... Line 406...
407
            $layers = $this->config->getLayers();
406
            $layers = $this->config->getLayers();
408
            if (!in_array($layer, $layers)) {
407
            if (!in_array($layer, $layers)) {
409
                return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";
408
                return " only the layers: \"" . implode('" or "', $layers) . "\" are supported";
410
            }
409
            }
411
        }
410
        }
-
 
411
 
412
        return false;
412
        return false;
413
    }
413
    }
414
 
-
 
415
    // }}}
-
 
416
}
414
}
417
 
-
 
418
?>
-