6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
15 function replaceBlock (&$in, $replace, $start, $end) {
16 $begin = substr($in,0,$start);
17 $end = substr($in,$end,strlen($in)-$end);
18 $in = $begin.$replace.$end;
21 /* Having this defined in just one spot could help when changes need
22 * to be made to the pattern
23 * Make sure that the expression is evaluated case insensitively
25 * Here's pretty sophisticated IP matching:
26 * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
27 * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
30 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
31 $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
32 $Host_RegExp_Match = '(' . $IP_RegExp_Match .
33 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
34 $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
35 ')?@' . $Host_RegExp_Match;
37 function parseEmail (&$body) {
38 global $color, $Email_RegExp_Match;
39 $Size = strlen($body);
42 * This is here in case we ever decide to use highlighting of searched
43 * text. this does it for email addresses
45 * if ($what && ($where == "BODY" || $where == "TEXT")) {
46 * eregi ($Email_RegExp_Match, $body, $regs);
47 * $oldaddr = $regs[0];
49 * $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
50 * $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
53 * $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
57 if( eregi($Email_RegExp_Match, $body, $regs) ) {
58 $body = str_replace($regs[0], '<a href="../src/compose.php?send_to='.
59 urlencode($regs[0]).'">'.$regs[0].'</a>', $body);
62 /* If there are any changes, it'll just get bigger. */
63 if ($Size != strlen($body)) {
70 /* We don't want to re-initialize this stuff for every line. Save work
71 * and just do it once here.
73 global $url_parser_url_tokens;
74 $url_parser_url_tokens = array(
78 'telnet:', // Special case -- doesn't need the slashes
82 global $url_parser_poss_ends;
83 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
84 '. ', ' ', ')', '(', '"', '<', '>', '.<',
85 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
88 function parseUrl (&$body) {
89 global $url_parser_poss_ends, $url_parser_url_tokens;;
91 $target_pos = strlen($body);
93 while ($start != $target_pos) {
96 /* Find the first token to replace */
97 foreach ($url_parser_url_tokens as $the_token) {
98 $pos = strpos(strtolower($body), $the_token, $start);
99 if (is_int($pos) && $pos < $target_pos) {
101 $target_token = $the_token;
105 /* Look for email addresses between $start and $target_pos */
106 $check_str = substr($body, $start, $target_pos);
108 if (parseEmail($check_str)) {
109 replaceBlock($body, $check_str, $start, $target_pos);
110 $target_pos = strlen($check_str) +
$start;
113 /* If there was a token to replace, replace it */
114 if ($target_token != '') {
115 /* Find the end of the URL */
117 foreach ($url_parser_poss_ends as $key => $val) {
118 $enda = strpos($body,$val,$target_pos);
119 if (is_int($enda) && $enda < $end) {
125 $url = substr($body, $target_pos, $end-$target_pos);
127 /* Needed since lines are not passed with \n or \r */
128 while ( ereg("[,\.]$", $url) ) {
129 $url = substr( $url, 0, -1 );
133 /* Replace URL with HyperLinked Url, requires 1 char in link */
134 if ($url != '' && $url != $target_token) {
135 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
136 replaceBlock($body,$url_str,$target_pos,$end);
137 $target_pos +
= strlen($url_str);
140 // Not quite a valid link, skip ahead to next chance
141 $target_pos +
= strlen($target_token);
146 $start = $target_pos;
147 $target_pos = strlen($body);