Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 jpm 1
<?php
2
/**
3
 * PEAR_Registry
4
 *
5
 * PHP versions 4 and 5
6
 *
7
 * @category   pear
8
 * @package    PEAR
9
 * @author     Stig Bakken <ssb@php.net>
10
 * @author     Tomas V. V. Cox <cox@idecnet.com>
11
 * @author     Greg Beaver <cellog@php.net>
187 mathias 12
 * @copyright  1997-2009 The Authors
13
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
94 jpm 14
 * @link       http://pear.php.net/package/PEAR
15
 * @since      File available since Release 0.1
16
 */
17
 
18
/**
19
 * for PEAR_Error
20
 */
21
require_once 'PEAR.php';
22
require_once 'PEAR/DependencyDB.php';
23
 
187 mathias 24
define('PEAR_REGISTRY_ERROR_LOCK',         -2);
25
define('PEAR_REGISTRY_ERROR_FORMAT',       -3);
26
define('PEAR_REGISTRY_ERROR_FILE',         -4);
27
define('PEAR_REGISTRY_ERROR_CONFLICT',     -5);
94 jpm 28
define('PEAR_REGISTRY_ERROR_CHANNEL_FILE', -6);
29
 
30
/**
31
 * Administration class used to maintain the installed package database.
32
 * @category   pear
33
 * @package    PEAR
34
 * @author     Stig Bakken <ssb@php.net>
35
 * @author     Tomas V. V. Cox <cox@idecnet.com>
36
 * @author     Greg Beaver <cellog@php.net>
187 mathias 37
 * @copyright  1997-2009 The Authors
38
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
39
 * @version    Release: 1.10.1
94 jpm 40
 * @link       http://pear.php.net/package/PEAR
41
 * @since      Class available since Release 1.4.0a1
42
 */
43
class PEAR_Registry extends PEAR
44
{
45
    /**
46
     * File containing all channel information.
47
     * @var string
48
     */
49
    var $channels = '';
50
 
51
    /** Directory where registry files are stored.
52
     * @var string
53
     */
54
    var $statedir = '';
55
 
56
    /** File where the file map is stored
57
     * @var string
58
     */
59
    var $filemap = '';
60
 
61
    /** Directory where registry files for channels are stored.
62
     * @var string
63
     */
64
    var $channelsdir = '';
65
 
66
    /** Name of file used for locking the registry
67
     * @var string
68
     */
69
    var $lockfile = '';
70
 
71
    /** File descriptor used during locking
72
     * @var resource
73
     */
74
    var $lock_fp = null;
75
 
76
    /** Mode used during locking
77
     * @var int
78
     */
79
    var $lock_mode = 0; // XXX UNUSED
80
 
81
    /** Cache of package information.  Structure:
82
     * array(
83
     *   'package' => array('id' => ... ),
84
     *   ... )
85
     * @var array
86
     */
87
    var $pkginfo_cache = array();
88
 
89
    /** Cache of file map.  Structure:
90
     * array( '/path/to/file' => 'package', ... )
91
     * @var array
92
     */
93
    var $filemap_cache = array();
94
 
95
    /**
96
     * @var false|PEAR_ChannelFile
97
     */
98
    var $_pearChannel;
99
 
100
    /**
101
     * @var false|PEAR_ChannelFile
102
     */
103
    var $_peclChannel;
104
 
105
    /**
187 mathias 106
     * @var false|PEAR_ChannelFile
107
     */
108
    var $_docChannel;
109
 
110
    /**
94 jpm 111
     * @var PEAR_DependencyDB
112
     */
113
    var $_dependencyDB;
114
 
115
    /**
116
     * @var PEAR_Config
117
     */
118
    var $_config;
119
 
120
    /**
121
     * PEAR_Registry constructor.
122
     *
123
     * @param string (optional) PEAR install directory (for .php files)
124
     * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PEAR channel, if
125
     *        default values are not desired.  Only used the very first time a PEAR
126
     *        repository is initialized
127
     * @param PEAR_ChannelFile PEAR_ChannelFile object representing the PECL channel, if
128
     *        default values are not desired.  Only used the very first time a PEAR
129
     *        repository is initialized
130
     *
131
     * @access public
132
     */
187 mathias 133
    function __construct($pear_install_dir = PEAR_INSTALL_DIR, $pear_channel = false,
134
                           $pecl_channel = false, $pear_metadata_dir = '')
94 jpm 135
    {
187 mathias 136
        parent::__construct();
137
        $this->setInstallDir($pear_install_dir, $pear_metadata_dir);
94 jpm 138
        $this->_pearChannel = $pear_channel;
139
        $this->_peclChannel = $pecl_channel;
187 mathias 140
        $this->_config      = false;
94 jpm 141
    }
142
 
187 mathias 143
    function setInstallDir($pear_install_dir = PEAR_INSTALL_DIR, $pear_metadata_dir = '')
144
    {
145
        $ds = DIRECTORY_SEPARATOR;
146
        $this->install_dir = $pear_install_dir;
147
        if (!$pear_metadata_dir) {
148
            $pear_metadata_dir = $pear_install_dir;
149
        }
150
        $this->channelsdir = $pear_metadata_dir.$ds.'.channels';
151
        $this->statedir    = $pear_metadata_dir.$ds.'.registry';
152
        $this->filemap     = $pear_metadata_dir.$ds.'.filemap';
153
        $this->lockfile    = $pear_metadata_dir.$ds.'.lock';
154
    }
155
 
94 jpm 156
    function hasWriteAccess()
157
    {
158
        if (!file_exists($this->install_dir)) {
159
            $dir = $this->install_dir;
160
            while ($dir && $dir != '.') {
187 mathias 161
                $olddir = $dir;
162
                $dir    = dirname($dir);
94 jpm 163
                if ($dir != '.' && file_exists($dir)) {
164
                    if (is_writeable($dir)) {
165
                        return true;
166
                    }
187 mathias 167
 
168
                    return false;
94 jpm 169
                }
187 mathias 170
 
171
                if ($dir == $olddir) { // this can happen in safe mode
172
                    return @is_writable($dir);
173
                }
94 jpm 174
            }
187 mathias 175
 
94 jpm 176
            return false;
177
        }
187 mathias 178
 
94 jpm 179
        return is_writeable($this->install_dir);
180
    }
181
 
187 mathias 182
    function setConfig(&$config, $resetInstallDir = true)
94 jpm 183
    {
184
        $this->_config = &$config;
187 mathias 185
        if ($resetInstallDir) {
186
            $this->setInstallDir($config->get('php_dir'), $config->get('metadata_dir'));
187
        }
94 jpm 188
    }
189
 
190
    function _initializeChannelDirs()
191
    {
192
        static $running = false;
193
        if (!$running) {
194
            $running = true;
195
            $ds = DIRECTORY_SEPARATOR;
196
            if (!is_dir($this->channelsdir) ||
197
                  !file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
198
                  !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
187 mathias 199
                  !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
94 jpm 200
                  !file_exists($this->channelsdir . $ds . '__uri.reg')) {
201
                if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
202
                    $pear_channel = $this->_pearChannel;
203
                    if (!is_a($pear_channel, 'PEAR_ChannelFile') || !$pear_channel->validate()) {
204
                        if (!class_exists('PEAR_ChannelFile')) {
205
                            require_once 'PEAR/ChannelFile.php';
206
                        }
187 mathias 207
 
94 jpm 208
                        $pear_channel = new PEAR_ChannelFile;
209
                        $pear_channel->setAlias('pear');
210
                        $pear_channel->setServer('pear.php.net');
211
                        $pear_channel->setSummary('PHP Extension and Application Repository');
212
                        $pear_channel->setDefaultPEARProtocols();
213
                        $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
214
                        $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
187 mathias 215
                        $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
216
                        //$pear_channel->setBaseURL('REST1.4', 'http://pear.php.net/rest/');
94 jpm 217
                    } else {
187 mathias 218
                        $pear_channel->setServer('pear.php.net');
94 jpm 219
                        $pear_channel->setAlias('pear');
220
                    }
187 mathias 221
 
94 jpm 222
                    $pear_channel->validate();
223
                    $this->_addChannel($pear_channel);
224
                }
187 mathias 225
 
94 jpm 226
                if (!file_exists($this->channelsdir . $ds . 'pecl.php.net.reg')) {
227
                    $pecl_channel = $this->_peclChannel;
228
                    if (!is_a($pecl_channel, 'PEAR_ChannelFile') || !$pecl_channel->validate()) {
229
                        if (!class_exists('PEAR_ChannelFile')) {
230
                            require_once 'PEAR/ChannelFile.php';
231
                        }
187 mathias 232
 
94 jpm 233
                        $pecl_channel = new PEAR_ChannelFile;
234
                        $pecl_channel->setAlias('pecl');
235
                        $pecl_channel->setServer('pecl.php.net');
236
                        $pecl_channel->setSummary('PHP Extension Community Library');
237
                        $pecl_channel->setDefaultPEARProtocols();
238
                        $pecl_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
239
                        $pecl_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
240
                        $pecl_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
241
                    } else {
187 mathias 242
                        $pecl_channel->setServer('pecl.php.net');
94 jpm 243
                        $pecl_channel->setAlias('pecl');
244
                    }
187 mathias 245
 
94 jpm 246
                    $pecl_channel->validate();
247
                    $this->_addChannel($pecl_channel);
248
                }
187 mathias 249
 
250
                if (!file_exists($this->channelsdir . $ds . 'doc.php.net.reg')) {
251
                    $doc_channel = $this->_docChannel;
252
                    if (!is_a($doc_channel, 'PEAR_ChannelFile') || !$doc_channel->validate()) {
253
                        if (!class_exists('PEAR_ChannelFile')) {
254
                            require_once 'PEAR/ChannelFile.php';
255
                        }
256
 
257
                        $doc_channel = new PEAR_ChannelFile;
258
                        $doc_channel->setAlias('phpdocs');
259
                        $doc_channel->setServer('doc.php.net');
260
                        $doc_channel->setSummary('PHP Documentation Team');
261
                        $doc_channel->setDefaultPEARProtocols();
262
                        $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
263
                        $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
264
                        $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
265
                    } else {
266
                        $doc_channel->setServer('doc.php.net');
267
                        $doc_channel->setAlias('doc');
268
                    }
269
 
270
                    $doc_channel->validate();
271
                    $this->_addChannel($doc_channel);
272
                }
273
 
94 jpm 274
                if (!file_exists($this->channelsdir . $ds . '__uri.reg')) {
275
                    if (!class_exists('PEAR_ChannelFile')) {
276
                        require_once 'PEAR/ChannelFile.php';
277
                    }
187 mathias 278
 
94 jpm 279
                    $private = new PEAR_ChannelFile;
280
                    $private->setName('__uri');
187 mathias 281
                    $private->setDefaultPEARProtocols();
282
                    $private->setBaseURL('REST1.0', '****');
94 jpm 283
                    $private->setSummary('Pseudo-channel for static packages');
284
                    $this->_addChannel($private);
285
                }
286
                $this->_rebuildFileMap();
287
            }
187 mathias 288
 
94 jpm 289
            $running = false;
290
        }
291
    }
