add.php 3.84 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();
7
$log->lfile($log_dir . '/add.php.log');
8
if (!$_POST['url']){
noplanman's avatar
noplanman committed
9
  $log->lwrite('no url given '.$_POST['domain']);
10
  die('no url given');
11 12
}
if (!$_POST['email']){
noplanman's avatar
noplanman committed
13
  $log->lwrite('no email given '.$_POST['domain']);
14
  die('no email given');
15 16
}
if (!$_POST['domain']){
noplanman's avatar
noplanman committed
17
  $log->lwrite('no domain given '.$_POST['domain']);
18
  die('no pod domain given');
19 20
}
if (!$_POST['url']){
noplanman's avatar
noplanman committed
21
  $log->lwrite('no api given '.$_POST['domain']);
22
  die('no API key for your stats');
23 24
}
if (strlen($_POST['url']) < 14){
noplanman's avatar
noplanman committed
25
  $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 32
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
if (!$dbh) {
33
  die('Error in connection: ' . pg_last_error());
34
}
35
$sql = 'SELECT domain,pingdomurl FROM pods';
dmorley's avatar
cleanup  
dmorley committed
36 37
$result = pg_query($dbh, $sql);
if (!$result) {
38
  die('Error in SQL query: ' . pg_last_error());
dmorley's avatar
cleanup  
dmorley committed
39 40
}
while ($row = pg_fetch_array($result)) {
41
  if ($row['domain'] == $_POST['domain']) {
noplanman's avatar
noplanman committed
42
    $log->lwrite('domain already exists '.$_POST['domain']);
43
    die('domain already exists');
dmorley's avatar
cleanup  
dmorley committed
44
  }
45
  if ($row['pingdomurl'] == $_POST['url']) {
noplanman's avatar
noplanman committed
46
    $log->lwrite('API key already exists '.$_POST['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://' . $_POST['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://' . $_POST['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
if (stristr($outputssl, 'nodeName')) {
noplanman's avatar
noplanman committed
73
  $log->lwrite('Your pod has ssl and is valid '.$_POST['domain']);
74
  echo 'Your pod has ssl and is valid<br>';
75 76
  $valid=1;
}
77
if (stristr($output, 'nodeName')) {
noplanman's avatar
noplanman committed
78
  $log->lwrite('Your pod does not have ssl but is a valid pod '.$_POST['domain']);
79
  echo 'Your pod does not have ssl but is a valid pod<br>';
80 81
  $valid=1;
}
82
if ($valid == '1') {
dmorley's avatar
cleanup  
dmorley committed
83 84 85
  $sql = "INSERT INTO pods (domain, pingdomurl, email) VALUES($1, $2, $3)";
  $result = pg_query_params($dbh, $sql, array($_POST['domain'], $_POST['url'], $_POST['email']));
  if (!$result) {
86
    die('Error in SQL query: ' . pg_last_error());
dmorley's avatar
cleanup  
dmorley committed
87 88
  }
  $to = $adminemail;
89 90 91 92 93
  $cc = $_POST['email'];
  $subject = 'New pod added to podupti.me ';
  $message.= "https://podupti.me\n\n Stats Url: https://api.uptimerobot.com/getMonitors?format=json&customUptimeRatio=7-30-60-90&apiKey=" . $_POST['url'] . "\n\n Pod: https://podupti.me/db/pull.php?debug=1&domain=" . $_POST['domain'] . "\n\n";
  $message.= 'Your pod will not show right away, needs to pass a few checks, Give it a few hours!';
  $headers = 'From: ' . $_POST['email'] . "\r\nReply-To: " . $_POST['email'] . "\r\nCc: " . $_POST['email'] . "\r\n";
dmorley's avatar
cleanup  
dmorley committed
94
  @mail( $to, $subject, $message, $headers );    
95

96
  echo 'Data successfully inserted! Your pod will be reviewed and live on the list in a few hours!';
97
    
dmorley's avatar
cleanup  
dmorley committed
98
  pg_free_result($result);
99
    
dmorley's avatar
cleanup  
dmorley committed
100
  pg_close($dbh);
101
} else {
dmorley's avatar
cleanup  
dmorley committed
102
  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>";$log->lwrite('Could not validate your pod on http or https, check your setup! '.$_POST['domain']);
103
}
dmorley's avatar
dmorley committed
104
$log->lclose();