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_Installer
4
 *
5
 * PHP versions 4 and 5
6
 *
7
 * LICENSE: This source file is subject to version 3.0 of the PHP license
8
 * that is available through the world-wide-web at the following URI:
9
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
10
 * the PHP License and are unable to obtain it through the web, please
11
 * send a note to license@php.net so we can mail you a copy immediately.
12
 *
13
 * @category   pear
14
 * @package    PEAR
15
 * @author     Stig Bakken <ssb@php.net>
16
 * @author     Tomas V.V. Cox <cox@idecnet.com>
17
 * @author     Martin Jansen <mj@php.net>
18
 * @author     Greg Beaver <cellog@php.net>
19
 * @copyright  1997-2006 The PHP Group
20
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
21
 * @version    CVS: $Id: Installer.php,v 1.243 2007/02/16 04:00:37 cellog Exp $
22
 * @link       http://pear.php.net/package/PEAR
23
 * @since      File available since Release 0.1
24
 */
25
 
26
/**
27
 * Used for installation groups in package.xml 2.0 and platform exceptions
28
 */
29
require_once 'OS/Guess.php';
30
require_once 'PEAR/Downloader.php';
31
 
32
define('PEAR_INSTALLER_NOBINARY', -240);
33
/**
34
 * Administration class used to install PEAR packages and maintain the
35
 * installed package database.
36
 *
37
 * @category   pear
38
 * @package    PEAR
39
 * @author     Stig Bakken <ssb@php.net>
40
 * @author     Tomas V.V. Cox <cox@idecnet.com>
41
 * @author     Martin Jansen <mj@php.net>
42
 * @author     Greg Beaver <cellog@php.net>
43
 * @copyright  1997-2006 The PHP Group
44
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
45
 * @version    Release: 1.5.1
46
 * @link       http://pear.php.net/package/PEAR
47
 * @since      Class available since Release 0.1
48
 */
