Added slovak
[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
f435778e 5 if (defined('url_parser_php'))
6 return;
7 define('url_parser_php', true);
43fcef5c 8
20a60f89 9 function replaceBlock (&$in, $replace, $start, $end) {
43fcef5c 10 $begin = substr($in,0,$start);
11 $end = substr($in,$end,strlen($in)-$end);
20a60f89 12 $in = $begin.$replace.$end;
43fcef5c 13 }
14
20a60f89 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:
f435778e 24 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
20a60f89 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
9eea179c 31 function parseEmail (&$body) {
20a60f89 32 global $color, $Email_RegExp_Match;
9eea179c 33 $Size = strlen($body);
e2ef6f4b 34
9297917e 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")) {
20a60f89 40 eregi ($Email_RegExp_Match, $body, $regs);
9297917e 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 {
20a60f89 47 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9297917e 48 }
49 */
8f7163e7 50
20a60f89 51 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9eea179c 52
53 // If there are any changes, it'll just get bigger.
54 if ($Size != strlen($body))
55 return 1;
56 return 0;
175e7218 57 }
43fcef5c 58
43fcef5c 59
20a60f89 60 // We don't want to re-initialize this stuff for every line. Save work
61 // and just do it once here.
f435778e 62 global $url_parser_url_tokens;
20a60f89 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://');
8f7163e7 70
f435778e 71 global $url_parser_poss_ends;
20a60f89 72 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
73 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
74 ']', '[', '{', '}', "\240");
8f7163e7 75
20a60f89 76
77 function parseUrl (&$body)
78 {
79 global $url_parser_poss_ends, $url_parser_url_tokens;;
8f7163e7 80 $start = 0;
81 $target_pos = strlen($body);
82
83 while ($start != $target_pos)
e2ef6f4b 84 {
8f7163e7 85 $target_token = '';
86
87 // Find the first token to replace
20a60f89 88 foreach ($url_parser_url_tokens as $the_token)
8f7163e7 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);
8f7163e7 100
9eea179c 101 if (parseEmail($check_str))
8f7163e7 102 {
20a60f89 103 replaceBlock($body, $check_str, $start, $target_pos);
9eea179c 104 $target_pos = strlen($check_str) + $start;
8f7163e7 105 }
e2ef6f4b 106
8f7163e7 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);
20a60f89 112 foreach ($url_parser_poss_ends as $key => $val)
8f7163e7 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>";
20a60f89 126 replaceBlock($body,$url_str,$target_pos,$end);
8f7163e7 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 }
43fcef5c 140 }
8f7163e7 141
43fcef5c 142?>