6 * Copyright (c) 1999-2004 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.
13 * @package squirrelmail
17 * Undocumented - complain, then patch.
19 function replaceBlock (&$in, $replace, $start, $end) {
20 $begin = substr($in,0,$start);
21 $end = substr($in,$end,strlen($in)-$end);
22 $in = $begin.$replace.$end;
25 /* Having this defined in just one spot could help when changes need
26 * to be made to the pattern
27 * Make sure that the expression is evaluated case insensitively
29 * RFC2822 (and RFC822) defines the left side of an email address as (roughly):
30 * 1*atext *("." 1*atext)
31 * where atext is: a-zA-Z0-9!#$%&'*+-/=?^_`{|}~
33 * Here's pretty sophisticated IP matching:
34 * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
35 * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
38 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
39 $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
40 $Host_RegExp_Match = '(' . $IP_RegExp_Match .
41 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
42 $atext = '([a-z0-9!#$&%*+/=?^_`{|}~-]|&)';
43 $dot_atom = $atext . '+(\.' . $atext . '+)*';
44 $Email_RegExp_Match = $dot_atom . '(%' . $Host_RegExp_Match . ')?@' .
48 * Parses a body and converts all found email addresses to clickable links.
50 * @param string body the body to process, by ref
51 * @return int the number of unique addresses found
53 function parseEmail (&$body) {
54 global $color, $Email_RegExp_Match;
58 /* Find all the email addresses in the body */
59 while(eregi($Email_RegExp_Match, $sbody, $regs)) {
60 $addresses[$regs[0]] = strtr($regs[0], array('&' => '&'));
61 $start = strpos($sbody, $regs[0]) +
strlen($regs[0]);
62 $sbody = substr($sbody, $start);
64 /* Replace each email address with a compose URL */
65 foreach ($addresses as $text => $email) {
66 $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $text);
67 $body = str_replace($text, $comp_uri, $body);
69 /* Return number of unique addresses found */
70 return count($addresses);
74 /* We don't want to re-initialize this stuff for every line. Save work
75 * and just do it once here.
77 global $url_parser_url_tokens;
78 $url_parser_url_tokens = array(
82 'telnet:', // Special case -- doesn't need the slashes
83 'mailto:', // Special case -- doesn't use the slashes
87 global $url_parser_poss_ends;
88 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
89 '. ', ' ', ')', '(', '"', '<', '>', '.<',
90 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
94 * rfc 2368 (mailto URL) preg_match() regexp
95 * @link http://www.ietf.org/rfc/rfc2368.txt
96 * @global string MailTo_PReg_Match the encapsulated regexp for preg_match()
98 global $MailTo_PReg_Match;
99 $Mailto_Email_RegExp = '[0-9a-z%]([-_.+%]?[0-9a-z])*(%' . $Host_RegExp_Match . ')?@' . $Host_RegExp_Match;
100 $MailTo_PReg_Match = '/((?:' . $Mailto_Email_RegExp . ')*)((?:\?(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)?(?:&(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)*)/i';
103 * Parses a body and converts all found URLs to clickable links.
105 * @param string body the body to process, by ref
108 function parseUrl (&$body) {
109 global $url_parser_poss_ends, $url_parser_url_tokens;
111 $blength = strlen($body);
113 while ($start < $blength) {
115 $target_pos = $blength;
117 /* Find the first token to replace */
118 foreach ($url_parser_url_tokens as $the_token) {
119 $pos = strpos(strtolower($body), $the_token, $start);
120 if (is_int($pos) && $pos < $target_pos) {
122 $target_token = $the_token;
126 /* Look for email addresses between $start and $target_pos */
127 $check_str = substr($body, $start, $target_pos-$start);
129 if (parseEmail($check_str)) {
130 replaceBlock($body, $check_str, $start, $target_pos);
131 $blength = strlen($body);
132 $target_pos = strlen($check_str) +
$start;
135 /* If there was a token to replace, replace it */
136 if ($target_token == 'mailto:') { // rfc 2368 (mailto URL)
137 $target_pos +
= 7; //skip mailto:
140 $mailto = substr($body, $target_pos, $end-$target_pos);
142 global $MailTo_PReg_Match;
143 if ((preg_match($MailTo_PReg_Match, $mailto, $regs)) && ($regs[0] != '')) {
145 $mailto_before = $target_token . $regs[0];
146 $mailto_params = $regs[10];
147 if ($regs[1]) { //if there is an email addr before '?', we need to merge it with the params
148 $to = 'to=' . $regs[1];
149 if (strpos($mailto_params, 'to=') > -1) //already a 'to='
150 $mailto_params = str_replace('to=', $to . '%2C%20', $mailto_params);
152 if ($mailto_params) //already some params, append to them
153 $mailto_params .= '&' . $to;
155 $mailto_params .= '?' . $to;
158 $url_str = str_replace(array('to=', 'cc=', 'bcc='), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params);
159 $comp_uri = makeComposeLink('src/compose.php' . $url_str, $mailto_before);
160 replaceBlock($body, $comp_uri, $target_pos - 7, $target_pos +
strlen($regs[0]));
161 $target_pos +
= strlen($comp_uri) - 7;
165 if ($target_token != '') {
166 /* Find the end of the URL */
168 foreach ($url_parser_poss_ends as $val) {
169 $enda = strpos($body, $val, $target_pos);
170 if (is_int($enda) && $enda < $end) {
176 $url = substr($body, $target_pos, $end-$target_pos);
178 /* Needed since lines are not passed with \n or \r */
179 while ( ereg("[,\.]$", $url) ) {
180 $url = substr( $url, 0, -1 );
184 /* Replace URL with HyperLinked Url, requires 1 char in link */
185 if ($url != '' && $url != $target_token) {
186 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
187 replaceBlock($body,$url_str,$target_pos,$end);
188 $target_pos +
= strlen($url_str);
191 // Not quite a valid link, skip ahead to next chance
192 $target_pos +
= strlen($target_token);
197 $start = $target_pos;
198 $blength = strlen($body);