Пример #1
0
function read_in_csv_row($row)
{
    global $COLLATE;
    global $dbo;
    $recordtype = $row['0'];
    $fieldcount = count($row);
    $result = array();
    /*
     *  Record format:
     *  block: (5 fields)
     *  'block','$block_name','$start_ip','$end_ip','$block_note'
     *  
     *  subnet: (5 fields)
     *  'subnet','$block_name','$subnet_name','$subnet','$subnet_note'
     *  
     *  acl: (4 fields)
     *  'acl','$acl_name','$start_ip','$end_ip'
     *  
     *  static ip: (5 fields)
     *  'static','$static_name','$ip_address','$static_contact','$static_note'
     */
    if ($recordtype == 'block' && $fieldcount != '5' || $recordtype == 'subnet' && $fieldcount != '5' || $recordtype == 'acl' && $fieldcount != '4' || $recordtype == 'static' && $fieldcount != '5') {
        $result['error'] = true;
        $result['errormessage'] = 'badfieldcount';
        return $result;
    }
    $last_modified_by = !isset($COLLATE['user']['username']) ? 'system' : $COLLATE['user']['username'];
    if ($recordtype == 'block') {
        $block_name = $row['1'];
        $block_start_ip = $row['2'];
        $block_end_ip = $row['3'];
        $block_note = $row['4'];
        $validate = validate_text($block_name, 'blockname');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $block_name = $validate['1'];
        }
        $query_result = $dbo->query("SELECT id from blocks where name='{$block_name}'");
        if ($query_result->rowCount() != '0') {
            $result['error'] = true;
            $result['errormessage'] = 'duplicatename';
            return $result;
        }
        if (preg_match('/^\\s*$/', $block_start_ip) && preg_match('/^\\s*$/', $block_end_ip)) {
            // block with no associated IP information
            $block_start_ip = '';
            $block_long_start_ip = '';
            $block_end_ip = '';
            $block_long_end_ip = '';
        } elseif (empty($block_end_ip) || ip2decimal($block_end_ip) === false) {
            // subnet
            $validate = validate_network($block_start_ip, 'block');
            if ($validate['0'] === false) {
                $result['error'] = true;
                $result['errormessage'] = $validate['error'];
                return $result;
            } else {
                $block_start_ip = $validate['start_ip'];
                $block_long_start_ip = $validate['long_start_ip'];
                $block_end_ip = $validate['end_ip'];
                $block_long_end_ip = $validate['long_end_ip'];
            }
        } else {
            // range
            $validate = validate_ip_range($block_start_ip, $block_end_ip, 'block');
            if ($validate['0'] === false) {
                $result['error'] = true;
                $result['errormessage'] = $validate['error'];
                return $result;
            } else {
                $block_start_ip = $validate['start_ip'];
                $block_long_start_ip = $validate['long_start_ip'];
                $block_end_ip = $validate['end_ip'];
                $block_long_end_ip = $validate['long_end_ip'];
            }
        }
        $validate = validate_text($block_note, 'note');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $block_note = $validate['1'];
        }
        $row_result['error'] = false;
        $row_result['sql'] = "INSERT INTO blocks (name, start_ip, end_ip, note, modified_by, modified_at) \r\n\t                  VALUES('{$block_name}', '{$block_long_start_ip}', '{$block_long_end_ip}', '{$block_note}', '{$last_modified_by}', now())";
        return $row_result;
    } elseif ($recordtype == 'subnet') {
        $block_name = $row['1'];
        $subnet_name = $row['2'];
        $subnet = $row['3'];
        $subnet_note = $row['4'];
        $validate = validate_text($block_name, 'blockname');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $block_name = $validate['1'];
        }
        $query_result = $dbo->query("SELECT id from blocks where name='{$block_name}'");
        if ($query_result->rowCount() != '1') {
            $result['error'] = true;
            $result['errormessage'] = 'blocknotfound';
            return $result;
        } else {
            $block_id = $query_result->fetchColumn();
        }
        $validate = validate_text($subnet_name, 'subnetname');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $subnet_name = $validate['1'];
        }
        $validate = validate_network($subnet);
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $subnet_start_ip = $validate['start_ip'];
            $subnet_long_start_ip = $validate['long_start_ip'];
            $subnet_end_ip = $validate['end_ip'];
            $subnet_long_end_ip = $validate['long_end_ip'];
            $subnet_mask = $validate['mask'];
            $subnet_long_mask = $validate['long_mask'];
        }
        $validate = validate_text($subnet_note, 'note');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $subnet_note = $validate['1'];
        }
        $return['error'] = false;
        $return['sql'] = "INSERT INTO subnets (name, start_ip, end_ip, mask, note, block_id, modified_by, modified_at) \r\n                      VALUES('{$subnet_name}', '{$subnet_long_start_ip}', '{$subnet_long_end_ip}', '{$subnet_long_mask}', \r\n\t\t\t\t\t  '{$subnet_note}', '{$block_id}', '{$last_modified_by}', now())";
        return $return;
    } elseif ($recordtype == 'acl') {
        $acl_name = $row['1'];
        $acl_start_ip = $row['2'];
        $acl_end_ip = $row['3'];
        $validate = validate_text($acl_name, 'blockname');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $acl_name = $validate['1'];
        }
        $validate = validate_ip_range($acl_start_ip, $acl_end_ip, 'acl', null);
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $subnet_id = $validate['subnet_id'];
            $acl_start_ip = $validate['start_ip'];
            $acl_long_start_ip = $validate['long_start_ip'];
            $acl_end_ip = $validate['end_ip'];
            $acl_long_end_ip = $validate['long_end_ip'];
        }
        $return['error'] = false;
        $return['sql'] = "INSERT INTO acl (name, start_ip, end_ip, subnet_id) \r\n\t                  VALUES ('{$acl_name}', '{$acl_long_start_ip}', '{$acl_long_end_ip}', '{$subnet_id}')";
        return $return;
    } else {
        // $recordtype == static
        $static_name = $row['1'];
        $static_ip = $row['2'];
        $static_long_ip = ip2decimal($static_ip);
        $static_contact = $row['3'];
        $static_note = $row['4'];
        $validate = validate_text($static_name, 'staticname');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $static_name = $validate['1'];
        }
        if ($static_long_ip === false) {
            $result['error'] = true;
            $result['errormessage'] = 'invalidip';
            return $result;
        }
        $sql = "SELECT id from subnets where CAST('{$static_long_ip}' AS UNSIGNED) & CAST(mask AS UNSIGNED) = CAST(start_ip AS UNSIGNED)";
        $subnet_result = $dbo->query($sql);
        if ($subnet_result->rowCount() != '1') {
            $result['error'] = true;
            $result['errormessage'] = 'subnetnotfound';
            return $result;
        } else {
            $subnet_id = $subnet_result->fetchColumn();
        }
        // Make sure the static IP isn't in use already or excluded from use via an ACL
        $validate = validate_static_ip($static_ip);
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        }
        $validate = validate_text($static_contact, 'contact');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $static_contact = $validate['1'];
        }
        $validate = validate_text($static_note, 'note');
        if ($validate['0'] === false) {
            $result['error'] = true;
            $result['errormessage'] = $validate['error'];
            return $result;
        } else {
            $static_note = $validate['1'];
        }
        $return['error'] = false;
        $return['sql'] = "INSERT INTO statics (ip, name, contact, note, subnet_id, modified_by, modified_at)\r\n                      VALUES('{$static_long_ip}', '{$static_name}', '{$static_contact}', '{$static_note}', \r\n\t\t\t\t\t  '{$subnet_id}', '{$last_modified_by}', now())";
        return $return;
    }
    // We should never get here
    exit;
}
Пример #2
0
function resize_subnet()
{
    global $COLLATE;
    global $dbo;
    include 'include/validation_functions.php';
    $subnet_id = isset($_POST['subnet_id']) && is_numeric($_POST['subnet_id']) ? $_POST['subnet_id'] : '';
    $new_subnet = isset($_POST['new_subnet']) ? $_POST['new_subnet'] : '';
    $confirm = isset($_POST['confirm']) ? true : false;
    $sql = "SELECT name, start_ip, end_ip, mask, block_id FROM subnets WHERE id='{$subnet_id}'";
    $result = $dbo->query($sql);
    if ($result->rowCount() != '1') {
        $notice = "invalidrequest";
        header("Location: blocks.php?notice={$notice}");
        exit;
    }
    list($original_subnet_name, $original_long_start_ip, $original_long_end_ip, $original_long_mask, $original_block_id) = $result->fetch(PDO::FETCH_NUM);
    $original_cidr = subnet2cidr($original_long_start_ip, $original_long_mask);
    $return = validate_network($new_subnet, 'subnet', null, true);
    #last parameter is saying it's ok if the subnet overlaps another
    if ($return['0'] === false) {
        $notice = "invalidrequest";
        header("Location: blocks.php?notice={$notice}");
        exit;
    }
    $new_start_ip = $return['start_ip'];
    $new_long_start_ip = $return['long_start_ip'];
    $new_end_ip = $return['end_ip'];
    $new_long_end_ip = $return['long_end_ip'];
    $new_long_mask = $return['long_mask'];
    $new_cidr = subnet2cidr($new_long_start_ip, $new_long_mask);
    if ($confirm === false) {
        require_once './include/header.php';
    } else {
        AccessControl('3', "Subnet {$original_subnet_name} resized from {$original_cidr} to {$new_cidr}");
    }
    # is new subnet larger or smaller?
    $original_binary_mask = sprintf("%032b", $original_long_mask);
    $new_binary_mask = sprintf("%032b", $new_long_mask);
    if (substr_count($original_binary_mask, '1') < substr_count($new_binary_mask, '1')) {
        # if smaller:
        #  * validate new network falls within the old one
        $test = $new_long_start_ip & $original_long_mask;
        if ($test != $original_long_start_ip) {
            $notice = "invalidshrink-notice";
            header("Location: subnets.php?op=modify&subnet_id={$subnet_id}&notice={$notice}");
            exit;
        }
        #  * list static IP addresses that would be lost
        if ($confirm === false) {
            $sql_action = "SELECT id, ip, name, contact, note, failed_scans FROM statics WHERE ";
            $sql_sort = ' ORDER BY `ip` ASC';
        } else {
            $sql_action = "DELETE FROM statics WHERE ";
        }
        # in old subnet, but not in new one
        $sql_selection = " CAST(ip & 0xFFFFFFFF AS UNSIGNED) & CAST('{$original_long_mask}' & 0xFFFFFFFF AS UNSIGNED) = \r\n\t                  CAST('{$original_long_start_ip}' & 0xFFFFFFFF AS UNSIGNED)\r\n                      AND CAST(ip & 0xFFFFFFFF AS UNSIGNED) & CAST('{$new_long_mask}' & 0xFFFFFFFF AS UNSIGNED) != \r\n                      CAST('{$new_long_start_ip}' & 0xFFFFFFFF AS UNSIGNED) ";
        $sql = $sql_action . $sql_selection;
        $sql = isset($sql_sort) ? $sql . $sql_sort : $sql;
        $result = $dbo->query($sql);
        if ($confirm === false) {
            $staticstobedeleted = str_replace("%original_subnet_name%", $original_subnet_name, $COLLATE['languages']['selected']['staticstodelete']);
            echo "<h1>{$staticstobedeleted}:</h1><br />\n";
            if ($result->rowCount() != '0') {
                echo "<table style=\"width: 100%\"><tr><th>" . $COLLATE['languages']['selected']['IPAddress'] . "</th><th>" . $COLLATE['languages']['selected']['Name'] . "</th><th>" . $COLLATE['languages']['selected']['Contact'] . "</th><th>" . $COLLATE['languages']['selected']['FailedScans'] . "</th></tr>" . "<tr><td colspan=\"5\"><hr class=\"head\" /></td></tr>\n";
                while (list($static_id, $ip, $name, $contact, $note, $failed_scans) = $result->fetch(PDO::FETCH_NUM)) {
                    $ip = long2ip($ip);
                    echo "<tr><td>{$ip}</td><td>{$name}</td><td>{$contact}</td><td>{$failed_scans}</td><td></td></tr>\n";
                    echo "<tr><td colspan=\"5\">{$note}</td></tr>\n";
                    echo "<tr><td colspan=\"5\"><hr class=\"division\" /></td></tr>\n";
                }
                echo "</table><br /><br />";
            } else {
                echo "<p>" . $COLLATE['languages']['selected']['nostaticsdeleted'] . "</p><br /><br />";
            }
        }
        #  * show how ACLs would be adjusted
        # Find acls matching original subnet_id and see if start and end fall within new subnet
        $sql = "SELECT id, name, start_ip, end_ip FROM acl WHERE subnet_id='{$subnet_id}' AND (\r\n           CAST(start_ip & 0xFFFFFFFF AS UNSIGNED) & CAST('{$new_long_mask}' & 0xFFFFFFFF AS UNSIGNED) != \r\n           CAST('{$new_long_start_ip}' & 0xFFFFFFFF AS UNSIGNED)\r\n           OR CAST(end_ip & 0xFFFFFFFF AS UNSIGNED) & CAST('{$new_long_mask}' & 0xFFFFFFFF AS UNSIGNED) != \r\n           CAST('{$new_long_start_ip}' & 0xFFFFFFFF AS UNSIGNED))";
        $result = $dbo->query($sql);
        if ($confirm === false) {
            $aclstobechanged = str_replace("%original_subnet_name%", $original_subnet_name, $COLLATE['languages']['selected']['aclstobechanged']);
            echo "<h1>{$aclstobechanged}:</h1><br />\n";
            if ($result->rowCount() == '0') {
                echo "<p>" . $COLLATE['languages']['selected']['noaclschanged'] . "</p><br /><br />";
            } else {
                echo "<table style=\"width: 100%\">\n" . "<tr><th>" . $COLLATE['languages']['selected']['Name'] . "\r\n             </th><th>" . $COLLATE['languages']['selected']['StartingIP'] . "</th><th>" . $COLLATE['languages']['selected']['EndIP'] . "</th><th>" . $COLLATE['languages']['selected']['Modification'] . "</th></tr>\n" . "<tr><td colspan=\"4\"><hr class=\"head\" /></td></tr>";
            }
        }
        while (list($acl_id, $acl_name, $acl_long_start_ip, $acl_long_end_ip) = $result->fetch(PDO::FETCH_NUM)) {
            $note = "";
            # this might not get set below.
            $sql = "";
            if (($acl_long_start_ip & $new_long_mask) == $new_long_start_ip) {
                $new_acl_start_ip = long2ip($acl_long_start_ip);
            } else {
                $new_acl_start_ip = $new_start_ip;
                $note = "<b>" . $COLLATE['languages']['selected']['StartingIPmodified'] . "</b>";
                $sql = "UPDATE acl SET start_ip='{$new_long_start_ip}' WHERE id='{$acl_id}'";
            }
            if (($acl_long_end_ip & $new_long_mask) == $new_long_start_ip) {
                $new_acl_end_ip = long2ip($acl_long_end_ip);
            } else {
                $new_acl_end_ip = $new_end_ip;
                $note = "<b>" . $COLLATE['languages']['selected']['EndIPmodified'] . "</b>";
                $sql = "UPDATE acl SET end_ip='{$new_long_end_ip}' WHERE id='{$acl_id}'";
            }
            if ($new_acl_start_ip == $new_start_ip && $new_acl_end_ip == $new_end_ip) {
                # we wouldn't generally have an ACL reserve a whole subnet. We'll just ditch the ACL
                # and let the user make something new
                $new_acl_start_ip = long2ip($acl_long_start_ip);
                $new_acl_end_ip = long2ip($acl_long_end_ip);
                $note = "<b>" . $COLLATE['languages']['selected']['ToBeDeleted'] . "</b>";
                $sql = "DELETE FROM acl WHERE id='{$acl_id}'";
            }
            if ($confirm === false) {
                echo "<tr><td>{$acl_name}</td><td>{$new_acl_start_ip}</td><td>{$new_acl_end_ip}</td><td>{$note}</td></tr>\n";
            } elseif (!empty($sql)) {
                $dbo->query($sql);
            }
        }
        if ($confirm === false) {
            echo "</table>\n";
        }
    } else {
        # if larger:
        if (($original_long_start_ip & $new_long_mask) != $new_long_start_ip) {
            $notice = "invalidgrow-notice";
            header("Location: subnets.php?op=modify&subnet_id={$subnet_id}&notice={$notice}");
            exit;
        }
        #  * list all subnets that new network overlaps
        $sql = "SELECT `id`, `name`, `start_ip`, `end_ip`, `mask`, `note` FROM `subnets` WHERE\r\n            CAST(start_ip & 0xFFFFFFFF AS UNSIGNED) & CAST('{$new_long_mask}' & 0xFFFFFFFF AS UNSIGNED) = \r\n            CAST('{$new_long_start_ip}' & 0xFFFFFFFF AS UNSIGNED) ORDER BY `start_ip` ASC";
        $results = $dbo->query($sql);
        $subnetstomerge = str_replace("%original_subnet_name%", $original_subnet_name, $COLLATE['languages']['selected']['subnetstomerge']);
        if ($confirm === false) {
            echo "<h1>{$subnetstomerge}:</h1><br />\n";
        }
        if ($results->rowCount() < '1' && $confirm === false) {
            echo "<p>" . $COLLATE['languages']['selected']['nosubnetsoverlap'] . "</p>";
        } else {
            if ($confirm === false) {
                echo "<table style=\"width: 100%\">" . "<tr><th style=\"text-align: left\">" . $COLLATE['languages']['selected']['SubnetName'] . "</th>" . "<th style=\"text-align: left\">" . $COLLATE['languages']['selected']['NetworkAddress'] . "</th>" . "<th style=\"text-align: left\">" . $COLLATE['languages']['selected']['SubnetMask'] . "</th>" . "<tr><td colspan=\"4\"><hr class=\"head\" /></td></tr>\n";
            }
            while (list($affected_subnet_id, $name, $long_start_ip, $long_end_ip, $long_mask, $note) = $results->fetch(PDO::FETCH_NUM)) {
                if ($confirm === false) {
                    $start_ip = long2ip($long_start_ip);
                    $mask = long2ip($long_mask);
                    echo "<tr><td><b>{$name}</b></td><td>{$start_ip}</td><td>{$mask}</td></tr>\n";
                    echo "<tr><td colspan=\"4\">{$note}</td></tr>\n";
                    echo "<tr><td colspan=\"5\"><hr class=\"division\" /></td></tr>\n";
                } else {
                    $sql = "UPDATE acl SET subnet_id='{$subnet_id}' WHERE subnet_id='{$affected_subnet_id}'";
                    $result = $dbo->query($sql);
                }
            }
            if ($confirm === false) {
                echo "</table>";
            } else {
                $sql = "DELETE FROM `subnets` WHERE CAST(start_ip & 0xFFFFFFFF AS UNSIGNED) & CAST('{$new_long_mask}' & 0xFFFFFFFF AS UNSIGNED) = \r\n                CAST('{$new_long_start_ip}' & 0xFFFFFFFF AS UNSIGNED)\r\n                AND id != '{$subnet_id}'";
                $result = $dbo->query($sql);
                $sql = "UPDATE statics SET subnet_id='{$subnet_id}' WHERE \r\n\t\t       CAST(ip & 0xFFFFFFFF AS UNSIGNED) & CAST('{$new_long_mask}' & 0xFFFFFFFF AS UNSIGNED) = \r\n\t\t\t   CAST('{$new_long_start_ip}' & 0xFFFFFFFF AS UNSIGNED)";
                $result = $dbo->query($sql);
            }
        }
    }
    if ($confirm === false) {
        echo "<br /><br /><h3>" . $COLLATE['languages']['selected']['confirmproceed'] . "</h3><hr /><br />\n" . "<form action=\"subnets.php?op=resize\" method=\"post\">\n" . "<input type=\"hidden\" name=\"subnet_id\" value=\"{$subnet_id}\" />" . "<input type=\"hidden\" name=\"confirm\" value=\"true\" />" . "<input type=\"hidden\" name=\"new_subnet\" value=\"{$new_subnet}\" />" . "<p><input type=\"submit\" value=\" " . $COLLATE['languages']['selected']['Go'] . " \" /> | <a href=\"subnets.php?block_id={$original_block_id}\">" . $COLLATE['languages']['selected']['altcancel'] . "</a></p>" . "</form>";
    } else {
        $sql = "UPDATE subnets set start_ip='{$new_long_start_ip}', end_ip='{$new_long_end_ip}', mask='{$new_long_mask}' WHERE id='{$subnet_id}'";
        $result = $dbo->query($sql);
        $notice = "resized-notice";
        header("Location: subnets.php?block_id={$original_block_id}&notice={$notice}");
        exit;
    }
}
Пример #3
0
function submit_block()
{
    #validation here might look messy, but it's essentially in order of parameters listed below by
    # 1. all checks that don't require db lookups
    # 2. all other checks
    global $COLLATE;
    global $dbo;
    include 'include/validation_functions.php';
    $block_id = isset($_POST['block_id']) ? $_POST['block_id'] : '';
    $name = isset($_POST['name']) ? $_POST['name'] : '';
    $note = isset($_POST['note']) ? $_POST['note'] : '';
    # this input is optional
    $ip = isset($_POST['ip']) ? $_POST['ip'] : '';
    $end_ip = isset($_POST['end_ip']) ? $_POST['end_ip'] : '';
    $username = empty($_SESSION['username']) ? 'system' : $_SESSION['username'];
    $update_block = isset($_POST['update_block']) ? $_POST['update_block'] : false;
    $submit_op = $update_block == 'true' ? "modify&block_id={$block_id}" : 'add';
    $parent_block = isset($_POST['parent_block']) ? $_POST['parent_block'] : '';
    $block_type = isset($_POST['block_type']) ? $_POST['block_type'] : '';
    if ($block_type == 'container') {
        #containers don't have IP ranges associated with them
        $ip = '';
        $end_ip = '';
    }
    if (empty($name) || !empty($end_ip) && empty($ip) || empty($block_type)) {
        $notice = "missingfield-notice";
        header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&block_type={$block_type}&parent_block={$parent_block}&notice={$notice}");
        exit;
    }
    if (empty($parent_block) || !preg_match("/[0-9]*/", $parent_block) && $parent_block != 'null') {
        $notice = "invalidrequest";
        header("Location: blocks.php?notice={$notice}");
        exit;
    }
    $return = validate_text($name, 'blockname');
    if ($return['0'] === false) {
        $notice = $return['error'];
        header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&block_type={$block_type}&parent_block={$parent_block}&notice={$notice}");
        exit;
    } else {
        $name = $return['1'];
    }
    unset($return);
    if (!preg_match('/^container$|^ipv4$/', $block_type)) {
        $notice = 'invalidrequest';
        header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&parent_block={$parent_block}&notice={$notice}");
        exit;
    }
    if ($update_block === false) {
        # checking for duplicate block name
        $sql = "SELECT id from blocks where name='{$name}'";
        $result = $dbo->query($sql);
        if ($result->rowCount() != '0') {
            header("HTTP/1.1 400 Bad Request");
            $notice = 'duplicatename';
            header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&block_type={$block_type}&parent_block={$parent_block}&notice={$notice}");
            exit;
        }
    } else {
        # checking that we're updating a block that actually exists
        $sql = "SELECT name FROM blocks WHERE id='{$block_id}'";
        $result = $dbo->query($sql);
        if ($result->rowCount() != '1') {
            header("HTTP/1.1 400 Bad Request");
            $notice = 'selectblock';
            header("Location: blocks.php?notice={$notice}");
            exit;
        }
        $old_block_name = $result->fetchColumn();
    }
    $return = validate_text($note, 'note');
    if ($return['0'] === false) {
        $notice = $return['error'];
        header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&block_type={$block_type}&parent_block={$parent_block}&notice={$notice}");
        exit;
    } else {
        $note = $return['1'];
    }
    unset($return);
    if (empty($end_ip) && !empty($ip)) {
        # subnet supplied
        $return = validate_network($ip, 'block', $block_id);
    } elseif (!empty($ip)) {
        # range supplied
        $return = validate_ip_range($ip, $end_ip, 'block', $block_id);
    }
    if (isset($return) && $return['0'] === false) {
        $notice = $return['error'];
        header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&block_type={$block_type}&parent_block={$parent_block}&notice={$notice}");
        exit;
    } elseif (isset($return)) {
        $long_start_ip = $return['long_start_ip'];
        $long_end_ip = $return['long_end_ip'];
    }
    unset($return);
    $result = '';
    if ($parent_block != 'null') {
        $sql = "SELECT id FROM blocks WHERE id='{$parent_block}'";
        $result = $dbo->query($sql);
        if ($result->rowCount() != '1') {
            $notice = "invalidrequest";
            header("Location: blocks.php?notice={$notice}");
            exit;
        }
        $parent_id = "'{$parent_block}'";
    } else {
        $parent_id = 'null';
    }
    if ($update_block === false) {
        # new block
        $old_parent_block = $parent_block;
        #we're going to redirect the user to the block they put this block into
    } else {
        $sql = "SELECT parent_id FROM blocks WHERE id='{$block_id}'";
        $result = $dbo->query($sql);
        $old_parent_block = $result->fetchColumn();
    }
    # If we're changing an existing block, we must make sure we don't orphan a child object
    if ($update_block !== false) {
        if ($block_type == 'ipv4' && find_child_blocks($block_id) !== false) {
            $notice = 'wouldorphanblocks';
            header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&notice={$notice}");
            exit;
        } elseif ($block_type == 'container') {
            # just check this block for subnets
            $sql = "SELECT count(*) FROM subnets where block_id='{$block_id}'";
            $result = $dbo->query($sql);
            if ($result->fetchColumn() != '0') {
                $notice = 'wouldorphansubnets';
                header("Location: blocks.php?op={$submit_op}&name={$name}&ip={$ip}&end_ip={$end_ip}&note={$note}&parent_block={$parent_block}&notice={$notice}");
                exit;
            }
        }
    }
    if ($update_block) {
        $sql = "UPDATE blocks SET name='{$name}', start_ip='{$long_start_ip}', end_ip='{$long_end_ip}', note='{$note}', modified_by='{$username}', modified_at=now(),\r\n           parent_id={$parent_id}, type='{$block_type}' WHERE id='{$block_id}'";
    } else {
        $sql = "INSERT INTO blocks (name, start_ip, end_ip, note, modified_by, modified_at, parent_id, type) \r\n\t       VALUES('{$name}', '{$long_start_ip}', '{$long_end_ip}', '{$note}', '{$username}', now(), {$parent_id}, '{$block_type}')";
    }
    $accesslevel = "4";
    $message = $update_block ? "IP Block updated: {$name}" : "IP Block added: {$name}";
    $message .= $name != $old_block_name ? "(previously {$old_block_name})" : '';
    AccessControl($accesslevel, $message);
    // We don't want to generate logs when nothing is really happening, so this goes down here.
    $dbo->query($sql);
    $notice = $update_block ? 'blockupdated-notice' : 'blockadded-notice';
    if ($old_parent_block == 'null') {
        header("Location: blocks.php?notice={$notice}");
    } else {
        header("Location: blocks.php?block_id={$old_parent_block}&notice={$notice}");
    }
    exit;
}