inc/dns.inc.php
author peter
Wed, 16 Apr 2008 17:53:40 +0000
changeset 139 98ce0ffae5b8
parent 138 3e36ebbfe048
child 140 e68c3d6094d1
permissions -rw-r--r--
[feladat @ 245] Bugfix: When a record is deleted from the search screen, an error was displayed. This closes ticket:43.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
     1
<?php
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     2
119
f74e4f88b680 [feladat @ 222]
rejo
parents: 97
diff changeset
     3
/*  Poweradmin, a friendly web-based admin tool for PowerDNS.
47
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
     4
 *  See <https://rejo.zenger.nl/poweradmin> for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
     5
 *
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
     6
 *  Copyright 2007, 2008  Rejo Zenger <rejo@zenger.nl>
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
     7
 *
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
     8
 *  This program is free software: you can redistribute it and/or modify
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
     9
 *  it under the terms of the GNU General Public License as published by
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    10
 *  the Free Software Foundation, either version 3 of the License, or
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    11
 *  (at your option) any later version.
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    12
 *
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    13
 *  This program is distributed in the hope that it will be useful,
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    16
 *  GNU General Public License for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    17
 *
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    18
 *  You should have received a copy of the GNU General Public License
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    20
 */
ae140472d97c [feladat @ 94]
rejo
parents: 16
diff changeset
    21
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
    22
