inc/record.inc.php
author rejo
Tue, 10 Apr 2007 22:40:43 +0000
changeset 1 58094faf794d
child 5 ebf8868e99f5
permissions -rwxr-xr-x
[feladat @ 2] Creation of initial working branch.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     1
<?
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     2
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     3
// +--------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     4
// | PowerAdmin                                                         |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     5
// +--------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     6
// | Copyright (c) 1997-2002 The PowerAdmin Team                        |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     7
// +--------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     8
// | This source file is subject to the license carried by the overal   |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     9
// | program PowerAdmin as found on http://poweradmin.sf.net            |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    10
// | The PowerAdmin program falls under the QPL License:                |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    11
// | http://www.trolltech.com/developer/licensing/qpl.html              |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    12
// +--------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    13
// | Authors: Roeland Nieuwenhuis <trancer <AT> trancer <DOT> nl>       |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    14
// |          Sjeemz <sjeemz <AT> sjeemz <DOT> nl>                      |
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    15
// +--------------------------------------------------------------------+
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    16
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    17
// Filename: record.inc.php
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    18
// Startdate: 26-10-2002
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    19
// This file is ought to edit the records in the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    20
// Records are domains aswell, they also belong here.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    21
// All database functions are associative.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    22
// use nextID first to get a new id. Then insert it into the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    23
// do not rely on auto_increment (see above).
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    24
// use dns.inc.php for validation functions.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    25
//
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    26
// $Id: record.inc.php,v 1.21 2003/05/10 20:21:01 azurazu Exp $
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    27
//
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    28
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    29
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    30
function update_soa_serial($domain_id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    31
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    32
    global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    33
	/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    34
	 * THIS CODE ISNT TESTED THROUGH MUCH YET!
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    35
	 * !!!!!!! BETACODE !!!!!!!!!!
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    36
	 * Code committed by DeViCeD, Thanks a lot!
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    37
	 * Heavily hax0red by Trancer/azurazu
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    38
	 *
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    39
	 * First we have to check, wheather current searial number 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    40
	 * was already updated on the other nameservers.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    41
	 * If field 'notified_serial' is NULL, then I guess domain is
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    42
	 * NATIVE and we don't have any secondary nameservers for this domain.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    43
	 * NOTICE: Serial number *will* be RFC1912 compilant after update 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    44
	 * NOTICE: This function will allow only 100 DNS zone transfers ;-)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    45
	 * YYYYMMDDnn
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    46
	 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    47
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    48
	$sqlq = "SELECT `notified_serial` FROM `domains` WHERE `id` = '".$domain_id."'";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    49
	$notified_serial = $db->getOne($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    50
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    51
	$sqlq = "SELECT `content` FROM `records` WHERE `type` = 'SOA' AND `domain_id` = '".$domain_id."'";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    52
	$content = $db->getOne($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    53
    $need_to_update = false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    54
	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    55
	// Getting the serial field.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    56
	$soa = explode(" ", $content);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    57
	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    58
	if(empty($notified_serial))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    59
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    60
        // Ok native replication, so we have to update.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    61
        $need_to_update = true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    62
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    63
    elseif($notified_serial >= $soa[2])
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    64
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    65
        $need_to_update = true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    66
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    67
    elseif(strlen($soa[2]) != 10)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    68
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    69
        $need_to_update = true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    70
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    71
    else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    72
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    73
        $need_to_update = false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    74
    }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    75
    if($need_to_update)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    76
    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    77
        // Ok so we have to update it seems.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    78
        $current_serial = $soa[2];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    79
        
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    80
		/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    81
		 * What we need here (for RFC1912) is YEAR, MONTH and DAY
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    82
		 * so let's get it ...
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    83
		 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    84
		$new_serial = date('Ymd'); // we will add revision number later
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    85
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    86
		if(strncmp($new_serial, $current_serial, 8) === 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    87
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    88
            /*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    89
             * Ok, so we already made updates tonight
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    90
             * let's just increase the revision number
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    91
             */				
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    92
            $revision_number = (int) substr($current_serial, -2);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    93
            if ($revision_number == 99) return false; // ok, we cannot update anymore tonight
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    94
            ++$revision_number;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    95
            // here it is ... same date, new revision
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    96
            $new_serial .= str_pad($revision_number, 2, "0", STR_PAD_LEFT);	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    97
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    98
 		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    99
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   100
            /*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   101
			 * Current serial is not RFC1912 compilant, so let's make a new one
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   102
			 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   103
 			$new_serial .= '00';
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   104
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   105
        $soa[2] = $new_serial; // change serial in SOA array
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   106
		$new_soa = "";		
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   107
		// build new soa and update SQL after that
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   108
		for ($i = 0; $i < count($soa); $i++) 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   109
		{	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   110
			$new_soa .= $soa[$i] . " "; 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   111
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   112
		$sqlq = "UPDATE `records` SET `content` = '".$new_soa."' WHERE `domain_id` = '".$domain_id."' AND `type` = 'SOA' LIMIT 1";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   113
		$db->Query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   114
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   115
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   116
}  
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   117
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   118
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   119
 * Edit a record.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   120
 * This function validates it if correct it inserts it into the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   121
 * return values: true if succesful.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   122
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   123
function edit_record($recordid, $zoneid, $name, $type, $content, $ttl, $prio)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   124
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   125
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   126
  	if($content == "")
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   127
  	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   128
  		error(ERR_RECORD_EMPTY_CONTENT);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   129
  	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   130
  	// Edits the given record (validates specific stuff first)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   131
	if (!xs(recid_to_domid($recordid)))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   132
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   133
		error(ERR_RECORD_ACCESS_DENIED);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   134
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   135
	if (is_numeric($zoneid))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   136
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   137
		validate_input($recordid, $zoneid, $type, $content, $name, $prio, $ttl);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   138
                $change = time();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   139
                $db->query("UPDATE records set name='$name', type='$type', content='$content', ttl='$ttl', prio='$prio', change_date='$change' WHERE id=$recordid");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   140
		
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   141
		/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   142
		 * Added by DeViCeD - Update SOA Serial number
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   143
		 * There should be more checks
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   144
		 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   145
		if ($type != 'SOA')
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   146
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   147
			update_soa_serial($zoneid);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   148
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   149
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   150
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   151
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   152
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   153
		error(sprintf(ERR_INV_ARGC, "edit_record", "no zoneid given"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   154
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   155
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   156
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   157
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   158
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   159
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   160
 * Adds a record.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   161
 * This function validates it if correct it inserts it into the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   162
 * return values: true if succesful.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   163
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   164
function add_record($zoneid, $name, $type, $content, $ttl, $prio)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   165
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   166
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   167
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   168
	if (!xs($zoneid))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   169
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   170
		error(ERR_RECORD_ACCESS_DENIED);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   171
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   172
	if (is_numeric($zoneid))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   173
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   174
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   175
		// Get the Insert ID, we can not rely on auto_increment
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   176
		$idrecords = $db->nextID('records');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   177
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   178
		// Check the user input.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   179
		validate_input($idrecords, $zoneid, $type, $content, $name, $prio, $ttl);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   180
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   181
		// Generate new timestamp for the Daemon
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   182
		$change = time();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   183
		// Execute query.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   184
		$db->query("INSERT INTO records (id, domain_id, name, type, content, ttl, prio, change_date) VALUES ($idrecords, $zoneid, '$name', '$type', '$content', $ttl, '$prio', $change)");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   185
		if ($type != 'SOA')
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   186
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   187
			update_soa_serial($zoneid);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   188
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   189
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   190
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   191
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   192
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   193
		error(sprintf(ERR_INV_ARG, "add_record"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   194
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   195
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   196
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   197
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   198
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   199
 * Delete a record by a given id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   200
 * return values: true, this function is always succesful.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   201
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   202
function delete_record($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   203
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   204
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   205
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   206
	// Check if the user has access.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   207
	if (!xs(recid_to_domid($id)))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   208
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   209
		error(ERR_RECORD_ACCESS_DENIED);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   210
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   211
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   212
	// Retrieve the type of record to see if we can actually remove it.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   213
	$recordtype = get_recordtype_from_id($id);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   214
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   215
	// If the record type is NS and the user tries to delete it while ALLOW_NS_EDIT is set to 0
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   216
	// OR
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   217
	// check if the name of the record isnt the domain name (if so it should delete all records)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   218
	// OR
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   219
	// check if we are dealing with a SOA field (same story as NS)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   220
	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))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   221
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   222
		error(sprintf(ERR_RECORD_DELETE_TYPE_DENIED, $recordtype));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   223
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   224
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   225
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   226
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   227
	    $did = recid_to_domid($id);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   228
		$db->query('DELETE FROM records WHERE id=' . $id . ' LIMIT 1');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   229
		if ($type != 'SOA')
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   230
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   231
			update_soa_serial($did);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   232
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   233
        // $id doesnt exist in database anymore so its deleted or just not there which means "true"	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   234
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   235
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   236
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   237
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   238
		error(sprintf(ERR_INV_ARG, "delete_record"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   239
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   240
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   241
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   242
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   243
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   244
 * Add a domain to the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   245
 * A domain is name obligatory, so is an owner.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   246
 * return values: true when succesful.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   247
 * Empty means templates dont have to be applied.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   248
 * --------------------------------------------------------------------------
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   249
 * This functions eats a template and by that it inserts various records.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   250
 * first we start checking if something in an arpa record
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   251
 * remember to request nextID's from the database to be able to insert record.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   252
 * if anything is invalid the function will error
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   253
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   254
function add_domain($domain, $owner, $webip, $mailip, $empty, $type)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   255
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   256
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   257
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   258
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   259
	if (!level(5))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   260
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   261
		error(ERR_LEVEL_5);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   262
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   263
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   264
	// If domain, owner and mailip are given
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   265
	// OR
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   266
	// empty is given and owner and domain
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   267
	// OR
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   268
	// the domain is an arpa record and owner is given
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   269
	// THAN
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   270
	// Continue this function
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   271
	if (($domain && $owner && $webip && $mailip) || ($empty && $owner && $domain) || (eregi('in-addr.arpa', $domain) && $owner))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   272
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   273
		// Retrieve next zones id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   274
		$idzones = $db->nextID('zones');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   275
		$iddomains = $db->nextID('domains');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   276
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   277
		$db->query("INSERT INTO zones (id, domain_id, owner) VALUES ('$idzones', '$iddomains', $owner)");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   278
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   279
		/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   280
		 * TODO : NATIVE OPERATION ONLY FOR NOW
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   281
		 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   282
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   283
		$db->query("INSERT INTO domains (id, name, type) VALUES ('$iddomains', '$domain', '$type')");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   284
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   285
		// Generate new timestamp. We need this one anyhow.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   286
		$now = time();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   287
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   288
		if ($empty && $iddomains)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   289
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   290
			// If we come into this if statement we dont want to apply templates.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   291
			// Retrieve configuration settings.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   292
			$ns1 = $GLOBALS["NS1"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   293
			$hm  = $GLOBALS["HOSTMASTER"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   294
			$ttl = $GLOBALS["DEFAULT_TTL"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   295
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   296
			// Retrieve next records id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   297
			$idrecords = $db->nextID('records');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   298
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   299
			// Build and execute query
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   300
			$sql = "INSERT INTO records (id, domain_id, name, content, type, ttl, prio, change_date) VALUES ('$idrecords', '$iddomains', '$domain', '$ns1 $hm 1', 'SOA', $ttl, '', '$now')";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   301
			$db->query($sql);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   302
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   303
			// Done
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   304
			return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   305
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   306
		elseif ($iddomains)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   307
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   308
			// If we are here we want to apply templates.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   309
			global $template;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   310
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   311
			// Iterate over the template and apply it for each field.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   312
			foreach ($template as $r)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   313
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   314
				// Same type of if statement as previous.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   315
				if ((eregi('in-addr.arpa', $domain) && ($r["type"] == "NS" || $r["type"] == "SOA")) || (!eregi('in-addr.arpa', $domain)))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   316
				{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   317
					// Parse the template.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   318
					$name     = parse_template_value($r["name"], $domain, $webip, $mailip);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   319
					$type     = $r["type"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   320
					$content  = parse_template_value($r["content"], $domain, $webip, $mailip);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   321
					$ttl      = $r["ttl"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   322
					$prio     = $r["prio"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   323
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   324
					// If no ttl is given, use the default.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   325
					if (!$ttl)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   326
					{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   327
						$ttl = $GLOBALS["DEFAULT_TTL"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   328
					}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   329
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   330
					// Retrieve new record id;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   331
					$idrecords = $db->nextID('records');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   332
					$sql = "INSERT INTO records (id, domain_id, name, content, type, ttl, prio, change_date) VALUES ('$idrecords', '$iddomains', '$name','$content','$type','$ttl','$prio','$now')";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   333
					$db->query($sql);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   334
				}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   335
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   336
			// All done.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   337
			return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   338
                 }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   339
                 else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   340
                 {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   341
			error(sprintf(ERR_INV_ARGC, "add_domain", "could not create zone"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   342
                 }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   343
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   344
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   345
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   346
		error(sprintf(ERR_INV_ARG, "add_domain"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   347
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   348
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   349
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   350
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   351
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   352
 * Deletes a domain by a given id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   353
 * Function always succeeds. If the field is not found in the database, thats what we want anyway.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   354
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   355
function delete_domain($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   356
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   357
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   358
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   359
	if (!level(5))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   360
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   361
		error(ERR_LEVEL_5);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   362
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   363
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   364
	// See if the ID is numeric.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   365
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   366
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   367
		$db->query("DELETE FROM zones WHERE domain_id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   368
		$db->query("DELETE FROM domains WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   369
		$db->query("DELETE FROM records WHERE domain_id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   370
		// Nothing in the database. If the delete deleted 0 records it means the id is just not there.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   371
		// therefore the is no need to check the affectedRows values.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   372
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   373
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   374
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   375
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   376
		error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   377
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   378
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   379
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   380
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   381
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   382
 * Gets the id of the domain by a given record id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   383
 * return values: the domain id that was requested.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   384
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   385
function recid_to_domid($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   386
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   387
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   388
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   389
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   390
		$result = $db->query("SELECT domain_id FROM records WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   391
		$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   392
		return $r["domain_id"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   393
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   394
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   395
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   396
		error(sprintf(ERR_INV_ARGC, "recid_to_domid", "id must be a number"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   397
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   398
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   399
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   400
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   401
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   402
 * Sorts a zone by records.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   403
 * return values: the sorted zone.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   404
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   405
function sort_zone($records)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   406
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   407
	$ar_so = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   408
	$ar_ns = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   409
	$ar_mx = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   410
	$ar_mb = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   411
	$ar_ur = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   412
	$ar_ov = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   413
	foreach ($records as $c)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   414
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   415
		switch(strtoupper($c['type']))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   416
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   417
			case "SOA":
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   418
				$ar_so[] = $c;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   419
				break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   420
			case "NS":
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   421
				$ar_ns[] = $c;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   422
				break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   423
			case "MX":
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   424
				$ar_mx[] = $c;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   425
				break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   426
			case "MBOXFW":
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   427
				$ar_mb[] = $c;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   428
				break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   429
			case "URL":
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   430
				$ar_ur[] = $c;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   431
				break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   432
			default:
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   433
				$ar_ov[] = $c;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   434
				break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   435
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   436
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   437
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   438
	$res = array_merge($ar_so, $ar_ns, $ar_mx, $ar_mb, $ar_ur, $ar_ov);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   439
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   440
	if (count($records) == count($res))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   441
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   442
		$records = $res;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   443
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   444
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   445
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   446
		error(sprintf(ERR_INV_ARGC, "sort_zone", "records sorting failed!"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   447
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   448
	return $records;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   449
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   450
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   451
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   452
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   453
 * Change owner of a domain.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   454
 * Function should actually be in users.inc.php. But its more of a record modification than a user modification
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   455
 * return values: true when succesful.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   456
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   457
function add_owner($domain, $newowner)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   458
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   459
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   460
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   461
	if (!level(5))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   462
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   463
		error(ERR_LEVEL_5);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   464
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   465
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   466
	if (is_numeric($domain) && is_numeric($newowner) && is_valid_user($newowner))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   467
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   468
		$iid = $db->nextID('zones');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   469
		if($db->getOne("SELECT COUNT(id) FROM zones WHERE owner=$newowner AND domain_id=$domain") == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   470
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   471
			$db->query("INSERT INTO zones(id, domain_id, owner) VALUES($iid, $domain, $newowner)");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   472
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   473
		return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   474
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   475
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   476
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   477
		error(sprintf(ERR_INV_ARGC, "change_owner", "$domain / $newowner"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   478
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   479
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   480
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   481
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   482
function delete_owner($domain, $owner)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   483
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   484
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   485
	if($db->getOne("SELECT COUNT(id) FROM zones WHERE owner=$owner AND domain_id=$domain") != 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   486
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   487
		$db->query("DELETE FROM zones WHERE owner=$owner AND domain_id=$domain");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   488
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   489
	return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   490
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   491
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   492
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   493
 * Retrieves all supported dns record types
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   494
 * This function might be deprecated.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   495
 * return values: array of types in string form.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   496
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   497
function get_record_types()
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   498
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   499
	global $rtypes;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   500
	return $rtypes;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   501
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   502
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   503
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   504
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   505
 * Retrieve all records by a given type and domain id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   506
 * Example: get all records that are of type A from domain id 1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   507
 * return values: a DB class result object
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   508
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   509
function get_records_by_type_from_domid($type, $recid)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   510
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   511
	global $rtypes;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   512
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   513
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   514
	// Does this type exist?
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   515
	if(!in_array(strtoupper($type), $rtypes))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   516
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   517
		error(sprintf(ERR_INV_ARGC, "get_records_from_type", "this is not a supported record"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   518
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   519
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   520
	// Get the domain id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   521
	$domid = recid_to_domid($recid);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   522
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   523
	$result = $db->query("select id, type from records where domain_id=$recid and type='$type'");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   524
	return $result;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   525
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   526
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   527
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   528
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   529
 * Retrieves the type of a record from a given id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   530
 * return values: the type of the record (one of the records types in $rtypes assumable).
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   531
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   532
function get_recordtype_from_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   533
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   534
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   535
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   536
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   537
		$result = $db->query("SELECT type FROM records WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   538
		$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   539
		return $r["type"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   540
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   541
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   542
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   543
		error(sprintf(ERR_INV_ARG, "get_recordtype_from_id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   544
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   545
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   546
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   547
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   548
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   549
 * Retrieves the name (e.g. bla.test.com) of a record by a given id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   550
 * return values: the name associated with the id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   551
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   552
function get_name_from_record_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   553
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   554
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   555
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   556
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   557
		$result = $db->query("SELECT name FROM records WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   558
		$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   559
		return $r["name"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   560
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   561
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   562
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   563
		error(sprintf(ERR_INV_ARG, "get_name_from_record_id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   564
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   565
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   566
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   567
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   568
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   569
 * Get all the domains from a database of which the user is the owner.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   570
 * return values: an array with the id of the domain and its name.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   571
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   572
function get_domains_from_userid($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   573
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   574
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   575
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   576
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   577
		$result = $db->query("SELECT domains.id AS domain_id, domains.name AS name FROM domains LEFT JOIN zones ON domains.id=zones.domain_id WHERE owner=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   578
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   579
		$ret = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   580
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   581
		// Put all the information in a big array.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   582
		while ($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   583
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   584
			$ret[] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   585
			"id"            =>              $r["domain_id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   586
			"name"          =>              $r["name"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   587
			);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   588
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   589
		return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   590
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   591
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   592
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   593
		error(sprintf(ERR_INV_ARGC, "get_domains_from_userid", "This is not a valid userid: $id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   594
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   595
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   596
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   597
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   598
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   599
 * Get domain name from a given id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   600
 * return values: the name of the domain associated with the id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   601
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   602
function get_domain_name_from_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   603
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   604
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   605
	if (!xs($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   606
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   607
		error(ERR_RECORD_ACCESS_DENIED);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   608
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   609
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   610
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   611
		$result = $db->query("SELECT name FROM domains WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   612
		if ($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   613
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   614
 			$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   615
 			return $r["name"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   616
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   617
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   618
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   619
	 		error(sprintf(ERR_INV_ARGC, "get_domain_name_from_id", "more than one domain found?! whaaa! BAD! BAD! Contact admin!"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   620
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   621
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   622
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   623
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   624
		error(sprintf(ERR_INV_ARGC, "get_domain_name_from_id", "Not a valid domainid: $id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   625
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   626
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   627
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   628
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   629
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   630
 * Get information about a domain name from a given domain id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   631
 * the function looks up the domainname, the owner of the domain and the number of records in it.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   632
 * return values: an array containing the information.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   633
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   634
function get_domain_info_from_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   635
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   636
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   637
	if (!xs($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   638
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   639
		error(ERR_RECORD_ACCESS_DENIED);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   640
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   641
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   642
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   643
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   644
	if ($_SESSION[$id."_ispartial"] == 1) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   645
	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   646
	$sqlq = "SELECT domains.name AS name,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   647
	users.fullname AS owner,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   648
	count(record_owners.id) AS aantal
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   649
	FROM domains, users, record_owners, records
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   650
	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   651
        WHERE record_owners.user_id = ".$_SESSION["userid"]."
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   652
        AND record_owners.record_id = records.id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   653
	AND records.domain_id = ".$id."
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   654
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   655
	GROUP BY name, owner, users.fullname
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   656
	ORDER BY name";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   657
	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   658
	$result = $db->getRow($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   659
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   660
	$ret = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   661
	"name"          =>              $result["name"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   662
	"ownerid"       =>              $_SESSION["userid"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   663
	"owner"         =>              $result["owner"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   664
	"numrec"        =>              $result["aantal"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   665
	);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   666
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   667
	return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   668
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   669
	} else{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   670
	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   671
		// Query that retrieves the information we need.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   672
		$sqlq = "SELECT domains.name AS name,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   673
			min(zones.owner) AS ownerid,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   674
			users.fullname AS owner,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   675
			count(records.domain_id) AS aantal
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   676
			FROM domains
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   677
			LEFT JOIN records ON domains.id=records.domain_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   678
			LEFT JOIN zones ON domains.id=zones.domain_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   679
			LEFT JOIN users ON zones.owner=users.id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   680
			WHERE domains.id=$id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   681
			GROUP BY name, owner, users.fullname
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   682
			ORDER BY zones.id";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   683
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   684
		// Put the first occurence in an array and return it.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   685
		$result = $db->getRow($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   686
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   687
		//$result["ownerid"] = ($result["ownerid"] == NULL) ? $db->getOne("select min(id) from users where users.level=10") : $result["ownerid"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   688
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   689
		$ret = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   690
		"name"          =>              $result["name"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   691
		"ownerid"       =>              $result["ownerid"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   692
		"owner"         =>              $result["owner"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   693
		"numrec"        =>              $result["aantal"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   694
		);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   695
		return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   696
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   697
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   698
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   699
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   700
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   701
		error(sprintf(ERR_INV_ARGC, "get_domain_num_records_from_id", "This is not a valid domainid: $id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   702
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   703
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   704
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   705
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   706
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   707
 * Check if a domain is already existing.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   708
 * return values: true if existing, false if it doesnt exist.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   709
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   710
function domain_exists($domain)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   711
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   712
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   713
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   714
	if (!level(5))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   715
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   716
		error(ERR_LEVEL_5);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   717
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   718
	if (is_valid_domain($domain))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   719
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   720
		$result = $db->query("SELECT id FROM domains WHERE name='$domain'");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   721
		if ($result->numRows() == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   722
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   723
			return false;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   724
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   725
		elseif ($result->numRows() >= 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   726
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   727
			return true;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   728
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   729
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   730
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   731
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   732
		error(ERR_DOMAIN_INVALID);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   733
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   734
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   735
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   736
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   737
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   738
 * Get all domains from the database.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   739
 * This function gets all the domains from the database unless a user id is below 5.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   740
 * if a user id is below 5 this function will only retrieve records for that user.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   741
 * return values: the array of domains or -1 if nothing is found.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   742
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   743
function get_domains($userid=true,$letterstart=all,$rowstart=0,$rowamount=999999)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   744
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   745
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   746
	if((!level(5) || !$userid) && !level(10) && !level(5))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   747
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   748
		$add = " AND zones.owner=".$_SESSION["userid"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   749
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   750
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   751
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   752
		$add = "";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   753
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   754
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   755
	$sqlq = "SELECT domains.id AS domain_id,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   756
	min(zones.owner) AS owner,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   757
	count(DISTINCT records.id) AS aantal,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   758
	domains.name AS domainname
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   759
	FROM domains
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   760
	LEFT JOIN zones ON domains.id=zones.domain_id 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   761
	LEFT JOIN records ON records.domain_id=domains.id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   762
	WHERE 1 $add ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   763
	if ($letterstart!=all && $letterstart!=1) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   764
	   $sqlq.=" AND domains.name LIKE '".$letterstart."%' ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   765
	} elseif ($letterstart==1) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   766
	      $sqlq.=" AND ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   767
	   for ($i=0;$i<=9;$i++) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   768
	      $sqlq.="domains.name LIKE '".$i."%'";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   769
	      if ($i!=9) $sqlq.=" OR ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   770
	   }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   771
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   772
	$sqlq.=" GROUP BY domainname, domain_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   773
	ORDER BY domainname
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   774
	LIMIT $rowstart,$rowamount";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   775
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   776
	$result = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   777
	$result2 = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   778
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   779
	$andnot="";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   780
	while($r = $result2->fetchRow()) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   781
	   $andnot.=" AND domains.id!= '".$r["domain_id"]."' ";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   782
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   783
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   784
if ($letterstart!=all && $letterstart!=1) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   785
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   786
	$sqlq = "SELECT domains.id AS domain_id,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   787
	count(DISTINCT record_owners.record_id) AS aantal,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   788
	domains.name AS domainname
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   789
	FROM domains, record_owners,records, zones
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   790
	WHERE record_owners.user_id = '".$_SESSION["userid"]."'
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   791
	AND (records.id = record_owners.record_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   792
	AND domains.id = records.domain_id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   793
	$andnot 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   794
	AND domains.name LIKE '".$letterstart."%' 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   795
	AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."')
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   796
	GROUP BY domainname, domain_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   797
	ORDER BY domainname";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   798
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   799
	$result_extra = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   800
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   801
} else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   802
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   803
   for ($i=0;$i<=9;$i++) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   804
        $sqlq = "SELECT domains.id AS domain_id,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   805
        count(DISTINCT record_owners.record_id) AS aantal,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   806
        domains.name AS domainname
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   807
        FROM domains, record_owners,records, zones
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   808
        WHERE record_owners.user_id = '".$_SESSION["userid"]."'
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   809
        AND (records.id = record_owners.record_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   810
        AND domains.id = records.domain_id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   811
        $andnot 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   812
        AND domains.name LIKE '".$i."%' 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   813
        AND (zones.domain_id != records.domain_id AND zones.owner!='".$_SESSION["userid"]."')
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   814
        GROUP BY domainname, domain_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   815
        ORDER BY domainname";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   816
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   817
        $result_extra[$i] = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   818
   }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   819
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   820
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   821
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   822
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   823
	if ($result->numRows() == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   824
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   825
		// Nothing found.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   826
		return -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   827
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   828
*/
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   829
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   830
	while($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   831
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   832
		$r["owner"] = ($r["owner"] == NULL) ? $db->getOne("select min(id) from users where users.level=10") : $r["owner"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   833
	     	$ret[$r["domainname"]] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   834
		"name"          =>              $r["domainname"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   835
		"id"            =>              $r["domain_id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   836
		"owner"         =>              $r["owner"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   837
		"numrec"        =>              $r["aantal"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   838
		);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   839
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   840
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   841
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   842
if ($letterstart!=all && $letterstart!=1) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   843
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   844
	while($r = $result_extra->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   845
        {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   846
               $ret[$r["domainname"]] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   847
	       "name"          =>              $r["domainname"]."*",
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   848
               "id"            =>              $r["domain_id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   849
               "owner"         =>              $_SESSION["userid"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   850
               "numrec"        =>              $r["aantal"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   851
               );
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   852
	       $_SESSION["partial_".$r["domainname"]] = 1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   853
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   854
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   855
} else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   856
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   857
	foreach ($result_extra as $result_e) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   858
        while($r = $result_e->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   859
        {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   860
               $ret[$r["domainname"]] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   861
               "name"          =>              $r["domainname"]."*",
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   862
               "id"            =>              $r["domain_id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   863
               "owner"         =>              $_SESSION["userid"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   864
               "numrec"        =>              $r["aantal"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   865
               );
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   866
	       $_SESSION["partial_".$r["domainname"]] = 1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   867
        }
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   868
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   869
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   870
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   871
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   872
if (empty($ret)) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   873
   return -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   874
} else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   875
   sort($ret);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   876
   return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   877
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   878
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   879
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   880
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   881
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   882
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   883
 * Get a record from an id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   884
 * Retrieve all fields of the record and send it back to the function caller.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   885
 * return values: the array with information, or -1 is nothing is found.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   886
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   887
function get_record_from_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   888
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   889
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   890
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   891
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   892
		$result = $db->query("SELECT id, domain_id, name, type, content, ttl, prio, change_date FROM records WHERE id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   893
		if($result->numRows() == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   894
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   895
			return -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   896
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   897
		elseif ($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   898
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   899
			$r = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   900
			$ret = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   901
			"id"            =>      $r["id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   902
			"domain_id"     =>      $r["domain_id"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   903
			"name"          =>      $r["name"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   904
			"type"          =>      $r["type"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   905
			"content"       =>      $r["content"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   906
			"ttl"           =>      $r["ttl"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   907
			"prio"          =>      $r["prio"],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   908
			"change_date"   =>      $r["change_date"]
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   909
			);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   910
			return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   911
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   912
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   913
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   914
			error(sprintf(ERR_INV_ARGC, "get_record_from_id", "More than one row returned! This is bad!"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   915
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   916
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   917
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   918
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   919
		error(sprintf(ERR_INV_ARG, "get_record_from_id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   920
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   921
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   922
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   923
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   924
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   925
 * Get all records from a domain id.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   926
 * Retrieve all fields of the records and send it back to the function caller.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   927
 * return values: the array with information, or -1 is nothing is found.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   928
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   929
function get_records_from_domain_id($id,$rowstart=0,$rowamount=999999)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   930
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   931
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   932
	if (is_numeric($id))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   933
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   934
		if ($_SESSION[$id."_ispartial"] == 1) {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   935
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   936
		$result = $db->query("SELECT record_owners.record_id as id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   937
		FROM record_owners,domains,records
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   938
		WHERE record_owners.user_id = ".$_SESSION["userid"]."
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   939
		AND record_owners.record_id = records.id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   940
		AND records.domain_id = ".$id."
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   941
		GROUP bY record_owners.record_id
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   942
		LIMIT $rowstart,$rowamount");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   943
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   944
		$ret = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   945
		if($result->numRows() == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   946
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   947
		return -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   948
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   949
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   950
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   951
		$ret[] = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   952
		$retcount = 0;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   953
		while($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   954
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   955
		// Call get_record_from_id for each row.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   956
		$ret[$retcount] = get_record_from_id($r["id"]);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   957
		$retcount++;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   958
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   959
		return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   960
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   961
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   962
		} else {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   963
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   964
		$result = $db->query("SELECT id FROM records WHERE domain_id=$id LIMIT $rowstart,$rowamount");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   965
		$ret = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   966
		if($result->numRows() == 0)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   967
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   968
			return -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   969
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   970
		else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   971
		{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   972
			$ret[] = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   973
			$retcount = 0;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   974
			while($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   975
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   976
				// Call get_record_from_id for each row.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   977
				$ret[$retcount] = get_record_from_id($r["id"]);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   978
				$retcount++;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   979
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   980
			return $ret;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   981
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   982
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   983
		}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   984
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   985
	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   986
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   987
		error(sprintf(ERR_INV_ARG, "get_records_from_domain_id"));
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   988
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   989
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   990
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   991
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   992
function get_users_from_domain_id($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   993
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   994
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   995
	$result = $db->getCol("SELECT owner FROM zones WHERE domain_id=$id");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   996
	$ret = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   997
	foreach($result as $uid)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   998
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   999
		$fullname = $db->getOne("SELECT fullname FROM users WHERE id=$uid");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1000
		$ret[] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1001
		"id" 		=> 	$uid,
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1002
		"fullname"	=>	$fullname		
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1003
		);		
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1004
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1005
	return $ret;	
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1006
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1007
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1008
function search_record($question)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1009
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1010
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1011
	$question = trim($question);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1012
	if (empty($question)) 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1013
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1014
		$S_INPUT_TYPE = -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1015
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1016
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1017
	/* now for some input-type searching */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1018
	if (is_valid_ip($question) || is_valid_ip6($question))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1019
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1020
		$S_INPUT_TYPE = 0;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1021
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1022
	elseif(is_valid_domain($question) || 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1023
		is_valid_hostname($question) ||
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1024
		is_valid_mboxfw($question)) // I guess this one can appear in records table too (content?!)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1025
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1026
		$S_INPUT_TYPE = 1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1027
	}	  
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1028
	else 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1029
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1030
		$S_INPUT_TYPE = -1;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1031
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1032
	switch($S_INPUT_TYPE)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1033
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1034
		case '0': 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1035
			$sqlq = "SELECT * FROM `records` WHERE `content` = '".$question."' ORDER BY `type` DESC";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1036
			$result = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1037
			$ret_r = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1038
			while ($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1039
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1040
			    if(xs($r['domain_id']))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1041
			    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1042
    				$ret_r[] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1043
    				  'id'			=>	$r['id'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1044
    				  'domain_id'		=>	$r['domain_id'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1045
    				  'name'		=>	$r['name'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1046
    				  'type'		=>	$r['type'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1047
    				  'content'		=>	$r['content'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1048
    				  'ttl'			=>	$r['ttl'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1049
    				  'prio'		=>	$r['prio'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1050
    				  'change_date'		=>	$r['change_date']
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1051
    				);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1052
				}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1053
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1054
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1055
	    
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1056
		case '1' :
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1057
			$sqlq = "SELECT `domains`.*, count(`records`.`id`) AS `numrec`, `zones`.`owner`  
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1058
					FROM `domains`, `records`, `zones`  
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1059
					WHERE `domains`.`id` = `records`.`domain_id` 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1060
					AND `zones`.`domain_id` = `domains`.`id` 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1061
					AND `domains`.`name` = '".$question."' 
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1062
					GROUP BY (`domains`.`id`)";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1063
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1064
			$result = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1065
			$ret_d = array();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1066
			while ($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1067
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1068
			    if(xs($r['domain_id']))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1069
			    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1070
				    $ret_d[] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1071
    					'id'			=>	$r['id'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1072
    					'name'		=>	$r['name'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1073
    					'numrec'		=>	$r['numrec'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1074
    					'owner'		=>	$r['owner']
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1075
    				);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1076
				}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1077
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1078
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1079
			$sqlq = "SELECT * FROM `records` WHERE `name` = '".$question."' OR `content` = '".$question."' ORDER BY `type` DESC";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1080
			$result = $db->query($sqlq);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1081
			while ($r = $result->fetchRow())
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1082
			{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1083
			    if(xs($r['domain_id']))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1084
			    {
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1085
    				$ret_r[] = array(
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1086
    					'id'			=>	$r['id'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1087
    					'domain_id'		=>	$r['domain_id'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1088
    					'name'		=>	$r['name'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1089
    					'type'		=>	$r['type'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1090
    					'content'		=>	$r['content'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1091
    					'ttl'			=>	$r['ttl'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1092
    					'prio'		=>	$r['prio'],
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1093
    				);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1094
    			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1095
			}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1096
			break;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1097
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1098
	if($S_INPUT_TYPE == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1099
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1100
		return array('domains' => $ret_d, 'records' => $ret_r);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1101
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1102
	return array('records' => $ret_r);
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1103
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1104
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1105
function get_domain_type($id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1106
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1107
	global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1108
	$type = $db->getOne("SELECT `type` FROM `domains` WHERE `id` = '".$id."'");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1109
	if($type == "")
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1110
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1111
		$type = "NATIVE";
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1112
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1113
	return $type;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1114
    
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1115
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1116
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1117
function change_domain_type($type, $id)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1118
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1119
    global $db;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1120
    $result = $db->query("UPDATE `domains` SET `type` = '" .$type. "' WHERE `id` = '".$id."'");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1121
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
  1122
?>