url_parser.php 4.2 KB

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