Subversion Repositories Applications.gtt

Rev

Rev 94 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
1
<?php
1
<?php
2
/**
2
/**
3
 * PEAR_PackageFile, package.xml parsing utility class
3
 * PEAR_PackageFile, package.xml parsing utility class
4
 *
4
 *
5
 * PHP versions 4 and 5
5
 * PHP versions 4 and 5
6
 *
6
 *
7
 * LICENSE: This source file is subject to version 3.0 of the PHP license
-
 
8
 * that is available through the world-wide-web at the following URI:
-
 
9
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
-
 
10
 * the PHP License and are unable to obtain it through the web, please
-
 
11
 * send a note to license@php.net so we can mail you a copy immediately.
-
 
12
 *
-
 
13
 * @category   pear
7
 * @category   pear
14
 * @package    PEAR
8
 * @package    PEAR
15
 * @author     Greg Beaver <cellog@php.net>
9
 * @author     Greg Beaver <cellog@php.net>
16
 * @copyright  1997-2006 The PHP Group
10
 * @copyright  1997-2009 The Authors
17
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
11
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
18
 * @version    CVS: $Id: PackageFile.php,v 1.40 2006/09/25 05:12:21 cellog Exp $
-
 
19
 * @link       http://pear.php.net/package/PEAR
12
 * @link       http://pear.php.net/package/PEAR
20
 * @since      File available since Release 1.4.0a1
13
 * @since      File available since Release 1.4.0a1
21
 */
14
 */
22
 
15
 
23
/**
16
/**
24
 * needed for PEAR_VALIDATE_* constants
17
 * needed for PEAR_VALIDATE_* constants
25
 */
18
 */
26
require_once 'PEAR/Validate.php';
19
require_once 'PEAR/Validate.php';
27
/**
20
/**
28
 * Error code if the package.xml <package> tag does not contain a valid version
21
 * Error code if the package.xml <package> tag does not contain a valid version
29
 */
22
 */
30
define('PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION', 1);
23
define('PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION', 1);
31
/**
24
/**
32
 * Error code if the package.xml <package> tag version is not supported (version 1.0 and 1.1 are the only supported versions,
25
 * Error code if the package.xml <package> tag version is not supported (version 1.0 and 1.1 are the only supported versions,
33
 * currently
26
 * currently
34
 */
27
 */
35
define('PEAR_PACKAGEFILE_ERROR_INVALID_PACKAGEVERSION', 2);
28
define('PEAR_PACKAGEFILE_ERROR_INVALID_PACKAGEVERSION', 2);
36
/**
29
/**
37
 * Abstraction for the package.xml package description file
30
 * Abstraction for the package.xml package description file
38
 *
31
 *
39
 * @category   pear
32
 * @category   pear
40
 * @package    PEAR
33
 * @package    PEAR
41
 * @author     Greg Beaver <cellog@php.net>
34
 * @author     Greg Beaver <cellog@php.net>
42
 * @copyright  1997-2006 The PHP Group
35
 * @copyright  1997-2009 The Authors
43
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
36
 * @license    http://opensource.org/licenses/bsd-license.php New BSD License
44
 * @version    Release: 1.5.1
37
 * @version    Release: 1.10.1
45
 * @link       http://pear.php.net/package/PEAR
38
 * @link       http://pear.php.net/package/PEAR
46
 * @since      Class available since Release 1.4.0a1
39
 * @since      Class available since Release 1.4.0a1
47
 */
40
 */