292
 
293
    function _initializeDirs()
294
    {
295
        $ds = DIRECTORY_SEPARATOR;
296
        // XXX Compatibility code should be removed in the future
297
        // rename all registry files if any to lowercase
298
        if (!OS_WINDOWS && file_exists($this->statedir) && is_dir($this->statedir) &&
299
              $handle = opendir($this->statedir)) {
300
            $dest = $this->statedir . $ds;
301
            while (false !== ($file = readdir($handle))) {
187 mathias 302
                if (preg_match('/^.*[A-Z].*\.reg\\z/', $file)) {
94 jpm 303
                    rename($dest . $file, $dest . strtolower($file));
304
                }
305
            }
306
            closedir($handle);
307
        }
187 mathias 308
 
94 jpm 309
        $this->_initializeChannelDirs();
310
        if (!file_exists($this->filemap)) {
311
            $this->_rebuildFileMap();
312
        }
313
        $this->_initializeDepDB();
314
    }
315
 
316
    function _initializeDepDB()
317
    {
318
        if (!isset($this->_dependencyDB)) {
319
            static $initializing = false;
320
            if (!$initializing) {
321
                $initializing = true;
322
                if (!$this->_config) { // never used?
187 mathias 323
                    $file = OS_WINDOWS ? 'pear.ini' : '.pearrc';
324
                    $this->_config = new PEAR_Config($this->statedir . DIRECTORY_SEPARATOR .
94 jpm 325
                        $file);
326
                    $this->_config->setRegistry($this);
327
                    $this->_config->set('php_dir', $this->install_dir);
328
                }
187 mathias 329
 
94 jpm 330
                $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
331
                if (PEAR::isError($this->_dependencyDB)) {
332
                    // attempt to recover by removing the dep db
187 mathias 333
                    if (file_exists($this->_config->get('metadata_dir', null, 'pear.php.net') .
94 jpm 334
                        DIRECTORY_SEPARATOR . '.depdb')) {
187 mathias 335
                        @unlink($this->_config->get('metadata_dir', null, 'pear.php.net') .
94 jpm 336
                            DIRECTORY_SEPARATOR . '.depdb');
337
                    }
187 mathias 338
 
94 jpm 339
                    $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->_config);
340
                    if (PEAR::isError($this->_dependencyDB)) {
341
                        echo $this->_dependencyDB->getMessage();
342
                        echo 'Unrecoverable error';
343
                        exit(1);
344
                    }
345
                }
187 mathias 346
 
94 jpm 347
                $initializing = false;
348
            }
349
        }
350
    }
351
 
352
    /**
353
     * PEAR_Registry destructor.  Makes sure no locks are forgotten.
354
     *
355
     * @access private
356
     */
357
    function _PEAR_Registry()
358
    {
359
        parent::_PEAR();
360
        if (is_resource($this->lock_fp)) {
361
            $this->_unlock();
362
        }
363
    }
364
 
365
    /**
366
     * Make sure the directory where we keep registry files exists.
367
     *
368
     * @return bool TRUE if directory exists, FALSE if it could not be
369
     * created
370
     *
371
     * @access private
372
     */
373
    function _assertStateDir($channel = false)
374
    {
375
        if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
376
            return $this->_assertChannelStateDir($channel);
377
        }
187 mathias 378
 
94 jpm 379
        static $init = false;
380
        if (!file_exists($this->statedir)) {
381
            if (!$this->hasWriteAccess()) {
382
                return false;
383
            }
187 mathias 384
 
94 jpm 385
            require_once 'System.php';
386
            if (!System::mkdir(array('-p', $this->statedir))) {
387
                return $this->raiseError("could not create directory '{$this->statedir}'");
388
            }
389
            $init = true;
390
        } elseif (!is_dir($this->statedir)) {
391
            return $this->raiseError('Cannot create directory ' . $this->statedir . ', ' .
392
                'it already exists and is not a directory');
393
        }
187 mathias 394
 
94 jpm 395
        $ds = DIRECTORY_SEPARATOR;
396
        if (!file_exists($this->channelsdir)) {
397
            if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg') ||
398
                  !file_exists($this->channelsdir . $ds . 'pecl.php.net.reg') ||
187 mathias 399
                  !file_exists($this->channelsdir . $ds . 'doc.php.net.reg') ||
94 jpm 400
                  !file_exists($this->channelsdir . $ds . '__uri.reg')) {
401
                $init = true;
402
            }
403
        } elseif (!is_dir($this->channelsdir)) {
404
            return $this->raiseError('Cannot create directory ' . $this->channelsdir . ', ' .
405
                'it already exists and is not a directory');
406
        }
187 mathias 407
 
94 jpm 408
        if ($init) {
409
            static $running = false;
410
            if (!$running) {
411
                $running = true;
412
                $this->_initializeDirs();
413
                $running = false;
414
                $init = false;
415
            }
416
        } else {
417
            $this->_initializeDepDB();
418
        }
187 mathias 419
 
94 jpm 420
        return true;
421
    }
422
 
423
    /**
424
     * Make sure the directory where we keep registry files exists for a non-standard channel.
425
     *
426
     * @param string channel name
427
     * @return bool TRUE if directory exists, FALSE if it could not be
428
     * created
429
     *
430
     * @access private
431
     */
432
    function _assertChannelStateDir($channel)
433
    {
434
        $ds = DIRECTORY_SEPARATOR;
435
        if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
436
            if (!file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
437
                $this->_initializeChannelDirs();
438
            }
439
            return $this->_assertStateDir($channel);
440
        }
187 mathias 441
 
94 jpm 442
        $channelDir = $this->_channelDirectoryName($channel);
443
        if (!is_dir($this->channelsdir) ||
444
              !file_exists($this->channelsdir . $ds . 'pear.php.net.reg')) {
445
            $this->_initializeChannelDirs();
446
        }
187 mathias 447
 
94 jpm 448
        if (!file_exists($channelDir)) {
449
            if (!$this->hasWriteAccess()) {
450
                return false;
451
            }
187 mathias 452
 
94 jpm 453
            require_once 'System.php';
454
            if (!System::mkdir(array('-p', $channelDir))) {
455
                return $this->raiseError("could not create directory '" . $channelDir .
456
                    "'");
457
            }
458
        } elseif (!is_dir($channelDir)) {
459
            return $this->raiseError("could not create directory '" . $channelDir .
460
                "', already exists and is not a directory");
461
        }
187 mathias 462
 
94 jpm 463
        return true;
464
    }
465
 
466
    /**
467
     * Make sure the directory where we keep registry files for channels exists
468
     *
469
     * @return bool TRUE if directory exists, FALSE if it could not be
470
     * created
471
     *
472
     * @access private
473
     */
474
    function _assertChannelDir()
475
    {
476
        if (!file_exists($this->channelsdir)) {
477
            if (!$this->hasWriteAccess()) {
478
                return false;
479
            }
187 mathias 480
 
94 jpm 481
            require_once 'System.php';
482
            if (!System::mkdir(array('-p', $this->channelsdir))) {
483
                return $this->raiseError("could not create directory '{$this->channelsdir}'");
484
            }
485
        } elseif (!is_dir($this->channelsdir)) {
486
            return $this->raiseError("could not create directory '{$this->channelsdir}" .
487
                "', it already exists and is not a directory");
488
        }
187 mathias 489
 
94 jpm 490
        if (!file_exists($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
491
            if (!$this->hasWriteAccess()) {
492
                return false;
493
            }
187 mathias 494
 
94 jpm 495
            require_once 'System.php';
496
            if (!System::mkdir(array('-p', $this->channelsdir . DIRECTORY_SEPARATOR . '.alias'))) {
497
                return $this->raiseError("could not create directory '{$this->channelsdir}/.alias'");
498
            }
499
        } elseif (!is_dir($this->channelsdir . DIRECTORY_SEPARATOR . '.alias')) {
500
            return $this->raiseError("could not create directory '{$this->channelsdir}" .
501
                "/.alias', it already exists and is not a directory");
502
        }
187 mathias 503
 
94 jpm 504
        return true;
505
    }
506
 
507
    /**
508
     * Get the name of the file where data for a given package is stored.
509
     *
510
     * @param string channel name, or false if this is a PEAR package
511
     * @param string package name
512
     *
513
     * @return string registry file name
514
     *
515
     * @access public
516
     */
517
    function _packageFileName($package, $channel = false)
518
    {
519
        if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
520
            return $this->_channelDirectoryName($channel) . DIRECTORY_SEPARATOR .
521
                strtolower($package) . '.reg';
522
        }
187 mathias 523
 
94 jpm 524
        return $this->statedir . DIRECTORY_SEPARATOR . strtolower($package) . '.reg';
525
    }
526
 
527
    /**
528
     * Get the name of the file where data for a given channel is stored.
529
     * @param string channel name
530
     * @return string registry file name
531
     */
532
    function _channelFileName($channel, $noaliases = false)
533
    {
534
        if (!$noaliases) {
535
            if (file_exists($this->_getChannelAliasFileName($channel))) {
536
                $channel = implode('', file($this->_getChannelAliasFileName($channel)));
537
            }
538
        }
539
        return $this->channelsdir . DIRECTORY_SEPARATOR . str_replace('/', '_',
540
            strtolower($channel)) . '.reg';
541
    }
542
 
543
    /**
544
     * @param string
545
     * @return string
546
     */
547
    function _getChannelAliasFileName($alias)
548
    {
549
        return $this->channelsdir . DIRECTORY_SEPARATOR . '.alias' .
550
              DIRECTORY_SEPARATOR . str_replace('/', '_', strtolower($alias)) . '.txt';
551
    }
552
 
553
    /**
554
     * Get the name of a channel from its alias
555
     */
556
    function _getChannelFromAlias($channel)
557
    {
558
        if (!$this->_channelExists($channel)) {
559
            if ($channel == 'pear.php.net') {
560
                return 'pear.php.net';
561
            }
187 mathias 562
 
94 jpm 563
            if ($channel == 'pecl.php.net') {
564
                return 'pecl.php.net';
565
            }
187 mathias 566
 
567
            if ($channel == 'doc.php.net') {
568
                return 'doc.php.net';
569
            }
570
 
94 jpm 571
            if ($channel == '__uri') {
572
                return '__uri';
573
            }
187 mathias 574
 
94 jpm 575
            return false;
576
        }
187 mathias 577
 
94 jpm 578
        $channel = strtolower($channel);
579
        if (file_exists($this->_getChannelAliasFileName($channel))) {
580
            // translate an alias to an actual channel
581
            return implode('', file($this->_getChannelAliasFileName($channel)));
582
        }
583
 
187 mathias 584
        return $channel;
585
    }
