Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * CLI Script to generate text phpinfo() style PEAR information
4
 *
5
 * PHP versions 4 and 5
6
 *
7
 * LICENSE: This source file is subject to version 3.01 of the PHP license
8
 * that is available through the world-wide-web at the following URI:
9
 * http://www.php.net/license/3_01.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_Info
15
 * @author   Laurent Laville <pear@laurent-laville.org>
16
 * @license  http://www.php.net/license/3_01.txt  PHP License 3.01
17
 * @version  CVS: $Id: Cli.php,v 1.4 2009/01/07 21:52:44 farell Exp $
18
 * @link     http://pear.php.net/package/PEAR_Info
19
 * @since    File available since Release 1.8.0
20
 */
21
 
22
require_once 'PEAR/Info.php';
23
require_once 'Console/Getargs.php';
24
 
25
/**
26
 * CLI Script to display information about your PEAR installation
27
 *
28
 * <code>
29
 * <?php
30
 *     require_once 'PEAR/Info/Cli.php';
31
 *     $cli = new PEAR_Info_Cli();
32
 *     $cli->run();
33
 * ?>
34
 * </code>
35
 *
36
 * @category PEAR
37
 * @package  PEAR_Info
38
 * @author   Laurent Laville <pear@laurent-laville.org>
39
 * @license  http://www.php.net/license/3_01.txt  PHP License 3.01
40
 * @version  Release: 1.9.2
41
 * @link     http://pear.php.net/package/PEAR_Info
42
 * @since    Class available since Release 1.8.0
43
 */
44
 
