[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/validator/rules/ -> urlformatrule.class.php (source)

   1  <?php
   2  
   3  lt_include(PLOG_CLASS_PATH."class/data/validator/rules/rule.class.php");
   4  lt_include(PLOG_CLASS_PATH."class/net/dns.class.php");
   5  lt_include(PLOG_CLASS_PATH."class/net/http/httpvars.class.php");
   6  
   7  define( "EW_OK", 1 );
   8  
   9  define( "EW_ERR_URL_INVALID_PROTOCOL", 10 );
  10  define( "EW_ERR_URL_INVALID_USER", 20 );
  11  define( "EW_ERR_URL_INVALID_PASSWORD", 30 );
  12  define( "EW_ERR_URL_INVALID_SERVER", 40 );
  13  define( "EW_ERR_URL_INVALID_TLD", 50 );
  14  define( "EW_ERR_URL_INVALID_PORT", 60 );
  15  define( "EW_ERR_URL_INVALID_RESOURCE", 70 );
  16  define( "EW_ERR_URL_INVALID_QUERYSTRING", 80 );
  17  define( "EW_ERR_URL_INVALID_ANCHOR", 100 );
  18  
  19  define( "EW_ERR_URL_MISSING_PROTOCOL", 110 );
  20  define( "EW_ERR_URL_MISSING_USER", 120 );
  21  define( "EW_ERR_URL_MISSING_PASSWORD", 130 );
  22  define( "EW_ERR_URL_MISSING_SERVER", 140 );
  23  define( "EW_ERR_URL_MISSING_TLD", 150 );
  24  define( "EW_ERR_URL_MISSING_PORT", 160 );
  25  define( "EW_ERR_URL_MISSING_RESOURCE", 170 );
  26  define( "EW_ERR_URL_MISSING_QUERYSTRING", 180 );
  27  define( "EW_ERR_URL_MISSING_ANCHOR", 190 );
  28  
  29  
  30  
  31      /**
  32       * \ingroup Validator_Rules
  33       *
  34       * Checks that a given URL is valid.
  35       *
  36       * @copyright 2004   Esben Maaløe esm-at-baseclass.modulweb.dk
  37       * @author   Esben Maaløe esm-at-baseclass.modulweb.dk
  38       * @license You are free to copy/modify this function to your hearts content
  39       *          However I ask that you return any improvements you make to me,
  40       *          and that you credit me in your sourcecode if you use it
  41       * @version 0.24
  42       * http://baseclass.modulweb.dk/urlvalidator/
  43       */
  44  
  45  class UrlFormatRule extends Rule
  46  {
  47      var $_options;
  48  
  49      function UrlFormatRule( $options = Array())
  50      {
  51          $this->Rule();
  52          $this->_options = $options;
  53      }
  54          
  55      function validate( $value )
  56      {
  57          $result = $this->_ValURL( $value, $this->_options );
  58          return( $result['Result'] == EW_OK );
  59      }
  60  
  61      function _ValURL($value, $options = array())
  62      {
  63          $value = trim($value);
  64  
  65          if (!$value)
  66              return array('Result' => array(EW_ERR_URL_EMPTY_STRING), 'Value' => '');
  67  
  68              /* Set up default options */
  69          $options = array_merge(array(/**/
  70                                         /* array('http', 'https', etc.) always lcase! */
  71                                     'AllowedProtocols' =>
  72                                     array('http', 'https', 'ftp', 'mailto',
  73                                           'file', 'news', 'gopher', 'telnet',
  74                                           'nntp'),
  75                                     'AllowBracks' => false, /* Allow square brackets in the query string ? */
  76                                     'Protocols' => array('http', 'https', 'ftp', 'mailto', 'file', 'news', 'gopher', 'telnet', 'nntp'), /**/
  77                                     'AssumeProtocol' => false, /**/
  78                                     ), $options);
  79  
  80              /* Setup default values for $options['Require]*/
  81          @ $options['Require'] = array_merge(array(/**/
  82                                                  'Protocol' => true, /**/
  83                                                  'User' => false, /**/
  84                                                  'Password' => false, /**/
  85                                                  'Server' => true, /**/
  86                                                  'TLD' => false, /**/
  87                                                  'Port' => false, /**/
  88                                                  'Resource' => false, /**/
  89                                                  'QueryString' => false, /**/
  90                                                  'Anchor' => false, /**/
  91                                                  ), $options['Require']);
  92  
  93              /* Setup default values for $options['Forbid]*/
  94          @ $options['Forbid'] = array_merge(array(/**/
  95                                                 'Protocol' => false, /**/
  96                                                 'User' => false, /**/
  97                                                 'Password' => false, /**/
  98                                                 'Server' => false, /**/
  99                                                 'TLD' => false, /**/
 100                                                 'Port' => false, /**/
 101                                                 'Resource' => false, /**/
 102                                                 'QueryString' => false, /**/
 103                                                 'Anchor' => false, /**/
 104                                                 ), $options['Forbid']);
 105  
 106              /* Create a container for the URL parts*/
 107          $url = array(/**/
 108              'Protocol' => '', /**/
 109              'User' => '', /**/
 110              'Password' => '', /**/
 111              'Server' => '', /**/
 112              'Port' => '', /**/
 113              'Resource' => '', /**/
 114              'TLD' => '', /**/
 115              'QueryString' => '', /**/
 116              'Anchor' => '');
 117  
 118              /* Setup errorcodes for invalid elements */
 119          $errCodeInvalid = array(/**/
 120              'Protocol' => EW_ERR_URL_INVALID_PROTOCOL, /**/
 121              'User' => EW_ERR_URL_INVALID_USER, /**/
 122              'Password' => EW_ERR_URL_INVALID_PASSWORD, /**/
 123              'Server' => EW_ERR_URL_INVALID_SERVER, /**/
 124              'TLD' => EW_ERR_URL_INVALID_TLD, /**/
 125              'Port' => EW_ERR_URL_INVALID_PORT, /**/
 126              'Resource' => EW_ERR_URL_INVALID_RESOURCE, /**/
 127              'QueryString' => EW_ERR_URL_INVALID_QUERYSTRING, /**/
 128              'Anchor' => EW_ERR_URL_INVALID_ANCHOR);
 129  
 130              /* Setup errorcodes for missing elements */
 131          $errCodeMissing = array(/**/
 132              'Protocol' => EW_ERR_URL_MISSING_PROTOCOL, /**/
 133              'User' => EW_ERR_URL_MISSING_USER, /**/
 134              'Password' => EW_ERR_URL_MISSING_PASSWORD, /**/
 135              'Server' => EW_ERR_URL_MISSING_SERVER, /**/
 136              'TLD' => EW_ERR_URL_MISSING_TLD, /**/
 137              'Port' => EW_ERR_URL_MISSING_PORT, /**/
 138              'Resource' => EW_ERR_URL_MISSING_RESOURCE, /**/
 139              'QueryString' => EW_ERR_URL_MISSING_QUERYSTRING, /**/
 140              'Anchor' => EW_ERR_URL_MISSING_ANCHOR);
 141  
 142              /* set up some needed vars */
 143          extract($options);
 144          $errArr = array();
 145          $tmpValue = $value;
 146          $lcValue = strtolower($value);
 147  
 148              /**
 149               * Split the url into it's subparts
 150               */
 151  
 152          foreach ($Protocols as $key => $protocol)
 153          {
 154              if (strpos($lcValue, "$protocol:") === 0)
 155              {
 156                  $tmp = explode(':', $tmpValue, 2);
 157                  $url['Protocol'] = $tmp[0];
 158                  $tmpValue = $tmp[1];
 159  
 160                  if ($url['Protocol'] == 'mailto' || $url['Protocol'] == 'news')
 161                  {
 162  
 163                          /* Check for % that is NOT an escape sequence */
 164                      if (preg_match('/%[^a-f0-9]/i', $tmpValue) || preg_match("/^[^a-z0-9;&=+$,_.!*'()%~-]/i", $tmpValue))
 165                      {
 166                          $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL;
 167                      }
 168                  }
 169                  else
 170                  {
 171                      if (!(strpos($tmpValue, '//') === 0))
 172                      {
 173                          $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL;
 174                      }
 175                      else
 176                      {
 177                          $tmpValue = substr($tmpValue, 2);
 178                      }
 179                  }
 180              }
 181          }
 182  
 183          if (!$url['Protocol'])
 184          {
 185              if (strpos(strtolower($tmpValue), ('mailto:')) === 0 || strpos(strtolower($tmpValue), ('news:')) === 0)
 186                  $tmp = ':';
 187              else
 188                  $tmp = '://';
 189  
 190              $tmp = explode($tmp, $tmpValue, 2);
 191              if (count($tmp) == 2)
 192              {
 193                  $url['Protocol'] = strtolower($tmp[0]);
 194                  $tmpValue = $tmp[1];
 195              }
 196          }
 197  
 198          $tmp = explode('?', $tmpValue);
 199  
 200          if (count($tmp) > 1)
 201          {
 202              $tmpValue = $tmp[0];
 203              $url['QueryString'] = $tmp[1];
 204  
 205              $tmp = explode('#', $url['QueryString']);
 206              if (count($tmp) > 1)
 207              {
 208                  $url['QueryString'] = $tmp[0];
 209                  $url['Anchor'] = $tmp[1];
 210              }
 211          }
 212          else
 213          {
 214              $tmp = explode('#', $tmpValue);
 215              if (count($tmp) > 1)
 216              {
 217                  $tmpValue = $tmp[0];
 218                  $url['Anchor'] = $tmp[1];
 219              }
 220          }
 221  
 222          $tmp = explode('/', $tmpValue, 2);
 223          if (count($tmp) > 1)
 224          {
 225              $url['Server'] = strtolower($tmp[0]);
 226              $url['Resource'] = $tmp[1];
 227          }
 228          else
 229          {
 230              $url['Server'] = strtolower($tmpValue);
 231          }
 232  
 233              /* User / password */
 234          $tmp = explode('@', $url['Server']);
 235          if (count($tmp) > 1)
 236          {
 237              $url['User'] = $tmp[0];
 238              $url['Server'] = $tmp[1];
 239  
 240              if ($url['User'])
 241              {
 242                  $tmp = explode(':', $url['User']);
 243                  if (count($tmp) > 1)
 244                  {
 245                      $url['User'] = $tmp[0];
 246                      $url['Password'] = $tmp[1];
 247                  }
 248              }
 249          }
 250  
 251          $tmp = explode(':', $url['Server'], 2);
 252          if (count($tmp) > 1)
 253          {
 254              if ($tmp[0])
 255              {
 256                  $url['Server'] = $tmp[0];
 257                  $url['Port'] = $tmp[1];
 258  
 259              }
 260          }
 261  
 262          if (!$url['Protocol'] && !$url['Password'] && in_array(strtolower($url['User']), array('mail', 'news')))
 263          {
 264              $url['Protocol'] = strtolower($url['User']);
 265              $url['User'] = '';
 266  
 267          }
 268  
 269          if ($url['Protocol'] == 'mailto' && $url['Server'] && !$url['User'])
 270          {
 271              $url['User'] = $url['Server'];
 272              $url['Server'] = '';
 273          }
 274  
 275              /**
 276               * Validate the different subparts
 277               */
 278  
 279              /* Check the protocol */
 280          if ($url['Protocol'])
 281          {
 282              $tmp = preg_replace("/[^a-z0-9+-.]/", '', $url['Protocol']);
 283  
 284              if ($tmp != $url['Protocol'])
 285              {
 286                  $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL;
 287              }
 288  
 289              if (count($options['AllowedProtocols']))
 290                  if (!in_array($url['Protocol'], $options['AllowedProtocols']))
 291                      $errArr[EW_ERR_URL_INVALID_PROTOCOL] = EW_ERR_URL_INVALID_PROTOCOL;
 292  
 293          }
 294  
 295              /* check userinfo */
 296          if ($url['User'])
 297          {
 298                  /* Check for % that is NOT an escape sequence */
 299              if (preg_match('/%[^a-f0-9]/i', $url['User']) || preg_match("/[^a-z0-9;&=+$,_.!~*'()%-]/i", $url['User']))
 300              {
 301                  $errArr[EW_ERR_URL_INVALID_USER] = EW_ERR_URL_INVALID_USER;
 302                  $url['User'] = urlencode(urldecode($url['User']));
 303              }
 304          }
 305          if ($url['Password'])
 306          {
 307                  /* Check for % that is NOT an escape sequence */
 308              if (preg_match('/%[^a-f0-9]/i', $url['Password']) || preg_match("/[^a-z0-9;&=+$,_.!~*'()%-]/i", $url['Password']))
 309              {
 310                  $errArr[EW_ERR_URL_INVALID_PASSWORD] = EW_ERR_URL_INVALID_PASSWORD;
 311              }
 312              $url['Password'] = urlencode(urldecode($url['Password']));
 313          }
 314  
 315              //      userinfo      = *( unreserved | escaped |
 316              //                         ";" | ":" | "&" | "=" | "+" | "$" | "," )
 317              //      unreserved    = alphanum | mark
 318              //      mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |
 319              //                      "(" | ")"
 320  
 321              //      escaped       = "%" hex hex
 322              /* Check if the server part is an ip */
 323          if ($url['Server'])
 324          {
 325              if (!preg_match('/[^.0-9]/', $url['Server']))
 326              {
 327                  $ServerIsIP = true;
 328  
 329                  $ipErr = false;
 330  
 331                  $ipPart = explode('.', $url['Server']);
 332  
 333                  if ($ipPart[0] > 224 || $ipPart[0] == 0)
 334                  {
 335                      $errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
 336                  }
 337                  else
 338                  {
 339                      for ($i = 1; $i < 4; $i ++)
 340                      {
 341                          $ipPart[$i] = (integer) $ipPart[$i];
 342                          if ($ipPart[$i] > 255)
 343                              $errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
 344                      }
 345                  }
 346  
 347                      /**
 348                       * @todo Implement checking for reserved class D and E, and
 349                       * other reserved addresses such as 0.0.0.0 or 255.255.255.255
 350                       * and ip-addresses where either the host or the network part
 351                       * is all binary 0s or all binary 1s
 352                       * check:
 353                       * http://www.cisco.com/univercd/cc/td/doc/product/atm/l2020/2020r21x/planning/appndxa.htm#xtocid87496
 354                       */
 355  
 356                  $url['Server'] = join('.', $ipPart);
 357              }
 358                  /* url is not an ip */
 359              else
 360              {
 361                  $ServerIsIP = false;
 362  
 363                  $serverParts = explode('.', $url['Server']);
 364  
 365                      /* check serverparts */
 366                  for ($i = 0; $i < count($serverParts); $i ++)
 367                  {
 368                      $tmp = preg_replace('/[^a-z0-9-]/', '', $serverParts[$i]);
 369                      
 370                          /* Check if it is a top-level server */
 371                      if ($i  && $i == count($serverParts) - 1)
 372                          $tmp = preg_replace('/^[^a-z]/', '', $tmp);
 373                      else
 374                          $tmp = preg_replace('/^[^a-z0-9]/', '', $serverParts[$i]);
 375                      
 376                      $tmp = preg_replace('/[^a-z0-9]$/', '', $tmp);
 377  
 378                      if ($serverParts[$i] == '' || $tmp != $serverParts[$i])
 379                      {
 380                          if ($tmp != '')
 381                              $serverParts[$i] = $tmp;
 382                          else
 383                              unset($serverParts[$i]);
 384  
 385                          $errArr[EW_ERR_URL_INVALID_SERVER] = EW_ERR_URL_INVALID_SERVER;
 386  
 387                      }
 388                  }
 389  
 390                  if (count($serverParts) < 2)
 391                  {
 392                      if ($Require['TLD'])
 393                      {
 394                          $errArr[EW_ERR_URL_MISSING_TLD] = EW_ERR_URL_MISSING_TLD;
 395                      }
 396                  }
 397                  else
 398                  {
 399  
 400                      $url['TLD'] = $serverParts[count($serverParts) - 1];
 401                  }
 402  
 403                  $url['Server'] = join('.', $serverParts);
 404              }
 405          }
 406  
 407              /* Check the Port */
 408          if ($url['Port'])
 409          {
 410              $tmp = (integer) $url['Port'];
 411              if ($url['Port'] != (string) $tmp)
 412              {
 413                  $errArr[EW_ERR_URL_INVALID_PORT] = EW_ERR_URL_INVALID_PORT;
 414  
 415                  $url['Port'] = '';
 416              }
 417              else
 418              {
 419                  $url['Port'] = $tmp;
 420                  if ($url['Port'] > 65535)
 421                      $errArr[EW_ERR_URL_INVALID_PORT] = EW_ERR_URL_INVALID_PORT;
 422              }
 423  
 424          }
 425  
 426              /* Check the resource */
 427              //path          = [ abs_path | opaque_part ]
 428              //path_segments = segment *( "/" segment )
 429              //segment       = *pchar *( ";" param )
 430              //param         = *pchar
 431              //pchar         = unreserved | escaped |
 432              //                ":" | "@" | "&" | "=" | "+" | "$" | ","
 433  
 434          if ($url['Resource'])
 435          {
 436              $resourceParts = explode('/', $url['Resource']);
 437  
 438              if ($resourceParts[count($resourceParts) - 1] == '')
 439                  array_pop($resourceParts);
 440  
 441              if ($resourceParts[0] == '')
 442                  unset($resourceParts[0]);
 443  
 444              foreach ($resourceParts as $key => $part)
 445              {
 446                  if ($part == '')
 447                  {
 448                      $errArr[EW_ERR_URL_INVALID_RESOURCE] = EW_ERR_URL_INVALID_RESOURCE;
 449                      unset($resourceParts[$key]);
 450                  }
 451  
 452                      /* Check for % that is NOT an escape sequence || invalid chars*/
 453                  elseif (preg_match('/%[^a-f0-9]/i', $part) || preg_match("/[^@a-z0-9_.!~*'()$+&,%:=;?-]/i", $part))
 454                  {
 455                      $errArr[EW_ERR_URL_INVALID_RESOURCE] = EW_ERR_URL_INVALID_RESOURCE;
 456                      $resourceParts[$key] = urlencode(urldecode($part));
 457                  }
 458  
 459                      /* check for invalid chars */
 460  
 461              }
 462              $url['Resource'] = join('/', $resourceParts);
 463          }
 464  
 465          if ($url['QueryString'])
 466          {
 467  
 468                  /* Check for % NOT part of an escape sequence || invalid chars */
 469              $tmp = $options['AllowBracks'] ? /**/
 470                  "^a-z0-9_.!~*'()%;\/?:@&=+$,\[\]-" : /**/
 471                  "^a-z0-9_.!~*'()%;\/?:@&=+$,-"; /**/
 472  
 473              if (preg_match('/%[^a-f0-9]/i', $url['QueryString']) || preg_match("/[$tmp]+/i", $url['QueryString']))
 474              {
 475                  $errArr[EW_ERR_URL_INVALID_QUERYSTRING] = EW_ERR_URL_INVALID_QUERYSTRING;
 476                  $url['QueryString'] = $url['QueryString'];
 477              }
 478  
 479          }
 480          if ($url['Anchor'])
 481          {
 482              if (preg_match('/%[^a-f0-9][a-f0-9]?/i', $url['Anchor']) || //
 483                  preg_match("/[^a-z0-9-_.!~*'()%;\/?:@&=+$,]/i", $url['Anchor']))
 484              {
 485                  $errArr[EW_ERR_URL_INVALID_ANCHOR] = EW_ERR_URL_INVALID_ANCHOR;
 486                  $url['Anchor'] = $url['Anchor'];
 487              }
 488  
 489          }
 490          foreach ($url as $partName => $notused)
 491          {
 492              if ($partName == 'TLD' && $ServerIsIP)
 493                  continue;
 494  
 495              if ($Require[$partName] && !$url[$partName])
 496                  $errArr[$errCodeMissing[$partName]] = $errCodeMissing[$partName];
 497  
 498              if ($Forbid[$partName] && $url[$partName])
 499                  $errArr[$errCodeMissing[$partName]] = $errCodeInvalid[$partName];
 500          }
 501  
 502              /* Construct an estimate of what the value should've been */
 503          if ($options['AssumeProtocol'] && !$url['Protocol'] && ($url['Server'] || (!$url['Server'] && !$url['Resource'])))
 504              $url['Protocol'] = $options['AssumeProtocol'];
 505  
 506          $value = $url['Protocol'];
 507  
 508          if ($url['Protocol'])
 509          {
 510              if ($url['Protocol'] == 'mailto' | $url['Protocol'] == 'mailto')
 511                  $value.= ':';
 512              else
 513                  $value.= '://';
 514          }
 515  
 516          if ($url['User'])
 517          {
 518              if ($url['Password'])
 519                  $value.= "{$url['User']}:{$url['Password']}";
 520              else
 521                  $value.= "{$url['User']}";
 522  
 523              if ($url['Server'])
 524                  $value.= '@';
 525          }
 526  
 527          $value.= $url['Server'];
 528  
 529          if ($url['Port'])
 530              $value.= ":{$url['Port']}";
 531  
 532          if ($url['Server'] && $url['Resource'])
 533              $value.= "/";
 534  
 535          $value.= $url['Resource'];
 536  
 537          if ($url['QueryString'])
 538              $value.= "?{$url['QueryString']}";
 539  
 540          if ($url['Anchor'])
 541              $value.= "#{$url['Anchor']}";
 542  
 543          $r = array('Result' => count($errArr) ? $errArr : EW_OK, 'Value' => $value, 'URLParts' => $url);
 544  
 545          return $r;
 546  
 547      }
 548  
 549  }
 550  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics