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