45
class PEAR_Info_Cli extends PEAR_Info
46
{
47
    /**
48
     * @var    array    Current CLI Flags
49
     * @since  1.8.0
50
     */
51
    var $opts = array();
52
 
53
    /**
54
     * @var    string   error message
55
     * @since  1.8.0
56
     */
57
    var $error;
58
 
59
    /**
60
     * @var    object   Console_Getargs instance
61
     * @since  1.8.0
62
     */
63
    var $args;
64
 
65
 
66
    /**
67
     * ZE2 Constructor
68
     *
69
     * @since  1.8.0
70
     */
71
    function __construct()
72
    {
73
        $this->opts = array(
74
            'php-dir' =>
75
                array('short' => 'pd',
76
                      'desc'  => 'PEAR directory',
77
                      'default' => '',
78
                      'min'   => 0 , 'max' => 1),
79
            'usr-cfg' =>
80
                array('short' => 'uc',
81
                      'desc' => 'User Configuration File',
82
                      'default' => '',
83
                      'min'   => 0 , 'max' => 1),
84
            'sys-cfg' =>
85
                array('short' => 'sc',
86
                      'desc' => 'System Configuration File',
87
                      'default' => '',
88
                      'min'   => 0 , 'max' => 1),
89
            'http-proxy' =>
90
                array('short' => 'hp',
91
                      'desc' => 'HTTP Proxy Server Address',
92
                      'default' => '',
93
                      'min'   => 0 , 'max' => 1),
94
            'all' =>
95
                array('short'   => 'a',
96
                      'desc'    => 'Display informations for installed packages',
97
                      'default' => PEAR_INFO_ALL,
98
                      'min'     => 0 , 'max' => 1),
99
            'allchannels' =>
100
                array('short' => 'A',
101
                      'desc'  => 'List packages from all channels, '
102
                               . 'not just the default one',
103
                      'max'   => 0),
104
            'channel' =>
105
                array('short'   => 'c',
106
                      'desc'    => 'Specify which channel',
107
                      'default' => '',
108
                      'min'     => 0 , 'max' => 1),
109
            'version' =>
110
                array('short' => 'V',
111
                      'desc'  => 'Print version information',
112
                      'max'   => 0),
113
            'help' =>
114
                array('short' => 'h',
115
                      'desc'  => 'Show this help',
116
                      'max'   => 0),
117
        );
118
        $this->args = & Console_Getargs::factory($this->opts);
119
        if (PEAR::isError($this->args)) {
120
            if ($this->args->getCode() === CONSOLE_GETARGS_HELP) {
121
                $this->error = '';
122
            } else {
123
                $this->error = $this->args->getMessage();
124
            }
125
            return;
126
        }
127
 
128
        $options = array('channels' => array('pear.php.net'),
129
                         'resume' => PEAR_INFO_ALL);
130
 
131
        // php-dir
132
        if ($this->args->isDefined('pd')) {
133
            $pear_dir = $this->args->getValue('pd');
134
        } else {
135
            $pear_dir = '';
136
        }
137
 
138
        // usr-cfg
139
        if ($this->args->isDefined('uc')) {
140
            $user_file = $this->args->getValue('uc');
141
            if (!file_exists($user_file)) {
142
                $this->error = 'Failed opening PEAR user configuration file "'
143
                     . $user_file
144
                     . '". Please check your spelling and try again.';
145
                return;
146
            }
147
        } else {
148
            $user_file = '';
149
        }
150
 
151
        // sys-cfg
152
        if ($this->args->isDefined('sc')) {
153
            $system_file = $this->args->getValue('sc');
154
            if (!file_exists($system_file)) {
155
                $this->error = 'Failed opening PEAR system configuration file "'
156
                     . $system_file
157
                     . '". Please check your spelling and try again.';
158
                return;
159
            }
160
        } else {
161
            $system_file = '';
162
        }
163
 
164
        // http-proxy
165
        if ($this->args->isDefined('hp')) {
166
            $proxy = $this->args->getValue('hp');
167
            $res   = PEAR_Info::setProxy($proxy);
168
            if ($res === false) {
169
                $this->error = 'Failed define Proxy Server Address.'
170
                     . ' Please check your spelling and try again.';
171
                return;
172
            }
173
        }
174
 
175
        // all
176
        if ($this->args->isDefined('a')) {
177
            $a = $this->args->getValue('a');
178
            if (is_numeric($a)) {
179
                $options['resume'] = intval($a);
180
            } else {
181
                $this->error = "No valid 'resume' option for argument all."
182
                     . ' Please check your spelling and try again.';
183
                return;
184
            }
185
        }
186
 
187
        // allchannels
188
        $A = $this->args->getValue('A');
189
        if (isset($A)) {
190
            $options['channels'] = array();
191
        }
192
 
193
        // channel
194
        if ($this->args->isDefined('c')) {
195
            $chan = $this->args->getValue('c');
196
            if (is_string($chan)) {
197
                $options['channels'] = explode(',', $chan);
198
            } else {
199
                $this->error = 'No valid channel list provided.'
200
                     . ' Please check your spelling and try again.';
201
                return;
202
            }
203
        }
204
 
205
        // version
206
        $V = $this->args->getValue('V');
207
        if (isset($V)) {
208
            $this->error = 'PEAR_Info (cli) version 1.9.2'
209
                         . ' (http://pear.php.net/package/PEAR_Info)';
210
            return;
211
        }
212
 
213
        parent::__construct($pear_dir, $user_file, $system_file, $options);
214
    }
215
 
216
    /**
217
     * ZE1 PHP4 Compatible Constructor
218
     *
219
     * @since  1.8.0
220
     */
221
    function PEAR_Info_Cli()
222
    {
223
        $this->__construct();
224
    }
225
 
226
    /**
227
     * Run the CLI Script
228
     *
229
     * @return void
230
     * @access public
231
     * @since  1.8.0
232
     */
233
    function run()
234
    {
235
        if (isset($this->error)) {
236
            if (strpos($this->error, 'PEAR_Info') === false) {
237
                $this->_printUsage($this->error);
238
            } else {
239
                // when Version asked, do not print help usage
240
                echo $this->error;
241
            }
242
            return;
243
        }
244
 
245
        $table      = 0;
246
        $skip_table = 0;
247
        $tdeps      = false;
248
        $td         = '';
249
        $lines      = explode("\n", $this->info);
250
 
251
        foreach ($lines as $line) {
252
            $line = trim($line);
253
            if (strlen($line) == 0 && $tdeps === false) {
254
                // skip blank line
255
 
256
            } elseif ($line == '<br />') {
257
                // skip
258
 
259
            } elseif (substr($line, 0, 6) == '<a id=') {
260
                // skip package anchor
261
 
262
            } elseif (substr($line, 0, 4) == '<h1>') {
263
                // skip PEAR credits link
264
 
265
            } elseif (substr($line, 0, 4) == '<h2>') {
266
                if (substr($line, 4, 10) == '<a id="top') {
267
                    // skip anchors
268
                    $skip_table = 1;
269
                } else {
270
                    $td = strip_tags($line);
271
                    echo PHP_EOL . $td . PHP_EOL;
272
                }
273
 
274
            } elseif ($skip_table == 0 && $line == '<table>') {
275
                echo PHP_EOL;
276
                $table++;
277
 
278
            } elseif ($line == '</table>') {
279
                $skip_table = 0;
280
                if ($tdeps) {
281
                    $td = PHP_EOL . implode(PHP_EOL, $deps);
282
                }
283
                $tdeps = false;
284
 
285
            } elseif ($skip_table == 0 && $line == '<tr>') {
286
                $tr_class = '';
287
 
288
            } elseif ($skip_table == 0 && substr($line, 0, 3) == '<tr') {
289
                $tr_class = substr($line, 11, 1);
290
                if ($tr_class == 'w') {
291
                    $tdeps = true;
292
                    $tr_w  = array();
293
                    $w     = 0;
294
                } elseif ($tr_class == 'h') {
295
                    $td = '';
296
                } else {
297
                    $td = array('', '');
298
                }
299
 
300
            } elseif ($skip_table == 0 && $line == '</tr>' && $tr_class != '') {
301
                if ($tr_class == 'h') {
302
                    $td = explode(' ', $td);
303
                }
304
                if (isset($td_v)) {
305
                    $td = $td_v;
306
                }
307
                if ($tdeps) {
308
                    if ($tr_w[0] != 'Required') {
309
                        if ($tr_w[1] == 'PHP') {
310
                            $td = $tr_w[1] .' '. $tr_w[3] .' '. $tr_w[4];
311
                        } else {
312
                            if ($tr_w[3] == 'has' || $tr_w[3] == 'not') {
313
                                $td = $tr_w[3] .' '. $tr_w[2] .' '. $tr_w[1] .' '.
314
                                      $tr_w[4];
315
                            } else {
316
                                $td = strip_tags($tr_w[2]) .' '. $tr_w[1] .' '.
317
                                      $tr_w[3] .' '. $tr_w[4];
318
                            }
319
                            if ($tr_w[0] == 'No') {
320
                                $td .= ' (optional)';
321
                            }
322
                        }
323
                        $deps[] = ' - ' . $td;
324
                    }
325
                    $w = 0;
326
                } else {
327
                    if (isset($td_c) && count($td_c) > 0) {
328
                        echo $td[0] . ' => ' . PHP_EOL;
329
                        echo $td_c[0] . PHP_EOL;
330
                        echo $td_c[1] . PHP_EOL;
331
                    } else {
332
                        echo $td[0] . ' => ' . $td[1] . PHP_EOL;
333
                    }
334
                }
335
 
336
            } elseif ($skip_table == 0 && $line == '</td>') {
337
                if (isset($td_v)) {
338
                    if (empty($td_v[0])) {
339
                        $td_v[0] = $td;
340
                        $td      = '';
341
                    } else {
342
                        $td_v[1] = $td;
343
                    }
344
                }
345
 
346
            } elseif ($skip_table == 0 && $line == '<td>') {
347
                // skip
348
 
349
            } elseif ($skip_table == 0 && substr($line, 0, 3) == '<td') {
350
                if ($tr_class == 'h') {
351
                    $td = '';
352
 
353
                } elseif ($tr_class == 'v') {
354
                    if ($line == '<td class="e">') {
355
                        $td_v = array('', '');
356
                        $td   = '';
357
                    } else {
358
                        unset($td_v);
359
                        preg_match('`\<td(.*)\>(.*)\</td\>`', $line, $matches);
360
                        if (empty($matches[2])) {
361
                            $td_c = array();
362
                            if (substr($line, 0, 8) == '<td><dl>') {
363
                                preg_match('`\<td\>\<dl\>\<dt\>(.*)\</dt\>(.*)\<dt\>(.*)\</dt\>(.*)\</dl\>\</td\>`', $line, $matches);
364
                                $td_c[0] = ' - '.$matches[1];
365
                                $td_c[1] = ' - '.$matches[3];
366
                                $dd_user = $matches[2];
367
                                $dd_sys  = $matches[4];
368
                                preg_match('`\<dd class="cfg_(.*)"\>(.*)\</dd\>`', $dd_user, $matches);
369
                                $td_c[0] .= strtoupper($matches[1]).' => '.$matches[2];
370
                                preg_match('`\<dd class="cfg_(.*)"\>(.*)\</dd\>`', $dd_sys, $matches);
371
                                $td_c[1] .= strtoupper($matches[1]).' => '.$matches[2];
372
                            } else {
373
                                $td[1] = strip_tags($matches[0]);
374
                            }
375
                        } else {
376
                            if (empty($matches[1])) {
377
                                $td[1] = strip_tags($matches[2]);
378
                            } else {
379
                                $td[0] = $matches[2];
380
                            }
381
                        }
382
                    }
383
                }
384
 
385
            } else {
386
                if ($tdeps) {
387
                    $tr_w[$w] = $line;
388
                    $w++;
389
                } else {
390
                    if ($line == '<table class="d">') {
391
                        $deps = array();
392
                    } else {
393
                        $line = strip_tags($line);
394
                        $td  .= $line;
395
                    }
396
                }
397
            }
398
        }
399
    }
400
 
401
    /**
402
     * Show full help information
403
     *
404
     * @param string $footer (optional) page footer content
405
     *
406
     * @return void
407
     * @access private
408
     */
409
    function _printUsage($footer = '')
410
    {
411
        $header = 'Usage: '
412
            . basename($_SERVER['SCRIPT_NAME']) . " [options]\n\n";
413
        echo Console_Getargs::getHelp($this->opts, $header,
414
            "\n$footer\n", 78, 2)."\n";
415
    }
416
}
417
?>