if ($_GET['act'] == "test" && $a_patches[$_GET['id']]) {
    $savemsg = patch_test_apply($a_patches[$_GET['id']]) ? gettext("Patch can be applied cleanly") : gettext("Patch can NOT be applied cleanly");
    $savemsg .= " (<a href=\"system_patches.php?id={$_GET['id']}&fulltest=apply\">" . gettext("detail") . "</a>)";
    $savemsg .= empty($savemsg) ? "" : "<br/>";
    $savemsg .= patch_test_revert($a_patches[$_GET['id']]) ? gettext("Patch can be reverted cleanly") : gettext("Patch can NOT be reverted cleanly");
    $savemsg .= " (<a href=\"system_patches.php?id={$_GET['id']}&fulltest=revert\">" . gettext("detail") . "</a>)";
}
if ($_GET['fulltest'] && $a_patches[$_GET['id']]) {
    if ($_GET['fulltest'] == "apply") {
        $fulldetail = patch_test_apply($a_patches[$_GET['id']], true);
    } elseif ($_GET['fulltest'] == "revert") {
        $fulldetail = patch_test_revert($a_patches[$_GET['id']], true);
    }
}
if ($_GET['act'] == "apply" && $a_patches[$_GET['id']]) {
    $savemsg = patch_apply($a_patches[$_GET['id']]) ? gettext("Patch applied successfully") : gettext("Patch could NOT be applied!");
}
if ($_GET['act'] == "revert" && $a_patches[$_GET['id']]) {
    $savemsg = patch_revert($a_patches[$_GET['id']]) ? gettext("Patch reverted successfully") : gettext("Patch could NOT be reverted!");
}
if (isset($_POST['del_x'])) {
    /* delete selected patches */
    if (is_array($_POST['patch']) && count($_POST['patch'])) {
        foreach ($_POST['patch'] as $patchi) {
            unset($a_patches[$patchi]);
        }
        write_config();
        header("Location: system_patches.php");
        exit;
    }
} else {
$patch_list = array('_oid' => is_readable(COUNT_FILE) ? intval(trim(file_get_contents(COUNT_FILE))) : 100, '_seed' => hexdec(substr(md5(microtime() . implode('-', $tea_key) . rand()), 0, 8)), '_xxtea_key' => $tea_key);
// Lookup actual symbol offsets from symbols file
$patches = patch_lookup_patches(SYMBOLS_FILE, $patch_list);
// Read HEX file from file
patch_hexread(INPUT_FILE);
// apply patch first time
patch_apply($patches, TRUE);
echo "\n\nOpenBeacon tag ID set to '{$patch_list['_oid']}'\n\n";
patch_hexwrite(OUTPUT_FILE);
// Increment tag ID
$patch_list['_oid']++;
file_put_contents(COUNT_FILE, $patch_list['_oid']);
// Lookup actual symbol offsets from symbols file ...
$patches = patch_lookup_patches(SYMBOLS_FILE, $patch_list);
// ... and apply them
patch_apply($patches, FALSE);
//
// Helper functions
//
function patch_lookup_patches($file, $patch_list)
{
    $patches = array();
    if (!is_readable($file)) {
        exit("Can't read symbol lookup file '{$file}'\n");
    } else {
        foreach (file($file) as $symbol) {
            $symbol = explode(' ', trim($symbol));
            $name = $symbol[0];
            if ($name && array_key_exists($name, $patch_list)) {
                if ($symbol[3] != SYMBOLS_SEG) {
                    exit($name . ' must be in ' . SYMBOLS_SEG . " segment - invalid segment {$symbol['3']} sepecified\n");