[ Index ]
 

Code source de GeekLog 1.4.1

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/system/pear/PEAR/ -> Validate.php (source)

   1  <?php
   2  /**
   3   * PEAR_Validate
   4   *
   5   * PHP versions 4 and 5
   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
  14   * @package    PEAR
  15   * @author     Greg Beaver <cellog@php.net>
  16   * @copyright  1997-2006 The PHP Group
  17   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  18   * @version    CVS: $Id: Validate.php,v 1.46.2.3 2006/07/17 17:49:37 pajoye Exp $
  19   * @link       http://pear.php.net/package/PEAR
  20   * @since      File available since Release 1.4.0a1
  21   */
  22  /**#@+
  23   * Constants for install stage
  24   */
  25  define('PEAR_VALIDATE_INSTALLING', 1);
  26  define('PEAR_VALIDATE_UNINSTALLING', 2); // this is not bit-mapped like the others
  27  define('PEAR_VALIDATE_NORMAL', 3);
  28  define('PEAR_VALIDATE_DOWNLOADING', 4); // this is not bit-mapped like the others
  29  define('PEAR_VALIDATE_PACKAGING', 7);
  30  /**#@-*/
  31  require_once 'PEAR/Common.php';
  32  require_once 'PEAR/Validator/PECL.php';
  33  
  34  /**
  35   * Validation class for package.xml - channel-level advanced validation
  36   * @category   pear
  37   * @package    PEAR
  38   * @author     Greg Beaver <cellog@php.net>
  39   * @copyright  1997-2006 The PHP Group
  40   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  41   * @version    Release: 1.4.11
  42   * @link       http://pear.php.net/package/PEAR
  43   * @since      Class available since Release 1.4.0a1
  44   */
  45  class PEAR_Validate
  46  {
  47      var $packageregex = _PEAR_COMMON_PACKAGE_NAME_PREG;
  48      /**
  49       * @var PEAR_PackageFile_v1|PEAR_PackageFile_v2
  50       */
  51      var $_packagexml;
  52      /**
  53       * @var int one of the PEAR_VALIDATE_* constants
  54       */
  55      var $_state = PEAR_VALIDATE_NORMAL;
  56      /**
  57       * Format: ('error' => array('field' => name, 'reason' => reason), 'warning' => same)
  58       * @var array
  59       * @access private
  60       */
  61      var $_failures = array('error' => array(), 'warning' => array());
  62  
  63      /**
  64       * Override this method to handle validation of normal package names
  65       * @param string
  66       * @return bool
  67       * @access protected
  68       */
  69      function _validPackageName($name)
  70      {
  71          return (bool) preg_match('/^' . $this->packageregex . '$/', $name);
  72      }
  73  
  74      /**
  75       * @param string package name to validate
  76       * @param string name of channel-specific validation package
  77       * @final
  78       */
  79      function validPackageName($name, $validatepackagename = false)
  80      {
  81          if ($validatepackagename) {
  82              if (strtolower($name) == strtolower($validatepackagename)) {
  83                  return (bool) preg_match('/^[a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+)*$/', $name);
  84              }
  85          }
  86          return $this->_validPackageName($name);
  87      }
  88  
  89      /**
  90       * This validates a bundle name, and bundle names must conform
  91       * to the PEAR naming convention, so the method is final and static.
  92       * @param string
  93       * @final
  94       * @static
  95       */
  96      function validGroupName($name)
  97      {
  98          return (bool) preg_match('/^' . _PEAR_COMMON_PACKAGE_NAME_PREG . '$/', $name);
  99      }
 100  
 101      /**
 102       * Determine whether $state represents a valid stability level
 103       * @param string
 104       * @return bool
 105       * @static
 106       * @final
 107       */
 108      function validState($state)
 109      {
 110          return in_array($state, array('snapshot', 'devel', 'alpha', 'beta', 'stable'));
 111      }
 112  
 113      /**
 114       * Get a list of valid stability levels
 115       * @return array
 116       * @static
 117       * @final
 118       */
 119      function getValidStates()
 120      {
 121          return array('snapshot', 'devel', 'alpha', 'beta', 'stable');
 122      }
 123  
 124      /**
 125       * Determine whether a version is a properly formatted version number that can be used
 126       * by version_compare
 127       * @param string
 128       * @return bool
 129       * @static
 130       * @final
 131       */
 132      function validVersion($ver)
 133      {
 134          return (bool) preg_match(PEAR_COMMON_PACKAGE_VERSION_PREG, $ver);
 135      }
 136  
 137      /**
 138       * @param PEAR_PackageFile_v1|PEAR_PackageFile_v2
 139       */
 140      function setPackageFile(&$pf)
 141      {
 142          $this->_packagexml = &$pf;
 143      }
 144  
 145      /**
 146       * @access private
 147       */
 148      function _addFailure($field, $reason)
 149      {
 150          $this->_failures['errors'][] = array('field' => $field, 'reason' => $reason);
 151      }
 152  
 153      /**
 154       * @access private
 155       */
 156      function _addWarning($field, $reason)
 157      {
 158          $this->_failures['warnings'][] = array('field' => $field, 'reason' => $reason);
 159      }
 160  
 161      function getFailures()
 162      {
 163          $failures = $this->_failures;
 164          $this->_failures = array('warnings' => array(), 'errors' => array());
 165          return $failures;
 166      }
 167  
 168      /**
 169       * @param int one of the PEAR_VALIDATE_* constants
 170       */
 171      function validate($state = null)
 172      {
 173          if (!isset($this->_packagexml)) {
 174              return false;
 175          }
 176          if ($state !== null) {
 177              $this->_state = $state;
 178          }
 179          $this->_failures = array('warnings' => array(), 'errors' => array());
 180          $this->validatePackageName();
 181          $this->validateVersion();
 182          $this->validateMaintainers();
 183          $this->validateDate();
 184          $this->validateSummary();
 185          $this->validateDescription();
 186          $this->validateLicense();
 187          $this->validateNotes();
 188          if ($this->_packagexml->getPackagexmlVersion() == '1.0') {
 189              $this->validateState();
 190              $this->validateFilelist();
 191          } elseif ($this->_packagexml->getPackagexmlVersion() == '2.0') {
 192              $this->validateTime();
 193              $this->validateStability();
 194              $this->validateDeps();
 195              $this->validateMainFilelist();
 196              $this->validateReleaseFilelist();
 197              //$this->validateGlobalTasks();
 198              $this->validateChangelog();
 199          }
 200          return !((bool) count($this->_failures['errors']));
 201      }
 202  
 203      /**
 204       * @access protected
 205       */
 206      function validatePackageName()
 207      {
 208          if ($this->_state == PEAR_VALIDATE_PACKAGING ||
 209                $this->_state == PEAR_VALIDATE_NORMAL) {
 210              if ($this->_packagexml->getPackagexmlVersion() == '2.0' &&
 211                    $this->_packagexml->getExtends()) {
 212                  $version = $this->_packagexml->getVersion() . '';
 213                  $name = $this->_packagexml->getPackage();
 214                  $test = array_shift($a = explode('.', $version));
 215                  if ($test == '0') {
 216                      return true;
 217                  }
 218                  $vlen = strlen($test);
 219                  $majver = substr($name, strlen($name) - $vlen);
 220                  while ($majver && !is_numeric($majver{0})) {
 221                      $majver = substr($majver, 1);
 222                  }
 223                  if ($majver != $test) {
 224                      $this->_addWarning('package', "package $name extends package " .
 225                          $this->_packagexml->getExtends() . ' and so the name should ' .
 226                          'have a postfix equal to the major version like "' .
 227                          $this->_packagexml->getExtends() . $test . '"');
 228                      return true;
 229                  } elseif (substr($name, 0, strlen($name) - $vlen) !=
 230                              $this->_packagexml->getExtends()) {
 231                      $this->_addWarning('package', "package $name extends package " .
 232                          $this->_packagexml->getExtends() . ' and so the name must ' .
 233                          'be an extension like "' . $this->_packagexml->getExtends() .
 234                          $test . '"');
 235                      return true;
 236                  }
 237              }
 238          }
 239          if (!$this->validPackageName($this->_packagexml->getPackage())) {
 240              $this->_addFailure('name', 'package name "' .
 241                  $this->_packagexml->getPackage() . '" is invalid');
 242              return false;
 243          }
 244      }
 245  
 246      /**
 247       * @access protected
 248       */
 249      function validateVersion()
 250      {
 251          if ($this->_state != PEAR_VALIDATE_PACKAGING) {
 252              if (!$this->validVersion($this->_packagexml->getVersion())) {
 253                  $this->_addFailure('version',
 254                      'Invalid version number "' . $this->_packagexml->getVersion() . '"');
 255              }
 256              return false;
 257          }
 258          $version = $this->_packagexml->getVersion();
 259          $versioncomponents = explode('.', $version);
 260          if (count($versioncomponents) != 3) {
 261              $this->_addWarning('version',
 262                  'A version number should have 3 decimals (x.y.z)');
 263              return true;
 264          }
 265          $name = $this->_packagexml->getPackage();
 266          // version must be based upon state
 267          switch ($this->_packagexml->getState()) {
 268              case 'snapshot' :
 269                  return true;
 270              case 'devel' :
 271                  if ($versioncomponents[0] . 'a' == '0a') {
 272                      return true;
 273                  }
 274                  if ($versioncomponents[0] == 0) {
 275                      $versioncomponents[0] = '0';
 276                      $this->_addWarning('version',
 277                          'version "' . $version . '" should be "' .
 278                          implode('.' ,$versioncomponents) . '"');
 279                  } else {
 280                      $this->_addWarning('version',
 281                          'packages with devel stability must be < version 1.0.0');
 282                  }
 283                  return true;
 284              break;
 285              case 'alpha' :
 286              case 'beta' :
 287                  // check for a package that extends a package,
 288                  // like Foo and Foo2
 289                  if ($this->_state == PEAR_VALIDATE_PACKAGING) {
 290                      if (substr($versioncomponents[2], 1, 2) == 'rc') {
 291                          $this->_addFailure('version', 'Release Candidate versions ' .
 292                              'must have capital RC, not lower-case rc');
 293                          return false;
 294                      }
 295                  }
 296                  if (!$this->_packagexml->getExtends()) {
 297                      if ($versioncomponents[0] == '1') {
 298                          if ($versioncomponents[2]{0} == '0') {
 299                              if ($versioncomponents[2] == '0') {
 300                                  // version 1.*.0000
 301                                  $this->_addWarning('version',
 302                                      'version 1.' . $versioncomponents[1] .
 303                                          '.0 probably should not be alpha or beta');
 304                                  return true;
 305                              } elseif (strlen($versioncomponents[2]) > 1) {
 306                                  // version 1.*.0RC1 or 1.*.0beta24 etc.
 307                                  return true;
 308                              } else {
 309                                  // version 1.*.0
 310                                  $this->_addWarning('version',
 311                                      'version 1.' . $versioncomponents[1] .
 312                                          '.0 probably should not be alpha or beta');
 313                                  return true;
 314                              }
 315                          } else {
 316                              $this->_addWarning('version',
 317                                  'bugfix versions (1.3.x where x > 0) probably should ' .
 318                                  'not be alpha or beta');
 319                              return true;
 320                          }
 321                      } elseif ($versioncomponents[0] != '0') {
 322                          $this->_addWarning('version',
 323                              'major versions greater than 1 are not allowed for packages ' .
 324                              'without an <extends> tag or an identical postfix (foo2 v2.0.0)');
 325                          return true;
 326                      }
 327                      if ($versioncomponents[0] . 'a' == '0a') {
 328                          return true;
 329                      }
 330                      if ($versioncomponents[0] == 0) {
 331                          $versioncomponents[0] = '0';
 332                          $this->_addWarning('version',
 333                              'version "' . $version . '" should be "' .
 334                              implode('.' ,$versioncomponents) . '"');
 335                      }
 336                  } else {
 337                      $vlen = strlen($versioncomponents[0] . '');
 338                      $majver = substr($name, strlen($name) - $vlen);
 339                      while ($majver && !is_numeric($majver{0})) {
 340                          $majver = substr($majver, 1);
 341                      }
 342                      if (($versioncomponents[0] != 0) && $majver != $versioncomponents[0]) {
 343                          $this->_addWarning('version', 'first version number "' .
 344                              $versioncomponents[0] . '" must match the postfix of ' .
 345                              'package name "' . $name . '" (' .
 346                              $majver . ')');
 347                          return true;
 348                      }
 349                      if ($versioncomponents[0] == $majver) {
 350                          if ($versioncomponents[2]{0} == '0') {
 351                              if ($versioncomponents[2] == '0') {
 352                                  // version 2.*.0000
 353                                  $this->_addWarning('version',
 354                                      "version $majver." . $versioncomponents[1] .
 355                                          '.0 probably should not be alpha or beta');
 356                                  return false;
 357                              } elseif (strlen($versioncomponents[2]) > 1) {
 358                                  // version 2.*.0RC1 or 2.*.0beta24 etc.
 359                                  return true;
 360                              } else {
 361                                  // version 2.*.0
 362                                  $this->_addWarning('version',
 363                                      "version $majver." . $versioncomponents[1] .
 364                                          '.0 cannot be alpha or beta');
 365                                  return true;
 366                              }
 367                          } else {
 368                              $this->_addWarning('version',
 369                                  "bugfix versions ($majver.x.y where y > 0) should " .
 370                                  'not be alpha or beta');
 371                              return true;
 372                          }
 373                      } elseif ($versioncomponents[0] != '0') {
 374                          $this->_addWarning('version',
 375                              "only versions 0.x.y and $majver.x.y are allowed for alpha/beta releases");
 376                          return true;
 377                      }
 378                      if ($versioncomponents[0] . 'a' == '0a') {
 379                          return true;
 380                      }
 381                      if ($versioncomponents[0] == 0) {
 382                          $versioncomponents[0] = '0';
 383                          $this->_addWarning('version',
 384                              'version "' . $version . '" should be "' .
 385                              implode('.' ,$versioncomponents) . '"');
 386                      }
 387                  }
 388                  return true;
 389              break;
 390              case 'stable' :
 391                  if ($versioncomponents[0] == '0') {
 392                      $this->_addWarning('version', 'versions less than 1.0.0 cannot ' .
 393                      'be stable');
 394                      return true;
 395                  }
 396                  if (!is_numeric($versioncomponents[2])) {
 397                      if (preg_match('/\d+(rc|a|alpha|b|beta)\d*/i',
 398                            $versioncomponents[2])) {
 399                          $this->_addWarning('version', 'version "' . $version . '" or any ' .
 400                              'RC/beta/alpha version cannot be stable');
 401                          return true;
 402                      }
 403                  }
 404                  // check for a package that extends a package,
 405                  // like Foo and Foo2
 406                  if ($this->_packagexml->getExtends()) {
 407                      $vlen = strlen($versioncomponents[0] . '');
 408                      $majver = substr($name, strlen($name) - $vlen);
 409                      while ($majver && !is_numeric($majver{0})) {
 410                          $majver = substr($majver, 1);
 411                      }
 412                      if (($versioncomponents[0] != 0) && $majver != $versioncomponents[0]) {
 413                          $this->_addWarning('version', 'first version number "' .
 414                              $versioncomponents[0] . '" must match the postfix of ' .
 415                              'package name "' . $name . '" (' .
 416                              $majver . ')');
 417                          return true;
 418                      }
 419                  } elseif ($versioncomponents[0] > 1) {
 420                      $this->_addWarning('version', 'major version x in x.y.z may not be greater than ' .
 421                          '1 for any package that does not have an <extends> tag');
 422                  }
 423                  return true;
 424              break;
 425              default :
 426                  return false;
 427              break;
 428          }
 429      }
 430  
 431      /**
 432       * @access protected
 433       */
 434      function validateMaintainers()
 435      {
 436          // maintainers can only be truly validated server-side for most channels
 437          // but allow this customization for those who wish it
 438          return true;
 439      }
 440  
 441      /**
 442       * @access protected
 443       */
 444      function validateDate()
 445      {
 446          if ($this->_state == PEAR_VALIDATE_NORMAL ||
 447                $this->_state == PEAR_VALIDATE_PACKAGING) {
 448              if (!preg_match('/(\d\d\d\d)\-(\d\d)\-(\d\d)/',
 449                    $this->_packagexml->getDate(), $res) ||
 450                    count($res) < 4
 451                    || !checkdate($res[2], $res[3], $res[1])
 452                  ) {
 453                  $this->_addFailure('date', 'invalid release date "' .
 454                      $this->_packagexml->getDate() . '"');
 455                  return false;
 456              }
 457  
 458              if ($this->_state == PEAR_VALIDATE_PACKAGING &&
 459                    $this->_packagexml->getDate() != date('Y-m-d')) {
 460                  $this->_addWarning('date', 'Release Date "' .
 461                      $this->_packagexml->getDate() . '" is not today');
 462              }
 463          }
 464          return true;
 465      }
 466  
 467      /**
 468       * @access protected
 469       */
 470      function validateTime()
 471      {
 472          if (!$this->_packagexml->getTime()) {
 473              // default of no time value set
 474              return true;
 475          }
 476          // packager automatically sets time, so only validate if
 477          // pear validate is called
 478          if ($this->_state = PEAR_VALIDATE_NORMAL) {
 479              if (!preg_match('/\d\d:\d\d:\d\d/',
 480                    $this->_packagexml->getTime())) {
 481                  $this->_addFailure('time', 'invalid release time "' .
 482                      $this->_packagexml->getTime() . '"');
 483                  return false;
 484              }
 485              if (strtotime($this->_packagexml->getTime()) == -1) {
 486                  $this->_addFailure('time', 'invalid release time "' .
 487                      $this->_packagexml->getTime() . '"');
 488                  return false;
 489              }
 490          }
 491          return true;
 492      }
 493  
 494      /**
 495       * @access protected
 496       */
 497      function validateState()
 498      {
 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');
 504              }
 505              $this->_addFailure('state', 'invalid release state "' .
 506                  $this->_packagexml->getState() . '", must be one of: ' .
 507                  implode(', ', PEAR_Validate::getValidStates()));
 508              return false;
 509          }
 510          return true;
 511      }
 512  
 513      /**
 514       * @access protected
 515       */
 516      function validateStability()
 517      {
 518          $ret = true;
 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()));
 525              $ret = false;
 526          }
 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));
 533              $ret = false;
 534          }
 535          return $ret;
 536      }
 537  
 538      /**
 539       * @access protected
 540       */
 541      function validateSummary()
 542      {
 543          return true;
 544      }
 545  
 546      /**
 547       * @access protected
 548       */
 549      function validateDescription()
 550      {
 551          return true;
 552      }
 553  
 554      /**
 555       * @access protected
 556       */
 557      function validateLicense()
 558      {
 559          return true;
 560      }
 561  
 562      /**
 563       * @access protected
 564       */
 565      function validateNotes()
 566      {
 567          return true;
 568      }
 569  
 570      /**
 571       * for package.xml 2.0 only - channels can't use package.xml 1.0
 572       * @access protected
 573       */
 574      function validateDependencies()
 575      {
 576          return true;
 577      }
 578  
 579      /**
 580       * for package.xml 1.0 only
 581       * @access private
 582       */
 583      function _validateFilelist()
 584      {
 585          return true; // placeholder for now
 586      }
 587  
 588      /**
 589       * for package.xml 2.0 only
 590       * @access protected
 591       */
 592      function validateMainFilelist()
 593      {
 594          return true; // placeholder for now
 595      }
 596  
 597      /**
 598       * for package.xml 2.0 only
 599       * @access protected
 600       */
 601      function validateReleaseFilelist()
 602      {
 603          return true; // placeholder for now
 604      }
 605  
 606      /**
 607       * @access protected
 608       */
 609      function validateChangelog()
 610      {
 611          return true;
 612      }
 613  
 614      /**
 615       * @access protected
 616       */
 617      function validateFilelist()
 618      {
 619          return true;
 620      }
 621  
 622      /**
 623       * @access protected
 624       */
 625      function validateDeps()
 626      {
 627          return true;
 628      }
 629  }
 630  ?>


Généré le : Wed Nov 21 12:27:40 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics