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$ |
13 | */ |
43fcef5c |
14 | |
35586184 |
15 | function replaceBlock (&$in, $replace, $start, $end) { |
16 | $begin = substr($in,0,$start); |
17 | $end = substr($in,$end,strlen($in)-$end); |
18 | $in = $begin.$replace.$end; |
19 | } |
43fcef5c |
20 | |
01d27858 |
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 |
7e235a1a |
24 | * |
01d27858 |
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}\\]?'; |
7e235a1a |
32 | $Host_RegExp_Match = '(' . $IP_RegExp_Match . |
01d27858 |
33 | '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)'; |
7e235a1a |
34 | $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match . |
01d27858 |
35 | ')?@' . $Host_RegExp_Match; |
7e235a1a |
36 | |
01d27858 |
37 | function parseEmail (&$body) { |
d62c4938 |
38 | global $color, $Email_RegExp_Match; |
cf0d436a |
39 | $sbody = $body; |
40 | $addresses = array(); |
41 | |
42 | /* Find all the email addresses in the body */ |
43 | while(eregi($Email_RegExp_Match, $sbody, $regs)) { |
44 | $addresses[$regs[0]] = $regs[0]; |
45 | $start = strpos($sbody, $regs[0]) + strlen($regs[0]); |
46 | $sbody = substr($sbody, $start); |
47 | } |
48 | /* Replace each email address with a compose URL */ |
49 | foreach ($addresses as $email) { |
d62c4938 |
50 | $comp_uri = makeComposeLink('src/compose.php?send_to='.urlencode($email), $email); |
cf0d436a |
51 | $body = str_replace($email, $comp_uri, $body); |
7e235a1a |
52 | } |
cf0d436a |
53 | /* Return number of unique addresses found */ |
54 | return count($addresses); |
01d27858 |
55 | } |
43fcef5c |
56 | |
43fcef5c |
57 | |
01d27858 |
58 | /* We don't want to re-initialize this stuff for every line. Save work |
59 | * and just do it once here. |
60 | */ |
61 | global $url_parser_url_tokens; |
62 | $url_parser_url_tokens = array( |
63 | 'http://', |
64 | 'https://', |
65 | 'ftp://', |
66 | 'telnet:', // Special case -- doesn't need the slashes |
67 | 'gopher://', |
68 | 'news://'); |
8f7163e7 |
69 | |
01d27858 |
70 | global $url_parser_poss_ends; |
71 | $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n", |
72 | '. ', ' ', ')', '(', '"', '<', '>', '.<', |
73 | ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r"); |
8f7163e7 |
74 | |
20a60f89 |
75 | |
01d27858 |
76 | function parseUrl (&$body) { |
77 | global $url_parser_poss_ends, $url_parser_url_tokens;; |
cf0d436a |
78 | $start = 0; |
79 | $blength = strlen($body); |
cf0d436a |
80 | |
81 | while ($start != $blength) { |
8f7163e7 |
82 | $target_token = ''; |
0d3ff000 |
83 | $target_pos = $blength; |
cf0d436a |
84 | |
01d27858 |
85 | /* Find the first token to replace */ |
86 | foreach ($url_parser_url_tokens as $the_token) { |
87 | $pos = strpos(strtolower($body), $the_token, $start); |
cf0d436a |
88 | if (is_int($pos) && $pos < $blength) { |
89 | $target_pos = $pos; |
01d27858 |
90 | $target_token = $the_token; |
91 | } |
8f7163e7 |
92 | } |
cf0d436a |
93 | |
01d27858 |
94 | /* Look for email addresses between $start and $target_pos */ |
cf0d436a |
95 | $check_str = substr($body, $start, $target_pos-$start); |
96 | |
01d27858 |
97 | if (parseEmail($check_str)) { |
98 | replaceBlock($body, $check_str, $start, $target_pos); |
cf0d436a |
99 | $blength = strlen($body); |
01d27858 |
100 | $target_pos = strlen($check_str) + $start; |
8f7163e7 |
101 | } |
e2ef6f4b |
102 | |
01d27858 |
103 | /* If there was a token to replace, replace it */ |
104 | if ($target_token != '') { |
105 | /* Find the end of the URL */ |
cf0d436a |
106 | $end = $blength; |
107 | foreach ($url_parser_poss_ends as $val) { |
108 | $enda = strpos($body, $val, $target_pos); |
01d27858 |
109 | if (is_int($enda) && $enda < $end) { |
110 | $end = $enda; |
111 | } |
112 | } |
cf0d436a |
113 | |
01d27858 |
114 | /* Extract URL */ |
115 | $url = substr($body, $target_pos, $end-$target_pos); |
cf0d436a |
116 | |
01d27858 |
117 | /* Needed since lines are not passed with \n or \r */ |
118 | while ( ereg("[,\.]$", $url) ) { |
119 | $url = substr( $url, 0, -1 ); |
120 | $end--; |
121 | } |
1b3324b3 |
122 | |
01d27858 |
123 | /* Replace URL with HyperLinked Url, requires 1 char in link */ |
124 | if ($url != '' && $url != $target_token) { |
125 | $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>"; |
126 | replaceBlock($body,$url_str,$target_pos,$end); |
127 | $target_pos += strlen($url_str); |
cf0d436a |
128 | } |
129 | else { |
130 | // Not quite a valid link, skip ahead to next chance |
131 | $target_pos += strlen($target_token); |
01d27858 |
132 | } |
8f7163e7 |
133 | } |
cf0d436a |
134 | |
01d27858 |
135 | /* Move forward */ |
cf0d436a |
136 | $start = $target_pos; |
137 | $blength = strlen($body); |
01d27858 |
138 | } |
139 | } |
43fcef5c |
140 | ?> |