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 | |
16 | // Changed the expression to the one in abook_take |
17 | // This works very well, especially it looks like you might have |
18 | // three instances of it below. Having it defined in |
19 | // just one spot could help when you need to change it. |
20 | $Expression = "[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-wyz][a-z](g|l|m|pa|t|u|v)?"; |
21 | |
9297917e |
22 | /* |
23 | This is here in case we ever decide to use highlighting of searched |
24 | text. this does it for email addresses |
25 | |
26 | if ($what && ($where == "BODY" || $where == "TEXT")) { |
e2ef6f4b |
27 | // Use the $Expression |
28 | eregi ($Expression, $body, $regs); |
9297917e |
29 | $oldaddr = $regs[0]; |
30 | if ($oldaddr) { |
31 | $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr); |
32 | $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body); |
33 | } |
34 | } else { |
e2ef6f4b |
35 | // Use the $Expression |
36 | $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body); |
9297917e |
37 | } |
38 | */ |
e2ef6f4b |
39 | // Use the $Expression |
40 | $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body); |
175e7218 |
41 | return $body; |
42 | } |
e2ef6f4b |
43 | |
43fcef5c |
44 | function parseUrl ($body) { |
45 | #Possible ways a URL could finish. |
46 | |
e2ef6f4b |
47 | // Removed "--" since it could be part of a URL |
612eda0a |
48 | $poss_ends=array(" ", "\n", "\r", "<", ">", ".\r", ".\n", ". ", " ", ")", "(", |
e2ef6f4b |
49 | """, "<", ">", ".<", "]", "[", "{", "}"); |
43fcef5c |
50 | $done=False; |
51 | while (!$done) { |
52 | #Look for when a URL starts |
e2ef6f4b |
53 | // Added gopher, news. Modified telnet. |
2b3db0f2 |
54 | $url_tokens = array( |
55 | "http://", |
56 | "https://", |
57 | "ftp://", |
e2ef6f4b |
58 | "telnet:", // Special case -- doesn't need the slashes |
59 | "gopher://", |
60 | "news://"); |
2b3db0f2 |
61 | for($i = 0; $i < sizeof($url_tokens); $i++) { |
e2ef6f4b |
62 | // Removed the use of "^^" -- it is unneeded |
63 | if(is_int($where = strpos(strtolower($body), $url_tokens[$i], $start))) |
2b3db0f2 |
64 | break; |
65 | } |
e2ef6f4b |
66 | // Look between $start and $where for email links |
67 | $check_str = substr($body, $start, $where); |
68 | $new_str = parseEmail($check_str); |
69 | |
70 | if ($check_str != $new_str) |
71 | { |
72 | $body = replaceBlock($body, $new_str, $start, $where); |
73 | $where = strlen($new_str) + $start; |
74 | } |
75 | |
2b3db0f2 |
76 | //$where = strpos(strtolower($body),"http://",$start); |
e2ef6f4b |
77 | // Fixed this to work with $i instead of $where |
78 | if ($i < sizeof($url_tokens)) { |
79 | // Removed the "^^" so I removed the next line |
80 | //$where = $where - 2; // because we added the ^^ at the begining |
43fcef5c |
81 | # Find the end of that URL |
82 | reset($poss_ends); $end=0; |
83 | while (list($key, $val) = each($poss_ends)) { |
84 | $enda = strpos($body,$val,$where); |
85 | if ($end == 0) $end = $enda; |
86 | if ($enda < $end and $enda != 0) $end = $enda; |
87 | } |
612eda0a |
88 | if (!$end) $end = strlen($body); |
43fcef5c |
89 | #Extract URL |
90 | $url = substr($body,$where,$end-$where); |
91 | #Replace URL with HyperLinked Url |
e2ef6f4b |
92 | // Now this code doesn't simply match on url_tokens |
93 | // It will need some more text. This is good. |
94 | if ($url != "" && $url != $url_tokens[$i]) { |
43fcef5c |
95 | $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>"; |
96 | # $body = str_replace($url,$url_str,$body); |
14a1d0be |
97 | # echo "$where, $end<br>"; |
43fcef5c |
98 | $body = replaceBlock($body,$url_str,$where,$end); |
e2ef6f4b |
99 | // Removed unnecessary strpos call. Searching |
100 | // a string takes longer than just figuring out |
101 | // the length. |
102 | // $start = strpos($body,"</a>",$where); |
103 | $start = $where + strlen($url_str); |
43fcef5c |
104 | } else { |
e2ef6f4b |
105 | // Proper length increment -- Don't just assume 7 |
106 | $start = $where + strlen($url_tokens[$i]); |
43fcef5c |
107 | } |
108 | } else { |
109 | $done=true; |
110 | } |
111 | } |
112 | |
e2ef6f4b |
113 | // Look after $start for more email links. |
114 | $check_str = substr($body, $start); |
115 | $new_str = parseEmail($check_str); |
116 | |
117 | if ($check_str != $new_str) |
118 | { |
119 | $body = replaceBlock($body, $new_str, $start, strlen($body)); |
120 | } |
121 | |
43fcef5c |
122 | return $body; |
123 | } |
124 | |
125 | ?> |