586
 
94 jpm 587
    /**
588
     * Get the alias of a channel from its alias or its name
589
     */
590
    function _getAlias($channel)
591
    {
592
        if (!$this->_channelExists($channel)) {
593
            if ($channel == 'pear.php.net') {
594
                return 'pear';
595
            }
187 mathias 596
 
94 jpm 597
            if ($channel == 'pecl.php.net') {
598
                return 'pecl';
599
            }
187 mathias 600
 
601
            if ($channel == 'doc.php.net') {
602
                return 'phpdocs';
603
            }
604
 
94 jpm 605
            return false;
606
        }
187 mathias 607
 
94 jpm 608
        $channel = $this->_getChannel($channel);
609
        if (PEAR::isError($channel)) {
610
            return $channel;
611
        }
187 mathias 612
 
94 jpm 613
        return $channel->getAlias();
187 mathias 614
    }
94 jpm 615
 
616
    /**
617
     * Get the name of the file where data for a given package is stored.
618
     *
619
     * @param string channel name, or false if this is a PEAR package
620
     * @param string package name
621
     *
622
     * @return string registry file name
623
     *
624
     * @access public
625
     */
626
    function _channelDirectoryName($channel)
627
    {
628
        if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
629
            return $this->statedir;
630
        }
187 mathias 631
 
632
        $ch = $this->_getChannelFromAlias($channel);
633
        if (!$ch) {
634
            $ch = $channel;
635
        }
636
 
637
        return $this->statedir . DIRECTORY_SEPARATOR . strtolower('.channel.' .
638
            str_replace('/', '_', $ch));
94 jpm 639
    }
640
 
641
    function _openPackageFile($package, $mode, $channel = false)
642
    {
643
        if (!$this->_assertStateDir($channel)) {
644
            return null;
645
        }
187 mathias 646
 
94 jpm 647
        if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
648
            return null;
649
        }
187 mathias 650
 
94 jpm 651
        $file = $this->_packageFileName($package, $channel);
652
        if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
653
            return null;
654
        }
187 mathias 655
 
94 jpm 656
        $fp = @fopen($file, $mode);
657
        if (!$fp) {
658
            return null;
659
        }
187 mathias 660
 
94 jpm 661
        return $fp;
662
    }
663
 
664
    function _closePackageFile($fp)
665
    {
666
        fclose($fp);
667
    }
668
 
669
    function _openChannelFile($channel, $mode)
670
    {
671
        if (!$this->_assertChannelDir()) {
672
            return null;
673
        }
187 mathias 674
 
94 jpm 675
        if (!in_array($mode, array('r', 'rb')) && !$this->hasWriteAccess()) {
676
            return null;
677
        }
187 mathias 678
 
94 jpm 679
        $file = $this->_channelFileName($channel);
680
        if (!file_exists($file) && $mode == 'r' || $mode == 'rb') {
681
            return null;
682
        }
187 mathias 683
 
94 jpm 684
        $fp = @fopen($file, $mode);
685
        if (!$fp) {
686
            return null;
687
        }
187 mathias 688
 
94 jpm 689
        return $fp;
690
    }
691
 
692
    function _closeChannelFile($fp)
693
    {
694
        fclose($fp);
695
    }
696
 
697
    function _rebuildFileMap()
698
    {
699
        if (!class_exists('PEAR_Installer_Role')) {
700
            require_once 'PEAR/Installer/Role.php';
701
        }
187 mathias 702
 
94 jpm 703
        $channels = $this->_listAllPackages();
704
        $files = array();
705
        foreach ($channels as $channel => $packages) {
706
            foreach ($packages as $package) {
707
                $version = $this->_packageInfo($package, 'version', $channel);
708
                $filelist = $this->_packageInfo($package, 'filelist', $channel);
709
                if (!is_array($filelist)) {
710
                    continue;
711
                }
187 mathias 712
 
94 jpm 713
                foreach ($filelist as $name => $attrs) {
714
                    if (isset($attrs['attribs'])) {
715
                        $attrs = $attrs['attribs'];
716
                    }
187 mathias 717
 
94 jpm 718
                    // it is possible for conflicting packages in different channels to
719
                    // conflict with data files/doc files
720
                    if ($name == 'dirtree') {
721
                        continue;
722
                    }
187 mathias 723
 
94 jpm 724
                    if (isset($attrs['role']) && !in_array($attrs['role'],
725
                          PEAR_Installer_Role::getInstallableRoles())) {
726
                        // these are not installed
727
                        continue;
728
                    }
187 mathias 729
 
94 jpm 730
                    if (isset($attrs['role']) && !in_array($attrs['role'],
731
                          PEAR_Installer_Role::getBaseinstallRoles())) {
732
                        $attrs['baseinstalldir'] = $package;
733
                    }
187 mathias 734
 
94 jpm 735
                    if (isset($attrs['baseinstalldir'])) {
736
                        $file = $attrs['baseinstalldir'].DIRECTORY_SEPARATOR.$name;
737
                    } else {
738
                        $file = $name;
739
                    }
187 mathias 740
 
94 jpm 741
                    $file = preg_replace(',^/+,', '', $file);
742
                    if ($channel != 'pear.php.net') {
187 mathias 743
                        if (!isset($files[$attrs['role']])) {
744
                            $files[$attrs['role']] = array();
745
                        }
94 jpm 746
                        $files[$attrs['role']][$file] = array(strtolower($channel),
747
                            strtolower($package));
748
                    } else {
749
                        if (!isset($files[$attrs['role']])) {
750
                            $files[$attrs['role']] = array();
751
                        }
752
                        $files[$attrs['role']][$file] = strtolower($package);
753
                    }
754
                }
755
            }
756
        }
187 mathias 757
 
758
 
94 jpm 759
        $this->_assertStateDir();
760
        if (!$this->hasWriteAccess()) {
761
            return false;
762
        }
187 mathias 763
 
94 jpm 764
        $fp = @fopen($this->filemap, 'wb');
765
        if (!$fp) {
766
            return false;
767
        }
187 mathias 768
 
94 jpm 769
        $this->filemap_cache = $files;
770
        fwrite($fp, serialize($files));
771
        fclose($fp);
772
        return true;
773
    }
774
 
775
    function _readFileMap()
776
    {
777
        if (!file_exists($this->filemap)) {
778
            return array();
779
        }
187 mathias 780
 
94 jpm 781
        $fp = @fopen($this->filemap, 'r');
782
        if (!$fp) {
783
            return $this->raiseError('PEAR_Registry: could not open filemap "' . $this->filemap . '"', PEAR_REGISTRY_ERROR_FILE, null, null, $php_errormsg);
784
        }
187 mathias 785
 
94 jpm 786
        clearstatcache();
787
        $fsize = filesize($this->filemap);
788
        fclose($fp);
789
        $data = file_get_contents($this->filemap);
790
        $tmp = unserialize($data);
791
        if (!$tmp && $fsize > 7) {
792
            return $this->raiseError('PEAR_Registry: invalid filemap data', PEAR_REGISTRY_ERROR_FORMAT, null, null, $data);
793
        }
187 mathias 794
 
94 jpm 795
        $this->filemap_cache = $tmp;
796
        return true;
797
    }
798
 
799
    /**
800
     * Lock the registry.
801
     *
802
     * @param integer lock mode, one of LOCK_EX, LOCK_SH or LOCK_UN.
803
     *                See flock manual for more information.
804
     *
805
     * @return bool TRUE on success, FALSE if locking failed, or a
806
     *              PEAR error if some other error occurs (such as the
807
     *              lock file not being writable).
808
     *
809
     * @access private
810
     */
811
    function _lock($mode = LOCK_EX)
812
    {
187 mathias 813
        if (stristr(php_uname(), 'Windows 9')) {
814
            return true;
815
        }
816
 
817
        if ($mode != LOCK_UN && is_resource($this->lock_fp)) {
818
            // XXX does not check type of lock (LOCK_SH/LOCK_EX)
819
            return true;
820
        }
821
 
822
        if (!$this->_assertStateDir()) {
823
            if ($mode == LOCK_EX) {
824
                return $this->raiseError('Registry directory is not writeable by the current user');
94 jpm 825
            }
187 mathias 826
 
827
            return true;
828
        }
829
 
830
        $open_mode = 'w';
831
        // XXX People reported problems with LOCK_SH and 'w'
832
        if ($mode === LOCK_SH || $mode === LOCK_UN) {
833
            if (!file_exists($this->lockfile)) {
834
                touch($this->lockfile);
94 jpm 835
            }
187 mathias 836
            $open_mode = 'r';
837
        }
94 jpm 838
 
187 mathias 839
        if (!is_resource($this->lock_fp)) {
840
            $this->lock_fp = @fopen($this->lockfile, $open_mode);
841
        }
842
 
843
        if (!is_resource($this->lock_fp)) {
844
            $this->lock_fp = null;
845
            return $this->raiseError("could not create lock file" .
846
                                     (isset($php_errormsg) ? ": " . $php_errormsg : ""));
847
        }
848
 
849
        if (!(int)flock($this->lock_fp, $mode)) {
850
            switch ($mode) {
851
                case LOCK_SH: $str = 'shared';    break;
852
                case LOCK_EX: $str = 'exclusive'; break;
853
                case LOCK_UN: $str = 'unlock';    break;
854
                default:      $str = 'unknown';   break;
94 jpm 855
            }
856
 
187 mathias 857
            //is resource at this point, close it on error.
858
            fclose($this->lock_fp);
859
            $this->lock_fp = null;
860
            return $this->raiseError("could not acquire $str lock ($this->lockfile)",
861
                                     PEAR_REGISTRY_ERROR_LOCK);
94 jpm 862
        }
187 mathias 863
 
94 jpm 864
        return true;
865
    }
866
 
867
    function _unlock()
868
    {
869
        $ret = $this->_lock(LOCK_UN);
870
        if (is_resource($this->lock_fp)) {
871
            fclose($this->lock_fp);
872
        }
187 mathias 873
 
94 jpm 874
        $this->lock_fp = null;
875
        return $ret;
876
    }
877
 
878
    function _packageExists($package, $channel = false)
879
    {
880
        return file_exists($this->_packageFileName($package, $channel));
881
    }
882
 
883
    /**
884
     * Determine whether a channel exists in the registry
187 mathias 885
     *
94 jpm 886
     * @param string Channel name
887
     * @param bool if true, then aliases will be ignored
888
     * @return boolean
889
     */
