url_parser.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * url_parser.php
  4. *
  5. * Copyright (c) 1999-2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This code provides various string manipulation functions that are
  9. * used by the rest of the Squirrelmail code.
  10. *
  11. * @version $Id$
  12. * @package squirrelmail
  13. */
  14. /**
  15. * Undocumented - complain, then patch.
  16. */
  17. function replaceBlock (&$in, $replace, $start, $end) {
  18. $begin = substr($in,0,$start);
  19. $end = substr($in,$end,strlen($in)-$end);
  20. $in = $begin.$replace.$end;
  21. }
  22. /* Having this defined in just one spot could help when changes need
  23. * to be made to the pattern
  24. * Make sure that the expression is evaluated case insensitively
  25. *
  26. * Here's pretty sophisticated IP matching:
  27. * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
  28. * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
  29. */
  30. /* Here's enough: */
  31. global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
  32. $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
  33. $Host_RegExp_Match = '(' . $IP_RegExp_Match .
  34. '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
  35. $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
  36. ')?@' . $Host_RegExp_Match;
  37. /**
  38. * Parses a body and converts all found email addresses to clickable links.
  39. *
  40. * @param string body the body to process, by ref
  41. * @return int the number of unique addresses found
  42. */
  43. function parseEmail (&$body) {
  44. global $color, $Email_RegExp_Match;
  45. $sbody = $body;
  46. $addresses = array();
  47. /* Find all the email addresses in the body */
  48. while(eregi($Email_RegExp_Match, $sbody, $regs)) {
  49. $addresses[$regs[0]] = $regs[0];
  50. $start = strpos($sbody, $regs[0]) + strlen($regs[0]);
  51. $sbody = substr($sbody, $start);
  52. }
  53. /* Replace each email address with a compose URL */
  54. foreach ($addresses as $email) {
  55. $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email);
  56. $body = str_replace($email, $comp_uri, $body);
  57. }
  58. /* Return number of unique addresses found */
  59. return count($addresses);
  60. }
  61. /* We don't want to re-initialize this stuff for every line. Save work
  62. * and just do it once here.
  63. */
  64. global $url_parser_url_tokens;
  65. $url_parser_url_tokens = array(
  66. 'http://',
  67. 'https://',
  68. 'ftp://',
  69. 'telnet:', // Special case -- doesn't need the slashes
  70. 'mailto:', // Special case -- doesn't use the slashes
  71. 'gopher://',
  72. 'news://');
  73. global $url_parser_poss_ends;
  74. $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
  75. '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
  76. ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
  77. /**
  78. * rfc 2368 (mailto URL) preg_match() regexp
  79. * @link http://www.ietf.org/rfc/rfc2368.txt
  80. * @global string MailTo_PReg_Match the encapsulated regexp for preg_match()
  81. */
  82. global $MailTo_PReg_Match;
  83. $Mailto_Email_RegExp = '[0-9a-z%]([-_.+%]?[0-9a-z])*(%' . $Host_RegExp_Match . ')?@' . $Host_RegExp_Match;
  84. $MailTo_PReg_Match = '/((?:' . $Mailto_Email_RegExp . ')*)((?:\?(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)?(?:&amp;(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)*)/i';
  85. /**
  86. * Parses a body and converts all found URLs to clickable links.
  87. *
  88. * @param string body the body to process, by ref
  89. * @return void
  90. */
  91. function parseUrl (&$body) {
  92. global $url_parser_poss_ends, $url_parser_url_tokens;
  93. $start = 0;
  94. $blength = strlen($body);
  95. while ($start < $blength) {
  96. $target_token = '';
  97. $target_pos = $blength;
  98. /* Find the first token to replace */
  99. foreach ($url_parser_url_tokens as $the_token) {
  100. $pos = strpos(strtolower($body), $the_token, $start);
  101. if (is_int($pos) && $pos < $blength) {
  102. $target_pos = $pos;
  103. $target_token = $the_token;
  104. }
  105. }
  106. /* Look for email addresses between $start and $target_pos */
  107. $check_str = substr($body, $start, $target_pos-$start);
  108. if (parseEmail($check_str)) {
  109. replaceBlock($body, $check_str, $start, $target_pos);
  110. $blength = strlen($body);
  111. $target_pos = strlen($check_str) + $start;
  112. }
  113. /* If there was a token to replace, replace it */
  114. if ($target_token == 'mailto:') { // rfc 2368 (mailto URL)
  115. $target_pos += 7; //skip mailto:
  116. $end = $blength;
  117. $mailto = substr($body, $target_pos, $end-$target_pos);
  118. global $MailTo_PReg_Match;
  119. if ((preg_match($MailTo_PReg_Match, $mailto, $regs)) && ($regs[0] != '')) {
  120. //sm_print_r($regs);
  121. $mailto_before = $target_token . $regs[0];
  122. $mailto_params = $regs[10];
  123. if ($regs[1]) { //if there is an email addr before '?', we need to merge it with the params
  124. $to = 'to=' . $regs[1];
  125. if (strpos($mailto_params, 'to=') > -1) //already a 'to='
  126. $mailto_params = str_replace('to=', $to . '%2C%20', $mailto_params);
  127. else {
  128. if ($mailto_params) //already some params, append to them
  129. $mailto_params .= '&amp;' . $to;
  130. else
  131. $mailto_params .= '?' . $to;
  132. }
  133. }
  134. $url_str = str_replace(array('to=', 'cc=', 'bcc='), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params);
  135. $comp_uri = makeComposeLink('src/compose.php' . $url_str, $mailto_before);
  136. replaceBlock($body, $comp_uri, $target_pos - 7, $target_pos + strlen($regs[0]));
  137. $target_pos += strlen($comp_uri) - 7;
  138. }
  139. }
  140. else
  141. if ($target_token != '') {
  142. /* Find the end of the URL */
  143. $end = $blength;
  144. foreach ($url_parser_poss_ends as $val) {
  145. $enda = strpos($body, $val, $target_pos);
  146. if (is_int($enda) && $enda < $end) {
  147. $end = $enda;
  148. }
  149. }
  150. /* Extract URL */
  151. $url = substr($body, $target_pos, $end-$target_pos);
  152. /* Needed since lines are not passed with \n or \r */
  153. while ( ereg("[,\.]$", $url) ) {
  154. $url = substr( $url, 0, -1 );
  155. $end--;
  156. }
  157. /* Replace URL with HyperLinked Url, requires 1 char in link */
  158. if ($url != '' && $url != $target_token) {
  159. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  160. replaceBlock($body,$url_str,$target_pos,$end);
  161. $target_pos += strlen($url_str);
  162. }
  163. else {
  164. // Not quite a valid link, skip ahead to next chance
  165. $target_pos += strlen($target_token);
  166. }
  167. }
  168. /* Move forward */
  169. $start = $target_pos;
  170. $blength = strlen($body);
  171. }
  172. }
  173. ?>