inc/record.inc.php
changeset 192 3d18290ac993
parent 182 049347a649e0
child 200 6bdfaba077cb
equal deleted inserted replaced
191:963d62dc1c80 192:3d18290ac993
    19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    20  */
    20  */
    21 
    21 
    22 function zone_id_exists($zid) {
    22 function zone_id_exists($zid) {
    23 	global $db;
    23 	global $db;
    24 	$query = "SELECT COUNT(id) FROM domains WHERE id = " . $db->quote($zid);
    24 	$query = "SELECT COUNT(id) FROM domains WHERE id = " . $db->quote($zid, 'integer');
    25 	$count = $db->queryOne($query);
    25 	$count = $db->queryOne($query);
    26 	if (PEAR::isError($count)) { error($result->getMessage()); return false; }
    26 	if (PEAR::isError($count)) { error($result->getMessage()); return false; }
    27 	return $count;
    27 	return $count;
    28 }
    28 }
    29 
    29 
    30 
    30 
    31 function get_zone_id_from_record_id($rid) {
    31 function get_zone_id_from_record_id($rid) {
    32 	global $db;
    32 	global $db;
    33 	$query = "SELECT domain_id FROM records WHERE id = " . $db->quote($rid);
    33 	$query = "SELECT domain_id FROM records WHERE id = " . $db->quote($rid, 'integer');
    34 	$zid = $db->queryOne($query);
    34 	$zid = $db->queryOne($query);
    35 	return $zid;
    35 	return $zid;
    36 }
    36 }
    37 
    37 
    38 function count_zone_records($zone_id) {
    38 function count_zone_records($zone_id) {
    39 	global $db;
    39 	global $db;
    40 	$sqlq = "SELECT COUNT(id) FROM records WHERE domain_id = ".$db->quote($zone_id);
    40 	$sqlq = "SELECT COUNT(id) FROM records WHERE domain_id = ".$db->quote($zone_id, 'integer');
    41 	$record_count = $db->queryOne($sqlq);
    41 	$record_count = $db->queryOne($sqlq);
    42 	return $record_count;
    42 	return $record_count;
    43 }
    43 }
    44 
    44 
    45 function update_soa_serial($domain_id)
    45 function update_soa_serial($domain_id)
    46 {
    46 {
    47 	global $db;
    47 	global $db;
    48 
    48 
    49 	$sqlq = "SELECT notified_serial FROM domains WHERE id = ".$db->quote($domain_id);
    49 	$sqlq = "SELECT notified_serial FROM domains WHERE id = ".$db->quote($domain_id, 'integer');
    50 	$notified_serial = $db->queryOne($sqlq);
    50 	$notified_serial = $db->queryOne($sqlq);
    51 
    51 
    52 	$sqlq = "SELECT content FROM records WHERE type = 'SOA' AND domain_id = ".$db->quote($domain_id);
    52 	$sqlq = "SELECT content FROM records WHERE type = ".$db->quote('SOA', 'text')." AND domain_id = ".$db->quote($domain_id, 'integer');
    53 	$content = $db->queryOne($sqlq);
    53 	$content = $db->queryOne($sqlq);
    54 	$need_to_update = false;
    54 	$need_to_update = false;
    55 
    55 
    56 	// Getting the serial field.
    56 	// Getting the serial field.
    57 	$soa = explode(" ", $content);
    57 	$soa = explode(" ", $content);
    88 		$new_soa = "";		
    88 		$new_soa = "";		
    89 		// build new soa and update SQL after that
    89 		// build new soa and update SQL after that
    90 		for ($i = 0; $i < count($soa); $i++) {	
    90 		for ($i = 0; $i < count($soa); $i++) {	
    91 			$new_soa .= $soa[$i] . " "; 
    91 			$new_soa .= $soa[$i] . " "; 
    92 		}
    92 		}
    93 		$sqlq = "UPDATE records SET content = ".$db->quote($new_soa)." WHERE domain_id = ".$db->quote($domain_id)." AND type = 'SOA'";
    93 		$sqlq = "UPDATE records SET content = ".$db->quote($new_soa, 'text')." WHERE domain_id = ".$db->quote($domain_id, 'integer')." AND type = ".$db->quote('SOA', 'text');
    94 		$db->Query($sqlq);
    94 		$db->Query($sqlq);
    95 		return true;
    95 		return true;
    96 	}
    96 	}
    97 }  
    97 }  
    98 
    98 
   121 		global $db;
   121 		global $db;
   122 		// TODO: no need to check for numeric-ness of zone id if we check with validate_input as well?
   122 		// TODO: no need to check for numeric-ness of zone id if we check with validate_input as well?
   123 		if (is_numeric($record['zid'])) {
   123 		if (is_numeric($record['zid'])) {
   124 			if (validate_input($record['zid'], $record['type'], $record['content'], $record['name'], $record['prio'], $record['ttl'])) {
   124 			if (validate_input($record['zid'], $record['type'], $record['content'], $record['name'], $record['prio'], $record['ttl'])) {
   125 				$query = "UPDATE records 
   125 				$query = "UPDATE records 
   126 					SET name=".$db->quote($record['name']).", 
   126 					SET name=".$db->quote($record['name'], 'text').", 
   127 					type=".$db->quote($record['type']).", 
   127 					type=".$db->quote($record['type'], 'text').", 
   128 					content='" . $record['content'] . "', 
   128 					content=" . $db->quote($record['content'], 'text') . ", 
   129 					ttl=".$db->quote($record['ttl']).", 
   129 					ttl=".$db->quote($record['ttl'], 'integer').", 
   130 					prio=".$db->quote($record['prio'], 'integer').", 
   130 					prio=".$db->quote($record['prio'], 'integer').", 
   131 					change_date=".$db->quote(time())." 
   131 					change_date=".$db->quote(time(), 'integer')." 
   132 					WHERE id=".$db->quote($record['rid']);
   132 					WHERE id=".$db->quote($record['rid'], 'integer');
   133 				$result = $db->Query($query);
   133 				$result = $db->Query($query);
   134 				if (PEAR::isError($result)) {
   134 				if (PEAR::isError($result)) {
   135 					error($result->getMessage());
   135 					error($result->getMessage());
   136 					return false;
   136 					return false;
   137 				} elseif ($record['type'] != 'SOA') {
   137 				} elseif ($record['type'] != 'SOA') {
   171 		return false;
   171 		return false;
   172 	} else {
   172 	} else {
   173 		if (validate_input($zoneid, $type, $content, $name, $prio, $ttl) ) {
   173 		if (validate_input($zoneid, $type, $content, $name, $prio, $ttl) ) {
   174 			$change = time();
   174 			$change = time();
   175 			$query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES ("
   175 			$query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES ("
   176 						. $db->quote($zoneid) . ","
   176 						. $db->quote($zoneid, 'integer') . ","
   177 						. $db->quote($name) . "," 
   177 						. $db->quote($name, 'text') . "," 
   178 						. $db->quote($type) . "," 
   178 						. $db->quote($type, 'text') . "," 
   179 						. $db->quote($content) . ","
   179 						. $db->quote($content, 'text') . ","
   180 						. $db->quote($ttl) . ","
   180 						. $db->quote($ttl, 'integer') . ","
   181 						. $db->quote($prio, 'integer') . ","
   181 						. $db->quote($prio, 'integer') . ","
   182 						. $db->quote($change) . ")";
   182 						. $db->quote($change, 'integer') . ")";
   183 			$response = $db->query($query);
   183 			$response = $db->query($query);
   184 			if (PEAR::isError($response)) {
   184 			if (PEAR::isError($response)) {
   185 				error($response->getMessage());
   185 				error($response->getMessage());
   186 				return false;
   186 				return false;
   187 			} else {
   187 			} else {
   213 	}
   213 	}
   214         if (supermaster_exists($master_ip)) {
   214         if (supermaster_exists($master_ip)) {
   215                 error(ERR_SM_EXISTS);
   215                 error(ERR_SM_EXISTS);
   216 		return false;
   216 		return false;
   217         } else {
   217         } else {
   218                 $db->query("INSERT INTO supermasters VALUES (".$db->quote($master_ip).", ".$db->quote($ns_name).", ".$db->quote($account).")");
   218                 $db->query("INSERT INTO supermasters VALUES (".$db->quote($master_ip, 'text').", ".$db->quote($ns_name, 'text').", ".$db->quote($account, 'text').")");
   219                 return true;
   219                 return true;
   220         }
   220         }
   221 }
   221 }
   222 
   222 
   223 function delete_supermaster($master_ip) {
   223 function delete_supermaster($master_ip) {
   224 	global $db;
   224 	global $db;
   225         if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip))
   225         if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip))
   226         {
   226         {
   227                 $db->query("DELETE FROM supermasters WHERE ip = ".$db->quote($master_ip));
   227                 $db->query("DELETE FROM supermasters WHERE ip = ".$db->quote($master_ip, 'text'));
   228                 return true;
   228                 return true;
   229         }
   229         }
   230         else
   230         else
   231         {
   231         {
   232                 error(sprintf(ERR_INV_ARGC, "delete_supermaster", "No or no valid ipv4 or ipv6 address given."));
   232                 error(sprintf(ERR_INV_ARGC, "delete_supermaster", "No or no valid ipv4 or ipv6 address given."));
   236 function get_supermaster_info_from_ip($master_ip)
   236 function get_supermaster_info_from_ip($master_ip)
   237 {
   237 {
   238 	global $db;
   238 	global $db;
   239         if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip))
   239         if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip))
   240 	{
   240 	{
   241 	        $result = $db->queryRow("SELECT ip,nameserver,account FROM supermasters WHERE ip = ".$db->quote($master_ip));
   241 	        $result = $db->queryRow("SELECT ip,nameserver,account FROM supermasters WHERE ip = ".$db->quote($master_ip, 'text'));
   242 
   242 
   243 		$ret = array(
   243 		$ret = array(
   244 		"master_ip"	=>              $result["ip"],
   244 		"master_ip"	=>              $result["ip"],
   245 		"ns_name"	=>              $result["nameserver"],
   245 		"ns_name"	=>              $result["nameserver"],
   246 		"account"	=>              $result["account"]
   246 		"account"	=>              $result["account"]
   256 
   256 
   257 function get_record_details_from_record_id($rid) {
   257 function get_record_details_from_record_id($rid) {
   258 
   258 
   259 	global $db;
   259 	global $db;
   260 
   260 
   261 	$query = "SELECT id AS rid, domain_id AS zid, name, type, content, ttl, prio, change_date FROM records WHERE id = " . $db->quote($rid) ;
   261 	$query = "SELECT id AS rid, domain_id AS zid, name, type, content, ttl, prio, change_date FROM records WHERE id = " . $db->quote($rid, 'integer') ;
   262 
   262 
   263 	$response = $db->query($query);
   263 	$response = $db->query($query);
   264 	if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   264 	if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   265 	
   265 	
   266 	$return = $response->fetchRow();
   266 	$return = $response->fetchRow();
   285 
   285 
   286 	if ( $perm_content_edit == "all" || ($perm_content_edit == "own" && $user_is_zone_owner == "0" )) {
   286 	if ( $perm_content_edit == "all" || ($perm_content_edit == "own" && $user_is_zone_owner == "0" )) {
   287 		if ($record['type'] == "SOA") {
   287 		if ($record['type'] == "SOA") {
   288 			error(_('You are trying to delete the SOA record. If are not allowed to remove it, unless you remove the entire zone.'));
   288 			error(_('You are trying to delete the SOA record. If are not allowed to remove it, unless you remove the entire zone.'));
   289 		} else {
   289 		} else {
   290 			$query = "DELETE FROM records WHERE id = " . $db->quote($rid);
   290 			$query = "DELETE FROM records WHERE id = " . $db->quote($rid, 'integer');
   291 			$response = $db->query($query);
   291 			$response = $db->query($query);
   292 			if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   292 			if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   293 			return true;
   293 			return true;
   294 		}
   294 		}
   295 	} else {
   295 	} else {
   325 		if (($domain && $owner && $webip && $mailip) || 
   325 		if (($domain && $owner && $webip && $mailip) || 
   326 				($empty && $owner && $domain) || 
   326 				($empty && $owner && $domain) || 
   327 				(eregi('in-addr.arpa', $domain) && $owner) || 
   327 				(eregi('in-addr.arpa', $domain) && $owner) || 
   328 				$type=="SLAVE" && $domain && $owner && $slave_master) {
   328 				$type=="SLAVE" && $domain && $owner && $slave_master) {
   329 
   329 
   330 			$response = $db->query("INSERT INTO domains (name, type) VALUES (".$db->quote($domain).", ".$db->quote($type).")");
   330 			$response = $db->query("INSERT INTO domains (name, type) VALUES (".$db->quote($domain, 'text').", ".$db->quote($type, 'text').")");
   331 			if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   331 			if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   332 
   332 
   333 			$domain_id = $db->lastInsertId('domains', 'id');
   333 			$domain_id = $db->lastInsertId('domains', 'id');
   334 			if (PEAR::isError($domain_id)) { error($id->getMessage()); return false; }
   334 			if (PEAR::isError($domain_id)) { error($id->getMessage()); return false; }
   335 
   335 
   336 			$response = $db->query("INSERT INTO zones (domain_id, owner) VALUES (".$db->quote($domain_id).", ".$db->quote($owner).")");
   336 			$response = $db->query("INSERT INTO zones (domain_id, owner) VALUES (".$db->quote($domain_id, 'integer').", ".$db->quote($owner, 'integer').")");
   337 			if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   337 			if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   338 
   338 
   339 			if ($type == "SLAVE") {
   339 			if ($type == "SLAVE") {
   340 				$response = $db->query("UPDATE domains SET master = ".$db->quote($slave_master)." WHERE id = ".$db->quote($domain_id));
   340 				$response = $db->query("UPDATE domains SET master = ".$db->quote($slave_master, 'text')." WHERE id = ".$db->quote($domain_id, 'integer'));
   341 				if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   341 				if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   342 				return true;
   342 				return true;
   343 			} else {
   343 			} else {
   344 				$now = time();
   344 				$now = time();
   345 				if ($empty && $domain_id) {
   345 				if ($empty && $domain_id) {
   346 					$ns1 = $dns_ns1;
   346 					$ns1 = $dns_ns1;
   347 					$hm  = $dns_hostmaster;
   347 					$hm  = $dns_hostmaster;
   348 					$ttl = $dns_ttl;
   348 					$ttl = $dns_ttl;
   349 
   349 
   350 					$query = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES (" 
   350 					$query = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES (" 
   351 							. $db->quote($domain_id) . "," 
   351 							. $db->quote($domain_id, 'integer') . "," 
   352 							. $db->quote($domain) . "," 
   352 							. $db->quote($domain, 'text') . "," 
   353 							. $db->quote($ns1.' '.$hm.' 1') . ","
   353 							. $db->quote($ns1.' '.$hm.' 1', 'text') . ","
   354 							. $db->quote('SOA').","
   354 							. $db->quote('SOA', 'text').","
   355 							. $db->quote($ttl) 
   355 							. $db->quote($ttl, 'integer')."," 
   356 							. ", 0, "
   356 							. $db->quote(0, 'integer'). ","
   357 							. $db->quote($now).")";
   357 							. $db->quote($now, 'integer').")";
   358 					$response = $db->query($query);
   358 					$response = $db->query($query);
   359 					if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   359 					if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   360 				} elseif ($domain_id) {
   360 				} elseif ($domain_id) {
   361 					global $template;
   361 					global $template;
   362 					global $dns_ttl;
   362 					global $dns_ttl;
   373 							if (!$ttl) {
   373 							if (!$ttl) {
   374 								$ttl = $dns_ttl;
   374 								$ttl = $dns_ttl;
   375 							}
   375 							}
   376 
   376 
   377 							$query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES (" 
   377 							$query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES (" 
   378 									. $db->quote($domain_id) . ","
   378 									. $db->quote($domain_id, 'integer') . ","
   379 									. $db->quote($name) . ","
   379 									. $db->quote($name, 'text') . ","
   380 									. $db->quote($type) . ","
   380 									. $db->quote($type, 'text') . ","
   381 									. $db->quote($content) . ","
   381 									. $db->quote($content, 'text') . ","
   382 									. $db->quote($ttl) . ","
   382 									. $db->quote($ttl, 'integer') . ","
   383 									. $db->quote($prio, 'integer') . ","
   383 									. $db->quote($prio, 'integer') . ","
   384 									. $db->quote($now) . ")";
   384 									. $db->quote($now, 'integer') . ")";
   385 							$response = $db->query($query);
   385 							$response = $db->query($query);
   386 							if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   386 							if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   387 						}
   387 						}
   388 					}
   388 					}
   389 					return true;
   389 					return true;
   414 	else { $perm_edit = "none" ; }
   414 	else { $perm_edit = "none" ; }
   415 	$user_is_zone_owner = verify_user_is_owner_zoneid($id);
   415 	$user_is_zone_owner = verify_user_is_owner_zoneid($id);
   416 
   416 
   417         if ( $perm_edit == "all" || ( $perm_edit == "own" && $user_is_zone_owner == "1") ) {    
   417         if ( $perm_edit == "all" || ( $perm_edit == "own" && $user_is_zone_owner == "1") ) {    
   418 		if (is_numeric($id)) {
   418 		if (is_numeric($id)) {
   419 			$db->query("DELETE FROM zones WHERE domain_id=".$db->quote($id));
   419 			$db->query("DELETE FROM zones WHERE domain_id=".$db->quote($id, 'integer'));
   420 			$db->query("DELETE FROM domains WHERE id=".$db->quote($id));
   420 			$db->query("DELETE FROM domains WHERE id=".$db->quote($id, 'integer'));
   421 			$db->query("DELETE FROM records WHERE domain_id=".$db->quote($id));
   421 			$db->query("DELETE FROM records WHERE domain_id=".$db->quote($id, 'integer'));
   422 			return true;
   422 			return true;
   423 		} else {
   423 		} else {
   424 			error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number"));
   424 			error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number"));
   425 			return false;
   425 			return false;
   426 		}
   426 		}
   437 function recid_to_domid($id)
   437 function recid_to_domid($id)
   438 {
   438 {
   439 	global $db;
   439 	global $db;
   440 	if (is_numeric($id))
   440 	if (is_numeric($id))
   441 	{
   441 	{
   442 		$result = $db->query("SELECT domain_id FROM records WHERE id=".$db->quote($id));
   442 		$result = $db->query("SELECT domain_id FROM records WHERE id=".$db->quote($id, 'integer'));
   443 		$r = $result->fetchRow();
   443 		$r = $result->fetchRow();
   444 		return $r["domain_id"];
   444 		return $r["domain_id"];
   445 	}
   445 	}
   446 	else
   446 	else
   447 	{
   447 	{
   459 	global $db;
   459 	global $db;
   460 	if ( (verify_permission('zone_meta_edit_others')) || (verify_permission('zone_meta_edit_own')) && verify_user_is_owner_zoneid($_GET["id"])) {
   460 	if ( (verify_permission('zone_meta_edit_others')) || (verify_permission('zone_meta_edit_own')) && verify_user_is_owner_zoneid($_GET["id"])) {
   461 		// User is allowed to make change to meta data of this zone.
   461 		// User is allowed to make change to meta data of this zone.
   462 		if (is_numeric($zone_id) && is_numeric($user_id) && is_valid_user($user_id))
   462 		if (is_numeric($zone_id) && is_numeric($user_id) && is_valid_user($user_id))
   463 		{
   463 		{
   464 			if($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=".$db->quote($user_id)." AND domain_id=".$db->quote($zone_id)) == 0)
   464 			if($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=".$db->quote($user_id, 'integer')." AND domain_id=".$db->quote($zone_id, 'integer')) == 0)
   465 			{
   465 			{
   466 				$db->query("INSERT INTO zones (domain_id, owner) VALUES(".$db->quote($zone_id).", ".$db->quote($user_id).")");
   466 				$db->query("INSERT INTO zones (domain_id, owner) VALUES(".$db->quote($zone_id, 'integer').", ".$db->quote($user_id, 'integer').")");
   467 			}
   467 			}
   468 			return true;
   468 			return true;
   469 		} else {
   469 		} else {
   470 			error(sprintf(ERR_INV_ARGC, "add_owner_to_zone", "$zone_id / $user_id"));
   470 			error(sprintf(ERR_INV_ARGC, "add_owner_to_zone", "$zone_id / $user_id"));
   471 		}
   471 		}
   481 	if ( (verify_permission('zone_meta_edit_others')) || (verify_permission('zone_meta_edit_own')) && verify_user_is_owner_zoneid($_GET["id"])) {
   481 	if ( (verify_permission('zone_meta_edit_others')) || (verify_permission('zone_meta_edit_own')) && verify_user_is_owner_zoneid($_GET["id"])) {
   482 		// User is allowed to make change to meta data of this zone.
   482 		// User is allowed to make change to meta data of this zone.
   483 		if (is_numeric($zone_id) && is_numeric($user_id) && is_valid_user($user_id))
   483 		if (is_numeric($zone_id) && is_numeric($user_id) && is_valid_user($user_id))
   484 		{
   484 		{
   485 			// TODO: Next if() required, why not just execute DELETE query?
   485 			// TODO: Next if() required, why not just execute DELETE query?
   486 			if($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=".$db->quote($user_id)." AND domain_id=".$db->quote($zone_id)) != 0)
   486 			if($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=".$db->quote($user_id, 'integer')." AND domain_id=".$db->quote($zone_id, 'integer')) != 0)
   487 			{
   487 			{
   488 				$db->query("DELETE FROM zones WHERE owner=".$db->quote($user_id)." AND domain_id=".$db->quote($zone_id));
   488 				$db->query("DELETE FROM zones WHERE owner=".$db->quote($user_id, 'integer')." AND domain_id=".$db->quote($zone_id, 'integer'));
   489 			}
   489 			}
   490 			return true;
   490 			return true;
   491 		} else {
   491 		} else {
   492 			error(sprintf(ERR_INV_ARGC, "delete_owner_from_zone", "$zone_id / $user_id"));
   492 			error(sprintf(ERR_INV_ARGC, "delete_owner_from_zone", "$zone_id / $user_id"));
   493 		}
   493 		}
   526 	}
   526 	}
   527 
   527 
   528 	// Get the domain id.
   528 	// Get the domain id.
   529 	$domid = recid_to_domid($recid);
   529 	$domid = recid_to_domid($recid);
   530 
   530 
   531 	$result = $db->query("select id, type from records where domain_id=".$db->quote($recid)." and type=".$db->quote($type));
   531 	$result = $db->query("select id, type from records where domain_id=".$db->quote($recid, 'integer')." and type=".$db->quote($type, 'text'));
   532 	return $result;
   532 	return $result;
   533 }
   533 }
   534 
   534 
   535 
   535 
   536 /*
   536 /*
   540 function get_recordtype_from_id($id)
   540 function get_recordtype_from_id($id)
   541 {
   541 {
   542 	global $db;
   542 	global $db;
   543 	if (is_numeric($id))
   543 	if (is_numeric($id))
   544 	{
   544 	{
   545 		$result = $db->query("SELECT type FROM records WHERE id=".$db->quote($id));
   545 		$result = $db->query("SELECT type FROM records WHERE id=".$db->quote($id, 'integer'));
   546 		$r = $result->fetchRow();
   546 		$r = $result->fetchRow();
   547 		return $r["type"];
   547 		return $r["type"];
   548 	}
   548 	}
   549 	else
   549 	else
   550 	{
   550 	{
   559  */
   559  */
   560 function get_name_from_record_id($id)
   560 function get_name_from_record_id($id)
   561 {
   561 {
   562 	global $db;
   562 	global $db;
   563 	if (is_numeric($id)) {
   563 	if (is_numeric($id)) {
   564 		$result = $db->query("SELECT name FROM records WHERE id=".$db->quote($id));
   564 		$result = $db->query("SELECT name FROM records WHERE id=".$db->quote($id, 'integer'));
   565 		$r = $result->fetchRow();
   565 		$r = $result->fetchRow();
   566 		return $r["name"];
   566 		return $r["name"];
   567 	} else {
   567 	} else {
   568 		error(sprintf(ERR_INV_ARG, "get_name_from_record_id"));
   568 		error(sprintf(ERR_INV_ARG, "get_name_from_record_id"));
   569 	}
   569 	}
   574 {
   574 {
   575 	global $db;
   575 	global $db;
   576 
   576 
   577 	if (is_numeric($zid))
   577 	if (is_numeric($zid))
   578 	{
   578 	{
   579 		$result = $db->query("SELECT name FROM domains WHERE id=".$db->quote($zid));
   579 		$result = $db->query("SELECT name FROM domains WHERE id=".$db->quote($zid, 'integer'));
   580 		$rows = $result->numRows() ;
   580 		$rows = $result->numRows() ;
   581 		if ($rows == 1) {
   581 		if ($rows == 1) {
   582  			$r = $result->fetchRow();
   582  			$r = $result->fetchRow();
   583  			return $r["name"];
   583  			return $r["name"];
   584 		} elseif ($rows == "0") {
   584 		} elseif ($rows == "0") {
   609 		$query = "SELECT 	domains.type AS type, 
   609 		$query = "SELECT 	domains.type AS type, 
   610 					domains.name AS name, 
   610 					domains.name AS name, 
   611 					domains.master AS master_ip,
   611 					domains.master AS master_ip,
   612 					count(records.domain_id) AS record_count
   612 					count(records.domain_id) AS record_count
   613 					FROM domains LEFT OUTER JOIN records ON domains.id = records.domain_id 
   613 					FROM domains LEFT OUTER JOIN records ON domains.id = records.domain_id 
   614 					WHERE domains.id = " . $db->quote($zid) . "
   614 					WHERE domains.id = " . $db->quote($zid, 'integer') . "
   615 					GROUP BY domains.id, domains.type, domains.name, domains.master";
   615 					GROUP BY domains.id, domains.type, domains.name, domains.master";
   616 		$result = $db->query($query);
   616 		$result = $db->query($query);
   617 		if (PEAR::isError($result)) { error($result->getMessage()); return false; }
   617 		if (PEAR::isError($result)) { error($result->getMessage()); return false; }
   618 
   618 
   619 		if($result->numRows() != 1) {
   619 		if($result->numRows() != 1) {
   640 function domain_exists($domain)
   640 function domain_exists($domain)
   641 {
   641 {
   642 	global $db;
   642 	global $db;
   643 
   643 
   644 	if (is_valid_hostname_fqdn($domain,0)) {
   644 	if (is_valid_hostname_fqdn($domain,0)) {
   645 		$result = $db->query("SELECT id FROM domains WHERE name=".$db->quote($domain));
   645 		$result = $db->query("SELECT id FROM domains WHERE name=".$db->quote($domain, 'text'));
   646 		if ($result->numRows() == 0) {
   646 		if ($result->numRows() == 0) {
   647 			return false;
   647 			return false;
   648 		} elseif ($result->numRows() >= 1) {
   648 		} elseif ($result->numRows() >= 1) {
   649 			return true;
   649 			return true;
   650 		}
   650 		}
   679 function supermaster_exists($master_ip)
   679 function supermaster_exists($master_ip)
   680 {
   680 {
   681         global $db;
   681         global $db;
   682         if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip))
   682         if (is_valid_ipv4($master_ip) || is_valid_ipv6($master_ip))
   683         {
   683         {
   684                 $result = $db->query("SELECT ip FROM supermasters WHERE ip = ".$db->quote($master_ip));
   684                 $result = $db->query("SELECT ip FROM supermasters WHERE ip = ".$db->quote($master_ip, 'text'));
   685                 if ($result->numRows() == 0)
   685                 if ($result->numRows() == 0)
   686                 {
   686                 {
   687                         return false;
   687                         return false;
   688                 }
   688                 }
   689                 elseif ($result->numRows() >= 1)
   689                 elseif ($result->numRows() >= 1)
   709 	}
   709 	}
   710 	else
   710 	else
   711 	{
   711 	{
   712 		if ($perm == "own") {
   712 		if ($perm == "own") {
   713 			$sql_add = " AND zones.domain_id = domains.id
   713 			$sql_add = " AND zones.domain_id = domains.id
   714 				AND zones.owner = ".$db->quote($userid);
   714 				AND zones.owner = ".$db->quote($userid, 'integer');
   715 		}
   715 		}
   716 		if ($letterstart!='all' && $letterstart!=1) {
   716 		if ($letterstart!='all' && $letterstart!=1) {
   717 			$sql_add .=" AND domains.name LIKE ".$db->quote($letterstart."%")." ";
   717 			$sql_add .=" AND domains.name LIKE ".$db->quote($db->quote($letterstart, 'text', false, true)."%", 'text')." ";
   718 		} elseif ($letterstart==1) {
   718 		} elseif ($letterstart==1) {
   719 			$sql_add .=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'";
   719 			$sql_add .=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'";
   720 		}
   720 		}
   721 	}
   721 	}
   722 	
   722 	
   759 	} 
   759 	} 
   760 	else 
   760 	else 
   761 	{
   761 	{
   762 		if ($perm == "own") {
   762 		if ($perm == "own") {
   763 			$sql_add = " AND zones.domain_id = domains.id
   763 			$sql_add = " AND zones.domain_id = domains.id
   764 					AND zones.owner = ".$db->quote($_SESSION['userid']);
   764 					AND zones.owner = ".$db->quote($_SESSION['userid'], 'integer');
   765 			$fromTable .= ',zones';
   765 			$fromTable .= ',zones';
   766 		}
   766 		}
   767 		if ($letterstart!='all' && $letterstart!=1) {
   767 		if ($letterstart!='all' && $letterstart!=1) {
   768 			$sql_add .=" AND domains.name LIKE ".$db->quote($letterstart."%")." ";
   768 			$sql_add .=" AND domains.name LIKE ".$db->quote($db->quote($letterstart, 'text', false, true)."%", 'text')." ";
   769 		} elseif ($letterstart==1) {
   769 		} elseif ($letterstart==1) {
   770 			$sql_add .=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'";
   770 			$sql_add .=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'";
   771 		}
   771 		}
   772 
   772 
   773 		$sqlq = "SELECT COUNT(distinct domains.id) AS count_zones 
   773 		$sqlq = "SELECT COUNT(distinct domains.id) AS count_zones 
   781 
   781 
   782 function zone_count_for_uid($uid) {
   782 function zone_count_for_uid($uid) {
   783 	global $db;
   783 	global $db;
   784 	$query = "SELECT COUNT(domain_id) 
   784 	$query = "SELECT COUNT(domain_id) 
   785 			FROM zones 
   785 			FROM zones 
   786 			WHERE owner = " . $db->quote($uid) . " 
   786 			WHERE owner = " . $db->quote($uid, 'integer') . " 
   787 			ORDER BY domain_id";
   787 			ORDER BY domain_id";
   788 	$zone_count = $db->queryOne($query);
   788 	$zone_count = $db->queryOne($query);
   789 	return $zone_count;
   789 	return $zone_count;
   790 }
   790 }
   791 
   791 
   798 function get_record_from_id($id)
   798 function get_record_from_id($id)
   799 {
   799 {
   800 	global $db;
   800 	global $db;
   801 	if (is_numeric($id))
   801 	if (is_numeric($id))
   802 	{
   802 	{
   803 		$result = $db->query("SELECT id, domain_id, name, type, content, ttl, prio, change_date FROM records WHERE id=".$db->quote($id));
   803 		$result = $db->query("SELECT id, domain_id, name, type, content, ttl, prio, change_date FROM records WHERE id=".$db->quote($id, 'integer'));
   804 		if($result->numRows() == 0)
   804 		if($result->numRows() == 0)
   805 		{
   805 		{
   806 			return -1;
   806 			return -1;
   807 		}
   807 		}
   808 		elseif ($result->numRows() == 1)
   808 		elseif ($result->numRows() == 1)
   842 	if (is_numeric($id)) {
   842 	if (is_numeric($id)) {
   843 		if ((isset($_SESSION[$id."_ispartial"])) && ($_SESSION[$id."_ispartial"] == 1)) {
   843 		if ((isset($_SESSION[$id."_ispartial"])) && ($_SESSION[$id."_ispartial"] == 1)) {
   844 			$db->setLimit($rowamount, $rowstart);
   844 			$db->setLimit($rowamount, $rowstart);
   845 			$result = $db->query("SELECT record_owners.record_id as id
   845 			$result = $db->query("SELECT record_owners.record_id as id
   846 					FROM record_owners,domains,records
   846 					FROM record_owners,domains,records
   847 					WHERE record_owners.user_id = " . $db->quote($_SESSION["userid"]) . "
   847 					WHERE record_owners.user_id = " . $db->quote($_SESSION["userid"], 'integer') . "
   848 					AND record_owners.record_id = records.id
   848 					AND record_owners.record_id = records.id
   849 					AND records.domain_id = " . $db->quote($id) . "
   849 					AND records.domain_id = " . $db->quote($id, 'integer') . "
   850 					GROUP BY record_owners.record_id");
   850 					GROUP BY record_owners.record_id");
   851 
   851 
   852 			$ret = array();
   852 			$ret = array();
   853 			if($result->numRows() == 0) {
   853 			if($result->numRows() == 0) {
   854 				return -1;
   854 				return -1;
   864 				return $ret;
   864 				return $ret;
   865 			}
   865 			}
   866 
   866 
   867 		} else {
   867 		} else {
   868 			$db->setLimit($rowamount, $rowstart);
   868 			$db->setLimit($rowamount, $rowstart);
   869 			$result = $db->query("SELECT id FROM records WHERE domain_id=".$db->quote($id));
   869 			$result = $db->query("SELECT id FROM records WHERE domain_id=".$db->quote($id, 'integer'));
   870 			$ret = array();
   870 			$ret = array();
   871 			if($result->numRows() == 0)
   871 			if($result->numRows() == 0)
   872 			{
   872 			{
   873 				return -1;
   873 				return -1;
   874 			}
   874 			}
   894 }
   894 }
   895 
   895 
   896 
   896 
   897 function get_users_from_domain_id($id) {
   897 function get_users_from_domain_id($id) {
   898 	global $db;
   898 	global $db;
   899 	$sqlq = "SELECT owner FROM zones WHERE domain_id =" .$db->quote($id);
   899 	$sqlq = "SELECT owner FROM zones WHERE domain_id =" .$db->quote($id, 'integer');
   900 	$id_owners = $db->query($sqlq);
   900 	$id_owners = $db->query($sqlq);
   901 	if ($id_owners->numRows() == 0) {
   901 	if ($id_owners->numRows() == 0) {
   902 		return -1;
   902 		return -1;
   903 	} else {
   903 	} else {
   904 		while ($r = $id_owners->fetchRow()) {
   904 		while ($r = $id_owners->fetchRow()) {
   934 	else { $perm_content_edit = "none" ; }
   934 	else { $perm_content_edit = "none" ; }
   935 
   935 
   936 	// Search for matching domains
   936 	// Search for matching domains
   937 	if ($perm == "own") {
   937 	if ($perm == "own") {
   938 		$sql_add_from = ", zones ";
   938 		$sql_add_from = ", zones ";
   939 		$sql_add_where = " AND zones.domain_id = domains.id AND zones.owner = " . $db->quote($_SESSION['userid']);
   939 		$sql_add_where = " AND zones.domain_id = domains.id AND zones.owner = " . $db->quote($_SESSION['userid'], 'integer');
   940 	}
   940 	}
   941 	
   941 	
   942 	$query = "SELECT 
   942 	$query = "SELECT 
   943 			domains.id AS zid,
   943 			domains.id AS zid,
   944 			domains.name AS name,
   944 			domains.name AS name,
   945 			domains.type AS type,
   945 			domains.type AS type,
   946 			domains.master AS master
   946 			domains.master AS master
   947 			FROM domains" . $sql_add_from . "
   947 			FROM domains" . $sql_add_from . "
   948 			WHERE domains.name LIKE " . $db->quote($holy_grail)
   948 			WHERE domains.name LIKE " . $db->quote($holy_grail, 'text')
   949 			. $sql_add_where ;
   949 			. $sql_add_where ;
   950 	
   950 	
   951 	$response = $db->query($query);
   951 	$response = $db->query($query);
   952 	if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   952 	if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   953 
   953 
   961 
   961 
   962 	// Search for matching records
   962 	// Search for matching records
   963 
   963 
   964 	if ($perm == "own") {
   964 	if ($perm == "own") {
   965 		$sql_add_from = ", zones ";
   965 		$sql_add_from = ", zones ";
   966 		$sql_add_where = " AND zones.domain_id = records.domain_id AND zones.owner = " . $db->quote($_SESSION['userid']);
   966 		$sql_add_where = " AND zones.domain_id = records.domain_id AND zones.owner = " . $db->quote($_SESSION['userid'], 'integer');
   967 	}
   967 	}
   968 
   968 
   969 	$query = "SELECT
   969 	$query = "SELECT
   970 			records.id AS rid,
   970 			records.id AS rid,
   971 			records.name AS name,
   971 			records.name AS name,
   973 			records.content AS content,
   973 			records.content AS content,
   974 			records.ttl AS ttl,
   974 			records.ttl AS ttl,
   975 			records.prio AS prio,
   975 			records.prio AS prio,
   976 			records.domain_id AS zid
   976 			records.domain_id AS zid
   977 			FROM records" . $sql_add_from . "
   977 			FROM records" . $sql_add_from . "
   978 			WHERE (records.name LIKE " . $db->quote($holy_grail) . " OR records.content LIKE " . $db->quote($holy_grail) . ")"
   978 			WHERE (records.name LIKE " . $db->quote($holy_grail, 'text') . " OR records.content LIKE " . $db->quote($holy_grail, 'text') . ")"
   979 			. $sql_add_where ;
   979 			. $sql_add_where ;
   980 
   980 
   981 	$response = $db->query($query);
   981 	$response = $db->query($query);
   982 	if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   982 	if (PEAR::isError($response)) { error($response->getMessage()); return false; }
   983 
   983 
   995 }
   995 }
   996 
   996 
   997 function get_domain_type($id) {
   997 function get_domain_type($id) {
   998 	global $db;
   998 	global $db;
   999         if (is_numeric($id)) {
   999         if (is_numeric($id)) {
  1000 		$type = $db->queryOne("SELECT type FROM domains WHERE id = ".$db->quote($id));
  1000 		$type = $db->queryOne("SELECT type FROM domains WHERE id = ".$db->quote($id, 'integer'));
  1001 		if ($type == "") {
  1001 		if ($type == "") {
  1002 			$type = "NATIVE";
  1002 			$type = "NATIVE";
  1003 		}
  1003 		}
  1004 		return $type;
  1004 		return $type;
  1005         } else {
  1005         } else {
  1008 }
  1008 }
  1009 
  1009 
  1010 function get_domain_slave_master($id){
  1010 function get_domain_slave_master($id){
  1011 	global $db;
  1011 	global $db;
  1012         if (is_numeric($id)) {
  1012         if (is_numeric($id)) {
  1013 		$slave_master = $db->queryOne("SELECT master FROM domains WHERE type = 'SLAVE' and id = ".$db->quote($id));
  1013 		$slave_master = $db->queryOne("SELECT master FROM domains WHERE type = 'SLAVE' and id = ".$db->quote($id, 'integer'));
  1014 		return $slave_master;
  1014 		return $slave_master;
  1015         } else {
  1015         } else {
  1016                 error(sprintf(ERR_INV_ARG, "get_domain_slave_master", "no or no valid zoneid given"));
  1016                 error(sprintf(ERR_INV_ARG, "get_domain_slave_master", "no or no valid zoneid given"));
  1017         }
  1017         }
  1018 }
  1018 }
  1026 		// It is not really neccesary to clear the field that contains the IP address 
  1026 		// It is not really neccesary to clear the field that contains the IP address 
  1027 		// of the master if the type changes from slave to something else. PowerDNS will
  1027 		// of the master if the type changes from slave to something else. PowerDNS will
  1028 		// ignore the field if the type isn't something else then slave. But then again,
  1028 		// ignore the field if the type isn't something else then slave. But then again,
  1029 		// it's much clearer this way.
  1029 		// it's much clearer this way.
  1030 		if ($type != "SLAVE") {
  1030 		if ($type != "SLAVE") {
  1031 			$add = ", master=''";
  1031 			$add = ", master=".$db->quote('', 'text');
  1032 		}
  1032 		}
  1033 		$result = $db->query("UPDATE domains SET type = " . $db->quote($type) . $add . " WHERE id = ".$db->quote($id));
  1033 		$result = $db->query("UPDATE domains SET type = " . $db->quote($type, 'text') . $add . " WHERE id = ".$db->quote($id, 'integer'));
  1034 	} else {
  1034 	} else {
  1035                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1035                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1036         }
  1036         }
  1037 }
  1037 }
  1038 
  1038 
  1039 function change_zone_slave_master($zone_id, $ip_slave_master) {
  1039 function change_zone_slave_master($zone_id, $ip_slave_master) {
  1040 	global $db;
  1040 	global $db;
  1041         if (is_numeric($zone_id)) {
  1041         if (is_numeric($zone_id)) {
  1042        		if (is_valid_ipv4($ip_slave_master) || is_valid_ipv6($ip_slave_master)) {
  1042        		if (is_valid_ipv4($ip_slave_master) || is_valid_ipv6($ip_slave_master)) {
  1043 			$result = $db->query("UPDATE domains SET master = " .$db->quote($ip_slave_master). " WHERE id = ".$db->quote($zone_id));
  1043 			$result = $db->query("UPDATE domains SET master = " .$db->quote($ip_slave_master, 'text'). " WHERE id = ".$db->quote($zone_id, 'integer'));
  1044 		} else {
  1044 		} else {
  1045 			error(sprintf(ERR_INV_ARGC, "change_domain_ip_slave_master", "This is not a valid IPv4 or IPv6 address: $ip_slave_master"));
  1045 			error(sprintf(ERR_INV_ARGC, "change_domain_ip_slave_master", "This is not a valid IPv4 or IPv6 address: $ip_slave_master"));
  1046 		}
  1046 		}
  1047 	} else {
  1047 	} else {
  1048                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1048                 error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given"));
  1050 }
  1050 }
  1051 
  1051 
  1052 function get_serial_by_zid($zid) {
  1052 function get_serial_by_zid($zid) {
  1053 	global $db;
  1053 	global $db;
  1054 	if (is_numeric($zid)) {
  1054 	if (is_numeric($zid)) {
  1055 		$query = "SELECT content FROM records where TYPE = 'SOA' and domain_id = " . $db->quote($zid);
  1055 		$query = "SELECT content FROM records where TYPE = ".$db->quote('SOA', 'text')." and domain_id = " . $db->quote($zid, 'integer');
  1056 		$rr_soa = $db->queryOne($query);
  1056 		$rr_soa = $db->queryOne($query);
  1057 		if (PEAR::isError($rr_soa)) { error($rr_soa->getMessage()); return false; }
  1057 		if (PEAR::isError($rr_soa)) { error($rr_soa->getMessage()); return false; }
  1058 		$rr_soa_fields = explode(" ", $rr_soa);
  1058 		$rr_soa_fields = explode(" ", $rr_soa);
  1059 	} else {
  1059 	} else {
  1060 		error(sprintf(ERR_INV_ARGC, "get_serial_by_zid", "id must be a number"));
  1060 		error(sprintf(ERR_INV_ARGC, "get_serial_by_zid", "id must be a number"));