Masato:
[squirrelmail.git] / functions / url_parser.php
1 <?php
2
3 /**
4 * url_parser.php
5 *
6 * Copyright (c) 1999-2002 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 */
14
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 }
20
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
24 *
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}\\]?';
32 $Host_RegExp_Match = '(' . $IP_RegExp_Match .
33 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
34 $Email_RegExp_Match = '[0-9a-z]([-_.+]?[0-9a-z])*(%' . $Host_RegExp_Match .
35 ')?@' . $Host_RegExp_Match;
36
37 function parseEmail (&$body) {
38 global $color, $Email_RegExp_Match, $compose_new_win;
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);
51 if ($compose_new_win == '1') {
52 $comp_uri = 'javascript:void(0)" onClick="comp_in_new('
53 . "'$comp_uri'" . ')';
54 }
55 $comp_uri = '<a href="'.$comp_uri.'">'.$email.'</a>';
56 $body = str_replace($email, $comp_uri, $body);
57 }
58 /* Return number of unique addresses found */
59 return count($addresses);
60 }
61
62
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://');
74
75 global $url_parser_poss_ends;
76 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
77 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
78 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
79
80
81 function parseUrl (&$body) {
82 global $url_parser_poss_ends, $url_parser_url_tokens;;
83 $start = 0;
84 $blength = strlen($body);
85
86 while ($start != $blength) {
87 $target_token = '';
88 $target_pos = $blength;
89
90 /* Find the first token to replace */
91 foreach ($url_parser_url_tokens as $the_token) {
92 $pos = strpos(strtolower($body), $the_token, $start);
93 if (is_int($pos) && $pos < $blength) {
94 $target_pos = $pos;
95 $target_token = $the_token;
96 }
97 }
98
99 /* Look for email addresses between $start and $target_pos */
100 $check_str = substr($body, $start, $target_pos-$start);
101
102 if (parseEmail($check_str)) {
103 replaceBlock($body, $check_str, $start, $target_pos);
104 $blength = strlen($body);
105 $target_pos = strlen($check_str) + $start;
106 }
107
108 /* If there was a token to replace, replace it */
109 if ($target_token != '') {
110 /* Find the end of the URL */
111 $end = $blength;
112 foreach ($url_parser_poss_ends as $val) {
113 $enda = strpos($body, $val, $target_pos);
114 if (is_int($enda) && $enda < $end) {
115 $end = $enda;
116 }
117 }
118
119 /* Extract URL */
120 $url = substr($body, $target_pos, $end-$target_pos);
121
122 /* Needed since lines are not passed with \n or \r */
123 while ( ereg("[,\.]$", $url) ) {
124 $url = substr( $url, 0, -1 );
125 $end--;
126 }
127
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);
133 }
134 else {
135 // Not quite a valid link, skip ahead to next chance
136 $target_pos += strlen($target_token);
137 }
138 }
139
140 /* Move forward */
141 $start = $target_pos;
142 $blength = strlen($body);
143 }
144 }
145 ?>