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