url_parser.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. */
  13. function replaceBlock (&$in, $replace, $start, $end) {
  14. $begin = substr($in,0,$start);
  15. $end = substr($in,$end,strlen($in)-$end);
  16. $in = $begin.$replace.$end;
  17. }
  18. /* Having this defined in just one spot could help when changes need
  19. * to be made to the pattern
  20. * Make sure that the expression is evaluated case insensitively
  21. *
  22. * Here's pretty sophisticated IP matching:
  23. * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
  24. * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
  25. */
  26. /* Here's enough: */
  27. global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
  28. $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
  29. $Host_RegExp_Match = '(' . $IP_RegExp_Match .
  30. '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
  31. $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
  32. ')?@' . $Host_RegExp_Match;
  33. function parseEmail (&$body) {
  34. global $color, $Email_RegExp_Match;
  35. $sbody = $body;
  36. $addresses = array();
  37. /* Find all the email addresses in the body */
  38. while(eregi($Email_RegExp_Match, $sbody, $regs)) {
  39. $addresses[$regs[0]] = $regs[0];
  40. $start = strpos($sbody, $regs[0]) + strlen($regs[0]);
  41. $sbody = substr($sbody, $start);
  42. }
  43. /* Replace each email address with a compose URL */
  44. foreach ($addresses as $email) {
  45. $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email);
  46. $body = str_replace($email, $comp_uri, $body);
  47. }
  48. /* Return number of unique addresses found */
  49. return count($addresses);
  50. }
  51. /* We don't want to re-initialize this stuff for every line. Save work
  52. * and just do it once here.
  53. */
  54. global $url_parser_url_tokens;
  55. $url_parser_url_tokens = array(
  56. 'http://',
  57. 'https://',
  58. 'ftp://',
  59. 'telnet:', // Special case -- doesn't need the slashes
  60. 'gopher://',
  61. 'news://');
  62. global $url_parser_poss_ends;
  63. $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
  64. '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
  65. ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
  66. function parseUrl (&$body) {
  67. global $url_parser_poss_ends, $url_parser_url_tokens;;
  68. $start = 0;
  69. $blength = strlen($body);
  70. while ($start != $blength) {
  71. $target_token = '';
  72. $target_pos = $blength;
  73. /* Find the first token to replace */
  74. foreach ($url_parser_url_tokens as $the_token) {
  75. $pos = strpos(strtolower($body), $the_token, $start);
  76. if (is_int($pos) && $pos < $blength) {
  77. $target_pos = $pos;
  78. $target_token = $the_token;
  79. }
  80. }
  81. /* Look for email addresses between $start and $target_pos */
  82. $check_str = substr($body, $start, $target_pos-$start);
  83. if (parseEmail($check_str)) {
  84. replaceBlock($body, $check_str, $start, $target_pos);
  85. $blength = strlen($body);
  86. $target_pos = strlen($check_str) + $start;
  87. }
  88. /* If there was a token to replace, replace it */
  89. if ($target_token != '') {
  90. /* Find the end of the URL */
  91. $end = $blength;
  92. foreach ($url_parser_poss_ends as $val) {
  93. $enda = strpos($body, $val, $target_pos);
  94. if (is_int($enda) && $enda < $end) {
  95. $end = $enda;
  96. }
  97. }
  98. /* Extract URL */
  99. $url = substr($body, $target_pos, $end-$target_pos);
  100. /* Needed since lines are not passed with \n or \r */
  101. while ( ereg("[,\.]$", $url) ) {
  102. $url = substr( $url, 0, -1 );
  103. $end--;
  104. }
  105. /* Replace URL with HyperLinked Url, requires 1 char in link */
  106. if ($url != '' && $url != $target_token) {
  107. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  108. replaceBlock($body,$url_str,$target_pos,$end);
  109. $target_pos += strlen($url_str);
  110. }
  111. else {
  112. // Not quite a valid link, skip ahead to next chance
  113. $target_pos += strlen($target_token);
  114. }
  115. }
  116. /* Move forward */
  117. $start = $target_pos;
  118. $blength = strlen($body);
  119. }
  120. }
  121. ?>