}
require '../inc/error.inc.php';
require '../inc/database.inc.php';
require '../inc/file.inc.php';
require '../inc/record.inc.php';
require '../inc/migrations.inc.php';
$db = dbConnect();
$file_name = file_get_name_without_extension(__FILE__);
if (migration_exists($db, $file_name)) {
    migration_message('The migration had already been applied!');
    exit;
}
$zones = get_zones_with_templates($db);
foreach ($zones as $zone) {
    $domain = get_zone_name_from_id($zone['id']);
    $templ_records = get_zone_templ_records($zone['zone_templ_id']);
    $generated_templ_records = array();
    foreach ($templ_records as $templ_record) {
        $name = parse_template_value($templ_record['name'], $domain);
        $type = $templ_record['type'];
        $content = parse_template_value($templ_record['content'], $domain);
        $generated_templ_records[] = array('name' => $name, 'type' => $type, 'content' => $content);
    }
    $records = get_records_by_domain_id($db, $zone['domain_id']);
    foreach ($records as $record) {
        foreach ($generated_templ_records as $generated_templ_record) {
            if ($record['name'] == $generated_templ_record['name'] && $record['type'] == $generated_templ_record['type'] && $record['content'] == $generated_templ_record['content']) {
                if (!record_relation_to_templ_exists($db, $zone['domain_id'], $record['id'], $zone['zone_templ_id'])) {
                    add_record_relation_to_templ($db, $zone['domain_id'], $record['id'], $zone['zone_templ_id']);
                }
                break;
function update_zone_records($zone_id, $zone_template)
{
    global $db;
    global $dns_ns1;
    global $dns_hostmaster;
    global $dns_ttl;
    if (verify_permission('zone_content_edit_others')) {
        $perm_edit = "all";
    } elseif (verify_permission('zone_content_edit_own')) {
        $perm_edit = "own";
    } else {
        $perm_edit = "none";
    }
    $user_is_zone_owner = verify_user_is_owner_zoneid($zone_id);
    if (verify_permission('zone_master_add')) {
        $zone_master_add = "1";
    }
    if (verify_permission('zone_slave_add')) {
        $zone_slave_add = "1";
    }
    $response = $db->beginTransaction();
    if (0 != $zone_template) {
        if ($perm_edit == "all" || $perm_edit == "own" && $user_is_zone_owner == "1") {
            if (is_numeric($zone_id)) {
                $db->exec("DELETE FROM records WHERE domain_id=" . $db->quote($zone_id, 'integer'));
            } else {
                error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number"));
            }
        } else {
            error(ERR_PERM_DEL_ZONE);
        }
        if ($zone_master_add == "1" || $zone_slave_add == "1") {
            $domain = get_zone_name_from_id($zone_id);
            $now = time();
            $templ_records = get_zone_templ_records($zone_template);
            if ($templ_records == -1) {
                return;
            }
            foreach ($templ_records as $r) {
                if (preg_match('/in-addr.arpa/i', $zone_id) && ($r["type"] == "NS" || $r["type"] == "SOA") || !preg_match('/in-addr.arpa/i', $zone_id)) {
                    $name = parse_template_value($r["name"], $domain);
                    $type = $r["type"];
                    $content = parse_template_value($r["content"], $domain);
                    $ttl = $r["ttl"];
                    $prio = intval($r["prio"]);
                    if (!$ttl) {
                        $ttl = $dns_ttl;
                    }
                    $query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES (" . $db->quote($zone_id, 'integer') . "," . $db->quote($name, 'text') . "," . $db->quote($type, 'text') . "," . $db->quote($content, 'text') . "," . $db->quote($ttl, 'integer') . "," . $db->quote($prio, 'integer') . "," . $db->quote($now, 'integer') . ")";
                    $response = $db->exec($query);
                }
            }
        }
    }
    $query = "UPDATE zones\n                    SET zone_templ_id = " . $db->quote($zone_template, 'integer') . "\n                    WHERE domain_id = " . $db->quote($zone_id, 'integer');
    $response = $db->exec($query);
    if (PEAR::isError($response)) {
        $response = $db->rollback();
    } else {
        $response = $db->commit();
    }
}
Пример #3
0
/** Update All Zone Records for Zone ID with Zone Template
 *
 * @param int $zone_id Zone ID to update
 * @param int $zone_template_id Zone Template ID to use for update
 *
 * @return null
 */
function update_zone_records($zone_id, $zone_template_id)
{
    global $db;
    global $dns_ttl;
    global $db_type;
    if (do_hook('verify_permission', 'zone_content_edit_others')) {
        $perm_edit = "all";
    } elseif (do_hook('verify_permission', 'zone_content_edit_own')) {
        $perm_edit = "own";
    } else {
        $perm_edit = "none";
    }
    $user_is_zone_owner = do_hook('verify_user_is_owner_zoneid', $zone_id);
    if (do_hook('verify_permission', 'zone_master_add')) {
        $zone_master_add = "1";
    }
    if (do_hook('verify_permission', 'zone_slave_add')) {
        $zone_slave_add = "1";
    }
    $soa_rec = get_soa_record($zone_id);
    $response = $db->beginTransaction();
    if (0 != $zone_template_id) {
        if ($perm_edit == "all" || $perm_edit == "own" && $user_is_zone_owner == "1") {
            if (is_numeric($zone_id)) {
                $db->exec("DELETE FROM records WHERE id IN (SELECT record_id FROM records_zone_templ WHERE " . "domain_id = " . $db->quote($zone_id, 'integer') . " AND " . "zone_templ_id = " . $db->quote($zone_template_id, 'integer') . ")");
                $db->exec("DELETE FROM records_zone_templ WHERE domain_id = " . $db->quote($zone_id, 'integer'));
            } else {
                error(sprintf(ERR_INV_ARGC, "delete_domain", "id must be a number"));
            }
        } else {
            error(ERR_PERM_DEL_ZONE);
        }
        if ($zone_master_add == "1" || $zone_slave_add == "1") {
            $domain = get_zone_name_from_id($zone_id);
            $now = time();
            $templ_records = get_zone_templ_records($zone_template_id);
            if ($templ_records == -1) {
                return;
            }
            foreach ($templ_records as $r) {
                //fixme: appears to be a bug and regex match should occur against $domain
                if (preg_match('/in-addr.arpa/i', $zone_id) && ($r["type"] == "NS" || $r["type"] == "SOA") || !preg_match('/in-addr.arpa/i', $zone_id)) {
                    $name = parse_template_value($r["name"], $domain);
                    $type = $r["type"];
                    if ($type == "SOA") {
                        $content = get_updated_soa_record($soa_rec);
                    } else {
                        $content = parse_template_value($r["content"], $domain);
                    }
                    $ttl = $r["ttl"];
                    $prio = intval($r["prio"]);
                    if (!$ttl) {
                        $ttl = $dns_ttl;
                    }
                    $query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date) VALUES (" . $db->quote($zone_id, 'integer') . "," . $db->quote($name, 'text') . "," . $db->quote($type, 'text') . "," . $db->quote($content, 'text') . "," . $db->quote($ttl, 'integer') . "," . $db->quote($prio, 'integer') . "," . $db->quote($now, 'integer') . ")";
                    $response = $db->exec($query);
                    if ($db_type == 'pgsql') {
                        $record_id = $db->lastInsertId('records_id_seq');
                    } else {
                        $record_id = $db->lastInsertId();
                    }
                    $query = "INSERT INTO records_zone_templ (domain_id, record_id, zone_templ_id) VALUES (" . $db->quote($zone_id, 'integer') . "," . $db->quote($record_id, 'integer') . "," . $db->quote($zone_template_id, 'integer') . ")";
                    $response = $db->query($query);
                }
            }
        }
    }
    $query = "UPDATE zones\n                    SET zone_templ_id = " . $db->quote($zone_template_id, 'integer') . "\n                    WHERE domain_id = " . $db->quote($zone_id, 'integer');
    $response = $db->exec($query);
    if (PEAR::isError($response)) {
        $response = $db->rollback();
    } else {
        $response = $db->commit();
    }
}
        update_zone_records($zone['id'], $zone_templ_id);
    }
}
if (!verify_permission('zone_master_add') || !$owner) {
    error(ERR_PERM_EDIT_ZONE_TEMPL);
} else {
    if (zone_templ_id_exists($zone_templ_id) == "0") {
        error(ERR_ZONE_TEMPL_NOT_EXIST);
    } else {
        $record_count = count_zone_templ_records($zone_templ_id);
        $templ_details = get_zone_templ_details($zone_templ_id);
        echo "   <h2>" . _('Edit zone template') . " \"" . $templ_details['name'] . "\"</h2>\n";
        echo "   <div class=\"showmax\">\n";
        show_pages($record_count, $iface_rowamount, $zone_templ_id);
        echo "   </div>\n";
        $records = get_zone_templ_records($zone_templ_id, ROWSTART, $iface_rowamount, RECORD_SORT_BY);
        if ($records == "-1") {
            echo " <p>" . _("This template zone does not have any records yet.") . "</p>\n";
        } else {
            echo "   <form method=\"post\" action=\"\">\n";
            echo "   <table>\n";
            echo "    <tr>\n";
            echo "     <th>&nbsp;</th>\n";
            echo "     <th><a href=\"edit_zone_templ.php?id=" . $zone_templ_id . "&amp;record_sort_by=name\">" . _('Name') . "</a></th>\n";
            echo "     <th><a href=\"edit_zone_templ.php?id=" . $zone_templ_id . "&amp;record_sort_by=type\">" . _('Type') . "</a></th>\n";
            echo "     <th><a href=\"edit_zone_templ.php?id=" . $zone_templ_id . "&amp;record_sort_by=content\">" . _('Content') . "</a></th>\n";
            echo "     <th><a href=\"edit_zone_templ.php?id=" . $zone_templ_id . "&amp;record_sort_by=prio\">" . _('Priority') . "</a></th>\n";
            echo "     <th><a href=\"edit_zone_templ.php?id=" . $zone_templ_id . "&amp;record_sort_by=ttl\">" . _('TTL') . "</a></th>\n";
            echo "    </tr>\n";
            foreach ($records as $r) {
                echo "    <tr>\n";