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