| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * url_parser.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This code provides various string manipulation functions that are |
| 10 | * used by the rest of the Squirrelmail code. |
| 11 | * |
| 12 | * $Id$ |
| 13 | */ |
| 14 | |
| 15 | function replaceBlock (&$in, $replace, $start, $end) { |
| 16 | $begin = substr($in,0,$start); |
| 17 | $end = substr($in,$end,strlen($in)-$end); |
| 18 | $in = $begin.$replace.$end; |
| 19 | } |
| 20 | |
| 21 | /* Having this defined in just one spot could help when changes need |
| 22 | * to be made to the pattern |
| 23 | * Make sure that the expression is evaluated case insensitively |
| 24 | * |
| 25 | * Here's pretty sophisticated IP matching: |
| 26 | * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})'; |
| 27 | * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?'; |
| 28 | */ |
| 29 | /* Here's enough: */ |
| 30 | global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match; |
| 31 | $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?'; |
| 32 | $Host_RegExp_Match = '(' . $IP_RegExp_Match . |
| 33 | '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)'; |
| 34 | $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match . |
| 35 | ')?@' . $Host_RegExp_Match; |
| 36 | |
| 37 | function parseEmail (&$body) { |
| 38 | global $color, $Email_RegExp_Match; |
| 39 | $sbody = $body; |
| 40 | $addresses = array(); |
| 41 | |
| 42 | /* Find all the email addresses in the body */ |
| 43 | while(eregi($Email_RegExp_Match, $sbody, $regs)) { |
| 44 | $addresses[$regs[0]] = $regs[0]; |
| 45 | $start = strpos($sbody, $regs[0]) + strlen($regs[0]); |
| 46 | $sbody = substr($sbody, $start); |
| 47 | } |
| 48 | /* Replace each email address with a compose URL */ |
| 49 | foreach ($addresses as $email) { |
| 50 | $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email); |
| 51 | $body = str_replace($email, $comp_uri, $body); |
| 52 | } |
| 53 | /* Return number of unique addresses found */ |
| 54 | return count($addresses); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /* We don't want to re-initialize this stuff for every line. Save work |
| 59 | * and just do it once here. |
| 60 | */ |
| 61 | global $url_parser_url_tokens; |
| 62 | $url_parser_url_tokens = array( |
| 63 | 'http://', |
| 64 | 'https://', |
| 65 | 'ftp://', |
| 66 | 'telnet:', // Special case -- doesn't need the slashes |
| 67 | 'gopher://', |
| 68 | 'news://'); |
| 69 | |
| 70 | global $url_parser_poss_ends; |
| 71 | $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n", |
| 72 | '. ', ' ', ')', '(', '"', '<', '>', '.<', |
| 73 | ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r"); |
| 74 | |
| 75 | |
| 76 | function parseUrl (&$body) { |
| 77 | global $url_parser_poss_ends, $url_parser_url_tokens;; |
| 78 | $start = 0; |
| 79 | $blength = strlen($body); |
| 80 | |
| 81 | while ($start != $blength) { |
| 82 | $target_token = ''; |
| 83 | $target_pos = $blength; |
| 84 | |
| 85 | /* Find the first token to replace */ |
| 86 | foreach ($url_parser_url_tokens as $the_token) { |
| 87 | $pos = strpos(strtolower($body), $the_token, $start); |
| 88 | if (is_int($pos) && $pos < $blength) { |
| 89 | $target_pos = $pos; |
| 90 | $target_token = $the_token; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /* Look for email addresses between $start and $target_pos */ |
| 95 | $check_str = substr($body, $start, $target_pos-$start); |
| 96 | |
| 97 | if (parseEmail($check_str)) { |
| 98 | replaceBlock($body, $check_str, $start, $target_pos); |
| 99 | $blength = strlen($body); |
| 100 | $target_pos = strlen($check_str) + $start; |
| 101 | } |
| 102 | |
| 103 | /* If there was a token to replace, replace it */ |
| 104 | if ($target_token != '') { |
| 105 | /* Find the end of the URL */ |
| 106 | $end = $blength; |
| 107 | foreach ($url_parser_poss_ends as $val) { |
| 108 | $enda = strpos($body, $val, $target_pos); |
| 109 | if (is_int($enda) && $enda < $end) { |
| 110 | $end = $enda; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* Extract URL */ |
| 115 | $url = substr($body, $target_pos, $end-$target_pos); |
| 116 | |
| 117 | /* Needed since lines are not passed with \n or \r */ |
| 118 | while ( ereg("[,\.]$", $url) ) { |
| 119 | $url = substr( $url, 0, -1 ); |
| 120 | $end--; |
| 121 | } |
| 122 | |
| 123 | /* Replace URL with HyperLinked Url, requires 1 char in link */ |
| 124 | if ($url != '' && $url != $target_token) { |
| 125 | $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>"; |
| 126 | replaceBlock($body,$url_str,$target_pos,$end); |
| 127 | $target_pos += strlen($url_str); |
| 128 | } |
| 129 | else { |
| 130 | // Not quite a valid link, skip ahead to next chance |
| 131 | $target_pos += strlen($target_token); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* Move forward */ |
| 136 | $start = $target_pos; |
| 137 | $blength = strlen($body); |
| 138 | } |
| 139 | } |
| 140 | ?> |