api-more.php 1.73 KB
Newer Older
dmorley's avatar
dmorley committed
1 2
<?php

3 4 5 6 7 8 9
/**
 * This is just a single API for a pod for the Android app to get data.
 */

declare(strict_types=1);

use Poduptime\PodStatus;
noplanman's avatar
noplanman committed
10 11
use RedBeanPHP\R;

dmorley's avatar
dmorley committed
12 13 14 15 16 17
// Required parameters.
($_domain = $_GET['domain'] ?? null) || die('no domain given');

// Other parameters.
$_format = $_GET['format'] ?? '';

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

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);
noplanman's avatar
noplanman committed
27 28

try {
29 30 31 32 33
    $pod = R::getRow('
        SELECT hgitdate, 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
        WHERE domain = ?
    ', [$_domain]);
noplanman's avatar
noplanman committed
34
} catch (\RedBeanPHP\RedException $e) {
35
    die('Error in SQL query: ' . $e->getMessage());
noplanman's avatar
noplanman committed
36 37 38
}

if ($_format === 'json') {
39
    echo json_encode($pod);
noplanman's avatar
noplanman committed
40
} else {
41 42 43 44 45 46 47 48 49 50 51 52 53 54
    if ($pod['status'] === PodStatus::UP) {
        echo 'Status: Up<br>';
    }
    if ($pod['status'] === PodStatus::DOWN) {
        echo 'Status: Down<br>';
    }
    echo 'Last Git Pull: ' . $pod['hgitdate'] . '<br>';
    echo 'Uptime This Month ' . $pod['uptimelast7'] . '<br>';
    echo 'Months Monitored: ' . $pod['monthsmonitored'] . '<br>';
    echo 'Response Time: ' . $pod['responsetimelast7'] . '<br>';
    echo 'User Rating: ' . $pod['userrating'] . '<br>';
    echo 'Server Location: ' . $pod['country'] . '<br>';
    echo 'Latitude: ' . $pod['lat'] . '<br>';
    echo 'Longitude: ' . $pod['long'] . '<br>';
dmorley's avatar
dmorley committed
55
}