edit.php 3.55 KB
Newer Older
dmorley's avatar
dmorley committed
1
<?php
noplanman's avatar
noplanman committed
2 3 4

use RedBeanPHP\R;

5 6 7 8
// 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');
9

10
// Other parameters.
David Morley's avatar
David Morley committed
11
$_action           = $_GET['action'] ?? '';
12
$_weight           = $_GET['weight'] ?? 10;
David Morley's avatar
David Morley committed
13 14
$_email            = $_GET['email'] ?? '';
$_podmin_statement = $_GET['podmin_statement'] ?? '';
15
$_podmin_notify    = $_GET['podmin_notify'] ?? 0;
16

noplanman's avatar
noplanman committed
17
require_once __DIR__ . '/../vendor/autoload.php';
18 19
require_once __DIR__ . '/../config.php';

noplanman's avatar
noplanman committed
20
define('PODUPTIME', microtime(true));
21

noplanman's avatar
noplanman committed
22 23 24
// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
25

noplanman's avatar
noplanman committed
26 27 28 29 30 31
try {
  $pod = R::findOne('pods', 'domain = ?', [$_domain]);
  $pod || die('domain not found');
} catch (\RedBeanPHP\RedException $e) {
  die('Error in SQL query: ' . $e->getMessage());
}
32

noplanman's avatar
noplanman committed
33 34
$pod['token'] === $_token || die('token mismatch');
$pod['tokenexpire'] >= date('Y-m-d H:i:s') || die('token expired');
35

noplanman's avatar
noplanman committed
36 37 38 39 40
// Delete and exit.
if ('delete' === $_action) {
  R::trash($pod);
  die('pod removed from DB');
}
41

noplanman's avatar
noplanman committed
42 43 44
// Save and exit.
if ('save' === $_action) {
  $_weight <= 10 || die('10 is max weight');
45

noplanman's avatar
noplanman committed
46 47 48 49 50
  try {
    $pod['email']            = $_email;
    $pod['weight']           = $_weight;
    $pod['podmin_statement'] = $_podmin_statement;
    $pod['podmin_notify']    = $_podmin_notify;
51 52 53 54 55 56

    // @todo Temporary fix for RedBean property handling, hope this gets fixed soon!
    foreach ($pod->getProperties() as $key => $value) {
      $pod[$key] = $value;
    }

noplanman's avatar
noplanman committed
57 58 59 60
    R::store($pod);
  } catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
  }
61

noplanman's avatar
noplanman committed
62 63 64 65 66
  $to      = $_email;
  $headers = ['From: ' . $adminemail, 'Cc: ' . $pod['email'], 'Bcc: ' . $adminemail];
  $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));
67

noplanman's avatar
noplanman committed
68 69
  die('Data saved. Will go into effect on next hourly change');
}
dmorley's avatar
dmorley committed
70

noplanman's avatar
noplanman committed
71 72 73
// Forms.
?>
  Authorized to edit <b><?php echo $_domain; ?></b> until <?php echo $pod['tokenexpire']; ?><br>
74 75 76
  <form action="edit.php" method="get">
    <input type="hidden" name="domain" value="<?php echo $_domain; ?>">
    <input type="hidden" name="token" value="<?php echo $_token; ?>">
noplanman's avatar
noplanman committed
77 78 79
    <label>Email <input type="text" size="20" 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="100" rows="7" name="podmin_statement"><?php echo $pod['podmin_statement']; ?></textarea></label><br>
    <label>Weight <input type="text" size="2" name="weight" value="<?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
80
    <label>Notify if pod falls to hidden status? <input type="checkbox" name="podmin_notify" <?php echo $pod['podmin_notify'] ? 'checked' : ''; ?> ></label><br>
81 82 83 84 85 86 87 88 89 90
    <input type="submit" name="action" value="save">
  </form>
  <br>
  <br>
  <br>
  <form action="edit.php" method="get">
    <input type="hidden" name="domain" value="<?php echo $_domain; ?>">
    <input type="hidden" name="token" value="<?php echo $_token; ?>">
    WARNING: This can not be undone, you will need to add your pod again if you want back on list: <input type="submit" name="action" value="delete">
  </form>
noplanman's avatar
noplanman committed
91
<?php