05803c8c2793cb4aef11661d9e3bac2f14b96c44
[mailer.git] / inc / libs / blacklist_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 01/21/2013 *
4  * ===================                          Last change: 01/21/2013 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : blacklist_functions.php                          *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for ext-                               *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer ext-                             *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Checks whether given data is blacklisted
44 function isGenericBlacklisted ($type, $data) {
45         // Mark it as not listed by default
46         $listed = FALSE;
47
48         // Is black-listing enbaled?
49         if (!isGenericBlacklistEnabled($type)) {
50                 // No, then all emails are not in this list
51                 return FALSE;
52         } elseif (!isset($GLOBALS['blacklist_data'][$type][$data])) {
53                 // Check black-list for given email
54                 $result = SQL_QUERY_ESC("SELECT
55         `id`,
56         `data`,
57         `pool_id`,
58         `provider`,
59         `type`,
60         UNIX_TIMESTAMP(`added`) AS `added`
61 FROM
62         `{?_MYSQL_PREFIX?}_blacklist`
63 WHERE
64         '%s' REGEXP `data` AND
65         `type`='%s'
66 LIMIT 1",
67                         array(
68                                 $data,
69                                 strtoupper($type)
70                         ), __FUNCTION__, __LINE__);
71
72                 // Is there an entry?
73                 if (SQL_NUMROWS($result) == 1) {
74                         // Jupp, we got one listed
75                         $GLOBALS['blacklist_data'][$type][$data] = SQL_FETCHARRAY($result);
76
77                         // Mark it as listed
78                         $listed = TRUE;
79                 } // END - if
80
81                 // Free result
82                 SQL_FREERESULT($result);
83         } else {
84                 // Is found in cache -> black-listed
85                 $listed = TRUE;
86         }
87
88         // Return result
89         return $listed;
90 }
91
92 // Inserts a given email (pattern) in blacklist if not found
93 function insertEmailInBlacklist ($email, $provider = 'BLACKLIST') {
94         // Call inner function
95         insertGenericInBlacklist ('email', $email, NULL, $provider);
96 }
97
98 // Inserts a given URL in blacklist if not found
99 function insertUrlInBlacklist ($url, $poolId, $provider = 'BLACKLIST') {
100         // Call inner function
101         insertGenericInBlacklist ('url', $url, $poolId, $provider);
102 }
103
104 // Inserts a given URL in blacklist if not found
105 function insertGenericInBlacklist ($type, $data, $poolId = NULL, $provider = 'BLACKLIST') {
106         // Is this feature turned on and is the URL not there?
107         if (!isGenericBlacklistEnabled($type)) {
108                 // Not enabled, then please don't call this function
109                 reportBug(__FUNCTION__, __LINE__, 'Blacklisting of type ' . $type . ' is disabled, data=' . $data . ',poolId=' . convertZeroToNull($poolId));
110         } elseif (!isUrlBlacklisted($data)) {
111                 // Did not find a record so we can add it... :)
112                 SQL_QUERY_ESC("INSERT INTO
113         `{?_MYSQL_PREFIX?}_blacklist`
114 (
115         `data`,
116         `pool_id`,
117         `provider`,
118         `type`
119 ) VALUES (
120         '%s',
121         %s,
122         '%s',
123         '%s'
124 )",
125                 array(
126                         $data,
127                         convertZeroToNull($poolId),
128                         $provider,
129                         strtoupper($type)
130                 ), __FUNCTION__, __LINE__);
131         } // END - if
132 }
133
134 // Checks whether given email is blacklisted
135 function isEmailBlacklisted ($email) {
136         // Call inner function
137         return isGenericBlacklisted('email', $email);
138 }
139
140 // Checks whether given URL is blacklisted
141 function isUrlBlacklisted ($url) {
142         // Call inner function
143         return isGenericBlacklisted('url', $email);
144 }
145
146 // ----------------------------------------------------------------------------
147 //                      Configuration wrapper functions
148 // ----------------------------------------------------------------------------
149
150 // Generic wrapper
151 function isGenericBlacklistEnabled ($type) {
152         // Is there cache?
153         if (!isset($GLOBALS[__FUNCTION__])) {
154                 // Determine it
155                 $GLOBALS[__FUNCTION__] = (getConfig($type . '_blacklist') == 'Y');
156         } // END - if
157
158         // Return cache
159         return $GLOBALS[__FUNCTION__];
160 }
161
162 // Wrapper to check if url_blacklist is enabled
163 function isUrlBlacklistEnabled () {
164         // Is there cache?
165         if (!isset($GLOBALS[__FUNCTION__])) {
166                 // Determine it
167                 $GLOBALS[__FUNCTION__] = (getConfig('url_blacklist') == 'Y');
168         } // END - if
169
170         // Return cache
171         return $GLOBALS[__FUNCTION__];
172 }
173
174 // Wrapper to check if email_blacklist is enabled
175 function isEmailBlacklistEnabled () {
176         // Is there cache?
177         if (!isset($GLOBALS[__FUNCTION__])) {
178                 // Determine it
179                 $GLOBALS[__FUNCTION__] = (getConfig('email_blacklist') == 'Y');
180         } // END - if
181
182         // Return cache
183         return $GLOBALS[__FUNCTION__];
184 }
185
186 // [EOF]
187 ?>