dns_import.php 25.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

/*
Copyright (c) 2008, Till Brehm, projektfarm Gmbh
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of ISPConfig nor the names of its contributors
      may be used to endorse or promote products derived from this software without
      specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

31
32
require_once '../../lib/config.inc.php';
require_once '../../lib/app.inc.php';
33
34
35
36

//* Check permissions for module
$app->auth->check_module_permissions('dns');

37
38
$msg = '';
$error = '';
39
40
41
42

// Loading the template
$app->uses('tpl,validate_dns');
$app->tpl->newTemplate("form.tpl.htm");
43
$app->tpl->setInclude('content_tpl', 'templates/dns_import.htm');
44
45
46
$app->load_language_file('/web/dns/lib/lang/'.$_SESSION['s']['language'].'_dns_wizard.lng');

// import variables
47
48
$template_id = (isset($_POST['template_id']))?$app->functions->intval($_POST['template_id']):0;
$sys_groupid = (isset($_POST['client_group_id']))?$app->functions->intval($_POST['client_group_id']):0;
49
50
51
$domain = (isset($_POST['domain'])&&!empty($_POST['domain']))?$_POST['domain']:NULL;

// get the correct server_id
52
53
54
if (isset($_POST['server_id'])) {
	$server_id = $app->functions->intval($_POST['server_id']);
	$post_server_id = true;
55
56
57
} elseif (isset($_POST['server_id_value'])) {
	$server_id = $app->functions->intval($_POST['server_id_value']);
	$post_server_id = true;
58
} else {
59
60
	$server_id = 1;
	$post_server_id = false;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
}


// Load the templates
$records = $app->db->queryAllRecords("SELECT * FROM dns_template WHERE visible = 'Y'");
$template_id_option = '';
$n = 0;
foreach($records as $rec){
	$checked = ($rec['template_id'] == $template_id)?' SELECTED':'';
	$template_id_option .= '<option value="'.$rec['template_id'].'"'.$checked.'>'.$rec['name'].'</option>';
	if($n == 0 && $template_id == 0) $template_id = $rec['template_id'];
	$n++;
}
unset($n);
75
$app->tpl->setVar("template_id_option", $template_id_option);
76
77
78

// If the user is administrator
if($_SESSION['s']['user']['typ'] == 'admin') {
79

80
81
82
83
84
85
86
	// Load the list of servers
	$records = $app->db->queryAllRecords("SELECT server_id, server_name FROM server WHERE mirror_server_id = 0 AND dns_server = 1 ORDER BY server_name");
	$server_id_option = '';
	foreach($records as $rec){
		$checked = ($rec['server_id'] == $server_id)?' SELECTED':'';
		$server_id_option .= '<option value="'.$rec['server_id'].'"'.$checked.'>'.$rec['server_name'].'</option>';
	}
87
88
	$app->tpl->setVar("server_id", $server_id_option);

89
	// load the list of clients
90
	$sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND sys_group.client_id > 0 ORDER BY client.company_name, client.contact_name, sys_group.name";
91
92
93
94
95
96
	$clients = $app->db->queryAllRecords($sql);
	$client_select = '';
	if($_SESSION["s"]["user"]["typ"] == 'admin') $client_select .= "<option value='0'></option>";
	if(is_array($clients)) {
		foreach( $clients as $client) {
			$selected = ($client["groupid"] == $sys_groupid)?'SELECTED':'';
97
			$client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
98
99
100
		}
	}

101
	$app->tpl->setVar("client_group_id", $client_select);
102
103
104
}

if ($_SESSION["s"]["user"]["typ"] != 'admin' && $app->auth->has_clients($_SESSION['s']['user']['userid'])) {
105

106
	// Get the limits of the client
107
	$client_group_id = intval($_SESSION["s"]["user"]["default_group"]);
108
	$client = $app->db->queryOneRecord("SELECT client.client_id, client.contact_name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname, sys_group.name FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");
109

110

111
	// load the list of clients
112
	$sql = "SELECT sys_group.groupid, sys_group.name, CONCAT(IF(client.company_name != '', CONCAT(client.company_name, ' :: '), ''), client.contact_name, ' (', client.username, IF(client.customer_no != '', CONCAT(', ', client.customer_no), ''), ')') as contactname FROM sys_group, client WHERE sys_group.client_id = client.client_id AND client.parent_client_id = ".intval($client['client_id'])." ORDER BY client.company_name, client.contact_name, sys_group.name";
113
	$clients = $app->db->queryAllRecords($sql);
114
	$tmp = $app->db->queryOneRecord("SELECT groupid FROM sys_group WHERE client_id = ".intval($client['client_id']));
115
	$client_select = '<option value="'.$tmp['groupid'].'">'.$client['contactname'].'</option>';
116
117
118
	if(is_array($clients)) {
		foreach( $clients as $client) {
			$selected = ($client["groupid"] == $sys_groupid)?'SELECTED':'';
119
			$client_select .= "<option value='$client[groupid]' $selected>$client[contactname]</option>\r\n";
120
121
122
		}
	}

123
	$app->tpl->setVar("client_group_id", $client_select);
124
125
}

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
if($_SESSION["s"]["user"]["typ"] != 'admin')
{
	$client_group_id = $_SESSION["s"]["user"]["default_group"];
	$client_dns = $app->db->queryOneRecord("SELECT dns_servers FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = $client_group_id");

	$client_dns['dns_servers_ids'] = explode(',', $client_dns['dns_servers']);

	$only_one_server = count($client_dns['dns_servers_ids']) === 1;
	$app->tpl->setVar('only_one_server', $only_one_server);

	if ($only_one_server) {
		$app->tpl->setVar('server_id_value', $client_dns['dns_servers_ids'][0]);
	}

	$sql = "SELECT server_id, server_name FROM server WHERE server_id IN (" . $client_dns['dns_servers'] . ");";
	$dns_servers = $app->db->queryAllRecords($sql);

	$options_dns_servers = "";

	foreach ($dns_servers as $dns_server) {
		$options_dns_servers .= "<option value='$dns_server[server_id]'>$dns_server[server_name]</option>";
	}

	$app->tpl->setVar("server_id", $options_dns_servers);
	unset($options_dns_servers);

}

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Now we have to check, if we should use the domain-module to select the domain
 * or not
 */
$app->uses('ini_parser,getconf');
$settings = $app->getconf->get_global_config('domains');
if ($settings['use_domain_module'] == 'y') {
	/*
	 * The domain-module is in use.
	*/
	$domains = $app->tools_sites->getDomainModuleDomains();
	/*
	 * We can leave domain empty if domain is filename
	*/
	$domain_select = "<option value=''></option>\r\n";
	if(is_array($domains) && sizeof($domains) > 0) {
		/* We have domains in the list, so create the drop-down-list */
		foreach( $domains as $domain) {
			$domain_select .= "<option value=" . $domain['domain_id'] ;
			if ($domain['domain'] == $_POST['domain']) {
				$domain_select .= " selected";
			}
			$domain_select .= ">" . $app->functions->idn_decode($domain['domain']) . ".</option>\r\n";
		}
	}
	$app->tpl->setVar("domain_option", $domain_select);
	/* check if the selected domain can be used! */
	if ($domain) {
		$domain_check = $app->tools_sites->checkDomainModuleDomain($domain);
		if(!$domain_check) {
			// invalid domain selected
			$domain = NULL;
		} else {
			$domain = $domain_check;
		}
	}
}

192
$lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_dns_import.lng';
193
include $lng_file;
194
195
$app->tpl->setVar($wb);

