Security line in all includes changed
[mailer.git] / inc / libs / html_mail_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 04/25/2004 *
4  * ================                             Last change: 04/29/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : html_mail_functions.php                          *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for the HTML extension                 *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die HTML-Erweiterung             *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 //
41 function HTML_ADD_VALID_TAGS()
42 {
43         global $HTML_TAGS; $OUT = "";
44         if (!is_array($HTML_TAGS)) return "";
45         foreach ($HTML_TAGS as $tag)
46         {
47                 $OUT .= ", ".strtoupper($tag);
48         }
49         $OUT = substr($OUT, 2);
50         return $OUT;
51 }
52 //
53 function HTML_CHECK_TAGS($html)
54 {
55         global $HTML_TAGS;
56         $test = stripslashes($html);
57         while (ereg("<", $test) && ereg(">", $test))
58         {
59                 $check = strtolower(substr($test, strpos($test, "<") + 1, strpos($test, ">") - strpos($test, "<") - 1));
60                 $check = str_replace("/", "", $check);
61                 if (!in_array($check, $HTML_TAGS))
62                 {
63                         // Invalid tag found!
64                         return "";
65                 }
66                 $test = substr($test, strpos($test, ">") + 1);
67         }
68         // Return tested code
69         return $html;
70 }
71 //
72 function HTML_INSERT_URLS($text)
73 {
74         global $URL_ENDS, $VALID_EMAIL_CHARS;
75         $test = $text;
76
77         // First replace URLs...
78         while (ereg("http://", $test))
79         {
80                 $check = substr($test, strpos($test, "http://")); $check2 = $check;
81
82                 // See ext-html.php if you want to add more URL ends...
83                 foreach ($URL_ENDS as $end)
84                 {
85                         if (ereg($end, $check)) $check = substr($check, 0, strpos($check, $end));
86                 }
87
88                 // Now replace the URL against anchor container and pray...
89                 $text = substr($text, 0, strpos($text, $check2))."<A href=\"".DEREFERER($check)."\" target=\"_blank\">".$check."</A>".substr($text, strpos($text, $check2) + strlen($check));
90
91                 // Finally remove the url from testing string (or we have a loop and maybe server overload!)
92                 $test = substr($test, strpos($test, $check) + strlen($check));
93         }
94
95         // Now do the (nearly) same thing with email addresses
96         // but now we have the problem that email addresses didn't have
97         // a start mark like http:// and our templates are lame didn't have
98         // a mailto: ... :-(
99         $test = $text;
100
101         // ... what will the email address be out the @... ;-)
102         $PARTS = array();
103         while (ereg("@", $test))
104         {
105                 $pos = strpos($test, "@");
106                 $test2 = substr($test, 0, $pos);
107
108                 // First check backwards
109                 $idx = $pos - 1;
110                 while ($idx > 0)
111                 {
112                         $check = substr($test2, $idx, 1);
113                         if (!in_array($check, $VALID_EMAIL_CHARS))
114                         {
115                                 // Char found so we end here
116                                 break;
117                         }
118                         $idx--;
119                 }
120                 if ($idx > 0)
121                 {
122                         // Starting mark is found
123                         $check2 = substr($test, 0, ($idx + 1));
124                         $test = substr($test, ($idx + 1));
125                 }
126
127                 // And now go forward...
128                 $idx = 0;
129                 while ($idx < strlen($test))
130                 {
131                         $check = substr($test, $idx, 1);
132                         if ((!in_array($check, $VALID_EMAIL_CHARS)) && ($check != "@"))
133                         {
134                                 // Char found so end here again
135                                 break;
136                         }
137                         $idx++;
138                 }
139                 if ($idx > 0)
140                 {
141                         // Maybe this is the email address?
142                         $check = substr($test, 0, $idx);
143                 }
144
145                 // Now replace the email against anchor with mailto and pray...
146                 $PARTS[] = $check2."<A href=\"mailto:".$check."\">".$check."</A>";
147
148                 // Remove email from testing string (see above why...)
149                 $test = substr($test, strlen($check));
150         }
151         // Now put all parts together
152         $text = ""; $PARTS[] = $test;
153         foreach ($PARTS as $part)
154         {
155                 $text .= $part;
156         }
157
158         // Replace new-lines agains <br />-s and finally compile possible own HTML tags out...
159         return COMPILE_CODE(str_replace("\n", "<br />\n", $text));
160 }
161 //
162 function SEND_HTML_EMAIL($TO, $SUBJECT, $MSG, $FROM)
163 {
164         if (EXT_IS_ACTIVE("html_mail"))
165         {
166                 // Send mail away as HTML
167                 $FROM = "Content-Type: text/html\n".$FROM;
168                 SEND_EMAIL($TO, $SUBJECT, $MSG, 'N', $FROM);
169         }
170 }
171 //
172 ?>