890
    function _channelExists($channel, $noaliases = false)
891
    {
892
        $a = file_exists($this->_channelFileName($channel, $noaliases));
893
        if (!$a && $channel == 'pear.php.net') {
894
            return true;
895
        }
187 mathias 896
 
94 jpm 897
        if (!$a && $channel == 'pecl.php.net') {
898
            return true;
899
        }
187 mathias 900
 
901
        if (!$a && $channel == 'doc.php.net') {
902
            return true;
903
        }
904
 
94 jpm 905
        return $a;
906
    }
907
 
187 mathias 908
    /**
909
     * Determine whether a mirror exists within the deafult channel in the registry
910
     *
911
     * @param string Channel name
912
     * @param string Mirror name
913
     *
914
     * @return boolean
915
     */
916
    function _mirrorExists($channel, $mirror)
917
    {
918
        $data = $this->_channelInfo($channel);
919
        if (!isset($data['servers']['mirror'])) {
920
            return false;
921
        }
94 jpm 922
 
187 mathias 923
        foreach ($data['servers']['mirror'] as $m) {
924
            if ($m['attribs']['host'] == $mirror) {
925
                return true;
926
            }
927
        }
928
 
929
        return false;
930
    }
931
 
94 jpm 932
    /**
933
     * @param PEAR_ChannelFile Channel object
934
     * @param donotuse
935
     * @param string Last-Modified HTTP tag from remote request
936
     * @return boolean|PEAR_Error True on creation, false if it already exists
937
     */
938
    function _addChannel($channel, $update = false, $lastmodified = false)
939
    {
940
        if (!is_a($channel, 'PEAR_ChannelFile')) {
941
            return false;
942
        }
187 mathias 943
 
94 jpm 944
        if (!$channel->validate()) {
945
            return false;
946
        }
187 mathias 947
 
94 jpm 948
        if (file_exists($this->_channelFileName($channel->getName()))) {
949
            if (!$update) {
950
                return false;
951
            }
187 mathias 952
 
94 jpm 953
            $checker = $this->_getChannel($channel->getName());
954
            if (PEAR::isError($checker)) {
955
                return $checker;
956
            }
187 mathias 957
 
94 jpm 958
            if ($channel->getAlias() != $checker->getAlias()) {
959
                if (file_exists($this->_getChannelAliasFileName($checker->getAlias()))) {
960
                    @unlink($this->_getChannelAliasFileName($checker->getAlias()));
961
                }
962
            }
963
        } else {
187 mathias 964
            if ($update && !in_array($channel->getName(), array('pear.php.net', 'pecl.php.net', 'doc.php.net'))) {
94 jpm 965
                return false;
966
            }
967
        }
187 mathias 968
 
94 jpm 969
        $ret = $this->_assertChannelDir();
970
        if (PEAR::isError($ret)) {
971
            return $ret;
972
        }
187 mathias 973
 
94 jpm 974
        $ret = $this->_assertChannelStateDir($channel->getName());
975
        if (PEAR::isError($ret)) {
976
            return $ret;
977
        }
187 mathias 978
 
94 jpm 979
        if ($channel->getAlias() != $channel->getName()) {
980
            if (file_exists($this->_getChannelAliasFileName($channel->getAlias())) &&
981
                  $this->_getChannelFromAlias($channel->getAlias()) != $channel->getName()) {
982
                $channel->setAlias($channel->getName());
983
            }
187 mathias 984
 
94 jpm 985
            if (!$this->hasWriteAccess()) {
986
                return false;
987
            }
187 mathias 988
 
94 jpm 989
            $fp = @fopen($this->_getChannelAliasFileName($channel->getAlias()), 'w');
990
            if (!$fp) {
991
                return false;
992
            }
187 mathias 993
 
94 jpm 994
            fwrite($fp, $channel->getName());
995
            fclose($fp);
996
        }
187 mathias 997
 
94 jpm 998
        if (!$this->hasWriteAccess()) {
999
            return false;
1000
        }
187 mathias 1001
 
94 jpm 1002
        $fp = @fopen($this->_channelFileName($channel->getName()), 'wb');
1003
        if (!$fp) {
1004
            return false;
1005
        }
187 mathias 1006
 
94 jpm 1007
        $info = $channel->toArray();
1008
        if ($lastmodified) {
1009
            $info['_lastmodified'] = $lastmodified;
1010
        } else {
1011
            $info['_lastmodified'] = date('r');
1012
        }
187 mathias 1013
 
94 jpm 1014
        fwrite($fp, serialize($info));
1015
        fclose($fp);
1016
        return true;
1017
    }
1018
 
1019
    /**
1020
     * Deletion fails if there are any packages installed from the channel
1021
     * @param string|PEAR_ChannelFile channel name
1022
     * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
1023
     */
1024
    function _deleteChannel($channel)
1025
    {
1026
        if (!is_string($channel)) {
187 mathias 1027
            if (!is_a($channel, 'PEAR_ChannelFile')) {
94 jpm 1028
                return false;
1029
            }
187 mathias 1030
 
1031
            if (!$channel->validate()) {
1032
                return false;
1033
            }
1034
            $channel = $channel->getName();
94 jpm 1035
        }
187 mathias 1036
 
94 jpm 1037
        if ($this->_getChannelFromAlias($channel) == '__uri') {
1038
            return false;
1039
        }
187 mathias 1040
 
94 jpm 1041
        if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
1042
            return false;
1043
        }
187 mathias 1044
 
1045
        if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
1046
            return false;
1047
        }
1048
 
94 jpm 1049
        if (!$this->_channelExists($channel)) {
1050
            return false;
1051
        }
187 mathias 1052
 
94 jpm 1053
        if (!$channel || $this->_getChannelFromAlias($channel) == 'pear.php.net') {
1054
            return false;
1055
        }
187 mathias 1056
 
94 jpm 1057
        $channel = $this->_getChannelFromAlias($channel);
1058
        if ($channel == 'pear.php.net') {
1059
            return false;
1060
        }
187 mathias 1061
 
94 jpm 1062
        $test = $this->_listChannelPackages($channel);
1063
        if (count($test)) {
1064
            return false;
1065
        }
187 mathias 1066
 
94 jpm 1067
        $test = @rmdir($this->_channelDirectoryName($channel));
1068
        if (!$test) {
1069
            return false;
1070
        }
187 mathias 1071
 
94 jpm 1072
        $file = $this->_getChannelAliasFileName($this->_getAlias($channel));
1073
        if (file_exists($file)) {
1074
            $test = @unlink($file);
1075
            if (!$test) {
1076
                return false;
1077
            }
1078
        }
187 mathias 1079
 
94 jpm 1080
        $file = $this->_channelFileName($channel);
1081
        $ret = true;
1082
        if (file_exists($file)) {
1083
            $ret = @unlink($file);
1084
        }
187 mathias 1085
 
94 jpm 1086
        return $ret;
1087
    }
1088
 
1089
    /**
1090
     * Determine whether a channel exists in the registry
1091
     * @param string Channel Alias
1092
     * @return boolean
1093
     */
1094
    function _isChannelAlias($alias)
1095
    {
1096
        return file_exists($this->_getChannelAliasFileName($alias));
1097
    }
1098
 
1099
    /**
1100
     * @param string|null
1101
     * @param string|null
1102
     * @param string|null
1103
     * @return array|null
1104
     * @access private
1105
     */
1106
    function _packageInfo($package = null, $key = null, $channel = 'pear.php.net')
1107
    {
1108
        if ($package === null) {
1109
            if ($channel === null) {
1110
                $channels = $this->_listChannels();
1111
                $ret = array();
1112
                foreach ($channels as $channel) {
1113
                    $channel = strtolower($channel);
1114
                    $ret[$channel] = array();
1115
                    $packages = $this->_listPackages($channel);
1116
                    foreach ($packages as $package) {
1117
                        $ret[$channel][] = $this->_packageInfo($package, null, $channel);
1118
                    }
1119
                }
187 mathias 1120
 
94 jpm 1121
                return $ret;
1122
            }
187 mathias 1123
 
94 jpm 1124
            $ps = $this->_listPackages($channel);
1125
            if (!count($ps)) {
1126
                return array();
1127
            }
1128
            return array_map(array(&$this, '_packageInfo'),
1129
                             $ps, array_fill(0, count($ps), null),
1130
                             array_fill(0, count($ps), $channel));
1131
        }
187 mathias 1132
 
94 jpm 1133
        $fp = $this->_openPackageFile($package, 'r', $channel);
1134
        if ($fp === null) {
1135
            return null;
1136
        }
187 mathias 1137
 
94 jpm 1138
        clearstatcache();
1139
        $this->_closePackageFile($fp);
1140
        $data = file_get_contents($this->_packageFileName($package, $channel));
1141
        $data = unserialize($data);
1142
        if ($key === null) {
1143
            return $data;
1144
        }
187 mathias 1145
 
94 jpm 1146
        // compatibility for package.xml version 2.0
1147
        if (isset($data['old'][$key])) {
1148
            return $data['old'][$key];
1149
        }
187 mathias 1150
 
94 jpm 1151
        if (isset($data[$key])) {
1152
            return $data[$key];
1153
        }
187 mathias 1154
 
94 jpm 1155
        return null;
1156
    }
1157
 
1158
    /**
1159
     * @param string Channel name
1160
     * @param bool whether to strictly retrieve info of channels, not just aliases
1161
     * @return array|null
1162
     */
1163
    function _channelInfo($channel, $noaliases = false)
1164
    {
1165
        if (!$this->_channelExists($channel, $noaliases)) {
1166
            return null;
1167
        }
187 mathias 1168
 
94 jpm 1169
        $fp = $this->_openChannelFile($channel, 'r');
1170
        if ($fp === null) {
1171
            return null;
1172
        }
187 mathias 1173
 
94 jpm 1174
        clearstatcache();
1175
        $this->_closeChannelFile($fp);
1176
        $data = file_get_contents($this->_channelFileName($channel));
1177
        $data = unserialize($data);
1178
        return $data;
1179
    }
1180
 
1181
    function _listChannels()
1182
    {
1183
        $channellist = array();
1184
        if (!file_exists($this->channelsdir) || !is_dir($this->channelsdir)) {
187 mathias 1185
            return array('pear.php.net', 'pecl.php.net', 'doc.php.net', '__uri');
94 jpm 1186
        }
187 mathias 1187
 
94 jpm 1188
        $dp = opendir($this->channelsdir);
1189
        while ($ent = readdir($dp)) {
1190
            if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1191
                continue;
1192
            }
187 mathias 1193
 
94 jpm 1194
            if ($ent == '__uri.reg') {
1195
                $channellist[] = '__uri';
1196
                continue;
1197
            }
187 mathias 1198
 
94 jpm 1199
            $channellist[] = str_replace('_', '/', substr($ent, 0, -4));
1200
        }
