5 * Copyright (c) 1999-2001 The Squirrelmail Development Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * This code provides various string manipulation functions that are
9 * used by the rest of the Squirrelmail code.
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;
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
24 // Here's pretty sophisticated IP matching:
25 // $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
26 // $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
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;
36 function parseEmail (&$body) {
37 global $color, $Email_RegExp_Match;
38 $Size = strlen($body);
41 This is here in case we ever decide to use highlighting of searched
42 text. this does it for email addresses
44 if ($what && ($where == "BODY" || $where == "TEXT")) {
45 eregi ($Email_RegExp_Match, $body, $regs);
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);
52 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
56 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
58 // If there are any changes, it'll just get bigger.
59 if ($Size != strlen($body))
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(
72 'telnet:', // Special case -- doesn't need the slashes
76 global $url_parser_poss_ends;
77 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
78 '. ', ' ', ')', '(', '"', '<', '>', '.<',
79 ']', '[', '{', '}', "\240");
82 function parseUrl (&$body)
84 global $url_parser_poss_ends, $url_parser_url_tokens;;
86 $target_pos = strlen($body);
88 while ($start != $target_pos)
92 // Find the first token to replace
93 foreach ($url_parser_url_tokens as $the_token)
95 $pos = strpos(strtolower($body), $the_token, $start);
96 if (is_int($pos) && $pos < $target_pos)
99 $target_token = $the_token;
103 // Look for email addresses between $start and $target_pos
104 $check_str = substr($body, $start, $target_pos);
106 if (parseEmail($check_str))
108 replaceBlock($body, $check_str, $start, $target_pos);
109 $target_pos = strlen($check_str) +
$start;
112 // If there was a token to replace, replace it
113 if ($target_token != '')
115 // Find the end of the URL
117 foreach ($url_parser_poss_ends as $key => $val)
119 $enda = strpos($body,$val,$target_pos);
120 if (is_int($enda) && $enda < $end)
125 $url = substr($body, $target_pos, $end-$target_pos);
127 // Replace URL with HyperLinked Url, requires 1 char in link
128 if ($url != '' && $url != $target_token)
130 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
131 replaceBlock($body,$url_str,$target_pos,$end);
132 $target_pos +
= strlen($url_str);
136 // Not quite a valid link, skip ahead to next chance
137 $target_pos +
= strlen($target_token);
142 $start = $target_pos;
143 $target_pos = strlen($body);