adding address book block
[squirrelmail.git] / functions / url_parser.php
CommitLineData
59177427 1<?php
43fcef5c 2
35586184 3/**
4 * url_parser.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
11 *
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
35586184 14 */
43fcef5c 15
d6c32258 16/**
17 * Undocumented - complain, then patch.
18 */
35586184 19function replaceBlock (&$in, $replace, $start, $end) {
20 $begin = substr($in,0,$start);
21 $end = substr($in,$end,strlen($in)-$end);
22 $in = $begin.$replace.$end;
23}
43fcef5c 24
01d27858 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
7e235a1a 28 *
ff18cccd 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!#$%&'*+-/=?^_`{|}~
32 *
01d27858 33 * Here's pretty sophisticated IP matching:
34 * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
35 * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
36 */
37/* Here's enough: */
38global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
39$IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
7e235a1a 40$Host_RegExp_Match = '(' . $IP_RegExp_Match .
01d27858 41 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
ff18cccd 42$atext = '([a-z0-9!#$&%*+/=?^_`{|}~-]|&amp;)';
43$dot_atom = $atext . '+(\.' . $atext . '+)*';
44$Email_RegExp_Match = $dot_atom . '(%' . $Host_RegExp_Match . ')?@' .
45 $Host_RegExp_Match;
7e235a1a 46
8b096f0a 47/**
48 * Parses a body and converts all found email addresses to clickable links.
49 *
50 * @param string body the body to process, by ref
51 * @return int the number of unique addresses found
52 */
01d27858 53function parseEmail (&$body) {
916669ad 54 global $Email_RegExp_Match;
cf0d436a 55 $sbody = $body;
56 $addresses = array();
57
58 /* Find all the email addresses in the body */
59 while(eregi($Email_RegExp_Match, $sbody, $regs)) {
ff18cccd 60 $addresses[$regs[0]] = strtr($regs[0], array('&amp;' => '&'));
cf0d436a 61 $start = strpos($sbody, $regs[0]) + strlen($regs[0]);
62 $sbody = substr($sbody, $start);
63 }
916669ad 64
cf0d436a 65 /* Replace each email address with a compose URL */
ff18cccd 66 foreach ($addresses as $text => $email) {
67 $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $text);
68 $body = str_replace($text, $comp_uri, $body);
7e235a1a 69 }
916669ad 70
cf0d436a 71 /* Return number of unique addresses found */
72 return count($addresses);
01d27858 73}
43fcef5c 74
43fcef5c 75
01d27858 76/* We don't want to re-initialize this stuff for every line. Save work
77 * and just do it once here.
78 */
79global $url_parser_url_tokens;
80$url_parser_url_tokens = array(
81 'http://',
82 'https://',
83 'ftp://',
84 'telnet:', // Special case -- doesn't need the slashes
7efaee4f 85 'mailto:', // Special case -- doesn't use the slashes
01d27858 86 'gopher://',
87 'news://');
8f7163e7 88
01d27858 89global $url_parser_poss_ends;
62f7daa5 90$url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
91 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
01d27858 92 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
8f7163e7 93
20a60f89 94
8b096f0a 95/**
7efaee4f 96 * rfc 2368 (mailto URL) preg_match() regexp
99554426 97 * @link http://www.ietf.org/rfc/rfc2368.txt
7efaee4f 98 * @global string MailTo_PReg_Match the encapsulated regexp for preg_match()
99 */
100global $MailTo_PReg_Match;
101$Mailto_Email_RegExp = '[0-9a-z%]([-_.+%]?[0-9a-z])*(%' . $Host_RegExp_Match . ')?@' . $Host_RegExp_Match;
102$MailTo_PReg_Match = '/((?:' . $Mailto_Email_RegExp . ')*)((?:\?(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)?(?:&amp;(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)*)/i';
103
104/**
8b096f0a 105 * Parses a body and converts all found URLs to clickable links.
106 *
107 * @param string body the body to process, by ref
108 * @return void
109 */
01d27858 110function parseUrl (&$body) {
99554426 111 global $url_parser_poss_ends, $url_parser_url_tokens;
cf0d436a 112 $start = 0;
113 $blength = strlen($body);
cf0d436a 114
7efaee4f 115 while ($start < $blength) {
8f7163e7 116 $target_token = '';
0d3ff000 117 $target_pos = $blength;
cf0d436a 118
01d27858 119 /* Find the first token to replace */
120 foreach ($url_parser_url_tokens as $the_token) {
121 $pos = strpos(strtolower($body), $the_token, $start);
6f1450f4 122 if (is_int($pos) && $pos < $target_pos) {
cf0d436a 123 $target_pos = $pos;
01d27858 124 $target_token = $the_token;
125 }
8f7163e7 126 }
cf0d436a 127
01d27858 128 /* Look for email addresses between $start and $target_pos */
cf0d436a 129 $check_str = substr($body, $start, $target_pos-$start);
130
01d27858 131 if (parseEmail($check_str)) {
132 replaceBlock($body, $check_str, $start, $target_pos);
cf0d436a 133 $blength = strlen($body);
01d27858 134 $target_pos = strlen($check_str) + $start;
8f7163e7 135 }
e2ef6f4b 136
01d27858 137 /* If there was a token to replace, replace it */
7efaee4f 138 if ($target_token == 'mailto:') { // rfc 2368 (mailto URL)
99554426 139 $target_pos += 7; //skip mailto:
7efaee4f 140 $end = $blength;
141
142 $mailto = substr($body, $target_pos, $end-$target_pos);
143
144 global $MailTo_PReg_Match;
99554426 145 if ((preg_match($MailTo_PReg_Match, $mailto, $regs)) && ($regs[0] != '')) {
7efaee4f 146 //sm_print_r($regs);
147 $mailto_before = $target_token . $regs[0];
148 $mailto_params = $regs[10];
149 if ($regs[1]) { //if there is an email addr before '?', we need to merge it with the params
150 $to = 'to=' . $regs[1];
151 if (strpos($mailto_params, 'to=') > -1) //already a 'to='
152 $mailto_params = str_replace('to=', $to . '%2C%20', $mailto_params);
153 else {
154 if ($mailto_params) //already some params, append to them
99554426 155 $mailto_params .= '&amp;' . $to;
7efaee4f 156 else
99554426 157 $mailto_params .= '?' . $to;
7efaee4f 158 }
159 }
160 $url_str = str_replace(array('to=', 'cc=', 'bcc='), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params);
161 $comp_uri = makeComposeLink('src/compose.php' . $url_str, $mailto_before);
99554426 162 replaceBlock($body, $comp_uri, $target_pos - 7, $target_pos + strlen($regs[0]));
163 $target_pos += strlen($comp_uri) - 7;
7efaee4f 164 }
165 }
166 else
01d27858 167 if ($target_token != '') {
168 /* Find the end of the URL */
cf0d436a 169 $end = $blength;
170 foreach ($url_parser_poss_ends as $val) {
171 $enda = strpos($body, $val, $target_pos);
01d27858 172 if (is_int($enda) && $enda < $end) {
173 $end = $enda;
174 }
175 }
cf0d436a 176
01d27858 177 /* Extract URL */
178 $url = substr($body, $target_pos, $end-$target_pos);
cf0d436a 179
01d27858 180 /* Needed since lines are not passed with \n or \r */
181 while ( ereg("[,\.]$", $url) ) {
182 $url = substr( $url, 0, -1 );
183 $end--;
184 }
1b3324b3 185
01d27858 186 /* Replace URL with HyperLinked Url, requires 1 char in link */
187 if ($url != '' && $url != $target_token) {
188 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
189 replaceBlock($body,$url_str,$target_pos,$end);
190 $target_pos += strlen($url_str);
cf0d436a 191 }
192 else {
193 // Not quite a valid link, skip ahead to next chance
194 $target_pos += strlen($target_token);
01d27858 195 }
8f7163e7 196 }
cf0d436a 197
01d27858 198 /* Move forward */
cf0d436a 199 $start = $target_pos;
200 $blength = strlen($body);
01d27858 201 }
62f7daa5 202}
916669ad 203
204/**
205 * Parses a string and returns the first e-mail address found.
206 *
207 * @param string string the string to process
208 * @return string the first e-mail address found
209 */
210function getEmail($string) {
211 global $Email_RegExp_Match;
212 $addresses = array();
213
214 /* Find all the email addresses in the body */
215 while (eregi($Email_RegExp_Match, $string, $regs)) {
216 $addresses[$regs[0]] = strtr($regs[0], array('&amp;' => '&'));
217 $start = strpos($string, $regs[0]) + strlen($regs[0]);
218 $string = substr($string, $start);
219 }
220
221 /* Return the first address, or an empty string if no address was found */
222 $addresses = array_values($addresses);
223 return (array_key_exists(0, $addresses) ? $addresses[0] : '');
224}
225
62f7daa5 226?>