187 mathias 1201
 
94 jpm 1202
        closedir($dp);
1203
        if (!in_array('pear.php.net', $channellist)) {
1204
            $channellist[] = 'pear.php.net';
1205
        }
187 mathias 1206
 
94 jpm 1207
        if (!in_array('pecl.php.net', $channellist)) {
1208
            $channellist[] = 'pecl.php.net';
1209
        }
187 mathias 1210
 
1211
        if (!in_array('doc.php.net', $channellist)) {
1212
            $channellist[] = 'doc.php.net';
1213
        }
1214
 
1215
 
94 jpm 1216
        if (!in_array('__uri', $channellist)) {
1217
            $channellist[] = '__uri';
1218
        }
187 mathias 1219
 
1220
        natsort($channellist);
94 jpm 1221
        return $channellist;
1222
    }
1223
 
1224
    function _listPackages($channel = false)
1225
    {
1226
        if ($channel && $this->_getChannelFromAlias($channel) != 'pear.php.net') {
1227
            return $this->_listChannelPackages($channel);
1228
        }
187 mathias 1229
 
94 jpm 1230
        if (!file_exists($this->statedir) || !is_dir($this->statedir)) {
1231
            return array();
1232
        }
187 mathias 1233
 
94 jpm 1234
        $pkglist = array();
1235
        $dp = opendir($this->statedir);
1236
        if (!$dp) {
1237
            return $pkglist;
1238
        }
187 mathias 1239
 
94 jpm 1240
        while ($ent = readdir($dp)) {
1241
            if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1242
                continue;
1243
            }
187 mathias 1244
 
94 jpm 1245
            $pkglist[] = substr($ent, 0, -4);
1246
        }
1247
        closedir($dp);
1248
        return $pkglist;
1249
    }
1250
 
1251
    function _listChannelPackages($channel)
1252
    {
1253
        $pkglist = array();
1254
        if (!file_exists($this->_channelDirectoryName($channel)) ||
1255
              !is_dir($this->_channelDirectoryName($channel))) {
1256
            return array();
1257
        }
187 mathias 1258
 
94 jpm 1259
        $dp = opendir($this->_channelDirectoryName($channel));
1260
        if (!$dp) {
1261
            return $pkglist;
1262
        }
187 mathias 1263
 
94 jpm 1264
        while ($ent = readdir($dp)) {
1265
            if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
1266
                continue;
1267
            }
1268
            $pkglist[] = substr($ent, 0, -4);
1269
        }
187 mathias 1270
 
94 jpm 1271
        closedir($dp);
1272
        return $pkglist;
1273
    }
1274
 
1275
    function _listAllPackages()
1276
    {
1277
        $ret = array();
1278
        foreach ($this->_listChannels() as $channel) {
1279
            $ret[$channel] = $this->_listPackages($channel);
1280
        }
187 mathias 1281
 
94 jpm 1282
        return $ret;
1283
    }
1284
 
1285
    /**
1286
     * Add an installed package to the registry
1287
     * @param string package name
1288
     * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
1289
     * @return bool success of saving
1290
     * @access private
1291
     */
1292
    function _addPackage($package, $info)
1293
    {
1294
        if ($this->_packageExists($package)) {
1295
            return false;
1296
        }
187 mathias 1297
 
94 jpm 1298
        $fp = $this->_openPackageFile($package, 'wb');
1299
        if ($fp === null) {
1300
            return false;
1301
        }
187 mathias 1302
 
94 jpm 1303
        $info['_lastmodified'] = time();
1304
        fwrite($fp, serialize($info));
1305
        $this->_closePackageFile($fp);
1306
        if (isset($info['filelist'])) {
1307
            $this->_rebuildFileMap();
1308
        }
187 mathias 1309
 
94 jpm 1310
        return true;
1311
    }
1312
 
1313
    /**
1314
     * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
1315
     * @return bool
1316
     * @access private
1317
     */
1318
    function _addPackage2($info)
1319
    {
187 mathias 1320
        if (!is_a($info, 'PEAR_PackageFile_v1') && !is_a($info, 'PEAR_PackageFile_v2')) {
1321
            return false;
1322
        }
1323
 
94 jpm 1324
        if (!$info->validate()) {
1325
            if (class_exists('PEAR_Common')) {
1326
                $ui = PEAR_Frontend::singleton();
1327
                if ($ui) {
1328
                    foreach ($info->getValidationWarnings() as $err) {
1329
                        $ui->log($err['message'], true);
1330
                    }
1331
                }
1332
            }
1333
            return false;
1334
        }
187 mathias 1335
 
94 jpm 1336
        $channel = $info->getChannel();
1337
        $package = $info->getPackage();
1338
        $save = $info;
1339
        if ($this->_packageExists($package, $channel)) {
1340
            return false;
1341
        }
187 mathias 1342
 
94 jpm 1343
        if (!$this->_channelExists($channel, true)) {
1344
            return false;
1345
        }
187 mathias 1346
 
94 jpm 1347
        $info = $info->toArray(true);
1348
        if (!$info) {
1349
            return false;
1350
        }
187 mathias 1351
 
94 jpm 1352
        $fp = $this->_openPackageFile($package, 'wb', $channel);
1353
        if ($fp === null) {
1354
            return false;
1355
        }
187 mathias 1356
 
94 jpm 1357
        $info['_lastmodified'] = time();
1358
        fwrite($fp, serialize($info));
1359
        $this->_closePackageFile($fp);
1360
        $this->_rebuildFileMap();
1361
        return true;
1362
    }
1363
 
1364
    /**
1365
     * @param string Package name
1366
     * @param array parsed package.xml 1.0
1367
     * @param bool this parameter is only here for BC.  Don't use it.
1368
     * @access private
1369
     */
1370
    function _updatePackage($package, $info, $merge = true)
1371
    {
1372
        $oldinfo = $this->_packageInfo($package);
1373
        if (empty($oldinfo)) {
1374
            return false;
1375
        }
187 mathias 1376
 
94 jpm 1377
        $fp = $this->_openPackageFile($package, 'w');
1378
        if ($fp === null) {
1379
            return false;
1380
        }
187 mathias 1381
 
94 jpm 1382
        if (is_object($info)) {
1383
            $info = $info->toArray();
1384
        }
1385
        $info['_lastmodified'] = time();
187 mathias 1386
 
94 jpm 1387
        $newinfo = $info;
1388
        if ($merge) {
1389
            $info = array_merge($oldinfo, $info);
1390
        } else {
1391
            $diff = $info;
1392
        }
187 mathias 1393
 
94 jpm 1394
        fwrite($fp, serialize($info));
1395
        $this->_closePackageFile($fp);
1396
        if (isset($newinfo['filelist'])) {
1397
            $this->_rebuildFileMap();
1398
        }
187 mathias 1399
 
94 jpm 1400
        return true;
1401
    }
1402
 
1403
    /**
1404
     * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
1405
     * @return bool
1406
     * @access private
1407
     */
1408
    function _updatePackage2($info)
1409
    {
1410
        if (!$this->_packageExists($info->getPackage(), $info->getChannel())) {
1411
            return false;
1412
        }
187 mathias 1413
 
94 jpm 1414
        $fp = $this->_openPackageFile($info->getPackage(), 'w', $info->getChannel());
1415
        if ($fp === null) {
1416
            return false;
1417
        }
187 mathias 1418
 
94 jpm 1419
        $save = $info;
1420
        $info = $save->getArray(true);
1421
        $info['_lastmodified'] = time();
1422
        fwrite($fp, serialize($info));
1423
        $this->_closePackageFile($fp);
1424
        $this->_rebuildFileMap();
1425
        return true;
1426
    }
1427
 
1428
    /**
1429
     * @param string Package name
1430
     * @param string Channel name
1431
     * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
1432
     * @access private
1433
     */
1434
    function &_getPackage($package, $channel = 'pear.php.net')
1435
    {
1436
        $info = $this->_packageInfo($package, null, $channel);
1437
        if ($info === null) {
1438
            return $info;
1439
        }
187 mathias 1440
 
94 jpm 1441
        $a = $this->_config;
1442
        if (!$a) {
187 mathias 1443
            $this->_config = new PEAR_Config;
94 jpm 1444
            $this->_config->set('php_dir', $this->statedir);
1445
        }
187 mathias 1446
 
94 jpm 1447
        if (!class_exists('PEAR_PackageFile')) {
1448
            require_once 'PEAR/PackageFile.php';
1449
        }
187 mathias 1450
 
1451
        $pkg = new PEAR_PackageFile($this->_config);
94 jpm 1452
        $pf = &$pkg->fromArray($info);
1453
        return $pf;
1454
    }
1455
 
1456
    /**
1457
     * @param string channel name
1458
     * @param bool whether to strictly retrieve channel names
1459
     * @return PEAR_ChannelFile|PEAR_Error
1460
     * @access private
1461
     */
1462
    function &_getChannel($channel, $noaliases = false)
