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