api.php 3.75 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.
noplanman's avatar
noplanman committed
3 4 5

use RedBeanPHP\R;

6 7 8 9 10 11
($_GET['key'] ?? null) === '4r45tg' || die;

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

noplanman's avatar
noplanman committed
13
require_once __DIR__ . '/vendor/autoload.php';
14 15
require_once __DIR__ . '/config.php';

noplanman's avatar
noplanman committed
16 17 18 19 20
define('PODUPTIME', microtime(true));

// Set up global DB connection.
R::setup("pgsql:host={$pghost};dbname={$pgdb}", $pguser, $pgpass, true);
R::testConnection() || die('Error in DB connection');
21

22
if ($_format === 'georss') {
dmorley's avatar
cleanup  
dmorley committed
23
  echo <<<EOF
noplanman's avatar
noplanman committed
24
<?xml version="1.0" encoding="utf-8"?>
noplanman's avatar
noplanman committed
25
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss">
noplanman's avatar
noplanman committed
26
<title>Diaspora Pods</title>
dmorley's avatar
dmorley committed
27 28
<subtitle>IP Locations of Diaspora pods on {$_SERVER['HTTP_HOST']}</subtitle>
<link href="https://{$_SERVER['HTTP_HOST']}/"/>
David Morley's avatar
fix api  
David Morley committed
29 30

EOF;
noplanman's avatar
noplanman committed
31 32 33 34 35 36 37 38 39 40 41 42

  try {
    $pods = R::getAll('
      SELECT name, monthsmonitored, responsetimelast7, uptimelast7, dateupdated, score, domain, country, lat, long
      FROM pods_apiv1
    ');
  } catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
  }

  foreach ($pods as $pod) {
    $summary = sprintf(
noplanman's avatar
noplanman committed
43
      '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',
noplanman's avatar
noplanman committed
44 45 46 47 48 49
      htmlentities($pod['name'], ENT_QUOTES),
      $pod['monthsmonitored'],
      $pod['responsetimelast7'],
      $pod['uptimelast7'],
      $pod['dateupdated'],
      $pod['score']
noplanman's avatar
noplanman committed
50
    );
51
    echo <<<EOF
noplanman's avatar
noplanman committed
52
<entry>
noplanman's avatar
noplanman committed
53 54 55 56
  <title>https://{$pod['domain']}</title>
  <link href="https://{$pod['domain']}"/>
  <id>urn:{$pod['domain']}</id>
  <summary>Pod Location is: {$pod['country']}
noplanman's avatar
noplanman committed
57
	&#xA;
noplanman's avatar
noplanman committed
58
{$summary}</summary>
noplanman's avatar
noplanman committed
59 60
  <georss:point>{$pod['lat']} {$pod['long']}</georss:point>
  <georss:featureName>{$pod['domain']}</georss:featureName>
noplanman's avatar
noplanman committed
61
</entry>
root's avatar
root committed
62

David Morley's avatar
fix api  
David Morley committed
63
EOF;
dmorley's avatar
cleanup  
dmorley committed
64
  }
65
  echo '</feed>';
66
} elseif ($_format === 'json') {
noplanman's avatar
noplanman committed
67 68 69 70 71 72 73 74 75

  try {
    $pods = R::getAll('
      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_apiv1
    ');
  } catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
  }
76

dmorley's avatar
cleanup  
dmorley committed
77 78
  //json output, thx Vipul A M for fixing this
  header('Content-type: application/json');
noplanman's avatar
noplanman committed
79

noplanman's avatar
noplanman committed
80 81 82
  $obj = [
    'podcount' => count($pods),
    'pods'     => allToString($pods),
noplanman's avatar
noplanman committed
83
  ];
84 85
  if ($_method === 'jsonp') {
    print $_callback . '(' . json_encode($obj) . ')';
dmorley's avatar
cleanup  
dmorley committed
86 87 88
  } else {
    print json_encode($obj);
  }
89
} else {
noplanman's avatar
noplanman committed
90 91 92 93 94 95 96 97
  try {
    $pods = R::getAll('
      SELECT domain, uptimelast7, country
      FROM pods_apiv1
    ');
  } catch (\RedBeanPHP\RedException $e) {
    die('Error in SQL query: ' . $e->getMessage());
  }
noplanman's avatar
noplanman committed
98

noplanman's avatar
noplanman committed
99 100
  $i = 0;
  foreach ($pods as $pod) {
noplanman's avatar
noplanman committed
101 102
    $i++ > 0 && print ',';
    printf(
David Morley's avatar
David Morley committed
103
      '%1$s Up %2$s%% This Month - Located in: %3$s',
noplanman's avatar
noplanman committed
104 105 106
      $pod['domain'],
      $pod['uptimelast7'],
      $pod['country']
noplanman's avatar
noplanman committed
107
    );
108
  }
root's avatar
root committed
109
}
noplanman's avatar
noplanman committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

/**
 * Convert all passed items to strings.
 *
 * This method is for backwards compatibility of APIv1 only!
 * After v2 is released and stable, this can safely be removed.
 *
 * @param array $arr List of all elements to stringify.
 *
 * @return array
 */
function allToString(array $arr)
{
  $ret = $arr;
  foreach ($ret as &$item) {
    if (is_array($item)) {
      /** @var array $item */
      foreach ($item as &$field) {
        $field !== null && $field = (string) $field;
      }
    } else {
      $item !== null && $item = (string) $item;
    }
    unset($field, $item);
  }

  return $ret;
}