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