48
class PEAR_PackageFile
41
class PEAR_PackageFile
49
{
42
{
50
    /**
43
    /**
51
     * @var PEAR_Config
44
     * @var PEAR_Config
52
     */
45
     */
53
    var $_config;
46
    var $_config;
54
    var $_debug;
47
    var $_debug;
55
    /**
-
 
56
     * Temp directory for uncompressing tgz files.
-
 
57
     * @var string|false
-
 
58
     */
48
 
59
    var $_tmpdir;
-
 
60
    var $_logger = false;
49
    var $_logger = false;
61
    /**
50
    /**
62
     * @var boolean
51
     * @var boolean
63
     */
52
     */
64
    var $_rawReturn = false;
53
    var $_rawReturn = false;
65
 
54
 
66
    /**
55
    /**
-
 
56
     * helper for extracting Archive_Tar errors
-
 
57
     * @var array
-
 
58
     * @access private
-
 
59
     */
-
 
60
    var $_extractErrors = array();
-
 
61
 
-
 
62
    /**
67
     *
63
     *
68
     * @param   PEAR_Config $config
64
     * @param   PEAR_Config $config
69
     * @param   ?   $debug
65
     * @param   ?   $debug
70
     * @param   string @tmpdir Optional temporary directory for uncompressing
66
     * @param   string @tmpdir Optional temporary directory for uncompressing
71
     *          files
67
     *          files
72
     */
68
     */
73
    function PEAR_PackageFile(&$config, $debug = false, $tmpdir = false)
69
    function __construct(&$config, $debug = false)
74
    {
70
    {
75
        $this->_config = $config;
71
        $this->_config = $config;
76
        $this->_debug = $debug;
72
        $this->_debug = $debug;
77
        $this->_tmpdir = $tmpdir;
-
 
78
    }
73
    }
79
 
74
 
80
    /**
75
    /**
81
     * Turn off validation - return a parsed package.xml without checking it
76
     * Turn off validation - return a parsed package.xml without checking it
82
     *
77
     *
83
     * This is used by the package-validate command
78
     * This is used by the package-validate command
84
     */
79
     */
85
    function rawReturn()
80
    function rawReturn()
86
    {
81
    {
87
        $this->_rawReturn = true;
82
        $this->_rawReturn = true;
88
    }
83
    }
89
 
84
 
90
    function setLogger(&$l)
85
    function setLogger(&$l)
91
    {
86
    {
92
        $this->_logger = &$l;
87
        $this->_logger = &$l;
93
    }
88
    }
94
 
89
 
95
    /**
90
    /**
96
     * Create a PEAR_PackageFile_Parser_v* of a given version.
91
     * Create a PEAR_PackageFile_Parser_v* of a given version.
97
     * @param   int $version
92
     * @param   int $version
98
     * @return  PEAR_PackageFile_Parser_v1|PEAR_PackageFile_Parser_v1
93
     * @return  PEAR_PackageFile_Parser_v1|PEAR_PackageFile_Parser_v1
99
     */
94
     */
100
    function &parserFactory($version)
95
    function &parserFactory($version)
101
    {
96
    {
102
        if (!in_array($version{0}, array('1', '2'))) {
97
        if (!in_array($version{0}, array('1', '2'))) {
103
            $a = false;
98
            $a = false;
104
            return $a;
99
            return $a;
105
        }
100
        }
-
 
101
 
106
        include_once 'PEAR/PackageFile/Parser/v' . $version{0} . '.php';
102
        include_once 'PEAR/PackageFile/Parser/v' . $version{0} . '.php';
107
        $version = $version{0};
103
        $version = $version{0};
108
        $class = "PEAR_PackageFile_Parser_v$version";
104
        $class = "PEAR_PackageFile_Parser_v$version";
109
        $a = new $class;
105
        $a = new $class;
110
        return $a;
106
        return $a;
111
    }
107
    }
112
 
108
 
113
    /**
109
    /**
114
     * For simpler unit-testing
110
     * For simpler unit-testing
115
     * @return string
111
     * @return string
116
     */
112
     */
117
    function getClassPrefix()
113
    function getClassPrefix()
118
    {
114
    {
119
        return 'PEAR_PackageFile_v';
115
        return 'PEAR_PackageFile_v';
120
    }
116
    }
121
 
117
 
122
    /**
118
    /**
123
     * Create a PEAR_PackageFile_v* of a given version.
119
     * Create a PEAR_PackageFile_v* of a given version.
124
     * @param   int $version
120
     * @param   int $version
125
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v1
121
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v1
126
     */
122
     */
127
    function &factory($version)
123
    function &factory($version)
128
    {
124
    {
129
        if (!in_array($version{0}, array('1', '2'))) {
125
        if (!in_array($version{0}, array('1', '2'))) {
130
            $a = false;
126
            $a = false;
131
            return $a;
127
            return $a;
132
        }
128
        }
-
 
129
 
133
        include_once 'PEAR/PackageFile/v' . $version{0} . '.php';
130
        include_once 'PEAR/PackageFile/v' . $version{0} . '.php';
134
        $version = $version{0};
131
        $version = $version{0};
135
        $class = $this->getClassPrefix() . $version;
132
        $class = $this->getClassPrefix() . $version;
136
        $a = new $class;
133
        $a = new $class;
137
        return $a;
134
        return $a;
138
    }
135
    }
139
 
136
 
140
    /**
137
    /**
141
     * Create a PEAR_PackageFile_v* from its toArray() method
138
     * Create a PEAR_PackageFile_v* from its toArray() method
142
     *
139
     *
143
     * WARNING: no validation is performed, the array is assumed to be valid,
140
     * WARNING: no validation is performed, the array is assumed to be valid,
144
     * always parse from xml if you want validation.
141
     * always parse from xml if you want validation.
145
     * @param   array $arr
142
     * @param   array $arr
146
     * @return PEAR_PackageFileManager_v1|PEAR_PackageFileManager_v2
143
     * @return PEAR_PackageFileManager_v1|PEAR_PackageFileManager_v2
147
     * @uses    factory() to construct the returned object.
144
     * @uses    factory() to construct the returned object.
148
     */
145
     */
149
    function &fromArray($arr)
146
    function &fromArray($arr)
150
    {
147
    {
151
        if (isset($arr['xsdversion'])) {
148
        if (isset($arr['xsdversion'])) {
152
            $obj = &$this->factory($arr['xsdversion']);
149
            $obj = &$this->factory($arr['xsdversion']);
153
            if ($this->_logger) {
150
            if ($this->_logger) {
154
                $obj->setLogger($this->_logger);
151
                $obj->setLogger($this->_logger);
155
            }
152
            }
-
 
153
 
156
            $obj->setConfig($this->_config);
154
            $obj->setConfig($this->_config);
157
            $obj->fromArray($arr);
155
            $obj->fromArray($arr);
158
            return $obj;
156
            return $obj;
-
 
157
        }
-
 
158
 
-
 
159
        if (isset($arr['package']['attribs']['version'])) {
-
 
160
            $obj = &$this->factory($arr['package']['attribs']['version']);
159
        } else {
161
        } else {
160
            if (isset($arr['package']['attribs']['version'])) {
-
 
161
                $obj = &$this->factory($arr['package']['attribs']['version']);
-
 
162
            } else {
-
 
163
                $obj = &$this->factory('1.0');
162
            $obj = &$this->factory('1.0');
164
            }
-
 
165
            if ($this->_logger) {
-
 
166
                $obj->setLogger($this->_logger);
-
 
167
            }
-
 
168
            $obj->setConfig($this->_config);
-
 
169
            $obj->fromArray($arr);
-
 
170
            return $obj;
-
 
171
        }
163
        }
-
 
164
 
-
 
165
        if ($this->_logger) {
-
 
166
            $obj->setLogger($this->_logger);
-
 
167
        }
-
 
168
 
-
 
169
        $obj->setConfig($this->_config);
-
 
170
        $obj->fromArray($arr);
-
 
171
        return $obj;
172
    }
172
    }
173
 
173
 
174
    /**
174
    /**
175
     * Create a PEAR_PackageFile_v* from an XML string.
175
     * Create a PEAR_PackageFile_v* from an XML string.
176
     * @access  public
176
     * @access  public
177
     * @param   string $data contents of package.xml file
177
     * @param   string $data contents of package.xml file
178
     * @param   int $state package state (one of PEAR_VALIDATE_* constants)
178
     * @param   int $state package state (one of PEAR_VALIDATE_* constants)
179
     * @param   string $file full path to the package.xml file (and the files
179
     * @param   string $file full path to the package.xml file (and the files
180
     *          it references)
180
     *          it references)
181
     * @param   string $archive optional name of the archive that the XML was
181
     * @param   string $archive optional name of the archive that the XML was
182
     *          extracted from, if any
182
     *          extracted from, if any
183
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
183
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
184
     * @uses    parserFactory() to construct a parser to load the package.
184
     * @uses    parserFactory() to construct a parser to load the package.
185
     */
185
     */
186
    function &fromXmlString($data, $state, $file, $archive = false)
186
    function &fromXmlString($data, $state, $file, $archive = false)
187
    {
187
    {
188
        if (preg_match('/<package[^>]+version="([0-9]+\.[0-9]+)"/', $data, $packageversion)) {
188
        if (preg_match('/<package[^>]+version=[\'"]([0-9]+\.[0-9]+)[\'"]/', $data, $packageversion)) {
189
            if (!in_array($packageversion[1], array('1.0', '2.0', '2.1'))) {
189
            if (!in_array($packageversion[1], array('1.0', '2.0', '2.1'))) {
190
                return PEAR::raiseError('package.xml version "' . $packageversion[1] .
190
                return PEAR::raiseError('package.xml version "' . $packageversion[1] .
191
                    '" is not supported, only 1.0, 2.0, and 2.1 are supported.');
191
                    '" is not supported, only 1.0, 2.0, and 2.1 are supported.');
192
            }
192
            }
-
 
193
 
193
            $object = &$this->parserFactory($packageversion[1]);
194
            $object = &$this->parserFactory($packageversion[1]);
194
            if ($this->_logger) {
195
            if ($this->_logger) {
195
                $object->setLogger($this->_logger);
196
                $object->setLogger($this->_logger);
196
            }
197
            }
-
 
198
 
197
            $object->setConfig($this->_config);
199
            $object->setConfig($this->_config);
198
            $pf = $object->parse($data, $file, $archive);
200
            $pf = $object->parse($data, $file, $archive);
199
            if (PEAR::isError($pf)) {
201
            if (PEAR::isError($pf)) {
200
                return $pf;
202
                return $pf;
201
            }
203
            }
-
 
204
 
202
            if ($this->_rawReturn) {
205
            if ($this->_rawReturn) {
203
                return $pf;
206
                return $pf;
204
            }
207
            }
-
 
208
 
205
            if ($pf->validate($state)) {
209
            if (!$pf->validate($state)) {;
206
                if ($this->_logger) {
210
                if ($this->_config->get('verbose') > 0
207
                    if ($pf->getValidationWarnings(false)) {
211
                    && $this->_logger && $pf->getValidationWarnings(false)
208
                        foreach ($pf->getValidationWarnings() as $warning) {
-
 
209
                            $this->_logger->log(0, 'WARNING: ' . $warning['message']);
-
 
210
                        }
-
 
211
                    }
-
 
212
                }
-
 
213
                if (method_exists($pf, 'flattenFilelist')) {
-
 
214
                    $pf->flattenFilelist(); // for v2
-
 
215
                }
212
                ) {
216
                return $pf;
-
 
217
            } else {
-
 
218
                if ($this->_config->get('verbose') > 0) {
-
 
219
                    if ($this->_logger) {
-
 
220
                        if ($pf->getValidationWarnings(false)) {
-
 
221
                            foreach ($pf->getValidationWarnings(false) as $warning) {
213
                    foreach ($pf->getValidationWarnings(false) as $warning) {
222
                                $this->_logger->log(0, 'ERROR: ' . $warning['message']);
214
                        $this->_logger->log(0, 'ERROR: ' . $warning['message']);
223
                            }
-
 
224
                        }
-
 
225
                    }
215
                    }
226
                }
216
                }
-
 
217
 
227
                $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
218
                $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
228
                    2, null, null, $pf->getValidationWarnings());
219
                    2, null, null, $pf->getValidationWarnings());
229
                return $a;
220
                return $a;
230
            }
221
            }
-
 
222
 
-
 
223
            if ($this->_logger && $pf->getValidationWarnings(false)) {
-
 
224
                foreach ($pf->getValidationWarnings() as $warning) {
-
 
225
                    $this->_logger->log(0, 'WARNING: ' . $warning['message']);
-
 
226
                }
-
 
227
            }
-
 
228
 
-
 
229
            if (method_exists($pf, 'flattenFilelist')) {
-
 
230
                $pf->flattenFilelist(); // for v2
-
 
231
            }
-
 
232
 
-
 
233
            return $pf;
231
        } elseif (preg_match('/<package[^>]+version="([^"]+)"/', $data, $packageversion)) {
234
        } elseif (preg_match('/<package[^>]+version=[\'"]([^"\']+)[\'"]/', $data, $packageversion)) {
232
            $a = PEAR::raiseError('package.xml file "' . $file .
235
            $a = PEAR::raiseError('package.xml file "' . $file .
233
                '" has unsupported package.xml <package> version "' . $packageversion[1] . '"');
236
                '" has unsupported package.xml <package> version "' . $packageversion[1] . '"');
234
            return $a;
237
            return $a;
235
        } else {
238
        } else {
236
            if (!class_exists('PEAR_ErrorStack')) {
239
            if (!class_exists('PEAR_ErrorStack')) {
237
                require_once 'PEAR/ErrorStack.php';
240
                require_once 'PEAR/ErrorStack.php';
238
            }
241
            }
-
 
242
 
239
            PEAR_ErrorStack::staticPush('PEAR_PackageFile',
243
            PEAR_ErrorStack::staticPush('PEAR_PackageFile',
240
                PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION,
244
                PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION,
241
                'warning', array('xml' => $data), 'package.xml "' . $file .
245
                'warning', array('xml' => $data), 'package.xml "' . $file .
242
                    '" has no package.xml <package> version');
246
                    '" has no package.xml <package> version');
243
            $object = &$this->parserFactory('1.0');
247
            $object = &$this->parserFactory('1.0');
244
            $object->setConfig($this->_config);
248
            $object->setConfig($this->_config);
245
            $pf = $object->parse($data, $file, $archive);
249
            $pf = $object->parse($data, $file, $archive);
246
            if (PEAR::isError($pf)) {
250
            if (PEAR::isError($pf)) {
247
                return $pf;
251
                return $pf;
248
            }
252
            }
-
 
253
 
249
            if ($this->_rawReturn) {
254
            if ($this->_rawReturn) {
250
                return $pf;
255
                return $pf;
251
            }
256
            }
-
 
257
 
252
            if ($pf->validate($state)) {
258
            if (!$pf->validate($state)) {
253
                if ($this->_logger) {
-
 
254
                    if ($pf->getValidationWarnings(false)) {
-
 
255
                        foreach ($pf->getValidationWarnings() as $warning) {
-
 
256
                            $this->_logger->log(0, 'WARNING: ' . $warning['message']);
-
 
257
                        }
-
 
258
                    }
-
 
259
                }
-
 
260
                if (method_exists($pf, 'flattenFilelist')) {
-
 
261
                    $pf->flattenFilelist(); // for v2
-
 
262
                }
-
 
263
                return $pf;
-
 
264
            } else {
-
 
265
                $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
259
                $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
266
                    2, null, null, $pf->getValidationWarnings());
260
                    2, null, null, $pf->getValidationWarnings());
267
                return $a;
261
                return $a;
268
            }
262
            }
-
 
263
 
-
 
264
            if ($this->_logger && $pf->getValidationWarnings(false)) {
-
 
265
                foreach ($pf->getValidationWarnings() as $warning) {
-
 
266
                    $this->_logger->log(0, 'WARNING: ' . $warning['message']);
-
 
267
                }
-
 
268
            }
-
 
269
 
-
 
270
            if (method_exists($pf, 'flattenFilelist')) {
-
 
271
                $pf->flattenFilelist(); // for v2
-
 
272
            }
-
 
273
 
-
 
274
            return $pf;
269
        }
275
        }
