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