url_parser.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 . '|[0-9a-z]([-.]?[0-9a-z])*\.[a-wyz][a-z](g|l|m|pa|t|u|v)?)';
  25. $Expression = '[0-9a-z]([-_.]?[0-9a-z])*(%' . $Host . ')?@' . $Host;
  26. /*
  27. This is here in case we ever decide to use highlighting of searched
  28. text. this does it for email addresses
  29. if ($what && ($where == "BODY" || $where == "TEXT")) {
  30. eregi ($Expression, $body, $regs);
  31. $oldaddr = $regs[0];
  32. if ($oldaddr) {
  33. $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
  34. $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
  35. }
  36. } else {
  37. $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  38. }
  39. */
  40. $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  41. // If there are any changes, it'll just get bigger.
  42. if ($Size != strlen($body))
  43. return 1;
  44. return 0;
  45. }
  46. function parseUrl (&$body)
  47. {
  48. $url_tokens = array(
  49. 'http://',
  50. 'https://',
  51. 'ftp://',
  52. 'telnet:', // Special case -- doesn't need the slashes
  53. 'gopher://',
  54. 'news://');
  55. $poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n", '.&nbsp;',
  56. '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<', ']', '[', '{',
  57. '}', "\240");
  58. $start = 0;
  59. $target_pos = strlen($body);
  60. while ($start != $target_pos)
  61. {
  62. $target_token = '';
  63. // Find the first token to replace
  64. foreach ($url_tokens as $the_token)
  65. {
  66. $pos = strpos(strtolower($body), $the_token, $start);
  67. if (is_int($pos) && $pos < $target_pos)
  68. {
  69. $target_pos = $pos;
  70. $target_token = $the_token;
  71. }
  72. }
  73. // Look for email addresses between $start and $target_pos
  74. $check_str = substr($body, $start, $target_pos);
  75. if (parseEmail($check_str))
  76. {
  77. $body = replaceBlock($body, $check_str, $start, $target_pos);
  78. $target_pos = strlen($check_str) + $start;
  79. }
  80. // If there was a token to replace, replace it
  81. if ($target_token != '')
  82. {
  83. // Find the end of the URL
  84. $end=strlen($body);
  85. foreach ($poss_ends as $key => $val)
  86. {
  87. $enda = strpos($body,$val,$target_pos);
  88. if (is_int($enda) && $enda < $end)
  89. $end = $enda;
  90. }
  91. // Extract URL
  92. $url = substr($body, $target_pos, $end-$target_pos);
  93. // Replace URL with HyperLinked Url, requires 1 char in link
  94. if ($url != '' && $url != $target_token)
  95. {
  96. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  97. $body = replaceBlock($body,$url_str,$target_pos,$end);
  98. $target_pos += strlen($url_str);
  99. }
  100. else
  101. {
  102. // Not quite a valid link, skip ahead to next chance
  103. $target_pos += strlen($target_token);
  104. }
  105. }
  106. // Move forward
  107. $start = $target_pos;
  108. $target_pos = strlen($body);
  109. }
  110. }
  111. ?>