2 /* URL Passing code to allow links from with in emails */
5 if (defined('url_parser_php'))
7 define('url_parser_php', true);
9 function replaceBlock (&$in, $replace, $start, $end) {
10 $begin = substr($in,0,$start);
11 $end = substr($in,$end,strlen($in)-$end);
12 $in = $begin.$replace.$end;
15 // Having this defined in just one spot could help when changes need
16 // to be made to the pattern
17 // Make sure that the expression is evaluated case insensitively
19 // Here's pretty sophisticated IP matching:
20 // $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
21 // $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
24 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
25 $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
26 $Host_RegExp_Match = '(' . $IP_RegExp_Match .
27 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
28 $Email_RegExp_Match = '[0-9a-z]([-_.]?[0-9a-z])*(%' . $Host_RegExp_Match .
29 ')?@' . $Host_RegExp_Match;
31 function parseEmail (&$body) {
32 global $color, $Email_RegExp_Match;
33 $Size = strlen($body);
36 This is here in case we ever decide to use highlighting of searched
37 text. this does it for email addresses
39 if ($what && ($where == "BODY" || $where == "TEXT")) {
40 eregi ($Email_RegExp_Match, $body, $regs);
43 $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
44 $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
47 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
51 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
53 // If there are any changes, it'll just get bigger.
54 if ($Size != strlen($body))
60 // We don't want to re-initialize this stuff for every line. Save work
61 // and just do it once here.
62 global $url_parser_url_tokens;
63 $url_parser_url_tokens = array(
67 'telnet:', // Special case -- doesn't need the slashes
71 global $url_parser_poss_ends;
72 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
73 '. ', ' ', ')', '(', '"', '<', '>', '.<',
74 ']', '[', '{', '}', "\240");
77 function parseUrl (&$body)
79 global $url_parser_poss_ends, $url_parser_url_tokens;;
81 $target_pos = strlen($body);
83 while ($start != $target_pos)
87 // Find the first token to replace
88 foreach ($url_parser_url_tokens as $the_token)
90 $pos = strpos(strtolower($body), $the_token, $start);
91 if (is_int($pos) && $pos < $target_pos)
94 $target_token = $the_token;
98 // Look for email addresses between $start and $target_pos
99 $check_str = substr($body, $start, $target_pos);
101 if (parseEmail($check_str))
103 replaceBlock($body, $check_str, $start, $target_pos);
104 $target_pos = strlen($check_str) +
$start;
107 // If there was a token to replace, replace it
108 if ($target_token != '')
110 // Find the end of the URL
112 foreach ($url_parser_poss_ends as $key => $val)
114 $enda = strpos($body,$val,$target_pos);
115 if (is_int($enda) && $enda < $end)
120 $url = substr($body, $target_pos, $end-$target_pos);
122 // Replace URL with HyperLinked Url, requires 1 char in link
123 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);
131 // Not quite a valid link, skip ahead to next chance
132 $target_pos +
= strlen($target_token);
137 $start = $target_pos;
138 $target_pos = strlen($body);