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 6 7 8
($_GET['key'] ?? null) === '4r45tg' || die;

// Other parameters.
$_format   = $_GET['format'] ?? '';
$_method   = $_GET['method'] ?? '';
$_callback = $_GET['callback'] ?? '';
9 10 11

require_once __DIR__ . '/config.php';

dmorley's avatar
dmorley committed
12
$dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
13 14
$dbh || die('Error in connection: ' . pg_last_error());

15
if ($_format === 'georss') {
dmorley's avatar
dmorley committed
16
  echo <<<EOF
17
<?xml version="1.0" encoding="utf-8"?>
18
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">
19
<title>Diaspora Pods</title>
dmorley's avatar
dmorley committed
20 21
<subtitle>IP Locations of Diaspora pods on {$_SERVER['HTTP_HOST']}</subtitle>
<link href="https://{$_SERVER['HTTP_HOST']}/"/>
David Morley's avatar
David Morley committed
22 23

EOF;
dmorley's avatar
dmorley committed
24
  $sql    = "SELECT name,monthsmonitored,responsetime,uptime_alltime,dateupdated,score,domain,country,lat,long FROM pods";
dmorley's avatar
dmorley committed
25
  $result = pg_query($dbh, $sql);
26 27
  $result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
dmorley committed
28 29
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
30
    $pod_name = htmlentities($row['name'], ENT_QUOTES);
31
    $summary  = sprintf(
32 33 34
      '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'],
35
      $row['responsetime'],
dmorley's avatar
dmorley committed
36
      $row['uptime_alltime'],
37 38 39
      $row['dateupdated'],
      $row['score']
    );
40
    echo <<<EOF
41
<entry>
dmorley's avatar
dmorley committed
42
  <title>https://{$row['domain']}</title>
43
  <link href="{$scheme}{$row['domain']}"/>
44 45 46
  <id>urn:{$row['domain']}</id>
  <summary>Pod Location is: {$row['country']}
	&#xA;
47
{$summary}</summary>
48 49 50
  <georss:point>{$row['lat']} {$row['long']}</georss:point>
  <georss:featureName>{$row['domain']}</georss:featureName>
</entry>
root's avatar
root committed
51

David Morley's avatar
David Morley committed
52
EOF;
dmorley's avatar
dmorley committed
53
  }
54
  echo '</feed>';
55
} elseif ($_format === 'json') {
dmorley's avatar
dmorley committed
56
  $sql    = 'SELECT id,domain,status,secure,score,userrating,adminrating,city,state,country,lat,long,ip,ipv6,stats_apikey,monthsmonitored,uptime_alltime,responsetime,local_posts,comment_counts,date_created,date_updated,date_laststats,hidden,terms,sslexpire,uptime_custom,dnssec,softwarename,total_users,local_posts,comment_counts,service_facebook,service_twitter,service_tumblr,service_wordpress,service_xmpp FROM pods';
dmorley's avatar
dmorley committed
57
  $result = pg_query($dbh, $sql);
58 59
  $result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
dmorley committed
60 61
  //json output, thx Vipul A M for fixing this
  header('Content-type: application/json');
62 63 64 65 66 67 68

  $numrows = pg_num_rows($result);
  $rows    = array_values(pg_fetch_all($result));
  $obj     = [
    'podcount' => $numrows,
    'pods'     => $rows,
  ];
69 70
  if ($_method === 'jsonp') {
    print $_callback . '(' . json_encode($obj) . ')';
dmorley's avatar
dmorley committed
71 72 73
  } else {
    print json_encode($obj);
  }
74 75
} else {
  $i      = 0;
76
  $sql    = "SELECT domain,uptime_alltime,country,status FROM pods";
dmorley's avatar
dmorley committed
77
  $result = pg_query($dbh, $sql);
78 79
  $result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
dmorley committed
80 81
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
82 83 84

    $i++ > 0 && print ',';
    printf(
85
      '%1$s is %2$s now - online %3$s%% This Month - Located in: %4$s',
86
      $row['domain'],
87
      $row['status'],
dmorley's avatar
dmorley committed
88
      $row['uptime_alltime'],
89 90
      $row['country']
    );
91
  }
root's avatar
root committed
92
}
93 94 95

pg_free_result($result);
pg_close($dbh);