7fccd6fa40c83f3e62448bbffeb51dce04cf005a
[mailer.git] / inc / libs / newsletter_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 04/25/2004 *
4  * ================                             Last change: 04/29/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : newsletter_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 NL_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 //
52 function NL_CHECK_TAGS ($html) {
53         $test = stripslashes($html);
54         while (ereg("<", $test) && ereg(">", $test)) {
55                 $check = strtolower(substr($test, strpos($test, "<") + 1, strpos($test, ">") - strpos($test, "<") - 1));
56                 $check = str_replace("/", "", $check);
57                 if (!in_array($check, $GLOBALS['html_tags'])) {
58                         // Invalid tag found!
59                         return "";
60                 }
61                 $test = substr($test, strpos($test, ">") + 1);
62         }
63         // Return tested code
64         return $html;
65 }
66
67 //
68 function NL_INSERT_URLS ($text) {
69         $test = $text;
70
71         // First replace URLs...
72         while (ereg("http://", $test)) {
73                 $check = substr($test, strpos($test, "http://")); $check2 = $check;
74
75                 // See ext-html.php if you want to add more URL ends...
76                 foreach ($GLOBALS['url_ends'] as $end) {
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)) . DEREFERER($check) . 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                 $pos = strpos($test, "@");
97                 $test2 = substr($test, 0, $pos);
98
99                 // First check backwards
100                 $idx = $pos - 1;
101                 while ($idx > 0) {
102                         $check = substr($test2, $idx, 1);
103                         if (!in_array($check, $GLOBALS['valid_email_chars']))
104                         {
105                                 // Char found so we end here
106                                 break;
107                         }
108                         $idx--;
109                 }
110
111                 if ($idx > 0) {
112                         // Starting mark is found
113                         $check2 = substr($test, 0, ($idx + 1));
114                         $test = substr($test, ($idx + 1));
115                 }
116
117                 // And now go forward...
118                 $idx = 0;
119                 while ($idx < strlen($test)) {
120                         $check = substr($test, $idx, 1);
121                         if ((!in_array($check, $GLOBALS['valid_email_chars'])) && ($check != "@")) {
122                                 // Char found so end here again
123                                 break;
124                         }
125                         $idx++;
126                 }
127
128                 if ($idx > 0) {
129                         // Maybe this is the email address?
130                         $check = substr($test, 0, $idx);
131                 }
132
133                 // Now replace the email against anchor with mailto and pray...
134                 $PARTS[] = $check2.$check;
135
136                 // Remove email from testing string (see above why...)
137                 $test = substr($test, strlen($check));
138         }
139
140         // Now put all parts together
141         $text = ""; $PARTS[] = $test;
142         foreach ($PARTS as $part) {
143                 $text .= $part;
144         }
145
146         // Compile possible own HTML tags out...
147         return COMPILE_CODE($text);
148 }
149
150 //
151 function SEND_NEWSLETTER ($TO, $SUBJECT, $MSG, $MODE) {
152         // Send mail away as HTML
153         if ($_POST['auto_urls'] == "Y") {
154                 // Automatically insert URLs into newsletter
155                 if ((EXT_IS_ACTIVE("html")) && ($MODE == "html")) {
156                         // Send HTML mail
157                         SEND_EMAIL($TO, $SUBJECT, HTML_INSERT_URLS($MSG), "Y");
158                 } else {
159                         // Send normal mail
160                         SEND_EMAIL($TO, $SUBJECT, NL_INSERT_URLS($MSG), "N");
161                 }
162         } else {
163                 // Regular send-out
164                 if ((EXT_IS_ACTIVE("html")) && ($MODE == "html")) {
165                         SEND_EMAIL($TO, $SUBJECT, $MSG, "Y");
166                 } else {
167                         SEND_EMAIL($TO, $SUBJECT, $MSG, "N");
168                 }
169         }
170 }
171
172 //
173 ?>