196
197
// Import the zone-file
//if(1=="1")
198
199
if(isset($_FILES['file']['name']) && is_uploaded_file($_FILES['file']['tmp_name'])){
	$valid_zone_file = FALSE;
200

201
	$sql = "SELECT server_name FROM `server` WHERE server_id=".$app->functions->intval($server_id)." OR mirror_server_id=".$app->functions->intval($server_id)." ORDER BY server_name ASC";
202
203
204
	$servers = $app->db->queryAllRecords($sql);
	for ($i=0;$i<count($servers);$i++)
	{
205
		if (substr($servers[$i]['server_name'], strlen($servers[$i]['server_name'])-1) != ".")
206
207
208
209
210
		{
			$servers[$i]['server_name'] .= ".";
		}
	}
	$lines = file($_FILES['file']['tmp_name']);
211

212
213
214
215
	// Remove empty lines, comments, whitespace, tabs, etc.
	$new_lines = array();
	foreach($lines as $line){
		$line = trim($line);
216
217
218
219
		if ($line != '' && substr($line, 0, 1) != ';'){
			if(strpos($line, ";") !== FALSE) $line = substr($line, 0, strpos($line, ";"));
			if(strpos($line, "(") !== FALSE) $line = substr($line, 0, strpos($line, "("));
			if(strpos($line, ")") !== FALSE) $line = substr($line, 0, strpos($line, ")"));
220
221
222
223
224
225
226
227
228
229
230
			$line = trim($line);
			if ($line != ''){
				$sPattern = '/\s+/m';
				$sReplace = ' ';
				$new_lines[] = preg_replace($sPattern, $sReplace, $line);
			}
		}
	}
	unset($lines);
	$lines = $new_lines;
	unset($new_lines);
231

232
	//$lines = file("apriqot.se.txt");
233
234
	$name = str_replace("txt", "", $_FILES['file']['name']);
	$name = str_replace("zone", "", $name);
235
236

	if ($domain !== NULL){
237
		$name = $domain;
238
	}
239
240

	if (substr($name, -1) != "."){
241
242
		$name .= ".";
	}
243

244
245
246
247
248
249
250
251
	$i = 0;
	$origin_exists = FALSE;
	$soa_array_key = -1;
	$soa = array();
	$soa['name'] = $name;
	$r = 0;
	$dns_rr = array();
	foreach($lines as $line){
252

253
		$parts = explode(' ', $line);
254

255
256
257
258
259
260
261
262
		// make all elements lowercase
		$new_parts = array();
		foreach($parts as $part){
			$new_parts[] = strtolower($part);
		}
		unset($parts);
		$parts = $new_parts;
		unset($new_parts);
263

264
265
266
267
268
269
270
		// if ORIGIN exists, overwrite $soa['name']
		if($parts[0] == '$origin'){
			$soa['name'] = $parts[1];
			$origin_exists = TRUE;
		}
		// TTL
		if($parts[0] == '$ttl'){
271
			$time_format = strtolower(substr($parts[1], -1));
272
			switch ($time_format) {
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
			case 's':
				$soa['ttl'] = $app->functions->intval(substr($parts[1], 0, -1));
				break;
			case 'm':
				$soa['ttl'] = $app->functions->intval(substr($parts[1], 0, -1)) * 60;
				break;
			case 'h':
				$soa['ttl'] = $app->functions->intval(substr($parts[1], 0, -1)) * 3600;
				break;
			case 'd':
				$soa['ttl'] = $app->functions->intval(substr($parts[1], 0, -1)) * 86400;
				break;
			case 'w':
				$soa['ttl'] = $app->functions->intval(substr($parts[1], 0, -1)) * 604800;
				break;
			default:
				$soa['ttl'] = $app->functions->intval($parts[1]);
290
291
			}
			unset($time_format);
292
293
294
295
296
297
298
299
300
301
302
303
304
305
		}
		// SOA
		if(in_array("soa", $parts)){
			$soa['mbox'] = array_pop($parts);
			//$soa['ns'] = array_pop($parts);
			$soa['ns'] = $servers[0]['server_name'];
			// if domain is part of SOA, overwrite $soa['name']
			if($parts[0] != '@' && $parts[0] != 'in' && $parts[0] != 'soa' && $origin_exists === FALSE){
				$soa['name'] = $parts[0];
			}
			$soa_array_key = $i;
			$valid_zone_file = TRUE;
		}
		// SERIAL
306
		if($i == ($soa_array_key + 1)) $soa['serial'] = $app->functions->intval($parts[0]);
307
		// REFRESH
308
		if($i == ($soa_array_key + 2)){
309
			$time_format = strtolower(substr($parts[0], -1));
310
			switch ($time_format) {
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
			case 's':
				$soa['refresh'] = $app->functions->intval(substr($parts[0], 0, -1));
				break;
			case 'm':
				$soa['refresh'] = $app->functions->intval(substr($parts[0], 0, -1)) * 60;
				break;
			case 'h':
				$soa['refresh'] = $app->functions->intval(substr($parts[0], 0, -1)) * 3600;
				break;
			case 'd':
				$soa['refresh'] = $app->functions->intval(substr($parts[0], 0, -1)) * 86400;
				break;
			case 'w':
				$soa['refresh'] = $app->functions->intval(substr($parts[0], 0, -1)) * 604800;
				break;
			default:
				$soa['refresh'] = $app->functions->intval($parts[0]);
328
329
330
			}
			unset($time_format);
		}
331
		// RETRY
332
		if($i == ($soa_array_key + 3)){
333
			$time_format = strtolower(substr($parts[0], -1));
334
			switch ($time_format) {
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
			case 's':
				$soa['retry'] = $app->functions->intval(substr($parts[0], 0, -1));
				break;
			case 'm':
				$soa['retry'] = $app->functions->intval(substr($parts[0], 0, -1)) * 60;
				break;
			case 'h':
				$soa['retry'] = $app->functions->intval(substr($parts[0], 0, -1)) * 3600;
				break;
			case 'd':
				$soa['retry'] = $app->functions->intval(substr($parts[0], 0, -1)) * 86400;
				break;
			case 'w':
				$soa['retry'] = $app->functions->intval(substr($parts[0], 0, -1)) * 604800;
				break;
			default:
				$soa['retry'] = $app->functions->intval($parts[0]);
352
353
354
			}
			unset($time_format);
		}
355
		// EXPIRE
356
		if($i == ($soa_array_key + 4)){
357
			$time_format = strtolower(substr($parts[0], -1));
358
			switch ($time_format) {
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
			case 's':
				$soa['expire'] = $app->functions->intval(substr($parts[0], 0, -1));
				break;
			case 'm':
				$soa['expire'] = $app->functions->intval(substr($parts[0], 0, -1)) * 60;
				break;
			case 'h':
				$soa['expire'] = $app->functions->intval(substr($parts[0], 0, -1)) * 3600;
				break;
			case 'd':
				$soa['expire'] = $app->functions->intval(substr($parts[0], 0, -1)) * 86400;
				break;
			case 'w':
				$soa['expire'] = $app->functions->intval(substr($parts[0], 0, -1)) * 604800;
				break;
			default:
				$soa['expire'] = $app->functions->intval($parts[0]);
376
377
378
			}
			unset($time_format);
		}
379
		// MINIMUM
380
		if($i == ($soa_array_key + 5)){
381
			$time_format = strtolower(substr($parts[0], -1));
382
			switch ($time_format) {
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
			case 's':
				$soa['minimum'] = $app->functions->intval(substr($parts[0], 0, -1));
				break;
			case 'm':
				$soa['minimum'] = $app->functions->intval(substr($parts[0], 0, -1)) * 60;
				break;
			case 'h':
				$soa['minimum'] = $app->functions->intval(substr($parts[0], 0, -1)) * 3600;
				break;
			case 'd':
				$soa['minimum'] = $app->functions->intval(substr($parts[0], 0, -1)) * 86400;
				break;
			case 'w':
				$soa['minimum'] = $app->functions->intval(substr($parts[0], 0, -1)) * 604800;
				break;
			default:
				$soa['minimum'] = $app->functions->intval($parts[0]);
400
401
402
			}
			unset($time_format);
		}
403
404
		// RESOURCE RECORDS
		if($i > ($soa_array_key + 5)){
405
			if(substr($parts[0], -1) == '.' || $parts[0] == '@' || ($parts[0] != 'a' && $parts[0] != 'aaaa' && $parts[0] != 'ns' && $parts[0] != 'cname' && $parts[0] != 'hinfo' && $parts[0] != 'mx' && $parts[0] != 'naptr' && $parts[0] != 'ptr' && $parts[0] != 'rp' && $parts[0] != 'srv' && $parts[0] != 'txt')){
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
				if(is_numeric($parts[1])){
					if($parts[2] == 'in'){
						$resource_type = $parts[3];
						$pkey = 3;
					} else {
						$resource_type = $parts[2];
						$pkey = 2;
					}
				} else {
					if($parts[1] == 'in'){
						$resource_type = $parts[2];
						$pkey = 2;
					} else {
						$resource_type = $parts[1];
						$pkey = 1;
					}
				}
				$dns_rr[$r]['type'] = $resource_type;
				if($parts[0] == '@' || $parts[0] == '.'){
					$dns_rr[$r]['name'] = $soa['name'];
				} else {
					$dns_rr[$r]['name'] = $parts[0];
				}
				if(is_numeric($parts[1])){
430
					$dns_rr[$r]['ttl'] = $app->functions->intval($parts[1]);
431
432
433
434
				} else {
					$dns_rr[$r]['ttl'] = $soa['ttl'];
				}
				switch ($resource_type) {
435
436
437
438
439
440
441
442
443
444
445
446
447
448
				case 'mx':
				case 'srv':
					$dns_rr[$r]['aux'] = $app->functions->intval($parts[$pkey+1]);
					$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+2));
					break;
				case 'txt':
					$dns_rr[$r]['aux'] = 0;
					$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
					if(substr($dns_rr[$r]['data'], 0, 1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 1);
					if(substr($dns_rr[$r]['data'], -1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 0, -1);
					break;
				default:
					$dns_rr[$r]['aux'] = 0;
					$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
449
450
451
452
453
454
455
456
				}
			} else {
				// a 3600 IN A 1.2.3.4
				if(is_numeric($parts[1]) && $parts[2] == 'in' && ($parts[3] == 'a' || $parts[3] == 'aaaa' || $parts[3] == 'ns'|| $parts[3] == 'cname' || $parts[3] == 'hinfo' || $parts[3] == 'mx' || $parts[3] == 'naptr' || $parts[3] == 'ptr' || $parts[3] == 'rp' || $parts[3] == 'srv' || $parts[3] == 'txt')){
					$resource_type = $parts[3];
					$pkey = 3;
					$dns_rr[$r]['type'] = $resource_type;
					$dns_rr[$r]['name'] = $parts[0];
457
					$dns_rr[$r]['ttl'] = $app->functions->intval($parts[1]);
458
					switch ($resource_type) {
459
460
461
462
463
464
465
466
467
468
469
470
471
472
					case 'mx':
					case 'srv':
						$dns_rr[$r]['aux'] = $app->functions->intval($parts[$pkey+1]);
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+2));
						break;
					case 'txt':
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
						if(substr($dns_rr[$r]['data'], 0, 1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 1);
						if(substr($dns_rr[$r]['data'], -1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 0, -1);
						break;
					default:
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
473
					}
474
				}
475
476
477
478
479
480
481
482
				// a IN A 1.2.3.4
				elseif($parts[1] == 'in' && ($parts[2] == 'a' || $parts[2] == 'aaaa' || $parts[2] == 'ns'|| $parts[2] == 'cname' || $parts[2] == 'hinfo' || $parts[2] == 'mx' || $parts[2] == 'naptr' || $parts[2] == 'ptr' || $parts[2] == 'rp' || $parts[2] == 'srv' || $parts[2] == 'txt')){
					$resource_type = $parts[2];
					$pkey = 2;
					$dns_rr[$r]['type'] = $resource_type;
					$dns_rr[$r]['name'] = $parts[0];
					$dns_rr[$r]['ttl'] = $soa['ttl'];
					switch ($resource_type) {
483
484
485
486
487
488
489
490
491
492
493
494
495
496
					case 'mx':
					case 'srv':
						$dns_rr[$r]['aux'] = $app->functions->intval($parts[$pkey+1]);
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+2));
						break;
					case 'txt':
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
						if(substr($dns_rr[$r]['data'], 0, 1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 1);
						if(substr($dns_rr[$r]['data'], -1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 0, -1);
						break;
					default:
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
497
					}
498
				}
499
500
501
502
503
504
				// a 3600 A 1.2.3.4
				elseif(is_numeric($parts[1]) && ($parts[2] == 'a' || $parts[2] == 'aaaa' || $parts[2] == 'ns'|| $parts[2] == 'cname' || $parts[2] == 'hinfo' || $parts[2] == 'mx' || $parts[2] == 'naptr' || $parts[2] == 'ptr' || $parts[2] == 'rp' || $parts[2] == 'srv' || $parts[2] == 'txt')){
					$resource_type = $parts[2];
					$pkey = 2;
					$dns_rr[$r]['type'] = $resource_type;
					$dns_rr[$r]['name'] = $parts[0];
505
					$dns_rr[$r]['ttl'] = $app->functions->intval($parts[1]);
506
					switch ($resource_type) {
507
508
509
510
511
512
513
514
515
516
517
518
519
520
					case 'mx':
					case 'srv':
						$dns_rr[$r]['aux'] = $app->functions->intval($parts[$pkey+1]);
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+2));
						break;
					case 'txt':
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
						if(substr($dns_rr[$r]['data'], 0, 1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 1);
						if(substr($dns_rr[$r]['data'], -1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 0, -1);
						break;
					default:
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
521
					}
522
				}
523
524
525
526
527
528
529
530
531
532
				// A 1.2.3.4
				// MX 10 mail
				// TXT "v=spf1 a mx ptr -all"
				else {
					$resource_type = $parts[0];
					$pkey = 0;
					$dns_rr[$r]['type'] = $resource_type;
					$dns_rr[$r]['name'] = $soa['name'];
					$dns_rr[$r]['ttl'] = $soa['ttl'];
					switch ($resource_type) {
533
534
535
536
537
538
539
540
541
542
543
544
545
546
					case 'mx':
					case 'srv':
						$dns_rr[$r]['aux'] = $app->functions->intval($parts[$pkey+1]);
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+2));
						break;
					case 'txt':
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
						if(substr($dns_rr[$r]['data'], 0, 1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 1);
						if(substr($dns_rr[$r]['data'], -1) == '"') $dns_rr[$r]['data'] = substr($dns_rr[$r]['data'], 0, -1);
						break;
					default:
						$dns_rr[$r]['aux'] = 0;
						$dns_rr[$r]['data'] = implode(' ', array_slice($parts, $pkey+1));
547
548
549
550
551
552
553
554
					}
				}
			}
			$dns_rr[$r]['type'] = strtoupper($dns_rr[$r]['type']);
			if($dns_rr[$r]['type'] == 'NS' && $dns_rr[$r]['name'] == $soa['name']){
				unset($dns_rr[$r]);
			}
			$r++;
555
		}
556
		$i++;
557
	}
