59177427 |
1 | <?php |
43fcef5c |
2 | /* URL Passing code to allow links from with in emails */ |
3 | |
4 | $url_parser_php = true; |
5 | |
6 | function replaceBlock ($in, $replace, $start, $end) { |
7 | $begin = substr($in,0,$start); |
8 | $end = substr($in,$end,strlen($in)-$end); |
9 | $ret = $begin.$replace.$end; |
10 | return $ret; |
11 | } |
12 | |
175e7218 |
13 | function parseEmail ($body) { |
9297917e |
14 | global $color; |
e2ef6f4b |
15 | |
8f7163e7 |
16 | // Having this defined in just one spot could help when changes need |
17 | // to be made to the pattern |
18 | // Make sure that the expression is evaluated case insensitively |
19 | // |
20 | // Here's pretty sophisticated IP matching: |
21 | // $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})'; |
22 | // $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?'; |
23 | // |
24 | // Here's enough: |
25 | $IPMatch = '\[?[0-9]{1,3}(\.[0-9]{1,3}){3}\]?'; |
26 | $Host = '(' . $IPMatch . '|[0-9a-z]([-.]?[0-9a-z])*\.[a-wyz][a-z](g|l|m|pa|t|u|v)?)'; |
27 | $Expression = '[0-9a-z]([-_.]?[0-9a-z])*(%' . $Host . ')?@' . $Host; |
e2ef6f4b |
28 | |
9297917e |
29 | /* |
30 | This is here in case we ever decide to use highlighting of searched |
31 | text. this does it for email addresses |
32 | |
33 | if ($what && ($where == "BODY" || $where == "TEXT")) { |
e2ef6f4b |
34 | eregi ($Expression, $body, $regs); |
9297917e |
35 | $oldaddr = $regs[0]; |
36 | if ($oldaddr) { |
37 | $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr); |
38 | $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body); |
39 | } |
40 | } else { |
e2ef6f4b |
41 | $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body); |
9297917e |
42 | } |
43 | */ |
8f7163e7 |
44 | |
e2ef6f4b |
45 | $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body); |
175e7218 |
46 | return $body; |
47 | } |
43fcef5c |
48 | |
43fcef5c |
49 | |
8f7163e7 |
50 | function parseUrl ($body) |
51 | { |
52 | $url_tokens = array( |
53 | 'http://', |
54 | 'https://', |
55 | 'ftp://', |
56 | 'telnet:', // Special case -- doesn't need the slashes |
57 | 'gopher://', |
58 | 'news://'); |
59 | |
60 | $poss_ends = array(' ', '\n', '\r', '<', '>', '.\r', '.\n', '. ', |
61 | ' ', ')', '(', '"', '<', '>', '.<', ']', '[', '{', |
62 | '}', "\240"); |
63 | |
64 | $start = 0; |
65 | $target_pos = strlen($body); |
66 | |
67 | while ($start != $target_pos) |
e2ef6f4b |
68 | { |
8f7163e7 |
69 | $target_token = ''; |
70 | |
71 | // Find the first token to replace |
72 | foreach ($url_tokens as $the_token) |
73 | { |
74 | $pos = strpos(strtolower($body), $the_token, $start); |
75 | if (is_int($pos) && $pos < $target_pos) |
76 | { |
77 | $target_pos = $pos; |
78 | $target_token = $the_token; |
79 | } |
80 | } |
81 | |
82 | // Look for email addresses between $start and $target_pos |
83 | $check_str = substr($body, $start, $target_pos); |
84 | $new_str = parseEmail($check_str); |
85 | |
86 | if ($check_str != $new_str) |
87 | { |
88 | $body = replaceBlock($body, $new_str, $start, $target_pos); |
89 | $target_pos = strlen($new_str) + $start; |
90 | } |
e2ef6f4b |
91 | |
8f7163e7 |
92 | // If there was a token to replace, replace it |
93 | if ($target_token != '') |
94 | { |
95 | // Find the end of the URL |
96 | $end=strlen($body); |
97 | foreach ($poss_ends as $key => $val) |
98 | { |
99 | $enda = strpos($body,$val,$target_pos); |
100 | if (is_int($enda) && $enda < $end) |
101 | $end = $enda; |
102 | } |
103 | |
104 | // Extract URL |
105 | $url = substr($body, $target_pos, $end-$target_pos); |
106 | |
107 | // Replace URL with HyperLinked Url, requires 1 char in link |
108 | if ($url != '' && $url != $target_token) |
109 | { |
110 | $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>"; |
111 | $body = replaceBlock($body,$url_str,$target_pos,$end); |
112 | $target_pos += strlen($url_str); |
113 | } |
114 | else |
115 | { |
116 | // Not quite a valid link, skip ahead to next chance |
117 | $target_pos += strlen($target_token); |
118 | } |
119 | } |
120 | |
121 | // Move forward |
122 | $start = $target_pos; |
123 | $target_pos = strlen($body); |
124 | } |
125 | |
126 | return $body; |
43fcef5c |
127 | } |
8f7163e7 |
128 | |
43fcef5c |
129 | ?> |