inc/record.inc.php
changeset 55 a885f557678f
parent 47 ae140472d97c
child 56 6db9adfa86ac
equal deleted inserted replaced
54:e858e7e1aab4 55:a885f557678f
    35 	 * NOTICE: Serial number *will* be RFC1912 compilant after update 
    35 	 * NOTICE: Serial number *will* be RFC1912 compilant after update 
    36 	 * NOTICE: This function will allow only 100 DNS zone transfers ;-)
    36 	 * NOTICE: This function will allow only 100 DNS zone transfers ;-)
    37 	 * YYYYMMDDnn
    37 	 * YYYYMMDDnn
    38 	 */
    38 	 */
    39 
    39 
    40 	$sqlq = "SELECT `notified_serial` FROM `domains` WHERE `id` = '".$domain_id."'";
    40 	$sqlq = "SELECT notified_serial FROM domains WHERE id = '".$domain_id."'";
    41 	$notified_serial = $db->queryOne($sqlq);
    41 	$notified_serial = $db->queryOne($sqlq);
    42 
    42 
    43 	$sqlq = "SELECT `content` FROM `records` WHERE `type` = 'SOA' AND `domain_id` = '".$domain_id."'";
    43 	$sqlq = "SELECT content FROM records WHERE type = 'SOA' AND domain_id = '".$domain_id."'";
    44 	$content = $db->queryOne($sqlq);
    44 	$content = $db->queryOne($sqlq);
    45     $need_to_update = false;
    45     $need_to_update = false;
    46 	
    46 	
    47 	// Getting the serial field.
    47 	// Getting the serial field.
    48 	$soa = explode(" ", $content);
    48 	$soa = explode(" ", $content);
    99 		// build new soa and update SQL after that
    99 		// build new soa and update SQL after that
   100 		for ($i = 0; $i < count($soa); $i++) 
   100 		for ($i = 0; $i < count($soa); $i++) 
   101 		{	
   101 		{	
   102 			$new_soa .= $soa[$i] . " "; 
   102 			$new_soa .= $soa[$i] . " "; 
   103 		}
   103 		}
   104 		$sqlq = "UPDATE `records` SET `content` = '".$new_soa."' WHERE `domain_id` = '".$domain_id."' AND `type` = 'SOA' LIMIT 1";
   104 		$sqlq = "UPDATE records SET content = '".$new_soa."' WHERE domain_id = '".$domain_id."' AND type = 'SOA'";
   105 		$db->Query($sqlq);
   105 		$db->Query($sqlq);
   106 		return true;
   106 		return true;
   107 	}
   107 	}
   108 }  
   108 }  
   109 
   109 
   282 
   282 
   283 	}
   283 	}
   284 	if (is_numeric($id))
   284 	if (is_numeric($id))
   285 	{
   285 	{
   286 	    $did = recid_to_domid($id);
   286 	    $did = recid_to_domid($id);
   287 		$db->query('DELETE FROM records WHERE id=' . $id . ' LIMIT 1');
   287 		$db->query('DELETE FROM records WHERE id=' . $id );
   288 		if ($type != 'SOA')
   288 		if ($type != 'SOA')
   289 		{
   289 		{
   290 			update_soa_serial($did);
   290 			update_soa_serial($did);
   291 		}
   291 		}
   292         // $id doesnt exist in database anymore so its deleted or just not there which means "true"	
   292         // $id doesnt exist in database anymore so its deleted or just not there which means "true"	
   362 				$ns1 = $GLOBALS["NS1"];
   362 				$ns1 = $GLOBALS["NS1"];
   363 				$hm  = $GLOBALS["HOSTMASTER"];
   363 				$hm  = $GLOBALS["HOSTMASTER"];
   364 				$ttl = $GLOBALS["DEFAULT_TTL"];
   364 				$ttl = $GLOBALS["DEFAULT_TTL"];
   365 
   365 
   366 				// Build and execute query
   366 				// Build and execute query
   367 				$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$domain', '$ns1 $hm 1', 'SOA', $ttl, '', '$now')";
   367 				$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$domain', '$ns1 $hm 1', 'SOA', $ttl, 0, '$now')";
   368 				$db->query($sql);
   368 				$db->query($sql);
   369 
   369 
   370 				// Done
   370 				// Done
   371 				return true;
   371 				return true;
   372 			}
   372 			}
   384 						// Parse the template.
   384 						// Parse the template.
   385 						$name     = parse_template_value($r["name"], $domain, $webip, $mailip);
   385 						$name     = parse_template_value($r["name"], $domain, $webip, $mailip);
   386 						$type     = $r["type"];
   386 						$type     = $r["type"];
   387 						$content  = parse_template_value($r["content"], $domain, $webip, $mailip);
   387 						$content  = parse_template_value($r["content"], $domain, $webip, $mailip);
   388 						$ttl      = $r["ttl"];
   388 						$ttl      = $r["ttl"];
   389 						$prio     = $r["prio"];
   389 						$prio     = intval($r["prio"]);
   390 
   390 
   391 						// If no ttl is given, use the default.
   391 						// If no ttl is given, use the default.
   392 						if (!$ttl)
   392 						if (!$ttl)
   393 						{
   393 						{
   394 							$ttl = $GLOBALS["DEFAULT_TTL"];
   394 							$ttl = $GLOBALS["DEFAULT_TTL"];
   717 	
   717 	
   718         WHERE record_owners.user_id = ".$_SESSION["userid"]."
   718         WHERE record_owners.user_id = ".$_SESSION["userid"]."
   719         AND record_owners.record_id = records.id
   719         AND record_owners.record_id = records.id
   720 	AND records.domain_id = ".$id."
   720 	AND records.domain_id = ".$id."
   721 
   721 
   722 	GROUP BY name, owner, users.fullname
   722 	GROUP BY domains.name, owner, users.fullname, domains.type
   723 	ORDER BY name";
   723 	ORDER BY domains.name";
   724 	
   724 	
   725 	$result = $db->queryRow($sqlq);
   725 	$result = $db->queryRow($sqlq);
   726 
   726 
   727 	$ret = array(
   727 	$ret = array(
   728 	"name"          =>              $result["name"],
   728 	"name"          =>              $result["name"],
   746 			FROM domains
   746 			FROM domains
   747 			LEFT JOIN records ON domains.id=records.domain_id
   747 			LEFT JOIN records ON domains.id=records.domain_id
   748 			LEFT JOIN zones ON domains.id=zones.domain_id
   748 			LEFT JOIN zones ON domains.id=zones.domain_id
   749 			LEFT JOIN users ON zones.owner=users.id
   749 			LEFT JOIN users ON zones.owner=users.id
   750 			WHERE domains.id=$id
   750 			WHERE domains.id=$id
   751 			GROUP BY name, owner, users.fullname
   751 			GROUP BY domains.name, owner, users.fullname, domains.type, zones.id
   752 			ORDER BY zones.id";
   752 			ORDER BY zones.id";
   753 
   753 
   754 		// Put the first occurence in an array and return it.
   754 		// Put the first occurence in an array and return it.
   755 		$result = $db->queryRow($sqlq);
   755 		$result = $db->queryRow($sqlq);
   756 
   756 
   861  * return values: the array of domains or -1 if nothing is found.
   861  * return values: the array of domains or -1 if nothing is found.
   862  */
   862  */
   863 function get_domains($userid=true,$letterstart=all,$rowstart=0,$rowamount=999999)
   863 function get_domains($userid=true,$letterstart=all,$rowstart=0,$rowamount=999999)
   864 {
   864 {
   865 	global $db;
   865 	global $db;
       
   866 	global $sql_regexp;
   866 	if((!level(5) || !$userid) && !level(10) && !level(5))
   867 	if((!level(5) || !$userid) && !level(10) && !level(5))
   867 	{
   868 	{
   868 		$add = " AND zones.owner=".$_SESSION["userid"];
   869 		$add = " AND zones.owner=".$_SESSION["userid"];
   869 	}
   870 	}
   870 	else
   871 	else
   877 	count(DISTINCT records.id) AS aantal,
   878 	count(DISTINCT records.id) AS aantal,
   878 	domains.name AS domainname
   879 	domains.name AS domainname
   879 	FROM domains
   880 	FROM domains
   880 	LEFT JOIN zones ON domains.id=zones.domain_id 
   881 	LEFT JOIN zones ON domains.id=zones.domain_id 
   881 	LEFT JOIN records ON records.domain_id=domains.id
   882 	LEFT JOIN records ON records.domain_id=domains.id
   882 	WHERE 1 $add ";
   883 	WHERE 1=1 $add ";
   883 	if ($letterstart!=all && $letterstart!=1) {
   884 	if ($letterstart!=all && $letterstart!=1) {
   884 	   $sqlq.=" AND substring(domains.name,1,1) REGEXP '^".$letterstart."' ";
   885 	   $sqlq.=" AND substring(domains.name,1,1) ".$sql_regexp." '^".$letterstart."' ";
   885 	} elseif ($letterstart==1) {
   886 	} elseif ($letterstart==1) {
   886 	   $sqlq.=" AND substring(domains.name,1,1) REGEXP '^[[:digit:]]'";
   887 	   $sqlq.=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'";
   887 	}
   888 	}
   888 	$sqlq.=" GROUP BY domainname, domain_id
   889 	$sqlq.=" GROUP BY domainname, domains.id
   889 	ORDER BY domainname
   890 	ORDER BY domainname
   890 	LIMIT $rowstart,$rowamount";
   891 	LIMIT $rowamount OFFSET $rowstart";
   891 
   892 
   892 	$result = $db->query($sqlq);
   893 	$result = $db->query($sqlq);
   893 	$result2 = $db->query($sqlq); 
   894 	$result2 = $db->query($sqlq); 
   894 	
   895 	
   895 	$numrows = $result2->numRows();
   896 	$numrows = $result2->numRows();
   920 		AND (records.id = record_owners.record_id
   921 		AND (records.id = record_owners.record_id
   921 		AND domains.id = records.domain_id)
   922 		AND domains.id = records.domain_id)
   922 		$andnot 
   923 		$andnot 
   923 		AND domains.name LIKE '".$letterstart."%' 
   924 		AND domains.name LIKE '".$letterstart."%' 
   924 		AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."')
   925 		AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."')
   925 		GROUP BY domainname, domain_id
   926 		GROUP BY domainname, domains.id
   926 		ORDER BY domainname";
   927 		ORDER BY domainname";
   927 
   928 
   928 		$result_extra = $db->query($sqlq);
   929 		$result_extra = $db->query($sqlq);
   929 
   930 
   930 	} else {
   931 	} else {
   935 		FROM domains, record_owners,records, zones
   936 		FROM domains, record_owners,records, zones
   936 		WHERE record_owners.user_id = '".$_SESSION["userid"]."'
   937 		WHERE record_owners.user_id = '".$_SESSION["userid"]."'
   937 		AND (records.id = record_owners.record_id
   938 		AND (records.id = record_owners.record_id
   938 		AND domains.id = records.domain_id)
   939 		AND domains.id = records.domain_id)
   939 		$andnot 
   940 		$andnot 
   940 		AND substring(domains.name,1,1) REGEXP '^[[:digit:]]'
   941 		AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'
   941 		AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."')
   942 		AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."')
   942 		GROUP BY domainname, domain_id
   943 		GROUP BY domainname, domains.id
   943 		ORDER BY domainname";
   944 		ORDER BY domainname";
   944 
   945 
   945 		$result_extra[$i] = $db->query($sqlq);
   946 		$result_extra[$i] = $db->query($sqlq);
   946 
   947 
   947 	}
   948 	}
  1007  * @return integer the number of zones
  1008  * @return integer the number of zones
  1008  */
  1009  */
  1009 
  1010 
  1010 function zone_count($userid=true, $letterstart=all) {
  1011 function zone_count($userid=true, $letterstart=all) {
  1011         global $db;
  1012         global $db;
       
  1013 	global $sql_regexp;
  1012         if((!level(5) || !$userid) && !level(10) && !level(5))
  1014         if((!level(5) || !$userid) && !level(10) && !level(5))
  1013         {
  1015         {
  1014 		// First select the zones for which we have ownership on one or more records.
  1016 		// First select the zones for which we have ownership on one or more records.
  1015 		$query = 'SELECT records.domain_id FROM records, record_owners WHERE user_id = '.$_SESSION['userid'].' AND records.id = record_owners.record_id';
  1017 		$query = 'SELECT records.domain_id FROM records, record_owners WHERE user_id = '.$_SESSION['userid'].' AND records.id = record_owners.record_id';
  1016 		$result = $db->query($query);
  1018 		$result = $db->query($query);
  1032         }
  1034         }
  1033 
  1035 
  1034         if ($letterstart!=all && $letterstart!=1) {
  1036         if ($letterstart!=all && $letterstart!=1) {
  1035            $add .=" AND domains.name LIKE '".$letterstart."%' ";
  1037            $add .=" AND domains.name LIKE '".$letterstart."%' ";
  1036         } elseif ($letterstart==1) {
  1038         } elseif ($letterstart==1) {
  1037            $add .=" AND substring(domains.name,1,1) REGEXP '^[[:digit:]]'";
  1039            $add .=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'";
  1038         }
  1040         }
  1039 
  1041 
  1040         if (level(5))
  1042         if (level(5))
  1041         {
  1043         {
  1042                 $query = 'SELECT count(distinct domains.id) as zone_count FROM domains WHERE 1 '.$add;
  1044                 $query = 'SELECT count(distinct domains.id) as zone_count FROM domains WHERE 1=1 '.$add;
  1043         }
  1045         }
  1044         else
  1046         else
  1045         {
  1047         {
  1046                 $query = 'SELECT count(distinct zones.domain_id) as zone_count FROM zones, domains WHERE zones.domain_id = domains.id '.$add;
  1048                 $query = 'SELECT count(distinct zones.domain_id) as zone_count FROM zones, domains WHERE zones.domain_id = domains.id '.$add;
  1047         }
  1049         }
  1107 		FROM record_owners,domains,records
  1109 		FROM record_owners,domains,records
  1108 		WHERE record_owners.user_id = ".$_SESSION["userid"]."
  1110 		WHERE record_owners.user_id = ".$_SESSION["userid"]."
  1109 		AND record_owners.record_id = records.id
  1111 		AND record_owners.record_id = records.id
  1110 		AND records.domain_id = ".$id."
  1112 		AND records.domain_id = ".$id."
  1111 		GROUP bY record_owners.record_id
  1113 		GROUP bY record_owners.record_id
  1112 		LIMIT $rowstart,$rowamount");
  1114 		LIMIT $rowamount OFFSET $rowstart");
  1113 
  1115 
  1114 		$ret = array();
  1116 		$ret = array();
  1115 		if($result->numRows() == 0)
  1117 		if($result->numRows() == 0)
  1116 		{
  1118 		{
  1117 		return -1;
  1119 		return -1;
  1129 		return $ret;
  1131 		return $ret;
  1130 		}
  1132 		}
  1131 
  1133 
  1132 		} else {
  1134 		} else {
  1133 
  1135 
  1134 		$result = $db->query("SELECT id FROM records WHERE domain_id=$id LIMIT $rowstart,$rowamount");
  1136 		$result = $db->query("SELECT id FROM records WHERE domain_id=$id LIMIT $rowamount OFFSET $rowstart");
  1135 		$ret = array();
  1137 		$ret = array();
  1136 		if($result->numRows() == 0)
  1138 		if($result->numRows() == 0)
  1137 		{
  1139 		{
  1138 			return -1;
  1140 			return -1;
  1139 		}
  1141 		}
  1200 		$S_INPUT_TYPE = -1;
  1202 		$S_INPUT_TYPE = -1;
  1201 	}
  1203 	}
  1202 	switch($S_INPUT_TYPE)
  1204 	switch($S_INPUT_TYPE)
  1203 	{
  1205 	{
  1204 		case '0': 
  1206 		case '0': 
  1205 			$sqlq = "SELECT * FROM `records` WHERE `content` = '".$question."' ORDER BY `type` DESC";
  1207 			$sqlq = "SELECT * FROM records WHERE content = '".$question."' ORDER BY type DESC";
  1206 			$result = $db->query($sqlq);
  1208 			$result = $db->query($sqlq);
  1207 			$ret_r = array();
  1209 			$ret_r = array();
  1208 			while ($r = $result->fetchRow())
  1210 			while ($r = $result->fetchRow())
  1209 			{
  1211 			{
  1210 			    if(xs($r['domain_id']))
  1212 			    if(xs($r['domain_id']))
  1222 				}
  1224 				}
  1223 			}
  1225 			}
  1224 			break;
  1226 			break;
  1225 	    
  1227 	    
  1226 		case '1' :
  1228 		case '1' :
  1227 			$sqlq = "SELECT `domains`.*, count(`records`.`id`) AS `numrec`, `zones`.`owner`, `records`.`domain_id`
  1229 			$sqlq = "SELECT domains.id, domains.name, count(records.id) AS numrec, zones.owner, records.domain_id
  1228 					FROM `domains`, `records`, `zones`  
  1230 					FROM domains, records, zones  
  1229 					WHERE `domains`.`id` = `records`.`domain_id` 
  1231 					WHERE domains.id = records.domain_id 
  1230 					AND `zones`.`domain_id` = `domains`.`id` 
  1232 					AND zones.domain_id = domains.id 
  1231 					AND `domains`.`name` = '".$question."' 
  1233 					AND domains.name = '".$question."' 
  1232 					GROUP BY (`domains`.`id`)";
  1234 					GROUP BY domains.id, domains.name, zones.owner, records.domain_id";
  1233 
  1235 
  1234 			$result = $db->query($sqlq);
  1236 			$result = $db->query($sqlq);
  1235 			$ret_d = array();
  1237 			$ret_d = array();
  1236 			while ($r = $result->fetchRow())
  1238 			while ($r = $result->fetchRow())
  1237 			{
  1239 			{
  1244     					'owner'		=>	$r['owner']
  1246     					'owner'		=>	$r['owner']
  1245     				);
  1247     				);
  1246 				}
  1248 				}
  1247 			}
  1249 			}
  1248 
  1250 
  1249 			$sqlq = "SELECT * FROM `records` WHERE `name` = '".$question."' OR `content` = '".$question."' ORDER BY `type` DESC";
  1251 			$sqlq = "SELECT * FROM records WHERE name = '".$question."' OR content = '".$question."' ORDER BY type DESC";
  1250 			$result = $db->query($sqlq);
  1252 			$result = $db->query($sqlq);
  1251 			while ($r = $result->fetchRow())
  1253 			while ($r = $result->fetchRow())
  1252 			{
  1254 			{
  1253 			    if(xs($r['domain_id']))
  1255 			    if(xs($r['domain_id']))
  1254 			    {
  1256 			    {
  1275 function get_domain_type($id)
  1277 function get_domain_type($id)
  1276 {
  1278 {
  1277 	global $db;
  1279 	global $db;
  1278         if (is_numeric($id))
  1280         if (is_numeric($id))
  1279 	{
  1281 	{
  1280 		$type = $db->queryOne("SELECT `type` FROM `domains` WHERE `id` = '".$id."'");
  1282 		$type = $db->queryOne("SELECT type FROM domains WHERE id = '".$id."'");
  1281 		if($type == "")
  1283 		if($type == "")
  1282 		{
  1284 		{
  1283 			$type = "NATIVE";
  1285 			$type = "NATIVE";
  1284 		}
  1286 		}
  1285 		return $type;
  1287 		return $type;
  1293 function get_domain_slave_master($id)
  1295 function get_domain_slave_master($id)
  1294 {
  1296 {
  1295 	global $db;
  1297 	global $db;
  1296         if (is_numeric($id))
  1298         if (is_numeric($id))
  1297 	{
  1299 	{
  1298 		$slave_master = $db->queryOne("SELECT `master` FROM `domains` WHERE `type` = 'SLAVE' and `id` = '".$id."'");
  1300 		$slave_master = $db->queryOne("SELECT master FROM domains WHERE type = 'SLAVE' and id = '".$id."'");
  1299 		return $slave_master;
  1301 		return $slave_master;
  1300         }
  1302         }
  1301         else
  1303         else
  1302         {
  1304         {
  1303                 error(sprintf(ERR_INV_ARG, "get_domain_slave_master", "no or no valid zoneid given"));
  1305                 error(sprintf(ERR_INV_ARG, "get_domain_slave_master", "no or no valid zoneid given"));
  1315 		// fiedl, but it is cleaner anyway.
  1317 		// fiedl, but it is cleaner anyway.
  1316 		if ($type != "SLAVE")
  1318 		if ($type != "SLAVE")
  1317 		{
  1319 		{
  1318 			$add = ", master=''";
  1320 			$add = ", master=''";
  1319 		}
  1321 		}
  1320 		$result = $db->query("UPDATE `domains` SET `type` = '" .$type. "'".$add." WHERE `id` = '".$id."'");
  1322 		$result = $db->query("UPDATE domains SET type = '" .$type. "'".$add." WHERE id = '".$id."'");
  1321 	}
  1323 	}
  1322         else
  1324         else
  1323         {
  1325         {
  1324                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1326                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1325         }
  1327         }
  1330 	global $db;
  1332 	global $db;
  1331         if (is_numeric($id))
  1333         if (is_numeric($id))
  1332 	{
  1334 	{
  1333        		if (is_valid_ip($slave_master) || is_valid_ip6($slave_master))
  1335        		if (is_valid_ip($slave_master) || is_valid_ip6($slave_master))
  1334 		{
  1336 		{
  1335 			$result = $db->query("UPDATE `domains` SET `master` = '" .$slave_master. "' WHERE `id` = '".$id."'");
  1337 			$result = $db->query("UPDATE domains SET master = '" .$slave_master. "' WHERE id = '".$id."'");
  1336 		}
  1338 		}
  1337 		else
  1339 		else
  1338 		{
  1340 		{
  1339 			error(sprintf(ERR_INV_ARGC, "change_domain_slave_master", "This is not a valid IPv4 or IPv6 address: $slave_master"));
  1341 			error(sprintf(ERR_INV_ARGC, "change_domain_slave_master", "This is not a valid IPv4 or IPv6 address: $slave_master"));
  1340 		}
  1342 		}