url_parser.php 4.1 KB

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