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