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

3 4 5 6 7 8 9
/**
 * Add a new pod.
 */

declare(strict_types=1);

use Poduptime\Logging;
noplanman's avatar
noplanman committed
10 11 12
use RedBeanPHP\R;

require_once __DIR__ . '/../vendor/autoload.php';
dmorley's avatar
dmorley committed
13
require_once __DIR__ . '/../config.php';
noplanman's avatar
noplanman committed
14 15 16

define('PODUPTIME', microtime(true));

dmorley's avatar
dmorley committed
17
$log = new Logging();
18
$log->lfile( $log_dir . '/add.log');
David Morley's avatar
David Morley committed
19
if (!($_domain = $_GET['domain'] ?? null)) {
20 21
    $log->lwrite('no domain given');
    die('no pod domain given');
22
}
David Morley's avatar
David Morley committed
23

noplanman's avatar
noplanman committed
24
// Other parameters.
David Morley's avatar
David Morley committed
25 26
$_email            = $_GET['email'] ?? '';
$_podmin_statement = $_GET['podmin_statement'] ?? '';
dmorley's avatar
dmorley committed
27
$_podmin_notify    = $_GET['podmin_notify'] ?? 0;
David Morley's avatar
David Morley committed
28

David Morley's avatar
David Morley committed
29 30
$_domain = strtolower($_domain);
if (!filter_var(gethostbyname($_domain), FILTER_VALIDATE_IP)) {
31
    die('Could not validate the domain name, be sure to enter it as "domain.com" (no caps, no slashes, no extras)');
32
}
33

noplanman's avatar
noplanman committed
34 35 36
// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
dmorley's avatar
dmorley committed
37
R::usePartialBeans(true);
38

noplanman's avatar
noplanman committed
39
try {
40
    $pods = R::getAll('
41
    SELECT id, domain, publickey, email
noplanman's avatar
noplanman committed
42 43 44
    FROM pods
  ');
} catch (\RedBeanPHP\RedException $e) {
45
    die('Error in SQL query: ' . $e->getMessage());
noplanman's avatar
noplanman committed
46
}
47

noplanman's avatar
noplanman committed
48
foreach ($pods as $pod) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    if ($pod['domain'] === $_domain) {
        if ($pod['email']) {
            $log->lwrite('domain already exists and is registered to an owner' . $_domain);
            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) {
            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) {
                die('Error in SQL query: ' . $e->getMessage());
            }

            echo <<<EOF
72
      <form action="/?edit" method="get">
David Morley's avatar
David Morley committed
73 74 75
      <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>
76
      <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>
David Morley's avatar
David Morley committed
77 78 79 80
      <label>Weight <input type="text" size="2" name="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>
      <input type="submit" name="action" value="save">
      </form>
EOF;
noplanman's avatar
noplanman committed
81

82 83 84 85 86
            die;
        } else {
            $log->lwrite('domain already exists and can be registered' . $_domain);
            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>');
        }
David Morley's avatar
David Morley committed
87
    }
88 89
}

90
if ($infos = json_decode(file_get_contents('https://' . $_domain . '/.well-known/nodeinfo'), true)) {
91
    $link = max($infos['links'])['href'];
92
} else {
93
    $link = 'https://' . $_domain . '/.well-known/nodeinfo';
94 95
}

dmorley's avatar
cleanup  
dmorley committed
96
$chss = curl_init();
97
curl_setopt($chss, CURLOPT_URL, $link);
dmorley's avatar
cleanup  
dmorley committed
98 99 100 101 102 103 104
curl_setopt($chss, CURLOPT_POST, 0);
curl_setopt($chss, CURLOPT_HEADER, 0);
curl_setopt($chss, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($chss, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chss, CURLOPT_NOBODY, 0);
$outputssl = curl_exec($chss);
curl_close($chss);
105

dmorley's avatar
dmorley committed
106
if (stristr($outputssl, 'openRegistrations')) {
107 108
    $log->lwrite('Your pod has ssl and is valid ' . $_domain);
    echo 'Your pod has ssl and is valid<br>';
dmorley's avatar
fixes  
dmorley committed
109

110
    $publickey = md5(uniqid($_domain, true));
noplanman's avatar
noplanman committed
111

112 113 114 115 116 117 118
    try {
        $p                     = R::dispense('pods');
        $p['domain']           = $_domain;
        $p['email']            = $_email;
        $p['podmin_statement'] = $_podmin_statement;
        $p['podmin_notify']    = $_podmin_notify;
        $p['publickey']        = $publickey;
119

120 121 122 123
        R::store($p);
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }
124

125 126 127 128
    if ($_email) {
        $to      = $adminemail;
        $subject = 'New pod added to ' . $_SERVER['HTTP_HOST'];
        $headers = ['From: ' . $_email, 'Reply-To: ' . $_email, 'Cc: ' . $_email];
129

130 131 132 133 134
        $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!',
        ];
135

136 137
        @mail($to, $subject, implode("\r\n", $message_lines), implode("\r\n", $headers));
    }
noplanman's avatar
noplanman committed
138

139
    echo 'Data successfully inserted! Your pod will be checked and live on the list in a few hours!';
140

141
} else {
142 143
    $log->lwrite('Could not validate your pod, check your setup! ' . $_domain);
    echo 'Could not validate your pod, check your setup!<br>Take a look at <a href="' . $link . '">your /nodeinfo</a>';
144
}
dmorley's avatar
dmorley committed
145
$log->lclose();