Source for file url_parser.php

Documentation is available at url_parser.php

  1. <?php
  2. /**
  3. * url_parser.php
  4. *
  5. * This code provides various string manipulation functions that are
  6. * used by the rest of the SquirrelMail code.
  7. *
  8. * @copyright 1999-2020 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id: url_parser.php 14840 2020年01月07日 07:42:38Z pdontthink $
  11. * @package squirrelmail
  12. */
  13. /**
  14. * Undocumented - complain, then patch.
  15. */
  16. function replaceBlock (&$in, $replace, $start, $end) {
  17. $begin = substr ($in,0,$start);
  18. $end = substr ($in,$end,strlen ($in)-$end);
  19. $in = $begin.$replace.$end;
  20. }
  21. /* Having this defined in just one spot could help when changes need
  22. * to be made to the pattern
  23. * Make sure that the expression is evaluated case insensitively
  24. *
  25. * Here's pretty sophisticated IP matching:
  26. * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
  27. * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
  28. */
  29. /* Here's enough: */
  30. global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
  31. $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
  32. $Host_RegExp_Match = '(' . $IP_RegExp_Match .
  33. '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
  34. $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
  35. ')?@' . $Host_RegExp_Match;
  36. /**
  37. * rfc 2368 (mailto URL) preg_match() regexp
  38. * @link http://www.ietf.org/rfc/rfc2368.txt
  39. * @global string MailTo_PReg_Match the encapsulated regexp for preg_match()
  40. */
  41. global $MailTo_PReg_Match;
  42. $Mailto_Email_RegExp = '[0-9a-z%]([-_.+%]?[0-9a-z])*(%' . $Host_RegExp_Match . ')?@' . $Host_RegExp_Match;
  43. $MailTo_PReg_Match = '/((?:' . $Mailto_Email_RegExp . ')*)((?:\?(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)?(?:&amp;(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)*)/i';
  44. function parseEmail (&$body) {
  45. global $color, $Email_RegExp_Match, $compose_new_win;
  46. $sbody = $body;
  47. $addresses = array();
  48. /* Find all the email addresses in the body */
  49. while(preg_match ('/'.$Email_RegExp_Match.'/i', $sbody, $regs)) {
  50. $addresses[$regs[0]] = $regs[0];
  51. $start = strpos ($sbody, $regs[0]) + strlen ($regs[0]);
  52. $sbody = substr ($sbody, $start);
  53. }
  54. /* Replace each email address with a compose URL */
  55. foreach ($addresses as $email) {
  56. $comp_uri = makeComposeLink ('src/compose.php?send_to='.urlencode ($email), $email);
  57. $body = str_replace ($email, $comp_uri, $body);
  58. }
  59. /* Return number of unique addresses found */
  60. return count ($addresses);
  61. }
  62. /* We don't want to re-initialize this stuff for every line. Save work
  63. * and just do it once here.
  64. */
  65. global $url_parser_url_tokens;
  66. $url_parser_url_tokens = array(
  67. 'http://',
  68. 'https://',
  69. 'ftp://',
  70. 'telnet:', // Special case -- doesn't need the slashes
  71. 'mailto:', // Special case -- doesn't use the slashes
  72. 'gopher://',
  73. 'news://');
  74. global $url_parser_poss_ends;
  75. $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
  76. '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
  77. ']', '[', '{', '}', "240円", ', ', '. ', ",\n", ",\r");
  78. /**
  79. * Parses a body and converts all found URLs to clickable links.
  80. *
  81. * @param string body the body to process, by ref
  82. * @return void
  83. */
  84. function parseUrl (&$body) {
  85. global $url_parser_poss_ends, $url_parser_url_tokens;
  86. $start = 0;
  87. $blength = strlen ($body);
  88. while ($start < $blength) {
  89. $target_token = '';
  90. $target_pos = $blength;
  91. /* Find the first token to replace */
  92. foreach ($url_parser_url_tokens as $the_token) {
  93. $pos = strpos (strtolower ($body), $the_token, $start);
  94. if (is_int ($pos) && $pos < $target_pos) {
  95. $target_pos = $pos;
  96. $target_token = $the_token;
  97. }
  98. }
  99. /* Look for email addresses between $start and $target_pos */
  100. $check_str = substr ($body, $start, $target_pos-$start);
  101. if (parseEmail ($check_str)) {
  102. replaceBlock ($body, $check_str, $start, $target_pos);
  103. $blength = strlen ($body);
  104. $target_pos = strlen ($check_str) + $start;
  105. }
  106. // rfc 2368 (mailto URL)
  107. if ($target_token == 'mailto:') {
  108. $target_pos += 7; //skip mailto:
  109. $end = $blength;
  110. $mailto = substr ($body, $target_pos, $end-$target_pos);
  111. global $MailTo_PReg_Match;
  112. if ((preg_match ($MailTo_PReg_Match, $mailto, $regs)) && ($regs[0] != '')) {
  113. $mailto_before = $target_token . $regs[0];
  114. /**
  115. * '+' characters in a mailto URI don't need to be percent-encoded.
  116. * However, when mailto URI data is transported via HTTP, '+' must
  117. * be percent-encoded as %2B so that when the HTTP data is
  118. * percent-decoded, you get '+' back and not a space.
  119. */
  120. $mailto_params = str_replace ("+", "%2B", $regs[10]);
  121. if ($regs[1]) { //if there is an email addr before '?', we need to merge it with the params
  122. $to = 'to=' . str_replace ("+", "%2B", $regs[1]);
  123. if (strpos ($mailto_params, 'to=') > -1) //already a 'to='
  124. $mailto_params = str_replace ('to=', $to . '%2C%20', $mailto_params);
  125. else {
  126. if ($mailto_params) //already some params, append to them
  127. $mailto_params .= '&amp;' . $to;
  128. else
  129. $mailto_params .= '?' . $to;
  130. }
  131. }
  132. $url_str = preg_replace (array('/to=/i', '/(?<!b)cc=/i', '/bcc=/i'), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params);
  133. $comp_uri = makeComposeLink ('src/compose.php' . $url_str, $mailto_before);
  134. replaceBlock ($body, $comp_uri, $target_pos - 7, $target_pos + strlen ($regs[0]));
  135. $target_pos += strlen ($comp_uri) - 7;
  136. }
  137. }
  138. else
  139. /* If there was a token to replace, replace it */
  140. if ($target_token != '') {
  141. /* Find the end of the URL */
  142. $end = $blength;
  143. foreach ($url_parser_poss_ends as $val) {
  144. $enda = strpos ($body, $val, $target_pos);
  145. if (is_int ($enda) && $enda < $end) {
  146. $end = $enda;
  147. }
  148. }
  149. /* make sure that there are no 8bit chars between $target_pos and suspected end of URL */
  150. if (!is_bool ($first8bit=sq_strpos_8bit ($body,$target_pos,$end))) {
  151. $end = $first8bit;
  152. }
  153. /* Extract URL */
  154. $url = substr ($body, $target_pos, $end-$target_pos);
  155. /* Needed since lines are not passed with \n or \r */
  156. while ( preg_match ('/[,.]$/', $url) ) {
  157. $url = substr ( $url, 0, -1 );
  158. $end--;
  159. }
  160. /* Replace URL with HyperLinked Url, requires 1 char in link */
  161. if ($url != '' && $url != $target_token) {
  162. $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
  163. replaceBlock ($body,$url_str,$target_pos,$end);
  164. $target_pos += strlen ($url_str);
  165. }
  166. else {
  167. // Not quite a valid link, skip ahead to next chance
  168. $target_pos += strlen ($target_token);
  169. }
  170. }
  171. /* Move forward */
  172. $start = $target_pos;
  173. $blength = strlen ($body);
  174. }
  175. }
  176. /**
  177. * Finds first occurrence of 8bit data in the string
  178. *
  179. * Function finds first 8bit symbol or html entity that represents 8bit character.
  180. * Search start is defined by $offset argument. Search ends at $maxlength position.
  181. * If $maxlength is not defined or bigger than provided string, search ends when
  182. * string ends.
  183. *
  184. * Check returned data type in order to avoid confusion between bool(false)
  185. * (not found) and int(0) (first char in the string).
  186. * @param string $haystack
  187. * @param integer $offset
  188. * @param integer $maxlength
  189. * @return mixed integer with first 8bit character position or boolean false
  190. * @since 1.5.2 and 1.4.7
  191. */
  192. function sq_strpos_8bit ($haystack,$offset=0,$maxlength=false) {
  193. $ret = false;
  194. if ($maxlength===false || strlen ($haystack) < $maxlength) {
  195. $maxlength=strlen ($haystack);
  196. }
  197. for($i=$offset;$i<$maxlength;$i++) {
  198. /* rh7-8 compatibility. don't use full 8bit range in regexp */
  199. if (preg_match ('/[200円-237円]|240円|[241円-377円]/',$haystack[$i])) {
  200. /* we have 8bit char. stop here and return position */
  201. $ret = $i;
  202. break;
  203. } elseif ($haystack[$i]=='&') {
  204. $substring = substr ($haystack,$i);
  205. /**
  206. * 1. look for "&#(decimal number);" where decimal_number is bigger than 127
  207. * 2. look for "&x(hexadecimal number);", where hex number is bigger than x7f
  208. * 3. look for any html character entity that is not 7bit html special char. Use
  209. * own sq_get_html_translation_table() function with 'utf-8' character set in
  210. * order to get all html entities.
  211. */
  212. if ((preg_match ('/^&#(\d+);/',$substring,$match) && $match[1]>127) ||
  213. (preg_match ('/^&x([0-9a-f]+);/i',$substring,$match) && $match[1]>"\x7f") ||
  214. (preg_match ('/^&([a-z]+);/i',$substring,$match) &&
  215. !in_array ($match[0],get_html_translation_table (HTML_SPECIALCHARS)) &&
  216. in_array ($match[0],sq_get_html_translation_table(HTML_ENTITIES,ENT_COMPAT,'utf-8')))) {
  217. $ret = $i;
  218. break;
  219. }
  220. }
  221. }
  222. return $ret;
  223. }

Documentation generated on 2020年1月13日 04:25:24 +0100 by phpDocumentor 1.4.3

AltStyle によって変換されたページ (->オリジナル) /