url_parser.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * url_parser.php
  4. * Copyright (c) 1999-2001 The Squirrelmail Development Team
  5. * Licensed under the GNU GPL. For full terms see the file COPYING.
  6. *
  7. * This code provides various string manipulation functions that are
  8. * used by the rest of the Squirrelmail code.
  9. *
  10. * $Id$
  11. */
  12. function replaceBlock (&$in, $replace, $start, $end) {
  13. $begin = substr($in,0,$start);
  14. $end = substr($in,$end,strlen($in)-$end);
  15. $in = $begin.$replace.$end;
  16. }
  17. // Having this defined in just one spot could help when changes need
  18. // to be made to the pattern
  19. // Make sure that the expression is evaluated case insensitively
  20. //
  21. // Here's pretty sophisticated IP matching:
  22. // $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
  23. // $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
  24. //
  25. // Here's enough:
  26. global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
  27. $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
  28. $Host_RegExp_Match = '(' . $IP_RegExp_Match .
  29. '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
  30. $Email_RegExp_Match = '[0-9a-z]([-_.]?[0-9a-z])*(%' . $Host_RegExp_Match .
  31. ')?@' . $Host_RegExp_Match;
  32. function parseEmail (&$body) {
  33. global $color, $Email_RegExp_Match;
  34. $Size = strlen($body);
  35. /*
  36. This is here in case we ever decide to use highlighting of searched
  37. text. this does it for email addresses
  38. if ($what && ($where == "BODY" || $where == "TEXT")) {
  39. eregi ($Email_RegExp_Match, $body, $regs);
  40. $oldaddr = $regs[0];
  41. if ($oldaddr) {
  42. $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
  43. $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
  44. }
  45. } else {
  46. $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  47. }
  48. */
  49. $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  50. // If there are any changes, it'll just get bigger.
  51. if ($Size != strlen($body))
  52. return 1;
  53. return 0;
  54. }
  55. // We don't want to re-initialize this stuff for every line. Save work
  56. // and just do it once here.
  57. global $url_parser_url_tokens;
  58. $url_parser_url_tokens = array(
  59. 'http://',
  60. 'https://',
  61. 'ftp://',
  62. 'telnet:', // Special case -- doesn't need the slashes
  63. 'gopher://',
  64. 'news://');
  65. global $url_parser_poss_ends;
  66. $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
  67. '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
  68. ']', '[', '{', '}', "\240");
  69. function parseUrl (&$body)
  70. {
  71. global $url_parser_poss_ends, $url_parser_url_tokens;;
  72. $start = 0;
  73. $target_pos = strlen($body);
  74. while ($start != $target_pos)
  75. {
  76. $target_token = '';
  77. // Find the first token to replace
  78. foreach ($url_parser_url_tokens as $the_token)
  79. {
  80. $pos = strpos(strtolower($body), $the_token, $start);
  81. if (is_int($pos) && $pos < $target_pos)
  82. {
  83. $target_pos = $pos;
  84. $target_token = $the_token;
  85. }
  86. }
  87. // Look for email addresses between $start and $target_pos
  88. $check_str = substr($body, $start, $target_pos);
  89. if (parseEmail($check_str))
  90. {
  91. replaceBlock($body, $check_str, $start, $target_pos);
  92. $target_pos = strlen($check_str) + $start;
  93. }
  94. // If there was a token to replace, replace it
  95. if ($target_token != '')
  96. {
  97. // Find the end of the URL
  98. $end=strlen($body);
  99. foreach ($url_parser_poss_ends as $key => $val)
  100. {
  101. $enda = strpos($body,$val,$target_pos);
  102. if (is_int($enda) && $enda < $end)
  103. $end = $enda;
  104. }
  105. // Extract URL
  106. $url = substr($body, $target_pos, $end-$target_pos);
  107. // Replace URL with HyperLinked Url, requires 1 char in link
  108. if ($url != '' && $url != $target_token)
  109. {
  110. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  111. replaceBlock($body,$url_str,$target_pos,$end);
  112. $target_pos += strlen($url_str);
  113. }
  114. else
  115. {
  116. // Not quite a valid link, skip ahead to next chance
  117. $target_pos += strlen($target_token);
  118. }
  119. }
  120. // Move forward
  121. $start = $target_pos;
  122. $target_pos = strlen($body);
  123. }
  124. }
  125. ?>