** -> *
[squirrelmail.git] / functions / url_parser.php
CommitLineData
59177427 1<?php
43fcef5c 2
2d367c68 3 /**
7350889b 4 * url_parser.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development 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 */
43fcef5c 14
20a60f89 15 function replaceBlock (&$in, $replace, $start, $end) {
43fcef5c 16 $begin = substr($in,0,$start);
17 $end = substr($in,$end,strlen($in)-$end);
20a60f89 18 $in = $begin.$replace.$end;
43fcef5c 19 }
20
20a60f89 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:
f435778e 30 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
20a60f89 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
9eea179c 37 function parseEmail (&$body) {
20a60f89 38 global $color, $Email_RegExp_Match;
9eea179c 39 $Size = strlen($body);
e2ef6f4b 40
9297917e 41 /*
42 This is here in case we ever decide to use highlighting of searched
43 text. this does it for email addresses
44
45 if ($what && ($where == "BODY" || $where == "TEXT")) {
20a60f89 46 eregi ($Email_RegExp_Match, $body, $regs);
9297917e 47 $oldaddr = $regs[0];
48 if ($oldaddr) {
49 $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
50 $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
51 }
52 } else {
20a60f89 53 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9297917e 54 }
55 */
8f7163e7 56
20a60f89 57 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9eea179c 58
59 // If there are any changes, it'll just get bigger.
60 if ($Size != strlen($body))
61 return 1;
62 return 0;
175e7218 63 }
43fcef5c 64
43fcef5c 65
20a60f89 66 // We don't want to re-initialize this stuff for every line. Save work
67 // and just do it once here.
f435778e 68 global $url_parser_url_tokens;
20a60f89 69 $url_parser_url_tokens = array(
70 'http://',
71 'https://',
72 'ftp://',
73 'telnet:', // Special case -- doesn't need the slashes
74 'gopher://',
75 'news://');
8f7163e7 76
f435778e 77 global $url_parser_poss_ends;
20a60f89 78 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
79 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
1b3324b3 80 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
8f7163e7 81
20a60f89 82
83 function parseUrl (&$body)
84 {
85 global $url_parser_poss_ends, $url_parser_url_tokens;;
8f7163e7 86 $start = 0;
87 $target_pos = strlen($body);
88
89 while ($start != $target_pos)
e2ef6f4b 90 {
8f7163e7 91 $target_token = '';
92
93 // Find the first token to replace
20a60f89 94 foreach ($url_parser_url_tokens as $the_token)
8f7163e7 95 {
96 $pos = strpos(strtolower($body), $the_token, $start);
97 if (is_int($pos) && $pos < $target_pos)
98 {
99 $target_pos = $pos;
100 $target_token = $the_token;
101 }
102 }
103
104 // Look for email addresses between $start and $target_pos
105 $check_str = substr($body, $start, $target_pos);
8f7163e7 106
9eea179c 107 if (parseEmail($check_str))
8f7163e7 108 {
20a60f89 109 replaceBlock($body, $check_str, $start, $target_pos);
9eea179c 110 $target_pos = strlen($check_str) + $start;
8f7163e7 111 }
e2ef6f4b 112
8f7163e7 113 // If there was a token to replace, replace it
114 if ($target_token != '')
115 {
116 // Find the end of the URL
117 $end=strlen($body);
20a60f89 118 foreach ($url_parser_poss_ends as $key => $val)
8f7163e7 119 {
120 $enda = strpos($body,$val,$target_pos);
121 if (is_int($enda) && $enda < $end)
122 $end = $enda;
123 }
124
125 // Extract URL
126 $url = substr($body, $target_pos, $end-$target_pos);
127
1b3324b3 128 // Needed since lines are not passed with \n or \r
129 while ( ereg("[,\.]$", $url) ) {
130 $url = substr( $url, 0, -1 );
131 $end--;
132 }
133
8f7163e7 134 // Replace URL with HyperLinked Url, requires 1 char in link
135 if ($url != '' && $url != $target_token)
136 {
137 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
20a60f89 138 replaceBlock($body,$url_str,$target_pos,$end);
8f7163e7 139 $target_pos += strlen($url_str);
140 }
141 else
142 {
143 // Not quite a valid link, skip ahead to next chance
144 $target_pos += strlen($target_token);
145 }
146 }
147
148 // Move forward
149 $start = $target_pos;
150 $target_pos = strlen($body);
151 }
43fcef5c 152 }
8f7163e7 153
43fcef5c 154?>