api.php 3.16 KB
Newer Older
root's avatar
root committed
1
<?php
2
//Copyright (c) 2011, David Morley. This file is licensed under the Affero General Public License version 3 or later. See the COPYRIGHT file.
3
if ($_GET['key'] != '4r45tg') {die;}
4 5 6

require_once __DIR__ . '/config.php';

dmorley's avatar
cleanup  
dmorley committed
7 8
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
if (!$dbh) {
9
  die('Error in connection: ' . pg_last_error());
David Morley's avatar
fix api  
David Morley committed
10
}
11
if ($_GET['format'] == 'georss') {
dmorley's avatar
cleanup  
dmorley committed
12 13
  echo <<<EOF
  <?xml version="1.0" encoding="utf-8"?>
David Morley's avatar
fix api  
David Morley committed
14
  <feed xmlns="http://www.w3.org/2005/Atom"
15 16 17 18
  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/"/>
David Morley's avatar
fix api  
David Morley committed
19 20

EOF;
dmorley's avatar
cleanup  
dmorley committed
21 22 23
  $sql = "SELECT * FROM pods WHERE hidden <> 'yes'";
  $result = pg_query($dbh, $sql);
  if (!$result) {
24
   die('Error in SQL query: ' . pg_last_error());
David Morley's avatar
fix api  
David Morley committed
25
 }
dmorley's avatar
cleanup  
dmorley committed
26 27
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
28 29 30 31 32
    $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 100 this pod is a {$row['score']} right now";
    if ($row['secure'] == 'true') {$method = 'https://';} else {$method = 'http://';}
David Morley's avatar
fix api  
David Morley committed
33 34 35 36 37 38
   echo <<<EOF
   <entry>
   <title>{$method}{$row['domain']}</title>
   <link href="{$method}{$row['domain']}"/>
   <id>urn:{$row['domain']}</id>
   <summary>Pod Location is: {$row['country']}
dmorley's avatar
dmorley committed
39
	&#xA;{$tip}</summary>
David Morley's avatar
fix api  
David Morley committed
40 41 42
   <georss:point>{$row['lat']} {$row['long']}</georss:point>
   <georss:featureName>{$row['domain']}</georss:featureName>
   </entry>
root's avatar
root committed
43

David Morley's avatar
fix api  
David Morley committed
44
EOF;
dmorley's avatar
cleanup  
dmorley committed
45
  }
46
  echo '</feed>';
root's avatar
root committed
47
}
48 49
elseif ($_GET['format'] == 'json') {
  $sql = 'SELECT id,domain,status,secure,score,userrating,adminrating,city,state,country,lat,long,ip,ipv6,pingdomurl,monthsmonitored,uptimelast7,responsetimelast7,local_posts,comment_counts,dateCreated,dateUpdated,dateLaststats,hidden FROM pods';
dmorley's avatar
cleanup  
dmorley committed
50 51
  $result = pg_query($dbh, $sql);
  if (!$result) {
52
    die('Error in SQL query: ' . pg_last_error());
dmorley's avatar
cleanup  
dmorley committed
53 54 55 56 57 58 59
  }
  $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;
60
  if ($_GET['method'] == 'jsonp') {
dmorley's avatar
cleanup  
dmorley committed
61 62 63 64
    print $_GET['callback'] . '(' . json_encode($obj) . ')';
  } else {
    print json_encode($obj);
  }
David Morley's avatar
fix api  
David Morley committed
65 66
}
 else {
dmorley's avatar
cleanup  
dmorley committed
67 68 69 70
  $i=0;
  $sql = "SELECT * FROM pods WHERE hidden <> 'yes' ORDER BY uptimelast7 DESC";
  $result = pg_query($dbh, $sql);
  if (!$result) {
71
    die('Error in SQL query: ' . pg_last_error());
dmorley's avatar
cleanup  
dmorley committed
72 73 74
  }
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
75 76 77
    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'];
dmorley's avatar
cleanup  
dmorley committed
78
    if ($i < ($numrows -1)) {
79
      echo ',';
dmorley's avatar
cleanup  
dmorley committed
80 81
    }
    $i++;
David Morley's avatar
fix api  
David Morley committed
82 83 84 85 86

}

 pg_free_result($result);
 pg_close($dbh);
root's avatar
root committed
87
}