add.php 3.23 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
3
require_once __DIR__ . '/../logging.php';
dmorley's avatar
dmorley committed
4
require_once __DIR__ . '/../config.php';
dmorley's avatar
dmorley committed
5
$log = new Logging();
dmorley's avatar
dmorley committed
6
$log->lfile(__DIR__ . '/../' . $log_dir . '/add.log');
7 8 9 10
if (!($_domain = $_POST['domain'] ?? null)) {
  $log->lwrite('no domain given');
  die('no pod domain given');
}
noplanman's avatar
noplanman committed
11 12 13 14 15 16 17
if (!($_stats_apikey = $_POST['stats_apikey'] ?? null)) {
  $log->lwrite('no api given ' . $_domain);
  die('no API key for your stats');
}
if (strlen($_stats_apikey) < 14) {
  $log->lwrite('api key too short ' . $_domain);
  die('API key bad needs to be like m58978-80abdb799f6ccf15e3e3787ee');
18
}
19 20
if (!($_email = $_POST['email'] ?? null)) {
  $log->lwrite('no email given ' . $_domain);
21
  die('no email given');
22
}
23 24 25 26
if (!($_terms = $_POST['terms'] ?? null)) {
  $log->lwrite('terms link required ' . $_domain);
  die('no terms link');
}
27

dmorley's avatar
cleanup  
dmorley committed
28
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
29 30
$dbh || die('Error in connection: ' . pg_last_error());

dmorley's avatar
dmorley committed
31
$sql    = 'SELECT domain, stats_apikey FROM pods';
dmorley's avatar
cleanup  
dmorley committed
32
$result = pg_query($dbh, $sql);
33 34
$result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
cleanup  
dmorley committed
35
while ($row = pg_fetch_array($result)) {
36 37
  if ($row['domain'] === $_domain) {
    $log->lwrite('domain already exists ' . $_domain);
38
    die('domain already exists');
dmorley's avatar
cleanup  
dmorley committed
39
  }
noplanman's avatar
noplanman committed
40
  if ($row['stats_apikey'] === $_stats_apikey) {
41
    $log->lwrite('API key already exists ' . $_domain);
42
    die('API key already exists');
dmorley's avatar
cleanup  
dmorley committed
43
  }
44 45
}

dmorley's avatar
cleanup  
dmorley committed
46
$chss = curl_init();
47
curl_setopt($chss, CURLOPT_URL, 'https://' . $_domain . '/nodeinfo/1.0');
dmorley's avatar
cleanup  
dmorley committed
48 49 50 51 52 53 54
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);
55

56
if (stristr($outputssl, 'nodeName')) {
57
  $log->lwrite('Your pod has ssl and is valid ' . $_domain);
58
  echo 'Your pod has ssl and is valid<br>';
dmorley's avatar
fixes  
dmorley committed
59

dmorley's avatar
dmorley committed
60
  $sql    = 'INSERT INTO pods (domain, stats_apikey, email, terms) VALUES ($1, $2, $3, $4)';
noplanman's avatar
noplanman committed
61
  $result = pg_query_params($dbh, $sql, [$_domain, $_stats_apikey, $_email, $_terms]);
62 63
  $result || die('Error in SQL query: ' . pg_last_error());

64
  $to      = $adminemail;
65 66 67 68
  $subject = 'New pod added to ' . $_SERVER['HTTP_HOST'];
  $headers = ['From: ' . $_email, 'Reply-To: ' . $_email, 'Cc: ' . $_email];

  $message_lines = [
dmorley's avatar
dmorley committed
69
    'https://' . $_SERVER['HTTP_HOST'],
70 71 72 73 74 75 76 77
    'Stats Url: https://api.uptimerobot.com/getMonitors?format=json&noJsonCallback=1&customUptimeRatio=7-30-60-90&apiKey=' . $_stats_apikey,
    '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!',
  ];

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

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

81
} else {
dmorley's avatar
dmorley committed
82 83
  $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>';
84
}
dmorley's avatar
dmorley committed
85
$log->lclose();