edit.php 6.02 KB
Newer Older
1
<?php
2

David Morley's avatar
David Morley committed
3 4 5 6 7 8 9 10
/**
 * Edit an existing pod.
 */

declare(strict_types=1);

use Carbon\Carbon;
use Poduptime\PodStatus;
11 12
use RedBeanPHP\R;

13 14 15 16
// Required parameters.
($_domain = $_GET['domain'] ?? null) || die('no pod domain given');
($_token = $_GET['token'] ?? null) || die('no token given');
strlen($_token) > 6 || die('bad token');
17

18
// Other parameters.
19 20 21 22 23 24
$_action                 = $_GET['action'] ?? '';
$_weight                 = $_GET['weight'] ?? 10;
$_email                  = $_GET['email'] ?? '';
$_podmin_statement       = $_GET['podmin_statement'] ?? '';
$_podmin_notify          = $_GET['podmin_notify'] ?? 0;
$_podmin_notify_level    = $_GET['podmin_notify_level'] ?? 50;
25

26
require_once __DIR__ . '/../boot.php';
27

David Morley's avatar
David Morley committed
28 29 30 31 32
// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
R::usePartialBeans(true);

33
try {
David Morley's avatar
David Morley committed
34 35
    $pod = R::findOne('pods', 'domain = ?', [$_domain]);
    $pod || die('domain not found');
36
} catch (\RedBeanPHP\RedException $e) {
David Morley's avatar
David Morley committed
37
    die('Error in SQL query: ' . $e->getMessage());
38
}
39

40 41
$pod['token'] === $_token || die('token mismatch');
$pod['tokenexpire'] >= date('Y-m-d H:i:s') || die('token expired');
42

43 44
// Delete and exit.
if ('delete' === $_action) {
David Morley's avatar
David Morley committed
45 46 47 48 49 50 51
    try {
        $pod['status'] = PodStatus::USER_DELETED;
        R::store($pod);
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }
    die('pod deleted');
David Morley's avatar
David Morley committed
52 53 54 55
}

// Pause and exit.
if ('pause' === $_action) {
David Morley's avatar
David Morley committed
56 57 58 59 60 61 62
    try {
        $pod['status'] = PodStatus::PAUSED;
        R::store($pod);
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }
    die('pod paused');
David Morley's avatar
David Morley committed
63 64 65 66
}

// Un-Pause and exit.
if ('unpause' === $_action) {
David Morley's avatar
David Morley committed
67
    try {
David Morley's avatar
David Morley committed
68
        $pod['status'] = PodStatus::RECHECK;
David Morley's avatar
David Morley committed
69 70 71 72 73
        R::store($pod);
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }
    die('pod unpaused');
74
}
75

76 77
// Save and exit.
if ('save' === $_action) {
David Morley's avatar
David Morley committed
78 79 80
    $_weight <= 10 || die('10 is max weight');

    try {
81 82 83 84 85 86
        $pod['email']                  = $_email;
        $pod['weight']                 = $_weight;
        $pod['podmin_statement']       = $_podmin_statement;
        $pod['podmin_notify']          = $_podmin_notify;
        $pod['podmin_notify_level']    = $_podmin_notify_level;

David Morley's avatar
David Morley committed
87 88 89 90 91 92 93

        R::store($pod);
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }

    $to      = $_email;
94
    $headers = ['From: ' . c('adminemail'), 'Cc: ' . $pod['email'], 'Bcc: ' . c('adminemail')];
David Morley's avatar
David Morley committed
95 96 97 98 99
    $subject = 'Edit notice from poduptime';
    $message = 'Data for ' . $_domain . ' updated. If it was not you reply and let me know!';
    @mail($to, $subject, $message, implode("\r\n", $headers));

    die('Data saved. Will go into effect on next hourly change');
100
}
dmorley's avatar
dmorley committed
101

102
// Forms.
David Morley's avatar
David Morley committed
103

104
?>
David Morley's avatar
David Morley committed
105 106 107
<script>
    window.onload = function() {
        $("#weight").ionRangeSlider();
108
        $("#podmin_notify_level").ionRangeSlider();
David Morley's avatar
David Morley committed
109 110
    }
