add.php 3.27 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 11 12
if (!($_domain = $_POST['domain'] ?? null)) {
  $log->lwrite('no domain given');
  die('no pod domain given');
}
if (!($_url = $_POST['url'] ?? null)) {
  $log->lwrite('no url given ' . $_domain);
13
  die('no url given');
14
}
15 16
if (!($_email = $_POST['email'] ?? null)) {
  $log->lwrite('no email given ' . $_domain);
17
  die('no email given');
18
}
19 20 21 22
if (!($_terms = $_POST['terms'] ?? null)) {
  $log->lwrite('terms link required ' . $_domain);
  die('no terms link');
}
23 24
if (!$_url) {
  $log->lwrite('no api given ' . $_domain);
25
  die('no API key for your stats');
26
}
27 28
if (strlen($_url) < 14) {
  $log->lwrite('api key too short ' . $_domain);
29
  die('API key bad needs to be like m58978-80abdb799f6ccf15e3e3787ee');
30
}
31

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

dmorley's avatar
dmorley committed
35
$sql    = 'SELECT domain, stats_apikey FROM pods';
dmorley's avatar
cleanup  
dmorley committed
36
$result = pg_query($dbh, $sql);
37 38
$result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
cleanup  
dmorley committed
39
while ($row = pg_fetch_array($result)) {
40 41
  if ($row['domain'] === $_domain) {
    $log->lwrite('domain already exists ' . $_domain);
42
    die('domain already exists');
dmorley's avatar
cleanup  
dmorley committed
43
  }
dmorley's avatar
dmorley committed
44
  if ($row['stats_apikey'] === $_url) {
45
    $log->lwrite('API key already exists ' . $_domain);
46
    die('API key already exists');
dmorley's avatar
cleanup  
dmorley committed
47
  }
48 49
}

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

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

dmorley's avatar
dmorley committed
64
  $sql    = 'INSERT INTO pods (domain, stats_apikey, email, terms) VALUES ($1, $2, $3, $4)';
65
  $result = pg_query_params($dbh, $sql, [$_domain, $_url, $_email, $_terms]);
66 67
  $result || die('Error in SQL query: ' . pg_last_error());

68
  $to      = $adminemail;
69
  $cc      = $_email;
dmorley's avatar
dmorley committed
70
  $subject = 'New pod added to '. $_SERVER['HTTP_HOST'];
71 72
  $message = sprintf(
    "%1\$s\n\nStats Url: %2\$s\n\nPod: %3\$s\n\n",
dmorley's avatar
dmorley committed
73
    'https://' . $_SERVER['HTTP_HOST'],
noplanman's avatar
noplanman committed
74
    'https://api.uptimerobot.com/getMonitors?format=json&noJsonCallback=1&customUptimeRatio=7-30-60-90&apiKey=' . $_url,
dmorley's avatar
dmorley committed
75
    'https://' . $_SERVER['HTTP_HOST'] . '/db/pull.php?debug=1&domain=' . $_domain
noplanman's avatar
noplanman committed
76
  );
77
  $message .= 'Your pod will not show right away, needs to pass a few checks, Give it a few hours!';
78
  $headers = 'From: ' . $_email . "\r\nReply-To: " . $_email . "\r\nCc: " . $_email . "\r\n";
79
  @mail($to, $subject, $message, $headers);
80

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

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