function validate_input($zid, $type, &$content, &$name, &$prio, &$ttl)
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    23
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    24
	global $db;
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
    25
	$domain = get_domain_name_from_id($zid);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    26
	$nocheck = array('SOA', 'HINFO', 'NAPTR', 'URL', 'MBOXFW', 'TXT');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    27
	$hostname = false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    28
	$ip4 = false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    29
	$ip6 = false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    30
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    31
	if(!in_array(strtoupper($type), $nocheck)) {
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    32
		if(!is_valid_ip6($content)) {
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    33
			if(!is_valid_ip($content)) {
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    34
				if(!is_valid_hostname($content)) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    35
					error(ERR_DNS_CONTENT);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    36
					return false;
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    37
				} else {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    38
					$hostname = true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    39
				}
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    40
			} else {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    41
				$ip4 = true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    42
			}
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    43
		} else {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    44
			$ip6 = true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    45
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    46
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    47
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    48
	// Prepare total hostname.
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    49
	if ($name == '*') {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    50
		$wildcard = true;
79
0c0aa144356a [feladat @ 156]
peter
parents: 71
diff changeset
    51
	} else {
0c0aa144356a [feladat @ 156]
peter
parents: 71
diff changeset
    52
		$wildcard = false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    53
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    54
97
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    55
	if (preg_match("/@/", $name)) {
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    56
		$name = $domain ;
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    57
	} elseif ( !(preg_match("/$domain$/i", $name))) {
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    58
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    59
		if ( isset($name) && $name != "" ) {
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    60
			$name = $name . "." . $domain ;
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    61
		} else {
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    62
			$name = $domain ;
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    63
		}
96
35cd9e082b90 [feladat @ 196]
rejo
parents: 95
diff changeset
    64
	}
35cd9e082b90 [feladat @ 196]
rejo
parents: 95
diff changeset
    65
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    66
	if(!$wildcard) {
97
090be08858db [feladat @ 197]
rejo
parents: 96
diff changeset
    67
		if(!is_valid_hostname($name)) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    68
			error(ERR_DNS_HOSTNAME);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    69
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    70
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    71
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    72
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    73
	// Check record type (if it exists in our allowed list.
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    74
	if (!in_array(strtoupper($type), get_record_types())) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    75
		error(ERR_DNS_RECORDTYPE);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    76
		return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    77
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    78
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    79
	// Start handling the demands for the functions.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    80
	// Validation for IN A records. Can only have an IP. Nothing else.
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    81
	if ($type == 'A' && !$ip4) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    82
		error(ERR_DNS_IPV4);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    83
		return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    84
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    85
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    86
	if ($type == 'AAAA' && !$ip6) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    87
		error(ERR_DNS_IPV6);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    88
		return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    89
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    90
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    91
	if ($type == 'CNAME' && $hostname) {
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    92
		if(!is_valid_cname($name)) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    93
			error(ERR_DNS_CNAME);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    94
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    95
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    96
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    97
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
    98
	if ($type == 'NS') {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    99
		$status = is_valid_ns($content, $hostname);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   100
		if($status == -1) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   101
			error(ERR_DNS_NS_HNAME);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   102
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   103
		}
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   104
		elseif($status == -2) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   105
			error(ERR_DNS_NS_CNAME);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   106
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   107
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   108
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   109
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   110
	if ($type == 'SOA' && !is_valid_rr_soa($content)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   111
		return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   112
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   113
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   114
	// HINFO and TXT require no validation.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   115
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   116
	if ($type == 'URL') {
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   117
		if(!is_valid_url($content)) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   118
			error(ERR_INV_URL);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   119
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   120
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   121
	}
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   122
	if ($type == 'MBOXFW') 	{
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   123
		if(!is_valid_mboxfw($content)) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   124
			error(ERR_INV_EMAIL);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   125
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   126
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   127
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   128
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   129
	// NAPTR has to be done.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   130
	// Do we want that?
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   131
	// http://www.ietf.org/rfc/rfc2915.txt
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   132
	// http://www.zvon.org/tmRFC/RFC2915/Output/chapter2.html
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   133
	// http://www.zvon.org/tmRFC/RFC3403/Output/chapter4.html
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   134
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   135
	// See if the prio field is valid and if we have one.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   136
	// If we dont have one and the type is MX record, give it value '10'
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   137
	if($type == 'NAPTR') {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   138
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   139
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   140
	
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   141
	if($type == 'MX') {
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   142
		if($hostname) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   143
			$status = is_valid_mx($content, $prio);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   144
			if($status == -1) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   145
				error(ERR_DNS_MX_CNAME);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   146
				return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   147
			}
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   148
			elseif($status == -2) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   149
				error(ERR_DNS_MX_PRIO);
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   150
				return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   151
			}
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   152
		} else {
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   153
			error( _('If you specify an MX record it must be a hostname.') ); // TODO make error
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   154
			return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   155
		}
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   156
	} else {
55
a885f557678f [feladat @ 102]
peter
parents: 47
diff changeset
   157
		$prio=0;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   158
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   159
	// Validate the TTL, it has to be numeric.
136
c795dd75a77e [feladat @ 242]
rejo
parents: 122
diff changeset
   160
	$ttl = (!isset($ttl) || !is_numeric($ttl)) ? $dns_ttl : $ttl;
82
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   161
	
c255196bc447 [feladat @ 182]
rejo
parents: 79
diff changeset
   162
	return true;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   163
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   164
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   165
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   166
 * Validatis a CNAME record by the name it will have and its destination
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   167
 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   168
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   169
function is_valid_cname($dest)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   170
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   171
	/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   172
	 * This is really EVIL.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   173
	 * If the new record (a CNAME) record is being pointed to by a MX record or NS record we have to bork.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   174
	 * this is the idea.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   175
	 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   176
	 * MX record: blaat.nl MX mail.blaat.nl
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   177
	 * Now we look what mail.blaat.nl is
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   178
	 * We discover the following:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   179
	 * mail.blaat.nl CNAME bork.blaat.nl
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   180
	 * This is NOT allowed! mail.onthanet.nl can not be a CNAME!
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   181
	 * The same goes for NS. mail.blaat.nl must have a normal IN A record.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   182
	 * It MAY point to a CNAME record but its not wished. Lets not support it.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   183
	 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   184
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   185
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   186
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   187
	// Check if there are other records with this information of the following types.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   188
	// P.S. we might add CNAME to block CNAME recursion and chains.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   189
	$blockedtypes = " AND (type='MX' OR type='NS')";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   190
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 62
diff changeset
   191
	$cnamec = "SELECT type, content FROM records WHERE content=".$db->quote($dest) . $blockedtypes;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   192
	$result = $db->query($cnamec);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   193
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   194
	if($result->numRows() > 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   195
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   196
		return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   197
		// Lets inform the user he is doing something EVIL.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   198
		// Ok we found a record that has our content field in their content field.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   199
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   200
	return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   201
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   202
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   203
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   204
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   205
 * Checks if something is a valid domain.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   206
 * Checks for domainname with the allowed characters <a,b,...z,A,B,...Z> and - and _.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   207
 * This part must be followed by a 2 to 4 character TLD.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   208
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   209
function is_valid_domain($domain)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   210
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   211
	if ((eregi("^[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $domain)) && (strlen($domain) <= 128))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   212
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   213
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   214
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   215
	return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   216
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   217
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   218
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   219
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   220
 * Validates if given hostname is allowed.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   221
 * returns true if allowed.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   222
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   223
function is_valid_hostname($host)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   224
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   225
	if(count(explode(".", $host)) == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   226
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   227
		return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   228
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   229
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   230
	// Its not perfect (in_addr.int is allowed) but works for now.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   231
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   232
	if(preg_match('!(ip6|in-addr).(arpa|int)$!i', $host))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   233
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   234
		if(preg_match('!^(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+$!i', $host))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   235
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   236
			return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   237
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   238
		return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   239
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   240
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   241
	// Validate further.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   242
	return (preg_match('!^(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+$!i', $host)) ? true : false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   243
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   244
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   245
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   246
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   247
 * Validates an IPv4 IP.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   248
 * returns true if valid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   249
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   250
function is_valid_ip($ip)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   251
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   252
	// Stop reading at this point. Scroll down to the next function...
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   253
	// Ok... you didn't stop reading... now you have to rewrite the whole function! enjoy ;-)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   254
	// Trance unborked it. Twice even!
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   255
	return ($ip == long2ip(ip2long($ip))) ? true : false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   256
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   257
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   258
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   259
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   260
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   261
 * Validates an IPv6 IP.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   262
 * returns true if valid.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   263
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   264
function is_valid_ip6($ip)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   265
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   266
	// Validates if the given IP is truly an IPv6 address.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   267
	// Precondition: have a string
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   268
	// Postcondition: false: Error in IP
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   269
	//                true: IP is correct
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   270
	// Requires: String
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   271
	// Date: 10-sep-2002
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   272
	if(preg_match('!^[A-F0-9:]{1,39}$!i', $ip) == true)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   273
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   274
		// Not 3 ":" or more.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   275
		$p = explode(':::', $ip);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   276
		if(sizeof($p) > 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   277
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   278
			return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   279
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   280
		// Find if there is only one occurence of "::".
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   281
		$p = explode('::', $ip);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   282
		if(sizeof($p) > 2)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   283
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   284
			return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   285
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   286
		// Not more than 8 octects
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   287
		$p = explode(':', $ip);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   288
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   289
		if(sizeof($p) > 8)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   290
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   291
			return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   292
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   293
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   294
		// Check octet length
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   295
		foreach($p as $checkPart)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   296
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   297
			if(strlen($checkPart) > 4)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   298
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   299
				return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   300
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   301
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   302
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   303
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   304
	return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   305
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   306
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   307
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   308
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   309
 * FANCY RECORD.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   310
 * Validates if the fancy record mboxfw is an actual email address.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   311
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   312
function is_valid_mboxfw($email)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   313
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   314
	return is_valid_email($email);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   315
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   316
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   317
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   318
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   319
 * Validates MX records.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   320
 * an MX record cant point to a CNAME record. This has to be checked.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   321
 * this function also sets a proper priority.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   322
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   323
function is_valid_mx($content, &$prio)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   324
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   325
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   326
	// See if the destination to which this MX is pointing is NOT a CNAME record.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   327
	// Check inside our dns server.
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 62
diff changeset
   328
	if($db->queryOne("SELECT count(id) FROM records WHERE name=".$db->quote($content)." AND type='CNAME'") > 0)
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   329
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   330
		return -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   331
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   332
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   333
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   334
		// Fix the proper priority for the record.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   335
		// Bugfix, thanks Oscar :)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   336
		if(!isset($prio))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   337
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   338
			$prio = 10;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   339
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   340
		if(!is_numeric($prio))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   341
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   342
			if($prio == '')
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   343
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   344
				$prio = 10;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   345
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   346
			else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   347
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   348
				return -2;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   349
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   350
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   351
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   352
	return 1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   353
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   354
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   355
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   356
 * Validates NS records.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   357
 * an NS record cant point to a CNAME record. This has to be checked.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   358
 * $hostname directive means if its a hostname or not (this to avoid that NS records get ip fields)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   359
 * NS must have a hostname, it is not allowed to have an IP.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   360
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   361
function is_valid_ns($content, $hostname)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   362
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   363
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   364
	// Check if the field is a hostname, it MUST be a hostname.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   365
	if(!$hostname)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   366
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   367
		return -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   368
		// "an IN NS field must be a hostname."
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   369
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   370
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 62
diff changeset
   371
	if($db->queryOne("SELECT count(id) FROM records WHERE name=".$db->quote($content)." AND type='CNAME'") > 0)
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   372
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   373
		return -2;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   374
		// "You can not point a NS record to a CNAME record. Remove/rename the CNAME record first or take another name."
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   375
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   376
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   377
	return 1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   378
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   379
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   380
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   381
function is_valid_hostname_label($hostname_label) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   382
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   383
        // See <https://www.poweradmin.org/trac/wiki/Documentation/DNS-hostnames>.
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   384
        if (!preg_match('/^[a-z\d]([a-z\d-]*[a-z\d])*$/i',$hostname_label)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   385
		return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   386
        } elseif (preg_match('/^[\d]+$/i',$hostname_label)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   387
                return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   388
        } elseif ((strlen($hostname_label) < 2) || (strlen($hostname_label) > 63)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   389
                return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   390
        }
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   391
        return true;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   392
}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   393
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   394
function is_valid_hostname_fqdn($hostname) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   395
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   396
        // See <https://www.poweradmin.org/trac/wiki/Documentation/DNS-hostnames>.
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   397
	global $dns_strict_tld_check;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   398
	global $valid_tlds;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   399
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   400
	$hostname = ereg_replace("\.$","",$hostname);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   401
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   402
	if (strlen($hostname) > 255) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   403
		error(ERR_DNS_HN_TOO_LONG);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   404
		return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   405
	}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   406
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   407
        $hostname_labels = explode ('.', $hostname);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   408
        $label_count = count($hostname_labels);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   409
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   410
	if ($dns_strict_tld_check == "1" && !in_array($hostname_labels[$label_count-1], $valid_tlds)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   411
		error(ERR_DNS_INV_TLD);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   412
		return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   413
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   414
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   415
	if ($hostname_labels[$label_count-1] == "arpa") {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   416
		// FIXME
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   417
	} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   418
		foreach ($hostname_labels as $hostname_label) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   419
			if (!is_valid_hostname_label($hostname_label)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   420
				error(ERR_DNS_HOSTNAME);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   421
				return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   422
			}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   423
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   424
	}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   425
	return true;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   426
}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   427
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   428
function is_valid_rr_soa(&$content) {
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   429
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   430
	// TODO move to appropiate location
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   431
//	$return = get_records_by_type_from_domid("SOA", $zid);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   432
//	if($return->numRows() > 1) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   433
//		return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   434
//	}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   435
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   436
	$fields = preg_split("/\s+/", trim($content));
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   437
        $field_count = count($fields);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   438
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   439
	if ($field_count == 0 || $field_count > 7) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   440
		return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   441
	} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   442
		if (!is_valid_hostname_fqdn($fields[0]) || preg_match('/\.arpa\.?$/',$fields[0])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   443
			return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   444
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   445
		$final_soa = $fields[0];
121
90cab8725693 [feladat @ 224]
rejo
parents: 119
diff changeset
   446
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   447
		if (isset($fields[1])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   448
			$addr_input = $fields[1];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   449
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   450
			global $dns_hostmaster;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   451
			$addr_input = $dns_hostmaster;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   452
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   453
		if (!preg_match("/@/", $addr_input)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   454
			$addr_input = preg_split('/(?<!\\\)\./', $addr_input, 2);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   455
			$addr_to_check = str_replace("\\", "", $addr_input[0]) . "@" . $addr_input[1];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   456
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   457
			$addr_to_check = $addr_input;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   458
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   459
		
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   460
		if (!is_valid_email($addr_to_check)) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   461
			return false;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   462
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   463
			$addr_final = explode('@', $addr_to_check, 2);
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   464
			$final_soa .= " " . str_replace(".", "\\.", $addr_final[0]) . "." . $addr_final[1];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   465
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   466
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   467
		if (isset($fields[2])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   468
			if (!is_numeric($fields[2])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   469
				return false;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   470
			}
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   471
			$final_soa .= " " . $fields[2];
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   472
		} else {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   473
			$final_soa .= " 0";
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   474
		}
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   475
		
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   476
		if ($field_count == 7) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   477
			for ($i = 3; ($i < 7); $i++) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   478
				if (!is_numeric($fields[$i])) {
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   479
					return false;
121
90cab8725693 [feladat @ 224]
rejo
parents: 119
diff changeset
   480
				} else {
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   481
					$final_soa .= " " . $fields[$i];
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   482
				}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   483
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   484
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   485
	}
138
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   486
	$content = $final_soa;
3e36ebbfe048 [feladat @ 244]
rejo
parents: 136
diff changeset
   487
	return true;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   488
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   489
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   490
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   491
function is_valid_url($url)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   492
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   493
	return preg_match('!^(http://)(([A-Z\d]|[A-Z\d][A-Z\d-]*[A-Z\d])\.)*[A-Z\d]+([//]([0-9a-z//~#%&\'_\-+=:?.]*))?$!i',  $url);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   494
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   495
62
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   496
function is_valid_search($holygrail)
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   497
{
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   498
	// Only allow for alphanumeric, numeric, dot, dash, underscore and 
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   499
	// percent in search string. The last two are wildcards for SQL.
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   500
	// Needs extension probably for more usual record types.
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   501
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   502
	return preg_match('/^[a-z0-9.\-%_]+$/i', $holygrail);
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   503
}
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   504
ce4919f37489 [feladat @ 109]
rejo
parents: 55
diff changeset
   505
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   506
?>