Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * About validateUrlSyntax():
  20   * This function will verify if a http URL is formatted properly, returning
  21   * either with true or false.
  22   *
  23   * I used rfc #2396 URI: Generic Syntax as my guide when creating the
  24   * regular expression. For all the details see the comments below.
  25   *
  26   * Usage:
  27   *     validateUrlSyntax( url_to_check[, options])
  28   *
  29   *     url_to_check - string - The url to check
  30   *
  31   *     options - string - A optional string of options to set which parts of
  32   *          the url are required, optional, or not allowed. Each option
  33   *          must be followed by a "+" for required, "?" for optional, or
  34   *          "-" for not allowed.
  35   *
  36   *          s - Scheme. Allows "+?-", defaults to "s?"
  37   *              H - http:// Allows "+?-", defaults to "H?"
  38   *              S - https:// (SSL). Allows "+?-", defaults to "S?"
  39   *              E - mailto: (email). Allows "+?-", defaults to "E-"
  40   *              F - ftp:// Allows "+?-", defaults to "F-"
  41   *                  Dependant on scheme being enabled
  42   *          u - User section. Allows "+?-", defaults to "u?"
  43   *              P - Password in user section. Allows "+?-", defaults to "P?"
  44   *                  Dependant on user section being enabled
  45   *          a - Address (ip or domain). Allows "+?-", defaults to "a+"
  46   *              I - Ip address. Allows "+?-", defaults to "I?"
  47   *                  If I+, then domains are disabled
  48   *                  If I-, then domains are required
  49   *                  Dependant on address being enabled
  50   *          p - Port number. Allows "+?-", defaults to "p?"
  51   *          f - File path. Allows "+?-", defaults to "f?"
  52   *          q - Query section. Allows "+?-", defaults to "q?"
  53   *          r - Fragment (anchor). Allows "+?-", defaults to "r?"
  54   *
  55   *  Paste the funtion code, or include_once() this template at the top of the page
  56   *  you wish to use this function.
  57   *
  58   *
  59   * Examples:
  60   * <code>
  61   *  validateUrlSyntax('http://george@www.cnn.com/#top')
  62   *
  63   *  validateUrlSyntax('https://games.yahoo.com:8080/board/chess.htm?move=true')
  64   *
  65   *  validateUrlSyntax('http://www.hotmail.com/', 's+u-I-p-q-r-')
  66   *
  67   *  validateUrlSyntax('/directory/file.php#top', 's-u-a-p-f+')
  68   *
  69   *
  70   *  if (validateUrlSyntax('http://www.canowhoopass.com/', 'u-'))
  71   *  {
  72   *      echo 'URL SYNTAX IS VERIFIED';
  73   *  } else {
  74   *      echo 'URL SYNTAX IS ILLEGAL';
  75   *  }
  76   * </code>
  77   *
  78   * Last Edited:
  79   *  June 15th 2017
  80   *
  81   *
  82   * Changelog:
  83   *  June 15th 2017 by Moodle.
  84   *    -Added rtmp:// option.
  85   *
  86   *  December 15th 2004
  87   *    -Added new TLD's - .jobs, .mobi, .post and .travel. They are official, but not yet active.
  88   *
  89   *  August 31th 2004
  90   *    -Fixed bug allowing empty username even when it was required
  91   *    -Changed and added a few options to add extra schemes
  92   *    -Added mailto: ftp:// and http:// options
  93   *    -https option was 'l' now it is 'S' (capital)
  94   *    -Added password option. Now passwords can be disabled while usernames are ok (for email)
  95   *    -IP Address option was 'i' now it is 'I' (capital)
  96   *    -Options are now case sensitive
  97   *    -Added validateEmailSyntax() and validateFtpSyntax() functions below<br>
  98   *
  99   *  August 27th, 2004
 100   *    -IP group range is more specific. Used to allow 0-299. Now it is 0-255
 101   *    -Port range more specific. Used to allow 0-69999. Now it is 0-65535<br>
 102   *    -Fixed bug disallowing 'i-' option.<br>
 103   *    -Changed license to GPL
 104   *
 105   *  July 8th, 2004
 106   *    -Fixed bug disallowing 'l-' option. Thanks Dr. Cheap
 107   *
 108   *  June 15, 2004
 109   *    -Added options parameter to make it easier for people to plug the function in
 110   *     without needed to rework the code.
 111   *    -Split the example application away from the function
 112   *
 113   *  June 1, 2004
 114   *    -Complete rewrite
 115   *    -Now more modular
 116   *      -Easier to disable sections
 117   *      -Easier to port to other languages
 118   *      -Easier to port to verify email addresses
 119   *    -Uses only simple regular expressions so it is more portable
 120   *    -Follows RFC closer for domain names. Some "play" domains may break
 121   *    -Renamed from 'verifyUrl()' to 'validateUrlSyntax()'
 122   *    -Removed extra code which added 'http://' and trailing '/' if it was missing
 123   *      -That code was better suited for a massaging function, not verifying
 124   *    -Bug fixes:
 125   *      -Now splits up and forces '/path?query#fragment' order
 126   *      -No longer requires a path when using a query or fragment
 127   *
 128   *  August 29, 2003
 129   *    -Allowed port numbers above 9999. Now allows up to 69999
 130   *
 131   *  Sometime, 2002
 132   *    -Added new top level domains
 133   *      -aero, coop, museum, name, info, biz, pro
 134   *
 135   *  October 5, 2000
 136   *    -First Version
 137   *
 138   *
 139   * Intentional Limitations:
 140   *  -Does not verify url actually exists. Only validates the syntax
 141   *  -Strictly follows the RFC standards. Some urls exist in the wild which will
 142   *   not validate. Including ones with square brackets in the query section '[]'
 143   *
 144   *
 145   * Known Problems:
 146   *  -None at this time
 147   *
 148   *
 149   * Author(s):
 150   *  Rod Apeldoorn - rod(at)canowhoopass(dot)com
 151   *
 152   *
 153   * Homepage:
 154   *  http://www.canowhoopass.com/
 155   *
 156   *
 157   * Thanks!:
 158   *  -WEAV -Several members of Weav helped to test - http://weav.bc.ca/
 159   *  -There were also a number of emails from other developers expressing
 160   *   thanks and suggestions. It is nice to be appreciated. Thanks!
 161   *
 162   * Alternate Commercial Licenses:
 163   * For information in regards to alternate licensing, contact me.
 164   *
 165   * @package moodlecore
 166   * @copyright Copyright 2004, Rod Apeldoorn {@link http://www.canowhoopass.com/}
 167   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 168   */
 169  
 170  /**
 171   *  BEGINNING OF validateUrlSyntax() function
 172   */
 173  function validateUrlSyntax( $urladdr, $options="" ){
 174  
 175      // Force Options parameter to be lower case
 176      // DISABLED PERMAMENTLY - OK to remove from code
 177      //    $options = strtolower($options);
 178  
 179      // Check Options Parameter
 180      if (!preg_match( '/^([sHSEFRuPaIpfqr][+?-])*$/', $options ))
 181      {
 182          trigger_error("Options attribute malformed", E_USER_ERROR);
 183      }
 184  
 185      // Set Options Array, set defaults if options are not specified
 186      // Scheme
 187      if (strpos( $options, 's') === false) $aOptions['s'] = '?';
 188      else $aOptions['s'] = substr( $options, strpos( $options, 's') + 1, 1);
 189      // http://
 190      if (strpos( $options, 'H') === false) $aOptions['H'] = '?';
 191      else $aOptions['H'] = substr( $options, strpos( $options, 'H') + 1, 1);
 192      // https:// (SSL)
 193      if (strpos( $options, 'S') === false) $aOptions['S'] = '?';
 194      else $aOptions['S'] = substr( $options, strpos( $options, 'S') + 1, 1);
 195      // mailto: (email)
 196      if (strpos( $options, 'E') === false) $aOptions['E'] = '-';
 197      else $aOptions['E'] = substr( $options, strpos( $options, 'E') + 1, 1);
 198      // ftp://
 199      if (strpos( $options, 'F') === false) $aOptions['F'] = '-';
 200      else $aOptions['F'] = substr( $options, strpos( $options, 'F') + 1, 1);
 201      // rtmp://
 202      if (strpos( $options, 'R') === false) $aOptions['R'] = '-';
 203      else $aOptions['R'] = substr( $options, strpos( $options, 'R') + 1, 1);
 204      // User section
 205      if (strpos( $options, 'u') === false) $aOptions['u'] = '?';
 206      else $aOptions['u'] = substr( $options, strpos( $options, 'u') + 1, 1);
 207      // Password in user section
 208      if (strpos( $options, 'P') === false) $aOptions['P'] = '?';
 209      else $aOptions['P'] = substr( $options, strpos( $options, 'P') + 1, 1);
 210      // Address Section
 211      if (strpos( $options, 'a') === false) $aOptions['a'] = '+';
 212      else $aOptions['a'] = substr( $options, strpos( $options, 'a') + 1, 1);
 213      // IP Address in address section
 214      if (strpos( $options, 'I') === false) $aOptions['I'] = '?';
 215      else $aOptions['I'] = substr( $options, strpos( $options, 'I') + 1, 1);
 216      // Port number
 217      if (strpos( $options, 'p') === false) $aOptions['p'] = '?';
 218      else $aOptions['p'] = substr( $options, strpos( $options, 'p') + 1, 1);
 219      // File Path
 220      if (strpos( $options, 'f') === false) $aOptions['f'] = '?';
 221      else $aOptions['f'] = substr( $options, strpos( $options, 'f') + 1, 1);
 222      // Query Section
 223      if (strpos( $options, 'q') === false) $aOptions['q'] = '?';
 224      else $aOptions['q'] = substr( $options, strpos( $options, 'q') + 1, 1);
 225      // Fragment (Anchor)
 226      if (strpos( $options, 'r') === false) $aOptions['r'] = '?';
 227      else $aOptions['r'] = substr( $options, strpos( $options, 'r') + 1, 1);
 228  
 229  
 230      // Loop through options array, to search for and replace "-" to "{0}" and "+" to ""
 231      foreach($aOptions as $key => $value)
 232      {
 233          if ($value == '-')
 234          {
 235              $aOptions[$key] = '{0}';
 236          }
 237          if ($value == '+')
 238          {
 239              $aOptions[$key] = '';
 240          }
 241      }
 242  
 243      // DEBUGGING - Unescape following line to display to screen current option values
 244      // echo '<pre>'; print_r($aOptions); echo '</pre>';
 245  
 246  
 247      // Preset Allowed Characters
 248      $alphanum    = '[a-zA-Z0-9]';  // Alpha Numeric
 249      $unreserved  = '[a-zA-Z0-9_.!~*' . '\'' . '()-]';
 250      $escaped     = '(%[0-9a-fA-F]{2})'; // Escape sequence - In Hex - %6d would be a 'm'
 251      $reserved    = '[;/?:@&=+$,]'; // Special characters in the URI
 252  
 253      // Beginning Regular Expression
 254      // Scheme - Allows for 'http://', 'https://', 'mailto:', 'ftp://' or 'rtmp://'
 255      $scheme            = '(';
 256      if     ($aOptions['H'] === '') { $scheme .= 'http://'; }
 257      elseif ($aOptions['S'] === '') { $scheme .= 'https://'; }
 258      elseif ($aOptions['E'] === '') { $scheme .= 'mailto:'; }
 259      elseif ($aOptions['F'] === '') { $scheme .= 'ftp://'; }
 260      elseif ($aOptions['R'] === '') { $scheme .= 'rtmp://'; }
 261      else
 262      {
 263          if ($aOptions['H'] === '?') { $scheme .= '|(http://)'; }
 264          if ($aOptions['S'] === '?') { $scheme .= '|(https://)'; }
 265          if ($aOptions['E'] === '?') { $scheme .= '|(mailto:)'; }
 266          if ($aOptions['F'] === '?') { $scheme .= '|(ftp://)'; }
 267          if ($aOptions['R'] === '?') { $scheme .= '|(rtmp://)'; }
 268          $scheme = str_replace('(|', '(', $scheme); // fix first pipe
 269      }
 270      $scheme            .= ')' . $aOptions['s'];
 271      // End setting scheme
 272  
 273                         // User Info - Allows for 'username@' or 'username:password@'. Note: contrary to rfc, I removed ':' from username section, allowing it only in password.
 274                         //   /---------------- Username -----------------------\  /-------------------------------- Password ------------------------------\
 275      $userinfo          = '((' . $unreserved . '|' . $escaped . '|[;&=+$,]' . ')+(:(' . $unreserved . '|' . $escaped . '|[;:&=+$,]' . ')+)' . $aOptions['P'] . '@)' . $aOptions['u'];
 276  
 277                         // IP ADDRESS - Allows 0.0.0.0 to 255.255.255.255
 278      $ipaddress         = '((((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9]))\.){3}((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9])))';
 279  
 280                         // Tertiary Domain(s) - Optional - Multi - Although some sites may use other characters, the RFC says tertiary domains have the same naming restrictions as second level domains
 281      $domain_tertiary   = '(' . $alphanum . '(([a-zA-Z0-9-]{0,62})' . $alphanum . ')?\.)*';
 282  
 283  /* MDL-9295 - take out domain_secondary here and below, so that URLs like http://localhost/ and lan addresses like http://host/ are accepted.
 284                         // Second Level Domain - Required - First and last characters must be Alpha-numeric. Hyphens are allowed inside.
 285      $domain_secondary  = '(' . $alphanum . '(([a-zA-Z0-9-]{0,62})' . $alphanum . ')?\.)';
 286  */
 287  
 288  // we want more relaxed URLs in Moodle: MDL-11462
 289                         // Top Level Domain - First character must be Alpha. Last character must be AlphaNumeric. Hyphens are allowed inside.
 290      $domain_toplevel   = '([a-zA-Z](([a-zA-Z0-9-]*)[a-zA-Z0-9])?)';
 291  /*                       // Top Level Domain - Required - Domain List Current As Of December 2004. Use above escaped line to be forgiving of possible future TLD's
 292      $domain_toplevel   = '(aero|biz|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|post|pro|travel|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ax|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)';
 293  */
 294  
 295                         // Address can be IP address or Domain
 296      if ($aOptions['I'] === '{0}') {       // IP Address Not Allowed
 297          $address       = '(' . $domain_tertiary . /* MDL-9295 $domain_secondary . */ $domain_toplevel . ')';
 298      } elseif ($aOptions['I'] === '') {  // IP Address Required
 299          $address       = '(' . $ipaddress . ')';
 300      } else {                            // IP Address Optional
 301          $address       = '((' . $ipaddress . ')|(' . $domain_tertiary . /* MDL-9295 $domain_secondary . */ $domain_toplevel . '))';
 302      }
 303      $address = $address . $aOptions['a'];
 304  
 305                         // Port Number - :80 or :8080 or :65534 Allows range of :0 to :65535
 306                         //    (0-59999)         |(60000-64999)   |(65000-65499)    |(65500-65529)  |(65530-65535)
 307      $port_number       = '(:(([0-5]?[0-9]{1,4})|(6[0-4][0-9]{3})|(65[0-4][0-9]{2})|(655[0-2][0-9])|(6553[0-5])))' . $aOptions['p'];
 308  
 309                         // Path - Can be as simple as '/' or have multiple folders and filenames
 310      $path              = '(/((;)?(' . $unreserved . '|' . $escaped . '|' . '[:@&=+$,]' . ')+(/)?)*)' . $aOptions['f'];
 311  
 312                         // Query Section - Accepts ?var1=value1&var2=value2 or ?2393,1221 and much more
 313      $querystring       = '(\?(' . $reserved . '|' . $unreserved . '|' . $escaped . ')*)' . $aOptions['q'];
 314  
 315                         // Fragment Section - Accepts anchors such as #top
 316      $fragment          = '(\#(' . $reserved . '|' . $unreserved . '|' . $escaped . ')*)' . $aOptions['r'];
 317  
 318  
 319      // Building Regular Expression
 320      $regexp = '#^' . $scheme . $userinfo . $address . $port_number . $path . $querystring . $fragment . '$#i';
 321  
 322      // DEBUGGING - Uncomment Line Below To Display The Regular Expression Built
 323      // echo '<pre>' . htmlentities(wordwrap($regexp,70,"\n",1)) . '</pre>';
 324  
 325      // Running the regular expression
 326      if (preg_match( $regexp, $urladdr ))
 327      {
 328          return true; // The domain passed
 329      }
 330      else
 331      {
 332          return false; // The domain didn't pass the expression
 333      }
 334  
 335  } // END Function validateUrlSyntax()
 336  
 337  
 338  
 339  /**
 340   * About ValidateEmailSyntax():
 341   * This function uses the ValidateUrlSyntax() function to easily check the
 342   * syntax of an email address. It accepts the same options as ValidateURLSyntax
 343   * but defaults them for email addresses.
 344   *
 345   *
 346   * Usage:
 347   * <code>
 348   *  validateEmailSyntax( url_to_check[, options])
 349   * </code>
 350   *  url_to_check - string - The url to check
 351   *
 352   *  options - string - A optional string of options to set which parts of
 353   *          the url are required, optional, or not allowed. Each option
 354   *          must be followed by a "+" for required, "?" for optional, or
 355   *          "-" for not allowed. See ValidateUrlSyntax() docs for option list.
 356   *
 357   *  The default options are changed to:
 358   *      s-H-S-E+F-u+P-a+I-p-f-q-r-
 359   *
 360   *  This only allows an address of "name@domain".
 361   *
 362   * Examples:
 363   * <code>
 364   *  validateEmailSyntax('george@fakemail.com')
 365   *  validateEmailSyntax('mailto:george@fakemail.com', 's+')
 366   *  validateEmailSyntax('george@fakemail.com?subject=Hi%20George', 'q?')
 367   *  validateEmailSyntax('george@212.198.33.12', 'I?')
 368   * </code>
 369   *
 370   *
 371   * Author(s):
 372   *  Rod Apeldoorn - rod(at)canowhoopass(dot)com
 373   *
 374   *
 375   * Homepage:
 376   *  http://www.canowhoopass.com/
 377   *
 378   *
 379   * License:
 380   *  Copyright 2004 - Rod Apeldoorn
 381   *
 382   *  Released under same license as validateUrlSyntax(). For details, contact me.
 383   */
 384  
 385  function validateEmailSyntax( $emailaddr, $options="" ){
 386  
 387      // Check Options Parameter
 388      if (!preg_match( '/^([sHSEFuPaIpfqr][+?-])*$/', $options ))
 389      {
 390          trigger_error("Options attribute malformed", E_USER_ERROR);
 391      }
 392  
 393      // Set Options Array, set defaults if options are not specified
 394      // Scheme
 395      if (strpos( $options, 's') === false) $aOptions['s'] = '-';
 396      else $aOptions['s'] = substr( $options, strpos( $options, 's') + 1, 1);
 397      // http://
 398      if (strpos( $options, 'H') === false) $aOptions['H'] = '-';
 399      else $aOptions['H'] = substr( $options, strpos( $options, 'H') + 1, 1);
 400      // https:// (SSL)
 401      if (strpos( $options, 'S') === false) $aOptions['S'] = '-';
 402      else $aOptions['S'] = substr( $options, strpos( $options, 'S') + 1, 1);
 403      // mailto: (email)
 404      if (strpos( $options, 'E') === false) $aOptions['E'] = '?';
 405      else $aOptions['E'] = substr( $options, strpos( $options, 'E') + 1, 1);
 406      // ftp://
 407      if (strpos( $options, 'F') === false) $aOptions['F'] = '-';
 408      else $aOptions['F'] = substr( $options, strpos( $options, 'F') + 1, 1);
 409      // User section
 410      if (strpos( $options, 'u') === false) $aOptions['u'] = '+';
 411      else $aOptions['u'] = substr( $options, strpos( $options, 'u') + 1, 1);
 412      // Password in user section
 413      if (strpos( $options, 'P') === false) $aOptions['P'] = '-';
 414      else $aOptions['P'] = substr( $options, strpos( $options, 'P') + 1, 1);
 415      // Address Section
 416      if (strpos( $options, 'a') === false) $aOptions['a'] = '+';
 417      else $aOptions['a'] = substr( $options, strpos( $options, 'a') + 1, 1);
 418      // IP Address in address section
 419      if (strpos( $options, 'I') === false) $aOptions['I'] = '-';
 420      else $aOptions['I'] = substr( $options, strpos( $options, 'I') + 1, 1);
 421      // Port number
 422      if (strpos( $options, 'p') === false) $aOptions['p'] = '-';
 423      else $aOptions['p'] = substr( $options, strpos( $options, 'p') + 1, 1);
 424      // File Path
 425      if (strpos( $options, 'f') === false) $aOptions['f'] = '-';
 426      else $aOptions['f'] = substr( $options, strpos( $options, 'f') + 1, 1);
 427      // Query Section
 428      if (strpos( $options, 'q') === false) $aOptions['q'] = '-';
 429      else $aOptions['q'] = substr( $options, strpos( $options, 'q') + 1, 1);
 430      // Fragment (Anchor)
 431      if (strpos( $options, 'r') === false) $aOptions['r'] = '-';
 432      else $aOptions['r'] = substr( $options, strpos( $options, 'r') + 1, 1);
 433  
 434      // Generate options
 435      $newoptions = '';
 436      foreach($aOptions as $key => $value)
 437      {
 438          $newoptions .= $key . $value;
 439      }
 440  
 441      // DEBUGGING - Uncomment line below to display generated options
 442      // echo '<pre>' . $newoptions . '</pre>';
 443  
 444      // Send to validateUrlSyntax() and return result
 445      return validateUrlSyntax( $emailaddr, $newoptions);
 446  
 447  } // END Function validateEmailSyntax()
 448  
 449  
 450  
 451  /**
 452   * About ValidateFtpSyntax():
 453   * This function uses the ValidateUrlSyntax() function to easily check the
 454   * syntax of an FTP address. It accepts the same options as ValidateURLSyntax
 455   * but defaults them for FTP addresses.
 456   *
 457   *
 458   * Usage:
 459   * <code>
 460   *  validateFtpSyntax( url_to_check[, options])
 461   * </code>
 462   *  url_to_check - string - The url to check
 463   *
 464   *  options - string - A optional string of options to set which parts of
 465   *          the url are required, optional, or not allowed. Each option
 466   *          must be followed by a "+" for required, "?" for optional, or
 467   *          "-" for not allowed. See ValidateUrlSyntax() docs for option list.
 468   *
 469   *  The default options are changed to:
 470   *      s?H-S-E-F+u?P?a+I?p?f?q-r-
 471   *
 472   * Examples:
 473   * <code>
 474   *  validateFtpSyntax('ftp://netscape.com')
 475   *  validateFtpSyntax('moz:iesucks@netscape.com')
 476   *  validateFtpSyntax('ftp://netscape.com:2121/browsers/ns7/', 'u-')
 477   * </code>
 478   *
 479   * Author(s):
 480   *  Rod Apeldoorn - rod(at)canowhoopass(dot)com
 481   *
 482   *
 483   * Homepage:
 484   *  http://www.canowhoopass.com/
 485   *
 486   *
 487   * License:
 488   *  Copyright 2004 - Rod Apeldoorn
 489   *
 490   *  Released under same license as validateUrlSyntax(). For details, contact me.
 491   */
 492  
 493  function validateFtpSyntax( $ftpaddr, $options="" ){
 494  
 495      // Check Options Parameter
 496      if (!preg_match( '/^([sHSEFuPaIpfqr][+?-])*$/', $options ))
 497      {
 498          trigger_error("Options attribute malformed", E_USER_ERROR);
 499      }
 500  
 501      // Set Options Array, set defaults if options are not specified
 502      // Scheme
 503      if (strpos( $options, 's') === false) $aOptions['s'] = '?';
 504      else $aOptions['s'] = substr( $options, strpos( $options, 's') + 1, 1);
 505      // http://
 506      if (strpos( $options, 'H') === false) $aOptions['H'] = '-';
 507      else $aOptions['H'] = substr( $options, strpos( $options, 'H') + 1, 1);
 508      // https:// (SSL)
 509      if (strpos( $options, 'S') === false) $aOptions['S'] = '-';
 510      else $aOptions['S'] = substr( $options, strpos( $options, 'S') + 1, 1);
 511      // mailto: (email)
 512      if (strpos( $options, 'E') === false) $aOptions['E'] = '-';
 513      else $aOptions['E'] = substr( $options, strpos( $options, 'E') + 1, 1);
 514      // ftp://
 515      if (strpos( $options, 'F') === false) $aOptions['F'] = '+';
 516      else $aOptions['F'] = substr( $options, strpos( $options, 'F') + 1, 1);
 517      // User section
 518      if (strpos( $options, 'u') === false) $aOptions['u'] = '?';
 519      else $aOptions['u'] = substr( $options, strpos( $options, 'u') + 1, 1);
 520      // Password in user section
 521      if (strpos( $options, 'P') === false) $aOptions['P'] = '?';
 522      else $aOptions['P'] = substr( $options, strpos( $options, 'P') + 1, 1);
 523      // Address Section
 524      if (strpos( $options, 'a') === false) $aOptions['a'] = '+';
 525      else $aOptions['a'] = substr( $options, strpos( $options, 'a') + 1, 1);
 526      // IP Address in address section
 527      if (strpos( $options, 'I') === false) $aOptions['I'] = '?';
 528      else $aOptions['I'] = substr( $options, strpos( $options, 'I') + 1, 1);
 529      // Port number
 530      if (strpos( $options, 'p') === false) $aOptions['p'] = '?';
 531      else $aOptions['p'] = substr( $options, strpos( $options, 'p') + 1, 1);
 532      // File Path
 533      if (strpos( $options, 'f') === false) $aOptions['f'] = '?';
 534      else $aOptions['f'] = substr( $options, strpos( $options, 'f') + 1, 1);
 535      // Query Section
 536      if (strpos( $options, 'q') === false) $aOptions['q'] = '-';
 537      else $aOptions['q'] = substr( $options, strpos( $options, 'q') + 1, 1);
 538      // Fragment (Anchor)
 539      if (strpos( $options, 'r') === false) $aOptions['r'] = '-';
 540      else $aOptions['r'] = substr( $options, strpos( $options, 'r') + 1, 1);
 541  
 542      // Generate options
 543      $newoptions = '';
 544      foreach($aOptions as $key => $value)
 545      {
 546          $newoptions .= $key . $value;
 547      }
 548  
 549      // DEBUGGING - Uncomment line below to display generated options
 550      // echo '<pre>' . $newoptions . '</pre>';
 551  
 552      // Send to validateUrlSyntax() and return result
 553      return validateUrlSyntax( $ftpaddr, $newoptions);
 554  
 555  } // END Function validateFtpSyntax()