Code cleanup brigage...
[squirrelmail.git] / functions / url_parser.php
1 <?php
2
3 /**
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 */
14
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
31 function 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 }
36
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:
46 global $IP_RegExp_Match, $Host_RegExp_Match, $Email_RegExp_Match;
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
53 function parseEmail (&$body) {
54 global $color, $Email_RegExp_Match;
55 $Size = strlen($body);
56
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")) {
62 eregi ($Email_RegExp_Match, $body, $regs);
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 {
69 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
70 }
71 */
72
73 $body = eregi_replace ($Email_RegExp_Match, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
74
75 // If there are any changes, it'll just get bigger.
76 if ($Size != strlen($body))
77 return 1;
78 return 0;
79 }
80
81
82 // We don't want to re-initialize this stuff for every line. Save work
83 // and just do it once here.
84 global $url_parser_url_tokens;
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://');
92
93 global $url_parser_poss_ends;
94 $url_parser_poss_ends = array(' ', "\n", "\r", '<', '>', ".\r", ".\n",
95 '.&nbsp;', '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<',
96 ']', '[', '{', '}', "\240", ', ', '. ', ",\n", ",\r");
97
98
99 function parseUrl (&$body)
100 {
101 global $url_parser_poss_ends, $url_parser_url_tokens;;
102 $start = 0;
103 $target_pos = strlen($body);
104
105 while ($start != $target_pos)
106 {
107 $target_token = '';
108
109 // Find the first token to replace
110 foreach ($url_parser_url_tokens as $the_token)
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);
122
123 if (parseEmail($check_str))
124 {
125 replaceBlock($body, $check_str, $start, $target_pos);
126 $target_pos = strlen($check_str) + $start;
127 }
128
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);
134 foreach ($url_parser_poss_ends as $key => $val)
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
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
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>";
154 replaceBlock($body,$url_str,$target_pos,$end);
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 }
168 }
169
170 ?>