fixed bug #500497
[squirrelmail.git] / functions / url_parser.php
CommitLineData
59177427 1<?php
43fcef5c 2
35586184 3/**
4 * url_parser.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 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/*****************************************************************/
16/*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
17/*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
18/*** + Base level indent should begin at left margin, as ***/
19/*** the first line of the function definition below. ***/
20/*** + All identation should consist of four space blocks ***/
21/*** + Tab characters are evil. ***/
22/*** + all comments should use "slash-star ... star-slash" ***/
23/*** style -- no pound characters, no slash-slash style ***/
24/*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
25/*** ALWAYS USE { AND } CHARACTERS!!! ***/
26/*** + Please use ' instead of ", when possible. Note " ***/
27/*** should always be used in _( ) function calls. ***/
28/*** Thank you for your help making the SM code more readable. ***/
29/*****************************************************************/
30
31function replaceBlock (&$in, $replace, $start, $end) {
32 $begin = substr($in,0,$start);
33 $end = substr($in,$end,strlen($in)-$end);
34 $in = $begin.$replace.$end;
35}
43fcef5c 36
20a60f89 37 // Having this defined in just one spot could help when changes need
38 // to be made to the pattern
39 // Make sure that the expression is evaluated case insensitively
40 //
41 // Here's pretty sophisticated IP matching:
42 // $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
43 // $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
44 //
45 // Here's enough:
f435778e 46 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
20a60f89 47 $IP_RegExp_Match = '\\[?[0-9]{1,3}(\\.[0-9]{1,3}){3}\\]?';
48 $Host_RegExp_Match = '(' . $IP_RegExp_Match .
49 '|[0-9a-z]([-.]?[0-9a-z])*\\.[a-z][a-z]+)';
50 $Email_RegExp_Match = '[0-9a-z]([-_.]?[0-9a-z])*(%' . $Host_RegExp_Match .
51 ')?@' . $Host_RegExp_Match;
52
9eea179c 53 function parseEmail (&$body) {
20a60f89 54 global $color, $Email_RegExp_Match;
9eea179c 55 $Size = strlen($body);
e2ef6f4b 56
9297917e 57 /*
58 This is here in case we ever decide to use highlighting of searched
59 text. this does it for email addresses
60
61 if ($what && ($where == "BODY" || $where == "TEXT")) {
20a60f89 62 eregi ($Email_RegExp_Match, $body, $regs);
9297917e 63 $oldaddr = $regs[0];
64 if ($oldaddr) {
65 $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
66 $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
67 }
68 } else {
20a60f89 69 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9297917e 70 }
71 */
8f7163e7 72
20a60f89 73 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
9eea179c 74
75 // If there are any changes, it'll just get bigger.
76 if ($Size != strlen($body))
77 return 1;
78 return 0;
175e7218 79 }
43fcef5c 80
43fcef5c 81
20a60f89 82 // We don't want to re-initialize this stuff for every line. Save work
83 // and just do it once here.
f435778e 84 global $url_parser_url_tokens;
20a60f89 85 $url_parser_url_tokens = array(
86 'http://',
87 'https://',
88 'ftp://',
89 'telnet:', // Special case -- doesn't need the slashes
90 'gopher://',
91 'news://');
8f7163e7 92
f435778e 93 global $url_parser_poss_ends;
20a60f89 94 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
95 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
1b3324b3 96 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
8f7163e7 97
20a60f89 98
99 function parseUrl (&$body)
100 {
101 global $url_parser_poss_ends, $url_parser_url_tokens;;
8f7163e7 102 $start = 0;
103 $target_pos = strlen($body);
104
105 while ($start != $target_pos)
e2ef6f4b 106 {
8f7163e7 107 $target_token = '';
108
109 // Find the first token to replace
20a60f89 110 foreach ($url_parser_url_tokens as $the_token)
8f7163e7 111 {
112 $pos = strpos(strtolower($body), $the_token, $start);
113 if (is_int($pos) && $pos < $target_pos)
114 {
115 $target_pos = $pos;
116 $target_token = $the_token;
117 }
118 }
119
120 // Look for email addresses between $start and $target_pos
121 $check_str = substr($body, $start, $target_pos);
8f7163e7 122
9eea179c 123 if (parseEmail($check_str))
8f7163e7 124 {
20a60f89 125 replaceBlock($body, $check_str, $start, $target_pos);
9eea179c 126 $target_pos = strlen($check_str) + $start;
8f7163e7 127 }
e2ef6f4b 128
8f7163e7 129 // If there was a token to replace, replace it
130 if ($target_token != '')
131 {
132 // Find the end of the URL
133 $end=strlen($body);
20a60f89 134 foreach ($url_parser_poss_ends as $key => $val)
8f7163e7 135 {
136 $enda = strpos($body,$val,$target_pos);
137 if (is_int($enda) && $enda < $end)
138 $end = $enda;
139 }
140
141 // Extract URL
142 $url = substr($body, $target_pos, $end-$target_pos);
143
1b3324b3 144 // Needed since lines are not passed with \n or \r
145 while ( ereg("[,\.]$", $url) ) {
146 $url = substr( $url, 0, -1 );
147 $end--;
148 }
149
8f7163e7 150 // Replace URL with HyperLinked Url, requires 1 char in link
151 if ($url != '' && $url != $target_token)
152 {
153 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
20a60f89 154 replaceBlock($body,$url_str,$target_pos,$end);
8f7163e7 155 $target_pos += strlen($url_str);
156 }
157 else
158 {
159 // Not quite a valid link, skip ahead to next chance
160 $target_pos += strlen($target_token);
161 }
162 }
163
164 // Move forward
165 $start = $target_pos;
166 $target_pos = strlen($body);
167 }
43fcef5c 168 }
8f7163e7 169
43fcef5c 170?>