270
    }
276
    }
271
 
277
 
272
    /**
278
    /**
273
     * Register a temporary file or directory.  When the destructor is
279
     * Register a temporary file or directory.  When the destructor is
274
     * executed, all registered temporary files and directories are
280
     * executed, all registered temporary files and directories are
275
     * removed.
281
     * removed.
276
     *
282
     *
277
     * @param string  $file  name of file or directory
283
     * @param string  $file  name of file or directory
278
     * @return  void
284
     * @return  void
279
     */
285
     */
280
    function addTempFile($file)
286
    function addTempFile($file)
281
    {
287
    {
282
        $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
288
        $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
283
    }
289
    }
284
 
290
 
285
    /**
291
    /**
286
     * Create a PEAR_PackageFile_v* from a compresed Tar or Tgz file.
292
     * Create a PEAR_PackageFile_v* from a compresed Tar or Tgz file.
287
     * @access  public
293
     * @access  public
288
     * @param string contents of package.xml file
294
     * @param string contents of package.xml file
289
     * @param int package state (one of PEAR_VALIDATE_* constants)
295
     * @param int package state (one of PEAR_VALIDATE_* constants)
290
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
296
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
291
     * @using   Archive_Tar to extract the files
297
     * @using   Archive_Tar to extract the files
292
     * @using   fromPackageFile() to load the package after the package.xml
298
     * @using   fromPackageFile() to load the package after the package.xml
293
     *          file is extracted.
299
     *          file is extracted.
294
     */
