url_parser.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. /*****************************************************************/
  14. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  15. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  16. /*** + Base level indent should begin at left margin, as ***/
  17. /*** the first line of the function definition below. ***/
  18. /*** + All identation should consist of four space blocks ***/
  19. /*** + Tab characters are evil. ***/
  20. /*** + all comments should use "slash-star ... star-slash" ***/
  21. /*** style -- no pound characters, no slash-slash style ***/
  22. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  23. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  24. /*** + Please use ' instead of ", when possible. Note " ***/
  25. /*** should always be used in _( ) function calls. ***/
  26. /*** Thank you for your help making the SM code more readable. ***/
  27. /*****************************************************************/
  28. function replaceBlock (&$in, $replace, $start, $end) {
  29. $begin = substr($in,0,$start);
  30. $end = substr($in,$end,strlen($in)-$end);
  31. $in = $begin.$replace.$end;
  32. }
  33. // Having this defined in just one spot could help when changes need
  34. // to be made to the pattern
  35. // Make sure that the expression is evaluated case insensitively
  36. //
  37. // Here's pretty sophisticated IP matching:
  38. // $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
  39. // $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
  40. //
  41. // Here's enough:
  42. global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
  43. $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
  44. $Host_RegExp_Match = '(' . $IP_RegExp_Match .
  45. '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
  46. $Email_RegExp_Match = '[0-9a-z]([-_.]?[0-9a-z])*(%' . $Host_RegExp_Match .
  47. ')?@' . $Host_RegExp_Match;
  48. function parseEmail (&$body) {
  49. global $color, $Email_RegExp_Match;
  50. $Size = strlen($body);
  51. /*
  52. This is here in case we ever decide to use highlighting of searched
  53. text. this does it for email addresses
  54. if ($what && ($where == "BODY" || $where == "TEXT")) {
  55. eregi ($Email_RegExp_Match, $body, $regs);
  56. $oldaddr = $regs[0];
  57. if ($oldaddr) {
  58. $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
  59. $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
  60. }
  61. } else {
  62. $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  63. }
  64. */
  65. $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  66. // If there are any changes, it'll just get bigger.
  67. if ($Size != strlen($body))
  68. return 1;
  69. return 0;
  70. }
  71. // We don't want to re-initialize this stuff for every line. Save work
  72. // and just do it once here.
  73. global $url_parser_url_tokens;
  74. $url_parser_url_tokens = array(
  75. 'http://',
  76. 'https://',
  77. 'ftp://',
  78. 'telnet:', // Special case -- doesn't need the slashes
  79. 'gopher://',
  80. 'news://');
  81. global $url_parser_poss_ends;
  82. $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
  83. '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
  84. ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
  85. function parseUrl (&$body)
  86. {
  87. global $url_parser_poss_ends, $url_parser_url_tokens;;
  88. $start = 0;
  89. $target_pos = strlen($body);
  90. while ($start != $target_pos)
  91. {
  92. $target_token = '';
  93. // Find the first token to replace
  94. foreach ($url_parser_url_tokens as $the_token)
  95. {
  96. $pos = strpos(strtolower($body), $the_token, $start);
  97. if (is_int($pos) && $pos < $target_pos)
  98. {
  99. $target_pos = $pos;
  100. $target_token = $the_token;
  101. }
  102. }
  103. // Look for email addresses between $start and $target_pos
  104. $check_str = substr($body, $start, $target_pos);
  105. if (parseEmail($check_str))
  106. {
  107. replaceBlock($body, $check_str, $start, $target_pos);
  108. $target_pos = strlen($check_str) + $start;
  109. }
  110. // If there was a token to replace, replace it
  111. if ($target_token != '')
  112. {
  113. // Find the end of the URL
  114. $end=strlen($body);
  115. foreach ($url_parser_poss_ends as $key => $val)
  116. {
  117. $enda = strpos($body,$val,$target_pos);
  118. if (is_int($enda) && $enda < $end)
  119. $end = $enda;
  120. }
  121. // Extract URL
  122. $url = substr($body, $target_pos, $end-$target_pos);
  123. // Needed since lines are not passed with \n or \r
  124. while ( ereg("[,\.]$", $url) ) {
  125. $url = substr( $url, 0, -1 );
  126. $end--;
  127. }
  128. // Replace URL with HyperLinked Url, requires 1 char in link
  129. if ($url != '' && $url != $target_token)
  130. {
  131. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  132. replaceBlock($body,$url_str,$target_pos,$end);
  133. $target_pos += strlen($url_str);
  134. }
  135. else
  136. {
  137. // Not quite a valid link, skip ahead to next chance
  138. $target_pos += strlen($target_token);
  139. }
  140. }
  141. // Move forward
  142. $start = $target_pos;
  143. $target_pos = strlen($body);
  144. }
  145. }
  146. ?>