59177427 |
1 | <?php |
43fcef5c |
2 | |
35586184 |
3 | /** |
4 | * url_parser.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 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 | * |
12 | * $Id$ |
d6c32258 |
13 | * @package squirrelmail |
35586184 |
14 | */ |
43fcef5c |
15 | |
d6c32258 |
16 | /** |
17 | * Undocumented - complain, then patch. |
18 | */ |
35586184 |
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 | } |
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: */ |
34 | global $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 | |
01d27858 |
41 | function parseEmail (&$body) { |
d62c4938 |
42 | global $color, $Email_RegExp_Match; |
cf0d436a |
43 | $sbody = $body; |
44 | $addresses = array(); |
45 | |
46 | /* Find all the email addresses in the body */ |
47 | while(eregi($Email_RegExp_Match, $sbody, $regs)) { |
48 | $addresses[$regs[0]] = $regs[0]; |
49 | $start = strpos($sbody, $regs[0]) + strlen($regs[0]); |
50 | $sbody = substr($sbody, $start); |
51 | } |
52 | /* Replace each email address with a compose URL */ |
53 | foreach ($addresses as $email) { |
d62c4938 |
54 | $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email); |
cf0d436a |
55 | $body = str_replace($email, $comp_uri, $body); |
7e235a1a |
56 | } |
cf0d436a |
57 | /* Return number of unique addresses found */ |
58 | return count($addresses); |
01d27858 |
59 | } |
43fcef5c |
60 | |
43fcef5c |
61 | |
01d27858 |
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 | 'gopher://', |
72 | 'news://'); |
8f7163e7 |
73 | |
01d27858 |
74 | global $url_parser_poss_ends; |
75 | $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n", |
76 | '. ', ' ', ')', '(', '"', '<', '>', '.<', |
77 | ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r"); |
8f7163e7 |
78 | |
20a60f89 |
79 | |
01d27858 |
80 | function parseUrl (&$body) { |
81 | global $url_parser_poss_ends, $url_parser_url_tokens;; |
cf0d436a |
82 | $start = 0; |
83 | $blength = strlen($body); |
cf0d436a |
84 | |
85 | while ($start != $blength) { |
8f7163e7 |
86 | $target_token = ''; |
0d3ff000 |
87 | $target_pos = $blength; |
cf0d436a |
88 | |
01d27858 |
89 | /* Find the first token to replace */ |
90 | foreach ($url_parser_url_tokens as $the_token) { |
91 | $pos = strpos(strtolower($body), $the_token, $start); |
cf0d436a |
92 | if (is_int($pos) && $pos < $blength) { |
93 | $target_pos = $pos; |
01d27858 |
94 | $target_token = $the_token; |
95 | } |
8f7163e7 |
96 | } |
cf0d436a |
97 | |
01d27858 |
98 | /* Look for email addresses between $start and $target_pos */ |
cf0d436a |
99 | $check_str = substr($body, $start, $target_pos-$start); |
100 | |
01d27858 |
101 | if (parseEmail($check_str)) { |
102 | replaceBlock($body, $check_str, $start, $target_pos); |
cf0d436a |
103 | $blength = strlen($body); |
01d27858 |
104 | $target_pos = strlen($check_str) + $start; |
8f7163e7 |
105 | } |
e2ef6f4b |
106 | |
01d27858 |
107 | /* If there was a token to replace, replace it */ |
108 | if ($target_token != '') { |
109 | /* Find the end of the URL */ |
cf0d436a |
110 | $end = $blength; |
111 | foreach ($url_parser_poss_ends as $val) { |
112 | $enda = strpos($body, $val, $target_pos); |
01d27858 |
113 | if (is_int($enda) && $enda < $end) { |
114 | $end = $enda; |
115 | } |
116 | } |
cf0d436a |
117 | |
01d27858 |
118 | /* Extract URL */ |
119 | $url = substr($body, $target_pos, $end-$target_pos); |
cf0d436a |
120 | |
01d27858 |
121 | /* Needed since lines are not passed with \n or \r */ |
122 | while ( ereg("[,\.]$", $url) ) { |
123 | $url = substr( $url, 0, -1 ); |
124 | $end--; |
125 | } |
1b3324b3 |
126 | |
01d27858 |
127 | /* Replace URL with HyperLinked Url, requires 1 char in link */ |
128 | if ($url != '' && $url != $target_token) { |
129 | $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>"; |
130 | replaceBlock($body,$url_str,$target_pos,$end); |
131 | $target_pos += strlen($url_str); |
cf0d436a |
132 | } |
133 | else { |
134 | // Not quite a valid link, skip ahead to next chance |
135 | $target_pos += strlen($target_token); |
01d27858 |
136 | } |
8f7163e7 |
137 | } |
cf0d436a |
138 | |
01d27858 |
139 | /* Move forward */ |
cf0d436a |
140 | $start = $target_pos; |
141 | $blength = strlen($body); |
01d27858 |
142 | } |
143 | } |
43fcef5c |
144 | ?> |