api.php 3.19 KB
Newer Older
root's avatar
root committed
1
<?php
2 3
//Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file.
if ($_GET['key'] != "4r45tg") {exit;}
dmorley's avatar
cleanup  
dmorley committed
4 5 6 7 8
include('db/config.php');
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
if (!$dbh) {
  die("Error in connection: " . pg_last_error());
}  
9
if ($_GET['format'] == "georss") {
dmorley's avatar
cleanup  
dmorley committed
10 11 12
  echo <<<EOF
  <?xml version="1.0" encoding="utf-8"?>
  <feed xmlns="http://www.w3.org/2005/Atom" 
13 14 15 16
  xmlns:georss="http://www.georss.org/georss">
  <title>Diaspora Pods</title>
  <subtitle>IP Locations of Diaspora pods on podupti.me</subtitle>
  <link href="http://podupti.me/"/>
dmorley's avatar
cleanup  
dmorley committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  EOF;
  $sql = "SELECT * FROM pods WHERE hidden <> 'yes'";
  $result = pg_query($dbh, $sql);
  if (!$result) {
    die("Error in SQL query: " . pg_last_error());
  }
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
    $pod_name = htmlentities($row["name"], ENT_QUOTES);
    $tip="";
    $tip.="\n This pod {$pod_name} has been watched for {$row["monthsmonitored"]} months and its average ping time is {$row["responsetimelast7"]} with uptime of {$row["uptimelast7"]}% this month and was last checked on {$row["dateupdated"]}. ";
    $tip.="On a score of -20 to +20 this pod is a {$row["score"]} right now";
    if ($row["secure"] == "true") {$method = "https://";} else {$method = "http://";}
    echo <<<EOF
    <entry>
32 33 34
    <title>{$method}{$row['domain']}</title>
    <link href="{$method}{$row['domain']}"/>
    <id>urn:{$row['domain']}</id>
dmorley's avatar
dmorley committed
35 36
    <summary>Pod Location is: {$row['country']} 
	&#xA;{$tip}</summary>
37 38
    <georss:point>{$row['lat']} {$row['long']}</georss:point>
    <georss:featureName>{$row['domain']}</georss:featureName>
dmorley's avatar
cleanup  
dmorley committed
39
    </entry>
root's avatar
root committed
40

dmorley's avatar
cleanup  
dmorley committed
41 42 43
    EOF;
  }
  echo "</feed>";
root's avatar
root committed
44
}
45
elseif ($_GET['format'] == "json") {
dmorley's avatar
cleanup  
dmorley committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  $sql = "SELECT id,domain,status,secure,score,userrating,adminrating,city,state,country,lat,long,ip,ipv6,hgitdate,hgitref,pingdomurl,pingdomlast,monthsmonitored,uptimelast7,responsetimelast7,hruntime,hencoding,dateCreated,dateUpdated,dateLaststats,hidden FROM pods";
  $result = pg_query($dbh, $sql);
  if (!$result) {
    die("Error in SQL query: " . pg_last_error());
  }
  $numrows = pg_num_rows($result);
  //json output, thx Vipul A M for fixing this
  header('Content-type: application/json');
  $rows=array_values(pg_fetch_all($result));
  $obj->podcount          = $numrows;
  $obj->pods             = $rows;
  if ($_GET['method'] == "jsonp") {
    print $_GET['callback'] . '(' . json_encode($obj) . ')';
  } else {
    print json_encode($obj);
  }
root's avatar
root committed
62
} else {
dmorley's avatar
cleanup  
dmorley committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  $i=0;
  $sql = "SELECT * FROM pods WHERE hidden <> 'yes' ORDER BY uptimelast7 DESC";
  $result = pg_query($dbh, $sql);
  if (!$result) {
    die("Error in SQL query: " . pg_last_error());
  }
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
    if ($row["status"] == "up"){$status="Online";}else{$status="Offline";} 
    if ($row["secure"] == "true") {$method = "https://";$class="green";} else {$method = "http://";$class="red";}
    echo $row["domain"] ." Up ".$row["uptimelast7"]."% This Month - Located in: ".$row["country"];
    if ($i < ($numrows -1)) {
      echo ",";
    }
    $i++;
78
  }
root's avatar
root committed
79
}
dmorley's avatar
cleanup  
dmorley committed
80 81
pg_free_result($result);       
pg_close($dbh);
root's avatar
root committed
82
?>