Extension ext-coupon continued, a lot improvements applied:
[mailer.git] / inc / classes / resolver.class.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 03/05/2010 *
4  * ===================                          Last change: 03/05/2010 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : resolver.class.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Resolver class                                   *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Resolver-Klasse                                  *
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 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://www.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 // Resolver class for cached hostname -> IP address resolving
44 class HostnameResolver {
45         // Resolve hostname -> IP address
46         function resolveHostname ($hostname) {
47                 // If sql_patches is not at least 0.7.0, abort here and return the hostname (gethostbyname() may return something unwanted)
48                 if (!isExtensionInstalledAndNewer('sql_patches', '0.7.0')) {
49                         // Abort here
50                         return $hostname;
51                 } // END - if
52
53                 // Prepare hostname, look for the ':port' part
54                 $hostArray = explode(':', $hostname, 2);
55                 $hostname = strtolower($hostArray[0]);
56
57                 // Is the hostname an IP address?
58                 if (preg_match('/(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])/', $hostname, $matches)) {
59                         // Then we don't need to look it up
60                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Hostname %s is an IP address. No need to lookup.", $hostname));
61                         return $hostname;
62                 } // END - if
63
64                 // Log entry
65                 //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Begin lookup: %s", $hostname));
66                 $ret = '0.0.0.0';
67
68                 // Search for hostname in cache
69                 $result = SQL_QUERY_ESC("SELECT `ip` FROM `{?_MYSQL_PREFIX?}_dns_cache` WHERE `hostname`='%s' LIMIT 1",
70                         array($hostname), __METHOD__, __LINE__);
71
72                 // Does an entry exist?
73                 if (SQL_NUMROWS($result) == 1) {
74                         // Then load the hostname
75                         list($ip) = SQL_FETCHROW($result);
76
77                         // Count cache hit
78                         incrementStatsEntry('dns_cache_hits');
79
80                         // Set fetched IP number as return value
81                         $ret = $ip;
82                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Cache used: %s->%s", $hostname, $ip));
83                 } else {
84                         // Get IP address
85                         $ip = gethostbyname($hostname);
86
87                         // Count lookup hit
88                         incrementStatsEntry('dns_lookup_hits');
89
90                         // Is it an IP address?
91                         if (($ip != $hostname) && (preg_match('/(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])/', $ip, $matches))) {
92                                 // Seems to be an IP! Now check deeper...
93                                 if (($matches[0] == $ip) && ($matches[1] >= 0) && ($matches[1] <= 255) && ($matches[2] >= 0) && ($matches[2] <= 255) && ($matches[3] >= 0) && ($matches[3] <= 255) && ($matches[4] > 0) && ($matches[4] < 255)) {
94                                         // We also cache IP addresses
95                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_dns_cache` (`ip`, `hostname`, `added`) VALUES('%s', '%s', NOW())",
96                                                 array($ip, $hostname), __METHOD__, __LINE__);
97
98                                         // Set return value to $ip
99                                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("IP detected, cache entry written: %s->%s", $hostname, $ip));
100                                 } else {
101                                         // Network address or broadcast address found
102                                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("IP %s is possibly a network or broadcast address.", $ip));
103                                 }
104                         } elseif ($ip == $hostname) {
105                                 // Nothing found, maybe invalid!
106                                 //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Cannot lookup: %s", $hostname));
107                         } else {
108                                 // Put entry in DB
109                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_dns_cache` (`ip`, `hostname`, `added`) VALUES('%s', '%s', NOW())",
110                                         array($ip, $hostname), __METHOD__, __LINE__);
111
112                                 // Set return value to $ip
113                                 $ret = $ip;
114                                 //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Lookup successfull, cache entry written: %s->%s", $hostname, $ip));
115                         }
116                 }
117
118                 // Free result
119                 SQL_FREERESULT($result);
120
121                 // Return IP number (let's hope it!
122                 return $ret;
123         }
124
125         // Purge old entries
126         function purgeEntries() {
127                 // SQL for cleaning up
128                 SQL_QUERY('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_dns_cache` WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`added`) >= {?dns_cache_timeout?})',
129                         __METHOD__, __LINE__);
130         }
131 }
132
133 // [EOF]
134 ?>