49
class PEAR_Installer extends PEAR_Downloader
50
{
51
    // {{{ properties
52
 
53
    /** name of the package directory, for example Foo-1.0
54
     * @var string
55
     */
56
    var $pkgdir;
57
 
58
    /** directory where PHP code files go
59
     * @var string
60
     */
61
    var $phpdir;
62
 
63
    /** directory where PHP extension files go
64
     * @var string
65
     */
66
    var $extdir;
67
 
68
    /** directory where documentation goes
69
     * @var string
70
     */
71
    var $docdir;
72
 
73
    /** installation root directory (ala PHP's INSTALL_ROOT or
74
     * automake's DESTDIR
75
     * @var string
76
     */
77
    var $installroot = '';
78
 
79
    /** debug level
80
     * @var int
81
     */
82
    var $debug = 1;
83
 
84
    /** temporary directory
85
     * @var string
86
     */
87
    var $tmpdir;
88
 
89
    /**
90
     * PEAR_Registry object used by the installer
91
     * @var PEAR_Registry
92
     */
93
    var $registry;
94
 
95
    /**
96
     * array of PEAR_Downloader_Packages
97
     * @var array
98
     */
99
    var $_downloadedPackages;
100
 
101
    /** List of file transactions queued for an install/upgrade/uninstall.
102
     *
103
     *  Format:
104
     *    array(
105
     *      0 => array("rename => array("from-file", "to-file")),
106
     *      1 => array("delete" => array("file-to-delete")),
107
     *      ...
108
     *    )
109
     *
110
     * @var array
111
     */
112
    var $file_operations = array();
113
 
114
    // }}}
115
 
116
    // {{{ constructor
117
 
118
    /**
119
     * PEAR_Installer constructor.
120
     *
121
     * @param object $ui user interface object (instance of PEAR_Frontend_*)
122
     *
123
     * @access public
124
     */
125
    function PEAR_Installer(&$ui)
126
    {
127
        parent::PEAR_Common();
128
        $this->setFrontendObject($ui);
129
        $this->debug = $this->config->get('verbose');
130
    }
131
 
132
    function setOptions($options)
133
    {
134
        $this->_options = $options;
135
    }
136
 
137
    function setConfig(&$config)
138
    {
139
        $this->config = &$config;
140
        $this->_registry = &$config->getRegistry();
141
    }
142
 
143
    // }}}
144
 
145
    function _removeBackups($files)
146
    {
147
        foreach ($files as $path) {
148
            $this->addFileOperation('removebackup', array($path));
149
        }
150
    }
151
 
152
    // {{{ _deletePackageFiles()
153
 
154
    /**
155
     * Delete a package's installed files, does not remove empty directories.
156
     *
157
     * @param string package name
158
     * @param string channel name
159
     * @param bool if true, then files are backed up first
160
     * @return bool TRUE on success, or a PEAR error on failure
161
     * @access protected
162
     */
163
    function _deletePackageFiles($package, $channel = false, $backup = false)
164
    {
165
        if (!$channel) {
166
            $channel = 'pear.php.net';
167
        }
168
        if (!strlen($package)) {
169
            return $this->raiseError("No package to uninstall given");
170
        }
171
        if (strtolower($package) == 'pear' && $channel == 'pear.php.net') {
172
            // to avoid race conditions, include all possible needed files
173
            require_once 'PEAR/Task/Common.php';
174
            require_once 'PEAR/Task/Replace.php';
175
            require_once 'PEAR/Task/Unixeol.php';
176
            require_once 'PEAR/Task/Windowseol.php';
177
            require_once 'PEAR/PackageFile/v1.php';
178
            require_once 'PEAR/PackageFile/v2.php';
179
            require_once 'PEAR/PackageFile/Generator/v1.php';
180
            require_once 'PEAR/PackageFile/Generator/v2.php';
181
        }
182
        $filelist = $this->_registry->packageInfo($package, 'filelist', $channel);
183
        if ($filelist == null) {
184
            return $this->raiseError("$channel/$package not installed");
185
        }
186
        $ret = array();
187
        foreach ($filelist as $file => $props) {
188
            if (empty($props['installed_as'])) {
189
                continue;
190
            }
191
            $path = $props['installed_as'];
192
            if ($backup) {
193
                $this->addFileOperation('backup', array($path));
194
                $ret[] = $path;
195
            }
196
            $this->addFileOperation('delete', array($path));
197
        }
198
        if ($backup) {
199
            return $ret;
200
        }
201
        return true;
202
    }
203
 
204
    // }}}
205
    // {{{ _installFile()
206
 
207
    /**
208
     * @param string filename
209
     * @param array attributes from <file> tag in package.xml
210
     * @param string path to install the file in
211
     * @param array options from command-line
212
     * @access private
213
     */
214
    function _installFile($file, $atts, $tmp_path, $options)
215
    {
216
        // {{{ return if this file is meant for another platform
217
        static $os;
218
        if (!isset($this->_registry)) {
219
            $this->_registry = &$this->config->getRegistry();
220
        }
221
        if (isset($atts['platform'])) {
222
            if (empty($os)) {
223
                $os = new OS_Guess();
224
            }
225
            if (strlen($atts['platform']) && $atts['platform']{0} == '!') {
226
                $negate = true;
227
                $platform = substr($atts['platform'], 1);
228
            } else {
229
                $negate = false;
230
                $platform = $atts['platform'];
231
            }
232
            if ((bool) $os->matchSignature($platform) === $negate) {
233
                $this->log(3, "skipped $file (meant for $atts[platform], we are ".$os->getSignature().")");
234
                return PEAR_INSTALLER_SKIPPED;
235
            }
236
        }
237
        // }}}
238
 
239
        $channel = $this->pkginfo->getChannel();
240
        // {{{ assemble the destination paths
241
        switch ($atts['role']) {
242
            case 'doc':
243
            case 'data':
244
            case 'test':
245
                $dest_dir = $this->config->get($atts['role'] . '_dir', null, $channel) .
246
                            DIRECTORY_SEPARATOR . $this->pkginfo->getPackage();
247
                unset($atts['baseinstalldir']);
248
                break;
249
            case 'ext':
250
            case 'php':
251
                $dest_dir = $this->config->get($atts['role'] . '_dir', null, $channel);
252
                break;
253
            case 'script':
254
                $dest_dir = $this->config->get('bin_dir', null, $channel);
255
                break;
256
            case 'src':
257
            case 'extsrc':
258
                $this->source_files++;
259
                return;
260
            default:
261
                return $this->raiseError("Invalid role `$atts[role]' for file $file");
262
        }
263
        $save_destdir = $dest_dir;
264
        if (!empty($atts['baseinstalldir'])) {
265
            $dest_dir .= DIRECTORY_SEPARATOR . $atts['baseinstalldir'];
266
        }
267
        if (dirname($file) != '.' && empty($atts['install-as'])) {
268
            $dest_dir .= DIRECTORY_SEPARATOR . dirname($file);
269
        }
270
        if (empty($atts['install-as'])) {
271
            $dest_file = $dest_dir . DIRECTORY_SEPARATOR . basename($file);
272
        } else {
273
            $dest_file = $dest_dir . DIRECTORY_SEPARATOR . $atts['install-as'];
274
        }
275
        $orig_file = $tmp_path . DIRECTORY_SEPARATOR . $file;
276
 
277
        // Clean up the DIRECTORY_SEPARATOR mess
278
        $ds2 = DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR;
279
        list($dest_file, $orig_file) = preg_replace(array('!\\\\+!', '!/!', "!$ds2+!"),
280
                                                    array(DIRECTORY_SEPARATOR,
281
                                                          DIRECTORY_SEPARATOR,
282
                                                          DIRECTORY_SEPARATOR),
283
                                                    array($dest_file, $orig_file));
284
        $final_dest_file = $installed_as = $dest_file;
285
        if (isset($this->_options['packagingroot'])) {
286
            $installedas_dest_dir = dirname($final_dest_file);
287
            $installedas_dest_file = $dest_dir . DIRECTORY_SEPARATOR . '.tmp' . basename($final_dest_file);
288
            $final_dest_file = $this->_prependPath($final_dest_file,
289
                $this->_options['packagingroot']);
290
        } else {
291
            $installedas_dest_dir = dirname($final_dest_file);
292
            $installedas_dest_file = $installedas_dest_dir . DIRECTORY_SEPARATOR . '.tmp' . basename($final_dest_file);
293
        }
294
        $dest_dir = dirname($final_dest_file);
295
        $dest_file = $dest_dir . DIRECTORY_SEPARATOR . '.tmp' . basename($final_dest_file);
296
        // }}}
297
 
298
        if (empty($this->_options['register-only']) &&
299
              (!file_exists($dest_dir) || !is_dir($dest_dir))) {
300
            if (!$this->mkDirHier($dest_dir)) {
301
                return $this->raiseError("failed to mkdir $dest_dir",
302
                                         PEAR_INSTALLER_FAILED);
303
            }
304
            $this->log(3, "+ mkdir $dest_dir");
305
        }
306
        // pretty much nothing happens if we are only registering the install
307
        if (empty($this->_options['register-only'])) {
308
            if (empty($atts['replacements'])) {
309
                if (!file_exists($orig_file)) {
310
                    return $this->raiseError("file $orig_file does not exist",
311
                                             PEAR_INSTALLER_FAILED);
312
                }
313
                if (!@copy($orig_file, $dest_file)) {
314
                    return $this->raiseError("failed to write $dest_file: $php_errormsg",
315
                                             PEAR_INSTALLER_FAILED);
316
                }
317
                $this->log(3, "+ cp $orig_file $dest_file");
318
                if (isset($atts['md5sum'])) {
319
                    $md5sum = md5_file($dest_file);
320
                }
321
            } else {
322
                // {{{ file with replacements
323
                if (!file_exists($orig_file)) {
324
                    return $this->raiseError("file does not exist",
325
                                             PEAR_INSTALLER_FAILED);
326
                }
327
                $contents = file_get_contents($orig_file);
328
                if ($contents === false) {
329
                    $contents = '';
330
                }
331
                if (isset($atts['md5sum'])) {
332
                    $md5sum = md5($contents);
333
                }
334
                $subst_from = $subst_to = array();
335
                foreach ($atts['replacements'] as $a) {
336
                    $to = '';
337
                    if ($a['type'] == 'php-const') {
338
                        if (preg_match('/^[a-z0-9_]+$/i', $a['to'])) {
339
                            eval("\$to = $a[to];");
340
                        } else {
341
                            if (!isset($options['soft'])) {
342
                                $this->log(0, "invalid php-const replacement: $a[to]");
343
                            }
344
                            continue;
345
                        }
346
                    } elseif ($a['type'] == 'pear-config') {
347
                        if ($a['to'] == 'master_server') {
348
                            $chan = $this->_registry->getChannel($channel);
349
                            if (!PEAR::isError($chan)) {
350
                                $to = $chan->getServer();
351
                            } else {
352
                                $to = $this->config->get($a['to'], null, $channel);
353
                            }
354
                        } else {
355
                            $to = $this->config->get($a['to'], null, $channel);
356
                        }
357
                        if (is_null($to)) {
358
                            if (!isset($options['soft'])) {
359
                                $this->log(0, "invalid pear-config replacement: $a[to]");
360
                            }
361
                            continue;
362
                        }
363
                    } elseif ($a['type'] == 'package-info') {
364
                        if ($t = $this->pkginfo->packageInfo($a['to'])) {
365
                            $to = $t;
366
                        } else {
367
                            if (!isset($options['soft'])) {
368
                                $this->log(0, "invalid package-info replacement: $a[to]");
369
                            }
370
                            continue;
371
                        }
372
                    }
373
                    if (!is_null($to)) {
374
                        $subst_from[] = $a['from'];
375
                        $subst_to[] = $to;
376
                    }
377
                }
378
                $this->log(3, "doing ".sizeof($subst_from)." substitution(s) for $final_dest_file");
379
                if (sizeof($subst_from)) {
380
                    $contents = str_replace($subst_from, $subst_to, $contents);
381
                }
382
                $wp = @fopen($dest_file, "wb");
383
                if (!is_resource($wp)) {
384
                    return $this->raiseError("failed to create $dest_file: $php_errormsg",
385
                                             PEAR_INSTALLER_FAILED);
386
                }
387
                if (@fwrite($wp, $contents) === false) {
388
                    return $this->raiseError("failed writing to $dest_file: $php_errormsg",
389
                                             PEAR_INSTALLER_FAILED);
390
                }
391
                fclose($wp);
392
                // }}}
393
            }
394
            // {{{ check the md5
395
            if (isset($md5sum)) {
396
                if (strtolower($md5sum) == strtolower($atts['md5sum'])) {
397
                    $this->log(2, "md5sum ok: $final_dest_file");
398
                } else {
399
                    if (empty($options['force'])) {
400
                        // delete the file
401
                        if (file_exists($dest_file)) {
402
                            unlink($dest_file);
403
                        }
404
                        if (!isset($options['ignore-errors'])) {
405
                            return $this->raiseError("bad md5sum for file $final_dest_file",
406
                                                 PEAR_INSTALLER_FAILED);
407
                        } else {
408
                            if (!isset($options['soft'])) {
409
                                $this->log(0, "warning : bad md5sum for file $final_dest_file");
410
                            }
411
                        }
412
                    } else {
413
                        if (!isset($options['soft'])) {
414
                            $this->log(0, "warning : bad md5sum for file $final_dest_file");
415
                        }
416
                    }
417
                }
418
            }
419
            // }}}
420
            // {{{ set file permissions
421
            if (!OS_WINDOWS) {
422
                if ($atts['role'] == 'script') {
423
                    $mode = 0777 & ~(int)octdec($this->config->get('umask'));
424
                    $this->log(3, "+ chmod +x $dest_file");
425
                } else {
426
                    $mode = 0666 & ~(int)octdec($this->config->get('umask'));
427
                }
428
                $this->addFileOperation("chmod", array($mode, $dest_file));
429
                if (!@chmod($dest_file, $mode)) {
430
                    if (!isset($options['soft'])) {
431
                        $this->log(0, "failed to change mode of $dest_file: $php_errormsg");
432
                    }
433
                }
434
            }
435
            // }}}
436
            $this->addFileOperation("rename", array($dest_file, $final_dest_file,
437
                $atts['role'] == 'ext'));
438
        }
439
        // Store the full path where the file was installed for easy unistall
440
        $this->addFileOperation("installed_as", array($file, $installed_as,
441
                                $save_destdir, dirname(substr($installedas_dest_file, strlen($save_destdir)))));
442
 
443
        //$this->log(2, "installed: $dest_file");
444
        return PEAR_INSTALLER_OK;
445
    }
446
 
447
    // }}}
448
    // {{{ _installFile2()
449
 
450
    /**
451
     * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
452
     * @param string filename
453
     * @param array attributes from <file> tag in package.xml
454
     * @param string path to install the file in
455
     * @param array options from command-line
456
     * @access private
457
     */
458
    function _installFile2(&$pkg, $file, $atts, $tmp_path, $options)
459
    {
460
        if (!isset($this->_registry)) {
461
            $this->_registry = &$this->config->getRegistry();
462
        }
463
 
464
        $channel = $pkg->getChannel();
465
        // {{{ assemble the destination paths
466
        if (!in_array($atts['attribs']['role'],
467
              PEAR_Installer_Role::getValidRoles($pkg->getPackageType()))) {
468
            return $this->raiseError('Invalid role `' . $atts['attribs']['role'] .
469
                    "' for file $file");
470
        }
471
        $role = &PEAR_Installer_Role::factory($pkg, $atts['attribs']['role'], $this->config);
472
        $err = $role->setup($this, $pkg, $atts['attribs'], $file);
473
        if (PEAR::isError($err)) {
474
            return $err;
475
        }
476
        if (!$role->isInstallable()) {
477
            return;
478
        }
479
        $info = $role->processInstallation($pkg, $atts['attribs'], $file, $tmp_path);
480
        if (PEAR::isError($info)) {
481
            return $info;
482
        } else {
483
            list($save_destdir, $dest_dir, $dest_file, $orig_file) = $info;
484
        }
485
        $final_dest_file = $installed_as = $dest_file;
486
        if (isset($this->_options['packagingroot'])) {
487
            $final_dest_file = $this->_prependPath($final_dest_file,
488
                $this->_options['packagingroot']);
489
        }
490
        $dest_dir = dirname($final_dest_file);
491
        $dest_file = $dest_dir . DIRECTORY_SEPARATOR . '.tmp' . basename($final_dest_file);
492
        // }}}
493
 
494
        if (empty($this->_options['register-only'])) {
495
            if (!file_exists($dest_dir) || !is_dir($dest_dir)) {
496
                if (!$this->mkDirHier($dest_dir)) {
497
                    return $this->raiseError("failed to mkdir $dest_dir",
498
                                             PEAR_INSTALLER_FAILED);
499
                }
500
                $this->log(3, "+ mkdir $dest_dir");
501
            }
502
        }
503
        $attribs = $atts['attribs'];
504
        unset($atts['attribs']);
505
        // pretty much nothing happens if we are only registering the install
506
        if (empty($this->_options['register-only'])) {
507
            if (!count($atts)) { // no tasks
508
                if (!file_exists($orig_file)) {
509
                    return $this->raiseError("file $orig_file does not exist",
510
                                             PEAR_INSTALLER_FAILED);
511
                }
512
                if (!@copy($orig_file, $dest_file)) {
513
                    return $this->raiseError("failed to write $dest_file: $php_errormsg",
514
                                             PEAR_INSTALLER_FAILED);
515
                }
516
                $this->log(3, "+ cp $orig_file $dest_file");
517
                if (isset($attribs['md5sum'])) {
518
                    $md5sum = md5_file($dest_file);
519
                }
520
            } else { // file with tasks
521
                if (!file_exists($orig_file)) {
522
                    return $this->raiseError("file $orig_file does not exist",
523
                                             PEAR_INSTALLER_FAILED);
524
                }
525
                $contents = file_get_contents($orig_file);
526
                if ($contents === false) {
527
                    $contents = '';
528
                }
529
                if (isset($attribs['md5sum'])) {
530
                    $md5sum = md5($contents);
531
                }
532
                foreach ($atts as $tag => $raw) {
533
                    $tag = str_replace(array($pkg->getTasksNs() . ':', '-'),
534
                        array('', '_'), $tag);
535
                    $task = "PEAR_Task_$tag";
536
                    $task = &new $task($this->config, $this, PEAR_TASK_INSTALL);
537
                    if (!$task->isScript()) { // scripts are only handled after installation
538
                        $task->init($raw, $attribs, $pkg->getLastInstalledVersion());
539
                        $res = $task->startSession($pkg, $contents, $final_dest_file);
540
                        if ($res === false) {
541
                            continue; // skip this file
542
                        }
543
                        if (PEAR::isError($res)) {
544
                            return $res;
545
                        }
546
                        $contents = $res; // save changes
547
                    }
548
                    $wp = @fopen($dest_file, "wb");
549
                    if (!is_resource($wp)) {
550
                        return $this->raiseError("failed to create $dest_file: $php_errormsg",
551
                                                 PEAR_INSTALLER_FAILED);
552
                    }
553
                    if (fwrite($wp, $contents) === false) {
554
                        return $this->raiseError("failed writing to $dest_file: $php_errormsg",
555
                                                 PEAR_INSTALLER_FAILED);
556
                    }
557
                    fclose($wp);
558
                }
559
            }
560
            // {{{ check the md5
561
            if (isset($md5sum)) {
562
                if (strtolower($md5sum) == strtolower($attribs['md5sum'])) {
563
                    $this->log(2, "md5sum ok: $final_dest_file");
564
                } else {
565
                    if (empty($options['force'])) {
566
                        // delete the file
567
                        if (file_exists($dest_file)) {
568
                            unlink($dest_file);
569
                        }
570
                        if (!isset($options['ignore-errors'])) {
571
                            return $this->raiseError("bad md5sum for file $final_dest_file",
572
                                                     PEAR_INSTALLER_FAILED);
573
                        } else {
574
                            if (!isset($options['soft'])) {
575
                                $this->log(0, "warning : bad md5sum for file $final_dest_file");
576
                            }
577
                        }
578
                    } else {
579
                        if (!isset($options['soft'])) {
580
                            $this->log(0, "warning : bad md5sum for file $final_dest_file");
581
                        }
582
                    }
583
                }
584
            }
585
            // }}}
586
            // {{{ set file permissions
587
            if (!OS_WINDOWS) {
588
                if ($role->isExecutable()) {
589
                    $mode = 0777 & ~(int)octdec($this->config->get('umask'));
590
                    $this->log(3, "+ chmod +x $dest_file");
591
                } else {
592
                    $mode = 0666 & ~(int)octdec($this->config->get('umask'));
593
                }
594
                $this->addFileOperation("chmod", array($mode, $dest_file));
595
                if (!@chmod($dest_file, $mode)) {
596
                    if (!isset($options['soft'])) {
597
                        $this->log(0, "failed to change mode of $dest_file: $php_errormsg");
598
                    }
599
                }
600
            }
601
            // }}}
602
            $this->addFileOperation("rename", array($dest_file, $final_dest_file, $role->isExtension()));
603
        }
604
        // Store the full path where the file was installed for easy uninstall
605
        $this->addFileOperation("installed_as", array($file, $installed_as,
606
                            $save_destdir, dirname(substr($dest_file, strlen($save_destdir)))));
607
 
608
        //$this->log(2, "installed: $dest_file");
609
        return PEAR_INSTALLER_OK;
610
    }
611
 
612
    // }}}
613
    // {{{ addFileOperation()
614
 
615
    /**
616
     * Add a file operation to the current file transaction.
617
     *
618
     * @see startFileTransaction()
619
     * @param string $type This can be one of:
620
     *    - rename:  rename a file ($data has 3 values)
621
     *    - backup:  backup an existing file ($data has 1 value)
622
     *    - removebackup:  clean up backups created during install ($data has 1 value)
623
     *    - chmod:   change permissions on a file ($data has 2 values)
624
     *    - delete:  delete a file ($data has 1 value)
625
     *    - rmdir:   delete a directory if empty ($data has 1 value)
626
     *    - installed_as: mark a file as installed ($data has 4 values).
627
     * @param array $data For all file operations, this array must contain the
628
     *    full path to the file or directory that is being operated on.  For
629
     *    the rename command, the first parameter must be the file to rename,
630
     *    the second its new name, the third whether this is a PHP extension.
631
     *
632
     *    The installed_as operation contains 4 elements in this order:
633
     *    1. Filename as listed in the filelist element from package.xml
634
     *    2. Full path to the installed file
635
     *    3. Full path from the php_dir configuration variable used in this
636
     *       installation
637
     *    4. Relative path from the php_dir that this file is installed in
638
     */
639
    function addFileOperation($type, $data)
640
    {
641
        if (!is_array($data)) {
642
            return $this->raiseError('Internal Error: $data in addFileOperation'
643
                . ' must be an array, was ' . gettype($data));
644
        }
645
        if ($type == 'chmod') {
646
            $octmode = decoct($data[0]);
647
            $this->log(3, "adding to transaction: $type $octmode $data[1]");
648
        } else {
649
            $this->log(3, "adding to transaction: $type " . implode(" ", $data));
650
        }
651
        $this->file_operations[] = array($type, $data);
652
    }
653
 
654
    // }}}
655
    // {{{ startFileTransaction()
656
 
657
    function startFileTransaction($rollback_in_case = false)
658
    {
659
        if (count($this->file_operations) && $rollback_in_case) {
660
            $this->rollbackFileTransaction();
661
        }
662
        $this->file_operations = array();
663
    }
664
 
665
    // }}}
666
    // {{{ commitFileTransaction()
667
 
668
    function commitFileTransaction()
669
    {
670
        $n = count($this->file_operations);
671
        $this->log(2, "about to commit $n file operations");
672
        // {{{ first, check permissions and such manually
673
        $errors = array();
674
        foreach ($this->file_operations as $tr) {
675
            list($type, $data) = $tr;
676
            switch ($type) {
677
                case 'rename':
678
                    if (!file_exists($data[0])) {
679
                        $errors[] = "cannot rename file $data[0], doesn't exist";
680
                    }
681
                    // check that dest dir. is writable
682
                    if (!is_writable(dirname($data[1]))) {
683
                        $errors[] = "permission denied ($type): $data[1]";
684
                    }
685
                    break;
686
                case 'chmod':
687
                    // check that file is writable
688
                    if (!is_writable($data[1])) {
689
                        $errors[] = "permission denied ($type): $data[1] " . decoct($data[0]);
690
                    }
691
                    break;
692
                case 'delete':
693
                    if (!file_exists($data[0])) {
694
                        $this->log(2, "warning: file $data[0] doesn't exist, can't be deleted");
695
                    }
696
                    // check that directory is writable
697
                    if (file_exists($data[0])) {
698
                        if (!is_writable(dirname($data[0]))) {
699
                            $errors[] = "permission denied ($type): $data[0]";
700
                        } else {
701
                            // make sure the file to be deleted can be opened for writing
702
                            $fp = false;
703
                            if (!is_dir($data[0]) &&
704
                                  (!is_writable($data[0]) || !($fp = @fopen($data[0], 'a')))) {
705
                                $errors[] = "permission denied ($type): $data[0]";
706
                            } elseif ($fp) {
707
                                fclose($fp);
708
                            }
709
                        }
710
                    }
711
                    break;
712
            }
713
 
714
        }
715
        // }}}
716
        $m = sizeof($errors);
717
        if ($m > 0) {
718
            foreach ($errors as $error) {
719
                if (!isset($this->_options['soft'])) {
720
                    $this->log(1, $error);
721
                }
722
            }
723
            if (!isset($this->_options['ignore-errors'])) {
724
                return false;
725
            }
726
        }
727
        $this->_dirtree = array();
728
        // {{{ really commit the transaction
729
        foreach ($this->file_operations as $i => $tr) {
730
            if (!$tr) {
731
                // support removal of non-existing backups
732
                continue;
733
            }
734
            list($type, $data) = $tr;
735
            switch ($type) {
736
                case 'backup':
737
                    if (!file_exists($data[0])) {
738
                        $this->file_operations[$i] = false;
739
                        break;
740
                    }
741
                    if (!@copy($data[0], $data[0] . '.bak')) {
742
                        $this->log(1, 'Could not copy ' . $data[0] . ' to ' . $data[0] .
743
                            '.bak ' . $php_errormsg);
744
                        return false;
745
                    }
746
                    $this->log(3, "+ backup $data[0] to $data[0].bak");
747
                    break;
748
                case 'removebackup':
749
                    if (file_exists($data[0] . '.bak') && is_writable($data[0] . '.bak')) {
750
                        unlink($data[0] . '.bak');
751
                        $this->log(3, "+ rm backup of $data[0] ($data[0].bak)");
752
                    }
753
                    break;
754
                case 'rename':
755
                    if (file_exists($data[1])) {
756
                        $test = @unlink($data[1]);
757
                    } else {
758
                        $test = null;
759
                    }
760
                    if (!$test && file_exists($data[1])) {
761
                        if ($data[2]) {
762
                            $extra = ', this extension must be installed manually.  Rename to "' .
763
                                basename($data[1]) . '"';
764
                        } else {
765
                            $extra = '';
766
                        }
767
                        if (!isset($this->_options['soft'])) {
768
                            $this->log(1, 'Could not delete ' . $data[1] . ', cannot rename ' .
769
                                $data[0] . $extra);
770
                        }
771
                        if (!isset($this->_options['ignore-errors'])) {
772
                            return false;
773
                        }
774
                    }
775
                    // permissions issues with rename - copy() is far superior
776
                    $perms = @fileperms($data[0]);
777
                    if (!@copy($data[0], $data[1])) {
778
                        $this->log(1, 'Could not rename ' . $data[0] . ' to ' . $data[1] .
779
                            ' ' . $php_errormsg);
780
                        return false;
781
                    }
782
                    // copy over permissions, otherwise they are lost
783
                    @chmod($data[1], $perms);
784
                    @unlink($data[0]);
785
                    $this->log(3, "+ mv $data[0] $data[1]");
786
                    break;
787
                case 'chmod':
788
                    if (!@chmod($data[1], $data[0])) {
789
                        $this->log(1, 'Could not chmod ' . $data[1] . ' to ' .
790
                            decoct($data[0]) . ' ' . $php_errormsg);
791
                        return false;
792
                    }
793
                    $octmode = decoct($data[0]);
794
                    $this->log(3, "+ chmod $octmode $data[1]");
795
                    break;
796
                case 'delete':
797
                    if (file_exists($data[0])) {
798
                        if (!@unlink($data[0])) {
799
                            $this->log(1, 'Could not delete ' . $data[0] . ' ' .
800
                                $php_errormsg);
801
                            return false;
802
                        }
803
                        $this->log(3, "+ rm $data[0]");
804
                    }
805
                    break;
806
                case 'rmdir':
807
                    if (file_exists($data[0])) {
808
                        do {
809
                            $testme = opendir($data[0]);
810
                            while (false !== ($entry = readdir($testme))) {
811
                                if ($entry == '.' || $entry == '..') {
812
                                    continue;
813
                                }
814
                                closedir($testme);
815
                                break 2; // this directory is not empty and can't be
816
                                         // deleted
817
                            }
818
                            closedir($testme);
819
                            if (!@rmdir($data[0])) {
820
                                $this->log(1, 'Could not rmdir ' . $data[0] . ' ' .
821
                                    $php_errormsg);
822
                                return false;
823
                            }
824
                            $this->log(3, "+ rmdir $data[0]");
825
                        } while (false);
826
                    }
827
                    break;
828
                case 'installed_as':
829
                    $this->pkginfo->setInstalledAs($data[0], $data[1]);
830
                    if (!isset($this->_dirtree[dirname($data[1])])) {
831
                        $this->_dirtree[dirname($data[1])] = true;
832
                        $this->pkginfo->setDirtree(dirname($data[1]));
833
 
834
                        while(!empty($data[3]) && $data[3] != '/' && $data[3] != '\\'
835
                              && $data[3] != '.') {
836
                            $this->pkginfo->setDirtree($pp =
837
                                $this->_prependPath($data[3], $data[2]));
838
                            $this->_dirtree[$pp] = true;
839
                            $data[3] = dirname($data[3]);
840
                        }
841
                    }
842
                    break;
843
            }
844
        }
845
        // }}}
846
        $this->log(2, "successfully committed $n file operations");
847
        $this->file_operations = array();
848
        return true;
849
    }
850
 
851
    // }}}
852
    // {{{ rollbackFileTransaction()
853
 
854
    function rollbackFileTransaction()
855
    {
856
        $n = count($this->file_operations);
857
        $this->log(2, "rolling back $n file operations");
858
        foreach ($this->file_operations as $tr) {
859
            list($type, $data) = $tr;
860
            switch ($type) {
861
                case 'backup':
862
                    if (file_exists($data[0] . '.bak')) {
863
                        if (file_exists($data[0] && is_writable($data[0]))) {
864
                            unlink($data[0]);
865
                        }
866
                        @copy($data[0] . '.bak', $data[0]);
867
                        $this->log(3, "+ restore $data[0] from $data[0].bak");
868
                    }
869
                    break;
870
                case 'removebackup':
871
                    if (file_exists($data[0] . '.bak') && is_writable($data[0] . '.bak')) {
872
                        unlink($data[0] . '.bak');
873
                        $this->log(3, "+ rm backup of $data[0] ($data[0].bak)");
874
                    }
875
                    break;
876
                case 'rename':
877
                    @unlink($data[0]);
878
                    $this->log(3, "+ rm $data[0]");
879
                    break;
880
                case 'mkdir':
881
                    @rmdir($data[0]);
882
                    $this->log(3, "+ rmdir $data[0]");
883
                    break;
884
                case 'chmod':
885
                    break;
886
                case 'delete':
887
                    break;
888
                case 'installed_as':
889
                    $this->pkginfo->setInstalledAs($data[0], false);
890
                    break;
891
            }
892
        }
893
        $this->pkginfo->resetDirtree();
894
        $this->file_operations = array();
895
    }
896
 
897
    // }}}
898
    // {{{ mkDirHier($dir)
899
 
900
    function mkDirHier($dir)
901
    {
902
        $this->addFileOperation('mkdir', array($dir));
903
        return parent::mkDirHier($dir);
904
    }
905
 
906
    // }}}
907
    // {{{ download()
908
 
909
    /**
910
     * Download any files and their dependencies, if necessary
911
     *
912
     * @param array a mixed list of package names, local files, or package.xml
913
     * @param PEAR_Config
914
     * @param array options from the command line
915
     * @param array this is the array that will be populated with packages to
916
     *              install.  Format of each entry:
917
     *
918
     * <code>
919
     * array('pkg' => 'package_name', 'file' => '/path/to/local/file',
920
     *    'info' => array() // parsed package.xml
921
     * );
922
     * </code>
923
     * @param array this will be populated with any error messages
924
     * @param false private recursion variable
925
     * @param false private recursion variable
926
     * @param false private recursion variable
927
     * @deprecated in favor of PEAR_Downloader
928
     */
929
    function download($packages, $options, &$config, &$installpackages,
930
                      &$errors, $installed = false, $willinstall = false, $state = false)
931
    {
932
        // trickiness: initialize here
933
        parent::PEAR_Downloader($this->ui, $options, $config);
934
        $ret = parent::download($packages);
935
        $errors = $this->getErrorMsgs();
936
        $installpackages = $this->getDownloadedPackages();
937
        trigger_error("PEAR Warning: PEAR_Installer::download() is deprecated " .
938
                      "in favor of PEAR_Downloader class", E_USER_WARNING);
939
        return $ret;
940
    }
941
 
942
    // }}}
943
    // {{{ _parsePackageXml()
944
 
945
    function _parsePackageXml(&$descfile, &$tmpdir)
946
    {
947
        if (substr($descfile, -4) == '.xml') {
948
            $tmpdir = false;
949
        } else {
950
            // {{{ Decompress pack in tmp dir -------------------------------------
951
 
952
            // To allow relative package file names
953
            $descfile = realpath($descfile);
954
 
955
            if (PEAR::isError($tmpdir = System::mktemp('-d'))) {
956
                return $tmpdir;
957
            }
958
            $this->log(3, '+ tmp dir created at ' . $tmpdir);
959
            // }}}
960
        }
961
        // Parse xml file -----------------------------------------------
962
        $pkg = new PEAR_PackageFile($this->config, $this->debug, $tmpdir);
963
        PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
964
        $p = &$pkg->fromAnyFile($descfile, PEAR_VALIDATE_INSTALLING);
965
        PEAR::staticPopErrorHandling();
966
        if (PEAR::isError($p)) {
967
            if (is_array($p->getUserInfo())) {
968
                foreach ($p->getUserInfo() as $err) {
969
                    $loglevel = $err['level'] == 'error' ? 0 : 1;
970
                    if (!isset($this->_options['soft'])) {
971
                        $this->log($loglevel, ucfirst($err['level']) . ': ' . $err['message']);
972
                    }
973
                }
974
            }
975
            return $this->raiseError('Installation failed: invalid package file');
976
        } else {
977
            $descfile = $p->getPackageFile();
978
        }
979
        return $p;
980
    }
981
 
982
    // }}}
983
    /**
984
     * Set the list of PEAR_Downloader_Package objects to allow more sane
985
     * dependency validation
986
     * @param array
987
     */
988
    function setDownloadedPackages(&$pkgs)
989
    {
990
        PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
991
        $err = $this->analyzeDependencies($pkgs);
992
        PEAR::popErrorHandling();
993
        if (PEAR::isError($err)) {
994
            return $err;
995
        }
996
        $this->_downloadedPackages = &$pkgs;
997
    }
998
 
999
    /**
1000
     * Set the list of PEAR_Downloader_Package objects to allow more sane
1001
     * dependency validation
1002
     * @param array
1003
     */
1004
    function setUninstallPackages(&$pkgs)
1005
    {
1006
        $this->_downloadedPackages = &$pkgs;
1007
    }
1008
 
1009
    function getInstallPackages()
1010
    {
1011
        return $this->_downloadedPackages;
1012
    }
1013
 
1014
    // {{{ install()
1015
 
1016
    /**
1017
     * Installs the files within the package file specified.
1018
     *
1019
     * @param string|PEAR_Downloader_Package $pkgfile path to the package file,
1020
     *        or a pre-initialized packagefile object
1021
     * @param array $options
1022
     * recognized options:
1023
     * - installroot   : optional prefix directory for installation
1024
     * - force         : force installation
1025
     * - register-only : update registry but don't install files
1026
     * - upgrade       : upgrade existing install
1027
     * - soft          : fail silently
1028
     * - nodeps        : ignore dependency conflicts/missing dependencies
1029
     * - alldeps       : install all dependencies
1030
     * - onlyreqdeps   : install only required dependencies
1031
     *
1032
     * @return array|PEAR_Error package info if successful
1033
     */
1034
 
1035
    function install($pkgfile, $options = array())
1036
    {
1037
        $this->_options = $options;
1038
        $this->_registry = &$this->config->getRegistry();
1039
        if (is_object($pkgfile)) {
1040
            $dlpkg = &$pkgfile;
1041
            $pkg = $pkgfile->getPackageFile();
1042
            $pkgfile = $pkg->getArchiveFile();
1043
            $descfile = $pkg->getPackageFile();
1044
            $tmpdir = dirname($descfile);
1045
        } else {
1046
            $descfile = $pkgfile;
1047
            $tmpdir = '';
1048
            if (PEAR::isError($pkg = &$this->_parsePackageXml($descfile, $tmpdir))) {
1049
                return $pkg;
1050
            }
1051
        }
1052
 
1053
        if (realpath($descfile) != realpath($pkgfile)) {
1054
            $tar = new Archive_Tar($pkgfile);
1055
            if (!$tar->extract($tmpdir)) {
1056
                return $this->raiseError("unable to unpack $pkgfile");
1057
            }
1058
        }
1059
 
1060
        $pkgname = $pkg->getName();
1061
        $channel = $pkg->getChannel();
1062
        if (isset($this->_options['packagingroot'])) {
1063
            $regdir = $this->_prependPath(
1064
                $this->config->get('php_dir', null, 'pear.php.net'),
1065
                $this->_options['packagingroot']);
1066
            $packrootphp_dir = $this->_prependPath(
1067
                $this->config->get('php_dir', null, $channel),
1068
                $this->_options['packagingroot']);
1069
        }
1070
 
1071
        if (isset($options['installroot'])) {
1072
            $this->config->setInstallRoot($options['installroot']);
1073
            $this->_registry = &$this->config->getRegistry();
1074
            $installregistry = &$this->_registry;
1075
            $this->installroot = ''; // all done automagically now
1076
            $php_dir = $this->config->get('php_dir', null, $channel);
1077
        } else {
1078
            $this->config->setInstallRoot(false);
1079
            $this->_registry = &$this->config->getRegistry();
1080
            if (isset($this->_options['packagingroot'])) {
1081
                $installregistry = &new PEAR_Registry($regdir);
1082
                if (!$installregistry->channelExists($channel, true)) {
1083
                    // we need to fake a channel-discover of this channel
1084
                    $chanobj = $this->_registry->getChannel($channel, true);
1085
                    $installregistry->addChannel($chanobj);
1086
                }
1087
                $php_dir = $packrootphp_dir;
1088
            } else {
1089
                $installregistry = &$this->_registry;
1090
                $php_dir = $this->config->get('php_dir', null, $channel);
1091
            }
1092
            $this->installroot = '';
1093
        }
1094
 
1095
        // {{{ checks to do when not in "force" mode
1096
        if (empty($options['force']) &&
1097
              (file_exists($this->config->get('php_dir')) &&
1098
               is_dir($this->config->get('php_dir')))) {
1099
            $testp = $channel == 'pear.php.net' ? $pkgname : array($channel, $pkgname);
1100
            $instfilelist = $pkg->getInstallationFileList(true);
1101
            if (PEAR::isError($instfilelist)) {
1102
                return $instfilelist;
1103
            }
1104
            // ensure we have the most accurate registry
1105
            $installregistry->flushFileMap();
1106
            $test = $installregistry->checkFileMap($instfilelist, $testp, '1.1');
1107
            if (PEAR::isError($test)) {
1108
                return $test;
1109
            }
1110
            if (sizeof($test)) {
1111
                $pkgs = $this->getInstallPackages();
1112
                $found = false;
1113
                foreach ($pkgs as $param) {
1114
                    if ($pkg->isSubpackageOf($param)) {
1115
                        $found = true;
1116
                        break;
1117
                    }
1118
                }
1119
                if ($found) {
1120
                    // subpackages can conflict with earlier versions of parent packages
1121
                    $parentreg = $installregistry->packageInfo($param->getPackage(), null, $param->getChannel());
1122
                    $tmp = $test;
1123
                    foreach ($tmp as $file => $info) {
1124
                        if (is_array($info)) {
1125
                            if (strtolower($info[1]) == strtolower($param->getPackage()) &&
1126
                                  strtolower($info[0]) == strtolower($param->getChannel())) {
1127
                                unset($test[$file]);
1128
                                unset($parentreg['filelist'][$file]);
1129
                            }
1130
                        } else {
1131
                            if (strtolower($param->getChannel()) != 'pear.php.net') {
1132
                                continue;
1133
                            }
1134
                            if (strtolower($info) == strtolower($param->getPackage())) {
1135
                                unset($test[$file]);
1136
                                unset($parentreg['filelist'][$file]);
1137
                            }
1138
                        }
1139
                    }
1140
                    $pfk = &new PEAR_PackageFile($this->config);
1141
                    $parentpkg = &$pfk->fromArray($parentreg);
1142
                    $installregistry->updatePackage2($parentpkg);
1143
                }
1144
                if ($param->getChannel() == 'pecl.php.net' && isset($options['upgrade'])) {
1145
                    $tmp = $test;
1146
                    foreach ($tmp as $file => $info) {
1147
                        if (is_string($info)) {
1148
                            // pear.php.net packages are always stored as strings
1149
                            if (strtolower($info) == strtolower($param->getPackage())) {
1150
                                // upgrading existing package
1151
                                unset($test[$file]);
1152
                            }
1153
                        }
1154
                    }
1155
                }
1156
                if (sizeof($test)) {
1157
                    $msg = "$channel/$pkgname: conflicting files found:\n";
1158
                    $longest = max(array_map("strlen", array_keys($test)));
1159
                    $fmt = "%${longest}s (%s)\n";
1160
                    foreach ($test as $file => $info) {
1161
                        if (!is_array($info)) {
1162
                            $info = array('pear.php.net', $info);
1163
                        }
1164
                        $info = $info[0] . '/' . $info[1];
1165
                        $msg .= sprintf($fmt, $file, $info);
1166
                    }
1167
                    if (!isset($options['ignore-errors'])) {
1168
                        return $this->raiseError($msg);
1169
                    } else {
1170
                        if (!isset($options['soft'])) {
1171
                            $this->log(0, "WARNING: $msg");
1172
                        }
1173
                    }
1174
                }
1175
            }
1176
        }
1177
        // }}}
1178
 
1179
        $this->startFileTransaction();
1180
 
1181
        if (empty($options['upgrade']) && empty($options['soft'])) {
1182
            // checks to do only when installing new packages
1183
            if ($channel == 'pecl.php.net') {
1184
                $test = $installregistry->packageExists($pkgname, $channel);
1185
                if (!$test) {
1186
                    $test = $installregistry->packageExists($pkgname, 'pear.php.net');
1187
                }
1188
            } else {
1189
                $test = $installregistry->packageExists($pkgname, $channel);
1190
            }
1191
            if (empty($options['force']) && $test) {
1192
                return $this->raiseError("$channel/$pkgname is already installed");
1193
            }
1194
        } else {
1195
            $usechannel = $channel;
1196
            if ($channel == 'pecl.php.net') {
1197
                $test = $installregistry->packageExists($pkgname, $channel);
1198
                if (!$test) {
1199
                    $test = $installregistry->packageExists($pkgname, 'pear.php.net');
1200
                    $usechannel = 'pear.php.net';
1201
                }
1202
            } else {
1203
                $test = $installregistry->packageExists($pkgname, $channel);
1204
            }
1205
            if ($test) {
1206
                $v1 = $installregistry->packageInfo($pkgname, 'version', $usechannel);
1207
                $v2 = $pkg->getVersion();
1208
                $cmp = version_compare("$v1", "$v2", 'gt');
1209
                if (empty($options['force']) && !version_compare("$v2", "$v1", 'gt')) {
1210
                    return $this->raiseError("upgrade to a newer version ($v2 is not newer than $v1)");
1211
                }
1212
                if (empty($options['register-only'])) {
1213
                    // when upgrading, remove old release's files first:
1214
                    if (PEAR::isError($err = $this->_deletePackageFiles($pkgname, $usechannel,
1215
                          true))) {
1216
                        if (!isset($options['ignore-errors'])) {
1217
                            return $this->raiseError($err);
1218
                        } else {
1219
                            if (!isset($options['soft'])) {
1220
                                $this->log(0, 'WARNING: ' . $err->getMessage());
1221
                            }
1222
                        }
1223
                    } else {
1224
                        $backedup = $err;
1225
                    }
1226
                }
1227
            }
1228
        }
1229
 
1230
        // {{{ Copy files to dest dir ---------------------------------------
1231
 
1232
        // info from the package it self we want to access from _installFile
1233
        $this->pkginfo = &$pkg;
1234
        // used to determine whether we should build any C code
1235
        $this->source_files = 0;
1236
 
1237
        $savechannel = $this->config->get('default_channel');
1238
        if (empty($options['register-only']) && !is_dir($php_dir)) {
1239
            if (PEAR::isError(System::mkdir(array('-p'), $php_dir))) {
1240
                return $this->raiseError("no installation destination directory '$php_dir'\n");
1241
            }
1242
        }
1243
 
1244
        $tmp_path = dirname($descfile);
1245
        if (substr($pkgfile, -4) != '.xml') {
1246
            $tmp_path .= DIRECTORY_SEPARATOR . $pkgname . '-' . $pkg->getVersion();
1247
        }
1248
 
1249
        $this->configSet('default_channel', $channel);
1250
        // {{{ install files
1251
 
1252
        $ver = $pkg->getPackagexmlVersion();
1253
        if (version_compare($ver, '2.0', '>=')) {
1254
            $filelist = $pkg->getInstallationFilelist();
1255
        } else {
1256
            $filelist = $pkg->getFileList();
1257
        }
1258
        if (PEAR::isError($filelist)) {
1259
            return $filelist;
1260
        }
1261
        $pkg->resetFilelist();
1262
        $pkg->setLastInstalledVersion($installregistry->packageInfo($pkg->getPackage(),
1263
            'version', $pkg->getChannel()));
1264
        foreach ($filelist as $file => $atts) {
1265
            if ($pkg->getPackagexmlVersion() == '1.0') {
1266
                $this->expectError(PEAR_INSTALLER_FAILED);
1267
                $res = $this->_installFile($file, $atts, $tmp_path, $options);
1268
                $this->popExpect();
1269
            } else {
1270
                $this->expectError(PEAR_INSTALLER_FAILED);
1271
                $res = $this->_installFile2($pkg, $file, $atts, $tmp_path, $options);
1272
                $this->popExpect();
1273
            }
1274
            if (PEAR::isError($res)) {
1275
                if (empty($options['ignore-errors'])) {
1276
                    $this->rollbackFileTransaction();
1277
                    if ($res->getMessage() == "file does not exist") {
1278
                        $this->raiseError("file $file in package.xml does not exist");
1279
                    }
1280
                    return $this->raiseError($res);
1281
                } else {
1282
                    if (!isset($options['soft'])) {
1283
                        $this->log(0, "Warning: " . $res->getMessage());
1284
                    }
1285
                }
1286
            }
1287
            if ($res == PEAR_INSTALLER_OK) {
1288
                // Register files that were installed
1289
                $pkg->installedFile($file, $atts);
1290
            }
1291
        }
1292
        // }}}
1293
 
1294
        // {{{ compile and install source files
1295
        if ($this->source_files > 0 && empty($options['nobuild'])) {
1296
            if (PEAR::isError($err =
1297
                  $this->_compileSourceFiles($savechannel, $pkg))) {
1298
                return $err;
1299
            }
1300
        }
1301
        // }}}
1302
 
1303
        if (isset($backedup)) {
1304
            $this->_removeBackups($backedup);
1305
        }
1306
        if (!$this->commitFileTransaction()) {
1307
            $this->rollbackFileTransaction();
1308
            $this->configSet('default_channel', $savechannel);
1309
            return $this->raiseError("commit failed", PEAR_INSTALLER_FAILED);
1310
        }
1311
        // }}}
1312
 
1313
        $ret = false;
1314
        $installphase = 'install';
1315
        $oldversion = false;
1316
        // {{{ Register that the package is installed -----------------------
1317
        if (empty($options['upgrade'])) {
1318
            // if 'force' is used, replace the info in registry
1319
            $usechannel = $channel;
1320
            if ($channel == 'pecl.php.net') {
1321
                $test = $installregistry->packageExists($pkgname, $channel);
1322
                if (!$test) {
1323
                    $test = $installregistry->packageExists($pkgname, 'pear.php.net');
1324
                    $usechannel = 'pear.php.net';
1325
                }
1326
            } else {
1327
                $test = $installregistry->packageExists($pkgname, $channel);
1328
            }
1329
            if (!empty($options['force']) && $test) {
1330
                $oldversion = $installregistry->packageInfo($pkgname, 'version', $usechannel);
1331
                $installregistry->deletePackage($pkgname, $usechannel);
1332
            }
1333
            $ret = $installregistry->addPackage2($pkg);
1334
        } else {
1335
            $usechannel = $channel;
1336
            if ($channel == 'pecl.php.net') {
1337
                $test = $installregistry->packageExists($pkgname, $channel);
1338
                if (!$test) {
1339
                    $test = $installregistry->packageExists($pkgname, 'pear.php.net');
1340
                    $usechannel = 'pear.php.net';
1341
                }
1342
            } else {
1343
                $test = $installregistry->packageExists($pkgname, $channel);
1344
            }
1345
            // new: upgrade installs a package if it isn't installed
1346
            if (!$test) {
1347
                $ret = $installregistry->addPackage2($pkg);
1348
            } else {
1349
                if ($usechannel != $channel) {
1350
                    $installregistry->deletePackage($pkgname, $usechannel);
1351
                    $ret = $installregistry->addPackage2($pkg);
1352
                } else {
1353
                    $ret = $installregistry->updatePackage2($pkg);
1354
                }
1355
                $installphase = 'upgrade';
1356
            }
1357
        }
1358
        if (!$ret) {
1359
            $this->configSet('default_channel', $savechannel);
1360
            return $this->raiseError("Adding package $channel/$pkgname to registry failed");
1361
        }
1362
        // }}}
1363
        $this->configSet('default_channel', $savechannel);
1364
        if (class_exists('PEAR_Task_Common')) { // this is auto-included if any tasks exist
1365
            if (PEAR_Task_Common::hasPostinstallTasks()) {
1366
                PEAR_Task_Common::runPostinstallTasks($installphase);
1367
            }
1368
        }
1369
        return $pkg->toArray(true);
1370
    }
1371
 
1372
    // }}}
1373
 
1374
    // {{{ _compileSourceFiles()
1375
    /**
1376
     * @param string
1377
     * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
1378
     */
1379
    function _compileSourceFiles($savechannel, &$filelist)
1380
    {
1381
        require_once 'PEAR/Builder.php';
1382
        $this->log(1, "$this->source_files source files, building");
1383
        $bob = &new PEAR_Builder($this->ui);
1384
        $bob->debug = $this->debug;
1385
        $built = $bob->build($filelist, array(&$this, '_buildCallback'));
1386
        if (PEAR::isError($built)) {
1387
            $this->rollbackFileTransaction();
1388
            $this->configSet('default_channel', $savechannel);
1389
            return $built;
1390
        }
1391
        $this->log(1, "\nBuild process completed successfully");
1392
        foreach ($built as $ext) {
1393
            $bn = basename($ext['file']);
1394
            list($_ext_name, $_ext_suff) = explode('.', $bn);
1395
            if ($_ext_suff == '.so' || $_ext_suff == '.dll') {
1396
                if (extension_loaded($_ext_name)) {
1397
                    $this->raiseError("Extension '$_ext_name' already loaded. " .
1398
                                      'Please unload it in your php.ini file ' .
1399
                                      'prior to install or upgrade');
1400
                }
1401
                $role = 'ext';
1402
            } else {
1403
                $role = 'src';
1404
            }
1405
            $dest = $ext['dest'];
1406
            $packagingroot = '';
1407
            if (isset($this->_options['packagingroot'])) {
1408
                $packagingroot = $this->_options['packagingroot'];
1409
            }
1410
            $copyto = $this->_prependPath($dest, $packagingroot);
1411
            if ($copyto != $dest) {
1412
	            $this->log(1, "Installing '$dest' as '$copyto'");
1413
            } else {
1414
	            $this->log(1, "Installing '$dest'");
1415
            }
1416
            $copydir = dirname($copyto);
1417
            // pretty much nothing happens if we are only registering the install
1418
            if (empty($this->_options['register-only'])) {
1419
                if (!file_exists($copydir) || !is_dir($copydir)) {
1420
                    if (!$this->mkDirHier($copydir)) {
1421
                        return $this->raiseError("failed to mkdir $copydir",
1422
                            PEAR_INSTALLER_FAILED);
1423
                    }
1424
                    $this->log(3, "+ mkdir $copydir");
1425
                }
1426
                if (!@copy($ext['file'], $copyto)) {
1427
                    return $this->raiseError("failed to write $copyto ($php_errormsg)", PEAR_INSTALLER_FAILED);
1428
                }
1429
                $this->log(3, "+ cp $ext[file] $copyto");
1430
                $this->addFileOperation('rename', array($ext['file'], $copyto));
1431
                if (!OS_WINDOWS) {
1432
                    $mode = 0666 & ~(int)octdec($this->config->get('umask'));
1433
                    $this->addFileOperation('chmod', array($mode, $copyto));
1434
                    if (!@chmod($copyto, $mode)) {
1435
                        $this->log(0, "failed to change mode of $copyto ($php_errormsg)");
1436
                    }
1437
                }
1438
            }
1439
 
1440
            if ($filelist->getPackageXmlVersion() == '1.0') {
1441
                $filelist->installedFile($bn, array(
1442
                    'role' => $role,
1443
                    'name' => $bn,
1444
                    'installed_as' => $dest,
1445
                    'php_api' => $ext['php_api'],
1446
                    'zend_mod_api' => $ext['zend_mod_api'],
1447
                    'zend_ext_api' => $ext['zend_ext_api'],
1448
                    ));
1449
            } else {
1450
                $filelist->installedFile($bn, array('attribs' => array(
1451
                    'role' => $role,
1452
                    'name' => $bn,
1453
                    'installed_as' => $dest,
1454
                    'php_api' => $ext['php_api'],
1455
                    'zend_mod_api' => $ext['zend_mod_api'],
1456
                    'zend_ext_api' => $ext['zend_ext_api'],
1457
                    )));
1458
            }
1459
        }
1460
    }
1461
 
1462
    // }}}
1463
    function &getUninstallPackages()
1464
    {
1465
        return $this->_downloadedPackages;
1466
    }
1467
    // {{{ uninstall()
1468
 
1469
    /**
1470
     * Uninstall a package
1471
     *
1472
     * This method removes all files installed by the application, and then
1473
     * removes any empty directories.
1474
     * @param string package name
1475
     * @param array Command-line options.  Possibilities include:
1476
     *
1477
     *              - installroot: base installation dir, if not the default
1478
     *              - register-only : update registry but don't remove files
1479
     *              - nodeps: do not process dependencies of other packages to ensure
1480
     *                        uninstallation does not break things
1481
     */
1482
    function uninstall($package, $options = array())
1483
    {
1484
        if (isset($options['installroot'])) {
1485
            $this->config->setInstallRoot($options['installroot']);
1486
            $this->installroot = '';
1487
        } else {
1488
            $this->config->setInstallRoot('');
1489
            $this->installroot = '';
1490
        }
1491
        $this->_registry = &$this->config->getRegistry();
1492
        if (is_object($package)) {
1493
            $channel = $package->getChannel();
1494
            $pkg = $package;
1495
            $package = $pkg->getPackage();
1496
        } else {
1497
            $pkg = false;
1498
            $info = $this->_registry->parsePackageName($package,
1499
                $this->config->get('default_channel'));
1500
            $channel = $info['channel'];
1501
            $package = $info['package'];
1502
        }
1503
        $savechannel = $this->config->get('default_channel');
1504
        $this->configSet('default_channel', $channel);
1505
        if (!is_object($pkg)) {
1506
            $pkg = $this->_registry->getPackage($package, $channel);
1507
        }
1508
        if (!$pkg) {
1509
            $this->configSet('default_channel', $savechannel);
1510
            return $this->raiseError($this->_registry->parsedPackageNameToString(
1511
                array(
1512
                    'channel' => $channel,
1513
                    'package' => $package
1514
                ), true) . ' not installed');
1515
        }
1516
        if ($pkg->getInstalledBinary()) {
1517
            // this is just an alias for a binary package
1518
            return $this->_registry->deletePackage($package, $channel);
1519
        }
1520
        $filelist = $pkg->getFilelist();
1521
        PEAR::staticPushErrorHandling(PEAR_ERROR_RETURN);
1522
        if (!class_exists('PEAR_Dependency2')) {
1523
            require_once 'PEAR/Dependency2.php';
1524
        }
1525
        $depchecker = &new PEAR_Dependency2($this->config, $options,
1526
            array('channel' => $channel, 'package' => $package),
1527
            PEAR_VALIDATE_UNINSTALLING);
1528
        $e = $depchecker->validatePackageUninstall($this);
1529
        PEAR::staticPopErrorHandling();
1530
        if (PEAR::isError($e)) {
1531
            if (!isset($options['ignore-errors'])) {
1532
                return $this->raiseError($e);
1533
            } else {
1534
                if (!isset($options['soft'])) {
1535
                    $this->log(0, 'WARNING: ' . $e->getMessage());
1536
                }
1537
            }
1538
        } elseif (is_array($e)) {
1539
            if (!isset($options['soft'])) {
1540
                $this->log(0, $e[0]);
1541
            }
1542
        }
1543
        $this->pkginfo = &$pkg;
1544
        // pretty much nothing happens if we are only registering the uninstall
1545
        if (empty($options['register-only'])) {
1546
            // {{{ Delete the files
1547
            $this->startFileTransaction();
1548
            PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
1549
            if (PEAR::isError($err = $this->_deletePackageFiles($package, $channel))) {
1550
                PEAR::popErrorHandling();
1551
                $this->rollbackFileTransaction();
1552
                $this->configSet('default_channel', $savechannel);
1553
                if (!isset($options['ignore-errors'])) {
1554
                    return $this->raiseError($err);
1555
                } else {
1556
                    if (!isset($options['soft'])) {
1557
                        $this->log(0, 'WARNING: ' . $err->getMessage());
1558
                    }
1559
                }
1560
            } else {
1561
                PEAR::popErrorHandling();
1562
            }
1563
            if (!$this->commitFileTransaction()) {
1564
                $this->rollbackFileTransaction();
1565
                if (!isset($options['ignore-errors'])) {
1566
                    return $this->raiseError("uninstall failed");
1567
                } elseif (!isset($options['soft'])) {
1568
                    $this->log(0, 'WARNING: uninstall failed');
1569
                }
1570
            } else {
1571
                $this->startFileTransaction();
1572
                if ($dirtree = $pkg->getDirTree()) {
1573
                    // attempt to delete empty directories
1574
                    uksort($dirtree, array($this, '_sortDirs'));
1575
                    foreach($dirtree as $dir => $notused) {
1576
                        $this->addFileOperation('rmdir', array($dir));
1577
                    }
1578
                } else {
1579
                    $this->configSet('default_channel', $savechannel);
1580
                    return $this->_registry->deletePackage($package, $channel);
1581
                }
1582
                if (!$this->commitFileTransaction()) {
1583
                    $this->rollbackFileTransaction();
1584
                    if (!isset($options['ignore-errors'])) {
1585
                        return $this->raiseError("uninstall failed");
1586
                    } elseif (!isset($options['soft'])) {
1587
                        $this->log(0, 'WARNING: uninstall failed');
1588
                    }
1589
                }
1590
            }
1591
            // }}}
1592
        }
1593
 
1594
        $this->configSet('default_channel', $savechannel);
1595
        // Register that the package is no longer installed
1596
        return $this->_registry->deletePackage($package, $channel);
1597
    }
1598
 
1599
    /**
1600
     * Sort a list of arrays of array(downloaded packagefilename) by dependency.
1601
     *
1602
     * It also removes duplicate dependencies
1603
     * @param array an array of PEAR_PackageFile_v[1/2] objects
1604
     * @return array|PEAR_Error array of array(packagefilename, package.xml contents)
1605
     */
1606
    function sortPackagesForUninstall(&$packages)
1607
    {
1608
        $this->_dependencyDB = &PEAR_DependencyDB::singleton($this->config);
1609
        if (PEAR::isError($this->_dependencyDB)) {
1610
            return $this->_dependencyDB;
1611
        }
1612
        usort($packages, array(&$this, '_sortUninstall'));
1613
    }
1614
 
1615
    function _sortUninstall($a, $b)
1616
    {
1617
        if (!$a->getDeps() && !$b->getDeps()) {
1618
            return 0; // neither package has dependencies, order is insignificant
1619
        }
1620
        if ($a->getDeps() && !$b->getDeps()) {
1621
            return -1; // $a must be installed after $b because $a has dependencies
1622
        }
1623
        if (!$a->getDeps() && $b->getDeps()) {
1624
            return 1; // $b must be installed after $a because $b has dependencies
1625
        }
1626
        // both packages have dependencies
1627
        if ($this->_dependencyDB->dependsOn($a, $b)) {
1628
            return -1;
1629
        }
1630
        if ($this->_dependencyDB->dependsOn($b, $a)) {
1631
            return 1;
1632
        }
1633
        return 0;
1634
    }
1635
 
1636
    // }}}
1637
    // {{{ _sortDirs()
1638
    function _sortDirs($a, $b)
1639
    {
1640
        if (strnatcmp($a, $b) == -1) return 1;
1641
        if (strnatcmp($a, $b) == 1) return -1;
1642
        return 0;
1643
    }
1644
 
1645
    // }}}
1646
 
1647
    // {{{ _buildCallback()
1648
 
1649
    function _buildCallback($what, $data)
1650
    {
1651
        if (($what == 'cmdoutput' && $this->debug > 1) ||
1652
            ($what == 'output' && $this->debug > 0)) {
1653
            $this->ui->outputData(rtrim($data), 'build');
1654
        }
1655
    }
1656
 
1657
    // }}}
1658
}
1659
 
1660
// {{{ md5_file() utility function
1661
if (!function_exists("md5_file")) {
1662
    function md5_file($filename) {
1663
        if (!$fd = @fopen($file, 'r')) {
1664
            return false;
1665
        }
1666
        fclose($fd);
1667
        return md5(file_get_contents($filename));
1668
    }
1669
}
1670
// }}}
1671
 
1672
?>