aboutsummaryrefslogtreecommitdiff
path: root/admin/api/getIpLocation.php
blob: c40d96e67e62b314c7acc390b41809bf98216914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

require_once $_SERVER['DOCUMENT_ROOT'] . "/admin/private/header.api.php";

if (isset($_GET['_']) && strpos($_GET['_'], "/") === false) {
    $arg = $_GET['_'];
} else {
    die();
}

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/admin/private/ipcache.json")) {
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/admin/private/ipcache.json", "[]");
}

$ipCache = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/admin/private/ipcache.json"), true);

$data = null;
foreach ($ipCache as $ip => $item) {
    if ($ip === $_GET['_']) {
        $data = $item;
    }
}

if ($data === null) {
    $data = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $arg));
    $ipCache[$_GET['_']] = $data;
    file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/admin/private/ipcache.json", json_encode($ipCache));
}

if ($data["geoplugin_status"] === 404) {
    die("<span class='text-muted'>" . l("Local IP address", "Adresse IP locale") . "</span>");
} else {
    if ($data["geoplugin_inEU"]) {
        die($data["geoplugin_countryName"] . " <span class='badge text-light border-light' style='border:1px solid;vertical-align: middle;'>" . l("EU", "UE") . "</span>");
    } else {
        die($data["geoplugin_countryName"]);
    }
}