1463
    {
1464
        $ch = false;
1465
        if ($this->_channelExists($channel, $noaliases)) {
1466
            $chinfo = $this->_channelInfo($channel, $noaliases);
1467
            if ($chinfo) {
1468
                if (!class_exists('PEAR_ChannelFile')) {
1469
                    require_once 'PEAR/ChannelFile.php';
1470
                }
187 mathias 1471
 
94 jpm 1472
                $ch = &PEAR_ChannelFile::fromArrayWithErrors($chinfo);
1473
            }
1474
        }
187 mathias 1475
 
94 jpm 1476
        if ($ch) {
1477
            if ($ch->validate()) {
1478
                return $ch;
1479
            }
187 mathias 1480
 
94 jpm 1481
            foreach ($ch->getErrors(true) as $err) {
1482
                $message = $err['message'] . "\n";
1483
            }
187 mathias 1484
 
94 jpm 1485
            $ch = PEAR::raiseError($message);
1486
            return $ch;
1487
        }
187 mathias 1488
 
94 jpm 1489
        if ($this->_getChannelFromAlias($channel) == 'pear.php.net') {
1490
            // the registry is not properly set up, so use defaults
1491
            if (!class_exists('PEAR_ChannelFile')) {
1492
                require_once 'PEAR/ChannelFile.php';
1493
            }
187 mathias 1494
 
94 jpm 1495
            $pear_channel = new PEAR_ChannelFile;
187 mathias 1496
            $pear_channel->setServer('pear.php.net');
94 jpm 1497
            $pear_channel->setAlias('pear');
1498
            $pear_channel->setSummary('PHP Extension and Application Repository');
1499
            $pear_channel->setDefaultPEARProtocols();
1500
            $pear_channel->setBaseURL('REST1.0', 'http://pear.php.net/rest/');
1501
            $pear_channel->setBaseURL('REST1.1', 'http://pear.php.net/rest/');
187 mathias 1502
            $pear_channel->setBaseURL('REST1.3', 'http://pear.php.net/rest/');
94 jpm 1503
            return $pear_channel;
1504
        }
187 mathias 1505
 
94 jpm 1506
        if ($this->_getChannelFromAlias($channel) == 'pecl.php.net') {
1507
            // the registry is not properly set up, so use defaults
1508
            if (!class_exists('PEAR_ChannelFile')) {
1509
                require_once 'PEAR/ChannelFile.php';
1510
            }
1511
            $pear_channel = new PEAR_ChannelFile;
187 mathias 1512
            $pear_channel->setServer('pecl.php.net');
94 jpm 1513
            $pear_channel->setAlias('pecl');
1514
            $pear_channel->setSummary('PHP Extension Community Library');
1515
            $pear_channel->setDefaultPEARProtocols();
1516
            $pear_channel->setBaseURL('REST1.0', 'http://pecl.php.net/rest/');
1517
            $pear_channel->setBaseURL('REST1.1', 'http://pecl.php.net/rest/');
1518
            $pear_channel->setValidationPackage('PEAR_Validator_PECL', '1.0');
1519
            return $pear_channel;
1520
        }
187 mathias 1521
 
1522
        if ($this->_getChannelFromAlias($channel) == 'doc.php.net') {
1523
            // the registry is not properly set up, so use defaults
1524
            if (!class_exists('PEAR_ChannelFile')) {
1525
                require_once 'PEAR/ChannelFile.php';
1526
            }
1527
 
1528
            $doc_channel = new PEAR_ChannelFile;
1529
            $doc_channel->setServer('doc.php.net');
1530
            $doc_channel->setAlias('phpdocs');
1531
            $doc_channel->setSummary('PHP Documentation Team');
1532
            $doc_channel->setDefaultPEARProtocols();
1533
            $doc_channel->setBaseURL('REST1.0', 'http://doc.php.net/rest/');
1534
            $doc_channel->setBaseURL('REST1.1', 'http://doc.php.net/rest/');
1535
            $doc_channel->setBaseURL('REST1.3', 'http://doc.php.net/rest/');
1536
            return $doc_channel;
1537
        }
1538
 
1539
 
94 jpm 1540
        if ($this->_getChannelFromAlias($channel) == '__uri') {
1541
            // the registry is not properly set up, so use defaults
1542
            if (!class_exists('PEAR_ChannelFile')) {
1543
                require_once 'PEAR/ChannelFile.php';
1544
            }
187 mathias 1545
 
94 jpm 1546
            $private = new PEAR_ChannelFile;
1547
            $private->setName('__uri');
187 mathias 1548
            $private->setDefaultPEARProtocols();
1549
            $private->setBaseURL('REST1.0', '****');
94 jpm 1550
            $private->setSummary('Pseudo-channel for static packages');
1551
            return $private;
1552
        }
187 mathias 1553
 
94 jpm 1554
        return $ch;
1555
    }
1556
 
1557
    /**
1558
     * @param string Package name
1559
     * @param string Channel name
1560
     * @return bool
1561
     */
1562
    function packageExists($package, $channel = 'pear.php.net')
1563
    {
1564
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1565
            return $e;
1566
        }
1567
        $ret = $this->_packageExists($package, $channel);
1568
        $this->_unlock();
1569
        return $ret;
1570
    }
1571
 
1572
    // }}}
1573
 
1574
    // {{{ channelExists()
1575
 
1576
    /**
1577
     * @param string channel name
1578
     * @param bool if true, then aliases will be ignored
1579
     * @return bool
1580
     */
1581
    function channelExists($channel, $noaliases = false)
1582
    {
1583
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1584
            return $e;
1585
        }
1586
        $ret = $this->_channelExists($channel, $noaliases);
1587
        $this->_unlock();
1588
        return $ret;
1589
    }
1590
 
1591
    // }}}
1592
 
187 mathias 1593
    /**
1594
     * @param string channel name mirror is in
1595
     * @param string mirror name
1596
     *
1597
     * @return bool
1598
     */
1599
    function mirrorExists($channel, $mirror)
1600
    {
1601
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1602
            return $e;
1603
        }
1604
 
1605
        $ret = $this->_mirrorExists($channel, $mirror);
1606
        $this->_unlock();
1607
        return $ret;
1608
    }
1609
 
94 jpm 1610
    // {{{ isAlias()
1611
 
1612
    /**
1613
     * Determines whether the parameter is an alias of a channel
1614
     * @param string
1615
     * @return bool
1616
     */
1617
    function isAlias($alias)
1618
    {
1619
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1620
            return $e;
1621
        }
1622
        $ret = $this->_isChannelAlias($alias);
1623
        $this->_unlock();
1624
        return $ret;
1625
    }
1626
 
1627
    // }}}
1628
    // {{{ packageInfo()
1629
 
1630
    /**
1631
     * @param string|null
1632
     * @param string|null
1633
     * @param string
1634
     * @return array|null
1635
     */
1636
    function packageInfo($package = null, $key = null, $channel = 'pear.php.net')
1637
    {
1638
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1639
            return $e;
1640
        }
1641
        $ret = $this->_packageInfo($package, $key, $channel);
1642
        $this->_unlock();
1643
        return $ret;
1644
    }
1645
 
1646
    // }}}
1647
    // {{{ channelInfo()
1648
 
1649
    /**
1650
     * Retrieve a raw array of channel data.
1651
     *
1652
     * Do not use this, instead use {@link getChannel()} for normal
1653
     * operations.  Array structure is undefined in this method
1654
     * @param string channel name
1655
     * @param bool whether to strictly retrieve information only on non-aliases
1656
     * @return array|null|PEAR_Error
1657
     */
1658
    function channelInfo($channel = null, $noaliases = false)
1659
    {
1660
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1661
            return $e;
1662
        }
1663
        $ret = $this->_channelInfo($channel, $noaliases);
1664
        $this->_unlock();
1665
        return $ret;
1666
    }
1667
 
1668
    // }}}
1669
 
1670
    /**
1671
     * @param string
1672
     */
1673
    function channelName($channel)
1674
    {
1675
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1676
            return $e;
1677
        }
1678
        $ret = $this->_getChannelFromAlias($channel);
1679
        $this->_unlock();
1680
        return $ret;
1681
    }
1682
 
1683
    /**
1684
     * @param string
1685
     */
1686
    function channelAlias($channel)
1687
    {
1688
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1689
            return $e;
1690
        }
1691
        $ret = $this->_getAlias($channel);
1692
        $this->_unlock();
1693
        return $ret;
1694
    }
1695
    // {{{ listPackages()
1696
 
1697
    function listPackages($channel = false)
1698
    {
1699
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1700
            return $e;
1701
        }
1702
        $ret = $this->_listPackages($channel);
1703
        $this->_unlock();
1704
        return $ret;
1705
    }
1706
 
1707
    // }}}
1708
    // {{{ listAllPackages()
1709
 
1710
    function listAllPackages()
1711
    {
1712
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1713
            return $e;
1714
        }
1715
        $ret = $this->_listAllPackages();
1716
        $this->_unlock();
1717
        return $ret;
1718
    }
1719
 
1720
    // }}}
1721
    // {{{ listChannel()
1722
 
1723
    function listChannels()
1724
    {
1725
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1726
            return $e;
1727
        }
1728
        $ret = $this->_listChannels();
1729
        $this->_unlock();
1730
        return $ret;
1731
    }
1732
 
1733
    // }}}
1734
    // {{{ addPackage()
1735
 
1736
    /**
1737
     * Add an installed package to the registry
1738
     * @param string|PEAR_PackageFile_v1|PEAR_PackageFile_v2 package name or object
1739
     *               that will be passed to {@link addPackage2()}
1740
     * @param array package info (parsed by PEAR_Common::infoFrom*() methods)
1741
     * @return bool success of saving
1742
     */
1743
    function addPackage($package, $info)
1744
    {
1745
        if (is_object($info)) {
1746
            return $this->addPackage2($info);
1747
        }
1748
        if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1749
            return $e;
1750
        }
1751
        $ret = $this->_addPackage($package, $info);
1752
        $this->_unlock();
1753
        if ($ret) {
1754
            if (!class_exists('PEAR_PackageFile_v1')) {
1755
                require_once 'PEAR/PackageFile/v1.php';
1756
            }
1757
            $pf = new PEAR_PackageFile_v1;
1758
            $pf->setConfig($this->_config);
1759
            $pf->fromArray($info);
1760
            $this->_dependencyDB->uninstallPackage($pf);
1761
            $this->_dependencyDB->installPackage($pf);
1762
        }
1763
        return $ret;
1764
    }
1765
 
1766
    // }}}
1767
    // {{{ addPackage2()
1768
 
1769
    function addPackage2($info)
1770
    {
1771
        if (!is_object($info)) {
1772
            return $this->addPackage($info['package'], $info);
1773
        }
1774
        if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1775
            return $e;
1776
        }
1777
        $ret = $this->_addPackage2($info);
1778
        $this->_unlock();
1779
        if ($ret) {
1780
            $this->_dependencyDB->uninstallPackage($info);
1781
            $this->_dependencyDB->installPackage($info);
1782
        }
1783
        return $ret;
1784
    }
1785
 
1786
    // }}}
1787
    // {{{ updateChannel()
1788
 
1789
    /**
1790
     * For future expandibility purposes, separate this
1791
     * @param PEAR_ChannelFile
1792
     */
1793
    function updateChannel($channel, $lastmodified = null)
1794
    {
1795
        if ($channel->getName() == '__uri') {
1796
            return false;
1797
        }
1798
        return $this->addChannel($channel, $lastmodified, true);
1799
    }
1800
 
1801
    // }}}
1802
    // {{{ deleteChannel()
1803
 
1804
    /**
1805
     * Deletion fails if there are any packages installed from the channel
1806
     * @param string|PEAR_ChannelFile channel name
1807
     * @return boolean|PEAR_Error True on deletion, false if it doesn't exist
1808
     */
1809
    function deleteChannel($channel)
1810
    {
1811
        if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1812
            return $e;
1813
        }
187 mathias 1814
 
94 jpm 1815
        $ret = $this->_deleteChannel($channel);
1816
        $this->_unlock();
1817
        if ($ret && is_a($this->_config, 'PEAR_Config')) {
1818
            $this->_config->setChannels($this->listChannels());
1819
        }
187 mathias 1820
 
94 jpm 1821
        return $ret;
1822
    }
1823
 
1824
    // }}}
1825
    // {{{ addChannel()
1826
 