300
     */
295
    function &fromTgzFile($file, $state)
301
    function &fromTgzFile($file, $state)
296
    {
302
    {
297
        if (!class_exists('Archive_Tar')) {
303
        if (!class_exists('Archive_Tar')) {
298
            require_once 'Archive/Tar.php';
304
            require_once 'Archive/Tar.php';
299
        }
305
        }
-
 
306
 
300
        $tar = new Archive_Tar($file);
307
        $tar = new Archive_Tar($file);
301
        if ($this->_debug <= 1) {
308
        if ($this->_debug <= 1) {
302
            $tar->pushErrorHandling(PEAR_ERROR_RETURN);
309
            $tar->pushErrorHandling(PEAR_ERROR_RETURN);
303
        }
310
        }
-
 
311
 
304
        $content = $tar->listContent();
312
        $content = $tar->listContent();
305
        if ($this->_debug <= 1) {
313
        if ($this->_debug <= 1) {
306
            $tar->popErrorHandling();
314
            $tar->popErrorHandling();
307
        }
315
        }
-
 
316
 
308
        if (!is_array($content)) {
317
        if (!is_array($content)) {
309
            if (is_string($file) && strlen($file < 255) &&
318
            if (is_string($file) && strlen($file < 255) &&
310
                  (!file_exists($file) || !@is_file($file))) {
319
                  (!file_exists($file) || !@is_file($file))) {
311
                $ret = PEAR::raiseError("could not open file \"$file\"");
320
                $ret = PEAR::raiseError("could not open file \"$file\"");
312
                return $ret;
321
                return $ret;
313
            }
322
            }
-
 
323
 
314
            $file = realpath($file);
324
            $file = realpath($file);
315
            $ret = PEAR::raiseError("Could not get contents of package \"$file\"".
325
            $ret = PEAR::raiseError("Could not get contents of package \"$file\"".
316
                                     '. Invalid tgz file.');
326
                                     '. Invalid tgz file.');
317
            return $ret;
327
            return $ret;
318
        } else {
-
 
319
            if (!count($content) && !@is_file($file)) {
-
 
320
                $ret = PEAR::raiseError("could not open file \"$file\"");
-
 
321
                return $ret;
-
 
322
            }
-
 
323
        }
328
        }
-
 
329
 
-
 
330
        if (!count($content) && !@is_file($file)) {
-
 
331
            $ret = PEAR::raiseError("could not open file \"$file\"");
-
 
332
            return $ret;
-
 
333
        }
-
 
334
 
324
        $xml = null;
335
        $xml      = null;
325
        $origfile = $file;
336
        $origfile = $file;
326
        foreach ($content as $file) {
337
        foreach ($content as $file) {
327
            $name = $file['filename'];
338
            $name = $file['filename'];
328
            if ($name == 'package2.xml') { // allow a .tgz to distribute both versions
339
            if ($name == 'package2.xml') { // allow a .tgz to distribute both versions
329
                $xml = $name;
340
                $xml = $name;
330
                break;
341
                break;
331
            }
342
            }
-
 
343
 
332
            if ($name == 'package.xml') {
344
            if ($name == 'package.xml') {
333
                $xml = $name;
345
                $xml = $name;
334
                break;
346
                break;
335
            } elseif (ereg('package.xml$', $name, $match)) {
347
            } elseif (preg_match('/package.xml$/', $name, $match)) {
336
                $xml = $name;
348
                $xml = $name;
337
                break;
349
                break;
338
            }
350
            }
339
        }
351
        }
-
 
352
 
340
        if ($this->_tmpdir) {
353
        $tmpdir = System::mktemp('-t "' . $this->_config->get('temp_dir') . '" -d pear');
341
            $tmpdir = $this->_tmpdir;
354
        if ($tmpdir === false) {
342
        } else {
-
 
343
            $tmpdir = System::mkTemp(array('-d', 'pear'));
355
            $ret = PEAR::raiseError("there was a problem with getting the configured temp directory");
344
            PEAR_PackageFile::addTempFile($tmpdir);
356
            return $ret;
345
        }
357
        }
-
 
358
 
-
 
359
        PEAR_PackageFile::addTempFile($tmpdir);
-
 
360
 
346
        $this->_extractErrors();
361
        $this->_extractErrors();
347
        PEAR::staticPushErrorHandling(PEAR_ERROR_CALLBACK, array($this, '_extractErrors'));
362
        PEAR::staticPushErrorHandling(PEAR_ERROR_CALLBACK, array($this, '_extractErrors'));
-
 
363
 
348
        if (!$xml || !$tar->extractList(array($xml), $tmpdir)) {
364
        if (!$xml || !$tar->extractList(array($xml), $tmpdir)) {
349
            $extra = implode("\n", $this->_extractErrors());
365
            $extra = implode("\n", $this->_extractErrors());
350
            if ($extra) {
366
            if ($extra) {
351
                $extra = ' ' . $extra;
367
                $extra = ' ' . $extra;
352
            }
368
            }
-
 
369
 
353
            PEAR::staticPopErrorHandling();
370
            PEAR::staticPopErrorHandling();
354
            $ret = PEAR::raiseError('could not extract the package.xml file from "' .
371
            $ret = PEAR::raiseError('could not extract the package.xml file from "' .
355
                $origfile . '"' . $extra);
372
                $origfile . '"' . $extra);
356
            return $ret;
373
            return $ret;
357
        }
374
        }
-
 
375
 
358
        PEAR::staticPopErrorHandling();
376
        PEAR::staticPopErrorHandling();
359
        $ret = &PEAR_PackageFile::fromPackageFile("$tmpdir/$xml", $state, $origfile);
377
        $ret = &PEAR_PackageFile::fromPackageFile("$tmpdir/$xml", $state, $origfile);
360
        return $ret;
378
        return $ret;
361
    }
379
    }
