add.php 4.91 KB
Newer Older
1
<!-- /* Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file. */ -->
David Morley's avatar
David Morley committed
2
<?php
noplanman's avatar
noplanman committed
3 4 5 6

use RedBeanPHP\R;

require_once __DIR__ . '/../vendor/autoload.php';
7
require_once __DIR__ . '/../logging.php';
dmorley's avatar
dmorley committed
8
require_once __DIR__ . '/../config.php';
noplanman's avatar
noplanman committed
9 10 11

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

12
$log = new Logging();
dmorley's avatar
dmorley committed
13
$log->lfile(__DIR__ . '/../' . $log_dir . '/add.log');
David Morley's avatar
David Morley committed
14
if (!($_domain = $_GET['domain'] ?? null)) {
15 16 17
  $log->lwrite('no domain given');
  die('no pod domain given');
}
David Morley's avatar
David Morley committed
18

noplanman's avatar
noplanman committed
19
// Other parameters.
David Morley's avatar
David Morley committed
20 21
$_email            = $_GET['email'] ?? '';
$_podmin_statement = $_GET['podmin_statement'] ?? '';
dmorley's avatar
dmorley committed
22
$_podmin_notify    = $_GET['podmin_notify'] ?? 0;
David Morley's avatar
David Morley committed
23

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

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

noplanman's avatar
noplanman committed
33 34 35 36 37 38 39 40
try {
  $pods = R::getAll('
    SELECT id, domain, stats_apikey, publickey, email
    FROM pods
  ');
} catch (\RedBeanPHP\RedException $e) {
  die('Error in SQL query: ' . $e->getMessage());
}
41

noplanman's avatar
noplanman committed
42 43 44
foreach ($pods as $pod) {
  if ($pod['domain'] === $_domain) {
    if ($pod['email']) {
David Morley's avatar
David Morley committed
45 46 47 48 49
      $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'));
noplanman's avatar
noplanman committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63
    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::findOne('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());
      }

David Morley's avatar
David Morley committed
64 65 66 67 68
      echo <<<EOF
      <form action="edit.php" method="get">
      <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>
69
      <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
70 71 72 73
      <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
74

David Morley's avatar
David Morley committed
75 76 77
      die;
    } else {
      $log->lwrite('domain already exists and can be registered' . $_domain);
noplanman's avatar
noplanman committed
78
      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
79
    }
dmorley's avatar
dmorley committed
80
  }
81 82
}

dmorley's avatar
dmorley committed
83
$chss = curl_init();
84
curl_setopt($chss, CURLOPT_URL, 'https://' . $_domain . '/nodeinfo/1.0');
dmorley's avatar
dmorley committed
85 86 87 88 89 90 91
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);
92

dmorley's avatar
dmorley committed
93
if (stristr($outputssl, 'openRegistrations')) {
94
  $log->lwrite('Your pod has ssl and is valid ' . $_domain);
95
  echo 'Your pod has ssl and is valid<br>';
dmorley's avatar
dmorley committed
96

noplanman's avatar
noplanman committed
97 98 99 100 101 102 103 104 105 106 107 108 109
  $publickey = md5(uniqid($_domain, true));

  try {
    $p                     = R::dispense('pods');
    $p['domain']           = $_domain;
    $p['email']            = $_email;
    $p['podmin_statement'] = $_podmin_statement;
    $p['podmin_notify']    = $_podmin_notify;
    $p['publickey']        = $publickey;
    R::store($p);
  } catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
  }
110

dmorley's avatar
dmorley committed
111 112 113 114
  if ($_email) {
    $to      = $adminemail;
    $subject = 'New pod added to ' . $_SERVER['HTTP_HOST'];
    $headers = ['From: ' . $_email, 'Reply-To: ' . $_email, 'Cc: ' . $_email];
115

dmorley's avatar
dmorley committed
116 117 118 119 120 121 122
    $message_lines = [
      'https://' . $_SERVER['HTTP_HOST'],
      'Pod: https://' . $_SERVER['HTTP_HOST'] . '/db/pull.php?debug=1&domain=' . $_domain,
      '',
      'Your pod will not show up right away, as it needs to pass a few checks first.',
      'Give it a few hours!',
    ];
123

dmorley's avatar
dmorley committed
124 125
    @mail($to, $subject, implode("\r\n", $message_lines), implode("\r\n", $headers));
  }
noplanman's avatar
noplanman committed
126

127
  echo 'Data successfully inserted! Your pod will be reviewed and live on the list in a few hours!';
128

129
} else {
dmorley's avatar
dmorley committed
130 131
  $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="https://' . $_domain . '/nodeinfo/1.0">your /nodeinfo</a>';
132
}
133
$log->lclose();