url_parser.php 4.9 KB

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