add.php 3.79 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
if (!$_url) {
  $log->lwrite('no api given ' . $_domain);
21
  die('no API key for your stats');
22
}
23 24
if (strlen($_url) < 14) {
  $log->lwrite('api key too short ' . $_domain);
25
  die('API key bad needs to be like m58978-80abdb799f6ccf15e3e3787ee');
26
}
27 28


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

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

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

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

dmorley's avatar
cleanup  
dmorley committed
58
$ch = curl_init();
59
curl_setopt($ch, CURLOPT_URL, 'http://' . $_domain . '/nodeinfo/1.0');
dmorley's avatar
cleanup  
dmorley committed
60 61 62 63 64 65 66
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);
67

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

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

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

dmorley's avatar
cleanup  
dmorley committed
99
  pg_free_result($result);
100

dmorley's avatar
cleanup  
dmorley committed
101
  pg_close($dbh);
102
} else {
103 104
  $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>';
105
}
dmorley's avatar
dmorley committed
106
$log->lclose();