url_parser.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * url_parser.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project 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, $compose_new_win;
  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. *
  40. * if ($what && ($where == "BODY" || $where == "TEXT")) {
  41. * eregi ($Email_RegExp_Match, $body, $regs);
  42. * $oldaddr = $regs[0];
  43. * if ($oldaddr) {
  44. * $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
  45. * $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
  46. * }
  47. * } else {
  48. * $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
  49. * }
  50. */
  51. if( eregi($Email_RegExp_Match, $body, $regs) ) {
  52. if ($compose_new_win == '1') {
  53. $body = str_replace($regs[0], '<a href="../src/compose.php?send_to='.urlencode($regs[0]).'" target="compose_window" onClick="comp_in_new()">'.$regs[0].'</a>', $body);
  54. }
  55. else {
  56. $body = str_replace($regs[0], '<a href="../src/compose.php?send_to='.
  57. urlencode($regs[0]).'">'.$regs[0].'</a>', $body);
  58. }
  59. }
  60. /* If there are any changes, it'll just get bigger. */
  61. if ($Size != strlen($body)) {
  62. return 1;
  63. }
  64. return 0;
  65. }
  66. /* We don't want to re-initialize this stuff for every line. Save work
  67. * and just do it once here.
  68. */
  69. global $url_parser_url_tokens;
  70. $url_parser_url_tokens = array(
  71. 'http://',
  72. 'https://',
  73. 'ftp://',
  74. 'telnet:', // Special case -- doesn't need the slashes
  75. 'gopher://',
  76. 'news://');
  77. global $url_parser_poss_ends;
  78. $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
  79. '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
  80. ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
  81. function parseUrl (&$body) {
  82. global $url_parser_poss_ends, $url_parser_url_tokens;;
  83. $start = 0;
  84. $target_pos = strlen($body);
  85. while ($start != $target_pos) {
  86. $target_token = '';
  87. /* Find the first token to replace */
  88. foreach ($url_parser_url_tokens as $the_token) {
  89. $pos = strpos(strtolower($body), $the_token, $start);
  90. if (is_int($pos) && $pos < $target_pos) {
  91. $target_pos = $pos;
  92. $target_token = $the_token;
  93. }
  94. }
  95. /* Look for email addresses between $start and $target_pos */
  96. $check_str = substr($body, $start, $target_pos);
  97. if (parseEmail($check_str)) {
  98. replaceBlock($body, $check_str, $start, $target_pos);
  99. $target_pos = strlen($check_str) + $start;
  100. }
  101. /* If there was a token to replace, replace it */
  102. if ($target_token != '') {
  103. /* Find the end of the URL */
  104. $end=strlen($body);
  105. foreach ($url_parser_poss_ends as $key => $val) {
  106. $enda = strpos($body,$val,$target_pos);
  107. if (is_int($enda) && $enda < $end) {
  108. $end = $enda;
  109. }
  110. }
  111. /* Extract URL */
  112. $url = substr($body, $target_pos, $end-$target_pos);
  113. /* Needed since lines are not passed with \n or \r */
  114. while ( ereg("[,\.]$", $url) ) {
  115. $url = substr( $url, 0, -1 );
  116. $end--;
  117. }
  118. /* Replace URL with HyperLinked Url, requires 1 char in link */
  119. if ($url != '' && $url != $target_token) {
  120. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  121. replaceBlock($body,$url_str,$target_pos,$end);
  122. $target_pos += strlen($url_str);
  123. }
  124. else {
  125. // Not quite a valid link, skip ahead to next chance
  126. $target_pos += strlen($target_token);
  127. }
  128. }
  129. /* Move forward */
  130. $start = $target_pos;
  131. $target_pos = strlen($body);
  132. }
  133. }
  134. ?>