</script>
David Morley's avatar
David Morley committed
111
Authorized to edit <b><?php echo $_domain; ?></b> for <?php echo (new Carbon($pod['tokenexpire']))->diffForHumans(null, true); ?><br>
David Morley's avatar
David Morley committed
112
<form action="/">
David Morley's avatar
David Morley committed
113
    <input type="hidden" name="edit">
114 115
    <input type="hidden" name="domain" value="<?php echo $_domain; ?>">
    <input type="hidden" name="token" value="<?php echo $_token; ?>">
David Morley's avatar
David Morley committed
116 117
    <label>Email <input type="text" size="40" name="email" value="<?php echo $pod['email']; ?>"></label><br>
    <label>Podmin Statement (You can use HTML to include links to your terms and policies and information about your pod you wish to share with users.) <br><textarea cols="150" rows="10" name="podmin_statement"><?php echo $pod['podmin_statement']; ?></textarea></label><br>
David Morley's avatar
David Morley committed
118
    <label>Weight <input type="text" data-min="0" data-step="1" data-max="10" name="weight" id="weight" data-from="<?php echo $pod['weight']; ?>"> This lets you weight your pod lower on the list if you have too much traffic coming in, 10 is the norm use lower to move down the list.</label><br>
David Morley's avatar
David Morley committed
119
    <label>Notify if pod falls off the list? <input type="checkbox" name="podmin_notify" <?php echo $pod['podmin_notify'] ? 'checked' : ''; ?> ></label><br>
120 121
    <label>Notify when your score falls to <input type="text" data-min="1" data-step="1" data-max="99" name="podmin_notify_level" id="podmin_notify_level" data-from="<?php echo $pod['podmin_notify_level']; ?>"></label><br>

122
    <input type="submit" name="action" value="save">
David Morley's avatar
David Morley committed
123 124 125 126
</form>
<br>
<br>Your pod status is currently: <?php echo PodStatus::getKey((int) $pod['status']); ?>
<br>
David Morley's avatar
David Morley committed
127
<form action="/">
David Morley's avatar
David Morley committed
128 129 130
    <input type="hidden" name="edit">
    <input type="hidden" name="domain" value="<?php echo $_domain; ?>">
    <input type="hidden" name="token" value="<?php echo $_token; ?>">
David Morley's avatar
David Morley committed
131
    <input type="submit" name="action" value="delete" aria-describedby="delete">
David Morley's avatar
David Morley committed
132
</form>
David Morley's avatar
David Morley committed
133 134 135 136
<small id="delete" class="form-text text-muted">
    Stop checking and Stop showing your pod. Can be re-enabled later.
</small>
<form action="/">
David Morley's avatar
David Morley committed
137 138 139
    <input type="hidden" name="edit">
    <input type="hidden" name="domain" value="<?php echo $_domain; ?>">
    <input type="hidden" name="token" value="<?php echo $_token; ?>">
David Morley's avatar
David Morley committed
140
    <input type="submit" name="action" value="pause" aria-describedby="pause">
David Morley's avatar
David Morley committed
141
</form>
David Morley's avatar
David Morley committed
142 143 144 145
<small id="pause" class="form-text text-muted">
    Stop checking your pod. Can be re-enabled later.
</small>
<form action="/">
David Morley's avatar
David Morley committed
146
    <input type="hidden" name="edit">
147 148
    <input type="hidden" name="domain" value="<?php echo $_domain; ?>">
    <input type="hidden" name="token" value="<?php echo $_token; ?>">
David Morley's avatar
David Morley committed
149
    <input type="submit" name="action" value="unpause" aria-describedby="undelete">
David Morley's avatar
David Morley committed
150
</form>
David Morley's avatar
David Morley committed
151 152 153 154 155 156
<small id="undelete" class="form-text text-muted">
    Unpause/Undelete - Start checking and Start showing your pod.
</small>
<input value="debug" type="button"  aria-describedby="debug" data-featherlight="/db/pull.php?debug=1&nowrite=1&domain=<?php echo $_domain; ?>"/>
<small id="debug" class="form-text text-muted">
    Do a debug pull of your pod. Won't update the database just show what it would look like on a pass.
157
</small>