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