362
 
380
 
363
    /**
381
    /**
364
     * helper for extracting Archive_Tar errors
-
 
365
     * @var array
-
 
366
     * @access private
-
 
367
     */
-
 
368
    var $_extractErrors = array();
-
 
369
 
-
 
370
    /**
-
 
371
     * helper callback for extracting Archive_Tar errors
382
     * helper callback for extracting Archive_Tar errors
372
     *
383
     *
373
     * @param PEAR_Error|null $err
384
     * @param PEAR_Error|null $err
374
     * @return array
385
     * @return array
375
     * @access private
386
     * @access private
376
     */
387
     */
377
    function _extractErrors($err = null)
388
    function _extractErrors($err = null)
378
    {
389
    {
379
        static $errors = array();
390
        static $errors = array();
380
        if ($err === null) {
391
        if ($err === null) {
381
            $e = $errors;
392
            $e = $errors;
382
            $errors = array();
393
            $errors = array();
383
            return $e;
394
            return $e;
384
        }
395
        }
385
        $errors[] = $err->getMessage();
396
        $errors[] = $err->getMessage();
386
    }
397
    }
387
 
398
 
388
    /**
399
    /**
389
     * Create a PEAR_PackageFile_v* from a package.xml file.
400
     * Create a PEAR_PackageFile_v* from a package.xml file.
390
     *
401
     *
391
     * @access public
402
     * @access public
392
     * @param   string  $descfile  name of package xml file
403
     * @param   string  $descfile  name of package xml file
393
     * @param   int     $state package state (one of PEAR_VALIDATE_* constants)
404
     * @param   int     $state package state (one of PEAR_VALIDATE_* constants)
394
     * @param   string|false $archive name of the archive this package.xml came
405
     * @param   string|false $archive name of the archive this package.xml came
395
     *          from, if any
406
     *          from, if any
396
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
407
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
397
     * @uses    PEAR_PackageFile::fromXmlString to create the oject after the
408
     * @uses    PEAR_PackageFile::fromXmlString to create the oject after the
398
     *          XML is loaded from the package.xml file.
409
     *          XML is loaded from the package.xml file.
399
     */
