url_parser.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * url_parser.php
  4. *
  5. * Copyright (c) 1999-2003 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. * $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. 'gopher://',
  71. 'news://');
  72. global $url_parser_poss_ends;
  73. $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
  74. '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
  75. ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
  76. /**
  77. * Parses a body and converts all found URLs to clickable links.
  78. *
  79. * @param string body the body to process, by ref
  80. * @return void
  81. */
  82. function parseUrl (&$body) {
  83. global $url_parser_poss_ends, $url_parser_url_tokens;;
  84. $start = 0;
  85. $blength = strlen($body);
  86. while ($start != $blength) {
  87. $target_token = '';
  88. $target_pos = $blength;
  89. /* Find the first token to replace */
  90. foreach ($url_parser_url_tokens as $the_token) {
  91. $pos = strpos(strtolower($body), $the_token, $start);
  92. if (is_int($pos) && $pos < $blength) {
  93. $target_pos = $pos;
  94. $target_token = $the_token;
  95. }
  96. }
  97. /* Look for email addresses between $start and $target_pos */
  98. $check_str = substr($body, $start, $target_pos-$start);
  99. if (parseEmail($check_str)) {
  100. replaceBlock($body, $check_str, $start, $target_pos);
  101. $blength = strlen($body);
  102. $target_pos = strlen($check_str) + $start;
  103. }
  104. /* If there was a token to replace, replace it */
  105. if ($target_token != '') {
  106. /* Find the end of the URL */
  107. $end = $blength;
  108. foreach ($url_parser_poss_ends as $val) {
  109. $enda = strpos($body, $val, $target_pos);
  110. if (is_int($enda) && $enda < $end) {
  111. $end = $enda;
  112. }
  113. }
  114. /* Extract URL */
  115. $url = substr($body, $target_pos, $end-$target_pos);
  116. /* Needed since lines are not passed with \n or \r */
  117. while ( ereg("[,\.]$", $url) ) {
  118. $url = substr( $url, 0, -1 );
  119. $end--;
  120. }
  121. /* Replace URL with HyperLinked Url, requires 1 char in link */
  122. if ($url != '' && $url != $target_token) {
  123. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  124. replaceBlock($body,$url_str,$target_pos,$end);
  125. $target_pos += strlen($url_str);
  126. }
  127. else {
  128. // Not quite a valid link, skip ahead to next chance
  129. $target_pos += strlen($target_token);
  130. }
  131. }
  132. /* Move forward */
  133. $start = $target_pos;
  134. $blength = strlen($body);
  135. }
  136. }
  137. ?>