removed making search results highlighted -- messed up other parsing badly
[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 This is here in case we ever decide to use highlighting of searched
17 text. this does it for email addresses
18
19 if ($what && ($where == "BODY" || $where == "TEXT")) {
20 eregi ("([a-z]|[0-9]|_|\.|-)+\@([a-z]|[0-9]|_|-)+(\.([a-z]|[0-9]|_|-)+)*", $body, $regs);
21 $oldaddr = $regs[0];
22 if ($oldaddr) {
23 $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
24 $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
25 }
26 } else {
27 $body = eregi_replace ("([a-z]|[0-9]|_|\.|-)+\@([a-z]|[0-9]|_|-)+(\.([a-z]|[0-9]|_|-)+)*", "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
28 }
29 */
30 $body = eregi_replace ("([a-z]|[0-9]|_|\.|-)+\@([a-z]|[0-9]|_|-)+(\.([a-z]|[0-9]|_|-)+)*", "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
31 return $body;
32 }
33
34 function parseUrl ($body) {
35 #Possible ways a URL could finish.
36
37 $poss_ends=array(" ", "\n", "\r", "<", ">", ".\r", ".\n", ".&nbsp", "&nbsp", ")", "(","&quot");
38 $done=False;
39 while (!$done) {
40 #Look for when a URL starts
41 $url_tokens = array(
42 "http://",
43 "https://",
44 "ftp://",
45 "telnet://");
46 for($i = 0; $i < sizeof($url_tokens); $i++) {
47 if($where = strpos(strtolower($body), $url_tokens[$i], $start))
48 break;
49 }
50 //$where = strpos(strtolower($body),"http://",$start);
51 if ($where) {
52 # Find the end of that URL
53 reset($poss_ends); $end=0;
54 while (list($key, $val) = each($poss_ends)) {
55 $enda = strpos($body,$val,$where);
56 if ($end == 0) $end = $enda;
57 if ($enda < $end and $enda != 0) $end = $enda;
58 }
59 #Extract URL
60 $url = substr($body,$where,$end-$where);
61 #Replace URL with HyperLinked Url
62 if ($url != "") {
63 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
64 # $body = str_replace($url,$url_str,$body);
65 $body = replaceBlock($body,$url_str,$where,$end);
66 $start = strpos($body,"</a>",$where);
67 } else {
68 $start = $where + 7;
69 }
70 } else {
71 $done=true;
72 }
73 }
74
75 return $body;
76 }
77
78 ?>
79