session_(un)register are deprecated as of 5.3.1
[mailer.git] / inc / libs / html_mail_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 }
44
45 //
46 function addValidHtmlTags() {
47         $OUT = '';
48         if (!is_array($GLOBALS['html_tags'])) return "";
49         foreach ($GLOBALS['html_tags'] as $tag) {
50                 $OUT .= ", ".strtoupper($tag);
51         }
52         $OUT = substr($OUT, 2);
53         return $OUT;
54 }
55
56 //
57 function checkHtmlTags ($html) {
58         $test = stripslashes($html);
59         while (ereg('<', $test) && ereg('>', $test)) {
60                 $check = strtolower(substr($test, strpos($test, '<') + 1, strpos($test, '>') - strpos($test, '<') - 1));
61                 $check = str_replace('/', '', $check);
62                 if (!in_array($check, $GLOBALS['html_tags'])) {
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 //
73 function insertUrlsIntoHtml ($text) {
74         $test = $text;
75
76         // First replace URLs...
77         while (ereg('http://', $test)) {
78                 $check = substr($test, strpos($test, 'http://')); $check2 = $check;
79
80                 // See ext-html.php if you want to add more URL ends...
81                 foreach ($GLOBALS['url_ends'] as $end) {
82                         if (ereg($end, $check)) $check = substr($check, 0, strpos($check, $end));
83                 } // END - foreach
84
85                 // Now replace the URL against anchor container and pray...
86                 $text = substr($text, 0, strpos($text, $check2)) . '<a href="' . generateDerefererUrl($check) . '" target="_blank">' . $check . '</a>' . substr($text, strpos($text, $check2) + strlen($check));
87
88                 // Finally remove the url from testing string (or we have a loop and maybe server overload!)
89                 $test = substr($test, strpos($test, $check) + strlen($check));
90         } // END - while
91
92         // Now do the (nearly) same thing with email addresses
93         // but now we have the problem that email addresses didn't have
94         // a start mark like http:// and our templates are lame didn't have
95         // a mailto: ... :-(
96         $test = $text;
97
98         // ... what will the email address be out the @... ;-)
99         $PARTS = array();
100         while (ereg('@', $test)) {
101                 $pos = strpos($test, '@');
102                 $test2 = substr($test, 0, $pos);
103
104                 // First check backwards
105                 $idx = $pos - 1;
106                 while ($idx > 0) {
107                         $check = substr($test2, $idx, 1);
108                         if (!in_array($check, $GLOBALS['valid_email_chars'])) {
109                                 // Char found so we end here
110                                 break;
111                         } // END - if
112                         $idx--;
113                 } // END - while
114
115                 if ($idx > 0) {
116                         // Starting mark is found
117                         $check2 = substr($test, 0, ($idx + 1));
118                         $test = substr($test, ($idx + 1));
119                 } // END - if
120
121                 // And now go forward...
122                 $idx = '0';
123                 while ($idx < strlen($test)) {
124                         $check = substr($test, $idx, 1);
125                         if ((!in_array($check, $GLOBALS['valid_email_chars'])) && ($check != '@')) {
126                                 // Char found so end here again
127                                 break;
128                         } // END - if
129                         $idx++;
130                 } // END - while
131
132                 if ($idx > 0) {
133                         // Maybe this is the email address?
134                         $check = substr($test, 0, $idx);
135                 } // END - if
136
137                 // Now replace the email against anchor with mailto and pray...
138                 $PARTS[] = $check2 . '<a href="' . generateEmailLink($check, 'user_data') . '">' . $check . '</a>';
139
140                 // Remove email from testing string (see above why...)
141                 $test = substr($test, strlen($check));
142         }
143
144         // Now put all parts together
145         $text = ''; $PARTS[] = $test;
146         foreach ($PARTS as $part) {
147                 $text .= $part;
148         } // END - foreach
149
150         // Replace new-lines agains <br />-s and finally compile possible own HTML tags out...
151         return compileCode(str_replace("\n", "<br />\n", $text));
152 }
153
154 // Sends a HTML mail to the user
155 function sendHtmlEmail($to, $subject, $message, $FROM) {
156         if (isExtensionActive('html_mail')) {
157                 // Send mail away as HTML
158                 $FROM = "Content-Type: text/html; charset=UTF-8\n" . $FROM;
159                 sendEmail($to, $subject, $message, 'N', $FROM);
160         } // END - if
161 }
162
163 //
164 ?>