236cd6ee6f431ee976165d610a202c1c62f0ca31
[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         $OUT = "";
43         if (!is_array($GLOBALS['html_tags'])) return "";
44         foreach ($GLOBALS['html_tags'] as $tag) {
45                 $OUT .= ", ".strtoupper($tag);
46         }
47         $OUT = substr($OUT, 2);
48         return $OUT;
49 }
50 //
51 function HTML_CHECK_TAGS($html {
52         $test = stripslashes($html);
53         while (ereg("<", $test) && ereg(">", $test)) {
54                 $check = strtolower(substr($test, strpos($test, "<") + 1, strpos($test, ">") - strpos($test, "<") - 1));
55                 $check = str_replace("/", "", $check);
56                 if (!in_array($check, $GLOBALS['html_tags'])) {
57                         // Invalid tag found!
58                         return "";
59                 }
60                 $test = substr($test, strpos($test, ">") + 1);
61         }
62         // Return tested code
63         return $html;
64 }
65 //
66 function HTML_INSERT_URLS ($text) {
67         $test = $text;
68
69         // First replace URLs...
70         while (ereg("http://", $test))
71         {
72                 $check = substr($test, strpos($test, "http://")); $check2 = $check;
73
74                 // See ext-html.php if you want to add more URL ends...
75                 foreach ($GLOBALS['url_ends'] as $end)
76                 {
77                         if (ereg($end, $check)) $check = substr($check, 0, strpos($check, $end));
78                 }
79
80                 // Now replace the URL against anchor container and pray...
81                 $text = substr($text, 0, strpos($text, $check2))."<a href=\"".DEREFERER($check)."\" target=\"_blank\">".$check."</a>".substr($text, strpos($text, $check2) + strlen($check));
82
83                 // Finally remove the url from testing string (or we have a loop and maybe server overload!)
84                 $test = substr($test, strpos($test, $check) + strlen($check));
85         }
86
87         // Now do the (nearly) same thing with email addresses
88         // but now we have the problem that email addresses didn't have
89         // a start mark like http:// and our templates are lame didn't have
90         // a mailto: ... :-(
91         $test = $text;
92
93         // ... what will the email address be out the @... ;-)
94         $PARTS = array();
95         while (ereg("@", $test))
96         {
97                 $pos = strpos($test, "@");
98                 $test2 = substr($test, 0, $pos);
99
100                 // First check backwards
101                 $idx = $pos - 1;
102                 while ($idx > 0)
103                 {
104                         $check = substr($test2, $idx, 1);
105                         if (!in_array($check, $GLOBALS['valid_email_chars']))
106                         {
107                                 // Char found so we end here
108                                 break;
109                         }
110                         $idx--;
111                 }
112                 if ($idx > 0)
113                 {
114                         // Starting mark is found
115                         $check2 = substr($test, 0, ($idx + 1));
116                         $test = substr($test, ($idx + 1));
117                 }
118
119                 // And now go forward...
120                 $idx = 0;
121                 while ($idx < strlen($test))
122                 {
123                         $check = substr($test, $idx, 1);
124                         if ((!in_array($check, $GLOBALS['valid_email_chars'])) && ($check != "@"))
125                         {
126                                 // Char found so end here again
127                                 break;
128                         }
129                         $idx++;
130                 }
131                 if ($idx > 0)
132                 {
133                         // Maybe this is the email address?
134                         $check = substr($test, 0, $idx);
135                 }
136
137                 // Now replace the email against anchor with mailto and pray...
138                 $PARTS[] = $check2."<a href=\"mailto:".$check."\">".$check."</a>";
139
140                 // Remove email from testing string (see above why...)
141                 $test = substr($test, strlen($check));
142         }
143         // Now put all parts together
144         $text = ""; $PARTS[] = $test;
145         foreach ($PARTS as $part)
146         {
147                 $text .= $part;
148         }
149
150         // Replace new-lines agains <br />-s and finally compile possible own HTML tags out...
151         return COMPILE_CODE(str_replace("\n", "<br />\n", $text));
152 }
153 //
154 function SEND_HTML_EMAIL($TO, $SUBJECT, $MSG, $FROM)
155 {
156         if (EXT_IS_ACTIVE("html_mail"))
157         {
158                 // Send mail away as HTML
159                 $FROM = "Content-Type: text/html\n".$FROM;
160                 SEND_EMAIL($TO, $SUBJECT, $MSG, 'N', $FROM);
161         }
162 }
163 //
164 ?>