summaryrefslogtreecommitdiff
path: root/pear/PEAR/Installer.php
blob: a4c595691a962aba4efa01e38daa2cbcd9ccd5a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
//
// +----------------------------------------------------------------------+
// | PHP version 4.0                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2001 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license,      |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Stig Bakken <ssb@fast.no>                                   |
// |          Tomas V.V.Cox <cox@idecnet.com>                             |
// |                                                                      |
// +----------------------------------------------------------------------+
//
// $Id$

require_once "PEAR/Common.php";

/**
 * Administration class used to install PEAR packages and maintain the
 * installed package database.
 *
 * TODO:
 *  - maintain file perms (look at umask or fileperms+chmod), ideas are welcome
 *
 * @since PHP 4.0.2
 * @author Stig Bakken <ssb@fast.no>
 */
class PEAR_Installer extends PEAR_Common
{
    // {{{ properties

    /** name of the package directory, for example Foo-1.0 */
    var $pkgdir;

    /** directory where PHP code files go */
    var $phpdir;

    /** directory where PHP extension files go */
    var $extdir;

    /** directory where documentation goes */
    var $docdir;

    /** directory where the package wants to put files, relative
     *  to one of the three previous dirs
     */
    var $destdir = '';

    /** debug level (integer) */
    var $debug = 1;

    /** temporary directory */
    var $tmpdir;

    // }}}

    // {{{ constructor

    function PEAR_Installer($phpdir = PEAR_INSTALL_DIR,
                            $extdir = PEAR_EXTENSION_DIR,
                            $docdir = '')
    {
        $this->PEAR();
        $this->phpdir = $phpdir;
        $this->extdir = $extdir;
        $this->docdir = $docdir;
        $this->statedir = "/var/lib/php"; // XXX FIXME Windows
    }

    // }}}
    // {{{ destructor

    function _PEAR_Installer()
    {
        chdir($this->pwd);
        if ($this->tmpdir && is_dir($this->tmpdir)) {
            system("rm -rf $this->tmpdir"); // XXX FIXME Windows
        }
        $this->tmpdir = null;
        $this->_PEAR_Common();
    }

    // }}}
    // {{{ install()

    /**
     * Installs the files within the package file specified.
     *
     * @param $pkgfile path to the package file
     *
     * @return bool true if successful, false if not
     */

