add.php 5.13 KB
Newer Older
David Morley's avatar
David Morley committed
1
<?php
noplanman's avatar
noplanman committed
2

dmorley's avatar
dmorley committed
3 4 5 6 7 8
/**
 * Add a new pod.
 */

declare(strict_types=1);

noplanman's avatar
noplanman committed
9 10
use RedBeanPHP\R;

noplanman's avatar
noplanman committed
11
require_once __DIR__ . '/../boot.php';
noplanman's avatar
noplanman committed
12

noplanman's avatar
noplanman committed
13
($_domain = $_GET['domain'] ?? null) || die('no pod domain given');
David Morley's avatar
David Morley committed
14

noplanman's avatar
noplanman committed
15
// Other parameters.
dmorley's avatar
dmorley committed
16 17 18 19
$_email                  = $_GET['email'] ?? '';
$_podmin_statement       = $_GET['podmin_statement'] ?? '';
$_podmin_notify          = $_GET['podmin_notify'] ?? 0;
$_podmin_notify_level    = $_GET['podmin_notify_level'] ?? 50;
David Morley's avatar
David Morley committed
20

David Morley's avatar
David Morley committed
21 22
$_domain = strtolower($_domain);
if (!filter_var(gethostbyname($_domain), FILTER_VALIDATE_IP)) {
dmorley's avatar
dmorley committed
23
    die('Could not validate the domain name, be sure to enter it as "domain.com" (no caps, no slashes, no extras)');
24
}
25

noplanman's avatar
noplanman committed
26
try {
dmorley's avatar
dmorley committed
27
    $pods = R::getAll('
28
    SELECT id, domain, publickey, email
noplanman's avatar
noplanman committed
29 30 31
    FROM pods
  ');
} catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
32
    debug('Error in SQL query: ' . sprintf($e->getMessage()));
noplanman's avatar
noplanman committed
33
}
34

noplanman's avatar
noplanman committed
35
$stop = false;
noplanman's avatar
noplanman committed
36
foreach ($pods as $pod) {
dmorley's avatar
dmorley committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    if ($pod['domain'] === $_domain) {
        if ($pod['email']) {
            die('domain already exists and is registered to an owner, use the edit function to modify');
        }

        $digtxt = exec(escapeshellcmd('dig ' . $_domain . ' TXT +short'));
        if (strpos($digtxt, $pod['publickey']) === false) {
            die('domain already exists, you can claim the domain by adding a DNS TXT record that states<br><b> ' . $_domain . ' IN TXT "' . $pod['publickey'] . '"</b>');
        }

        echo 'domain validated, you can now add details ';
        $uuid   = md5(uniqid($_domain, true));
        $expire = time() + 2700;

        try {
            $p                = R::load('pods', $pod['id']);
            $p['token']       = $uuid;
            $p['tokenexpire'] = date('Y-m-d H:i:s', $expire);

            R::store($p);
        } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
58
            debug('Error in SQL query: ' . sprintf($e->getMessage()));
dmorley's avatar
dmorley committed
59 60 61
        }

        echo <<<EOF
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
<script>
    window.onload = function() {
        $("#weight").ionRangeSlider();
        $("#podmin_notify_level").ionRangeSlider();
    }
</script>
<form action="/">
    <input type="hidden" name="edit">
    <input type="hidden" name="domain" value="{$_domain}">
    <input type="hidden" name="token" value="{$uuid}">
    <label>Email <input type="text" size="20" name="email"></label><br>
    <label>Podmin Statement (You can 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"></textarea></label><br>
    <label>Weight <input type="text" data-min="0" data-step="1" data-max="10" name="weight" id="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>
    <label>Notify when your score falls to <input type="text" data-min="1" data-step="1" data-max="99" data-from="50" name="podmin_notify_level" id="podmin_notify_level"></label><br>
    <input type="submit" name="action" value="save">
</form>
David Morley's avatar
David Morley committed
78
EOF;
noplanman's avatar
noplanman committed
79

noplanman's avatar
noplanman committed
80
        $stop = true;
David Morley's avatar
David Morley committed
81
    }
dmorley's avatar
dmorley committed
82 83
}

dmorley's avatar
3.2.1  
dmorley committed
84
if (!$stop) {
85 86 87 88 89 90 91 92
    $nodeinfo_meta = curl("https://{$_domain}/.well-known/nodeinfo");

    // Default link to fetch node info.
    $nodeinfo_url = "https://{$_domain}/nodeinfo/1.0";

    if ($info = json_decode($nodeinfo_meta['body'] ?: '', true)) {
        if (count($info['links'], COUNT_RECURSIVE) === 2) {
            $nodeinfo_url = $info['links']['href'];
93 94
        } elseif (count($info['links'], COUNT_RECURSIVE) === 4) {
            $nodeinfo_url = $info['links'][1]['href'];
95 96 97
        } else {
            $nodeinfo_url = max($info['links'])['href'];
        }
dmorley's avatar
dmorley committed
98
    }
99

100
    $output = curl($nodeinfo_url, true);
dmorley's avatar
dmorley committed
101 102

    if ($output['body'] && stripos($output['body'], 'openRegistrations') !== false) {
dmorley's avatar
3.2.1  
dmorley committed
103
        echo 'Your pod has ssl and is valid<br>';
104

dmorley's avatar
3.2.1  
dmorley committed
105
        $publickey = md5(uniqid($_domain, true));
noplanman's avatar
noplanman committed
106

dmorley's avatar
dmorley committed
107
        try {
dmorley's avatar
dmorley committed
108 109 110 111 112 113 114
            $p                           = R::dispense('pods');
            $p['domain']                 = $_domain;
            $p['email']                  = $_email;
            $p['podmin_statement']       = $_podmin_statement;
            $p['podmin_notify']          = $_podmin_notify;
            $p['podmin_notify_level']    = $_podmin_notify_level;
            $p['publickey']              = $publickey;
115

dmorley's avatar
dmorley committed
116 117
            R::store($p);
        } catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
118
            debug('Error in SQL query: ' . sprintf($e->getMessage()));
dmorley's avatar
dmorley committed
119 120 121
        }

        if ($_email) {
noplanman's avatar
noplanman committed
122
            $to      = c('adminemail');
dmorley's avatar
dmorley committed
123 124 125
            $subject = 'New pod added to ' . $_SERVER['HTTP_HOST'];
            $headers = ['From: ' . $_email, 'Reply-To: ' . $_email, 'Cc: ' . $_email];

dmorley's avatar
3.2.1  
dmorley committed
126 127 128 129 130 131 132 133 134 135 136
            $message_lines = [
                'https://' . $_SERVER['HTTP_HOST'],
                'Your pod ' . $_domain . ' will not show up right away, as it needs to pass a few checks first.',
                'Give it a few hours!',
            ];

            @mail($to, $subject, implode("\r\n", $message_lines), implode("\r\n", $headers));
        }

        echo 'Data successfully inserted! Your pod will be checked and live on the list in a few hours!';
    } else {
137
        echo 'Could not validate your pod, check your setup!<br>Take a look at your <a href="' . $nodeinfo_url . '">nodeinfo</a>';
dmorley's avatar
3.2.1  
dmorley committed
138
    }
noplanman's avatar
noplanman committed
139
}