6 * Copyright (c) 1999-2003 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;
42 /* Find all the email addresses in the body */
43 while(eregi($Email_RegExp_Match, $sbody, $regs)) {
44 $addresses[$regs[0]] = $regs[0];
45 $start = strpos($sbody, $regs[0]) +
strlen($regs[0]);
46 $sbody = substr($sbody, $start);
48 /* Replace each email address with a compose URL */
49 foreach ($addresses as $email) {
50 $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email);
51 $body = str_replace($email, $comp_uri, $body);
53 /* Return number of unique addresses found */
54 return count($addresses);
58 /* We don't want to re-initialize this stuff for every line. Save work
59 * and just do it once here.
61 global $url_parser_url_tokens;
62 $url_parser_url_tokens = array(
66 'telnet:', // Special case -- doesn't need the slashes
70 global $url_parser_poss_ends;
71 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
72 '. ', ' ', ')', '(', '"', '<', '>', '.<',
73 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
76 function parseUrl (&$body) {
77 global $url_parser_poss_ends, $url_parser_url_tokens;;
79 $blength = strlen($body);
81 while ($start != $blength) {
83 $target_pos = $blength;
85 /* Find the first token to replace */
86 foreach ($url_parser_url_tokens as $the_token) {
87 $pos = strpos(strtolower($body), $the_token, $start);
88 if (is_int($pos) && $pos < $blength) {
90 $target_token = $the_token;
94 /* Look for email addresses between $start and $target_pos */
95 $check_str = substr($body, $start, $target_pos-$start);
97 if (parseEmail($check_str)) {
98 replaceBlock($body, $check_str, $start, $target_pos);
99 $blength = strlen($body);
100 $target_pos = strlen($check_str) +
$start;
103 /* If there was a token to replace, replace it */
104 if ($target_token != '') {
105 /* Find the end of the URL */
107 foreach ($url_parser_poss_ends as $val) {
108 $enda = strpos($body, $val, $target_pos);
109 if (is_int($enda) && $enda < $end) {
115 $url = substr($body, $target_pos, $end-$target_pos);
117 /* Needed since lines are not passed with \n or \r */
118 while ( ereg("[,\.]$", $url) ) {
119 $url = substr( $url, 0, -1 );
123 /* Replace URL with HyperLinked Url, requires 1 char in link */
124 if ($url != '' && $url != $target_token) {
125 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
126 replaceBlock($body,$url_str,$target_pos,$end);
127 $target_pos +
= strlen($url_str);
130 // Not quite a valid link, skip ahead to next chance
131 $target_pos +
= strlen($target_token);
136 $start = $target_pos;
137 $blength = strlen($body);