url_parser.php 4.6 KB

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