|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00170 function validateUrlSyntax( $urladdr, $options="" ){ 00171 00172 // Force Options parameter to be lower case 00173 // DISABLED PERMAMENTLY - OK to remove from code 00174 // $options = strtolower($options); 00175 00176 // Check Options Parameter 00177 if (!preg_match( '/^([sHSEFuPaIpfqr][+?-])*$/', $options )) 00178 { 00179 trigger_error("Options attribute malformed", E_USER_ERROR); 00180 } 00181 00182 // Set Options Array, set defaults if options are not specified 00183 // Scheme 00184 if (strpos( $options, 's') === false) $aOptions['s'] = '?'; 00185 else $aOptions['s'] = substr( $options, strpos( $options, 's') + 1, 1); 00186 // http:// 00187 if (strpos( $options, 'H') === false) $aOptions['H'] = '?'; 00188 else $aOptions['H'] = substr( $options, strpos( $options, 'H') + 1, 1); 00189 // https:// (SSL) 00190 if (strpos( $options, 'S') === false) $aOptions['S'] = '?'; 00191 else $aOptions['S'] = substr( $options, strpos( $options, 'S') + 1, 1); 00192 // mailto: (email) 00193 if (strpos( $options, 'E') === false) $aOptions['E'] = '-'; 00194 else $aOptions['E'] = substr( $options, strpos( $options, 'E') + 1, 1); 00195 // ftp:// 00196 if (strpos( $options, 'F') === false) $aOptions['F'] = '-'; 00197 else $aOptions['F'] = substr( $options, strpos( $options, 'F') + 1, 1); 00198 // User section 00199 if (strpos( $options, 'u') === false) $aOptions['u'] = '?'; 00200 else $aOptions['u'] = substr( $options, strpos( $options, 'u') + 1, 1); 00201 // Password in user section 00202 if (strpos( $options, 'P') === false) $aOptions['P'] = '?'; 00203 else $aOptions['P'] = substr( $options, strpos( $options, 'P') + 1, 1); 00204 // Address Section 00205 if (strpos( $options, 'a') === false) $aOptions['a'] = '+'; 00206 else $aOptions['a'] = substr( $options, strpos( $options, 'a') + 1, 1); 00207 // IP Address in address section 00208 if (strpos( $options, 'I') === false) $aOptions['I'] = '?'; 00209 else $aOptions['I'] = substr( $options, strpos( $options, 'I') + 1, 1); 00210 // Port number 00211 if (strpos( $options, 'p') === false) $aOptions['p'] = '?'; 00212 else $aOptions['p'] = substr( $options, strpos( $options, 'p') + 1, 1); 00213 // File Path 00214 if (strpos( $options, 'f') === false) $aOptions['f'] = '?'; 00215 else $aOptions['f'] = substr( $options, strpos( $options, 'f') + 1, 1); 00216 // Query Section 00217 if (strpos( $options, 'q') === false) $aOptions['q'] = '?'; 00218 else $aOptions['q'] = substr( $options, strpos( $options, 'q') + 1, 1); 00219 // Fragment (Anchor) 00220 if (strpos( $options, 'r') === false) $aOptions['r'] = '?'; 00221 else $aOptions['r'] = substr( $options, strpos( $options, 'r') + 1, 1); 00222 00223 00224 // Loop through options array, to search for and replace "-" to "{0}" and "+" to "" 00225 foreach($aOptions as $key => $value) 00226 { 00227 if ($value == '-') 00228 { 00229 $aOptions[$key] = '{0}'; 00230 } 00231 if ($value == '+') 00232 { 00233 $aOptions[$key] = ''; 00234 } 00235 } 00236 00237 // DEBUGGING - Unescape following line to display to screen current option values 00238 // echo '<pre>'; print_r($aOptions); echo '</pre>'; 00239 00240 00241 // Preset Allowed Characters 00242 $alphanum = '[a-zA-Z0-9]'; // Alpha Numeric 00243 $unreserved = '[a-zA-Z0-9_.!~*' . '\'' . '()-]'; 00244 $escaped = '(%[0-9a-fA-F]{2})'; // Escape sequence - In Hex - %6d would be a 'm' 00245 $reserved = '[;/?:@&=+$,]'; // Special characters in the URI 00246 00247 // Beginning Regular Expression 00248 // Scheme - Allows for 'http://', 'https://', 'mailto:', or 'ftp://' 00249 $scheme = '('; 00250 if ($aOptions['H'] === '') { $scheme .= 'http://'; } 00251 elseif ($aOptions['S'] === '') { $scheme .= 'https://'; } 00252 elseif ($aOptions['E'] === '') { $scheme .= 'mailto:'; } 00253 elseif ($aOptions['F'] === '') { $scheme .= 'ftp://'; } 00254 else 00255 { 00256 if ($aOptions['H'] === '?') { $scheme .= '|(http://)'; } 00257 if ($aOptions['S'] === '?') { $scheme .= '|(https://)'; } 00258 if ($aOptions['E'] === '?') { $scheme .= '|(mailto:)'; } 00259 if ($aOptions['F'] === '?') { $scheme .= '|(ftp://)'; } 00260 $scheme = str_replace('(|', '(', $scheme); // fix first pipe 00261 } 00262 $scheme .= ')' . $aOptions['s']; 00263 // End setting scheme 00264 00265 // User Info - Allows for 'username@' or 'username:password@'. Note: contrary to rfc, I removed ':' from username section, allowing it only in password. 00266 // /---------------- Username -----------------------\ /-------------------------------- Password ------------------------------\ 00267 $userinfo = '((' . $unreserved . '|' . $escaped . '|[;&=+$,]' . ')+(:(' . $unreserved . '|' . $escaped . '|[;:&=+$,]' . ')+)' . $aOptions['P'] . '@)' . $aOptions['u']; 00268 00269 // IP ADDRESS - Allows 0.0.0.0 to 255.255.255.255 00270 $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])))'; 00271 00272 // 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 00273 $domain_tertiary = '(' . $alphanum . '(([a-zA-Z0-9-]{0,62})' . $alphanum . ')?\.)*'; 00274 00275 /* MDL-9295 - take out domain_secondary here and below, so that URLs like http://localhost/ and lan addresses like http://host/ are accepted. 00276 // Second Level Domain - Required - First and last characters must be Alpha-numeric. Hyphens are allowed inside. 00277 $domain_secondary = '(' . $alphanum . '(([a-zA-Z0-9-]{0,62})' . $alphanum . ')?\.)'; 00278 */ 00279 00280 // we want more relaxed URLs in Moodle: MDL-11462 00281 // Top Level Domain - First character must be Alpha. Last character must be AlphaNumeric. Hyphens are allowed inside. 00282 $domain_toplevel = '([a-zA-Z](([a-zA-Z0-9-]*)[a-zA-Z0-9])?)'; 00283 /* // Top Level Domain - Required - Domain List Current As Of December 2004. Use above escaped line to be forgiving of possible future TLD's 00284 $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)'; 00285 */ 00286 00287 // Address can be IP address or Domain 00288 if ($aOptions['I'] === '{0}') { // IP Address Not Allowed 00289 $address = '(' . $domain_tertiary . /* MDL-9295 $domain_secondary . */ $domain_toplevel . ')'; 00290 } elseif ($aOptions['I'] === '') { // IP Address Required 00291 $address = '(' . $ipaddress . ')'; 00292 } else { // IP Address Optional 00293 $address = '((' . $ipaddress . ')|(' . $domain_tertiary . /* MDL-9295 $domain_secondary . */ $domain_toplevel . '))'; 00294 } 00295 $address = $address . $aOptions['a']; 00296 00297 // Port Number - :80 or :8080 or :65534 Allows range of :0 to :65535 00298 // (0-59999) |(60000-64999) |(65000-65499) |(65500-65529) |(65530-65535) 00299 $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']; 00300 00301 // Path - Can be as simple as '/' or have multiple folders and filenames 00302 $path = '(/((;)?(' . $unreserved . '|' . $escaped . '|' . '[:@&=+$,]' . ')+(/)?)*)' . $aOptions['f']; 00303 00304 // Query Section - Accepts ?var1=value1&var2=value2 or ?2393,1221 and much more 00305 $querystring = '(\?(' . $reserved . '|' . $unreserved . '|' . $escaped . ')*)' . $aOptions['q']; 00306 00307 // Fragment Section - Accepts anchors such as #top 00308 $fragment = '(\#(' . $reserved . '|' . $unreserved . '|' . $escaped . ')*)' . $aOptions['r']; 00309 00310 00311 // Building Regular Expression 00312 $regexp = '#^' . $scheme . $userinfo . $address . $port_number . $path . $querystring . $fragment . '$#i'; 00313 00314 // DEBUGGING - Uncomment Line Below To Display The Regular Expression Built 00315 // echo '<pre>' . htmlentities(wordwrap($regexp,70,"\n",1)) . '</pre>'; 00316 00317 // Running the regular expression 00318 if (preg_match( $regexp, $urladdr )) 00319 { 00320 return true; // The domain passed 00321 } 00322 else 00323 { 00324 return false; // The domain didn't pass the expression 00325 } 00326 00327 } // END Function validateUrlSyntax() 00328 00329 00330 00377 function validateEmailSyntax( $emailaddr, $options="" ){ 00378 00379 // Check Options Parameter 00380 if (!preg_match( '/^([sHSEFuPaIpfqr][+?-])*$/', $options )) 00381 { 00382 trigger_error("Options attribute malformed", E_USER_ERROR); 00383 } 00384 00385 // Set Options Array, set defaults if options are not specified 00386 // Scheme 00387 if (strpos( $options, 's') === false) $aOptions['s'] = '-'; 00388 else $aOptions['s'] = substr( $options, strpos( $options, 's') + 1, 1); 00389 // http:// 00390 if (strpos( $options, 'H') === false) $aOptions['H'] = '-'; 00391 else $aOptions['H'] = substr( $options, strpos( $options, 'H') + 1, 1); 00392 // https:// (SSL) 00393 if (strpos( $options, 'S') === false) $aOptions['S'] = '-'; 00394 else $aOptions['S'] = substr( $options, strpos( $options, 'S') + 1, 1); 00395 // mailto: (email) 00396 if (strpos( $options, 'E') === false) $aOptions['E'] = '?'; 00397 else $aOptions['E'] = substr( $options, strpos( $options, 'E') + 1, 1); 00398 // ftp:// 00399 if (strpos( $options, 'F') === false) $aOptions['F'] = '-'; 00400 else $aOptions['F'] = substr( $options, strpos( $options, 'F') + 1, 1); 00401 // User section 00402 if (strpos( $options, 'u') === false) $aOptions['u'] = '+'; 00403 else $aOptions['u'] = substr( $options, strpos( $options, 'u') + 1, 1); 00404 // Password in user section 00405 if (strpos( $options, 'P') === false) $aOptions['P'] = '-'; 00406 else $aOptions['P'] = substr( $options, strpos( $options, 'P') + 1, 1); 00407 // Address Section 00408 if (strpos( $options, 'a') === false) $aOptions['a'] = '+'; 00409 else $aOptions['a'] = substr( $options, strpos( $options, 'a') + 1, 1); 00410 // IP Address in address section 00411 if (strpos( $options, 'I') === false) $aOptions['I'] = '-'; 00412 else $aOptions['I'] = substr( $options, strpos( $options, 'I') + 1, 1); 00413 // Port number 00414 if (strpos( $options, 'p') === false) $aOptions['p'] = '-'; 00415 else $aOptions['p'] = substr( $options, strpos( $options, 'p') + 1, 1); 00416 // File Path 00417 if (strpos( $options, 'f') === false) $aOptions['f'] = '-'; 00418 else $aOptions['f'] = substr( $options, strpos( $options, 'f') + 1, 1); 00419 // Query Section 00420 if (strpos( $options, 'q') === false) $aOptions['q'] = '-'; 00421 else $aOptions['q'] = substr( $options, strpos( $options, 'q') + 1, 1); 00422 // Fragment (Anchor) 00423 if (strpos( $options, 'r') === false) $aOptions['r'] = '-'; 00424 else $aOptions['r'] = substr( $options, strpos( $options, 'r') + 1, 1); 00425 00426 // Generate options 00427 $newoptions = ''; 00428 foreach($aOptions as $key => $value) 00429 { 00430 $newoptions .= $key . $value; 00431 } 00432 00433 // DEBUGGING - Uncomment line below to display generated options 00434 // echo '<pre>' . $newoptions . '</pre>'; 00435 00436 // Send to validateUrlSyntax() and return result 00437 return validateUrlSyntax( $emailaddr, $newoptions); 00438 00439 } // END Function validateEmailSyntax() 00440 00441 00442 00485 function validateFtpSyntax( $ftpaddr, $options="" ){ 00486 00487 // Check Options Parameter 00488 if (!preg_match( '/^([sHSEFuPaIpfqr][+?-])*$/', $options )) 00489 { 00490 trigger_error("Options attribute malformed", E_USER_ERROR); 00491 } 00492 00493 // Set Options Array, set defaults if options are not specified 00494 // Scheme 00495 if (strpos( $options, 's') === false) $aOptions['s'] = '?'; 00496 else $aOptions['s'] = substr( $options, strpos( $options, 's') + 1, 1); 00497 // http:// 00498 if (strpos( $options, 'H') === false) $aOptions['H'] = '-'; 00499 else $aOptions['H'] = substr( $options, strpos( $options, 'H') + 1, 1); 00500 // https:// (SSL) 00501 if (strpos( $options, 'S') === false) $aOptions['S'] = '-'; 00502 else $aOptions['S'] = substr( $options, strpos( $options, 'S') + 1, 1); 00503 // mailto: (email) 00504 if (strpos( $options, 'E') === false) $aOptions['E'] = '-'; 00505 else $aOptions['E'] = substr( $options, strpos( $options, 'E') + 1, 1); 00506 // ftp:// 00507 if (strpos( $options, 'F') === false) $aOptions['F'] = '+'; 00508 else $aOptions['F'] = substr( $options, strpos( $options, 'F') + 1, 1); 00509 // User section 00510 if (strpos( $options, 'u') === false) $aOptions['u'] = '?'; 00511 else $aOptions['u'] = substr( $options, strpos( $options, 'u') + 1, 1); 00512 // Password in user section 00513 if (strpos( $options, 'P') === false) $aOptions['P'] = '?'; 00514 else $aOptions['P'] = substr( $options, strpos( $options, 'P') + 1, 1); 00515 // Address Section 00516 if (strpos( $options, 'a') === false) $aOptions['a'] = '+'; 00517 else $aOptions['a'] = substr( $options, strpos( $options, 'a') + 1, 1); 00518 // IP Address in address section 00519 if (strpos( $options, 'I') === false) $aOptions['I'] = '?'; 00520 else $aOptions['I'] = substr( $options, strpos( $options, 'I') + 1, 1); 00521 // Port number 00522 if (strpos( $options, 'p') === false) $aOptions['p'] = '?'; 00523 else $aOptions['p'] = substr( $options, strpos( $options, 'p') + 1, 1); 00524 // File Path 00525 if (strpos( $options, 'f') === false) $aOptions['f'] = '?'; 00526 else $aOptions['f'] = substr( $options, strpos( $options, 'f') + 1, 1); 00527 // Query Section 00528 if (strpos( $options, 'q') === false) $aOptions['q'] = '-'; 00529 else $aOptions['q'] = substr( $options, strpos( $options, 'q') + 1, 1); 00530 // Fragment (Anchor) 00531 if (strpos( $options, 'r') === false) $aOptions['r'] = '-'; 00532 else $aOptions['r'] = substr( $options, strpos( $options, 'r') + 1, 1); 00533 00534 // Generate options 00535 $newoptions = ''; 00536 foreach($aOptions as $key => $value) 00537 { 00538 $newoptions .= $key . $value; 00539 } 00540 00541 // DEBUGGING - Uncomment line below to display generated options 00542 // echo '<pre>' . $newoptions . '</pre>'; 00543 00544 // Send to validateUrlSyntax() and return result 00545 return validateUrlSyntax( $ftpaddr, $newoptions); 00546 00547 } // END Function validateFtpSyntax()