Adds a config option to invert the time for Solaris machines in the US.
[squirrelmail.git] / functions / url_parser.php
... / ...
CommitLineData
1<?php
2 /* URL Passing code to allow links from with in emails */
3
4 $url_parser_php = true;
5
6 function replaceBlock ($in, $replace, $start, $end) {
7 $begin = substr($in,0,$start);
8 $end = substr($in,$end,strlen($in)-$end);
9 $ret = $begin.$replace.$end;
10 return $ret;
11 }
12
13 function parseEmail ($body) {
14 global $color;
15
16 // Having this defined in just one spot could help when changes need
17 // to be made to the pattern
18 // Make sure that the expression is evaluated case insensitively
19 //
20 // Here's pretty sophisticated IP matching:
21 // $IPMatch = '(2[0-5][0-9]|1?[0-9]{1,2})';
22 // $IPMatch = '\[?' . $IPMatch . '(\.' . $IPMatch . '){3}\]?';
23 //
24 // Here's enough:
25 $IPMatch = '\[?[0-9]{1,3}(\.[0-9]{1,3}){3}\]?';
26 $Host = '(' . $IPMatch . '|[0-9a-z]([-.]?[0-9a-z])*\.[a-wyz][a-z](g|l|m|pa|t|u|v)?)';
27 $Expression = '[0-9a-z]([-_.]?[0-9a-z])*(%' . $Host . ')?@' . $Host;
28
29 /*
30 This is here in case we ever decide to use highlighting of searched
31 text. this does it for email addresses
32
33 if ($what && ($where == "BODY" || $where == "TEXT")) {
34 eregi ($Expression, $body, $regs);
35 $oldaddr = $regs[0];
36 if ($oldaddr) {
37 $newaddr = eregi_replace ($what, "<b><font color=\"$color[2]\">$what</font></font></b>", $oldaddr);
38 $body = str_replace ($oldaddr, "<a href=\"../src/compose.php?send_to=$oldaddr\">$newaddr</a>", $body);
39 }
40 } else {
41 $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
42 }
43 */
44
45 $body = eregi_replace ($Expression, "<a href=\"../src/compose.php?send_to=\\0\">\\0</a>", $body);
46 return $body;
47 }
48
49
50 function parseUrl ($body)
51 {
52 $url_tokens = array(
53 'http://',
54 'https://',
55 'ftp://',
56 'telnet:', // Special case -- doesn't need the slashes
57 'gopher://',
58 'news://');
59
60 $poss_ends = array(' ', '\n', '\r', '<', '>', '.\r', '.\n', '.&nbsp;',
61 '&nbsp;', ')', '(', '&quot;', '&lt;', '&gt;', '.<', ']', '[', '{',
62 '}', "\240");
63
64 $start = 0;
65 $target_pos = strlen($body);
66
67 while ($start != $target_pos)
68 {
69 $target_token = '';
70
71 // Find the first token to replace
72 foreach ($url_tokens as $the_token)
73 {
74 $pos = strpos(strtolower($body), $the_token, $start);
75 if (is_int($pos) && $pos < $target_pos)
76 {
77 $target_pos = $pos;
78 $target_token = $the_token;
79 }
80 }
81
82 // Look for email addresses between $start and $target_pos
83 $check_str = substr($body, $start, $target_pos);
84 $new_str = parseEmail($check_str);
85
86 if ($check_str != $new_str)
87 {
88 $body = replaceBlock($body, $new_str, $start, $target_pos);
89 $target_pos = strlen($new_str) + $start;
90 }
91
92 // If there was a token to replace, replace it
93 if ($target_token != '')
94 {
95 // Find the end of the URL
96 $end=strlen($body);
97 foreach ($poss_ends as $key => $val)
98 {
99 $enda = strpos($body,$val,$target_pos);
100 if (is_int($enda) && $enda < $end)
101 $end = $enda;
102 }
103
104 // Extract URL
105 $url = substr($body, $target_pos, $end-$target_pos);
106
107 // Replace URL with HyperLinked Url, requires 1 char in link
108 if ($url != '' && $url != $target_token)
109 {
110 $url_str = "<a href=\"$url\" target=\"_blank\">$url</a>";
111 $body = replaceBlock($body,$url_str,$target_pos,$end);
112 $target_pos += strlen($url_str);
113 }
114 else
115 {
116 // Not quite a valid link, skip ahead to next chance
117 $target_pos += strlen($target_token);
118 }
119 }
120
121 // Move forward
122 $start = $target_pos;
123 $target_pos = strlen($body);
124 }
125
126 return $body;
127 }
128
129?>