api.php 3.77 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');
dmorley's avatar
dmorley committed
21
R::usePartialBeans(true);
22

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

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

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

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

  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());
  }
77

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

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

noplanman's avatar
noplanman committed
100 101
  $i = 0;
  foreach ($pods as $pod) {
102 103
    $i++ > 0 && print ',';
    printf(
104
      '%1$s Up %2$s%% This Month - Located in: %3$s',
noplanman's avatar
noplanman committed
105 106 107
      $pod['domain'],
      $pod['uptimelast7'],
      $pod['country']
108
    );
109
  }
root's avatar
root committed
110
}
noplanman's avatar
noplanman committed
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 138

/**
 * 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;
}