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: Validate.php 313023 2011-07-06 19:17:11Z dufuz $
13 * @link http://pear.php.net/package/PEAR
14 * @since File available since Release 1.4.0a1
17 * Constants for install stage
19 define('PEAR_VALIDATE_INSTALLING', 1);
20 define('PEAR_VALIDATE_UNINSTALLING', 2); // this is not bit-mapped like the others
21 define('PEAR_VALIDATE_NORMAL', 3);
22 define('PEAR_VALIDATE_DOWNLOADING', 4); // this is not bit-mapped like the others
23 define('PEAR_VALIDATE_PACKAGING', 7);
25 require_once 'PEAR/Common.php';
26 require_once 'PEAR/Validator/PECL.php';
29 * Validation class for package.xml - channel-level advanced validation
32 * @author Greg Beaver <cellog@php.net>
33 * @copyright 1997-2009 The Authors
34 * @license http://opensource.org/licenses/bsd-license.php New BSD License
35 * @version Release: 1.9.4
36 * @link http://pear.php.net/package/PEAR
37 * @since Class available since Release 1.4.0a1
41 var $packageregex = _PEAR_COMMON_PACKAGE_NAME_PREG;
43 * @var PEAR_PackageFile_v1|PEAR_PackageFile_v2
47 * @var int one of the PEAR_VALIDATE_* constants
49 var $_state = PEAR_VALIDATE_NORMAL;
51 * Format: ('error' => array('field' => name, 'reason' => reason), 'warning' => same)
55 var $_failures = array('error' => array(), 'warning' => array());
58 * Override this method to handle validation of normal package names
63 function _validPackageName($name)
65 return (bool) preg_match('/^' . $this->packageregex . '\\z/', $name);
69 * @param string package name to validate
70 * @param string name of channel-specific validation package
73 function validPackageName($name, $validatepackagename = false)
75 if ($validatepackagename) {
76 if (strtolower($name) == strtolower($validatepackagename)) {
77 return (bool) preg_match('/^[a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+)*\\z/', $name);
80 return $this->_validPackageName($name);
84 * This validates a bundle name, and bundle names must conform
85 * to the PEAR naming convention, so the method is final and static.
90 function validGroupName($name)
92 return (bool) preg_match('/^' . _PEAR_COMMON_PACKAGE_NAME_PREG . '\\z/', $name);
96 * Determine whether $state represents a valid stability level
102 function validState($state)
104 return in_array($state, array('snapshot', 'devel', 'alpha', 'beta', 'stable'));
108 * Get a list of valid stability levels
113 function getValidStates()
115 return array('snapshot', 'devel', 'alpha', 'beta', 'stable');
119 * Determine whether a version is a properly formatted version number that can be used
126 function validVersion($ver)
128 return (bool) preg_match(PEAR_COMMON_PACKAGE_VERSION_PREG, $ver);
132 * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
134 function setPackageFile(&$pf)
136 $this->_packagexml = &$pf;
142 function _addFailure($field, $reason)
144 $this->_failures['errors'][] = array('field' => $field, 'reason' => $reason);
150 function _addWarning($field, $reason)
152 $this->_failures['warnings'][] = array('field' => $field, 'reason' => $reason);
155 function getFailures()
157 $failures = $this->_failures;
158 $this->_failures = array('warnings' => array(), 'errors' => array());
163 * @param int one of the PEAR_VALIDATE_* constants
165 function validate($state = null)
167 if (!isset($this->_packagexml)) {
170 if ($state !== null) {
171 $this->_state = $state;
173 $this->_failures = array('warnings' => array(), 'errors' => array());
174 $this->validatePackageName();
175 $this->validateVersion();
176 $this->validateMaintainers();
177 $this->validateDate();
178 $this->validateSummary();
179 $this->validateDescription();
180 $this->validateLicense();
181 $this->validateNotes();
182 if ($this->_packagexml->getPackagexmlVersion() == '1.0') {
183 $this->validateState();
184 $this->validateFilelist();
185 } elseif ($this->_packagexml->getPackagexmlVersion() == '2.0' ||
186 $this->_packagexml->getPackagexmlVersion() == '2.1') {
187 $this->validateTime();
188 $this->validateStability();
189 $this->validateDeps();
190 $this->validateMainFilelist();
191 $this->validateReleaseFilelist();
192 //$this->validateGlobalTasks();
193 $this->validateChangelog();
195 return !((bool) count($this->_failures['errors']));
201 function validatePackageName()
203 if ($this->_state == PEAR_VALIDATE_PACKAGING ||
204 $this->_state == PEAR_VALIDATE_NORMAL) {
205 if (($this->_packagexml->getPackagexmlVersion() == '2.0' ||
206 $this->_packagexml->getPackagexmlVersion() == '2.1') &&
207 $this->_packagexml->getExtends()) {
208 $version = $this->_packagexml->getVersion() . '';
209 $name = $this->_packagexml->getPackage();
210 $test = array_shift($a = explode('.', $version));
214 $vlen = strlen($test);
215 $majver = substr($name, strlen($name) - $vlen);
216 while ($majver && !is_numeric($majver{0})) {
217 $majver = substr($majver, 1);
219 if ($majver != $test) {
220 $this->_addWarning('package', "package $name extends package " .
221 $this->_packagexml->getExtends() . ' and so the name should ' .
222 'have a postfix equal to the major version like "' .
223 $this->_packagexml->getExtends() . $test . '"');
225 } elseif (substr($name, 0, strlen($name) - $vlen) !=
226 $this->_packagexml->getExtends()) {
227 $this->_addWarning('package', "package $name extends package " .
228 $this->_packagexml->getExtends() . ' and so the name must ' .
229 'be an extension like "' . $this->_packagexml->getExtends() .
235 if (!$this->validPackageName($this->_packagexml->getPackage())) {
236 $this->_addFailure('name', 'package name "' .
237 $this->_packagexml->getPackage() . '" is invalid');
245 function validateVersion()
247 if ($this->_state != PEAR_VALIDATE_PACKAGING) {
248 if (!$this->validVersion($this->_packagexml->getVersion())) {
249 $this->_addFailure('version',
250 'Invalid version number "' . $this->_packagexml->getVersion() . '"');
254 $version = $this->_packagexml->getVersion();
255 $versioncomponents = explode('.', $version);
256 if (count($versioncomponents) != 3) {
257 $this->_addWarning('version',
258 'A version number should have 3 decimals (x.y.z)');
261 $name = $this->_packagexml->getPackage();
262 // version must be based upon state
263 switch ($this->_packagexml->getState()) {
267 if ($versioncomponents[0] . 'a' == '0a') {
270 if ($versioncomponents[0] == 0) {
271 $versioncomponents[0] = '0';
272 $this->_addWarning('version',
273 'version "' . $version . '" should be "' .
274 implode('.' ,$versioncomponents) . '"');
276 $this->_addWarning('version',
277 'packages with devel stability must be < version 1.0.0');
283 // check for a package that extends a package,
285 if ($this->_state == PEAR_VALIDATE_PACKAGING) {
286 if (substr($versioncomponents[2], 1, 2) == 'rc') {
287 $this->_addFailure('version', 'Release Candidate versions ' .
288 'must have capital RC, not lower-case rc');
292 if (!$this->_packagexml->getExtends()) {
293 if ($versioncomponents[0] == '1') {
294 if ($versioncomponents[2]{0} == '0') {
295 if ($versioncomponents[2] == '0') {
297 $this->_addWarning('version',
298 'version 1.' . $versioncomponents[1] .
299 '.0 probably should not be alpha or beta');
301 } elseif (strlen($versioncomponents[2]) > 1) {
302 // version 1.*.0RC1 or 1.*.0beta24 etc.
306 $this->_addWarning('version',
307 'version 1.' . $versioncomponents[1] .
308 '.0 probably should not be alpha or beta');
312 $this->_addWarning('version',
313 'bugfix versions (1.3.x where x > 0) probably should ' .
314 'not be alpha or beta');
317 } elseif ($versioncomponents[0] != '0') {
318 $this->_addWarning('version',
319 'major versions greater than 1 are not allowed for packages ' .
320 'without an <extends> tag or an identical postfix (foo2 v2.0.0)');
323 if ($versioncomponents[0] . 'a' == '0a') {
326 if ($versioncomponents[0] == 0) {
327 $versioncomponents[0] = '0';
328 $this->_addWarning('version',
329 'version "' . $version . '" should be "' .
330 implode('.' ,$versioncomponents) . '"');
333 $vlen = strlen($versioncomponents[0] . '');
334 $majver = substr($name, strlen($name) - $vlen);
335 while ($majver && !is_numeric($majver{0})) {
336 $majver = substr($majver, 1);
338 if (($versioncomponents[0] != 0) && $majver != $versioncomponents[0]) {
339 $this->_addWarning('version', 'first version number "' .
340 $versioncomponents[0] . '" must match the postfix of ' .
341 'package name "' . $name . '" (' .
345 if ($versioncomponents[0] == $majver) {
346 if ($versioncomponents[2]{0} == '0') {
347 if ($versioncomponents[2] == '0') {
349 $this->_addWarning('version',
350 "version $majver." . $versioncomponents[1] .
351 '.0 probably should not be alpha or beta');
353 } elseif (strlen($versioncomponents[2]) > 1) {
354 // version 2.*.0RC1 or 2.*.0beta24 etc.
358 $this->_addWarning('version',
359 "version $majver." . $versioncomponents[1] .
360 '.0 cannot be alpha or beta');
364 $this->_addWarning('version',
365 "bugfix versions ($majver.x.y where y > 0) should " .
366 'not be alpha or beta');
369 } elseif ($versioncomponents[0] != '0') {
370 $this->_addWarning('version',
371 "only versions 0.x.y and $majver.x.y are allowed for alpha/beta releases");
374 if ($versioncomponents[0] . 'a' == '0a') {
377 if ($versioncomponents[0] == 0) {
378 $versioncomponents[0] = '0';
379 $this->_addWarning('version',
380 'version "' . $version . '" should be "' .
381 implode('.' ,$versioncomponents) . '"');
387 if ($versioncomponents[0] == '0') {
388 $this->_addWarning('version', 'versions less than 1.0.0 cannot ' .
392 if (!is_numeric($versioncomponents[2])) {
393 if (preg_match('/\d+(rc|a|alpha|b|beta)\d*/i',
394 $versioncomponents[2])) {
395 $this->_addWarning('version', 'version "' . $version . '" or any ' .
396 'RC/beta/alpha version cannot be stable');
400 // check for a package that extends a package,
402 if ($this->_packagexml->getExtends()) {
403 $vlen = strlen($versioncomponents[0] . '');
404 $majver = substr($name, strlen($name) - $vlen);
405 while ($majver && !is_numeric($majver{0})) {
406 $majver = substr($majver, 1);
408 if (($versioncomponents[0] != 0) && $majver != $versioncomponents[0]) {
409 $this->_addWarning('version', 'first version number "' .
410 $versioncomponents[0] . '" must match the postfix of ' .
411 'package name "' . $name . '" (' .
415 } elseif ($versioncomponents[0] > 1) {
416 $this->_addWarning('version', 'major version x in x.y.z may not be greater than ' .
417 '1 for any package that does not have an <extends> tag');
430 function validateMaintainers()
432 // maintainers can only be truly validated server-side for most channels
433 // but allow this customization for those who wish it
440 function validateDate()
442 if ($this->_state == PEAR_VALIDATE_NORMAL ||
443 $this->_state == PEAR_VALIDATE_PACKAGING) {
445 if (!preg_match('/(\d\d\d\d)\-(\d\d)\-(\d\d)/',
446 $this->_packagexml->getDate(), $res) ||
448 || !checkdate($res[2], $res[3], $res[1])
450 $this->_addFailure('date', 'invalid release date "' .
451 $this->_packagexml->getDate() . '"');
455 if ($this->_state == PEAR_VALIDATE_PACKAGING &&
456 $this->_packagexml->getDate() != date('Y-m-d')) {
457 $this->_addWarning('date', 'Release Date "' .
458 $this->_packagexml->getDate() . '" is not today');
467 function validateTime()
469 if (!$this->_packagexml->getTime()) {
470 // default of no time value set
474 // packager automatically sets time, so only validate if pear validate is called
475 if ($this->_state = PEAR_VALIDATE_NORMAL) {
476 if (!preg_match('/\d\d:\d\d:\d\d/',
477 $this->_packagexml->getTime())) {
478 $this->_addFailure('time', 'invalid release time "' .
479 $this->_packagexml->getTime() . '"');
483 $result = preg_match('|\d{2}\:\d{2}\:\d{2}|', $this->_packagexml->getTime(), $matches);
484 if ($result === false || empty($matches)) {
485 $this->_addFailure('time', 'invalid release time "' .
486 $this->_packagexml->getTime() . '"');
497 function validateState()
499 // this is the closest to "final" php4 can get
500 if (!PEAR_Validate::validState($this->_packagexml->getState())) {
501 if (strtolower($this->_packagexml->getState() == 'rc')) {
502 $this->_addFailure('state', 'RC is not a state, it is a version ' .
503 'postfix, use ' . $this->_packagexml->getVersion() . 'RC1, state beta');
505 $this->_addFailure('state', 'invalid release state "' .
506 $this->_packagexml->getState() . '", must be one of: ' .
507 implode(', ', PEAR_Validate::getValidStates()));
516 function validateStability()
519 $packagestability = $this->_packagexml->getState();
520 $apistability = $this->_packagexml->getState('api');
521 if (!PEAR_Validate::validState($packagestability)) {
522 $this->_addFailure('state', 'invalid release stability "' .
523 $this->_packagexml->getState() . '", must be one of: ' .
524 implode(', ', PEAR_Validate::getValidStates()));
527 $apistates = PEAR_Validate::getValidStates();
528 array_shift($apistates); // snapshot is not allowed
529 if (!in_array($apistability, $apistates)) {
530 $this->_addFailure('state', 'invalid API stability "' .
531 $this->_packagexml->getState('api') . '", must be one of: ' .
532 implode(', ', $apistates));
541 function validateSummary()
549 function validateDescription()
557 function validateLicense()
565 function validateNotes()
571 * for package.xml 2.0 only - channels can't use package.xml 1.0
574 function validateDependencies()
580 * for package.xml 1.0 only
583 function _validateFilelist()
585 return true; // placeholder for now
589 * for package.xml 2.0 only
592 function validateMainFilelist()
594 return true; // placeholder for now
598 * for package.xml 2.0 only
601 function validateReleaseFilelist()
603 return true; // placeholder for now
609 function validateChangelog()
617 function validateFilelist()
625 function validateDeps()