showmap.php 3.75 KB
Newer Older
dmorley's avatar
dmorley committed
1
<?php
noplanman's avatar
noplanman committed
2

3 4 5 6 7 8 9
/**
 * Show map of pods.
 */

declare(strict_types=1);

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

12
require_once __DIR__ . '/boot.php';
dmorley's avatar
dmorley committed
13 14 15 16 17 18 19 20 21 22 23 24

// CloudFlare country code pull.
$country_code = $_SERVER['HTTP_CF_IPCOUNTRY'] ?? '';

if ($country_code) {
    $lat  = country(strtolower($country_code))->getLatitudeDesc();
    $long = country(strtolower($country_code))->getLongitudeDesc();
} else {
    $lat  = 31;
    $long = -99;
}

noplanman's avatar
noplanman committed
25
try {
26 27 28
    $pods = R::getAll("
        SELECT domain, signup, name, lat, long, softwarename, uptime_alltime, active_users_halfyear, service_facebook, service_twitter, service_tumblr, service_wordpress, service_xmpp
        FROM pods
dmorley's avatar
dmorley committed
29
        WHERE long != ''
30 31 32
            AND lat != ''
            AND status < ?
    ", [PodStatus::RECHECK]);
noplanman's avatar
noplanman committed
33
} catch (\RedBeanPHP\RedException $e) {
34
    die('Error in SQL query: ' . $e->getMessage());
noplanman's avatar
noplanman committed
35
}
36

dmorley's avatar
dmorley committed
37
?>
dmorley's avatar
dmorley committed
38 39 40
<link rel="stylesheet" href="node_modules/leaflet/dist/leaflet.css"/>
<script src="node_modules/leaflet/dist/leaflet.js"></script>
<script type="text/javascript" src="node_modules/leaflet.markercluster/dist/leaflet.markercluster.js"></script>
dmorley's avatar
dmorley committed
41 42
<div id="map"></div>
<script type="text/javascript">
43 44 45 46
    var geoJsonData = {
        'type': 'FeatureCollection',
        'features': [
            <?php
47

48 49 50 51
            $i = 0;
            foreach ($pods as $pod) {
                // If this isn't the first entry, put a comma to separate the entries.
                $i++ > 0 && print ',';
52

53 54 55 56 57 58
                $feat = '';
                $pod['service_facebook'] && $feat .= '<div class="smlogo smlogo-facebook" title="Publish to Facebook"></div>';
                $pod['service_twitter'] && $feat .= '<div class="smlogo smlogo-twitter" title="Publish to Twitter"></div>';
                $pod['service_tumblr'] && $feat .= '<div class="smlogo smlogo-tumblr" title="Publish to Tumblr"></div>';
                $pod['service_wordpress'] && $feat .= '<div class="smlogo smlogo-wordpress" title="Publish to WordPress"></div>';
                $pod['service_xmpp'] && $feat .= '<div class="smlogo smlogo-xmpp"><img src="/images/icon-xmpp.png" width="16" height="16" title="XMPP chat server" alt="XMPP chat server"></div>';
59

dmorley's avatar
dmorley committed
60
                $pod_name = htmlentities($pod['name'] ?? '', ENT_QUOTES);
61 62
                $signup   = $pod['signup'] ? 'yes' : 'no';
                echo <<<EOF
63 64 65 66
{
  'type': 'Feature',
  'id': '1',
  'properties' : {
noplanman's avatar
noplanman committed
67
    'html': '<a href="/go.php?domain={$pod['domain']}">{$pod_name}</a><br>Software: {$pod['softwarename']}<br> Open Signup: {$signup}<br> Users: {$pod['active_users_halfyear']}<br> Uptime: {$pod['uptime_alltime']}%<br> Services:{$feat}'
68 69 70
  },
  'geometry': {
    'type': 'Point',
noplanman's avatar
noplanman committed
71
    'coordinates': [{$pod['long']},{$pod['lat']}]
72
  }
73
}
dmorley's avatar
dmorley committed
74
EOF;
75 76 77 78
            }
            ?>
        ]
    };
79
    var tiles = L.tileLayer('https://{s}.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=<?php echo c('mapboxkey'); ?>', {
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        maxZoom: 18,
        attribution: '<a href="https://www.mapbox.com/about/maps/" target="_blank">&copy; Mapbox &copy; OpenStreetMap</a> <a class="mapbox-improve-map" href="https://www.mapbox.com/map-feedback/" target="_blank">Improve this map</a>'
    });
    var map = L.map('map', {zoom: 4, center: [<?php echo $lat; ?>, <?php echo $long; ?>]}).addLayer(tiles);
    var markers = L.markerClusterGroup({
        maxClusterRadius: 2, animateAddingMarkers: true, iconCreateFunction: function (cluster) {
            return new L.DivIcon({html: '<b class="icon">' + cluster.getChildCount() + '</b>', className: 'mycluster', iconSize: L.point(35, 35)});
        }
    });
    var geoJsonLayer = L.geoJson(geoJsonData, {
        onEachFeature: function (feature, layer) {
            layer.bindPopup(feature.properties.html);
        }
    });
    markers.addLayer(geoJsonLayer);
    map.addLayer(markers);
dmorley's avatar
dmorley committed
96
</script>