1827
    /**
1828
     * @param PEAR_ChannelFile Channel object
1829
     * @param string Last-Modified header from HTTP for caching
1830
     * @return boolean|PEAR_Error True on creation, false if it already exists
1831
     */
1832
    function addChannel($channel, $lastmodified = false, $update = false)
1833
    {
187 mathias 1834
        if (!is_a($channel, 'PEAR_ChannelFile') || !$channel->validate()) {
94 jpm 1835
            return false;
1836
        }
187 mathias 1837
 
94 jpm 1838
        if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1839
            return $e;
1840
        }
187 mathias 1841
 
94 jpm 1842
        $ret = $this->_addChannel($channel, $update, $lastmodified);
1843
        $this->_unlock();
1844
        if (!$update && $ret && is_a($this->_config, 'PEAR_Config')) {
1845
            $this->_config->setChannels($this->listChannels());
1846
        }
187 mathias 1847
 
94 jpm 1848
        return $ret;
1849
    }
1850
 
1851
    // }}}
1852
    // {{{ deletePackage()
1853
 
1854
    function deletePackage($package, $channel = 'pear.php.net')
1855
    {
1856
        if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1857
            return $e;
1858
        }
187 mathias 1859
 
94 jpm 1860
        $file = $this->_packageFileName($package, $channel);
187 mathias 1861
        $ret  = file_exists($file) ? @unlink($file) : false;
94 jpm 1862
        $this->_rebuildFileMap();
1863
        $this->_unlock();
1864
        $p = array('channel' => $channel, 'package' => $package);
1865
        $this->_dependencyDB->uninstallPackage($p);
1866
        return $ret;
1867
    }
1868
 
1869
    // }}}
1870
    // {{{ updatePackage()
1871
 
1872
    function updatePackage($package, $info, $merge = true)
1873
    {
1874
        if (is_object($info)) {
1875
            return $this->updatePackage2($info, $merge);
1876
        }
1877
        if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1878
            return $e;
1879
        }
1880
        $ret = $this->_updatePackage($package, $info, $merge);
1881
        $this->_unlock();
1882
        if ($ret) {
1883
            if (!class_exists('PEAR_PackageFile_v1')) {
1884
                require_once 'PEAR/PackageFile/v1.php';
1885
            }
1886
            $pf = new PEAR_PackageFile_v1;
1887
            $pf->setConfig($this->_config);
1888
            $pf->fromArray($this->packageInfo($package));
1889
            $this->_dependencyDB->uninstallPackage($pf);
1890
            $this->_dependencyDB->installPackage($pf);
1891
        }
1892
        return $ret;
1893
    }
1894
 
1895
    // }}}
1896
    // {{{ updatePackage2()
1897
 
1898
    function updatePackage2($info)
1899
    {
187 mathias 1900
 
94 jpm 1901
        if (!is_object($info)) {
1902
            return $this->updatePackage($info['package'], $info, $merge);
1903
        }
187 mathias 1904
 
94 jpm 1905
        if (!$info->validate(PEAR_VALIDATE_DOWNLOADING)) {
1906
            return false;
1907
        }
187 mathias 1908
 
94 jpm 1909
        if (PEAR::isError($e = $this->_lock(LOCK_EX))) {
1910
            return $e;
1911
        }
187 mathias 1912
 
94 jpm 1913
        $ret = $this->_updatePackage2($info);
1914
        $this->_unlock();
1915
        if ($ret) {
1916
            $this->_dependencyDB->uninstallPackage($info);
1917
            $this->_dependencyDB->installPackage($info);
1918
        }
187 mathias 1919
 
94 jpm 1920
        return $ret;
1921
    }
1922
 
1923
    // }}}
1924
    // {{{ getChannel()
1925
    /**
1926
     * @param string channel name
1927
     * @param bool whether to strictly return raw channels (no aliases)
1928
     * @return PEAR_ChannelFile|PEAR_Error
1929
     */
187 mathias 1930
    function getChannel($channel, $noaliases = false)
94 jpm 1931
    {
1932
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1933
            return $e;
1934
        }
187 mathias 1935
        $ret = $this->_getChannel($channel, $noaliases);
1936
        $this->_unlock();
94 jpm 1937
        if (!$ret) {
1938
            return PEAR::raiseError('Unknown channel: ' . $channel);
1939
        }
1940
        return $ret;
1941
    }
1942
 
1943
    // }}}
1944
    // {{{ getPackage()
1945
    /**
1946
     * @param string package name
1947
     * @param string channel name
1948
     * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2|null
1949
     */
1950
    function &getPackage($package, $channel = 'pear.php.net')
1951
    {
1952
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
1953
            return $e;
1954
        }
1955
        $pf = &$this->_getPackage($package, $channel);
1956
        $this->_unlock();
1957
        return $pf;
1958
    }
1959
 
1960
    // }}}
1961
 
1962
    /**
1963
     * Get PEAR_PackageFile_v[1/2] objects representing the contents of
1964
     * a dependency group that are installed.
1965
     *
1966
     * This is used at uninstall-time
1967
     * @param array
1968
     * @return array|false
1969
     */
1970
    function getInstalledGroup($group)
1971
    {
1972
        $ret = array();
1973
        if (isset($group['package'])) {
1974
            if (!isset($group['package'][0])) {
1975
                $group['package'] = array($group['package']);
1976
            }
1977
            foreach ($group['package'] as $package) {
1978
                $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
1979
                $p = &$this->getPackage($package['name'], $depchannel);
1980
                if ($p) {
1981
                    $save = &$p;
1982
                    $ret[] = &$save;
1983
                }
1984
            }
1985
        }
1986
        if (isset($group['subpackage'])) {
1987
            if (!isset($group['subpackage'][0])) {
1988
                $group['subpackage'] = array($group['subpackage']);
1989
            }
1990
            foreach ($group['subpackage'] as $package) {
1991
                $depchannel = isset($package['channel']) ? $package['channel'] : '__uri';
1992
                $p = &$this->getPackage($package['name'], $depchannel);
1993
                if ($p) {
1994
                    $save = &$p;
1995
                    $ret[] = &$save;
1996
                }
1997
            }
1998
        }
1999
        if (!count($ret)) {
2000
            return false;
2001
        }
2002
        return $ret;
2003
    }
2004
 
2005
    // {{{ getChannelValidator()
2006
    /**
2007
     * @param string channel name
2008
     * @return PEAR_Validate|false
2009
     */
2010
    function &getChannelValidator($channel)
2011
    {
2012
        $chan = $this->getChannel($channel);
2013
        if (PEAR::isError($chan)) {
2014
            return $chan;
2015
        }
2016
        $val = $chan->getValidationObject();
2017
        return $val;
2018
    }
2019
    // }}}
2020
    // {{{ getChannels()
2021
    /**
2022
     * @param string channel name
2023
     * @return array an array of PEAR_ChannelFile objects representing every installed channel
2024
     */
2025
    function &getChannels()
2026
    {
2027
        $ret = array();
2028
        if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
2029
            return $e;
2030
        }
2031
        foreach ($this->_listChannels() as $channel) {
2032
            $e = &$this->_getChannel($channel);
2033
            if (!$e || PEAR::isError($e)) {
2034
                continue;
2035
            }
2036
            $ret[] = $e;
2037
        }
2038
        $this->_unlock();
2039
        return $ret;
2040
    }
2041
 
2042
    // }}}
2043
    // {{{ checkFileMap()
2044
 
2045
    /**
2046
     * Test whether a file or set of files belongs to a package.
2047
     *
2048
     * If an array is passed in
2049
     * @param string|array file path, absolute or relative to the pear
2050
     *                     install dir
2051
     * @param string|array name of PEAR package or array('package' => name, 'channel' =>
2052
     *                     channel) of a package that will be ignored
2053
     * @param string API version - 1.1 will exclude any files belonging to a package
2054
     * @param array private recursion variable
2055
     * @return array|false which package and channel the file belongs to, or an empty
2056
     *                     string if the file does not belong to an installed package,
2057
     *                     or belongs to the second parameter's package
2058
     */
2059
    function checkFileMap($path, $package = false, $api = '1.0', $attrs = false)
2060
    {
2061
        if (is_array($path)) {
2062
            static $notempty;
2063
            if (empty($notempty)) {
2064
                if (!class_exists('PEAR_Installer_Role')) {
2065
                    require_once 'PEAR/Installer/Role.php';
2066
                }
2067
                $notempty = create_function('$a','return !empty($a);');
2068
            }
2069
            $package = is_array($package) ? array(strtolower($package[0]), strtolower($package[1]))
2070
                : strtolower($package);
2071
            $pkgs = array();
2072
            foreach ($path as $name => $attrs) {
2073
                if (is_array($attrs)) {
2074
                    if (isset($attrs['install-as'])) {
2075
                        $name = $attrs['install-as'];
2076
                    }
2077
                    if (!in_array($attrs['role'], PEAR_Installer_Role::getInstallableRoles())) {
2078
                        // these are not installed
2079
                        continue;
2080
                    }
2081
                    if (!in_array($attrs['role'], PEAR_Installer_Role::getBaseinstallRoles())) {
2082
                        $attrs['baseinstalldir'] = is_array($package) ? $package[1] : $package;
2083
                    }
2084
                    if (isset($attrs['baseinstalldir'])) {
2085
                        $name = $attrs['baseinstalldir'] . DIRECTORY_SEPARATOR . $name;
2086
                    }
2087
                }
2088
                $pkgs[$name] = $this->checkFileMap($name, $package, $api, $attrs);
2089
                if (PEAR::isError($pkgs[$name])) {
2090
                    return $pkgs[$name];
2091
                }
2092
            }
2093
            return array_filter($pkgs, $notempty);
2094
        }
2095
        if (empty($this->filemap_cache)) {
2096
            if (PEAR::isError($e = $this->_lock(LOCK_SH))) {
2097
                return $e;
2098
            }
2099
            $err = $this->_readFileMap();
2100
            $this->_unlock();
2101
            if (PEAR::isError($err)) {
2102
                return $err;
2103
            }
2104
        }
2105
        if (!$attrs) {
2106
            $attrs = array('role' => 'php'); // any old call would be for PHP role only
2107
        }
2108
        if (isset($this->filemap_cache[$attrs['role']][$path])) {
2109
            if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
2110
                return false;
2111
            }
2112
            return $this->filemap_cache[$attrs['role']][$path];
2113
        }
2114
        $l = strlen($this->install_dir);
2115
        if (substr($path, 0, $l) == $this->install_dir) {
2116
            $path = preg_replace('!^'.DIRECTORY_SEPARATOR.'+!', '', substr($path, $l));
2117
        }