    function install($pkgfile)
    {
        // XXX FIXME Add here code to manage packages database
        //$this->loadPackageList("$this->statedir/packages.lst");
        $this->pwd = getcwd();
        $need_download = false;
        if (preg_match('#^(http|ftp)://#', $pkgfile)) {
            $need_download = true;
        } elseif (!@is_file($pkgfile)) {
            return $this->raiseError("Could not open the package file: $pkgfile");
        }
        // Download package -----------------------------------------------
        if ($need_download) {
            $file = basename($pkgfile);
            // XXX FIXME use ??? on Windows, use $TMPDIR on unix (use tmpnames?)
            $downloaddir = '/tmp/pearinstall';
            if (!$this->mkDirHier($downloaddir)) {
                return $this->raiseError("Failed to create tmp dir: $downloaddir");
            }
            $downloadfile = $downloaddir.DIRECTORY_SEPARATOR.$file;
            $this->log(1, "downloading $pkgfile ...");
            if (!$fp = @fopen($pkgfile, 'r')) {
                return $this->raiseError("$pkgfile: failed to download ($php_errormsg)");
            }
            if (!$wp = @fopen($downloadfile, 'w')) {
                return $this->raiseError("$downloadfile: write failed ($php_errormsg)");
            }
            $this->addTempFile($downloadfile);
            $bytes = 0;
            while ($data = @fread($fp, 16384)) {
                $bytes += strlen($data);
                if (!@fwrite($wp, $data)) {
                    return $this->raiseError("$downloadfile: write failed ($php_errormsg)");
                }
            }
            $pkgfile = $downloadfile;
            fclose($fp);
            fclose($wp);
            $this->log(1, '...done: ' . number_format($bytes, 0, '', ',') . ' bytes');
        }

        // Decompress pack in tmp dir -------------------------------------

        // To allow relative package file calls
        if (!chdir(dirname($pkgfile))) {
            return $this->raiseError('Unable to chdir to pakage directory');
        }
        $pkgfile = getcwd() . DIRECTORY_SEPARATOR . basename($pkgfile);

        // XXX FIXME Windows
        $this->tmpdir = tempnam('/tmp', 'pear');
        unlink($this->tmpdir);
        if (!mkdir($this->tmpdir, 0755)) {
            return $this->raiseError("Unable to create temporary directory $this->tmpdir.");
        } else {
            $this->log(2, '+ tmp dir created at ' . $this->tmpdir);
        }
        $this->addTempFile($this->tmpdir);
        if (!chdir($this->tmpdir)) {
            return $this->raiseError("Unable to chdir to $this->tmpdir.");
        }
        // XXX FIXME Windows
        $fp = popen("gzip -dc $pkgfile | tar -xvf -", 'r');
        $this->log(2, "+ launched command: gzip -dc $pkgfile | tar -xvf -");
        if (!is_resource($fp)) {
            return $this->raiseError("Unable to examine $pkgfile (gzip or tar failed)");
        }
        while ($line = fgets($fp, 4096)) {
            $line = rtrim($line);
            if (preg_match('!^[^/]+/package.xml$!', $line)) {
                if (isset($descfile)) {
                    return $this->raiseError("Invalid package: multiple package.xml files at depth one!");
                }
                $descfile = $line;
            }
        }
        pclose($fp);
        if (!isset($descfile) || !is_file($descfile)) {
            return $this->raiseError("No package.xml file after extracting the archive.");
        }

        // Parse xml file -----------------------------------------------
        $pkginfo = $this->infoFromDescriptionFile($descfile);
        if (PEAR::isError($pkginfo)) {
            return $pkginfo;
        }

        // Copy files to dest dir ---------------------------------------
        if (!is_dir($this->phpdir)) {
            return $this->raiseError("No script destination directory found\n",
                                     null, PEAR_ERROR_DIE);
        }
        $tmp_path = dirname($descfile);
        foreach ($pkginfo['filelist'] as $fname => $atts) {
            $dest_dir = $this->phpdir . DIRECTORY_SEPARATOR;
            if (isset($atts['BaseInstallDir'])) {
                $dest_dir .= $atts['BaseInstallDir'] . DIRECTORY_SEPARATOR;
            }
            if (dirname($fname) != '.') {
                $dest_dir .= dirname($fname) . DIRECTORY_SEPARATOR;
            }
            $fname = $tmp_path . DIRECTORY_SEPARATOR . $fname;
            $this->_installFile($fname, $dest_dir, $atts);
        }
        return true;
    }

    function _installFile($file, $dest_dir, $atts)
    {
        $type = strtolower($atts['Role']);
        switch ($type) {
            case "test":
                // don't install test files for now
                $this->log(2, "+ Test file $file won't be installed yet");
                return true;
                break;
            case 'doc':
            case 'php':
            default:
                $dest_file = $dest_dir . basename($file);
                break;
        }
        if (!@is_dir($dest_dir)) {
            if (!$this->mkDirHier($dest_dir)) {
                $this->log(0, "failed to mkdir $dest_dir");
                return false;
            }
            $this->log(2, "+ created dir $dest_dir");
        }
        if (!@copy($file, $dest_file)) {
            $this->log(0, "failed to copy $file to $dest_file");
            return false;
        }
        $this->log(2, "+ copy $file to $dest_file");
        // FIXME Update Package database here
        //$this->updatePackageListFrom("$d/$file");
        $this->log(1, "installed file $dest_file");
        return true;
    }
    // }}}
}
?>