add.php 3.93 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 32


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

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

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

dmorley's avatar
cleanup  
dmorley committed
51 52
//curl the header of pod with and without https
$chss = curl_init();
53
curl_setopt($chss, CURLOPT_URL, 'https://' . $_domain . '/nodeinfo/1.0');
dmorley's avatar
cleanup  
dmorley committed
54 55 56 57 58 59 60
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);
61

dmorley's avatar
cleanup  
dmorley committed
62
$ch = curl_init();
63
curl_setopt($ch, CURLOPT_URL, 'http://' . $_domain . '/nodeinfo/1.0');
dmorley's avatar
cleanup  
dmorley committed
64 65 66 67 68 69 70
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$output = curl_exec($ch);
curl_close($ch);
71

72
$valid = false;
73
if (stristr($outputssl, 'nodeName')) {
74
  $log->lwrite('Your pod has ssl and is valid ' . $_domain);
75
  echo 'Your pod has ssl and is valid<br>';
76
  $valid = true;
77
}
78
if (stristr($output, 'nodeName')) {
79
  $log->lwrite('Your pod does not have ssl but is a valid pod ' . $_domain);
80
  echo 'Your pod does not have ssl but is a valid pod<br>';
81
  $valid = true;
82
}
83
if ($valid) {
84 85
  $sql    = 'INSERT INTO pods (domain, statsurl, email, terms) VALUES ($1, $2, $3, $4)';
  $result = pg_query_params($dbh, $sql, [$_domain, $_url, $_email, $_terms]);
86 87
  $result || die('Error in SQL query: ' . pg_last_error());

88
  $to      = $adminemail;
89
  $cc      = $_email;
dmorley's avatar
dmorley committed
90
  $subject = 'New pod added to '. $_SERVER['HTTP_HOST'];
91 92
  $message = sprintf(
    "%1\$s\n\nStats Url: %2\$s\n\nPod: %3\$s\n\n",
dmorley's avatar
dmorley committed
93
    'https://' . $_SERVER['HTTP_HOST'],
94
    'https://api.uptimerobot.com/getMonitors?format=json&customUptimeRatio=7-30-60-90&apiKey=' . $_url,
dmorley's avatar
dmorley committed
95
    'https://' . $_SERVER['HTTP_HOST'] . '/db/pull.php?debug=1&domain=' . $_domain
noplanman's avatar
noplanman committed
96
  );
97
  $message .= 'Your pod will not show right away, needs to pass a few checks, Give it a few hours!';
98
  $headers = 'From: ' . $_email . "\r\nReply-To: " . $_email . "\r\nCc: " . $_email . "\r\n";
99
  @mail($to, $subject, $message, $headers);
100

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

dmorley's avatar
cleanup  
dmorley committed
103
  pg_free_result($result);
104

dmorley's avatar
cleanup  
dmorley committed
105
  pg_close($dbh);
106
} else {
107 108
  $log->lwrite('Could not validate your pod on http or https, check your setup! ' . $_domain);
  echo 'Could not validate your pod on http or https, check your setup!<br>Take a look at <a href="https://' . $_domain . '/nodeinfo/1.0">your /nodeinfo</a>';
109
}
dmorley's avatar
dmorley committed
110
$log->lclose();