1
|
1 |
<? |
|
2 |
|
47
|
3 |
/* PowerAdmin, a friendly web-based admin tool for PowerDNS. |
|
4 |
* See <https://rejo.zenger.nl/poweradmin> for more details. |
|
5 |
* |
|
6 |
* Copyright 2007, 2008 Rejo Zenger <rejo@zenger.nl> |
|
7 |
* |
|
8 |
* This program is free software: you can redistribute it and/or modify |
|
9 |
* it under the terms of the GNU General Public License as published by |
|
10 |
* the Free Software Foundation, either version 3 of the License, or |
|
11 |
* (at your option) any later version. |
|
12 |
* |
|
13 |
* This program is distributed in the hope that it will be useful, |
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 |
* GNU General Public License for more details. |
|
17 |
* |
|
18 |
* You should have received a copy of the GNU General Public License |
|
19 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 |
*/ |
1
|
21 |
|
|
22 |
function update_soa_serial($domain_id) |
|
23 |
{ |
|
24 |
global $db; |
|
25 |
/* |
|
26 |
* THIS CODE ISNT TESTED THROUGH MUCH YET! |
|
27 |
* !!!!!!! BETACODE !!!!!!!!!! |
|
28 |
* Code committed by DeViCeD, Thanks a lot! |
|
29 |
* Heavily hax0red by Trancer/azurazu |
|
30 |
* |
|
31 |
* First we have to check, wheather current searial number |
|
32 |
* was already updated on the other nameservers. |
|
33 |
* If field 'notified_serial' is NULL, then I guess domain is |
|
34 |
* NATIVE and we don't have any secondary nameservers for this domain. |
|
35 |
* NOTICE: Serial number *will* be RFC1912 compilant after update |
|
36 |
* NOTICE: This function will allow only 100 DNS zone transfers ;-) |
|
37 |
* YYYYMMDDnn |
|
38 |
*/ |
|
39 |
|
55
|
40 |
$sqlq = "SELECT notified_serial FROM domains WHERE id = '".$domain_id."'"; |
8
|
41 |
$notified_serial = $db->queryOne($sqlq); |
1
|
42 |
|
55
|
43 |
$sqlq = "SELECT content FROM records WHERE type = 'SOA' AND domain_id = '".$domain_id."'"; |
8
|
44 |
$content = $db->queryOne($sqlq); |
1
|
45 |
$need_to_update = false; |
|
46 |
|
|
47 |
// Getting the serial field. |
|
48 |
$soa = explode(" ", $content); |
|
49 |
|
|
50 |
if(empty($notified_serial)) |
|
51 |
{ |
|
52 |
// Ok native replication, so we have to update. |
|
53 |
$need_to_update = true; |
|
54 |
} |
|
55 |
elseif($notified_serial >= $soa[2]) |
|
56 |
{ |
|
57 |
$need_to_update = true; |
|
58 |
} |
|
59 |
elseif(strlen($soa[2]) != 10) |
|
60 |
{ |
|
61 |
$need_to_update = true; |
|
62 |
} |
|
63 |
else |
|
64 |
{ |
|
65 |
$need_to_update = false; |
|
66 |
} |
|
67 |
if($need_to_update) |
|
68 |
{ |
|
69 |
// Ok so we have to update it seems. |
|
70 |
$current_serial = $soa[2]; |
|
71 |
|
|
72 |
/* |
|
73 |
* What we need here (for RFC1912) is YEAR, MONTH and DAY |
|
74 |
* so let's get it ... |
|
75 |
*/ |
|
76 |
$new_serial = date('Ymd'); // we will add revision number later |
|
77 |
|
|
78 |
if(strncmp($new_serial, $current_serial, 8) === 0) |
|
79 |
{ |
|
80 |
/* |
|
81 |
* Ok, so we already made updates tonight |
|
82 |
* let's just increase the revision number |
|
83 |
*/ |
|
84 |
$revision_number = (int) substr($current_serial, -2); |
|
85 |
if ($revision_number == 99) return false; // ok, we cannot update anymore tonight |
|
86 |
++$revision_number; |
|
87 |
// here it is ... same date, new revision |
|
88 |
$new_serial .= str_pad($revision_number, 2, "0", STR_PAD_LEFT); |
|
89 |
} |
|
90 |
else |
|
91 |
{ |
|
92 |
/* |
|
93 |
* Current serial is not RFC1912 compilant, so let's make a new one |
|
94 |
*/ |
|
95 |
$new_serial .= '00'; |
|
96 |
} |
|
97 |
$soa[2] = $new_serial; // change serial in SOA array |
|
98 |
$new_soa = ""; |
|
99 |
// build new soa and update SQL after that |
|
100 |
for ($i = 0; $i < count($soa); $i++) |
|
101 |
{ |
|
102 |
$new_soa .= $soa[$i] . " "; |
|
103 |
} |
55
|
104 |
$sqlq = "UPDATE records SET content = '".$new_soa."' WHERE domain_id = '".$domain_id."' AND type = 'SOA'"; |
1
|
105 |
$db->Query($sqlq); |
|
106 |
return true; |
|
107 |
} |
|
108 |
} |
|
109 |
|
|
110 |
/* |
|
111 |
* Edit a record. |
|
112 |
* This function validates it if correct it inserts it into the database. |
|
113 |
* return values: true if succesful. |
|
114 |
*/ |
|
115 |
function edit_record($recordid, $zoneid, $name, $type, $content, $ttl, $prio) |
|
116 |
{ |
|
117 |
global $db; |
|
118 |
if($content == "") |
|
119 |
{ |
|
120 |
error(ERR_RECORD_EMPTY_CONTENT); |
|
121 |
} |
|
122 |
// Edits the given record (validates specific stuff first) |
|
123 |
if (!xs(recid_to_domid($recordid))) |
|
124 |
{ |
|
125 |
error(ERR_RECORD_ACCESS_DENIED); |
|
126 |
} |
|
127 |
if (is_numeric($zoneid)) |
|
128 |
{ |
19
|
129 |
validate_input($zoneid, $type, $content, $name, $prio, $ttl); |
1
|
130 |
$change = time(); |
|
131 |
$db->query("UPDATE records set name='$name', type='$type', content='$content', ttl='$ttl', prio='$prio', change_date='$change' WHERE id=$recordid"); |
|
132 |
|
|
133 |
/* |
|
134 |
* Added by DeViCeD - Update SOA Serial number |
|
135 |
* There should be more checks |
|
136 |
*/ |
|
137 |
if ($type != 'SOA') |
|
138 |
{ |
|
139 |
update_soa_serial($zoneid); |
|
140 |
} |
|
141 |
return true; |
|
142 |
} |
|
143 |
else |
|
144 |
{ |
|
145 |
error(sprintf(ERR_INV_ARGC, "edit_record", "no zoneid given")); |
|
146 |
} |
|
147 |
|
|
148 |
} |
|
149 |
|
|
150 |
|
56
|
151 |
function add_record_owner($zoneid,$userid,$recordid) |
|
152 |
{ |
|
153 |
global $db; |
|
154 |
if (!xs($zoneid)) |
|
155 |
{ |
|
156 |
error(ERR_RECORD_ACCESS_DENIED); |
|
157 |
} |
|
158 |
if (is_numeric($zoneid) || is_numeric($userid) || is_numeric($recordid)) |
|
159 |
{ |
|
160 |
$db->query("INSERT INTO record_owners (user_id, record_id) VALUES ('".$userid."','".$recordid."')"); |
|
161 |
return true; |
|
162 |
} |
|
163 |
else |
|
164 |
{ |
|
165 |
error(sprintf(ERR_INV_ARGC, "add_record_owner", "at least one of the arguments is not numeric")); |
|
166 |
} |
|
167 |
} |
|
168 |
|
57
|
169 |
function delete_record_owner($zoneid,$rowid,$recordid) |
|
170 |
{ |
|
171 |
global $db; |
|
172 |
if (!xs($zoneid)) |
|
173 |
{ |
|
174 |
error(ERR_RECORD_ACCESS_DENIED); |
|
175 |
} |
|
176 |
if (is_numeric($zoneid) || is_numeric($rowid) || is_numeric($recordid)) |
|
177 |
{ |
|
178 |
$db->query("DELETE FROM record_owners WHERE id='".$rowid."' AND record_id='".$recordid."'"); |
|
179 |
return true; |
|
180 |
} |
|
181 |
else |
|
182 |
{ |
|
183 |
error(sprintf(ERR_INV_ARGC, "delete_record_owner", "at least one of the arguments is not numeric")); |
|
184 |
} |
|
185 |
} |
|
186 |
|
1
|
187 |
/* |
|
188 |
* Adds a record. |
|
189 |
* This function validates it if correct it inserts it into the database. |
|
190 |
* return values: true if succesful. |
|
191 |
*/ |
|
192 |
function add_record($zoneid, $name, $type, $content, $ttl, $prio) |
|
193 |
{ |
|
194 |
|
|
195 |
global $db; |
|
196 |
if (!xs($zoneid)) |
|
197 |
{ |
|
198 |
error(ERR_RECORD_ACCESS_DENIED); |
|
199 |
} |
|
200 |
if (is_numeric($zoneid)) |
|
201 |
{ |
8
|
202 |
// Check the user input. |
|
203 |
validate_input($zoneid, $type, $content, $name, $prio, $ttl); |
1
|
204 |
|
8
|
205 |
// Generate new timestamp for the daemon |
1
|
206 |
$change = time(); |
8
|
207 |
|
1
|
208 |
// Execute query. |
8
|
209 |
$db->query("INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES ($zoneid, '$name', '$type', '$content', $ttl, '$prio', $change)"); |
1
|
210 |
if ($type != 'SOA') |
|
211 |
{ |
|
212 |
update_soa_serial($zoneid); |
|
213 |
} |
|
214 |
return true; |
|
215 |
} |
|
216 |
else |
|
217 |
{ |
|
218 |
error(sprintf(ERR_INV_ARG, "add_record")); |
|
219 |
} |
|
220 |
} |
|
221 |
|
|
222 |
|
13
|
223 |
function add_supermaster($master_ip, $ns_name, $account) |
|
224 |
{ |
|
225 |
global $db; |
|
226 |
if (!is_valid_ip($master_ip) && !is_valid_ip6($master_ip)) |
|
227 |
{ |
|
228 |
error(sprintf(ERR_INV_ARGC, "add_supermaster", "No or no valid ipv4 or ipv6 address given.")); |
|
229 |
} |
|
230 |
if (!is_valid_hostname($ns_name)) |
|
231 |
{ |
|
232 |
error(ERR_DNS_HOSTNAME); |
|
233 |
} |
|
234 |
if (!validate_account($account)) |
|
235 |
{ |
|
236 |
error(sprintf(ERR_INV_ARGC, "add_supermaster", "given account name is invalid (alpha chars only)")); |
|
237 |
} |
|
238 |
if (supermaster_exists($master_ip)) |
|
239 |
{ |
|
240 |
error(sprintf(ERR_INV_ARGC, "add_supermaster", "supermaster already exists")); |
|
241 |
} |
|
242 |
else |
|
243 |
{ |
|
244 |
$db->query("INSERT INTO supermasters VALUES ('$master_ip', '$ns_name', '$account')"); |
|
245 |
return true; |
|
246 |
} |
|
247 |
} |
|
248 |
|
|
249 |
function delete_supermaster($master_ip) |
|
250 |
{ |
|
251 |
global $db; |
|
252 |
if (!level(5)) |
|
253 |
{ |
|
254 |
error(ERR_LEVEL_5); |
|
255 |
} |
|
256 |
if (is_valid_ip($master_ip) || is_valid_ip6($master_ip)) |
|
257 |
{ |
|
258 |
$db->query("DELETE FROM supermasters WHERE ip = '$master_ip'"); |
|
259 |
return true; |
|
260 |
} |
|
261 |
else |
|
262 |
{ |
|
263 |
error(sprintf(ERR_INV_ARGC, "delete_supermaster", "No or no valid ipv4 or ipv6 address given.")); |
|
264 |
} |
|
265 |
} |
|
266 |
|
|
267 |
function get_supermaster_info_from_ip($master_ip) |
|
268 |
{ |
|
269 |
global $db; |
|
270 |
if (!level(5)) |
|
271 |
{ |
|
272 |
error(ERR_LEVEL_5); |
|
273 |
} |
|
274 |
if (is_valid_ip($master_ip) || is_valid_ip6($master_ip)) |
|
275 |
{ |
|
276 |
$result = $db->queryRow("SELECT ip,nameserver,account FROM supermasters WHERE ip = '$master_ip'"); |
|
277 |
|
|
278 |
$ret = array( |
|
279 |
"master_ip" => $result["ip"], |
|
280 |
"ns_name" => $result["nameserver"], |
|
281 |
"account" => $result["account"] |
|
282 |
); |
|
283 |
|
|
284 |
return $ret; |
|
285 |
} |
|
286 |
else |
|
287 |
{ |
|
288 |
error(sprintf(ERR_INV_ARGC, "get_supermaster_info_from_ip", "No or no valid ipv4 or ipv6 address given.")); |
|
289 |
} |
|
290 |
} |
|
291 |
|
|
292 |
|
1
|
293 |
/* |
|
294 |
* Delete a record by a given id. |
|
295 |
* return values: true, this function is always succesful. |
|
296 |
*/ |
|
297 |
function delete_record($id) |
|
298 |
{ |
|
299 |
global $db; |
|
300 |
|
|
301 |
// Check if the user has access. |
|
302 |
if (!xs(recid_to_domid($id))) |
|
303 |
{ |
|
304 |
error(ERR_RECORD_ACCESS_DENIED); |
|
305 |
} |
|
306 |
|
|
307 |
// Retrieve the type of record to see if we can actually remove it. |
|
308 |
$recordtype = get_recordtype_from_id($id); |
|
309 |
|
|
310 |
// If the record type is NS and the user tries to delete it while ALLOW_NS_EDIT is set to 0 |
|
311 |
// OR |
|
312 |
// check if the name of the record isnt the domain name (if so it should delete all records) |
|
313 |
// OR |
|
314 |
// check if we are dealing with a SOA field (same story as NS) |
|
315 |
if (($recordtype == "NS" && $GLOBALS["ALLOW_NS_EDIT"] != 1 && (get_name_from_record_id($id) == get_domain_name_from_id(recid_to_domid($id)))) || ($recordtype == "SOA" && $GLOBALS["ALLOW_SOA_EDIT"] != 1)) |
|
316 |
{ |
|
317 |
error(sprintf(ERR_RECORD_DELETE_TYPE_DENIED, $recordtype)); |
|
318 |
|
|
319 |
} |
|
320 |
if (is_numeric($id)) |
|
321 |
{ |
|
322 |
$did = recid_to_domid($id); |
55
|
323 |
$db->query('DELETE FROM records WHERE id=' . $id ); |
1
|
324 |
if ($type != 'SOA') |
|
325 |
{ |
|
326 |
update_soa_serial($did); |
|
327 |
} |
|
328 |
// $id doesnt exist in database anymore so its deleted or just not there which means "true" |
|
329 |
return true; |
|
330 |
} |
|
331 |
else |
|
332 |
{ |
|
333 |
error(sprintf(ERR_INV_ARG, "delete_record")); |
|
334 |
} |
|
335 |
} |
|
336 |
|
|
337 |
|
|
338 |
/* |
|
339 |
* Add a domain to the database. |
|
340 |
* A domain is name obligatory, so is an owner. |
|
341 |
* return values: true when succesful. |
|
342 |
* Empty means templates dont have to be applied. |
|
343 |
* -------------------------------------------------------------------------- |
|
344 |
* This functions eats a template and by that it inserts various records. |
|
345 |
* first we start checking if something in an arpa record |
|
346 |
* remember to request nextID's from the database to be able to insert record. |
|
347 |
* if anything is invalid the function will error |
|
348 |
*/ |
13
|
349 |
function add_domain($domain, $owner, $webip, $mailip, $empty, $type, $slave_master) |
1
|
350 |
{ |
|
351 |
|
|
352 |
global $db; |
|
353 |
|
|
354 |
if (!level(5)) |
|
355 |
{ |
|
356 |
error(ERR_LEVEL_5); |
|
357 |
} |
|
358 |
|
|
359 |
// If domain, owner and mailip are given |
|
360 |
// OR |
|
361 |
// empty is given and owner and domain |
|
362 |
// OR |
|
363 |
// the domain is an arpa record and owner is given |
13
|
364 |
// OR |
|
365 |
// the type is slave, domain, owner and slave_master are given |
1
|
366 |
// THAN |
|
367 |
// Continue this function |
13
|
368 |
if (($domain && $owner && $webip && $mailip) || ($empty && $owner && $domain) || (eregi('in-addr.arpa', $domain) && $owner) || $type=="SLAVE" && $domain && $owner && $slave_master) |
1
|
369 |
{ |
8
|
370 |
// First insert zone into domain table |
|
371 |
$db->query("INSERT INTO domains (name, type) VALUES ('$domain', '$type')"); |
1
|
372 |
|
8
|
373 |
// Determine id of insert zone (in other words, find domain_id) |
|
374 |
$iddomain = $db->lastInsertId('domains', 'id'); |
|
375 |
if (PEAR::isError($iddomain)) { |
|
376 |
die($id->getMessage()); |
|
377 |
} |
1
|
378 |
|
8
|
379 |
// Second, insert into zones tables |
|
380 |
$db->query("INSERT INTO zones (domain_id, owner) VALUES ('$iddomain', $owner)"); |
1
|
381 |
|
13
|
382 |
if ($type == "SLAVE") |
1
|
383 |
{ |
13
|
384 |
$db->query("UPDATE domains SET master = '$slave_master' WHERE id = '$iddomain';"); |
|
385 |
|
|
386 |
// Done |
|
387 |
return true; |
|
388 |
} |
|
389 |
else |
|
390 |
{ |
|
391 |
// Generate new timestamp. We need this one anyhow. |
|
392 |
$now = time(); |
1
|
393 |
|
13
|
394 |
if ($empty && $iddomain) |
|
395 |
{ |
|
396 |
// If we come into this if statement we dont want to apply templates. |
|
397 |
// Retrieve configuration settings. |
|
398 |
$ns1 = $GLOBALS["NS1"]; |
|
399 |
$hm = $GLOBALS["HOSTMASTER"]; |
|
400 |
$ttl = $GLOBALS["DEFAULT_TTL"]; |
1
|
401 |
|
13
|
402 |
// Build and execute query |
55
|
403 |
$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$domain', '$ns1 $hm 1', 'SOA', $ttl, 0, '$now')"; |
13
|
404 |
$db->query($sql); |
1
|
405 |
|
13
|
406 |
// Done |
|
407 |
return true; |
|
408 |
} |
|
409 |
elseif ($iddomain) |
1
|
410 |
{ |
13
|
411 |
// If we are here we want to apply templates. |
|
412 |
global $template; |
|
413 |
|
|
414 |
// Iterate over the template and apply it for each field. |
|
415 |
foreach ($template as $r) |
1
|
416 |
{ |
13
|
417 |
// Same type of if statement as previous. |
|
418 |
if ((eregi('in-addr.arpa', $domain) && ($r["type"] == "NS" || $r["type"] == "SOA")) || (!eregi('in-addr.arpa', $domain))) |
1
|
419 |
{ |
13
|
420 |
// Parse the template. |
|
421 |
$name = parse_template_value($r["name"], $domain, $webip, $mailip); |
|
422 |
$type = $r["type"]; |
|
423 |
$content = parse_template_value($r["content"], $domain, $webip, $mailip); |
|
424 |
$ttl = $r["ttl"]; |
55
|
425 |
$prio = intval($r["prio"]); |
1
|
426 |
|
13
|
427 |
// If no ttl is given, use the default. |
|
428 |
if (!$ttl) |
|
429 |
{ |
|
430 |
$ttl = $GLOBALS["DEFAULT_TTL"]; |
|
431 |
} |
|
432 |
|
|
433 |
$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES ('$iddomain', '$name','$content','$type','$ttl','$prio','$now')"; |
|
434 |
$db->query($sql); |
|
435 |
} |
1
|
436 |
} |
13
|
437 |
// All done. |
|
438 |
return true; |
|
439 |
} |
|
440 |
else |
|
441 |
{ |
|
442 |
error(sprintf(ERR_INV_ARGC, "add_domain", "could not create zone")); |
|
443 |
} |
|
444 |
} |
1
|
445 |
} |
|
446 |
else |
|
447 |
{ |
|
448 |
error(sprintf(ERR_INV_ARG, "add_domain")); |
|
449 |
} |
|
450 |
} |
|
451 |
|
|
452 |
|
|
453 |
/* |
|
454 |
* Deletes a domain by a given id. |
|
455 |
* Function always succeeds. If the field is not found in the database, thats what we want anyway. |
|
456 |
*/ |
|
457 |
function delete_domain($id) |
|
458 |
{ |
|
459 |
global $db; |
|
460 |
|
|
461 |
if (!level(5)) |
|
462 |
{ |
|
463 |
error(ERR_LEVEL_5); |
|
464 |
} |
|
465 |
|
|
466 |
// See if the ID is numeric. |
|
467 |
if (is_numeric($id)) |
|
468 |
{ |
|
469 |
$db->query("DELETE FROM zones WHERE domain_id=$id"); |
|
470 |
$db->query("DELETE FROM domains WHERE id=$id"); |
|
471 |
$db->query("DELETE FROM records WHERE domain_id=$id"); |
|
472 |
// Nothing in the database. If the delete deleted 0 records it means the id is just not there. |
|
473 |
// therefore the is no need to check the affectedRows values. |
|
474 |
return true; |
|
475 |
} |
|
476 |
else |
|
477 |
{ |
|
478 |
error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number")); |
|
479 |
} |
|
480 |
} |
|
481 |
|
|
482 |
|
|
483 |
/* |
|
484 |
* Gets the id of the domain by a given record id. |
|
485 |
* return values: the domain id that was requested. |
|
486 |
*/ |
|
487 |
function recid_to_domid($id) |
|
488 |
{ |
|
489 |
global $db; |
|
490 |
if (is_numeric($id)) |
|
491 |
{ |
|
492 |
$result = $db->query("SELECT domain_id FROM records WHERE id=$id"); |
|
493 |
$r = $result->fetchRow(); |
|
494 |
return $r["domain_id"]; |
|
495 |
} |
|
496 |
else |
|
497 |
{ |
|
498 |
error(sprintf(ERR_INV_ARGC, "recid_to_domid", "id must be a number")); |
|
499 |
} |
|
500 |
} |
|
501 |
|
|
502 |
|
|
503 |
/* |
|
504 |
* Sorts a zone by records. |
|
505 |
* return values: the sorted zone. |
|
506 |
*/ |
|
507 |
function sort_zone($records) |
|
508 |
{ |
|
509 |
$ar_so = array(); |
|
510 |
$ar_ns = array(); |
|
511 |
$ar_mx = array(); |
|
512 |
$ar_mb = array(); |
|
513 |
$ar_ur = array(); |
|
514 |
$ar_ov = array(); |
|
515 |
foreach ($records as $c) |
|
516 |
{ |
|
517 |
switch(strtoupper($c['type'])) |
|
518 |
{ |
|
519 |
case "SOA": |
|
520 |
$ar_so[] = $c; |
|
521 |
break; |
|
522 |
case "NS": |
|
523 |
$ar_ns[] = $c; |
|
524 |
break; |
|
525 |
case "MX": |
|
526 |
$ar_mx[] = $c; |
|
527 |
break; |
|
528 |
case "MBOXFW": |
|
529 |
$ar_mb[] = $c; |
|
530 |
break; |
|
531 |
case "URL": |
|
532 |
$ar_ur[] = $c; |
|
533 |
break; |
|
534 |
default: |
|
535 |
$ar_ov[] = $c; |
|
536 |
break; |
|
537 |
} |
|
538 |
} |
|
539 |
|
|
540 |
$res = array_merge($ar_so, $ar_ns, $ar_mx, $ar_mb, $ar_ur, $ar_ov); |
|
541 |
|
|
542 |
if (count($records) == count($res)) |
|
543 |
{ |
|
544 |
$records = $res; |
|
545 |
} |
|
546 |
else |
|
547 |
{ |
|
548 |
error(sprintf(ERR_INV_ARGC, "sort_zone", "records sorting failed!")); |
|
549 |
} |
|
550 |
return $records; |
|
551 |
} |
|
552 |
|
|
553 |
|
|
554 |
/* |
|
555 |
* Change owner of a domain. |
|
556 |
* Function should actually be in users.inc.php. But its more of a record modification than a user modification |
|
557 |
* return values: true when succesful. |
|
558 |
*/ |
|
559 |
function add_owner($domain, $newowner) |
|
560 |
{ |
|
561 |
global $db; |
|
562 |
|
|
563 |
if (!level(5)) |
|
564 |
{ |
|
565 |
error(ERR_LEVEL_5); |
|
566 |
} |
|
567 |
|
|
568 |
if (is_numeric($domain) && is_numeric($newowner) && is_valid_user($newowner)) |
|
569 |
{ |
8
|
570 |
if($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=$newowner AND domain_id=$domain") == 0) |
1
|
571 |
{ |
8
|
572 |
$db->query("INSERT INTO zones (domain_id, owner) VALUES($domain, $newowner)"); |
1
|
573 |
} |
|
574 |
return true; |
|
575 |
} |
|
576 |
else |
|
577 |
{ |
|
578 |
error(sprintf(ERR_INV_ARGC, "change_owner", "$domain / $newowner")); |
|
579 |
} |
|
580 |
} |
|
581 |
|
|
582 |
|
|
583 |
function delete_owner($domain, $owner) |
|
584 |
{ |
|
585 |
global $db; |
8
|
586 |
if($db->queryOne("SELECT COUNT(id) FROM zones WHERE owner=$owner AND domain_id=$domain") != 0) |
1
|
587 |
{ |
|
588 |
$db->query("DELETE FROM zones WHERE owner=$owner AND domain_id=$domain"); |
|
589 |
} |
|
590 |
return true; |
|
591 |
} |
|
592 |
|
|
593 |
/* |
|
594 |
* Retrieves all supported dns record types |
|
595 |
* This function might be deprecated. |
|
596 |
* return values: array of types in string form. |
|
597 |
*/ |
|
598 |
function get_record_types() |
|
599 |
{ |
|
600 |
global $rtypes; |
|
601 |
return $rtypes; |
|
602 |
} |
|
603 |
|
|
604 |
|
|
605 |
/* |
|
606 |
* Retrieve all records by a given type and domain id. |
|
607 |
* Example: get all records that are of type A from domain id 1 |
|
608 |
* return values: a DB class result object |
|
609 |
*/ |
|
610 |
function get_records_by_type_from_domid($type, $recid) |
|
611 |
{ |
|
612 |
global $rtypes; |
|
613 |
global $db; |
|
614 |
|
|
615 |
// Does this type exist? |
|
616 |
if(!in_array(strtoupper($type), $rtypes)) |
|
617 |
{ |
|
618 |
error(sprintf(ERR_INV_ARGC, "get_records_from_type", "this is not a supported record")); |
|
619 |
} |
|
620 |
|
|
621 |
// Get the domain id. |
|
622 |
$domid = recid_to_domid($recid); |
|
623 |
|
|
624 |
$result = $db->query("select id, type from records where domain_id=$recid and type='$type'"); |
|
625 |
return $result; |
|
626 |
} |
|
627 |
|
|
628 |
|
|
629 |
/* |
|
630 |
* Retrieves the type of a record from a given id. |
|
631 |
* return values: the type of the record (one of the records types in $rtypes assumable). |
|
632 |
*/ |
|
633 |
function get_recordtype_from_id($id) |
|
634 |
{ |
|
635 |
global $db; |
|
636 |
if (is_numeric($id)) |
|
637 |
{ |
|
638 |
$result = $db->query("SELECT type FROM records WHERE id=$id"); |
|
639 |
$r = $result->fetchRow(); |
|
640 |
return $r["type"]; |
|
641 |
} |
|
642 |
else |
|
643 |
{ |
|
644 |
error(sprintf(ERR_INV_ARG, "get_recordtype_from_id")); |
|
645 |
} |
|
646 |
} |
|
647 |
|
|
648 |
|
|
649 |
/* |
|
650 |
* Retrieves the name (e.g. bla.test.com) of a record by a given id. |
|
651 |
* return values: the name associated with the id. |
|
652 |
*/ |
|
653 |
function get_name_from_record_id($id) |
|
654 |
{ |
|
655 |
global $db; |
|
656 |
if (is_numeric($id)) |
|
657 |
{ |
|
658 |
$result = $db->query("SELECT name FROM records WHERE id=$id"); |
|
659 |
$r = $result->fetchRow(); |
|
660 |
return $r["name"]; |
|
661 |
} |
|
662 |
else |
|
663 |
{ |
|
664 |
error(sprintf(ERR_INV_ARG, "get_name_from_record_id")); |
|
665 |
} |
|
666 |
} |
|
667 |
|
|
668 |
|
|
669 |
/* |
|
670 |
* Get all the domains from a database of which the user is the owner. |
|
671 |
* return values: an array with the id of the domain and its name. |
|
672 |
*/ |
|
673 |
function get_domains_from_userid($id) |
|
674 |
{ |
|
675 |
global $db; |
|
676 |
if (is_numeric($id)) |
|
677 |
{ |
60
|
678 |
$a_zones = array(); |
|
679 |
|
|
680 |
// Check for zones the user has full access for (the |
|
681 |
// user is owner of the zone. |
|
682 |
|
|
683 |
$res_full = $db->query("SELECT |
|
684 |
domains.id AS domain_id, |
|
685 |
domains.name AS name |
|
686 |
FROM domains |
|
687 |
LEFT JOIN zones ON domains.id=zones.domain_id |
|
688 |
WHERE owner=$id"); |
|
689 |
|
|
690 |
// Process the output. |
1
|
691 |
|
60
|
692 |
$numrows = $res_full->numRows(); |
|
693 |
$i=1; |
|
694 |
if ($numrows > 0) |
|
695 |
{ |
|
696 |
$andnot=" AND NOT domains.id IN ("; |
|
697 |
while($r = $res_full->fetchRow()) { |
|
698 |
|
|
699 |
// Create array of zone id's and name's the owner |
|
700 |
// has full access to. |
|
701 |
|
|
702 |
$a_zones[] = array( |
|
703 |
"id" => $r["domain_id"], |
|
704 |
"name" => $r["name"], |
|
705 |
"partial" => "0" |
|
706 |
); |
1
|
707 |
|
60
|
708 |
// Create AND NOT for query of zones the user has |
|
709 |
// only partial access to. In that query we just |
|
710 |
// want to see the zones he has not full access to |
|
711 |
// as well. |
|
712 |
|
|
713 |
$andnot.=$r["domain_id"]; |
|
714 |
if ($i < $numrows) { |
|
715 |
$andnot.=","; |
|
716 |
$i++; |
|
717 |
} |
|
718 |
|
|
719 |
} |
|
720 |
$andnot.=")"; |
|
721 |
} |
|
722 |
else |
1
|
723 |
{ |
60
|
724 |
$andnot=""; |
|
725 |
} |
|
726 |
|
|
727 |
// Check for zones the user has partial access only to. |
|
728 |
|
|
729 |
$res_partial = $db->query("SELECT DISTINCT |
|
730 |
records.domain_id, |
|
731 |
domains.name |
|
732 |
FROM records, record_owners, domains |
|
733 |
WHERE record_owners.user_id = '".$id."' |
|
734 |
AND records.id = record_owners.record_id |
|
735 |
AND domains.id = records.domain_id |
|
736 |
".$andnot.";"); |
|
737 |
|
|
738 |
// Add these zones to the array as well. |
|
739 |
|
|
740 |
while ($r = $res_partial->fetchRow()) |
|
741 |
{ |
|
742 |
$a_zones[] = array( |
1
|
743 |
"id" => $r["domain_id"], |
60
|
744 |
"name" => $r["name"], |
|
745 |
"partial" => "1" |
1
|
746 |
); |
|
747 |
} |
60
|
748 |
|
|
749 |
return $a_zones; |
1
|
750 |
} |
|
751 |
else |
|
752 |
{ |
|
753 |
error(sprintf(ERR_INV_ARGC, "get_domains_from_userid", "This is not a valid userid: $id")); |
|
754 |
} |
|
755 |
} |
|
756 |
|
|
757 |
|
|
758 |
/* |
|
759 |
* Get domain name from a given id |
|
760 |
* return values: the name of the domain associated with the id. |
|
761 |
*/ |
|
762 |
function get_domain_name_from_id($id) |
|
763 |
{ |
|
764 |
global $db; |
|
765 |
if (!xs($id)) |
|
766 |
{ |
|
767 |
error(ERR_RECORD_ACCESS_DENIED); |
|
768 |
} |
|
769 |
if (is_numeric($id)) |
|
770 |
{ |
|
771 |
$result = $db->query("SELECT name FROM domains WHERE id=$id"); |
|
772 |
if ($result->numRows() == 1) |
|
773 |
{ |
|
774 |
$r = $result->fetchRow(); |
|
775 |
return $r["name"]; |
|
776 |
} |
|
777 |
else |
|
778 |
{ |
|
779 |
error(sprintf(ERR_INV_ARGC, "get_domain_name_from_id", "more than one domain found?! whaaa! BAD! BAD! Contact admin!")); |
|
780 |
} |
|
781 |
} |
|
782 |
else |
|
783 |
{ |
|
784 |
error(sprintf(ERR_INV_ARGC, "get_domain_name_from_id", "Not a valid domainid: $id")); |
|
785 |
} |
|
786 |
} |
|
787 |
|
|
788 |
|
|
789 |
/* |
|
790 |
* Get information about a domain name from a given domain id. |
|
791 |
* the function looks up the domainname, the owner of the domain and the number of records in it. |
|
792 |
* return values: an array containing the information. |
|
793 |
*/ |
|
794 |
function get_domain_info_from_id($id) |
|
795 |
{ |
|
796 |
global $db; |
|
797 |
if (!xs($id)) |
|
798 |
{ |
|
799 |
error(ERR_RECORD_ACCESS_DENIED); |
|
800 |
} |
|
801 |
if (is_numeric($id)) |
|
802 |
{ |
|
803 |
|
|
804 |
if ($_SESSION[$id."_ispartial"] == 1) { |
|
805 |
|
13
|
806 |
$sqlq = "SELECT |
|
807 |
domains.type AS type, |
|
808 |
domains.name AS name, |
1
|
809 |
users.fullname AS owner, |
|
810 |
count(record_owners.id) AS aantal |
|
811 |
FROM domains, users, record_owners, records |
|
812 |
|
|
813 |
WHERE record_owners.user_id = ".$_SESSION["userid"]." |
|
814 |
AND record_owners.record_id = records.id |
|
815 |
AND records.domain_id = ".$id." |
|
816 |
|
55
|
817 |
GROUP BY domains.name, owner, users.fullname, domains.type |
|
818 |
ORDER BY domains.name"; |
1
|
819 |
|
8
|
820 |
$result = $db->queryRow($sqlq); |
1
|
821 |
|
|
822 |
$ret = array( |
|
823 |
"name" => $result["name"], |
|
824 |
"ownerid" => $_SESSION["userid"], |
|
825 |
"owner" => $result["owner"], |
13
|
826 |
"type" => $result["type"], |
1
|
827 |
"numrec" => $result["aantal"] |
|
828 |
); |
|
829 |
|
|
830 |
return $ret; |
|
831 |
|
|
832 |
} else{ |
|
833 |
|
|
834 |
// Query that retrieves the information we need. |
13
|
835 |
$sqlq = "SELECT |
|
836 |
domains.type AS type, |
|
837 |
domains.name AS name, |
1
|
838 |
min(zones.owner) AS ownerid, |
|
839 |
users.fullname AS owner, |
|
840 |
count(records.domain_id) AS aantal |
|
841 |
FROM domains |
|
842 |
LEFT JOIN records ON domains.id=records.domain_id |
|
843 |
LEFT JOIN zones ON domains.id=zones.domain_id |
|
844 |
LEFT JOIN users ON zones.owner=users.id |
|
845 |
WHERE domains.id=$id |
55
|
846 |
GROUP BY domains.name, owner, users.fullname, domains.type, zones.id |
1
|
847 |
ORDER BY zones.id"; |
|
848 |
|
|
849 |
// Put the first occurence in an array and return it. |
8
|
850 |
$result = $db->queryRow($sqlq); |
1
|
851 |
|
8
|
852 |
//$result["ownerid"] = ($result["ownerid"] == NULL) ? $db->queryOne("select min(id) from users where users.level=10") : $result["ownerid"]; |
1
|
853 |
|
|
854 |
$ret = array( |
|
855 |
"name" => $result["name"], |
|
856 |
"ownerid" => $result["ownerid"], |
|
857 |
"owner" => $result["owner"], |
13
|
858 |
"type" => $result["type"], |
1
|
859 |
"numrec" => $result["aantal"] |
|
860 |
); |
|
861 |
return $ret; |
|
862 |
} |
|
863 |
|
|
864 |
} |
|
865 |
else |
|
866 |
{ |
|
867 |
error(sprintf(ERR_INV_ARGC, "get_domain_num_records_from_id", "This is not a valid domainid: $id")); |
|
868 |
} |
|
869 |
} |
|
870 |
|
|
871 |
|
|
872 |
/* |
|
873 |
* Check if a domain is already existing. |
|
874 |
* return values: true if existing, false if it doesnt exist. |
|
875 |
*/ |
|
876 |
function domain_exists($domain) |
|
877 |
{ |
|
878 |
global $db; |
|
879 |
|
|
880 |
if (!level(5)) |
|
881 |
{ |
|
882 |
error(ERR_LEVEL_5); |
|
883 |
} |
|
884 |
if (is_valid_domain($domain)) |
|
885 |
{ |
|
886 |
$result = $db->query("SELECT id FROM domains WHERE name='$domain'"); |
|
887 |
if ($result->numRows() == 0) |
|
888 |
{ |
|
889 |
return false; |
|
890 |
} |
|
891 |
elseif ($result->numRows() >= 1) |
|
892 |
{ |
|
893 |
return true; |
|
894 |
} |
|
895 |
} |
|
896 |
else |
|
897 |
{ |
|
898 |
error(ERR_DOMAIN_INVALID); |
|
899 |
} |
|
900 |
} |
|
901 |
|
13
|
902 |
function get_supermasters() |
|
903 |
{ |
|
904 |
global $db; |
|
905 |
$result = $db->query("SELECT ip, nameserver, account FROM supermasters"); |
|
906 |
$ret = array(); |
|
907 |
|
|
908 |
if($result->numRows() == 0) |
|
909 |
{ |
|
910 |
return -1; |
|
911 |
} |
|
912 |
else |
|
913 |
{ |
|
914 |
while ($r = $result->fetchRow()) |
|
915 |
{ |
|
916 |
$ret[] = array( |
|
917 |
"master_ip" => $r["ip"], |
|
918 |
"ns_name" => $r["nameserver"], |
|
919 |
"account" => $r["account"], |
|
920 |
); |
|
921 |
} |
36
|
922 |
return $ret; |
13
|
923 |
} |
|
924 |
} |
|
925 |
|
|
926 |
function supermaster_exists($master_ip) |
|
927 |
{ |
|
928 |
global $db; |
|
929 |
if (!level(5)) |
|
930 |
{ |
|
931 |
error(ERR_LEVEL_5); |
|
932 |
} |
|
933 |
if (is_valid_ip($master_ip) || is_valid_ip6($master_ip)) |
|
934 |
{ |
|
935 |
$result = $db->query("SELECT ip FROM supermasters WHERE ip = '$master_ip'"); |
|
936 |
if ($result->numRows() == 0) |
|
937 |
{ |
|
938 |
return false; |
|
939 |
} |
|
940 |
elseif ($result->numRows() >= 1) |
|
941 |
{ |
|
942 |
return true; |
|
943 |
} |
|
944 |
} |
|
945 |
else |
|
946 |
{ |
|
947 |
error(sprintf(ERR_INV_ARGC, "supermaster_exists", "No or no valid IPv4 or IPv6 address given.")); |
|
948 |
} |
|
949 |
} |
|
950 |
|
1
|
951 |
|
|
952 |
/* |
13
|
953 |
* Get all domains from the database |
1
|
954 |
* This function gets all the domains from the database unless a user id is below 5. |
|
955 |
* if a user id is below 5 this function will only retrieve records for that user. |
|
956 |
* return values: the array of domains or -1 if nothing is found. |
|
957 |
*/ |
|
958 |
function get_domains($userid=true,$letterstart=all,$rowstart=0,$rowamount=999999) |
|
959 |
{ |
|
960 |
global $db; |
55
|
961 |
global $sql_regexp; |
1
|
962 |
if((!level(5) || !$userid) && !level(10) && !level(5)) |
|
963 |
{ |
|
964 |
$add = " AND zones.owner=".$_SESSION["userid"]; |
|
965 |
} |
|
966 |
else |
|
967 |
{ |
|
968 |
$add = ""; |
|
969 |
} |
|
970 |
|
|
971 |
$sqlq = "SELECT domains.id AS domain_id, |
|
972 |
min(zones.owner) AS owner, |
|
973 |
count(DISTINCT records.id) AS aantal, |
|
974 |
domains.name AS domainname |
|
975 |
FROM domains |
|
976 |
LEFT JOIN zones ON domains.id=zones.domain_id |
|
977 |
LEFT JOIN records ON records.domain_id=domains.id |
55
|
978 |
WHERE 1=1 $add "; |
1
|
979 |
if ($letterstart!=all && $letterstart!=1) { |
55
|
980 |
$sqlq.=" AND substring(domains.name,1,1) ".$sql_regexp." '^".$letterstart."' "; |
1
|
981 |
} elseif ($letterstart==1) { |
55
|
982 |
$sqlq.=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'"; |
1
|
983 |
} |
55
|
984 |
$sqlq.=" GROUP BY domainname, domains.id |
1
|
985 |
ORDER BY domainname |
55
|
986 |
LIMIT $rowamount OFFSET $rowstart"; |
1
|
987 |
|
|
988 |
$result = $db->query($sqlq); |
21
|
989 |
$result2 = $db->query($sqlq); |
|
990 |
|
|
991 |
$numrows = $result2->numRows(); |
|
992 |
$i=1; |
|
993 |
if ($numrows > 0) { |
|
994 |
$andnot=" AND NOT domains.id IN ("; |
|
995 |
while($r = $result2->fetchRow()) { |
|
996 |
$andnot.=$r["domain_id"]; |
|
997 |
if ($i < $numrows) { |
|
998 |
$andnot.=","; |
|
999 |
$i++; |
|
1000 |
} |
|
1001 |
} |
|
1002 |
$andnot.=")"; |
|
1003 |
} |
|
1004 |
else |
|
1005 |
{ |
|
1006 |
$andnot=""; |
1
|
1007 |
} |
|
1008 |
|
21
|
1009 |
if ($letterstart!=all && $letterstart!=1) { |
1
|
1010 |
|
21
|
1011 |
$sqlq = "SELECT domains.id AS domain_id, |
|
1012 |
count(DISTINCT record_owners.record_id) AS aantal, |
|
1013 |
domains.name AS domainname |
|
1014 |
FROM domains, record_owners,records, zones |
|
1015 |
WHERE record_owners.user_id = '".$_SESSION["userid"]."' |
|
1016 |
AND (records.id = record_owners.record_id |
|
1017 |
AND domains.id = records.domain_id) |
|
1018 |
$andnot |
|
1019 |
AND domains.name LIKE '".$letterstart."%' |
|
1020 |
AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."') |
55
|
1021 |
GROUP BY domainname, domains.id |
21
|
1022 |
ORDER BY domainname"; |
1
|
1023 |
|
21
|
1024 |
$result_extra = $db->query($sqlq); |
1
|
1025 |
|
21
|
1026 |
} else { |
1
|
1027 |
|
21
|
1028 |
$sqlq = "SELECT domains.id AS domain_id, |
|
1029 |
count(DISTINCT record_owners.record_id) AS aantal, |
|
1030 |
domains.name AS domainname |
|
1031 |
FROM domains, record_owners,records, zones |
|
1032 |
WHERE record_owners.user_id = '".$_SESSION["userid"]."' |
|
1033 |
AND (records.id = record_owners.record_id |
|
1034 |
AND domains.id = records.domain_id) |
|
1035 |
$andnot |
55
|
1036 |
AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]' |
21
|
1037 |
AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."') |
55
|
1038 |
GROUP BY domainname, domains.id |
21
|
1039 |
ORDER BY domainname"; |
1
|
1040 |
|
21
|
1041 |
$result_extra[$i] = $db->query($sqlq); |
|
1042 |
|
1
|
1043 |
} |
|
1044 |
|
|
1045 |
while($r = $result->fetchRow()) |
|
1046 |
{ |
8
|
1047 |
$r["owner"] = ($r["owner"] == NULL) ? $db->queryOne("select min(id) from users where users.level=10") : $r["owner"]; |
1
|
1048 |
$ret[$r["domainname"]] = array( |
|
1049 |
"name" => $r["domainname"], |
|
1050 |
"id" => $r["domain_id"], |
|
1051 |
"owner" => $r["owner"], |
|
1052 |
"numrec" => $r["aantal"] |
|
1053 |
); |
|
1054 |
} |
|
1055 |
|
|
1056 |
|
21
|
1057 |
if ($letterstart!=all && $letterstart!=1) { |
1
|
1058 |
|
21
|
1059 |
while($r = $result_extra->fetchRow()) |
|
1060 |
{ |
|
1061 |
$ret[$r["domainname"]] = array( |
|
1062 |
"name" => $r["domainname"]."*", |
|
1063 |
"id" => $r["domain_id"], |
|
1064 |
"owner" => $_SESSION["userid"], |
|
1065 |
"numrec" => $r["aantal"] |
|
1066 |
); |
|
1067 |
$_SESSION["partial_".$r["domainname"]] = 1; |
|
1068 |
} |
1
|
1069 |
|
21
|
1070 |
} else { |
1
|
1071 |
|
21
|
1072 |
foreach ($result_extra as $result_e) { |
|
1073 |
while($r = $result_e->fetchRow()) |
|
1074 |
{ |
|
1075 |
$ret[$r["domainname"]] = array( |
|
1076 |
"name" => $r["domainname"]."*", |
|
1077 |
"id" => $r["domain_id"], |
|
1078 |
"owner" => $_SESSION["userid"], |
|
1079 |
"numrec" => $r["aantal"] |
|
1080 |
); |
|
1081 |
$_SESSION["partial_".$r["domainname"]] = 1; |
|
1082 |
} |
|
1083 |
} |
|
1084 |
|
1
|
1085 |
} |
|
1086 |
|
21
|
1087 |
if (empty($ret)) { |
|
1088 |
return -1; |
|
1089 |
} else { |
|
1090 |
sort($ret); |
|
1091 |
return $ret; |
|
1092 |
} |
1
|
1093 |
|
|
1094 |
} |
|
1095 |
|
|
1096 |
|
|
1097 |
/* |
30
|
1098 |
* zone_count |
|
1099 |
* Does a select query to count how many zones we have in the database |
|
1100 |
* |
|
1101 |
* @todo: see whether or not it is possible to add the records |
|
1102 |
* @param $userid integer The userid of the current user |
|
1103 |
* @return integer the number of zones |
|
1104 |
*/ |
|
1105 |
|
|
1106 |
function zone_count($userid=true, $letterstart=all) { |
|
1107 |
global $db; |
55
|
1108 |
global $sql_regexp; |
30
|
1109 |
if((!level(5) || !$userid) && !level(10) && !level(5)) |
|
1110 |
{ |
37
|
1111 |
// First select the zones for which we have ownership on one or more records. |
|
1112 |
$query = 'SELECT records.domain_id FROM records, record_owners WHERE user_id = '.$_SESSION['userid'].' AND records.id = record_owners.record_id'; |
|
1113 |
$result = $db->query($query); |
|
1114 |
$zones = array(); |
|
1115 |
if (!PEAR::isError($result)) { |
|
1116 |
$zones = $result->fetchCol(); |
|
1117 |
} |
|
1118 |
|
|
1119 |
$add = " AND (zones.owner=".$_SESSION["userid"]; |
|
1120 |
if (count($zones) > 0) { |
|
1121 |
$add .= ' OR zones.domain_id IN ('.implode(',', $zones).') '; |
|
1122 |
|
|
1123 |
} |
|
1124 |
$add .= ')'; |
30
|
1125 |
} |
|
1126 |
else |
|
1127 |
{ |
|
1128 |
$add = ""; |
|
1129 |
} |
|
1130 |
|
|
1131 |
if ($letterstart!=all && $letterstart!=1) { |
|
1132 |
$add .=" AND domains.name LIKE '".$letterstart."%' "; |
|
1133 |
} elseif ($letterstart==1) { |
55
|
1134 |
$add .=" AND substring(domains.name,1,1) ".$sql_regexp." '^[[:digit:]]'"; |
30
|
1135 |
} |
|
1136 |
|
40
|
1137 |
if (level(5)) |
|
1138 |
{ |
55
|
1139 |
$query = 'SELECT count(distinct domains.id) as zone_count FROM domains WHERE 1=1 '.$add; |
40
|
1140 |
} |
|
1141 |
else |
|
1142 |
{ |
|
1143 |
$query = 'SELECT count(distinct zones.domain_id) as zone_count FROM zones, domains WHERE zones.domain_id = domains.id '.$add; |
|
1144 |
} |
30
|
1145 |
$numRows = $db->queryOne($query); |
|
1146 |
return $numRows; |
|
1147 |
} |
|
1148 |
|
|
1149 |
/* |
1
|
1150 |
* Get a record from an id. |
|
1151 |
* Retrieve all fields of the record and send it back to the function caller. |
|
1152 |
* return values: the array with information, or -1 is nothing is found. |
|
1153 |
*/ |
|
1154 |
function get_record_from_id($id) |
|
1155 |
{ |
|
1156 |
global $db; |
|
1157 |
if (is_numeric($id)) |
|
1158 |
{ |
|
1159 |
$result = $db->query("SELECT id, domain_id, name, type, content, ttl, prio, change_date FROM records WHERE id=$id"); |
|
1160 |
if($result->numRows() == 0) |
|
1161 |
{ |
|
1162 |
return -1; |
|
1163 |
} |
|
1164 |
elseif ($result->numRows() == 1) |
|
1165 |
{ |
|
1166 |
$r = $result->fetchRow(); |
|
1167 |
$ret = array( |
|
1168 |
"id" => $r["id"], |
|
1169 |
"domain_id" => $r["domain_id"], |
|
1170 |
"name" => $r["name"], |
|
1171 |
"type" => $r["type"], |
|
1172 |
"content" => $r["content"], |
|
1173 |
"ttl" => $r["ttl"], |
|
1174 |
"prio" => $r["prio"], |
|
1175 |
"change_date" => $r["change_date"] |
|
1176 |
); |
|
1177 |
return $ret; |
|
1178 |
} |
|
1179 |
else |
|
1180 |
{ |
|
1181 |
error(sprintf(ERR_INV_ARGC, "get_record_from_id", "More than one row returned! This is bad!")); |
|
1182 |
} |
|
1183 |
} |
|
1184 |
else |
|
1185 |
{ |
|
1186 |
error(sprintf(ERR_INV_ARG, "get_record_from_id")); |
|
1187 |
} |
|
1188 |
} |
|
1189 |
|
|
1190 |
|
|
1191 |
/* |
|
1192 |
* Get all records from a domain id. |
|
1193 |
* Retrieve all fields of the records and send it back to the function caller. |
|
1194 |
* return values: the array with information, or -1 is nothing is found. |
|
1195 |
*/ |
|
1196 |
function get_records_from_domain_id($id,$rowstart=0,$rowamount=999999) |
|
1197 |
{ |
|
1198 |
global $db; |
|
1199 |
if (is_numeric($id)) |
|
1200 |
{ |
|
1201 |
if ($_SESSION[$id."_ispartial"] == 1) { |
|
1202 |
|
|
1203 |
$result = $db->query("SELECT record_owners.record_id as id |
|
1204 |
FROM record_owners,domains,records |
|
1205 |
WHERE record_owners.user_id = ".$_SESSION["userid"]." |
|
1206 |
AND record_owners.record_id = records.id |
|
1207 |
AND records.domain_id = ".$id." |
|
1208 |
GROUP bY record_owners.record_id |
55
|
1209 |
LIMIT $rowamount OFFSET $rowstart"); |
1
|
1210 |
|
|
1211 |
$ret = array(); |
|
1212 |
if($result->numRows() == 0) |
|
1213 |
{ |
|
1214 |
return -1; |
|
1215 |
} |
|
1216 |
else |
|
1217 |
{ |
|
1218 |
$ret[] = array(); |
|
1219 |
$retcount = 0; |
|
1220 |
while($r = $result->fetchRow()) |
|
1221 |
{ |
|
1222 |
// Call get_record_from_id for each row. |
|
1223 |
$ret[$retcount] = get_record_from_id($r["id"]); |
|
1224 |
$retcount++; |
|
1225 |
} |
|
1226 |
return $ret; |
|
1227 |
} |
|
1228 |
|
|
1229 |
} else { |
|
1230 |
|
55
|
1231 |
$result = $db->query("SELECT id FROM records WHERE domain_id=$id LIMIT $rowamount OFFSET $rowstart"); |
1
|
1232 |
$ret = array(); |
|
1233 |
if($result->numRows() == 0) |
|
1234 |
{ |
|
1235 |
return -1; |
|
1236 |
} |
|
1237 |
else |
|
1238 |
{ |
|
1239 |
$ret[] = array(); |
|
1240 |
$retcount = 0; |
|
1241 |
while($r = $result->fetchRow()) |
|
1242 |
{ |
|
1243 |
// Call get_record_from_id for each row. |
|
1244 |
$ret[$retcount] = get_record_from_id($r["id"]); |
|
1245 |
$retcount++; |
|
1246 |
} |
|
1247 |
return $ret; |
|
1248 |
} |
|
1249 |
|
|
1250 |
} |
|
1251 |
} |
|
1252 |
else |
|
1253 |
{ |
|
1254 |
error(sprintf(ERR_INV_ARG, "get_records_from_domain_id")); |
|
1255 |
} |
|
1256 |
} |
|
1257 |
|
|
1258 |
|
|
1259 |
function get_users_from_domain_id($id) |
|
1260 |
{ |
|
1261 |
global $db; |
8
|
1262 |
$result = $db->queryCol("SELECT owner FROM zones WHERE domain_id=$id"); |
1
|
1263 |
$ret = array(); |
|
1264 |
foreach($result as $uid) |
|
1265 |
{ |
8
|
1266 |
$fullname = $db->queryOne("SELECT fullname FROM users WHERE id=$uid"); |
1
|
1267 |
$ret[] = array( |
|
1268 |
"id" => $uid, |
|
1269 |
"fullname" => $fullname |
|
1270 |
); |
|
1271 |
} |
|
1272 |
return $ret; |
|
1273 |
} |
|
1274 |
|
|
1275 |
function search_record($question) |
|
1276 |
{ |
|
1277 |
global $db; |
|
1278 |
$question = trim($question); |
62
|
1279 |
|
|
1280 |
if (is_valid_search($question)) |
1
|
1281 |
{ |
62
|
1282 |
$sqlq = "SELECT * |
|
1283 |
FROM records |
|
1284 |
WHERE content LIKE '".$question."' |
|
1285 |
OR name LIKE '".$question."' |
|
1286 |
ORDER BY type DESC"; |
|
1287 |
$result = $db->query($sqlq); |
|
1288 |
$ret_r = array(); |
|
1289 |
while ($r = $result->fetchRow()) |
|
1290 |
{ |
|
1291 |
if(xs($r['domain_id'])) |
|
1292 |
{ |
|
1293 |
$ret_r[] = array( |
|
1294 |
'id' => $r['id'], |
|
1295 |
'domain_id' => $r['domain_id'], |
|
1296 |
'name' => $r['name'], |
|
1297 |
'type' => $r['type'], |
|
1298 |
'content' => $r['content'], |
|
1299 |
'ttl' => $r['ttl'], |
|
1300 |
'prio' => $r['prio'], |
|
1301 |
'change_date' => $r['change_date'] |
|
1302 |
); |
|
1303 |
} |
|
1304 |
} |
|
1305 |
|
|
1306 |
$sqlq = "SELECT domains.id, domains.name, count(records.id) AS numrec, zones.owner, records.domain_id |
|
1307 |
FROM domains, records, zones |
|
1308 |
WHERE domains.id = records.domain_id |
|
1309 |
AND zones.domain_id = domains.id |
|
1310 |
AND domains.name LIKE '".$question."' |
|
1311 |
GROUP BY domains.id, domains.name, zones.owner, records.domain_id"; |
|
1312 |
$result = $db->query($sqlq); |
|
1313 |
$ret_d = array(); |
|
1314 |
while ($r = $result->fetchRow()) |
|
1315 |
{ |
|
1316 |
if(xs($r['domain_id'])) |
|
1317 |
{ |
|
1318 |
$ret_d[] = array( |
|
1319 |
'id' => $r['id'], |
|
1320 |
'name' => $r['name'], |
|
1321 |
'numrec' => $r['numrec'], |
|
1322 |
'owner' => $r['owner'] |
|
1323 |
); |
|
1324 |
} |
|
1325 |
} |
|
1326 |
return array('domains' => $ret_d, 'records' => $ret_r); |
|
1327 |
} |
|
1328 |
else |
|
1329 |
{ |
|
1330 |
error(sprintf(ERR_INV_ARGC, "search_record", "Invalid searchstring: $question")); |
1
|
1331 |
} |
|
1332 |
|
|
1333 |
} |
|
1334 |
|
|
1335 |
function get_domain_type($id) |
|
1336 |
{ |
|
1337 |
global $db; |
13
|
1338 |
if (is_numeric($id)) |
1
|
1339 |
{ |
55
|
1340 |
$type = $db->queryOne("SELECT type FROM domains WHERE id = '".$id."'"); |
13
|
1341 |
if($type == "") |
|
1342 |
{ |
|
1343 |
$type = "NATIVE"; |
|
1344 |
} |
|
1345 |
return $type; |
|
1346 |
} |
|
1347 |
else |
|
1348 |
{ |
|
1349 |
error(sprintf(ERR_INV_ARG, "get_record_from_id", "no or no valid zoneid given")); |
|
1350 |
} |
|
1351 |
} |
|
1352 |
|
|
1353 |
function get_domain_slave_master($id) |
|
1354 |
{ |
|
1355 |
global $db; |
|
1356 |
if (is_numeric($id)) |
|
1357 |
{ |
55
|
1358 |
$slave_master = $db->queryOne("SELECT master FROM domains WHERE type = 'SLAVE' and id = '".$id."'"); |
13
|
1359 |
return $slave_master; |
|
1360 |
} |
|
1361 |
else |
|
1362 |
{ |
|
1363 |
error(sprintf(ERR_INV_ARG, "get_domain_slave_master", "no or no valid zoneid given")); |
|
1364 |
} |
1
|
1365 |
} |
|
1366 |
|
|
1367 |
function change_domain_type($type, $id) |
|
1368 |
{ |
13
|
1369 |
global $db; |
|
1370 |
unset($add); |
|
1371 |
if (is_numeric($id)) |
|
1372 |
{ |
|
1373 |
// It is not really neccesary to clear the master field if a |
|
1374 |
// zone is not of the type "slave" as powerdns will ignore that |
|
1375 |
// fiedl, but it is cleaner anyway. |
|
1376 |
if ($type != "SLAVE") |
|
1377 |
{ |
|
1378 |
$add = ", master=''"; |
|
1379 |
} |
55
|
1380 |
$result = $db->query("UPDATE domains SET type = '" .$type. "'".$add." WHERE id = '".$id."'"); |
13
|
1381 |
} |
|
1382 |
else |
|
1383 |
{ |
|
1384 |
error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given")); |
|
1385 |
} |
|
1386 |
} |
|
1387 |
|
|
1388 |
function change_domain_slave_master($id, $slave_master) |
|
1389 |
{ |
|
1390 |
global $db; |
|
1391 |
if (is_numeric($id)) |
|
1392 |
{ |
|
1393 |
if (is_valid_ip($slave_master) || is_valid_ip6($slave_master)) |
|
1394 |
{ |
55
|
1395 |
$result = $db->query("UPDATE domains SET master = '" .$slave_master. "' WHERE id = '".$id."'"); |
13
|
1396 |
} |
|
1397 |
else |
|
1398 |
{ |
|
1399 |
error(sprintf(ERR_INV_ARGC, "change_domain_slave_master", "This is not a valid IPv4 or IPv6 address: $slave_master")); |
|
1400 |
} |
|
1401 |
} |
|
1402 |
else |
|
1403 |
{ |
|
1404 |
error(sprintf(ERR_INV_ARG, "change_domain_type", "no or no valid zoneid given")); |
|
1405 |
} |
|
1406 |
} |
|
1407 |
|
|
1408 |
|
|
1409 |
function validate_account($account) |
|
1410 |
{ |
|
1411 |
|
|
1412 |
if(preg_match("/^[A-Z0-9._-]+$/i",$account)) |
|
1413 |
{ |
|
1414 |
return true; |
|
1415 |
} |
|
1416 |
else |
|
1417 |
{ |
|
1418 |
return false; |
|
1419 |
} |
1
|
1420 |
} |
|
1421 |
?> |