api.php 3.86 KB
Newer Older
root's avatar
root committed
1
<?php
dmorley's avatar
dmorley committed
2 3 4 5 6 7

/**
 * API access for pod data.
 */

declare(strict_types=1);
noplanman's avatar
noplanman committed
8 9 10

use RedBeanPHP\R;

11 12 13 14 15 16
($_GET['key'] ?? null) === '4r45tg' || die;

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

noplanman's avatar
noplanman committed
18
require_once __DIR__ . '/vendor/autoload.php';
19 20
require_once __DIR__ . '/config.php';

noplanman's avatar
noplanman committed
21 22 23 24 25
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
26
R::usePartialBeans(true);
27

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

EOF;
noplanman's avatar
noplanman committed
37

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

David Morley's avatar
David Morley committed
69
EOF;
dmorley's avatar
dmorley committed
70 71
    }
    echo '</feed>';
72
} elseif ($_format === 'json') {
dmorley's avatar
dmorley committed
73 74 75 76 77 78 79 80 81 82 83
    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());
    }

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

dmorley's avatar
dmorley committed
85 86 87 88 89 90 91 92 93
    $obj = [
        'podcount' => count($pods),
        'pods'     => allToString($pods),
    ];
    if ($_method === 'jsonp') {
        print $_callback . '(' . json_encode($obj) . ')';
    } else {
        print json_encode($obj);
    }
94
} else {
dmorley's avatar
dmorley committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    try {
        $pods = R::getAll('
            SELECT domain, uptimelast7, country
            FROM pods_apiv1
        ');
    } catch (\RedBeanPHP\RedException $e) {
        die('Error in SQL query: ' . $e->getMessage());
    }

    $i = 0;
    foreach ($pods as $pod) {
        $i++ > 0 && print ',';
        printf(
            '%1$s Up %2$s%% This Month - Located in: %3$s',
            $pod['domain'],
            $pod['uptimelast7'],
            $pod['country']
        );
    }
root's avatar
root committed
114
}
noplanman's avatar
noplanman committed
115 116 117 118 119 120 121 122 123 124 125 126 127

/**
 * 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)
{
dmorley's avatar
dmorley committed
128 129 130 131 132 133 134 135 136
    $ret = $arr;
    foreach ($ret as &$item) {
        if (is_array($item)) {
            $item = allToString($item);
            continue;
        }

        $item !== null && $item = (string) $item;
        unset($item);
noplanman's avatar
noplanman committed
137 138
    }

dmorley's avatar
dmorley committed
139
    return $ret;
noplanman's avatar
noplanman committed
140
}