410
     */
400
    function &fromPackageFile($descfile, $state, $archive = false)
411
    function &fromPackageFile($descfile, $state, $archive = false)
401
    {
412
    {
-
 
413
        $fp = false;
402
        if (is_string($descfile) && strlen($descfile) < 255 &&
414
        if (is_string($descfile) && strlen($descfile) < 255 &&
-
 
415
             (
403
             (!file_exists($descfile) || !is_file($descfile) || !is_readable($descfile) ||
416
              !file_exists($descfile) || !is_file($descfile) || !is_readable($descfile)
404
             (!$fp = @fopen($descfile, 'r')))) {
417
              || (!$fp = @fopen($descfile, 'r'))
-
 
418
             )
-
 
419
        ) {
405
            $a = PEAR::raiseError("Unable to open $descfile");
420
            $a = PEAR::raiseError("Unable to open $descfile");
406
            return $a;
421
            return $a;
407
        }
422
        }
408
 
423
 
409
        // read the whole thing so we only get one cdata callback
424
        // read the whole thing so we only get one cdata callback
410
        // for each block of cdata
425
        // for each block of cdata
411
        fclose($fp);
426
        fclose($fp);
412
        $data = file_get_contents($descfile);
427
        $data = file_get_contents($descfile);
413
        $ret = &PEAR_PackageFile::fromXmlString($data, $state, $descfile, $archive);
428
        $ret = &PEAR_PackageFile::fromXmlString($data, $state, $descfile, $archive);
414
        return $ret;
429
        return $ret;
415
    }
430
    }
