Changed version number
[squirrelmail.git] / functions / url_parser.php
1 <?php
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
13 function parseEmail ($body) {
14 global $color;
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
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")) {
27 // Use the $Expression
28 eregi ($Expression, $body, $regs);
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 {
35 // Use the $Expression
36 $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
37 }
38 */
39 // Use the $Expression
40 $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
41 return $body;
42 }
43
44 function parseUrl ($body) {
45 #Possible ways a URL could finish.
46
47 // Removed "--" since it could be part of a URL
48 $poss_ends=array(" ", "\n", "\r", "<", ">", ".\r", ".\n", ".&nbsp;", "&nbsp;", ")", "(",
49 "&quot;", "&lt;", "&gt;", ".<", "]", "[", "{", "}");
50 $done=False;
51 while (!$done) {
52 #Look for when a URL starts
53 // Added gopher, news. Modified telnet.
54 $url_tokens = array(
55 "http://",
56 "https://",
57 "ftp://",
58 "telnet:", // Special case -- doesn't need the slashes
59 "gopher://",
60 "news://");
61 for($i = 0; $i < sizeof($url_tokens); $i++) {
62 // Removed the use of "^^" -- it is unneeded
63 if(is_int($where = strpos(strtolower($body), $url_tokens[$i], $start)))
64 break;
65 }
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
76 //$where = strpos(strtolower($body),"http://",$start);
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
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 }
88 if (!$end) $end = strlen($body);
89 #Extract URL
90 $url = substr($body,$where,$end-$where);
91 #Replace URL with HyperLinked Url
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]) {
95 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
96 # $body = str_replace($url,$url_str,$body);
97 # echo "$where, $end<br>";
98 $body = replaceBlock($body,$url_str,$where,$end);
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);
104 } else {
105 // Proper length increment -- Don't just assume 7
106 $start = $where + strlen($url_tokens[$i]);
107 }
108 } else {
109 $done=true;
110 }
111 }
112
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
122 return $body;
123 }
124
125 ?>