api.php 3.59 KB
Newer Older
root's avatar
root committed
1
<?php
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

18
require_once __DIR__ . '/boot.php';
19

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

EOF;
noplanman's avatar
noplanman committed
29

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

David Morley's avatar
David Morley committed
61
EOF;
62 63
    }
    echo '</feed>';
64
} elseif ($_format === 'json') {
65 66 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());
    }

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

77 78 79 80 81 82 83 84 85
    $obj = [
        'podcount' => count($pods),
        'pods'     => allToString($pods),
    ];
    if ($_method === 'jsonp') {
        print $_callback . '(' . json_encode($obj) . ')';
    } else {
        print json_encode($obj);
    }
86
} else {
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    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
106
}
noplanman's avatar
noplanman committed
107 108 109 110 111 112 113 114 115 116 117 118 119

/**
 * 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)
{
120 121 122
    $ret = $arr;
    foreach ($ret as &$item) {
        if (is_array($item)) {
dmorley's avatar
dmorley committed
123 124
            $item = allToString($item);
            continue;
125
        }
dmorley's avatar
dmorley committed
126 127 128

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

131
    return $ret;
noplanman's avatar
noplanman committed
132
}