add.php 3.74 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 4
require_once __DIR__ . '/../logging.php';

dmorley's avatar
dmorley committed
5
$log = new Logging();
David Morley's avatar
David Morley 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 29

require_once __DIR__ . '/../config.php';

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

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

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

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

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

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

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

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

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

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