2118
        if (isset($this->filemap_cache[$attrs['role']][$path])) {
2119
            if ($api >= '1.1' && $this->filemap_cache[$attrs['role']][$path] == $package) {
2120
                return false;
2121
            }
2122
            return $this->filemap_cache[$attrs['role']][$path];
2123
        }
2124
        return false;
2125
    }
2126
 
2127
    // }}}
2128
    // {{{ flush()
2129
    /**
2130
     * Force a reload of the filemap
2131
     * @since 1.5.0RC3
2132
     */
2133
    function flushFileMap()
2134
    {
2135
        $this->filemap_cache = null;
2136
        clearstatcache(); // ensure that the next read gets the full, current filemap
2137
    }
2138
 
2139
    // }}}
2140
    // {{{ apiVersion()
2141
    /**
2142
     * Get the expected API version.  Channels API is version 1.1, as it is backwards
2143
     * compatible with 1.0
2144
     * @return string
2145
     */
2146
    function apiVersion()
2147
    {
2148
        return '1.1';
2149
    }
2150
    // }}}
2151
 
2152
 
2153
    /**
2154
     * Parse a package name, or validate a parsed package name array
2155
     * @param string|array pass in an array of format
2156
     *                     array(
2157
     *                      'package' => 'pname',
2158
     *                     ['channel' => 'channame',]
2159
     *                     ['version' => 'version',]
2160
     *                     ['state' => 'state',]
2161
     *                     ['group' => 'groupname'])
2162
     *                     or a string of format
2163
     *                     [channel://][channame/]pname[-version|-state][/group=groupname]
2164
     * @return array|PEAR_Error
2165
     */
2166
    function parsePackageName($param, $defaultchannel = 'pear.php.net')
2167
    {
2168
        $saveparam = $param;
2169
        if (is_array($param)) {
2170
            // convert to string for error messages
2171
            $saveparam = $this->parsedPackageNameToString($param);
2172
            // process the array
2173
            if (!isset($param['package'])) {
2174
                return PEAR::raiseError('parsePackageName(): array $param ' .
2175
                    'must contain a valid package name in index "param"',
2176
                    'package', null, null, $param);
2177
            }
2178
            if (!isset($param['uri'])) {
2179
                if (!isset($param['channel'])) {
2180
                    $param['channel'] = $defaultchannel;
2181
                }
2182
            } else {
2183
                $param['channel'] = '__uri';
2184
            }
2185
        } else {
2186
            $components = @parse_url((string) $param);
2187
            if (isset($components['scheme'])) {
2188
                if ($components['scheme'] == 'http') {
2189
                    // uri package
2190
                    $param = array('uri' => $param, 'channel' => '__uri');
2191
                } elseif($components['scheme'] != 'channel') {
2192
                    return PEAR::raiseError('parsePackageName(): only channel:// uris may ' .
2193
                        'be downloaded, not "' . $param . '"', 'invalid', null, null, $param);
2194
                }
2195
            }
2196
            if (!isset($components['path'])) {
2197
                return PEAR::raiseError('parsePackageName(): array $param ' .
2198
                    'must contain a valid package name in "' . $param . '"',
2199
                    'package', null, null, $param);
2200
            }
2201
            if (isset($components['host'])) {
2202
                // remove the leading "/"
2203
                $components['path'] = substr($components['path'], 1);
2204
            }
2205
            if (!isset($components['scheme'])) {
2206
                if (strpos($components['path'], '/') !== false) {
2207
                    if ($components['path']{0} == '/') {
2208
                        return PEAR::raiseError('parsePackageName(): this is not ' .
2209
                            'a package name, it begins with "/" in "' . $param . '"',
2210
                            'invalid', null, null, $param);
2211
                    }
2212
                    $parts = explode('/', $components['path']);
2213
                    $components['host'] = array_shift($parts);
2214
                    if (count($parts) > 1) {
2215
                        $components['path'] = array_pop($parts);
2216
                        $components['host'] .= '/' . implode('/', $parts);
2217
                    } else {
2218
                        $components['path'] = implode('/', $parts);
2219
                    }
2220
                } else {
2221
                    $components['host'] = $defaultchannel;
2222
                }
2223
            } else {
2224
                if (strpos($components['path'], '/')) {
2225
                    $parts = explode('/', $components['path']);
2226
                    $components['path'] = array_pop($parts);
2227
                    $components['host'] .= '/' . implode('/', $parts);
2228
                }
2229
            }
2230
 
2231
            if (is_array($param)) {
2232
                $param['package'] = $components['path'];
2233
            } else {
2234
                $param = array(
2235
                    'package' => $components['path']
2236
                    );
2237
                if (isset($components['host'])) {
2238
                    $param['channel'] = $components['host'];
2239
                }
2240
            }
2241
            if (isset($components['fragment'])) {
2242
                $param['group'] = $components['fragment'];
2243
            }
2244
            if (isset($components['user'])) {
2245
                $param['user'] = $components['user'];
2246
            }
2247
            if (isset($components['pass'])) {
2248
                $param['pass'] = $components['pass'];
2249
            }
2250
            if (isset($components['query'])) {
2251
                parse_str($components['query'], $param['opts']);
2252
            }
2253
            // check for extension
2254
            $pathinfo = pathinfo($param['package']);
2255
            if (isset($pathinfo['extension']) &&
2256
                  in_array(strtolower($pathinfo['extension']), array('tgz', 'tar'))) {
2257
                $param['extension'] = $pathinfo['extension'];
2258
                $param['package'] = substr($pathinfo['basename'], 0,
2259
                    strlen($pathinfo['basename']) - 4);
2260
            }
2261
            // check for version
2262
            if (strpos($param['package'], '-')) {
2263
                $test = explode('-', $param['package']);
2264
                if (count($test) != 2) {
2265
                    return PEAR::raiseError('parsePackageName(): only one version/state ' .
2266
                        'delimiter "-" is allowed in "' . $saveparam . '"',
2267
                        'version', null, null, $param);
2268
                }
2269
                list($param['package'], $param['version']) = $test;
2270
            }
2271
        }
2272
        // validation
2273
        $info = $this->channelExists($param['channel']);
2274
        if (PEAR::isError($info)) {
2275
            return $info;
2276
        }
2277
        if (!$info) {
2278
            return PEAR::raiseError('unknown channel "' . $param['channel'] .
2279
                '" in "' . $saveparam . '"', 'channel', null, null, $param);
2280
        }
2281
        $chan = $this->getChannel($param['channel']);
2282
        if (PEAR::isError($chan)) {
2283
            return $chan;
2284
        }
2285
        if (!$chan) {
2286
            return PEAR::raiseError("Exception: corrupt registry, could not " .
2287
                "retrieve channel " . $param['channel'] . " information",
2288
                'registry', null, null, $param);
2289
        }
2290
        $param['channel'] = $chan->getName();
2291
        $validate = $chan->getValidationObject();
2292
        $vpackage = $chan->getValidationPackage();
2293
        // validate package name
2294
        if (!$validate->validPackageName($param['package'], $vpackage['_content'])) {
2295
            return PEAR::raiseError('parsePackageName(): invalid package name "' .
2296
                $param['package'] . '" in "' . $saveparam . '"',
2297
                'package', null, null, $param);
2298
        }
2299
        if (isset($param['group'])) {
2300
            if (!PEAR_Validate::validGroupName($param['group'])) {
2301
                return PEAR::raiseError('parsePackageName(): dependency group "' . $param['group'] .
2302
                    '" is not a valid group name in "' . $saveparam . '"', 'group', null, null,
2303
                    $param);
2304
            }
2305
        }
2306
        if (isset($param['state'])) {
2307
            if (!in_array(strtolower($param['state']), $validate->getValidStates())) {
2308
                return PEAR::raiseError('parsePackageName(): state "' . $param['state']
2309
                    . '" is not a valid state in "' . $saveparam . '"',
2310
                    'state', null, null, $param);
2311
            }
2312
        }
2313
        if (isset($param['version'])) {
2314
            if (isset($param['state'])) {
2315
                return PEAR::raiseError('parsePackageName(): cannot contain both ' .
2316
                    'a version and a stability (state) in "' . $saveparam . '"',
2317
                    'version/state', null, null, $param);
2318
            }
2319
            // check whether version is actually a state
2320
            if (in_array(strtolower($param['version']), $validate->getValidStates())) {
2321
                $param['state'] = strtolower($param['version']);
2322
                unset($param['version']);
2323
            } else {
2324
                if (!$validate->validVersion($param['version'])) {
2325
                    return PEAR::raiseError('parsePackageName(): "' . $param['version'] .
2326
                        '" is neither a valid version nor a valid state in "' .
2327
                        $saveparam . '"', 'version/state', null, null, $param);
187 mathias 2328
                }
94 jpm 2329
            }
2330
        }
2331
        return $param;
2332
    }
2333
 
2334
    /**
2335
     * @param array
2336
     * @return string
2337
     */
2338
    function parsedPackageNameToString($parsed, $brief = false)
2339
    {
2340
        if (is_string($parsed)) {
2341
            return $parsed;
2342
        }
2343
        if (is_object($parsed)) {
2344
            $p = $parsed;
2345
            $parsed = array(
2346
                'package' => $p->getPackage(),
2347
                'channel' => $p->getChannel(),
2348
                'version' => $p->getVersion(),
2349
            );
2350
        }
2351
        if (isset($parsed['uri'])) {
2352
            return $parsed['uri'];
2353
        }
2354
        if ($brief) {
2355
            if ($channel = $this->channelAlias($parsed['channel'])) {
2356
                return $channel . '/' . $parsed['package'];
2357
            }
2358
        }
2359
        $upass = '';
2360
        if (isset($parsed['user'])) {
2361
            $upass = $parsed['user'];
2362
            if (isset($parsed['pass'])) {
2363
                $upass .= ':' . $parsed['pass'];
2364
            }
2365
            $upass = "$upass@";
2366
        }
2367
        $ret = 'channel://' . $upass . $parsed['channel'] . '/' . $parsed['package'];
2368
        if (isset($parsed['version']) || isset($parsed['state'])) {
2369
            $ver = isset($parsed['version']) ? $parsed['version'] : '';
2370
            $ver .= isset($parsed['state']) ? $parsed['state'] : '';
2371
            $ret .= '-' . $ver;
2372
        }
2373
        if (isset($parsed['extension'])) {
2374
            $ret .= '.' . $parsed['extension'];
2375
        }
2376
        if (isset($parsed['opts'])) {
2377
            $ret .= '?';
2378
            foreach ($parsed['opts'] as $name => $value) {
2379
                $parsed['opts'][$name] = "$name=$value";
2380
            }
2381
            $ret .= implode('&', $parsed['opts']);
2382
        }
2383
        if (isset($parsed['group'])) {
2384
            $ret .= '#' . $parsed['group'];
2385
        }
2386
        return $ret;
2387
    }
2388
}