add.php 4 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
$valid=0;
 include('config.php');
dmorley's avatar
dmorley committed
5 6 7
 include('../logging.php');
$log = new Logging();
$log->lfile($log_dir."/add.php.log");
8
if (!$_POST['url']){
dmorley's avatar
dmorley committed
9
  echo "no url given";$log->lwrite('no url given '.$_POST['domain']);
10 11 12
 die;
}
if (!$_POST['email']){
dmorley's avatar
dmorley committed
13
  echo "no email given";$log->lwrite('no email given '.$_POST['domain']);
14 15 16
 die;
}
if (!$_POST['domain']){
dmorley's avatar
dmorley committed
17
  echo "no pod domain given";$log->lwrite('no domain given '.$_POST['domain']);
18 19 20
 die;
}
if (!$_POST['url']){
dmorley's avatar
dmorley committed
21
  echo "no API key for your stats";$log->lwrite('no api given '.$_POST['domain']);
22 23 24
 die;
}
if (strlen($_POST['url']) < 14){
dmorley's avatar
dmorley committed
25
  echo "API key bad needs to be like m58978-80abdb799f6ccf15e3e3787ee";$log->lwrite('api key too short '.$_POST['domain']);
26 27 28 29 30 31 32 33 34 35 36 37 38
 die;
}
 $dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
     if (!$dbh) {
         die("Error in connection: " . pg_last_error());
     }
 $sql = "SELECT domain,pingdomurl FROM pods";
 $result = pg_query($dbh, $sql);
 if (!$result) {
     die("Error in SQL query: " . pg_last_error());
 }
 while ($row = pg_fetch_array($result)) {
if ($row["domain"] == $_POST['domain']) {
dmorley's avatar
dmorley committed
39
echo "domain already exists";$log->lwrite('domain already exists '.$_POST['domain']);die;
40 41
}
if ($row["pingdomurl"] == $_POST['url']) {
dmorley's avatar
dmorley committed
42
echo "API key already exists";$log->lwrite('API key already exists '.$_POST['domain']);die;
43 44 45 46 47 48
}
 }

     //curl the header of pod with and without https

        $chss = curl_init();
49
        curl_setopt($chss, CURLOPT_URL, "https://".$_POST['domain']."/nodeinfo/1.0");
dmorley's avatar
dmorley committed
50
        curl_setopt($chss, CURLOPT_POST, 0);
51
        curl_setopt($chss, CURLOPT_HEADER, 0);
52 53
        curl_setopt($chss, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($chss, CURLOPT_RETURNTRANSFER, 1);
54
        curl_setopt($chss, CURLOPT_NOBODY, 0);
55 56 57 58
        $outputssl = curl_exec($chss);
        curl_close($chss);

        $ch = curl_init();
59
        curl_setopt($ch, CURLOPT_URL, "http://".$_POST['domain']."/nodeinfo/1.0");
dmorley's avatar
dmorley committed
60
        curl_setopt($ch, CURLOPT_POST, 0);
61
        curl_setopt($ch, CURLOPT_HEADER, 0);
62 63
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
64
        curl_setopt($ch, CURLOPT_NOBODY, 0);
65 66 67
        $output = curl_exec($ch);
        curl_close($ch);

68
if (stristr($outputssl, 'nodeName')) {
dmorley's avatar
dmorley committed
69
  echo "Your pod has ssl and is valid<br>";$log->lwrite('Your pod has ssl and is valid '.$_POST['domain']);
70 71
  $valid=1;
}
72
if (stristr($output, 'nodeName')) {
dmorley's avatar
dmorley committed
73
  echo "Your pod does not have ssl but is a valid pod<br>";$log->lwrite('Your pod does not have ssl but is a valid pod '.$_POST['domain']);
74 75
  $valid=1;
}
76

77
if ($valid=="1") {    
dmorley's avatar
fix  
dmorley committed
78
     $sql = "INSERT INTO pods (domain, pingdomurl, email) VALUES($1, $2, $3)";
79 80 81 82 83
     $result = pg_query_params($dbh, $sql, array($_POST['domain'], $_POST['url'], $_POST['email']));
     if (!$result) {
         die("Error in SQL query: " . pg_last_error());
     }
     $to = $adminemail;
dmorley's avatar
dmorley committed
84 85
     $cc = $_POST["email"];
     $subject = "New pod added to podupti.me ";
dmorley's avatar
cleanup  
dmorley committed
86
     $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";
dmorley's avatar
dmorley committed
87 88
     $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";
89
     @mail( $to, $subject, $message, $headers );    
90

dmorley's avatar
dmorley committed
91
     echo "Data successfully inserted! Your pod will be reviewed and live on the list in a few hours!";
92 93 94 95 96
    
     pg_free_result($result);
    
     pg_close($dbh);
} else {
dmorley's avatar
dmorley committed
97
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']);
98
}
dmorley's avatar
dmorley committed
99
$log->lclose();
100
?>