inc/record.inc.php
changeset 13 2ff220cfde13
parent 8 47dd15d8bb8c
child 19 f9ea2a05149b
equal deleted inserted replaced
12:7be4525290cc 13:2ff220cfde13
   190 		error(sprintf(ERR_INV_ARG, "add_record"));
   190 		error(sprintf(ERR_INV_ARG, "add_record"));
   191 	}
   191 	}
   192 }
   192 }
   193 
   193 
   194 
   194 
       
   195 function add_supermaster($master_ip, $ns_name, $account)
       
   196 {
       
   197         global $db;
       
   198         if (!is_valid_ip($master_ip) && !is_valid_ip6($master_ip))
       
   199         {
       
   200                 error(sprintf(ERR_INV_ARGC, "add_supermaster", "No or no valid ipv4 or ipv6 address given."));
       
   201         }
       
   202         if (!is_valid_hostname($ns_name))
       
   203         {
       
   204                 error(ERR_DNS_HOSTNAME);
       
   205         }
       
   206 	if (!validate_account($account))
       
   207 	{
       
   208 		error(sprintf(ERR_INV_ARGC, "add_supermaster", "given account name is invalid (alpha chars only)"));
       
   209 	}
       
   210         if (supermaster_exists($master_ip))
       
   211         {
       
   212                 error(sprintf(ERR_INV_ARGC, "add_supermaster", "supermaster already exists"));
       
   213         }
       
   214         else
       
   215         {
       
   216                 $db->query("INSERT INTO supermasters VALUES ('$master_ip', '$ns_name', '$account')");
       
   217                 return true;
       
   218         }
       
   219 }
       
   220 
       
   221 function delete_supermaster($master_ip)
       
   222 {
       
   223         global $db;
       
   224         if (!level(5))
       
   225         {
       
   226                 error(ERR_LEVEL_5);
       
   227         }
       
   228         if (is_valid_ip($master_ip) || is_valid_ip6($master_ip))
       
   229         {
       
   230                 $db->query("DELETE FROM supermasters WHERE ip = '$master_ip'");
       
   231                 return true;
       
   232         }
       
   233         else
       
   234         {
       
   235                 error(sprintf(ERR_INV_ARGC, "delete_supermaster", "No or no valid ipv4 or ipv6 address given."));
       
   236         }
       
   237 }
       
   238 
       
   239 function get_supermaster_info_from_ip($master_ip)
       
   240 {
       
   241 	global $db;
       
   242         if (!level(5))
       
   243         {
       
   244                 error(ERR_LEVEL_5);
       
   245         }
       
   246         if (is_valid_ip($master_ip) || is_valid_ip6($master_ip))
       
   247 	{
       
   248 	        $result = $db->queryRow("SELECT ip,nameserver,account FROM supermasters WHERE ip = '$master_ip'");
       
   249 
       
   250 		$ret = array(
       
   251 		"master_ip"	=>              $result["ip"],
       
   252 		"ns_name"	=>              $result["nameserver"],
       
   253 		"account"	=>              $result["account"]
       
   254 		);
       
   255 
       
   256 		return $ret;	
       
   257 	}
       
   258         else
       
   259 	{
       
   260                 error(sprintf(ERR_INV_ARGC, "get_supermaster_info_from_ip", "No or no valid ipv4 or ipv6 address given."));
       
   261         }
       
   262 }
       
   263 
       
   264 
   195 /*
   265 /*
   196  * Delete a record by a given id.
   266  * Delete a record by a given id.
   197  * return values: true, this function is always succesful.
   267  * return values: true, this function is always succesful.
   198  */
   268  */
   199 function delete_record($id)
   269 function delete_record($id)
   246  * This functions eats a template and by that it inserts various records.
   316  * This functions eats a template and by that it inserts various records.
   247  * first we start checking if something in an arpa record
   317  * first we start checking if something in an arpa record
   248  * remember to request nextID's from the database to be able to insert record.
   318  * remember to request nextID's from the database to be able to insert record.
   249  * if anything is invalid the function will error
   319  * if anything is invalid the function will error
   250  */
   320  */
   251 function add_domain($domain, $owner, $webip, $mailip, $empty, $type)
   321 function add_domain($domain, $owner, $webip, $mailip, $empty, $type, $slave_master)
   252 {
   322 {
   253 
   323 
   254 	global $db;
   324 	global $db;
   255 
   325 
   256 	if (!level(5))
   326 	if (!level(5))
   261 	// If domain, owner and mailip are given
   331 	// If domain, owner and mailip are given
   262 	// OR
   332 	// OR
   263 	// empty is given and owner and domain
   333 	// empty is given and owner and domain
   264 	// OR
   334 	// OR
   265 	// the domain is an arpa record and owner is given
   335 	// the domain is an arpa record and owner is given
       
   336 	// OR
       
   337 	// the type is slave, domain, owner and slave_master are given
   266 	// THAN
   338 	// THAN
   267 	// Continue this function
   339 	// Continue this function
   268 	if (($domain && $owner && $webip && $mailip) || ($empty && $owner && $domain) || (eregi('in-addr.arpa', $domain) && $owner))
   340 	if (($domain && $owner && $webip && $mailip) || ($empty && $owner && $domain) || (eregi('in-addr.arpa', $domain) && $owner) || $type=="SLAVE" && $domain && $owner && $slave_master)
   269 	{
   341 	{
   270                 
       
   271                 // First insert zone into domain table
   342                 // First insert zone into domain table
   272                 $db->query("INSERT INTO domains (name, type) VALUES ('$domain', '$type')");
   343                 $db->query("INSERT INTO domains (name, type) VALUES ('$domain', '$type')");
   273 
   344 
   274                 // Determine id of insert zone (in other words, find domain_id)
   345                 // Determine id of insert zone (in other words, find domain_id)
   275                 $iddomain = $db->lastInsertId('domains', 'id');
   346                 $iddomain = $db->lastInsertId('domains', 'id');
   278                 }
   349                 }
   279 
   350 
   280                 // Second, insert into zones tables
   351                 // Second, insert into zones tables
   281                 $db->query("INSERT INTO zones (domain_id, owner) VALUES ('$iddomain', $owner)");
   352                 $db->query("INSERT INTO zones (domain_id, owner) VALUES ('$iddomain', $owner)");
   282 
   353 
   283                 // Generate new timestamp. We need this one anyhow.
   354 		if ($type == "SLAVE")
   284                 $now = time();
   355 		{
   285 
   356 			$db->query("UPDATE domains SET master = '$slave_master' WHERE id = '$iddomain';");
   286 		if ($empty && $iddomain)
   357 			
   287 		{
   358 			// Done
   288                         // If we come into this if statement we dont want to apply templates.
   359 			return true;
   289                         // Retrieve configuration settings.
   360 		}
   290                         $ns1 = $GLOBALS["NS1"];
   361 		else
   291                         $hm  = $GLOBALS["HOSTMASTER"];
   362 		{
   292                         $ttl = $GLOBALS["DEFAULT_TTL"];
   363 			// Generate new timestamp. We need this one anyhow.
   293 
   364 			$now = time();
   294                         // Build and execute query
   365 
   295                         $sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$domain', '$ns1 $hm 1', 'SOA', $ttl, '', '$now')";
   366 			if ($empty && $iddomain)
   296                         $db->query($sql);
       
   297 
       
   298                         // Done
       
   299                         return true;
       
   300 		}
       
   301 		elseif ($iddomain)
       
   302 		{
       
   303 			// If we are here we want to apply templates.
       
   304 			global $template;
       
   305 
       
   306 			// Iterate over the template and apply it for each field.
       
   307 			foreach ($template as $r)
       
   308 			{
   367 			{
   309 				// Same type of if statement as previous.
   368 				// If we come into this if statement we dont want to apply templates.
   310 				if ((eregi('in-addr.arpa', $domain) && ($r["type"] == "NS" || $r["type"] == "SOA")) || (!eregi('in-addr.arpa', $domain)))
   369 				// Retrieve configuration settings.
       
   370 				$ns1 = $GLOBALS["NS1"];
       
   371 				$hm  = $GLOBALS["HOSTMASTER"];
       
   372 				$ttl = $GLOBALS["DEFAULT_TTL"];
       
   373 
       
   374 				// Build and execute query
       
   375 				$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$domain', '$ns1 $hm 1', 'SOA', $ttl, '', '$now')";
       
   376 				$db->query($sql);
       
   377 
       
   378 				// Done
       
   379 				return true;
       
   380 			}
       
   381 			elseif ($iddomain)
       
   382 			{
       
   383 				// If we are here we want to apply templates.
       
   384 				global $template;
       
   385 
       
   386 				// Iterate over the template and apply it for each field.
       
   387 				foreach ($template as $r)
   311 				{
   388 				{
   312 					// Parse the template.
   389 					// Same type of if statement as previous.
   313 					$name     = parse_template_value($r["name"], $domain, $webip, $mailip);
   390 					if ((eregi('in-addr.arpa', $domain) && ($r["type"] == "NS" || $r["type"] == "SOA")) || (!eregi('in-addr.arpa', $domain)))
   314 					$type     = $r["type"];
       
   315 					$content  = parse_template_value($r["content"], $domain, $webip, $mailip);
       
   316 					$ttl      = $r["ttl"];
       
   317 					$prio     = $r["prio"];
       
   318 
       
   319 					// If no ttl is given, use the default.
       
   320 					if (!$ttl)
       
   321 					{
   391 					{
   322 						$ttl = $GLOBALS["DEFAULT_TTL"];
   392 						// Parse the template.
       
   393 						$name     = parse_template_value($r["name"], $domain, $webip, $mailip);
       
   394 						$type     = $r["type"];
       
   395 						$content  = parse_template_value($r["content"], $domain, $webip, $mailip);
       
   396 						$ttl      = $r["ttl"];
       
   397 						$prio     = $r["prio"];
       
   398 
       
   399 						// If no ttl is given, use the default.
       
   400 						if (!$ttl)
       
   401 						{
       
   402 							$ttl = $GLOBALS["DEFAULT_TTL"];
       
   403 						}
       
   404 
       
   405 						$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$name','$content','$type','$ttl','$prio','$now')";
       
   406 						$db->query($sql);
   323 					}
   407 					}
   324 
       
   325 					$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$name','$content','$type','$ttl','$prio','$now')";
       
   326 					$db->query($sql);
       
   327 				}
   408 				}
   328 			}
   409 				// All done.
   329 			// All done.
   410 				return true;
   330 			return true;
   411 			 }
   331                  }
   412 			 else
   332                  else
   413 			 {
   333                  {
   414 				error(sprintf(ERR_INV_ARGC, "add_domain", "could not create zone"));
   334 			error(sprintf(ERR_INV_ARGC, "add_domain", "could not create zone"));
   415 			 }
   335                  }
   416 		}
   336 	}
   417 	}
   337 	else
   418 	else
   338 	{
   419 	{
   339 		error(sprintf(ERR_INV_ARG, "add_domain"));
   420 		error(sprintf(ERR_INV_ARG, "add_domain"));
   340 	}
   421 	}
   633 	if (is_numeric($id))
   714 	if (is_numeric($id))
   634 	{
   715 	{
   635 
   716 
   636 	if ($_SESSION[$id."_ispartial"] == 1) {
   717 	if ($_SESSION[$id."_ispartial"] == 1) {
   637 	
   718 	
   638 	$sqlq = "SELECT domains.name AS name,
   719 	$sqlq = "SELECT 
       
   720 	domains.type AS type,
       
   721 	domains.name AS name,
   639 	users.fullname AS owner,
   722 	users.fullname AS owner,
   640 	count(record_owners.id) AS aantal
   723 	count(record_owners.id) AS aantal
   641 	FROM domains, users, record_owners, records
   724 	FROM domains, users, record_owners, records
   642 	
   725 	
   643         WHERE record_owners.user_id = ".$_SESSION["userid"]."
   726         WHERE record_owners.user_id = ".$_SESSION["userid"]."
   651 
   734 
   652 	$ret = array(
   735 	$ret = array(
   653 	"name"          =>              $result["name"],
   736 	"name"          =>              $result["name"],
   654 	"ownerid"       =>              $_SESSION["userid"],
   737 	"ownerid"       =>              $_SESSION["userid"],
   655 	"owner"         =>              $result["owner"],
   738 	"owner"         =>              $result["owner"],
       
   739 	"type"		=>		$result["type"],
   656 	"numrec"        =>              $result["aantal"]
   740 	"numrec"        =>              $result["aantal"]
   657 	);
   741 	);
   658 
   742 
   659 	return $ret;
   743 	return $ret;
   660 
   744 
   661 	} else{
   745 	} else{
   662 	
   746 	
   663 		// Query that retrieves the information we need.
   747 		// Query that retrieves the information we need.
   664 		$sqlq = "SELECT domains.name AS name,
   748 		$sqlq = "SELECT 
       
   749 			domains.type AS type,
       
   750 			domains.name AS name,
   665 			min(zones.owner) AS ownerid,
   751 			min(zones.owner) AS ownerid,
   666 			users.fullname AS owner,
   752 			users.fullname AS owner,
   667 			count(records.domain_id) AS aantal
   753 			count(records.domain_id) AS aantal
   668 			FROM domains
   754 			FROM domains
   669 			LEFT JOIN records ON domains.id=records.domain_id
   755 			LEFT JOIN records ON domains.id=records.domain_id
   680 
   766 
   681 		$ret = array(
   767 		$ret = array(
   682 		"name"          =>              $result["name"],
   768 		"name"          =>              $result["name"],
   683 		"ownerid"       =>              $result["ownerid"],
   769 		"ownerid"       =>              $result["ownerid"],
   684 		"owner"         =>              $result["owner"],
   770 		"owner"         =>              $result["owner"],
       
   771 		"type"          =>              $result["type"],
   685 		"numrec"        =>              $result["aantal"]
   772 		"numrec"        =>              $result["aantal"]
   686 		);
   773 		);
   687 		return $ret;
   774 		return $ret;
   688 	}
   775 	}
   689 
   776 
   723 	{
   810 	{
   724 		error(ERR_DOMAIN_INVALID);
   811 		error(ERR_DOMAIN_INVALID);
   725 	}
   812 	}
   726 }
   813 }
   727 
   814 
   728 
   815 function get_supermasters()
   729 /*
   816 {
   730  * Get all domains from the database.
   817         global $db;
       
   818         $result = $db->query("SELECT ip, nameserver, account FROM supermasters");
       
   819         $ret = array();
       
   820 
       
   821         if($result->numRows() == 0)
       
   822         {
       
   823                 return -1;
       
   824         }
       
   825         else
       
   826         {
       
   827                 while ($r = $result->fetchRow())
       
   828                 {
       
   829                         $ret[] = array(
       
   830                         "master_ip"     => $r["ip"],
       
   831                         "ns_name"       => $r["nameserver"],
       
   832                         "account"       => $r["account"],
       
   833                         );
       
   834                         return $ret;
       
   835                 }
       
   836         }
       
   837 }
       
   838 
       
   839 function supermaster_exists($master_ip)
       
   840 {
       
   841         global $db;
       
   842         if (!level(5))
       
   843         {
       
   844                 error(ERR_LEVEL_5);
       
   845         }
       
   846         if (is_valid_ip($master_ip) || is_valid_ip6($master_ip))
       
   847         {
       
   848                 $result = $db->query("SELECT ip FROM supermasters WHERE ip = '$master_ip'");
       
   849                 if ($result->numRows() == 0)
       
   850                 {
       
   851                         return false;
       
   852                 }
       
   853                 elseif ($result->numRows() >= 1)
       
   854                 {
       
   855                         return true;
       
   856                 }
       
   857         }
       
   858         else
       
   859         {
       
   860                 error(sprintf(ERR_INV_ARGC, "supermaster_exists", "No or no valid IPv4 or IPv6 address given."));
       
   861         }
       
   862 }
       
   863 
       
   864 
       
   865 /*
       
   866  * Get all domains from the database 
   731  * This function gets all the domains from the database unless a user id is below 5.
   867  * This function gets all the domains from the database unless a user id is below 5.
   732  * if a user id is below 5 this function will only retrieve records for that user.
   868  * if a user id is below 5 this function will only retrieve records for that user.
   733  * return values: the array of domains or -1 if nothing is found.
   869  * return values: the array of domains or -1 if nothing is found.
   734  */
   870  */
   735 function get_domains($userid=true,$letterstart=all,$rowstart=0,$rowamount=999999)
   871 function get_domains($userid=true,$letterstart=all,$rowstart=0,$rowamount=999999)
  1095 }
  1231 }
  1096 
  1232 
  1097 function get_domain_type($id)
  1233 function get_domain_type($id)
  1098 {
  1234 {
  1099 	global $db;
  1235 	global $db;
  1100 	$type = $db->queryOne("SELECT `type` FROM `domains` WHERE `id` = '".$id."'");
  1236         if (is_numeric($id))
  1101 	if($type == "")
  1237 	{
  1102 	{
  1238 		$type = $db->queryOne("SELECT `type` FROM `domains` WHERE `id` = '".$id."'");
  1103 		$type = "NATIVE";
  1239 		if($type == "")
  1104 	}
  1240 		{
  1105 	return $type;
  1241 			$type = "NATIVE";
  1106     
  1242 		}
       
  1243 		return $type;
       
  1244         }
       
  1245         else
       
  1246         {
       
  1247                 error(sprintf(ERR_INV_ARG, "get_record_from_id", "no or no valid zoneid given"));
       
  1248         }
       
  1249 }
       
  1250 
       
  1251 function get_domain_slave_master($id)
       
  1252 {
       
  1253 	global $db;
       
  1254         if (is_numeric($id))
       
  1255 	{
       
  1256 		$slave_master = $db->queryOne("SELECT `master` FROM `domains` WHERE `type` = 'SLAVE' and `id` = '".$id."'");
       
  1257 		return $slave_master;
       
  1258         }
       
  1259         else
       
  1260         {
       
  1261                 error(sprintf(ERR_INV_ARG, "get_domain_slave_master", "no or no valid zoneid given"));
       
  1262         }
  1107 }
  1263 }
  1108 
  1264 
  1109 function change_domain_type($type, $id)
  1265 function change_domain_type($type, $id)
  1110 {
  1266 {
  1111     global $db;
  1267 	global $db;
  1112     $result = $db->query("UPDATE `domains` SET `type` = '" .$type. "' WHERE `id` = '".$id."'");
  1268 	unset($add);
       
  1269         if (is_numeric($id))
       
  1270 	{
       
  1271 		// It is not really neccesary to clear the master field if a 
       
  1272 		// zone is not of the type "slave" as powerdns will ignore that
       
  1273 		// fiedl, but it is cleaner anyway.
       
  1274 		if ($type != "SLAVE")
       
  1275 		{
       
  1276 			$add = ", master=''";
       
  1277 		}
       
  1278 		$result = $db->query("UPDATE `domains` SET `type` = '" .$type. "'".$add." WHERE `id` = '".$id."'");
       
  1279 	}
       
  1280         else
       
  1281         {
       
  1282                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
       
  1283         }
       
  1284 }
       
  1285 
       
  1286 function change_domain_slave_master($id, $slave_master)
       
  1287 {
       
  1288 	global $db;
       
  1289         if (is_numeric($id))
       
  1290 	{
       
  1291        		if (is_valid_ip($slave_master) || is_valid_ip6($slave_master))
       
  1292 		{
       
  1293 			$result = $db->query("UPDATE `domains` SET `master` = '" .$slave_master. "' WHERE `id` = '".$id."'");
       
  1294 		}
       
  1295 		else
       
  1296 		{
       
  1297 			error(sprintf(ERR_INV_ARGC, "change_domain_slave_master", "This is not a valid IPv4 or IPv6 address: $slave_master"));
       
  1298 		}
       
  1299 	}
       
  1300         else
       
  1301         {
       
  1302                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
       
  1303         }
       
  1304 }
       
  1305 
       
  1306 
       
  1307 function validate_account($account)
       
  1308 {
       
  1309 	
       
  1310   	if(preg_match("/^[A-Z0-9._-]+$/i",$account))
       
  1311 	{
       
  1312 		return true;
       
  1313 	}
       
  1314 	else
       
  1315 	{
       
  1316 		return false;
       
  1317 	}
  1113 }
  1318 }
  1114 ?>
  1319 ?>