api.php 3.3 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 4 5
if ($_GET['key'] != '4r45tg') {
  die;
}
6 7 8

require_once __DIR__ . '/config.php';

dmorley's avatar
cleanup  
dmorley committed
9 10
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
if (!$dbh) {
11
  die('Error in connection: ' . pg_last_error());
David Morley's avatar
fix api  
David Morley committed
12
}
13

14
if ($_GET['format'] == 'georss') {
dmorley's avatar
cleanup  
dmorley committed
15
  echo <<<EOF
noplanman's avatar
noplanman committed
16 17 18 19 20 21
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"
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
22 23

EOF;
24
  $sql    = "SELECT * FROM pods WHERE hidden <> 'yes'";
dmorley's avatar
cleanup  
dmorley committed
25 26
  $result = pg_query($dbh, $sql);
  if (!$result) {
27 28
    die('Error in SQL query: ' . pg_last_error());
  }
dmorley's avatar
cleanup  
dmorley committed
29 30
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
31
    $pod_name = htmlentities($row['name'], ENT_QUOTES);
32
    $tip      = sprintf(
noplanman's avatar
noplanman committed
33 34 35 36 37 38 39 40
      'This pod %1$s has been watched for %2$s months and its average ping time is %3$s with uptime of %4$s%% this month and was last checked on %5$s. On a score of 100 this pod is a %6$s right now',
      $pod_name,
      $row['monthsmonitored'],
      $row['responsetimelast7'],
      $row['uptimelast7'],
      $row['dateupdated'],
      $row['score']
    );
41 42 43 44 45 46
    if ($row['secure'] == 'true') {
      $method = 'https://';
    } else {
      $method = 'http://';
    }
    echo <<<EOF
noplanman's avatar
noplanman committed
47 48 49 50 51 52 53 54 55 56
<entry>
  <title>{$method}{$row['domain']}</title>
  <link href="{$method}{$row['domain']}"/>
  <id>urn:{$row['domain']}</id>
  <summary>Pod Location is: {$row['country']}
	&#xA;
{$tip}</summary>
  <georss:point>{$row['lat']} {$row['long']}</georss:point>
  <georss:featureName>{$row['domain']}</georss:featureName>
</entry>
root's avatar
root committed
57

David Morley's avatar
fix api  
David Morley committed
58
EOF;
dmorley's avatar
cleanup  
dmorley committed
59
  }
60
  echo '</feed>';
61 62
} 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
63 64
  $result = pg_query($dbh, $sql);
  if (!$result) {
65
    die('Error in SQL query: ' . pg_last_error());
dmorley's avatar
cleanup  
dmorley committed
66 67 68 69
  }
  $numrows = pg_num_rows($result);
  //json output, thx Vipul A M for fixing this
  header('Content-type: application/json');
70 71 72
  $rows          = array_values(pg_fetch_all($result));
  $obj->podcount = $numrows;
  $obj->pods     = $rows;
73
  if ($_GET['method'] == 'jsonp') {
dmorley's avatar
cleanup  
dmorley committed
74 75 76 77
    print $_GET['callback'] . '(' . json_encode($obj) . ')';
  } else {
    print json_encode($obj);
  }
78 79 80
} else {
  $i      = 0;
  $sql    = "SELECT * FROM pods WHERE hidden <> 'yes' ORDER BY uptimelast7 DESC";
dmorley's avatar
cleanup  
dmorley committed
81 82
  $result = pg_query($dbh, $sql);
  if (!$result) {
83
    die('Error in SQL query: ' . pg_last_error());
dmorley's avatar
cleanup  
dmorley committed
84 85 86
  }
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
87 88 89 90 91 92 93 94 95 96 97 98
    if ($row['status'] == 'up') {
      $status = 'Online';
    } else {
      $status = 'Offline';
    }
    if ($row['secure'] == 'true') {
      $method = 'https://';
      $class  = 'green';
    } else {
      $method = 'http://';
      $class  = 'red';
    }
99
    echo $row['domain'] . ' Up ' . $row['uptimelast7'] . '% This Month - Located in: ' . $row['country'];
100
    if ($i < ($numrows - 1)) {
101
      echo ',';
dmorley's avatar
cleanup  
dmorley committed
102
    }
103 104
    $i ++;
  }
David Morley's avatar
fix api  
David Morley committed
105

106 107
  pg_free_result($result);
  pg_close($dbh);
root's avatar
root committed
108
}