416
 
-
 
417
 
431
 
418
    /**
432
    /**
419
     * Create a PEAR_PackageFile_v* from a .tgz archive or package.xml file.
433
     * Create a PEAR_PackageFile_v* from a .tgz archive or package.xml file.
420
     *
434
     *
421
     * This method is able to extract information about a package from a .tgz
435
     * This method is able to extract information about a package from a .tgz
422
     * archive or from a XML package definition file.
436
     * archive or from a XML package definition file.
423
     *
437
     *
424
     * @access public
438
     * @access public
425
     * @param   string  $info file name
439
     * @param   string  $info file name
426
     * @param   int     $state package state (one of PEAR_VALIDATE_* constants)
440
     * @param   int     $state package state (one of PEAR_VALIDATE_* constants)
427
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
441
     * @return  PEAR_PackageFile_v1|PEAR_PackageFile_v2
428
     * @uses    fromPackageFile() if the file appears to be XML
442
     * @uses    fromPackageFile() if the file appears to be XML
429
     * @uses    fromTgzFile() to load all non-XML files
443
     * @uses    fromTgzFile() to load all non-XML files
430
     */
444
     */
431
    function &fromAnyFile($info, $state)
445
    function &fromAnyFile($info, $state)
432
    {
446
    {
433
        if (is_dir($info)) {
447
        if (is_dir($info)) {
434
            $dir_name = realpath($info);
448
            $dir_name = realpath($info);
435
            if (file_exists($dir_name . '/package.xml')) {
449
            if (file_exists($dir_name . '/package.xml')) {
436
                $info = PEAR_PackageFile::fromPackageFile($dir_name .  '/package.xml', $state);
450
                $info = PEAR_PackageFile::fromPackageFile($dir_name .  '/package.xml', $state);
437
            } elseif (file_exists($dir_name .  '/package2.xml')) {
451
            } elseif (file_exists($dir_name .  '/package2.xml')) {
438
                $info = PEAR_PackageFile::fromPackageFile($dir_name .  '/package2.xml', $state);
452
                $info = PEAR_PackageFile::fromPackageFile($dir_name .  '/package2.xml', $state);
439
            } else {
453
            } else {
440
                $info = PEAR::raiseError("No package definition found in '$info' directory");
454
                $info = PEAR::raiseError("No package definition found in '$info' directory");
441
            }
455
            }
-
 
456
 
442
            return $info;
457
            return $info;
443
        }
458
        }
444
 
459
 
445
        $fp = false;
460
        $fp = false;
446
        if (is_string($info) && strlen($info) < 255 &&
461
        if (is_string($info) && strlen($info) < 255 &&
447
             (file_exists($info) || ($fp = @fopen($info, 'r')))) {
462
             (file_exists($info) || ($fp = @fopen($info, 'r')))
-
 
463
        ) {
-
 
464
 
448
            if ($fp) {
465
            if ($fp) {
449
                fclose($fp);
466
                fclose($fp);
450
            }
467
            }
-
 
468
 
451
            $tmp = substr($info, -4);
469
            $tmp = substr($info, -4);
452
            if ($tmp == '.xml') {
470
            if ($tmp == '.xml') {
453
                $info = &PEAR_PackageFile::fromPackageFile($info, $state);
471
                $info = &PEAR_PackageFile::fromPackageFile($info, $state);
454
            } elseif ($tmp == '.tar' || $tmp == '.tgz') {
472
            } elseif ($tmp == '.tar' || $tmp == '.tgz') {
455
                $info = &PEAR_PackageFile::fromTgzFile($info, $state);
473
                $info = &PEAR_PackageFile::fromTgzFile($info, $state);
456
            } else {
474
            } else {
457
                $fp = fopen($info, "r");
475
                $fp   = fopen($info, 'r');
458
                $test = fread($fp, 5);
476
                $test = fread($fp, 5);
459
                fclose($fp);
477
                fclose($fp);
460
                if ($test == "<?xml") {
478
                if ($test == '<?xml') {
461
                    $info = &PEAR_PackageFile::fromPackageFile($info, $state);
479
                    $info = &PEAR_PackageFile::fromPackageFile($info, $state);
462
                } else {
480
                } else {
463
                    $info = &PEAR_PackageFile::fromTgzFile($info, $state);
481
                    $info = &PEAR_PackageFile::fromTgzFile($info, $state);
464
                }
482
                }
465
            }
483
            }
466
        } else {
-
 
467
            $info = PEAR::raiseError("Cannot open '$info' for parsing");
-
 
-
 
484
 
468
            return $info;
485
            return $info;
469
        }
486
        }
-
 
487
 
-
 
488
        $info = PEAR::raiseError("Cannot open '$info' for parsing");
470
        return $info;
489
        return $info;
471
    }
490
    }
472
}
491
}
473
 
-
 
474
?>
-