url_parser.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // Changed the expression to the one in abook_take
  13. // This works very well, especially it looks like you might have
  14. // three instances of it below. Having it defined in
  15. // just one spot could help when you need to change it.
  16. $Expression = "[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-wyz][a-z](g|l|m|pa|t|u|v)?";
  17. /*
  18. This is here in case we ever decide to use highlighting of searched
  19. text. this does it for email addresses
  20. if ($what && ($where == "BODY" || $where == "TEXT")) {
  21. // Use the $Expression
  22. eregi ($Expression, $body, $regs);
  23. $oldaddr = $regs[0];
  24. if ($oldaddr) {
  25. $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
  26. $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
  27. }
  28. } else {
  29. // Use the $Expression
  30. $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  31. }
  32. */
  33. // Use the $Expression
  34. $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  35. return $body;
  36. }
  37. function parseUrl ($body) {
  38. #Possible ways a URL could finish.
  39. // Removed "--" since it could be part of a URL
  40. $poss_ends=array(" ", "\n", "\r", "<", ">", ".\r", ".\n", ".&nbsp;", "&nbsp;", ")", "(",
  41. "&quot;", "&lt;", "&gt;", ".<", "]", "[", "{", "}");
  42. $done=False;
  43. while (!$done) {
  44. #Look for when a URL starts
  45. // Added gopher, news. Modified telnet.
  46. $url_tokens = array(
  47. "http://",
  48. "https://",
  49. "ftp://",
  50. "telnet:", // Special case -- doesn't need the slashes
  51. "gopher://",
  52. "news://");
  53. for($i = 0; $i < sizeof($url_tokens); $i++) {
  54. // Removed the use of "^^" -- it is unneeded
  55. if(is_int($where = strpos(strtolower($body), $url_tokens[$i], $start)))
  56. break;
  57. }
  58. // Look between $start and $where for email links
  59. $check_str = substr($body, $start, $where);
  60. $new_str = parseEmail($check_str);
  61. if ($check_str != $new_str)
  62. {
  63. $body = replaceBlock($body, $new_str, $start, $where);
  64. $where = strlen($new_str) + $start;
  65. }
  66. //$where = strpos(strtolower($body),"http://",$start);
  67. // Fixed this to work with $i instead of $where
  68. if ($i < sizeof($url_tokens)) {
  69. // Removed the "^^" so I removed the next line
  70. //$where = $where - 2; // because we added the ^^ at the begining
  71. # Find the end of that URL
  72. reset($poss_ends); $end=0;
  73. while (list($key, $val) = each($poss_ends)) {
  74. $enda = strpos($body,$val,$where);
  75. if ($end == 0) $end = $enda;
  76. if ($enda < $end and $enda != 0) $end = $enda;
  77. }
  78. if (!$end) $end = strlen($body);
  79. #Extract URL
  80. $url = substr($body,$where,$end-$where);
  81. #Replace URL with HyperLinked Url
  82. // Now this code doesn't simply match on url_tokens
  83. // It will need some more text. This is good.
  84. if ($url != "" && $url != $url_tokens[$i]) {
  85. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  86. # $body = str_replace($url,$url_str,$body);
  87. # echo "$where, $end<br>";
  88. $body = replaceBlock($body,$url_str,$where,$end);
  89. // Removed unnecessary strpos call. Searching
  90. // a string takes longer than just figuring out
  91. // the length.
  92. // $start = strpos($body,"</a>",$where);
  93. $start = $where + strlen($url_str);
  94. } else {
  95. // Proper length increment -- Don't just assume 7
  96. $start = $where + strlen($url_tokens[$i]);
  97. }
  98. } else {
  99. $done=true;
  100. }
  101. }
  102. // Look after $start for more email links.
  103. $check_str = substr($body, $start);
  104. $new_str = parseEmail($check_str);
  105. if ($check_str != $new_str)
  106. {
  107. $body = replaceBlock($body, $new_str, $start, strlen($body));
  108. }
  109. return $body;
  110. }
  111. ?>