api.php 3.26 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 20
<title>Diaspora Pods</title>
<subtitle>IP Locations of Diaspora pods on podupti.me</subtitle>
noplanman's avatar
noplanman committed
21
<link href="https://podupti.me/"/>
David Morley's avatar
David Morley committed
22 23

EOF;
dmorley's avatar
dmorley committed
24
  $sql    = "SELECT * FROM pods WHERE NOT hidden";
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'],
dmorley's avatar
dmorley committed
35 36
      $row['responsetimems'],
      $row['uptime_alltime'],
37 38 39
      $row['dateupdated'],
      $row['score']
    );
dmorley's avatar
dmorley committed
40
    $scheme   = $row['secure'] ? 'https://' : 'http://';
41
    echo <<<EOF
42
<entry>
43 44
  <title>{$scheme}{$row['domain']}</title>
  <link href="{$scheme}{$row['domain']}"/>
45 46 47
  <id>urn:{$row['domain']}</id>
  <summary>Pod Location is: {$row['country']}
	&#xA;
48
{$summary}</summary>
49 50 51
  <georss:point>{$row['lat']} {$row['long']}</georss:point>
  <georss:featureName>{$row['domain']}</georss:featureName>
</entry>
root's avatar
root committed
52

David Morley's avatar
David Morley committed
53
EOF;
dmorley's avatar
dmorley committed
54
  }
55
  echo '</feed>';
56
} elseif ($_format === 'json') {
dmorley's avatar
dmorley committed
57
  $sql    = 'SELECT id,domain,status,secure,score,userrating,adminrating,city,state,country,lat,long,ip,ipv6,statsurl,monthsmonitored,uptime_alltime,responsetimems,local_posts,comment_counts,dateCreated,dateUpdated,dateLaststats,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
58
  $result = pg_query($dbh, $sql);
59 60
  $result || die('Error in SQL query: ' . pg_last_error());

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

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

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

    $i++ > 0 && print ',';
    printf(
      '%1$s Up %2$s%% This Month - Located in: %3$s',
      $row['domain'],
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);