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