558

559
	/*
560
561
562
563
564
565
	$i = 0;
	$r = 0;
	$s = 0;
	$dns_rr = array();
	foreach ($lines as $line)
	{
566
567
		$line = trim($line);
		if ($line != '' && substr($line,0,1) != ';' && substr($line,0,1) != '$')
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
		{
			$line = str_replace("\n",NULL,$line);
			$i++;

			// TODO - Find a better way to parse the SOA record. Lazy checking.
			if ($i <= 7)
			{
				if ($i > 1)
				{
					$s++;
					$line = str_replace("\t",NULL,$line);
					if (!empty($line))
					{
						print(strpos(";",$line));
						$line = substr($line,0,strpos($line,";"));
						if ($s == 1)
							$soa['serial'] = $line;
						else if ($s == 2)
							$soa['refresh'] = $line;
						else if ($s == 3)
							$soa['retry'] = $line;
						else if ($s == 4)
							$soa['expire'] = $line;
						else if ($s == 5)
592
							$soa['minimum'] = $line;
593
594
595
596
597
598

					}
				}
				else
				{
					$line = str_replace("\t",",",$line);
599
					$line = str_replace(" ",",",$line);
600
601
602
603
604
					$recs = explode(",",$line);

					foreach ($recs as $key => $rec)
					{
						$rec = trim($rec);
605
						if($rec == '') continue;
606
607
608
						//name	type	data	aux	ttl	active
						if ($key == 0)
						{
609
							if ($rec == '@')
610
611
612
613
614
615
616
617
618
619
620
621
622
							{
								$rec = $name;
							}

							$soa['name'] = $rec;
						}

						if ($key != 0 && strtolower($rec) == 'soa')
						{
							$typekeys[$s] = $key;
						}
						else if ($key > $typekey[$r])
						{
623
							if ($rec != "" && $rec != "(")
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
							{
								$rec = explode(" ",$rec);

								$soa['ns'] = $servers[0]['server_name'];
								$soa['mbox'] = $rec[1];
							}
						}
					}
				}
			}
			else
			{
				$line = str_replace("\n","",trim($line));

				if (!empty($line))
				{

					preg_match_all('/(.*?)\s*IN\s*(A|CNAME|MX|TXT|NS|AAAA)\s*(.*)/',$line, $recs);

					if ($recs[1][0] == '@' || trim($recs[1][0]) == "")
					{
						$recs[1][0] = $name;
					}
					$dns_rr[$r]['name'] = $recs[1][0];
					$dns_rr[$r]['type'] = $recs[2][0];
					if (strtolower($dns_rr[$r]['type'])=='mx')
					{
						$recs[3][0] = str_replace(" ","\t",$recs[3][0]);
						$mx[$r] = explode("\t",$recs[3][0]);
						for ($m=1;$m<count($mx[$r]);$m++)
						{
							if (!empty($mx[$r][$m]))
								$dns_rr[$r]['data'] = $mx[$r][$m];
						}
658

659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
						$dns_rr[$r]['aux'] = $mx[$r][0];
					}
					else if (strtolower($dns_rr[$r]['type'])=='txt')
					{
						$dns_rr[$r]['data'] = substr($recs[3][0],1,(strlen($recs[3][0])-2));
					}
					else
					{
						$dns_rr[$r]['data'] = $recs[3][0];
					}

					if (strtolower($dns_rr[$r]['type'])=='ns' && strtolower($dns_rr[$r]['name'])==$name)
					{
						unset($dns_rr[$r]);
					}

					$r++;
				}
			}

		}
	}
681
	*/
