Some fixup:
[squirrelmail.git] / functions / url_parser.php
CommitLineData
59177427 1<?php
43fcef5c 2
2d367c68 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 */
43fcef5c 13
20a60f89 14 function replaceBlock (&$in, $replace, $start, $end) {
43fcef5c 15 $begin = substr($in,0,$start);
16 $end = substr($in,$end,strlen($in)-$end);
20a60f89 17 $in = $begin.$replace.$end;
43fcef5c 18 }
19
20a60f89 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:
f435778e 29 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
20a60f89 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
9eea179c 36 function parseEmail (&$body) {
20a60f89 37 global $color, $Email_RegExp_Match;
9eea179c 38 $Size = strlen($body);
e2ef6f4b 39
9297917e 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")) {
20a60f89 45 eregi ($Email_RegExp_Match, $body, $regs);
9297917e 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 {
20a60f89 52 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9297917e 53 }
54 */
8f7163e7 55
20a60f89 56 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9eea179c 57
58 // If there are any changes, it'll just get bigger.
59 if ($Size != strlen($body))
60 return 1;
61 return 0;
175e7218 62 }
43fcef5c 63
43fcef5c 64
20a60f89 65 // We don't want to re-initialize this stuff for every line. Save work
66 // and just do it once here.
f435778e 67 global $url_parser_url_tokens;
20a60f89 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://');
8f7163e7 75
f435778e 76 global $url_parser_poss_ends;
20a60f89 77 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
78 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
79 ']', '[', '{', '}', "\240");
8f7163e7 80
20a60f89 81
82 function parseUrl (&$body)
83 {
84 global $url_parser_poss_ends, $url_parser_url_tokens;;
8f7163e7 85 $start = 0;
86 $target_pos = strlen($body);
87
88 while ($start != $target_pos)
e2ef6f4b 89 {
8f7163e7 90 $target_token = '';
91
92 // Find the first token to replace
20a60f89 93 foreach ($url_parser_url_tokens as $the_token)
8f7163e7 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);
8f7163e7 105
9eea179c 106 if (parseEmail($check_str))
8f7163e7 107 {
20a60f89 108 replaceBlock($body, $check_str, $start, $target_pos);
9eea179c 109 $target_pos = strlen($check_str) + $start;
8f7163e7 110 }
e2ef6f4b 111
8f7163e7 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);
20a60f89 117 foreach ($url_parser_poss_ends as $key => $val)
8f7163e7 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>";
20a60f89 131 replaceBlock($body,$url_str,$target_pos,$end);
8f7163e7 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 }
43fcef5c 145 }
8f7163e7 146
43fcef5c 147?>