api.php 3.27 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
cleanup  
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
cleanup  
dmorley committed
16
  echo <<<EOF
noplanman's avatar
noplanman committed
17
<?xml version="1.0" encoding="utf-8"?>
noplanman's avatar
noplanman committed
18
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">
noplanman's avatar
noplanman committed
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
fix api  
David Morley committed
22 23

EOF;
24
  $sql    = "SELECT * FROM pods WHERE hidden <> 'yes'";
dmorley's avatar
cleanup  
dmorley committed
25
  $result = pg_query($dbh, $sql);
26 27
  $result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
cleanup  
dmorley committed
28 29
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
30
    $pod_name = htmlentities($row['name'], ENT_QUOTES);
noplanman's avatar
noplanman committed
31
    $summary  = sprintf(
noplanman's avatar
noplanman committed
32 33 34 35 36 37 38 39
      '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']
    );
noplanman's avatar
noplanman committed
40
    $scheme   = $row['secure'] === 'true' ? 'https://' : 'http://';
41
    echo <<<EOF
noplanman's avatar
noplanman committed
42
<entry>
noplanman's avatar
noplanman committed
43 44
  <title>{$scheme}{$row['domain']}</title>
  <link href="{$scheme}{$row['domain']}"/>
noplanman's avatar
noplanman committed
45 46 47
  <id>urn:{$row['domain']}</id>
  <summary>Pod Location is: {$row['country']}
	&#xA;
noplanman's avatar
noplanman committed
48
{$summary}</summary>
noplanman's avatar
noplanman committed
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
fix api  
David Morley committed
53
EOF;
dmorley's avatar
cleanup  
dmorley committed
54
  }
55
  echo '</feed>';
56
} elseif ($_format === 'json') {
57
  $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
58
  $result = pg_query($dbh, $sql);
59 60
  $result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
cleanup  
dmorley committed
61 62
  //json output, thx Vipul A M for fixing this
  header('Content-type: application/json');
noplanman's avatar
noplanman committed
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
cleanup  
dmorley committed
72 73 74
  } else {
    print json_encode($obj);
  }
75 76 77
} else {
  $i      = 0;
  $sql    = "SELECT * FROM pods WHERE hidden <> 'yes' ORDER BY uptimelast7 DESC";
dmorley's avatar
cleanup  
dmorley committed
78
  $result = pg_query($dbh, $sql);
79 80
  $result || die('Error in SQL query: ' . pg_last_error());

dmorley's avatar
cleanup  
dmorley committed
81 82
  $numrows = pg_num_rows($result);
  while ($row = pg_fetch_array($result)) {
noplanman's avatar
noplanman committed
83 84 85 86 87 88 89 90 91 92 93
//    $status = $row['status'] === 'up' ? 'Online' : 'Offline';
//    $scheme = $row['secure'] === 'true' ? 'https://' : 'http://';
//    $class  = $row['secure'] === 'true' ? 'green' : 'red';

    $i++ > 0 && print ',';
    printf(
      '%1$s Up %2$s%% This Month - Located in: %3$s',
      $row['domain'],
      $row['uptimelast7'],
      $row['country']
    );
94
  }
root's avatar
root committed
95
}
96 97 98

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