add.php 3.89 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
$valid = 0;
4 5
require_once __DIR__ . '/../logging.php';

dmorley's avatar
dmorley committed
6
$log = new Logging();
David Morley's avatar
David Morley committed
7
$log->lfile(__DIR__ . $log_dir . '/add.log');
8 9
if (!$_POST['url']) {
  $log->lwrite('no url given ' . $_POST['domain']);
10
  die('no url given');
11
}
12 13
if (!$_POST['email']) {
  $log->lwrite('no email given ' . $_POST['domain']);
14
  die('no email given');
15
}
16 17
if (!$_POST['domain']) {
  $log->lwrite('no domain given ' . $_POST['domain']);
18
  die('no pod domain given');
19
}
20 21
if (!$_POST['url']) {
  $log->lwrite('no api given ' . $_POST['domain']);
22
  die('no API key for your stats');
23
}
24 25
if (strlen($_POST['url']) < 14) {
  $log->lwrite('api key too short ' . $_POST['domain']);
26
  die('API key bad needs to be like m58978-80abdb799f6ccf15e3e3787ee');
27
}
28 29 30

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

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

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

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

dmorley's avatar
cleanup  
dmorley committed
49 50
//curl the header of pod with and without https
$chss = curl_init();
51
curl_setopt($chss, CURLOPT_URL, 'https://' . $_POST['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

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

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

85 86
  $to      = $adminemail;
  $cc      = $_POST['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 91 92 93
    'https://podupti.me',
    'https://api.uptimerobot.com/getMonitors?format=json&customUptimeRatio=7-30-60-90&apiKey=' . $_POST['url'],
    'https://podupti.me/db/pull.php?debug=1&domain=' . $_POST['domain']
  );
94
  $message .= 'Your pod will not show right away, needs to pass a few checks, Give it a few hours!';
95
  $headers = 'From: ' . $_POST['email'] . "\r\nReply-To: " . $_POST['email'] . "\r\nCc: " . $_POST['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 {
noplanman's avatar
noplanman committed
104 105
  $log->lwrite('Could not validate your pod on http or https, check your setup! ' . $_POST['domain']);
  echo 'Could not validate your pod on http or https, check your setup!<br>Take a look at <a href="https://' . $_POST['domain'] . '/nodeinfo/1.0">your /nodeinfo</a>';
106
}
dmorley's avatar
dmorley committed
107
$log->lclose();