abcd567a / set-gain

4 stars 1 forks source link

Improved gain.sh #2

Open SusanTN opened 5 months ago

SusanTN commented 5 months ago

This cleans up the /usr/share/gain.php

I also added in the ability to set gain to -10 for autogain. Used javascript to refresh the iframe after the gain is updated. Removed php system and other calls in favor of internal php functions.

session_start();

// Generate a CSRF token
function generateToken() {
    if (empty($_SESSION['token'])) {
        $_SESSION['token'] = bin2hex(random_bytes(32));
    }
}

// Validate the CSRF token
function validateToken($token) {
    return $token === $_SESSION['token'];
}

// Get the current gain value from a secure file or database
function getCurrentGain() {
    // Assuming the gain value is stored in a secure file or database
    $gain = file_get_contents('/usr/local/sbin/gain/currentgain');
    return trim($gain);
}

// Set the new gain value securely
function setGain($gain) {
    $valid_gains = [
        '-10', '0.0', '0.9', '1.4', '2.7', '3.7', '7.7', '8.7', '12.5', '14.4', '15.7',
        '16.6', '19.7', '20.7', '22.9', '25.4', '28.0', '29.7', '32.8', '33.8', '36.4',
        '37.2', '38.6', '40.2', '42.1', '43.4', '43.9', '44.5', '48.0', '49.6', '60'
    ];

    if (in_array($gain, $valid_gains, true)) {
        file_put_contents('/usr/local/sbin/gain/newgain', $gain);
        sleep(5);
        echo "<script>localStorage.setItem('gainUpdated', 'true');</script>";
        echo "<script>window.location = 'gain.php';</script>";
        exit();
    } else {
        echo "Invalid gain value.";
    }
}

generateToken();

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['gain']) && validateToken($_POST['token'])) {
    $gain = $_POST['gain'];
    setGain($gain);
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "Invalid CSRF token.";
}

$current_gain = getCurrentGain();
?>

<!DOCTYPE html>
<html>
<head>
    <title>Gain Control</title>
    <script>
        document.addEventListener('DOMContentLoaded', (event) => {
            if (localStorage.getItem('gainUpdated') === 'true') {
                localStorage.removeItem('gainUpdated');
                location.reload();
            }
        });
    </script>
</head>
<body>
    <form id="myform" action="gain.php" method="post">
        <div style="display:block;">
            <font color="#ff0000" face="'Helvetica Neue', Helvetica, Arial, sans-serif">
                Current Gain: <?php echo htmlspecialchars($current_gain); ?>
            </font>
            <br />
            <select name="gain" id="gain">
                <option value="-10" <?php if ($current_gain == '-10') echo 'selected'; ?>>Auto Gain</option>
                <option value="0.0" <?php if ($current_gain == '0.0') echo 'selected'; ?>>0.0</option>
                <option value="0.9" <?php if ($current_gain == '0.9') echo 'selected'; ?>>0.9</option>
                <option value="1.4" <?php if ($current_gain == '1.4') echo 'selected'; ?>>1.4</option>
                <option value="2.7" <?php if ($current_gain == '2.7') echo 'selected'; ?>>2.7</option>
                <option value="3.7" <?php if ($current_gain == '3.7') echo 'selected'; ?>>3.7</option>
                <option value="7.7" <?php if ($current_gain == '7.7') echo 'selected'; ?>>7.7</option>
                <option value="8.7" <?php if ($current_gain == '8.7') echo 'selected'; ?>>8.7</option>
                <option value="12.5" <?php if ($current_gain == '12.5') echo 'selected'; ?>>12.5</option>
                <option value="14.4" <?php if ($current_gain == '14.4') echo 'selected'; ?>>14.4</option>
                <option value="15.7" <?php if ($current_gain == '15.7') echo 'selected'; ?>>15.7</option>
                <option value="16.6" <?php if ($current_gain == '16.6') echo 'selected'; ?>>16.6</option>
                <option value="19.7" <?php if ($current_gain == '19.7') echo 'selected'; ?>>19.7</option>
                <option value="20.7" <?php if ($current_gain == '20.7') echo 'selected'; ?>>20.7</option>
                <option value="22.9" <?php if ($current_gain == '22.9') echo 'selected'; ?>>22.9</option>
                <option value="25.4" <?php if ($current_gain == '25.4') echo 'selected'; ?>>25.4</option>
                <option value="28.0" <?php if ($current_gain == '28.0') echo 'selected'; ?>>28.0</option>
                <option value="29.7" <?php if ($current_gain == '29.7') echo 'selected'; ?>>29.7</option>
                <option value="32.8" <?php if ($current_gain == '32.8') echo 'selected'; ?>>32.8</option>
                <option value="33.8" <?php if ($current_gain == '33.8') echo 'selected'; ?>>33.8</option>
                <option value="36.4" <?php if ($current_gain == '36.4') echo 'selected'; ?>>36.4</option>
                <option value="37.2" <?php if ($current_gain == '37.2') echo 'selected'; ?>>37.2</option>
                <option value="38.6" <?php if ($current_gain == '38.6') echo 'selected'; ?>>38.6</option>
                <option value="40.2" <?php if ($current_gain == '40.2') echo 'selected'; ?>>40.2</option>
                <option value="42.1" <?php if ($current_gain == '42.1') echo 'selected'; ?>>42.1</option>
                <option value="43.4" <?php if ($current_gain == '43.4') echo 'selected'; ?>>43.4</option>
                <option value="43.9" <?php if ($current_gain == '43.9') echo 'selected'; ?>>43.9</option>
                <option value="44.5" <?php if ($current_gain == '44.5') echo 'selected'; ?>>44.5</option>
                <option value="48.0" <?php if ($current_gain == '48.0') echo 'selected'; ?>>48.0</option>
                <option value="49.6" <?php if ($current_gain == '49.6') echo 'selected'; ?>>49.6</option>
                <option value="60" <?php if ($current_gain == '60') echo 'selected'; ?>>60</option>
            </select>
            <input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>" />
            <input type="submit" value="Set Gain" style="color:#ffffff;background-color:#00A0E2;border-color:#00B0F0;" />
        </div>
    </form>
</body>
</html>
SusanTN commented 5 months ago

I had it reselect the current gain when the form updates. And had it verify that the submitted value is one of the acceptable options to prevent invalid submsisions.

SusanTN commented 5 months ago

It would be nice to have a button to turn agc on or off as well.