Added update_year.sh (still not fully flexible) and updated all years with it.
[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  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or    *
20  * (at your option) any later version.                                  *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
38 // Resolver class for cached hostname -> IP address resolving
39 class HostnameResolver {
40         // Resolve hostname -> IP address
41         function resolveHostname ($hostname) {
42                 // If ext-sql_patches is not at least 0.7.0, abort here and return the hostname (gethostbyname() may return something unwanted)
43                 if (!isExtensionInstalledAndNewer('sql_patches', '0.7.0')) {
44                         // Abort here
45                         return $hostname;
46                 } // END - if
47
48                 // Prepare hostname, look for the ':port' part
49                 $hostArray = explode(':', $hostname, 2);
50                 $hostname = strtolower($hostArray[0]);
51
52                 // Is the hostname an IP address?
53                 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)) {
54                         // Then we don't need to look it up
55                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Hostname %s is an IP address. No need to lookup.", $hostname));
56                         return $hostname;
57                 } // END - if
58
59                 // Log entry
60                 //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Begin lookup: %s", $hostname));
61                 $ret = '0.0.0.0';
62
63                 // Search for hostname in cache
64                 $result = sqlQueryEscaped("SELECT `ip` FROM `{?_MYSQL_PREFIX?}_dns_cache` WHERE `hostname`='%s' LIMIT 1",
65                         array(
66                                 $hostname
67                         ), __METHOD__, __LINE__
68                 );
69
70                 // Does an entry exist?
71                 if (sqlNumRows($result) == 1) {
72                         // Then load the hostname
73                         list($ip) = sqlFetchRow($result);
74
75                         // Count cache hit
76                         incrementStatsEntry('dns_cache_hits');
77
78                         // Set fetched IP number as return value
79                         $ret = $ip;
80                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Cache used: %s->%s", $hostname, $ip));
81                 } else {
82                         // Get IP address
83                         $ip = gethostbyname($hostname);
84                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, 'ip=' . $ip . ',hostname=' . $hostname . ' - RESOLVED!');
85
86                         // Count lookup hit
87                         incrementStatsEntry('dns_lookup_hits');
88
89                         // Is it an IP address?
90                         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))) {
91                                 // Seems to be an IP! Now check deeper...
92                                 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)) {
93                                         // We also cache IP addresses
94                                         sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_dns_cache` (`ip`, `hostname`, `added`) VALUES ('%s', '%s', NOW())",
95                                                 array(
96                                                         $ip,
97                                                         $hostname
98                                                 ), __METHOD__, __LINE__
99                                         );
100
101                                         // Set return value to $ip
102                                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("IP detected, cache entry written: %s->%s", $hostname, $ip));
103                                         $ret = $ip;
104                                 } else {
105                                         // Network address or broadcast address found
106                                         //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("IP %s is possibly a network or broadcast address.", $ip));
107                                 }
108                         } elseif ($ip == $hostname) {
109                                 // Nothing found, maybe invalid!
110                                 //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Cannot lookup: %s", $hostname));
111                         } else {
112                                 // Put entry in DB
113                                 sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_dns_cache` (`ip`, `hostname`, `added`) VALUES ('%s', '%s', NOW())",
114                                         array(
115                                                 $ip,
116                                                 $hostname
117                                         ), __METHOD__, __LINE__
118                                 );
119
120                                 // Set return value to $ip
121                                 $ret = $ip;
122                                 //* DEBUG: */ logDebugMessage(__METHOD__, __LINE__, sprintf("Lookup successfull, cache entry written: %s->%s", $hostname, $ip));
123                         }
124                 }
125
126                 // Free result
127                 sqlFreeResult($result);
128
129                 // Return IP number (let's hope it)
130                 return $ret;
131         }
132
133         // Purge old entries
134         function purgeEntries() {
135                 // SQL for cleaning up
136                 sqlQuery('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_dns_cache` WHERE (UNIX_TIMESTAMP() - UNIX_TIMESTAMP(`added`) >= {?dns_cache_timeout?})',
137                         __METHOD__, __LINE__);
138         }
139 }
140
141 // [EOF]
142 ?>