Introduced new extension ext-blacklist:
[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 // Inserts a given email (pattern) in blacklist if not found
44 function insertEmailInBlacklist ($email, $id) {
45         // Is this feature turned on and is the URL not there?
46         if (!isEmailBlacklistEnabled()) {
47                 // Not enabled, then please don't call this function
48                 reportBug(__FUNCTION__, __LINE__, 'URL blacklisting is disabled, email=' . $email . ',id=' . $id);
49         } elseif (!isEmailBlacklisted($email)) {
50                 // Did not find a record so we can add it... :)
51                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_blacklist` (`data`, `pool_id`, `type`) VALUES ('%s', %s, 'EMAIL')",
52                         array(
53                                 $email,
54                                 $id
55                         ), __FUNCTION__, __LINE__);
56         } // END - if
57 }
58
59 // Checks whether given email is blacklisted
60 function isEmailBlacklisted ($email) {
61         // Mark it as not listed by default
62         $listed = FALSE;
63
64         // Is black-listing enbaled?
65         if (!isEmailBlacklistEnabled()) {
66                 // No, then all emails are not in this list
67                 return FALSE;
68         } elseif (!isset($GLOBALS['blacklist_data']['email'][$email])) {
69                 // Check black-list for given email
70                 $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`added`) AS `added` FROM `{?_MYSQL_PREFIX?}_blacklist` WHERE '%s' REGEXP `data` AND `type`='EMAIL' LIMIT 1",
71                         array($email), __FUNCTION__, __LINE__);
72
73                 // Is there an entry?
74                 if (SQL_NUMROWS($result) == 1) {
75                         // Jupp, we got one listed
76                         $GLOBALS['blacklist_data']['email'][$email] = SQL_FETCHARRAY($result);
77
78                         // Mark it as listed
79                         $listed = TRUE;
80                 } // END - if
81
82                 // Free result
83                 SQL_FREERESULT($result);
84         } else {
85                 // Is found in cache -> black-listed
86                 $listed = TRUE;
87         }
88
89         // Return result
90         return $listed;
91 }
92
93 // Inserts a given URL in blacklist if not found
94 function insertUrlInBlacklist ($url, $id) {
95         // Is this feature turned on and is the URL not there?
96         if (!isUrlBlacklistEnabled()) {
97                 // Not enabled, then please don't call this function
98                 reportBug(__FUNCTION__, __LINE__, 'URL blacklisting is disabled, url=' . $url . ',id=' . $id);
99         } elseif (!isUrlBlacklisted($url)) {
100                 // Did not find a record so we can add it... :)
101                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_blacklist` (`data`, `pool_id`, `type`) VALUES ('%s', %s, 'URL')",
102                         array(
103                                 $url,
104                                 $id
105                         ), __FUNCTION__, __LINE__);
106         } // END - if
107 }
108
109 // Checks whether given URL is blacklisted
110 function isUrlBlacklisted ($url) {
111         // Mark it as not listed by default
112         $listed = FALSE;
113
114         // Is black-listing enbaled?
115         if (!isUrlBlacklistEnabled()) {
116                 // No, then all URLs are not in this list
117                 return FALSE;
118         } elseif (!isset($GLOBALS['blacklist_data']['url'][$url])) {
119                 // Check black-list for given URL
120                 $result = SQL_QUERY_ESC("SELECT UNIX_TIMESTAMP(`added`) AS `added`, `pool_id` FROM `{?_MYSQL_PREFIX?}_blacklist` WHERE `data`='%s' AND `type`='URL' LIMIT 1",
121                         array($url), __FUNCTION__, __LINE__);
122
123                 // Is there an entry?
124                 if (SQL_NUMROWS($result) == 1) {
125                         // Jupp, we got one listed
126                         $GLOBALS['blacklist_data']['url'][$url] = SQL_FETCHARRAY($result);
127
128                         // Mark it as listed
129                         $listed = TRUE;
130                 } // END - if
131
132                 // Free result
133                 SQL_FREERESULT($result);
134         } else {
135                 // Is found in cache -> black-listed
136                 $listed = TRUE;
137         }
138
139         // Return result
140         return $listed;
141 }
142
143 // ----------------------------------------------------------------------------
144 //                      Configuration wrapper functions
145 // ----------------------------------------------------------------------------
146
147 // Wrapper to check if url_blacklist is enabled
148 function isUrlBlacklistEnabled () {
149         // Is there cache?
150         if (!isset($GLOBALS[__FUNCTION__])) {
151                 // Determine it
152                 $GLOBALS[__FUNCTION__] = (getConfig('url_blacklist') == 'Y');
153         } // END - if
154
155         // Return cache
156         return $GLOBALS[__FUNCTION__];
157 }
158
159 // Wrapper to check if email_blacklist is enabled
160 function isEmailBlacklistEnabled () {
161         // Is there cache?
162         if (!isset($GLOBALS[__FUNCTION__])) {
163                 // Determine it
164                 $GLOBALS[__FUNCTION__] = (getConfig('email_blacklist') == 'Y');
165         } // END - if
166
167         // Return cache
168         return $GLOBALS[__FUNCTION__];
169 }
170
171 // [EOF]
172 ?>