/script/lib/PEAR/PackageFile/v2/Validator.php

https://bitbucket.org/renaatdemuynck/chamilo · PHP · 2299 lines · 2086 code · 106 blank · 107 comment · 426 complexity · e1668b824fdd19e1e4d3ee6ae57e069c MD5 · raw file

Large files are truncated click here to view the full file

  1. <?php
  2. /**
  3. * PEAR_PackageFile_v2, package.xml version 2.0, read/write version
  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: Validator.php 277885 2009-03-27 19:29:31Z dufuz $
  13. * @link http://pear.php.net/package/PEAR
  14. * @since File available since Release 1.4.0a8
  15. */
  16. /**
  17. * Private validation class used by PEAR_PackageFile_v2 - do not use directly, its
  18. * sole purpose is to split up the PEAR/PackageFile/v2.php file to make it smaller
  19. * @category pear
  20. * @package PEAR
  21. * @author Greg Beaver <cellog@php.net>
  22. * @copyright 1997-2009 The Authors
  23. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  24. * @version Release: 1.9.1
  25. * @link http://pear.php.net/package/PEAR
  26. * @since Class available since Release 1.4.0a8
  27. * @access private
  28. */
  29. class PEAR_PackageFile_v2_Validator
  30. {
  31. /**
  32. * @var array
  33. */
  34. var $_packageInfo;
  35. /**
  36. * @var PEAR_PackageFile_v2
  37. */
  38. var $_pf;
  39. /**
  40. * @var PEAR_ErrorStack
  41. */
  42. var $_stack;
  43. /**
  44. * @var int
  45. */
  46. var $_isValid = 0;
  47. /**
  48. * @var int
  49. */
  50. var $_filesValid = 0;
  51. /**
  52. * @var int
  53. */
  54. var $_curState = 0;
  55. /**
  56. * @param PEAR_PackageFile_v2
  57. * @param int
  58. */
  59. function validate(&$pf, $state = PEAR_VALIDATE_NORMAL)
  60. {
  61. $this->_pf = &$pf;
  62. $this->_curState = $state;
  63. $this->_packageInfo = $this->_pf->getArray();
  64. $this->_isValid = $this->_pf->_isValid;
  65. $this->_filesValid = $this->_pf->_filesValid;
  66. $this->_stack = &$pf->_stack;
  67. $this->_stack->getErrors(true);
  68. if (($this->_isValid & $state) == $state)
  69. {
  70. return true;
  71. }
  72. if (! isset($this->_packageInfo) || ! is_array($this->_packageInfo))
  73. {
  74. return false;
  75. }
  76. if (! isset($this->_packageInfo['attribs']['version']) || ($this->_packageInfo['attribs']['version'] != '2.0' && $this->_packageInfo['attribs']['version'] != '2.1'))
  77. {
  78. $this->_noPackageVersion();
  79. }
  80. $structure = array('name', 'channel|uri', '*extends', // can't be multiple, but this works fine
  81. 'summary', 'description', '+lead', // these all need content checks
  82. '*developer',
  83. '*contributor', '*helper', 'date', '*time', 'version', 'stability', 'license->?uri->?filesource',
  84. 'notes', 'contents', //special validation needed
  85. '*compatible', 'dependencies', //special validation needed
  86. '*usesrole', '*usestask', // reserve these for 1.4.0a1 to implement
  87. // this will allow a package.xml to gracefully say it
  88. // needs a certain package installed in order to implement a role or task
  89. '*providesextension',
  90. '*srcpackage|*srcuri',
  91. '+phprelease|+extsrcrelease|+extbinrelease|' . '+zendextsrcrelease|+zendextbinrelease|bundle', //special validation needed
  92. '*changelog');
  93. $test = $this->_packageInfo;
  94. if (isset($test['dependencies']) && isset($test['dependencies']['required']) && isset($test['dependencies']['required']['pearinstaller']) && isset($test['dependencies']['required']['pearinstaller']['min']) && version_compare('1.9.1', $test['dependencies']['required']['pearinstaller']['min'], '<'))
  95. {
  96. $this->_pearVersionTooLow($test['dependencies']['required']['pearinstaller']['min']);
  97. return false;
  98. }
  99. // ignore post-installation array fields
  100. if (array_key_exists('filelist', $test))
  101. {
  102. unset($test['filelist']);
  103. }
  104. if (array_key_exists('_lastmodified', $test))
  105. {
  106. unset($test['_lastmodified']);
  107. }
  108. if (array_key_exists('#binarypackage', $test))
  109. {
  110. unset($test['#binarypackage']);
  111. }
  112. if (array_key_exists('old', $test))
  113. {
  114. unset($test['old']);
  115. }
  116. if (array_key_exists('_lastversion', $test))
  117. {
  118. unset($test['_lastversion']);
  119. }
  120. if (! $this->_stupidSchemaValidate($structure, $test, '<package>'))
  121. {
  122. return false;
  123. }
  124. if (empty($this->_packageInfo['name']))
  125. {
  126. $this->_tagCannotBeEmpty('name');
  127. }
  128. $test = isset($this->_packageInfo['uri']) ? 'uri' : 'channel';
  129. if (empty($this->_packageInfo[$test]))
  130. {
  131. $this->_tagCannotBeEmpty($test);
  132. }
  133. if (is_array($this->_packageInfo['license']) && (! isset($this->_packageInfo['license']['_content']) || empty($this->_packageInfo['license']['_content'])))
  134. {
  135. $this->_tagCannotBeEmpty('license');
  136. }
  137. elseif (empty($this->_packageInfo['license']))
  138. {
  139. $this->_tagCannotBeEmpty('license');
  140. }
  141. if (empty($this->_packageInfo['summary']))
  142. {
  143. $this->_tagCannotBeEmpty('summary');
  144. }
  145. if (empty($this->_packageInfo['description']))
  146. {
  147. $this->_tagCannotBeEmpty('description');
  148. }
  149. if (empty($this->_packageInfo['date']))
  150. {
  151. $this->_tagCannotBeEmpty('date');
  152. }
  153. if (empty($this->_packageInfo['notes']))
  154. {
  155. $this->_tagCannotBeEmpty('notes');
  156. }
  157. if (isset($this->_packageInfo['time']) && empty($this->_packageInfo['time']))
  158. {
  159. $this->_tagCannotBeEmpty('time');
  160. }
  161. if (isset($this->_packageInfo['dependencies']))
  162. {
  163. $this->_validateDependencies();
  164. }
  165. if (isset($this->_packageInfo['compatible']))
  166. {
  167. $this->_validateCompatible();
  168. }
  169. if (! isset($this->_packageInfo['bundle']))
  170. {
  171. if (empty($this->_packageInfo['contents']))
  172. {
  173. $this->_tagCannotBeEmpty('contents');
  174. }
  175. if (! isset($this->_packageInfo['contents']['dir']))
  176. {
  177. $this->_filelistMustContainDir('contents');
  178. return false;
  179. }
  180. if (isset($this->_packageInfo['contents']['file']))
  181. {
  182. $this->_filelistCannotContainFile('contents');
  183. return false;
  184. }
  185. }
  186. $this->_validateMaintainers();
  187. $this->_validateStabilityVersion();
  188. $fail = false;
  189. if (array_key_exists('usesrole', $this->_packageInfo))
  190. {
  191. $roles = $this->_packageInfo['usesrole'];
  192. if (! is_array($roles) || ! isset($roles[0]))
  193. {
  194. $roles = array($roles);
  195. }
  196. foreach ($roles as $role)
  197. {
  198. if (! isset($role['role']))
  199. {
  200. $this->_usesroletaskMustHaveRoleTask('usesrole', 'role');
  201. $fail = true;
  202. }
  203. else
  204. {
  205. if (! isset($role['channel']))
  206. {
  207. if (! isset($role['uri']))
  208. {
  209. $this->_usesroletaskMustHaveChannelOrUri($role['role'], 'usesrole');
  210. $fail = true;
  211. }
  212. }
  213. elseif (! isset($role['package']))
  214. {
  215. $this->_usesroletaskMustHavePackage($role['role'], 'usesrole');
  216. $fail = true;
  217. }
  218. }
  219. }
  220. }
  221. if (array_key_exists('usestask', $this->_packageInfo))
  222. {
  223. $roles = $this->_packageInfo['usestask'];
  224. if (! is_array($roles) || ! isset($roles[0]))
  225. {
  226. $roles = array($roles);
  227. }
  228. foreach ($roles as $role)
  229. {
  230. if (! isset($role['task']))
  231. {
  232. $this->_usesroletaskMustHaveRoleTask('usestask', 'task');
  233. $fail = true;
  234. }
  235. else
  236. {
  237. if (! isset($role['channel']))
  238. {
  239. if (! isset($role['uri']))
  240. {
  241. $this->_usesroletaskMustHaveChannelOrUri($role['task'], 'usestask');
  242. $fail = true;
  243. }
  244. }
  245. elseif (! isset($role['package']))
  246. {
  247. $this->_usesroletaskMustHavePackage($role['task'], 'usestask');
  248. $fail = true;
  249. }
  250. }
  251. }
  252. }
  253. if ($fail)
  254. {
  255. return false;
  256. }
  257. $list = $this->_packageInfo['contents'];
  258. if (isset($list['dir']) && is_array($list['dir']) && isset($list['dir'][0]))
  259. {
  260. $this->_multipleToplevelDirNotAllowed();
  261. return $this->_isValid = 0;
  262. }
  263. $this->_validateFilelist();
  264. $this->_validateRelease();
  265. if (! $this->_stack->hasErrors())
  266. {
  267. $chan = $this->_pf->_registry->getChannel($this->_pf->getChannel(), true);
  268. if (PEAR :: isError($chan))
  269. {
  270. $this->_unknownChannel($this->_pf->getChannel());
  271. }
  272. else
  273. {
  274. $valpack = $chan->getValidationPackage();
  275. // for channel validator packages, always use the default PEAR validator.
  276. // otherwise, they can't be installed or packaged
  277. $validator = $chan->getValidationObject($this->_pf->getPackage());
  278. if (! $validator)
  279. {
  280. $this->_stack->push(__FUNCTION__, 'error', array_merge(array('channel' => $chan->getName(),
  281. 'package' => $this->_pf->getPackage()), $valpack), 'package "%channel%/%package%" cannot be properly validated without ' . 'validation package "%channel%/%name%-%version%"');
  282. return $this->_isValid = 0;
  283. }
  284. $validator->setPackageFile($this->_pf);
  285. $validator->validate($state);
  286. $failures = $validator->getFailures();
  287. foreach ($failures['errors'] as $error)
  288. {
  289. $this->_stack->push(__FUNCTION__, 'error', $error, 'Channel validator error: field "%field%" - %reason%');
  290. }
  291. foreach ($failures['warnings'] as $warning)
  292. {
  293. $this->_stack->push(__FUNCTION__, 'warning', $warning, 'Channel validator warning: field "%field%" - %reason%');
  294. }
  295. }
  296. }
  297. $this->_pf->_isValid = $this->_isValid = ! $this->_stack->hasErrors('error');
  298. if ($this->_isValid && $state == PEAR_VALIDATE_PACKAGING && ! $this->_filesValid)
  299. {
  300. if ($this->_pf->getPackageType() == 'bundle')
  301. {
  302. if ($this->_analyzeBundledPackages())
  303. {
  304. $this->_filesValid = $this->_pf->_filesValid = true;
  305. }
  306. else
  307. {
  308. $this->_pf->_isValid = $this->_isValid = 0;
  309. }
  310. }
  311. else
  312. {
  313. if (! $this->_analyzePhpFiles())
  314. {
  315. $this->_pf->_isValid = $this->_isValid = 0;
  316. }
  317. else
  318. {
  319. $this->_filesValid = $this->_pf->_filesValid = true;
  320. }
  321. }
  322. }
  323. if ($this->_isValid)
  324. {
  325. return $this->_pf->_isValid = $this->_isValid = $state;
  326. }
  327. return $this->_pf->_isValid = $this->_isValid = 0;
  328. }
  329. function _stupidSchemaValidate($structure, $xml, $root)
  330. {
  331. if (! is_array($xml))
  332. {
  333. $xml = array();
  334. }
  335. $keys = array_keys($xml);
  336. reset($keys);
  337. $key = current($keys);
  338. while ($key == 'attribs' || $key == '_contents')
  339. {
  340. $key = next($keys);
  341. }
  342. $unfoundtags = $optionaltags = array();
  343. $ret = true;
  344. $mismatch = false;
  345. foreach ($structure as $struc)
  346. {
  347. if ($key)
  348. {
  349. $tag = $xml[$key];
  350. }
  351. $test = $this->_processStructure($struc);
  352. if (isset($test['choices']))
  353. {
  354. $loose = true;
  355. foreach ($test['choices'] as $choice)
  356. {
  357. if ($key == $choice['tag'])
  358. {
  359. $key = next($keys);
  360. while ($key == 'attribs' || $key == '_contents')
  361. {
  362. $key = next($keys);
  363. }
  364. $unfoundtags = $optionaltags = array();
  365. $mismatch = false;
  366. if ($key && $key != $choice['tag'] && isset($choice['multiple']))
  367. {
  368. $unfoundtags[] = $choice['tag'];
  369. $optionaltags[] = $choice['tag'];
  370. if ($key)
  371. {
  372. $mismatch = true;
  373. }
  374. }
  375. $ret &= $this->_processAttribs($choice, $tag, $root);
  376. continue 2;
  377. }
  378. else
  379. {
  380. $unfoundtags[] = $choice['tag'];
  381. $mismatch = true;
  382. }
  383. if (! isset($choice['multiple']) || $choice['multiple'] != '*')
  384. {
  385. $loose = false;
  386. }
  387. else
  388. {
  389. $optionaltags[] = $choice['tag'];
  390. }
  391. }
  392. if (! $loose)
  393. {
  394. $this->_invalidTagOrder($unfoundtags, $key, $root);
  395. return false;
  396. }
  397. }
  398. else
  399. {
  400. if ($key != $test['tag'])
  401. {
  402. if (isset($test['multiple']) && $test['multiple'] != '*')
  403. {
  404. $unfoundtags[] = $test['tag'];
  405. $this->_invalidTagOrder($unfoundtags, $key, $root);
  406. return false;
  407. }
  408. else
  409. {
  410. if ($key)
  411. {
  412. $mismatch = true;
  413. }
  414. $unfoundtags[] = $test['tag'];
  415. $optionaltags[] = $test['tag'];
  416. }
  417. if (! isset($test['multiple']))
  418. {
  419. $this->_invalidTagOrder($unfoundtags, $key, $root);
  420. return false;
  421. }
  422. continue;
  423. }
  424. else
  425. {
  426. $unfoundtags = $optionaltags = array();
  427. $mismatch = false;
  428. }
  429. $key = next($keys);
  430. while ($key == 'attribs' || $key == '_contents')
  431. {
  432. $key = next($keys);
  433. }
  434. if ($key && $key != $test['tag'] && isset($test['multiple']))
  435. {
  436. $unfoundtags[] = $test['tag'];
  437. $optionaltags[] = $test['tag'];
  438. $mismatch = true;
  439. }
  440. $ret &= $this->_processAttribs($test, $tag, $root);
  441. continue;
  442. }
  443. }
  444. if (! $mismatch && count($optionaltags))
  445. {
  446. // don't error out on any optional tags
  447. $unfoundtags = array_diff($unfoundtags, $optionaltags);
  448. }
  449. if (count($unfoundtags))
  450. {
  451. $this->_invalidTagOrder($unfoundtags, $key, $root);
  452. }
  453. elseif ($key)
  454. {
  455. // unknown tags
  456. $this->_invalidTagOrder('*no tags allowed here*', $key, $root);
  457. while ($key = next($keys))
  458. {
  459. $this->_invalidTagOrder('*no tags allowed here*', $key, $root);
  460. }
  461. }
  462. return $ret;
  463. }
  464. function _processAttribs($choice, $tag, $context)
  465. {
  466. if (isset($choice['attribs']))
  467. {
  468. if (! is_array($tag))
  469. {
  470. $tag = array($tag);
  471. }
  472. $tags = $tag;
  473. if (! isset($tags[0]))
  474. {
  475. $tags = array($tags);
  476. }
  477. $ret = true;
  478. foreach ($tags as $i => $tag)
  479. {
  480. if (! is_array($tag) || ! isset($tag['attribs']))
  481. {
  482. foreach ($choice['attribs'] as $attrib)
  483. {
  484. if ($attrib{0} != '?')
  485. {
  486. $ret &= $this->_tagHasNoAttribs($choice['tag'], $context);
  487. continue 2;
  488. }
  489. }
  490. }
  491. foreach ($choice['attribs'] as $attrib)
  492. {
  493. if ($attrib{0} != '?')
  494. {
  495. if (! isset($tag['attribs'][$attrib]))
  496. {
  497. $ret &= $this->_tagMissingAttribute($choice['tag'], $attrib, $context);
  498. }
  499. }
  500. }
  501. }
  502. return $ret;
  503. }
  504. return true;
  505. }
  506. function _processStructure($key)
  507. {
  508. $ret = array();
  509. if (count($pieces = explode('|', $key)) > 1)
  510. {
  511. $ret['choices'] = array();
  512. foreach ($pieces as $piece)
  513. {
  514. $ret['choices'][] = $this->_processStructure($piece);
  515. }
  516. return $ret;
  517. }
  518. $multi = $key{0};
  519. if ($multi == '+' || $multi == '*')
  520. {
  521. $ret['multiple'] = $key{0};
  522. $key = substr($key, 1);
  523. }
  524. if (count($attrs = explode('->', $key)) > 1)
  525. {
  526. $ret['tag'] = array_shift($attrs);
  527. $ret['attribs'] = $attrs;
  528. }
  529. else
  530. {
  531. $ret['tag'] = $key;
  532. }
  533. return $ret;
  534. }
  535. function _validateStabilityVersion()
  536. {
  537. $structure = array('release', 'api');
  538. $a = $this->_stupidSchemaValidate($structure, $this->_packageInfo['version'], '<version>');
  539. $a &= $this->_stupidSchemaValidate($structure, $this->_packageInfo['stability'], '<stability>');
  540. if ($a)
  541. {
  542. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $this->_packageInfo['version']['release']))
  543. {
  544. $this->_invalidVersion('release', $this->_packageInfo['version']['release']);
  545. }
  546. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $this->_packageInfo['version']['api']))
  547. {
  548. $this->_invalidVersion('api', $this->_packageInfo['version']['api']);
  549. }
  550. if (! in_array($this->_packageInfo['stability']['release'], array('snapshot', 'devel', 'alpha', 'beta',
  551. 'stable')))
  552. {
  553. $this->_invalidState('release', $this->_packageInfo['stability']['release']);
  554. }
  555. if (! in_array($this->_packageInfo['stability']['api'], array('devel', 'alpha', 'beta', 'stable')))
  556. {
  557. $this->_invalidState('api', $this->_packageInfo['stability']['api']);
  558. }
  559. }
  560. }
  561. function _validateMaintainers()
  562. {
  563. $structure = array('name', 'user', 'email', 'active');
  564. foreach (array('lead', 'developer', 'contributor', 'helper') as $type)
  565. {
  566. if (! isset($this->_packageInfo[$type]))
  567. {
  568. continue;
  569. }
  570. if (isset($this->_packageInfo[$type][0]))
  571. {
  572. foreach ($this->_packageInfo[$type] as $lead)
  573. {
  574. $this->_stupidSchemaValidate($structure, $lead, '<' . $type . '>');
  575. }
  576. }
  577. else
  578. {
  579. $this->_stupidSchemaValidate($structure, $this->_packageInfo[$type], '<' . $type . '>');
  580. }
  581. }
  582. }
  583. function _validatePhpDep($dep, $installcondition = false)
  584. {
  585. $structure = array('min', '*max', '*exclude');
  586. $type = $installcondition ? '<installcondition><php>' : '<dependencies><required><php>';
  587. $this->_stupidSchemaValidate($structure, $dep, $type);
  588. if (isset($dep['min']))
  589. {
  590. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?(?:-[a-zA-Z0-9]+)?\\z/', $dep['min']))
  591. {
  592. $this->_invalidVersion($type . '<min>', $dep['min']);
  593. }
  594. }
  595. if (isset($dep['max']))
  596. {
  597. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?(?:-[a-zA-Z0-9]+)?\\z/', $dep['max']))
  598. {
  599. $this->_invalidVersion($type . '<max>', $dep['max']);
  600. }
  601. }
  602. if (isset($dep['exclude']))
  603. {
  604. if (! is_array($dep['exclude']))
  605. {
  606. $dep['exclude'] = array($dep['exclude']);
  607. }
  608. foreach ($dep['exclude'] as $exclude)
  609. {
  610. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?(?:-[a-zA-Z0-9]+)?\\z/', $exclude))
  611. {
  612. $this->_invalidVersion($type . '<exclude>', $exclude);
  613. }
  614. }
  615. }
  616. }
  617. function _validatePearinstallerDep($dep)
  618. {
  619. $structure = array('min', '*max', '*recommended', '*exclude');
  620. $this->_stupidSchemaValidate($structure, $dep, '<dependencies><required><pearinstaller>');
  621. if (isset($dep['min']))
  622. {
  623. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['min']))
  624. {
  625. $this->_invalidVersion('<dependencies><required><pearinstaller><min>', $dep['min']);
  626. }
  627. }
  628. if (isset($dep['max']))
  629. {
  630. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['max']))
  631. {
  632. $this->_invalidVersion('<dependencies><required><pearinstaller><max>', $dep['max']);
  633. }
  634. }
  635. if (isset($dep['recommended']))
  636. {
  637. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['recommended']))
  638. {
  639. $this->_invalidVersion('<dependencies><required><pearinstaller><recommended>', $dep['recommended']);
  640. }
  641. }
  642. if (isset($dep['exclude']))
  643. {
  644. if (! is_array($dep['exclude']))
  645. {
  646. $dep['exclude'] = array($dep['exclude']);
  647. }
  648. foreach ($dep['exclude'] as $exclude)
  649. {
  650. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $exclude))
  651. {
  652. $this->_invalidVersion('<dependencies><required><pearinstaller><exclude>', $exclude);
  653. }
  654. }
  655. }
  656. }
  657. function _validatePackageDep($dep, $group, $type = '<package>')
  658. {
  659. if (isset($dep['uri']))
  660. {
  661. if (isset($dep['conflicts']))
  662. {
  663. $structure = array('name', 'uri', 'conflicts', '*providesextension');
  664. }
  665. else
  666. {
  667. $structure = array('name', 'uri', '*providesextension');
  668. }
  669. }
  670. else
  671. {
  672. if (isset($dep['conflicts']))
  673. {
  674. $structure = array('name', 'channel', '*min', '*max', '*exclude', 'conflicts', '*providesextension');
  675. }
  676. else
  677. {
  678. $structure = array('name', 'channel', '*min', '*max', '*recommended', '*exclude', '*nodefault',
  679. '*providesextension');
  680. }
  681. }
  682. if (isset($dep['name']))
  683. {
  684. $type .= '<name>' . $dep['name'] . '</name>';
  685. }
  686. $this->_stupidSchemaValidate($structure, $dep, '<dependencies>' . $group . $type);
  687. if (isset($dep['uri']) && (isset($dep['min']) || isset($dep['max']) || isset($dep['recommended']) || isset($dep['exclude'])))
  688. {
  689. $this->_uriDepsCannotHaveVersioning('<dependencies>' . $group . $type);
  690. }
  691. if (isset($dep['channel']) && strtolower($dep['channel']) == '__uri')
  692. {
  693. $this->_DepchannelCannotBeUri('<dependencies>' . $group . $type);
  694. }
  695. if (isset($dep['min']))
  696. {
  697. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['min']))
  698. {
  699. $this->_invalidVersion('<dependencies>' . $group . $type . '<min>', $dep['min']);
  700. }
  701. }
  702. if (isset($dep['max']))
  703. {
  704. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['max']))
  705. {
  706. $this->_invalidVersion('<dependencies>' . $group . $type . '<max>', $dep['max']);
  707. }
  708. }
  709. if (isset($dep['recommended']))
  710. {
  711. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['recommended']))
  712. {
  713. $this->_invalidVersion('<dependencies>' . $group . $type . '<recommended>', $dep['recommended']);
  714. }
  715. }
  716. if (isset($dep['exclude']))
  717. {
  718. if (! is_array($dep['exclude']))
  719. {
  720. $dep['exclude'] = array($dep['exclude']);
  721. }
  722. foreach ($dep['exclude'] as $exclude)
  723. {
  724. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $exclude))
  725. {
  726. $this->_invalidVersion('<dependencies>' . $group . $type . '<exclude>', $exclude);
  727. }
  728. }
  729. }
  730. }
  731. function _validateSubpackageDep($dep, $group)
  732. {
  733. $this->_validatePackageDep($dep, $group, '<subpackage>');
  734. if (isset($dep['providesextension']))
  735. {
  736. $this->_subpackageCannotProvideExtension(isset($dep['name']) ? $dep['name'] : '');
  737. }
  738. if (isset($dep['conflicts']))
  739. {
  740. $this->_subpackagesCannotConflict(isset($dep['name']) ? $dep['name'] : '');
  741. }
  742. }
  743. function _validateExtensionDep($dep, $group = false, $installcondition = false)
  744. {
  745. if (isset($dep['conflicts']))
  746. {
  747. $structure = array('name', '*min', '*max', '*exclude', 'conflicts');
  748. }
  749. else
  750. {
  751. $structure = array('name', '*min', '*max', '*recommended', '*exclude');
  752. }
  753. if ($installcondition)
  754. {
  755. $type = '<installcondition><extension>';
  756. }
  757. else
  758. {
  759. $type = '<dependencies>' . $group . '<extension>';
  760. }
  761. if (isset($dep['name']))
  762. {
  763. $type .= '<name>' . $dep['name'] . '</name>';
  764. }
  765. $this->_stupidSchemaValidate($structure, $dep, $type);
  766. if (isset($dep['min']))
  767. {
  768. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['min']))
  769. {
  770. $this->_invalidVersion(substr($type, 1) . '<min', $dep['min']);
  771. }
  772. }
  773. if (isset($dep['max']))
  774. {
  775. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['max']))
  776. {
  777. $this->_invalidVersion(substr($type, 1) . '<max', $dep['max']);
  778. }
  779. }
  780. if (isset($dep['recommended']))
  781. {
  782. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $dep['recommended']))
  783. {
  784. $this->_invalidVersion(substr($type, 1) . '<recommended', $dep['recommended']);
  785. }
  786. }
  787. if (isset($dep['exclude']))
  788. {
  789. if (! is_array($dep['exclude']))
  790. {
  791. $dep['exclude'] = array($dep['exclude']);
  792. }
  793. foreach ($dep['exclude'] as $exclude)
  794. {
  795. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $exclude))
  796. {
  797. $this->_invalidVersion(substr($type, 1) . '<exclude', $exclude);
  798. }
  799. }
  800. }
  801. }
  802. function _validateOsDep($dep, $installcondition = false)
  803. {
  804. $structure = array('name', '*conflicts');
  805. $type = $installcondition ? '<installcondition><os>' : '<dependencies><required><os>';
  806. if ($this->_stupidSchemaValidate($structure, $dep, $type))
  807. {
  808. if ($dep['name'] == '*')
  809. {
  810. if (array_key_exists('conflicts', $dep))
  811. {
  812. $this->_cannotConflictWithAllOs($type);
  813. }
  814. }
  815. }
  816. }
  817. function _validateArchDep($dep, $installcondition = false)
  818. {
  819. $structure = array('pattern', '*conflicts');
  820. $type = $installcondition ? '<installcondition><arch>' : '<dependencies><required><arch>';
  821. $this->_stupidSchemaValidate($structure, $dep, $type);
  822. }
  823. function _validateInstallConditions($cond, $release)
  824. {
  825. $structure = array('*php', '*extension', '*os', '*arch');
  826. if (! $this->_stupidSchemaValidate($structure, $cond, $release))
  827. {
  828. return false;
  829. }
  830. foreach (array('php', 'extension', 'os', 'arch') as $type)
  831. {
  832. if (isset($cond[$type]))
  833. {
  834. $iter = $cond[$type];
  835. if (! is_array($iter) || ! isset($iter[0]))
  836. {
  837. $iter = array($iter);
  838. }
  839. foreach ($iter as $package)
  840. {
  841. if ($type == 'extension')
  842. {
  843. $this->{"_validate{$type}Dep"}($package, false, true);
  844. }
  845. else
  846. {
  847. $this->{"_validate{$type}Dep"}($package, true);
  848. }
  849. }
  850. }
  851. }
  852. }
  853. function _validateDependencies()
  854. {
  855. $structure = array('required', '*optional', '*group->name->hint');
  856. if (! $this->_stupidSchemaValidate($structure, $this->_packageInfo['dependencies'], '<dependencies>'))
  857. {
  858. return false;
  859. }
  860. foreach (array('required', 'optional') as $simpledep)
  861. {
  862. if (isset($this->_packageInfo['dependencies'][$simpledep]))
  863. {
  864. if ($simpledep == 'optional')
  865. {
  866. $structure = array('*package', '*subpackage', '*extension');
  867. }
  868. else
  869. {
  870. $structure = array('php', 'pearinstaller', '*package', '*subpackage', '*extension', '*os', '*arch');
  871. }
  872. if ($this->_stupidSchemaValidate($structure, $this->_packageInfo['dependencies'][$simpledep], "<dependencies><$simpledep>"))
  873. {
  874. foreach (array('package', 'subpackage', 'extension') as $type)
  875. {
  876. if (isset($this->_packageInfo['dependencies'][$simpledep][$type]))
  877. {
  878. $iter = $this->_packageInfo['dependencies'][$simpledep][$type];
  879. if (! isset($iter[0]))
  880. {
  881. $iter = array($iter);
  882. }
  883. foreach ($iter as $package)
  884. {
  885. if ($type != 'extension')
  886. {
  887. if (isset($package['uri']))
  888. {
  889. if (isset($package['channel']))
  890. {
  891. $this->_UrlOrChannel($type, $package['name']);
  892. }
  893. }
  894. else
  895. {
  896. if (! isset($package['channel']))
  897. {
  898. $this->_NoChannel($type, $package['name']);
  899. }
  900. }
  901. }
  902. $this->{"_validate{$type}Dep"}($package, "<$simpledep>");
  903. }
  904. }
  905. }
  906. if ($simpledep == 'optional')
  907. {
  908. continue;
  909. }
  910. foreach (array('php', 'pearinstaller', 'os', 'arch') as $type)
  911. {
  912. if (isset($this->_packageInfo['dependencies'][$simpledep][$type]))
  913. {
  914. $iter = $this->_packageInfo['dependencies'][$simpledep][$type];
  915. if (! isset($iter[0]))
  916. {
  917. $iter = array($iter);
  918. }
  919. foreach ($iter as $package)
  920. {
  921. $this->{"_validate{$type}Dep"}($package);
  922. }
  923. }
  924. }
  925. }
  926. }
  927. }
  928. if (isset($this->_packageInfo['dependencies']['group']))
  929. {
  930. $groups = $this->_packageInfo['dependencies']['group'];
  931. if (! isset($groups[0]))
  932. {
  933. $groups = array($groups);
  934. }
  935. $structure = array('*package', '*subpackage', '*extension');
  936. foreach ($groups as $group)
  937. {
  938. if ($this->_stupidSchemaValidate($structure, $group, '<group>'))
  939. {
  940. if (! PEAR_Validate :: validGroupName($group['attribs']['name']))
  941. {
  942. $this->_invalidDepGroupName($group['attribs']['name']);
  943. }
  944. foreach (array('package', 'subpackage', 'extension') as $type)
  945. {
  946. if (isset($group[$type]))
  947. {
  948. $iter = $group[$type];
  949. if (! isset($iter[0]))
  950. {
  951. $iter = array($iter);
  952. }
  953. foreach ($iter as $package)
  954. {
  955. if ($type != 'extension')
  956. {
  957. if (isset($package['uri']))
  958. {
  959. if (isset($package['channel']))
  960. {
  961. $this->_UrlOrChannelGroup($type, $package['name'], $group['name']);
  962. }
  963. }
  964. else
  965. {
  966. if (! isset($package['channel']))
  967. {
  968. $this->_NoChannelGroup($type, $package['name'], $group['name']);
  969. }
  970. }
  971. }
  972. $this->{"_validate{$type}Dep"}($package, '<group name="' . $group['attribs']['name'] . '">');
  973. }
  974. }
  975. }
  976. }
  977. }
  978. }
  979. }
  980. function _validateCompatible()
  981. {
  982. $compat = $this->_packageInfo['compatible'];
  983. if (! isset($compat[0]))
  984. {
  985. $compat = array($compat);
  986. }
  987. $required = array('name', 'channel', 'min', 'max', '*exclude');
  988. foreach ($compat as $package)
  989. {
  990. $type = '<compatible>';
  991. if (is_array($package) && array_key_exists('name', $package))
  992. {
  993. $type .= '<name>' . $package['name'] . '</name>';
  994. }
  995. $this->_stupidSchemaValidate($required, $package, $type);
  996. if (is_array($package) && array_key_exists('min', $package))
  997. {
  998. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $package['min']))
  999. {
  1000. $this->_invalidVersion(substr($type, 1) . '<min', $package['min']);
  1001. }
  1002. }
  1003. if (is_array($package) && array_key_exists('max', $package))
  1004. {
  1005. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $package['max']))
  1006. {
  1007. $this->_invalidVersion(substr($type, 1) . '<max', $package['max']);
  1008. }
  1009. }
  1010. if (is_array($package) && array_key_exists('exclude', $package))
  1011. {
  1012. if (! is_array($package['exclude']))
  1013. {
  1014. $package['exclude'] = array($package['exclude']);
  1015. }
  1016. foreach ($package['exclude'] as $exclude)
  1017. {
  1018. if (! preg_match('/^\d+(?:\.\d+)*(?:[a-zA-Z]+\d*)?\\z/', $exclude))
  1019. {
  1020. $this->_invalidVersion(substr($type, 1) . '<exclude', $exclude);
  1021. }
  1022. }
  1023. }
  1024. }
  1025. }
  1026. function _validateBundle($list)
  1027. {
  1028. if (! is_array($list) || ! isset($list['bundledpackage']))
  1029. {
  1030. return $this->_NoBundledPackages();
  1031. }
  1032. if (! is_array($list['bundledpackage']) || ! isset($list['bundledpackage'][0]))
  1033. {
  1034. return $this->_AtLeast2BundledPackages();
  1035. }
  1036. foreach ($list['bundledpackage'] as $package)
  1037. {
  1038. if (! is_string($package))
  1039. {
  1040. $this->_bundledPackagesMustBeFilename();
  1041. }
  1042. }
  1043. }
  1044. function _validateFilelist($list = false, $allowignore = false, $dirs = '')
  1045. {
  1046. $iscontents = false;
  1047. if (! $list)
  1048. {
  1049. $iscontents = true;
  1050. $list = $this->_packageInfo['contents'];
  1051. if (isset($this->_packageInfo['bundle']))
  1052. {
  1053. return $this->_validateBundle($list);
  1054. }
  1055. }
  1056. if ($allowignore)
  1057. {
  1058. $struc = array('*install->name->as', '*ignore->name');
  1059. }
  1060. else
  1061. {
  1062. $struc = array('*dir->name->?baseinstalldir', '*file->name->role->?baseinstalldir->?md5sum');
  1063. if (isset($list['dir']) && isset($list['file']))
  1064. {
  1065. // stave off validation errors without requiring a set order.
  1066. $_old = $list;
  1067. if (isset($list['attribs']))
  1068. {
  1069. $list = array('attribs' => $_old['attribs']);
  1070. }
  1071. $list['dir'] = $_old['dir'];
  1072. $list['file'] = $_old['file'];
  1073. }
  1074. }
  1075. if (! isset($list['attribs']) || ! isset($list['attribs']['name']))
  1076. {
  1077. $unknown = $allowignore ? '<filelist>' : '<dir name="*unknown*">';
  1078. $dirname = $iscontents ? '<contents>' : $unknown;
  1079. }
  1080. else
  1081. {
  1082. $dirname = '<dir name="' . $list['attribs']['name'] . '">';
  1083. if (preg_match('~/\.\.?(/|\\z)|^\.\.?/~', str_replace('\\', '/', $list['attribs']['name'])))
  1084. {
  1085. // file contains .. parent directory or . cur directory
  1086. $this->_invalidDirName($list['attribs']['name']);
  1087. }
  1088. }
  1089. $res = $this->_stupidSchemaValidate($struc, $list, $dirname);
  1090. if ($allowignore && $res)
  1091. {
  1092. $ignored_or_installed = array();
  1093. $this->_pf->getFilelist();
  1094. $fcontents = $this->_pf->getContents();
  1095. $filelist = array();
  1096. if (! isset($fcontents['dir']['file'][0]))
  1097. {
  1098. $fcontents['dir']['file'] = array($fcontents['dir']['file']);
  1099. }
  1100. foreach ($fcontents['dir']['file'] as $file)
  1101. {
  1102. $filelist[$file['attribs']['name']] = true;
  1103. }
  1104. if (isset($list['install']))
  1105. {
  1106. if (! isset($list['install'][0]))
  1107. {
  1108. $list['install'] = array($list['install']);
  1109. }
  1110. foreach ($list['install'] as $file)
  1111. {
  1112. if (! isset($filelist[$file['attribs']['name']]))
  1113. {
  1114. $this->_notInContents($file['attribs']['name'], 'install');
  1115. continue;
  1116. }
  1117. if (array_key_exists($file['attribs']['name'], $ignored_or_installed))
  1118. {
  1119. $this->_multipleInstallAs($file['attribs']['name']);
  1120. }
  1121. if (! isset($ignored_or_installed[$file['attribs']['name']]))
  1122. {
  1123. $ignored_or_installed[$file['attribs']['name']] = array();
  1124. }
  1125. $ignored_or_installed[$file['attribs']['name']][] = 1;
  1126. if (preg_match('~/\.\.?(/|\\z)|^\.\.?/~', str_replace('\\', '/', $file['attribs']['as'])))
  1127. {
  1128. // file contains .. parent directory or . cur directory references
  1129. $this->_invalidFileInstallAs($file['attribs']['name'], $file['attribs']['as']);
  1130. }
  1131. }
  1132. }
  1133. if (isset($list['ignore']))
  1134. {
  1135. if (! isset($list['ignore'][0]))
  1136. {
  1137. $list['ignore'] = array($list['ignore']);
  1138. }
  1139. foreach ($list['ignore'] as $file)
  1140. {
  1141. if (! isset($filelist[$file['attribs']['name']]))
  1142. {
  1143. $this->_notInContents($file['attribs']['name'], 'ignore');
  1144. continue;
  1145. }
  1146. if (array_key_exists($file['attribs']['name'], $ignored_or_installed))
  1147. {
  1148. $this->_ignoreAndInstallAs($file['attribs']['name']);
  1149. }
  1150. }
  1151. }
  1152. }
  1153. if (! $allowignore && isset($list['file']))
  1154. {
  1155. if (is_string($list['file']))
  1156. {
  1157. $this->_oldStyleFileNotAllowed();
  1158. return false;
  1159. }
  1160. if (! isset($list['file'][0]))
  1161. {
  1162. // single file
  1163. $list['file'] = array($list['file']);
  1164. }
  1165. foreach ($list['file'] as $i => $file)
  1166. {
  1167. if (isset($file['attribs']) && isset($file['attribs']['name']))
  1168. {
  1169. if ($file['attribs']['name']{0} == '.' && $file['attribs']['name']{1} == '/')
  1170. {
  1171. // name is something like "./doc/whatever.txt"
  1172. $this->_invalidFileName($file['attribs']['name'], $dirname);
  1173. }
  1174. if (preg_match('~/\.\.?(/|\\z)|^\.\.?/~', str_replace('\\', '/', $file['attribs']['name'])))
  1175. {
  1176. // file contains .. parent directory or . cur directory
  1177. $this->_invalidFileName($file['attribs']['name'], $dirname);
  1178. }
  1179. }
  1180. if (isset($file['attribs']) && isset($file['attribs']['role']))
  1181. {
  1182. if (! $this->_validateRole($file['attribs']['role']))
  1183. {
  1184. if (isset($this->_packageInfo['usesrole']))
  1185. {
  1186. $roles = $this->_packageInfo['usesrole'];
  1187. if (! isset($roles[0]))
  1188. {
  1189. $roles = array($roles);
  1190. }
  1191. foreach ($roles as $role)
  1192. {
  1193. if ($role['role'] = $file['attribs']['role'])
  1194. {
  1195. $msg = 'This package contains role "%role%" and requires ' . 'package "%package%" to be used';
  1196. if (isset($role['uri']))
  1197. {
  1198. $params = array('role' => $role['role'], 'package' => $role['uri']);
  1199. }
  1200. else
  1201. {
  1202. $params = array('role' => $role['role'],
  1203. 'package' => $this->_pf->_registry->parsedPackageNameToString(array(
  1204. 'package' => $role['package'], 'channel' => $role['channel']), true));
  1205. }
  1206. $this->_stack->push('_mustInstallRole', 'error', $params, $msg);
  1207. }
  1208. }
  1209. }
  1210. $this->_invalidFileRole($file['attribs']['name'], $dirname, $file['attribs']['role']);
  1211. }
  1212. }
  1213. if (! isset($file['attribs']))
  1214. {
  1215. continue;
  1216. }
  1217. $save = $file['attribs'];
  1218. if ($dirs)
  1219. {
  1220. $save['name'] = $dirs . '/' . $save['name'];
  1221. }
  1222. unset($file['attribs']);
  1223. if (count($file) && $this->_curState != PEAR_VALIDATE_DOWNLOADING)
  1224. { // has tasks
  1225. foreach ($file as $task => $value)
  1226. {
  1227. if ($tagClass = $this->_pf->getTask($task))
  1228. {
  1229. if (! is_array($value) || ! isset($value[0]))
  1230. {
  1231. $value = array($value);
  1232. }
  1233. foreach ($value as $v)
  1234. {
  1235. $ret = call_user_func(array($tagClass, 'validateXml'), $this->_pf, $v, $this->_pf->_config, $save);
  1236. if (is_array($ret))
  1237. {
  1238. $this->_invalidTask($task, $ret, isset($save['name']) ? $save['name'] : '');
  1239. }
  1240. }
  1241. }
  1242. else
  1243. {
  1244. if (isset($this->_packageInfo['usestask']))
  1245. {
  1246. $roles = $this->_packageInfo['usestask'];
  1247. if (! isset($roles[0]))
  1248. {
  1249. $roles = array($roles);
  1250. }
  1251. foreach ($roles as $role)
  1252. {
  1253. if ($role['task'] = $task)
  1254. {
  1255. $msg = 'This package contains task "%task%" and requires ' . 'package "%package%" to be used';
  1256. if (isset($role['uri']))
  1257. {
  1258. $params = array('task' => $role['task'], 'package' => $role['uri']);
  1259. }
  1260. else
  1261. {
  1262. $params = array('task' => $role['task'],
  1263. 'package' => $this->_pf->_registry->parsedPackageNameToString(array(