squirrelmail-webmail  1.4.22
About: SquirrelMail is a standards-based webmail package with strong MIME support, address books, and folder manipulation (written in PHP4).
  Fossies Dox: squirrelmail-webmail-1.4.22.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

url_parser.php
Go to the documentation of this file.
1 <?php
2 
18 function 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 }
23 
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
27  *
28  * Here's pretty sophisticated IP matching:
29  * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
30  * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
31  */
32 /* Here's enough: */
34 $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
36  '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
37 $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
38  ')?@' . $Host_RegExp_Match;
39 
46 $Mailto_Email_RegExp = '[0-9a-z%]([-_.+%]?[0-9a-z])*(%' . $Host_RegExp_Match . ')?@' . $Host_RegExp_Match;
47 $MailTo_PReg_Match = '/((?:' . $Mailto_Email_RegExp . ')*)((?:\?(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)?(?:&amp;(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)*)/i';
48 
49 function parseEmail (&$body) {
51  $sbody = $body;
52  $addresses = array();
53 
54  /* Find all the email addresses in the body */
55  while(preg_match('/'.$Email_RegExp_Match.'/i', $sbody, $regs)) {
56  $addresses[$regs[0]] = $regs[0];
57  $start = strpos($sbody, $regs[0]) + strlen($regs[0]);
58  $sbody = substr($sbody, $start);
59  }
60  /* Replace each email address with a compose URL */
61  foreach ($addresses as $email) {
62  $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email);
63  $body = str_replace($email, $comp_uri, $body);
64  }
65  /* Return number of unique addresses found */
66  return count($addresses);
67 }
68 
69 
70 /* We don't want to re-initialize this stuff for every line. Save work
71  * and just do it once here.
72  */
75  'http://',
76  'https://',
77  'ftp://',
78  'telnet:', // Special case -- doesn't need the slashes
79  'mailto:', // Special case -- doesn't use the slashes
80  'gopher://',
81  'news://');
82 
84 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
85  '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
86  ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
87 
88 
95 function parseUrl (&$body) {
97  $start = 0;
98  $blength = strlen($body);
99 
100  while ($start < $blength) {
101  $target_token = '';
102  $target_pos = $blength;
103 
104  /* Find the first token to replace */
105  foreach ($url_parser_url_tokens as $the_token) {
106  $pos = strpos(strtolower($body), $the_token, $start);
107  if (is_int($pos) && $pos < $target_pos) {
108  $target_pos = $pos;
109  $target_token = $the_token;
110  }
111  }
112 
113  /* Look for email addresses between $start and $target_pos */
114  $check_str = substr($body, $start, $target_pos-$start);
115 
116  if (parseEmail($check_str)) {
117  replaceBlock($body, $check_str, $start, $target_pos);
118  $blength = strlen($body);
119  $target_pos = strlen($check_str) + $start;
120  }
121 
122  // rfc 2368 (mailto URL)
123  if ($target_token == 'mailto:') {
124  $target_pos += 7; //skip mailto:
125  $end = $blength;
126 
127  $mailto = substr($body, $target_pos, $end-$target_pos);
128 
129  global $MailTo_PReg_Match;
130  if ((preg_match($MailTo_PReg_Match, $mailto, $regs)) && ($regs[0] != '')) {
131  $mailto_before = $target_token . $regs[0];
138  $mailto_params = str_replace("+", "%2B", $regs[10]);
139  if ($regs[1]) { //if there is an email addr before '?', we need to merge it with the params
140  $to = 'to=' . str_replace("+", "%2B", $regs[1]);
141  if (strpos($mailto_params, 'to=') > -1) //already a 'to='
142  $mailto_params = str_replace('to=', $to . '%2C%20', $mailto_params);
143  else {
144  if ($mailto_params) //already some params, append to them
145  $mailto_params .= '&amp;' . $to;
146  else
147  $mailto_params .= '?' . $to;
148  }
149  }
150  $url_str = preg_replace(array('/to=/i', '/(?<!b)cc=/i', '/bcc=/i'), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params);
151  $comp_uri = makeComposeLink('src/compose.php' . $url_str, $mailto_before);
152  replaceBlock($body, $comp_uri, $target_pos - 7, $target_pos + strlen($regs[0]));
153  $target_pos += strlen($comp_uri) - 7;
154  }
155  }
156  else
157  /* If there was a token to replace, replace it */
158  if ($target_token != '') {
159  /* Find the end of the URL */
160  $end = $blength;
161  foreach ($url_parser_poss_ends as $val) {
162  $enda = strpos($body, $val, $target_pos);
163  if (is_int($enda) && $enda < $end) {
164  $end = $enda;
165  }
166  }
167 
168  /* make sure that there are no 8bit chars between $target_pos and suspected end of URL */
169  if (!is_bool($first8bit=sq_strpos_8bit($body,$target_pos,$end))) {
170  $end = $first8bit;
171  }
172 
173  /* Extract URL */
174  $url = substr($body, $target_pos, $end-$target_pos);
175 
176  /* Needed since lines are not passed with \n or \r */
177  while ( preg_match('/[,.]$/', $url) ) {
178  $url = substr( $url, 0, -1 );
179  $end--;
180  }
181 
182  /* Replace URL with HyperLinked Url, requires 1 char in link */
183  if ($url != '' && $url != $target_token) {
184  $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
185  replaceBlock($body,$url_str,$target_pos,$end);
186  $target_pos += strlen($url_str);
187  }
188  else {
189  // Not quite a valid link, skip ahead to next chance
190  $target_pos += strlen($target_token);
191  }
192  }
193 
194  /* Move forward */
195  $start = $target_pos;
196  $blength = strlen($body);
197  }
198 }
199 
216 function sq_strpos_8bit($haystack,$offset=0,$maxlength=false) {
217  $ret = false;
218 
219  if ($maxlength===false || strlen($haystack) < $maxlength) {
220  $maxlength=strlen($haystack);
221  }
222 
223  for($i=$offset;$i<$maxlength;$i++) {
224  /* rh7-8 compatibility. don't use full 8bit range in regexp */
225  if (preg_match('/[\200-\237]|\240|[\241-\377]/',$haystack[$i])) {
226  /* we have 8bit char. stop here and return position */
227  $ret = $i;
228  break;
229  } elseif ($haystack[$i]=='&') {
230  $substring = substr($haystack,$i);
238  if ((preg_match('/^&#(\d+);/',$substring,$match) && $match[1]>127) ||
239  (preg_match('/^&x([0-9a-f]+);/i',$substring,$match) && $match[1]>"\x7f") ||
240  (preg_match('/^&([a-z]+);/i',$substring,$match) &&
241  !in_array($match[0],get_html_translation_table(HTML_SPECIALCHARS)) &&
242  in_array($match[0],sq_get_html_translation_table(HTML_ENTITIES,ENT_COMPAT,'utf-8')))) {
243  $ret = $i;
244  break;
245  }
246  }
247  }
248  return $ret;
249 }
$body
if(isset($ldap_server) && $ldap_server[0] &&! extension_loaded('ldap')) $body
Definition: bug_report.php:106
elseif
if(! sqgetGlobalVar('sound', $sound, SQ_GET)) elseif($sound=='(none)')
Definition: testsound.php:25
$ret
$ret
Definition: webmail.php:172
sq_strpos_8bit
sq_strpos_8bit($haystack, $offset=0, $maxlength=false)
Definition: url_parser.php:216
$Email_RegExp_Match
global $Email_RegExp_Match
Definition: url_parser.php:33
$url_parser_url_tokens
global $url_parser_url_tokens
Definition: url_parser.php:73
$MailTo_PReg_Match
global $MailTo_PReg_Match
Definition: url_parser.php:45
$Mailto_Email_RegExp
$Mailto_Email_RegExp
Definition: url_parser.php:46
$url
$url
Definition: mailto.php:81
$url_parser_poss_ends
global $url_parser_poss_ends
Definition: url_parser.php:83
$IP_RegExp_Match
global $IP_RegExp_Match
Definition: url_parser.php:33
parseUrl
parseUrl(&$body)
Definition: url_parser.php:95
makeComposeLink
makeComposeLink($url, $text=null, $target='')
Definition: strings.php:704
$Host_RegExp_Match
global $Host_RegExp_Match
Definition: url_parser.php:33
$end
$end
Definition: message_details_bottom.php:90
replaceBlock
replaceBlock(&$in, $replace, $start, $end)
Definition: url_parser.php:18
parseEmail
parseEmail(&$body)
Definition: url_parser.php:49
$to
$to
Definition: printer_friendly_bottom.php:66
$color
$color
Definition: load_prefs.php:28
$compose_new_win
$compose_new_win
Definition: load_prefs.php:251