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