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 * @link http://pear.php.net/package/PEAR
13 * @since File available since Release 1.4.0a1
16 * Constants for install stage
18 define('PEAR_VALIDATE_INSTALLING', 1);
19 define('PEAR_VALIDATE_UNINSTALLING', 2); // this is not bit-mapped like the others
20 define('PEAR_VALIDATE_NORMAL', 3);
21 define('PEAR_VALIDATE_DOWNLOADING', 4); // this is not bit-mapped like the others
22 define('PEAR_VALIDATE_PACKAGING', 7);
24 require_once 'PEAR/Common.php';
25 require_once 'PEAR/Validator/PECL.php';
28 * Validation class for package.xml - channel-level advanced validation
31 * @author Greg Beaver <cellog@php.net>
32 * @copyright 1997-2009 The Authors
33 * @license http://opensource.org/licenses/bsd-license.php New BSD License
34 * @version Release: 1.10.1
35 * @link http://pear.php.net/package/PEAR
36 * @since Class available since Release 1.4.0a1
40 var $packageregex = _PEAR_COMMON_PACKAGE_NAME_PREG;
42 * @var PEAR_PackageFile_v1|PEAR_PackageFile_v2
46 * @var int one of the PEAR_VALIDATE_* constants
48 var $_state = PEAR_VALIDATE_NORMAL;
50 * Format: ('error' => array('field' => name, 'reason' => reason), 'warning' => same)
54 var $_failures = array('error' => array(), 'warning' => array());
57 * Override this method to handle validation of normal package names
62 function _validPackageName($name)
64 return (bool) preg_match('/^' . $this->packageregex . '\\z/', $name);
68 * @param string package name to validate
69 * @param string name of channel-specific validation package
72 function validPackageName($name, $validatepackagename = false)
74 if ($validatepackagename) {
75 if (strtolower($name) == strtolower($validatepackagename)) {
76 return (bool) preg_match('/^[a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+)*\\z/', $name);
79 return $this->_validPackageName($name);
83 * This validates a bundle name, and bundle names must conform
84 * to the PEAR naming convention, so the method is final and static.
88 public static function validGroupName($name)
90 return (bool) preg_match('/^' . _PEAR_COMMON_PACKAGE_NAME_PREG . '\\z/', $name);
94 * Determine whether $state represents a valid stability level
99 public static function validState($state)
101 return in_array($state, array('snapshot', 'devel', 'alpha', 'beta', 'stable'));
105 * Get a list of valid stability levels
109 public static function getValidStates()
111 return array('snapshot', 'devel', 'alpha', 'beta', 'stable');
115 * Determine whether a version is a properly formatted version number that can be used
121 public static function validVersion($ver)
123 return (bool) preg_match(PEAR_COMMON_PACKAGE_VERSION_PREG, $ver);
127 * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
129 function setPackageFile(&$pf)
131 $this->_packagexml = &$pf;
137 function _addFailure($field, $reason)
139 $this->_failures['errors'][] = array('field' => $field, 'reason' => $reason);
145 function _addWarning($field, $reason)
147 $this->_failures['warnings'][] = array('field' => $field, 'reason' => $reason);
150 function getFailures()
152 $failures = $this->_failures;
153 $this->_failures = array('warnings' => array(), 'errors' => array());
158 * @param int one of the PEAR_VALIDATE_* constants
160 function validate($state = null)
162 if (!isset($this->_packagexml)) {
165 if ($state !== null) {
166 $this->_state = $state;
168 $this->_failures = array('warnings' => array(), 'errors' => array());
169 $this->validatePackageName();
170 $this->validateVersion();
171 $this->validateMaintainers();
172 $this->validateDate();
173 $this->validateSummary();
174 $this->validateDescription();
175 $this->validateLicense();
176 $this->validateNotes();
177 if ($this->_packagexml->getPackagexmlVersion() == '1.0') {
178 $this->validateState();
179 $this->validateFilelist();
180 } elseif ($this->_packagexml->getPackagexmlVersion() == '2.0' ||
181 $this->_packagexml->getPackagexmlVersion() == '2.1') {
182 $this->validateTime();
183 $this->validateStability();
184 $this->validateDeps();
185 $this->validateMainFilelist();
186 $this->validateReleaseFilelist();
187 //$this->validateGlobalTasks();
188 $this->validateChangelog();
190 return !((bool) count($this->_failures['errors']));
196 function validatePackageName()
198 if ($this->_state == PEAR_VALIDATE_PACKAGING ||
199 $this->_state == PEAR_VALIDATE_NORMAL) {
200 if (($this->_packagexml->getPackagexmlVersion() == '2.0' ||
201 $this->_packagexml->getPackagexmlVersion() == '2.1') &&
202 $this->_packagexml->getExtends()) {
203 $version = $this->_packagexml->getVersion() . '';
204 $name = $this->_packagexml->getPackage();
205 $a = explode('.', $version);
206 $test = array_shift($a);
210 $vlen = strlen($test);
211 $majver = substr($name, strlen($name) - $vlen);
212 while ($majver && !is_numeric($majver{0})) {
213 $majver = substr($majver, 1);
215 if ($majver != $test) {
216 $this->_addWarning('package', "package $name extends package " .
217 $this->_packagexml->getExtends() . ' and so the name should ' .
218 'have a postfix equal to the major version like "' .
219 $this->_packagexml->getExtends() . $test . '"');
221 } elseif (substr($name, 0, strlen($name) - $vlen) !=
222 $this->_packagexml->getExtends()) {
223 $this->_addWarning('package', "package $name extends package " .
224 $this->_packagexml->getExtends() . ' and so the name must ' .
225 'be an extension like "' . $this->_packagexml->getExtends() .
231 if (!$this->validPackageName($this->_packagexml->getPackage())) {
232 $this->_addFailure('name', 'package name "' .
233 $this->_packagexml->getPackage() . '" is invalid');
241 function validateVersion()
243 if ($this->_state != PEAR_VALIDATE_PACKAGING) {
244 if (!$this->validVersion($this->_packagexml->getVersion())) {
245 $this->_addFailure('version',
246 'Invalid version number "' . $this->_packagexml->getVersion() . '"');
250 $version = $this->_packagexml->getVersion();
251 $versioncomponents = explode('.', $version);
252 if (count($versioncomponents) != 3) {
253 $this->_addWarning('version',
254 'A version number should have 3 decimals (x.y.z)');
257 $name = $this->_packagexml->getPackage();
258 // version must be based upon state
259 switch ($this->_packagexml->getState()) {
263 if ($versioncomponents[0] . 'a' == '0a') {
266 if ($versioncomponents[0] == 0) {
267 $versioncomponents[0] = '0';
268 $this->_addWarning('version',
269 'version "' . $version . '" should be "' .
270 implode('.' ,$versioncomponents) . '"');
272 $this->_addWarning('version',
273 'packages with devel stability must be < version 1.0.0');
279 // check for a package that extends a package,
281 if ($this->_state == PEAR_VALIDATE_PACKAGING) {
282 if (substr($versioncomponents[2], 1, 2) == 'rc') {
283 $this->_addFailure('version', 'Release Candidate versions ' .
284 'must have capital RC, not lower-case rc');
288 if (!$this->_packagexml->getExtends()) {
289 if ($versioncomponents[0] == '1') {
290 if ($versioncomponents[2]{0} == '0') {
291 if ($versioncomponents[2] == '0') {
293 $this->_addWarning('version',
294 'version 1.' . $versioncomponents[1] .
295 '.0 probably should not be alpha or beta');
297 } elseif (strlen($versioncomponents[2]) > 1) {
298 // version 1.*.0RC1 or 1.*.0beta24 etc.
302 $this->_addWarning('version',
303 'version 1.' . $versioncomponents[1] .
304 '.0 probably should not be alpha or beta');
308 $this->_addWarning('version',
309 'bugfix versions (1.3.x where x > 0) probably should ' .
310 'not be alpha or beta');
313 } elseif ($versioncomponents[0] != '0') {
314 $this->_addWarning('version',
315 'major versions greater than 1 are not allowed for packages ' .
316 'without an <extends> tag or an identical postfix (foo2 v2.0.0)');
319 if ($versioncomponents[0] . 'a' == '0a') {
322 if ($versioncomponents[0] == 0) {
323 $versioncomponents[0] = '0';
324 $this->_addWarning('version',
325 'version "' . $version . '" should be "' .
326 implode('.' ,$versioncomponents) . '"');
329 $vlen = strlen($versioncomponents[0] . '');
330 $majver = substr($name, strlen($name) - $vlen);
331 while ($majver && !is_numeric($majver{0})) {
332 $majver = substr($majver, 1);
334 if (($versioncomponents[0] != 0) && $majver != $versioncomponents[0]) {
335 $this->_addWarning('version', 'first version number "' .
336 $versioncomponents[0] . '" must match the postfix of ' .
337 'package name "' . $name . '" (' .
341 if ($versioncomponents[0] == $majver) {
342 if ($versioncomponents[2]{0} == '0') {
343 if ($versioncomponents[2] == '0') {
345 $this->_addWarning('version',
346 "version $majver." . $versioncomponents[1] .
347 '.0 probably should not be alpha or beta');
349 } elseif (strlen($versioncomponents[2]) > 1) {
350 // version 2.*.0RC1 or 2.*.0beta24 etc.
354 $this->_addWarning('version',
355 "version $majver." . $versioncomponents[1] .
356 '.0 cannot be alpha or beta');
360 $this->_addWarning('version',
361 "bugfix versions ($majver.x.y where y > 0) should " .
362 'not be alpha or beta');
365 } elseif ($versioncomponents[0] != '0') {
366 $this->_addWarning('version',
367 "only versions 0.x.y and $majver.x.y are allowed for alpha/beta releases");
370 if ($versioncomponents[0] . 'a' == '0a') {
373 if ($versioncomponents[0] == 0) {
374 $versioncomponents[0] = '0';
375 $this->_addWarning('version',
376 'version "' . $version . '" should be "' .
377 implode('.' ,$versioncomponents) . '"');
383 if ($versioncomponents[0] == '0') {
384 $this->_addWarning('version', 'versions less than 1.0.0 cannot ' .
388 if (!is_numeric($versioncomponents[2])) {
389 if (preg_match('/\d+(rc|a|alpha|b|beta)\d*/i',
390 $versioncomponents[2])) {
391 $this->_addWarning('version', 'version "' . $version . '" or any ' .
392 'RC/beta/alpha version cannot be stable');
396 // check for a package that extends a package,
398 if ($this->_packagexml->getExtends()) {
399 $vlen = strlen($versioncomponents[0] . '');
400 $majver = substr($name, strlen($name) - $vlen);
401 while ($majver && !is_numeric($majver{0})) {
402 $majver = substr($majver, 1);
404 if (($versioncomponents[0] != 0) && $majver != $versioncomponents[0]) {
405 $this->_addWarning('version', 'first version number "' .
406 $versioncomponents[0] . '" must match the postfix of ' .
407 'package name "' . $name . '" (' .
411 } elseif ($versioncomponents[0] > 1) {
412 $this->_addWarning('version', 'major version x in x.y.z may not be greater than ' .
413 '1 for any package that does not have an <extends> tag');
426 function validateMaintainers()
428 // maintainers can only be truly validated server-side for most channels
429 // but allow this customization for those who wish it
436 function validateDate()
438 if ($this->_state == PEAR_VALIDATE_NORMAL ||
439 $this->_state == PEAR_VALIDATE_PACKAGING) {
441 if (!preg_match('/(\d\d\d\d)\-(\d\d)\-(\d\d)/',
442 $this->_packagexml->getDate(), $res) ||
444 || !checkdate($res[2], $res[3], $res[1])
446 $this->_addFailure('date', 'invalid release date "' .
447 $this->_packagexml->getDate() . '"');
451 if ($this->_state == PEAR_VALIDATE_PACKAGING &&
452 $this->_packagexml->getDate() != date('Y-m-d')) {
453 $this->_addWarning('date', 'Release Date "' .
454 $this->_packagexml->getDate() . '" is not today');
463 function validateTime()
465 if (!$this->_packagexml->getTime()) {
466 // default of no time value set
470 // packager automatically sets time, so only validate if pear validate is called
471 if ($this->_state = PEAR_VALIDATE_NORMAL) {
472 if (!preg_match('/\d\d:\d\d:\d\d/',
473 $this->_packagexml->getTime())) {
474 $this->_addFailure('time', 'invalid release time "' .
475 $this->_packagexml->getTime() . '"');
479 $result = preg_match('|\d{2}\:\d{2}\:\d{2}|', $this->_packagexml->getTime(), $matches);
480 if ($result === false || empty($matches)) {
481 $this->_addFailure('time', 'invalid release time "' .
482 $this->_packagexml->getTime() . '"');
493 function validateState()
495 // this is the closest to "final" php4 can get
496 if (!PEAR_Validate::validState($this->_packagexml->getState())) {
497 if (strtolower($this->_packagexml->getState() == 'rc')) {
498 $this->_addFailure('state', 'RC is not a state, it is a version ' .
499 'postfix, use ' . $this->_packagexml->getVersion() . 'RC1, state beta');
501 $this->_addFailure('state', 'invalid release state "' .
502 $this->_packagexml->getState() . '", must be one of: ' .
503 implode(', ', PEAR_Validate::getValidStates()));
512 function validateStability()
515 $packagestability = $this->_packagexml->getState();
516 $apistability = $this->_packagexml->getState('api');
517 if (!PEAR_Validate::validState($packagestability)) {
518 $this->_addFailure('state', 'invalid release stability "' .
519 $this->_packagexml->getState() . '", must be one of: ' .
520 implode(', ', PEAR_Validate::getValidStates()));
523 $apistates = PEAR_Validate::getValidStates();
524 array_shift($apistates); // snapshot is not allowed
525 if (!in_array($apistability, $apistates)) {
526 $this->_addFailure('state', 'invalid API stability "' .
527 $this->_packagexml->getState('api') . '", must be one of: ' .
528 implode(', ', $apistates));
537 function validateSummary()
545 function validateDescription()
553 function validateLicense()
561 function validateNotes()
567 * for package.xml 2.0 only - channels can't use package.xml 1.0
570 function validateDependencies()
576 * for package.xml 1.0 only
579 function _validateFilelist()
581 return true; // placeholder for now
585 * for package.xml 2.0 only
588 function validateMainFilelist()
590 return true; // placeholder for now
594 * for package.xml 2.0 only
597 function validateReleaseFilelist()
599 return true; // placeholder for now
605 function validateChangelog()
613 function validateFilelist()
621 function validateDeps()