71
+ − 1
<?php
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
65
+ − 40
$sqlq = "SELECT notified_serial FROM domains WHERE id = " . $db -> quote ( $domain_id );
8
+ − 41
$notified_serial = $db -> queryOne ( $sqlq );
1
+ − 42
65
+ − 43
$sqlq = "SELECT content FROM records WHERE type = 'SOA' AND domain_id = " . $db -> quote ( $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
}
65
+ − 104
$sqlq = "UPDATE records SET content = " . $db -> quote ( $new_soa ) . " WHERE domain_id = " . $db -> quote ( $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 ();
65
+ − 131
$db -> query ( "UPDATE records set name=" . $db -> quote ( $name ) . ", type=" . $db -> quote ( $type ) . ", content=" . $db -> quote ( $content ) . ", ttl=" . $db -> quote ( $ttl ) . ", prio=" . $db -> quote ( $prio ) . ", change_date=" . $db -> quote ( $change ) . " WHERE id=" . $db -> quote ( $recordid ));
1
+ − 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
{
65
+ − 160
$db -> query ( "INSERT INTO record_owners (user_id, record_id) VALUES (" . $db -> quote ( $userid ) . ", " . $db -> quote ( $recordid ) . ")" );
56
+ − 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
{
65
+ − 178
$db -> query ( "DELETE FROM record_owners WHERE id=" . $db -> quote ( $rowid ) . " AND record_id=" . $db -> quote ( $recordid ));
57
+ − 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.
65
+ − 209
$db -> query ( "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES (" . $db -> quote ( $zoneid ) . ", " . $db -> quote ( $name ) . ", " . $db -> quote ( $type ) . ", " . $db -> quote ( $content ) . ", " . $db -> quote ( $ttl ) . ", " . $db -> quote ( $prio ) . ", " . $db -> quote ( $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
{
65
+ − 244
$db -> query ( "INSERT INTO supermasters VALUES (" . $db -> quote ( $master_ip ) . ", " . $db -> quote ( $ns_name ) . ", " . $db -> quote ( $account ) . ")" );
13
+ − 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
{
65
+ − 258
$db -> query ( "DELETE FROM supermasters WHERE ip = " . $db -> quote ( $master_ip ));
13
+ − 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
{
65
+ − 276
$result = $db -> queryRow ( "SELECT ip,nameserver,account FROM supermasters WHERE ip = " . $db -> quote ( $master_ip ));
13
+ − 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 );
65
+ − 323
$db -> query ( 'DELETE FROM records WHERE id=' . $db -> quote ( $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
65
+ − 371
$db -> query ( "INSERT INTO domains (name, type) VALUES (" . $db -> quote ( $domain ) . ", " . $db -> quote ( $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
65
+ − 380
$db -> query ( "INSERT INTO zones (domain_id, owner) VALUES (" . $db -> quote ( $iddomain ) . ", " . $db -> quote ( $owner ) . ")" );
1
+ − 381
13
+ − 382
if ( $type == "SLAVE" )
1
+ − 383
{
65
+ − 384
$db -> query ( "UPDATE domains SET master = " . $db -> quote ( $slave_master ) . " WHERE id = " . $db -> quote ( $iddomain ));
13
+ − 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
65
+ − 403
$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES (" . $db -> quote ( $iddomain ) . ", " . $db -> quote ( $domain ) . ", " . $db -> quote ( $ns1 . ' ' . $hm . ' 1' ) . ", 'SOA', " . $db -> quote ( $ttl ) . ", 0, " . $db -> quote ( $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
65
+ − 433
$sql = "INSERT INTO records (domain_id, name, content, type, ttl, prio, change_date) VALUES (" . $db -> quote ( $iddomain ) . ", " . $db -> quote ( $name ) . ", " . $db -> quote ( $content ) . ", " . $db -> quote ( $type ) . ", " . $db -> quote ( $ttl ) . ", " . $db -> quote ( $prio ) . ", " . $db -> quote ( $now ) . ")" ;
13
+ − 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
{
65
+ − 469
$db -> query ( "DELETE FROM zones WHERE domain_id=" . $db -> quote ( $id ));
+ − 470
$db -> query ( "DELETE FROM domains WHERE id=" . $db -> quote ( $id ));
+ − 471
$db -> query ( "DELETE FROM records WHERE domain_id=" . $db -> quote ( $id ));
1
+ − 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
{
65
+ − 492
$result = $db -> query ( "SELECT domain_id FROM records WHERE id=" . $db -> quote ( $id ));
1
+ − 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
{
65
+ − 570
if ( $db -> queryOne ( "SELECT COUNT(id) FROM zones WHERE owner=" . $db -> quote ( $newowner ) . " AND domain_id=" . $db -> quote ( $domain )) == 0 )
1
+ − 571
{
65
+ − 572
$db -> query ( "INSERT INTO zones (domain_id, owner) VALUES(" . $db -> quote ( $domain ) . ", " . $db -> quote ( $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 ;
65
+ − 586
if ( $db -> queryOne ( "SELECT COUNT(id) FROM zones WHERE owner=" . $db -> quote ( $owner ) . " AND domain_id=" . $db -> quote ( $domain )) != 0 )
1
+ − 587
{
65
+ − 588
$db -> query ( "DELETE FROM zones WHERE owner=" . $db -> quote ( $owner ) . " AND domain_id=" . $db -> quote ( $domain ));
1
+ − 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
65
+ − 624
$result = $db -> query ( "select id, type from records where domain_id=" . $db -> quote ( $recid ) . " and type=" . $db -> quote ( $type ));
1
+ − 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
{
65
+ − 638
$result = $db -> query ( "SELECT type FROM records WHERE id=" . $db -> quote ( $id ));
1
+ − 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
{
65
+ − 658
$result = $db -> query ( "SELECT name FROM records WHERE id=" . $db -> quote ( $id ));
1
+ − 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
65
+ − 688
WHERE owner=" . $db -> quote ( $id ));
60
+ − 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
65
+ − 713
$andnot .= $db -> quote ( $r [ "domain_id" ]);
60
+ − 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
65
+ − 733
WHERE record_owners.user_id = " . $db -> quote ( $id ) . "
60
+ − 734
AND records.id = record_owners.record_id
+ − 735
AND domains.id = records.domain_id
65
+ − 736
" . $andnot );
60
+ − 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
{
65
+ − 771
$result = $db -> query ( "SELECT name FROM domains WHERE id=" . $db -> quote ( $id ));
1
+ − 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
65
+ − 813
WHERE record_owners.user_id = " . $db -> quote ( $_SESSION [ "userid" ]) . "
1
+ − 814
AND record_owners.record_id = records.id
65
+ − 815
AND records.domain_id = " . $db -> quote ( $id ) . "
1
+ − 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
{
65
+ − 886
$result = $db -> query ( "SELECT id FROM domains WHERE name=" . $db -> quote ( $domain ));
1
+ − 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
{
65
+ − 935
$result = $db -> query ( "SELECT ip FROM supermasters WHERE ip = " . $db -> quote ( $master_ip ));
13
+ − 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
{
65
+ − 964
$add = " AND zones.owner=" . $db -> quote ( $_SESSION [ "userid" ]);
1
+ − 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 ) {
65
+ − 980
$sqlq .= " AND substring(domains.name,1,1) " . $sql_regexp . " " . $db -> quote ( "^" . $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
65
+ − 985
ORDER BY domainname" ;
1
+ − 986
65
+ − 987
$db -> setLimit ( $rowstart , $rowamount );
1
+ − 988
$result = $db -> query ( $sqlq );
65
+ − 989
// Set limit needs to be called before each query
+ − 990
$db -> setLimit ( $rowstart , $rowamount );
21
+ − 991
$result2 = $db -> query ( $sqlq );
+ − 992
+ − 993
$numrows = $result2 -> numRows ();
+ − 994
$i = 1 ;
+ − 995
if ( $numrows > 0 ) {
+ − 996
$andnot = " AND NOT domains.id IN (" ;
+ − 997
while ( $r = $result2 -> fetchRow ()) {
65
+ − 998
$andnot .= $db -> quote ( $r [ "domain_id" ]);
21
+ − 999
if ( $i < $numrows ) {
+ − 1000
$andnot .= "," ;
+ − 1001
$i ++ ;
+ − 1002
}
+ − 1003
}
+ − 1004
$andnot .= ")" ;
+ − 1005
}
+ − 1006
else
+ − 1007
{
+ − 1008
$andnot = "" ;
1
+ − 1009
}
+ − 1010
21
+ − 1011
if ( $letterstart != all && $letterstart != 1 ) {
1
+ − 1012
21
+ − 1013
$sqlq = "SELECT domains.id AS domain_id,
+ − 1014
count(DISTINCT record_owners.record_id) AS aantal,
+ − 1015
domains.name AS domainname
+ − 1016
FROM domains, record_owners,records, zones
65
+ − 1017
WHERE record_owners.user_id = " . $db -> quote ( $_SESSION [ "userid" ]) . "
21
+ − 1018
AND (records.id = record_owners.record_id
+ − 1019
AND domains.id = records.domain_id)
+ − 1020
$andnot
65
+ − 1021
AND domains.name LIKE " . $db -> quote ( $letterstart . "%" ) . "
+ − 1022
AND (zones.domain_id != records.domain_id AND zones.owner!=" . $db -> quote ( $_SESSION [ "userid" ]) . ")
55
+ − 1023
GROUP BY domainname, domains.id
21
+ − 1024
ORDER BY domainname" ;
1
+ − 1025
21
+ − 1026
$result_extra = $db -> query ( $sqlq );
1
+ − 1027
21
+ − 1028
} else {
1
+ − 1029
21
+ − 1030
$sqlq = "SELECT domains.id AS domain_id,
+ − 1031
count(DISTINCT record_owners.record_id) AS aantal,
+ − 1032
domains.name AS domainname
+ − 1033
FROM domains, record_owners,records, zones
65
+ − 1034
WHERE record_owners.user_id = " . $db -> quote ( $_SESSION [ "userid" ]) . "
21
+ − 1035
AND (records.id = record_owners.record_id
+ − 1036
AND domains.id = records.domain_id)
+ − 1037
$andnot
55
+ − 1038
AND substring(domains.name,1,1) " . $sql_regexp . " '^[[:digit:]]'
65
+ − 1039
AND (zones.domain_id != records.domain_id AND zones.owner!=" . $db -> quote ( $_SESSION [ "userid" ]) . ")
55
+ − 1040
GROUP BY domainname, domains.id
21
+ − 1041
ORDER BY domainname" ;
1
+ − 1042
21
+ − 1043
$result_extra [ $i ] = $db -> query ( $sqlq );
+ − 1044
1
+ − 1045
}
+ − 1046
+ − 1047
while ( $r = $result -> fetchRow ())
+ − 1048
{
8
+ − 1049
$r [ "owner" ] = ( $r [ "owner" ] == NULL ) ? $db -> queryOne ( "select min(id) from users where users.level=10" ) : $r [ "owner" ];
1
+ − 1050
$ret [ $r [ "domainname" ]] = array (
+ − 1051
"name" => $r [ "domainname" ],
+ − 1052
"id" => $r [ "domain_id" ],
+ − 1053
"owner" => $r [ "owner" ],
+ − 1054
"numrec" => $r [ "aantal" ]
+ − 1055
);
+ − 1056
}
+ − 1057
+ − 1058
21
+ − 1059
if ( $letterstart != all && $letterstart != 1 ) {
1
+ − 1060
21
+ − 1061
while ( $r = $result_extra -> fetchRow ())
+ − 1062
{
+ − 1063
$ret [ $r [ "domainname" ]] = array (
+ − 1064
"name" => $r [ "domainname" ] . "*" ,
+ − 1065
"id" => $r [ "domain_id" ],
+ − 1066
"owner" => $_SESSION [ "userid" ],
+ − 1067
"numrec" => $r [ "aantal" ]
+ − 1068
);
+ − 1069
$_SESSION [ "partial_" . $r [ "domainname" ]] = 1 ;
+ − 1070
}
1
+ − 1071
21
+ − 1072
} else {
1
+ − 1073
21
+ − 1074
foreach ( $result_extra as $result_e ) {
+ − 1075
while ( $r = $result_e -> fetchRow ())
+ − 1076
{
+ − 1077
$ret [ $r [ "domainname" ]] = array (
+ − 1078
"name" => $r [ "domainname" ] . "*" ,
+ − 1079
"id" => $r [ "domain_id" ],
+ − 1080
"owner" => $_SESSION [ "userid" ],
+ − 1081
"numrec" => $r [ "aantal" ]
+ − 1082
);
+ − 1083
$_SESSION [ "partial_" . $r [ "domainname" ]] = 1 ;
+ − 1084
}
+ − 1085
}
+ − 1086
1
+ − 1087
}
+ − 1088
21
+ − 1089
if ( empty ( $ret )) {
+ − 1090
return - 1 ;
+ − 1091
} else {
+ − 1092
sort ( $ret );
+ − 1093
return $ret ;
+ − 1094
}
1
+ − 1095
+ − 1096
}
+ − 1097
+ − 1098
+ − 1099
/*
30
+ − 1100
* zone_count
+ − 1101
* Does a select query to count how many zones we have in the database
+ − 1102
*
+ − 1103
* @todo: see whether or not it is possible to add the records
+ − 1104
* @param $userid integer The userid of the current user
+ − 1105
* @return integer the number of zones
+ − 1106
*/
+ − 1107
+ − 1108
function zone_count ( $userid = true , $letterstart = all ) {
+ − 1109
global $db ;
55
+ − 1110
global $sql_regexp ;
30
+ − 1111
if (( ! level ( 5 ) || ! $userid ) && ! level ( 10 ) && ! level ( 5 ))
+ − 1112
{
37
+ − 1113
// First select the zones for which we have ownership on one or more records.
65
+ − 1114
$query = 'SELECT records.domain_id FROM records, record_owners WHERE user_id = ' . $db -> quote ( $_SESSION [ 'userid' ]) . ' AND records.id = record_owners.record_id' ;
37
+ − 1115
$result = $db -> query ( $query );
+ − 1116
$zones = array ();
+ − 1117
if ( ! PEAR :: isError ( $result )) {
+ − 1118
$zones = $result -> fetchCol ();
+ − 1119
}
+ − 1120
65
+ − 1121
$add = " AND (zones.owner=" . $db -> quote ( $_SESSION [ "userid" ]);
37
+ − 1122
if ( count ( $zones ) > 0 ) {
+ − 1123
$add .= ' OR zones.domain_id IN (' . implode ( ',' , $zones ) . ') ' ;
+ − 1124
+ − 1125
}
+ − 1126
$add .= ')' ;
30
+ − 1127
}
+ − 1128
else
+ − 1129
{
+ − 1130
$add = "" ;
+ − 1131
}
+ − 1132
+ − 1133
if ( $letterstart != all && $letterstart != 1 ) {
65
+ − 1134
$add .= " AND domains.name LIKE " . $db -> quote ( $letterstart . "%" ) . " " ;
30
+ − 1135
} elseif ( $letterstart == 1 ) {
55
+ − 1136
$add .= " AND substring(domains.name,1,1) " . $sql_regexp . " '^[[:digit:]]'" ;
30
+ − 1137
}
+ − 1138
40
+ − 1139
if ( level ( 5 ))
+ − 1140
{
55
+ − 1141
$query = 'SELECT count(distinct domains.id) as zone_count FROM domains WHERE 1=1 ' . $add ;
40
+ − 1142
}
+ − 1143
else
+ − 1144
{
+ − 1145
$query = 'SELECT count(distinct zones.domain_id) as zone_count FROM zones, domains WHERE zones.domain_id = domains.id ' . $add ;
+ − 1146
}
30
+ − 1147
$numRows = $db -> queryOne ( $query );
+ − 1148
return $numRows ;
+ − 1149
}
+ − 1150
+ − 1151
/*
1
+ − 1152
* Get a record from an id.
+ − 1153
* Retrieve all fields of the record and send it back to the function caller.
+ − 1154
* return values: the array with information, or -1 is nothing is found.
+ − 1155
*/
+ − 1156
function get_record_from_id ( $id )
+ − 1157
{
+ − 1158
global $db ;
+ − 1159
if ( is_numeric ( $id ))
+ − 1160
{
65
+ − 1161
$result = $db -> query ( "SELECT id, domain_id, name, type, content, ttl, prio, change_date FROM records WHERE id=" . $db -> quote ( $id ));
1
+ − 1162
if ( $result -> numRows () == 0 )
+ − 1163
{
+ − 1164
return - 1 ;
+ − 1165
}
+ − 1166
elseif ( $result -> numRows () == 1 )
+ − 1167
{
+ − 1168
$r = $result -> fetchRow ();
+ − 1169
$ret = array (
+ − 1170
"id" => $r [ "id" ],
+ − 1171
"domain_id" => $r [ "domain_id" ],
+ − 1172
"name" => $r [ "name" ],
+ − 1173
"type" => $r [ "type" ],
+ − 1174
"content" => $r [ "content" ],
+ − 1175
"ttl" => $r [ "ttl" ],
+ − 1176
"prio" => $r [ "prio" ],
+ − 1177
"change_date" => $r [ "change_date" ]
+ − 1178
);
+ − 1179
return $ret ;
+ − 1180
}
+ − 1181
else
+ − 1182
{
+ − 1183
error ( sprintf ( ERR_INV_ARGC , "get_record_from_id" , "More than one row returned! This is bad!" ));
+ − 1184
}
+ − 1185
}
+ − 1186
else
+ − 1187
{
+ − 1188
error ( sprintf ( ERR_INV_ARG , "get_record_from_id" ));
+ − 1189
}
+ − 1190
}
+ − 1191
+ − 1192
+ − 1193
/*
+ − 1194
* Get all records from a domain id.
+ − 1195
* Retrieve all fields of the records and send it back to the function caller.
+ − 1196
* return values: the array with information, or -1 is nothing is found.
+ − 1197
*/
+ − 1198
function get_records_from_domain_id ( $id , $rowstart = 0 , $rowamount = 999999 )
+ − 1199
{
+ − 1200
global $db ;
+ − 1201
if ( is_numeric ( $id ))
+ − 1202
{
+ − 1203
if ( $_SESSION [ $id . "_ispartial" ] == 1 ) {
65
+ − 1204
$db -> setLimit ( $rowstart , $rowamount );
1
+ − 1205
$result = $db -> query ( "SELECT record_owners.record_id as id
+ − 1206
FROM record_owners,domains,records
65
+ − 1207
WHERE record_owners.user_id = " . $db -> quote ( $_SESSION [ "userid" ]) . "
1
+ − 1208
AND record_owners.record_id = records.id
65
+ − 1209
AND records.domain_id = " . $db -> quote ( $id ) . "
+ − 1210
GROUP bY record_owners.record_id" );
1
+ − 1211
+ − 1212
$ret = array ();
+ − 1213
if ( $result -> numRows () == 0 )
+ − 1214
{
+ − 1215
return - 1 ;
+ − 1216
}
+ − 1217
else
+ − 1218
{
+ − 1219
$ret [] = array ();
+ − 1220
$retcount = 0 ;
+ − 1221
while ( $r = $result -> fetchRow ())
+ − 1222
{
+ − 1223
// Call get_record_from_id for each row.
+ − 1224
$ret [ $retcount ] = get_record_from_id ( $r [ "id" ]);
+ − 1225
$retcount ++ ;
+ − 1226
}
+ − 1227
return $ret ;
+ − 1228
}
+ − 1229
+ − 1230
} else {
65
+ − 1231
$db -> setLimit ( $rowstart , $rowamount );
+ − 1232
$result = $db -> query ( "SELECT id FROM records WHERE domain_id=" . $db -> quote ( $id ));
1
+ − 1233
$ret = array ();
+ − 1234
if ( $result -> numRows () == 0 )
+ − 1235
{
+ − 1236
return - 1 ;
+ − 1237
}
+ − 1238
else
+ − 1239
{
+ − 1240
$ret [] = array ();
+ − 1241
$retcount = 0 ;
+ − 1242
while ( $r = $result -> fetchRow ())
+ − 1243
{
+ − 1244
// Call get_record_from_id for each row.
+ − 1245
$ret [ $retcount ] = get_record_from_id ( $r [ "id" ]);
+ − 1246
$retcount ++ ;
+ − 1247
}
+ − 1248
return $ret ;
+ − 1249
}
+ − 1250
+ − 1251
}
+ − 1252
}
+ − 1253
else
+ − 1254
{
+ − 1255
error ( sprintf ( ERR_INV_ARG , "get_records_from_domain_id" ));
+ − 1256
}
+ − 1257
}
+ − 1258
+ − 1259
+ − 1260
function get_users_from_domain_id ( $id )
+ − 1261
{
+ − 1262
global $db ;
65
+ − 1263
$result = $db -> queryCol ( "SELECT owner FROM zones WHERE domain_id=" . $db -> quote ( $id ));
1
+ − 1264
$ret = array ();
+ − 1265
foreach ( $result as $uid )
+ − 1266
{
65
+ − 1267
$fullname = $db -> queryOne ( "SELECT fullname FROM users WHERE id=" . $db -> quote ( $uid ));
1
+ − 1268
$ret [] = array (
+ − 1269
"id" => $uid ,
+ − 1270
"fullname" => $fullname
+ − 1271
);
+ − 1272
}
+ − 1273
return $ret ;
+ − 1274
}
+ − 1275
+ − 1276
function search_record ( $question )
+ − 1277
{
+ − 1278
global $db ;
+ − 1279
$question = trim ( $question );
62
+ − 1280
+ − 1281
if ( is_valid_search ( $question ))
1
+ − 1282
{
62
+ − 1283
$sqlq = "SELECT *
+ − 1284
FROM records
65
+ − 1285
WHERE content LIKE " . $db -> quote ( $question ) . "
+ − 1286
OR name LIKE " . $db -> quote ( $question ) . "
62
+ − 1287
ORDER BY type DESC" ;
+ − 1288
$result = $db -> query ( $sqlq );
+ − 1289
$ret_r = array ();
+ − 1290
while ( $r = $result -> fetchRow ())
+ − 1291
{
+ − 1292
if ( xs ( $r [ 'domain_id' ]))
+ − 1293
{
+ − 1294
$ret_r [] = array (
+ − 1295
'id' => $r [ 'id' ],
+ − 1296
'domain_id' => $r [ 'domain_id' ],
+ − 1297
'name' => $r [ 'name' ],
+ − 1298
'type' => $r [ 'type' ],
+ − 1299
'content' => $r [ 'content' ],
+ − 1300
'ttl' => $r [ 'ttl' ],
+ − 1301
'prio' => $r [ 'prio' ],
+ − 1302
'change_date' => $r [ 'change_date' ]
+ − 1303
);
+ − 1304
}
+ − 1305
}
+ − 1306
+ − 1307
$sqlq = "SELECT domains.id, domains.name, count(records.id) AS numrec, zones.owner, records.domain_id
+ − 1308
FROM domains, records, zones
+ − 1309
WHERE domains.id = records.domain_id
+ − 1310
AND zones.domain_id = domains.id
65
+ − 1311
AND domains.name LIKE " . $db -> quote ( $question ) . "
62
+ − 1312
GROUP BY domains.id, domains.name, zones.owner, records.domain_id" ;
+ − 1313
$result = $db -> query ( $sqlq );
+ − 1314
$ret_d = array ();
+ − 1315
while ( $r = $result -> fetchRow ())
+ − 1316
{
+ − 1317
if ( xs ( $r [ 'domain_id' ]))
+ − 1318
{
+ − 1319
$ret_d [] = array (
+ − 1320
'id' => $r [ 'id' ],
+ − 1321
'name' => $r [ 'name' ],
+ − 1322
'numrec' => $r [ 'numrec' ],
+ − 1323
'owner' => $r [ 'owner' ]
+ − 1324
);
+ − 1325
}
+ − 1326
}
+ − 1327
return array ( 'domains' => $ret_d , 'records' => $ret_r );
+ − 1328
}
+ − 1329
else
+ − 1330
{
+ − 1331
error ( sprintf ( ERR_INV_ARGC , "search_record" , "Invalid searchstring: $question " ));
1
+ − 1332
}
+ − 1333
+ − 1334
}
+ − 1335
+ − 1336
function get_domain_type ( $id )
+ − 1337
{
+ − 1338
global $db ;
13
+ − 1339
if ( is_numeric ( $id ))
1
+ − 1340
{
65
+ − 1341
$type = $db -> queryOne ( "SELECT type FROM domains WHERE id = " . $db -> quote ( $id ));
13
+ − 1342
if ( $type == "" )
+ − 1343
{
+ − 1344
$type = "NATIVE" ;
+ − 1345
}
+ − 1346
return $type ;
+ − 1347
}
+ − 1348
else
+ − 1349
{
+ − 1350
error ( sprintf ( ERR_INV_ARG , "get_record_from_id" , "no or no valid zoneid given" ));
+ − 1351
}
+ − 1352
}
+ − 1353
+ − 1354
function get_domain_slave_master ( $id )
+ − 1355
{
+ − 1356
global $db ;
+ − 1357
if ( is_numeric ( $id ))
+ − 1358
{
65
+ − 1359
$slave_master = $db -> queryOne ( "SELECT master FROM domains WHERE type = 'SLAVE' and id = " . $db -> quote ( $id ));
13
+ − 1360
return $slave_master ;
+ − 1361
}
+ − 1362
else
+ − 1363
{
+ − 1364
error ( sprintf ( ERR_INV_ARG , "get_domain_slave_master" , "no or no valid zoneid given" ));
+ − 1365
}
1
+ − 1366
}
+ − 1367
+ − 1368
function change_domain_type ( $type , $id )
+ − 1369
{
13
+ − 1370
global $db ;
+ − 1371
unset ( $add );
+ − 1372
if ( is_numeric ( $id ))
+ − 1373
{
+ − 1374
// It is not really neccesary to clear the master field if a
+ − 1375
// zone is not of the type "slave" as powerdns will ignore that
+ − 1376
// fiedl, but it is cleaner anyway.
+ − 1377
if ( $type != "SLAVE" )
+ − 1378
{
+ − 1379
$add = ", master=''" ;
+ − 1380
}
65
+ − 1381
$result = $db -> query ( "UPDATE domains SET type = " . $db -> quote ( $type ) . $add . " WHERE id = " . $db -> quote ( $id ));
13
+ − 1382
}
+ − 1383
else
+ − 1384
{
+ − 1385
error ( sprintf ( ERR_INV_ARG , "change_domain_type" , "no or no valid zoneid given" ));
+ − 1386
}
+ − 1387
}
+ − 1388
+ − 1389
function change_domain_slave_master ( $id , $slave_master )
+ − 1390
{
+ − 1391
global $db ;
+ − 1392
if ( is_numeric ( $id ))
+ − 1393
{
+ − 1394
if ( is_valid_ip ( $slave_master ) || is_valid_ip6 ( $slave_master ))
+ − 1395
{
65
+ − 1396
$result = $db -> query ( "UPDATE domains SET master = " . $db -> quote ( $slave_master ) . " WHERE id = " . $db -> quote ( $id ));
13
+ − 1397
}
+ − 1398
else
+ − 1399
{
+ − 1400
error ( sprintf ( ERR_INV_ARGC , "change_domain_slave_master" , "This is not a valid IPv4 or IPv6 address: $slave_master " ));
+ − 1401
}
+ − 1402
}
+ − 1403
else
+ − 1404
{
+ − 1405
error ( sprintf ( ERR_INV_ARG , "change_domain_type" , "no or no valid zoneid given" ));
+ − 1406
}
+ − 1407
}
+ − 1408
+ − 1409
+ − 1410
function validate_account ( $account )
+ − 1411
{
+ − 1412
+ − 1413
if ( preg_match ( "/^[A-Z0-9._-]+$/i" , $account ))
+ − 1414
{
+ − 1415
return true ;
+ − 1416
}
+ − 1417
else
+ − 1418
{
+ − 1419
return false ;
+ − 1420
}
1
+ − 1421
}
+ − 1422
?>