Opps, not all elements for sprintf() has been set.
[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 - 2013 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 email is blacklisted
44 function isEmailBlacklisted ($email) {
45         // Call inner function
46         return isGenericBlacklisted('email', $email);
47 }
48
49 // Checks whether given IP is blacklisted
50 function isIpBlacklisted ($ip) {
51         // Call inner function
52         return isGenericBlacklisted('ip', $ip);
53 }
54
55 // Checks whether given URL is blacklisted
56 function isUrlBlacklisted ($url) {
57         // Call inner function
58         return isGenericBlacklisted('url', $email);
59 }
60
61 // Checks whether given data is blacklisted
62 function isGenericBlacklisted ($type, $data) {
63         // Mark it as not listed by default
64         $listed = FALSE;
65
66         // Is black-listing enbaled?
67         if (!isGenericBlacklistEnabled($type)) {
68                 // No, then all emails are not in this list
69                 return FALSE;
70         } elseif (!isset($GLOBALS['blacklist_data'][$type][$data])) {
71                 // Check black-list for given email
72                 $result = sqlQueryEscaped("SELECT
73         `id`,
74         `data`,
75         `pool_id`,
76         `provider`,
77         `type`,
78         UNIX_TIMESTAMP(`added`) AS `added`
79 FROM
80         `{?_MYSQL_PREFIX?}_blacklist`
81 WHERE
82         '%s' REGEXP `data` AND
83         `type`='%s'
84 LIMIT 1",
85                         array(
86                                 $data,
87                                 strtoupper($type)
88                         ), __FUNCTION__, __LINE__);
89
90                 // Is there an entry?
91                 if (sqlNumRows($result) == 1) {
92                         // Jupp, we got one listed
93                         $GLOBALS['blacklist_data'][$type][$data] = sqlFetchArray($result);
94
95                         // Mark it as listed
96                         $listed = TRUE;
97                 } // END - if
98
99                 // Free result
100                 sqlFreeResult($result);
101         } else {
102                 // Is found in cache -> black-listed
103                 $listed = TRUE;
104         }
105
106         // Return result
107         return $listed;
108 }
109
110 // Inserts a given email (pattern) in blacklist if not found
111 function insertEmailInBlacklist ($email, $provider = 'BLACKLIST') {
112         // Call inner function
113         return insertGenericInBlacklist ('email', $email, NULL, $provider);
114 }
115
116 // Inserts a given IP (pattern) in blacklist if not found
117 function insertIpInBlacklist ($ip, $provider = 'BLACKLIST') {
118         // Call inner function
119         return insertGenericInBlacklist ('ip', $ip, NULL, $provider);
120 }
121
122 // Inserts a given URL (pattern) in blacklist if not found
123 function insertUrlInBlacklist ($url, $poolId, $provider = 'BLACKLIST') {
124         // Call inner function
125         return insertGenericInBlacklist ('url', $url, $poolId, $provider);
126 }
127
128 // Inserts a given URL in blacklist if not found
129 function insertGenericInBlacklist ($type, $data, $poolId = NULL, $provider = 'BLACKLIST') {
130         // Is this feature turned on and is the URL not there?
131         if (!isGenericBlacklistEnabled($type)) {
132                 // Not enabled, then please don't call this function
133                 reportBug(__FUNCTION__, __LINE__, 'Blacklisting of type ' . $type . ' is disabled, data=' . $data . ',poolId=' . convertZeroToNull($poolId));
134         } elseif (!isUrlBlacklisted($data)) {
135                 // Did not find a record so we can add it... :)
136                 sqlQueryEscaped("INSERT INTO
137         `{?_MYSQL_PREFIX?}_blacklist`
138 (
139         `data`,
140         `pool_id`,
141         `provider`,
142         `type`
143 ) VALUES (
144         '%s',
145         %s,
146         '%s',
147         '%s'
148 )",
149                 array(
150                         $data,
151                         convertZeroToNull($poolId),
152                         $provider,
153                         strtoupper($type)
154                 ), __FUNCTION__, __LINE__);
155         } // END - if
156
157         // Return insert id for debugging/reporting pursposes
158         return getSqlInsertId();
159 }
160
161 // ----------------------------------------------------------------------------
162 //                      Configuration wrapper functions
163 // ----------------------------------------------------------------------------
164
165 // Generic wrapper
166 function isGenericBlacklistEnabled ($type) {
167         // Is there cache?
168         if (!isset($GLOBALS[__FUNCTION__])) {
169                 // Determine it
170                 $GLOBALS[__FUNCTION__] = (getConfig($type . '_blacklist') == 'Y');
171         } // END - if
172
173         // Return cache
174         return $GLOBALS[__FUNCTION__];
175 }
176
177 // Getter for url_blacklist
178 function getUrlBlacklist () {
179         // Is there cache?
180         if (!isset($GLOBALS[__FUNCTION__])) {
181                 // Determine it
182                 $GLOBALS[__FUNCTION__] = getConfig('url_blacklist');
183         } // END - if
184
185         // Return cache
186         return $GLOBALS[__FUNCTION__];
187 }
188
189 // Wrapper to check if url_blacklist is enabled
190 function isUrlBlacklistEnabled () {
191         // Is there cache?
192         if (!isset($GLOBALS[__FUNCTION__])) {
193                 // Determine it
194                 $GLOBALS[__FUNCTION__] = (getUrlBlacklist() == 'Y');
195         } // END - if
196
197         // Return cache
198         return $GLOBALS[__FUNCTION__];
199 }
200
201 // Getter for email_blacklist
202 function getEmailBlacklist () {
203         // Is there cache?
204         if (!isset($GLOBALS[__FUNCTION__])) {
205                 // Determine it
206                 $GLOBALS[__FUNCTION__] = getConfig('email_blacklist');
207         } // END - if
208
209         // Return cache
210         return $GLOBALS[__FUNCTION__];
211 }
212
213 // Wrapper to check if email_blacklist is enabled
214 function isEmailBlacklistEnabled () {
215         // Is there cache?
216         if (!isset($GLOBALS[__FUNCTION__])) {
217                 // Determine it
218                 $GLOBALS[__FUNCTION__] = (getEmailBlacklist() == 'Y');
219         } // END - if
220
221         // Return cache
222         return $GLOBALS[__FUNCTION__];
223 }
224
225 // Getter for ip_blacklist
226 function getIpBlacklist () {
227         // Is there cache?
228         if (!isset($GLOBALS[__FUNCTION__])) {
229                 // Determine it
230                 $GLOBALS[__FUNCTION__] = getConfig('ip_blacklist');
231         } // END - if
232
233         // Return cache
234         return $GLOBALS[__FUNCTION__];
235 }
236
237 // Wrapper to check if ip_blacklist is enabled
238 function isIpBlacklistEnabled () {
239         // Is there cache?
240         if (!isset($GLOBALS[__FUNCTION__])) {
241                 // Determine it
242                 $GLOBALS[__FUNCTION__] = (getIpBlacklist() == 'Y');
243         } // END - if
244
245         // Return cache
246         return $GLOBALS[__FUNCTION__];
247 }
248
249 // [EOF]
250 ?>