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