Phpdocumentor update - sed is your friend for these kinds of things ;)
[squirrelmail.git] / functions / url_parser.php
CommitLineData
59177427 1<?php
43fcef5c 2
35586184 3/**
4 * url_parser.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This code provides various string manipulation functions that are
10 * used by the rest of the Squirrelmail code.
11 *
31841a9e 12 * @version $Id$
d6c32258 13 * @package squirrelmail
35586184 14 */
43fcef5c 15
d6c32258 16/**
17 * Undocumented - complain, then patch.
18 */
35586184 19function replaceBlock (&$in, $replace, $start, $end) {
20 $begin = substr($in,0,$start);
21 $end = substr($in,$end,strlen($in)-$end);
22 $in = $begin.$replace.$end;
23}
43fcef5c 24
01d27858 25/* Having this defined in just one spot could help when changes need
26 * to be made to the pattern
27 * Make sure that the expression is evaluated case insensitively
7e235a1a 28 *
01d27858 29 * Here's pretty sophisticated IP matching:
30 * $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
31 * $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
32 */
33/* Here's enough: */
34global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
35$IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
7e235a1a 36$Host_RegExp_Match = '(' . $IP_RegExp_Match .
01d27858 37 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
7e235a1a 38$Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
01d27858 39 ')?@' . $Host_RegExp_Match;
7e235a1a 40
8b096f0a 41/**
42 * Parses a body and converts all found email addresses to clickable links.
43 *
44 * @param string body the body to process, by ref
45 * @return int the number of unique addresses found
46 */
01d27858 47function parseEmail (&$body) {
d62c4938 48 global $color, $Email_RegExp_Match;
cf0d436a 49 $sbody = $body;
50 $addresses = array();
51
52 /* Find all the email addresses in the body */
53 while(eregi($Email_RegExp_Match, $sbody, $regs)) {
54 $addresses[$regs[0]] = $regs[0];
55 $start = strpos($sbody, $regs[0]) + strlen($regs[0]);
56 $sbody = substr($sbody, $start);
57 }
58 /* Replace each email address with a compose URL */
59 foreach ($addresses as $email) {
d62c4938 60 $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email);
cf0d436a 61 $body = str_replace($email, $comp_uri, $body);
7e235a1a 62 }
cf0d436a 63 /* Return number of unique addresses found */
64 return count($addresses);
01d27858 65}
43fcef5c 66
43fcef5c 67
01d27858 68/* We don't want to re-initialize this stuff for every line. Save work
69 * and just do it once here.
70 */
71global $url_parser_url_tokens;
72$url_parser_url_tokens = array(
73 'http://',
74 'https://',
75 'ftp://',
76 'telnet:', // Special case -- doesn't need the slashes
7efaee4f 77 'mailto:', // Special case -- doesn't use the slashes
01d27858 78 'gopher://',
79 'news://');
8f7163e7 80
01d27858 81global $url_parser_poss_ends;
82$url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
83 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
84 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
8f7163e7 85
20a60f89 86
7efaee4f 87/**
88 * rfc 2368 (mailto URL) preg_match() regexp
99554426 89 * @link http://www.ietf.org/rfc/rfc2368.txt
7efaee4f 90 * @global string MailTo_PReg_Match the encapsulated regexp for preg_match()
91 */
92global $MailTo_PReg_Match;
93$Mailto_Email_RegExp = '[0-9a-z%]([-_.+%]?[0-9a-z])*(%' . $Host_RegExp_Match . ')?@' . $Host_RegExp_Match;
94$MailTo_PReg_Match = '/((?:' . $Mailto_Email_RegExp . ')*)((?:\?(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)?(?:&amp;(?:to|cc|bcc|subject|body)=[^\s\?&=,()]+)*)/i';
95
8b096f0a 96/**
97 * Parses a body and converts all found URLs to clickable links.
98 *
99 * @param string body the body to process, by ref
100 * @return void
101 */
01d27858 102function parseUrl (&$body) {
99554426 103 global $url_parser_poss_ends, $url_parser_url_tokens;
cf0d436a 104 $start = 0;
105 $blength = strlen($body);
cf0d436a 106
7efaee4f 107 while ($start < $blength) {
8f7163e7 108 $target_token = '';
0d3ff000 109 $target_pos = $blength;
cf0d436a 110
01d27858 111 /* Find the first token to replace */
112 foreach ($url_parser_url_tokens as $the_token) {
113 $pos = strpos(strtolower($body), $the_token, $start);
cf0d436a 114 if (is_int($pos) && $pos < $blength) {
115 $target_pos = $pos;
01d27858 116 $target_token = $the_token;
117 }
8f7163e7 118 }
cf0d436a 119
01d27858 120 /* Look for email addresses between $start and $target_pos */
cf0d436a 121 $check_str = substr($body, $start, $target_pos-$start);
122
01d27858 123 if (parseEmail($check_str)) {
124 replaceBlock($body, $check_str, $start, $target_pos);
cf0d436a 125 $blength = strlen($body);
01d27858 126 $target_pos = strlen($check_str) + $start;
8f7163e7 127 }
e2ef6f4b 128
01d27858 129 /* If there was a token to replace, replace it */
7efaee4f 130 if ($target_token == 'mailto:') { // rfc 2368 (mailto URL)
99554426 131 $target_pos += 7; //skip mailto:
7efaee4f 132 $end = $blength;
133
134 $mailto = substr($body, $target_pos, $end-$target_pos);
135
136 global $MailTo_PReg_Match;
99554426 137 if ((preg_match($MailTo_PReg_Match, $mailto, $regs)) && ($regs[0] != '')) {
7efaee4f 138 //sm_print_r($regs);
139 $mailto_before = $target_token . $regs[0];
140 $mailto_params = $regs[10];
141 if ($regs[1]) { //if there is an email addr before '?', we need to merge it with the params
142 $to = 'to=' . $regs[1];
143 if (strpos($mailto_params, 'to=') > -1) //already a 'to='
144 $mailto_params = str_replace('to=', $to . '%2C%20', $mailto_params);
145 else {
146 if ($mailto_params) //already some params, append to them
99554426 147 $mailto_params .= '&amp;' . $to;
7efaee4f 148 else
99554426 149 $mailto_params .= '?' . $to;
7efaee4f 150 }
151 }
152 $url_str = str_replace(array('to=', 'cc=', 'bcc='), array('send_to=', 'send_to_cc=', 'send_to_bcc='), $mailto_params);
153 $comp_uri = makeComposeLink('src/compose.php' . $url_str, $mailto_before);
99554426 154 replaceBlock($body, $comp_uri, $target_pos - 7, $target_pos + strlen($regs[0]));
155 $target_pos += strlen($comp_uri) - 7;
7efaee4f 156 }
157 }
158 else
01d27858 159 if ($target_token != '') {
160 /* Find the end of the URL */
cf0d436a 161 $end = $blength;
162 foreach ($url_parser_poss_ends as $val) {
163 $enda = strpos($body, $val, $target_pos);
01d27858 164 if (is_int($enda) && $enda < $end) {
165 $end = $enda;
166 }
167 }
cf0d436a 168
01d27858 169 /* Extract URL */
170 $url = substr($body, $target_pos, $end-$target_pos);
cf0d436a 171
01d27858 172 /* Needed since lines are not passed with \n or \r */
173 while ( ereg("[,\.]$", $url) ) {
174 $url = substr( $url, 0, -1 );
175 $end--;
176 }
1b3324b3 177
01d27858 178 /* Replace URL with HyperLinked Url, requires 1 char in link */
179 if ($url != '' && $url != $target_token) {
180 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
181 replaceBlock($body,$url_str,$target_pos,$end);
182 $target_pos += strlen($url_str);
cf0d436a 183 }
184 else {
185 // Not quite a valid link, skip ahead to next chance
186 $target_pos += strlen($target_token);
01d27858 187 }
8f7163e7 188 }
cf0d436a 189
01d27858 190 /* Move forward */
cf0d436a 191 $start = $target_pos;
192 $blength = strlen($body);
01d27858 193 }
194}
43fcef5c 195?>