url_parser.php 4.1 KB

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