Add phpdoc doc blocks to some files.
[squirrelmail.git] / functions / url_parser.php
1 <?php
2
3 /**
4 * url_parser.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
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 *
12 * $Id$
13 * @package squirrelmail
14 */
15
16 /**
17 * Undocumented - complain, then patch.
18 */
19 function 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 }
24
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
28 *
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: */
34 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
35 $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
36 $Host_RegExp_Match = '(' . $IP_RegExp_Match .
37 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
38 $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
39 ')?@' . $Host_RegExp_Match;
40
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 */
47 function parseEmail (&$body) {
48 global $color, $Email_RegExp_Match;
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) {
60 $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email);
61 $body = str_replace($email, $comp_uri, $body);
62 }
63 /* Return number of unique addresses found */
64 return count($addresses);
65 }
66
67
68 /* We don't want to re-initialize this stuff for every line. Save work
69 * and just do it once here.
70 */
71 global $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
77 'gopher://',
78 'news://');
79
80 global $url_parser_poss_ends;
81 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
82 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
83 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
84
85
86 /**
87 * Parses a body and converts all found URLs to clickable links.
88 *
89 * @param string body the body to process, by ref
90 * @return void
91 */
92 function parseUrl (&$body) {
93 global $url_parser_poss_ends, $url_parser_url_tokens;;
94 $start = 0;
95 $blength = strlen($body);
96
97 while ($start != $blength) {
98 $target_token = '';
99 $target_pos = $blength;
100
101 /* Find the first token to replace */
102 foreach ($url_parser_url_tokens as $the_token) {
103 $pos = strpos(strtolower($body), $the_token, $start);
104 if (is_int($pos) && $pos < $blength) {
105 $target_pos = $pos;
106 $target_token = $the_token;
107 }
108 }
109
110 /* Look for email addresses between $start and $target_pos */
111 $check_str = substr($body, $start, $target_pos-$start);
112
113 if (parseEmail($check_str)) {
114 replaceBlock($body, $check_str, $start, $target_pos);
115 $blength = strlen($body);
116 $target_pos = strlen($check_str) + $start;
117 }
118
119 /* If there was a token to replace, replace it */
120 if ($target_token != '') {
121 /* Find the end of the URL */
122 $end = $blength;
123 foreach ($url_parser_poss_ends as $val) {
124 $enda = strpos($body, $val, $target_pos);
125 if (is_int($enda) && $enda < $end) {
126 $end = $enda;
127 }
128 }
129
130 /* Extract URL */
131 $url = substr($body, $target_pos, $end-$target_pos);
132
133 /* Needed since lines are not passed with \n or \r */
134 while ( ereg("[,\.]$", $url) ) {
135 $url = substr( $url, 0, -1 );
136 $end--;
137 }
138
139 /* Replace URL with HyperLinked Url, requires 1 char in link */
140 if ($url != '' && $url != $target_token) {
141 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
142 replaceBlock($body,$url_str,$target_pos,$end);
143 $target_pos += strlen($url_str);
144 }
145 else {
146 // Not quite a valid link, skip ahead to next chance
147 $target_pos += strlen($target_token);
148 }
149 }
150
151 /* Move forward */
152 $start = $target_pos;
153 $blength = strlen($body);
154 }
155 }
156 ?>