682

683
684
685
	foreach ($servers as $server){
		$dns_rr[$r]['name'] = $soa['name'];
		$dns_rr[$r]['type'] = 'NS';
686
687
688
689
		$dns_rr[$r]['data'] = $server['server_name'];
		$dns_rr[$r]['aux'] = 0;
		$r++;
	}
690
691
692
693
694
	//print('<pre>');
	//print_r($dns_rr);
	//print('</pre>');


695
696
697
698
699
700
701
702
	// Insert the soa record
	$sys_userid = $_SESSION['s']['user']['userid'];
	$origin = $app->db->quote($soa['name']);
	$ns = $app->db->quote($soa['ns']);
	$mbox = $app->db->quote($soa['mbox']);
	$refresh = $app->db->quote($soa['refresh']);
	$retry = $app->db->quote($soa['retry']);
	$expire = $app->db->quote($soa['expire']);
703
	$minimum = $app->db->quote($soa['minimum']);
704
705
	$ttl = $app->db->quote($soa['ttl']);
	$xfer = $app->db->quote('');
706
	$serial = $app->db->quote($app->functions->intval($soa['serial'])+1);
707
708
709
710
711
712
	//print_r($soa);
	//die();
	if($valid_zone_file){
		$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `origin`, `ns`, `mbox`, `serial`, `refresh`, `retry`, `expire`, `minimum`, `ttl`, `active`, `xfer`) VALUES
		('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$origin', '$ns', '$mbox', '$serial', '$refresh', '$retry', '$expire', '$minimum', '$ttl', 'Y', '$xfer')";
		$dns_soa_id = $app->db->datalogInsert('dns_soa', $insert_data, 'id');
713

714
715
		// Insert the dns_rr records
		if(is_array($dns_rr) && $dns_soa_id > 0)
716
		{
717
718
719
			foreach($dns_rr as $rr)
			{
				$insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES
720
				('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '".$app->db->quote($rr['name'])."', '".$app->db->quote($rr['type'])."', '".$app->db->quote($rr['data'])."', '".$app->db->quote($rr['aux'])."', '".$app->db->quote($rr['ttl'])."', 'Y')";
721
722
				$dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
			}
723
		}
724
		$msg .= $wb['zone_file_successfully_imported_txt'];
725
	} else {
726
		$error .= $wb['error_no_valid_zone_file_txt'];
727
	}
728
	//header('Location: /dns/dns_soa_edit.php?id='.$dns_soa_id);
729
730
731
732
} else {
	if(isset($_FILES['file']['name'])) {
		$error = $wb['no_file_uploaded_error'];
	}
733
734
}

735

736
737
$app->tpl->setVar('msg', $msg);
$app->tpl->setVar('error', $error);
738
739
740
741
742

$app->tpl_defaults();
$app->tpl->pparse();


743
?>