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

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

declare(strict_types=1);

use Poduptime\PodStatus;
10 11
use RedBeanPHP\R;

12
require_once __DIR__ . '/boot.php';
David Morley's avatar
David Morley 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;
}

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
29
        WHERE long != ''
30
            AND lat != ''
David Morley's avatar
David Morley committed
31 32
            AND status < ? 
            AND score > 0
33
    ", [PodStatus::RECHECK]);
34
} catch (\RedBeanPHP\RedException $e) {
35
    die('Error in SQL query: ' . $e->getMessage());
36
}
37

dmorley's avatar
dmorley committed
38
?>
David Morley's avatar
David Morley committed
39 40 41
<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
42 43
<div id="map"></div>
<script type="text/javascript">
44 45 46 47
    var geoJsonData = {
        'type': 'FeatureCollection',
        'features': [
            <?php
48

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

54 55 56 57 58 59
                $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>';
60

61
                $pod_name = htmlentities($pod['name'] ?? '', ENT_QUOTES);
62 63
                $signup   = $pod['signup'] ? 'yes' : 'no';
                echo <<<EOF
64 65 66 67
{
  'type': 'Feature',
  'id': '1',
  'properties' : {
68
    '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}'
69 70 71
  },
  'geometry': {
    'type': 'Point',
72
    'coordinates': [{$pod['long']},{$pod['lat']}]
73
  }
74
}
dmorley's avatar
dmorley committed
75
EOF;
76 77 78 79
            }
            ?>
        ]
    };
80
    var tiles = L.tileLayer('https://{s}.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=<?php echo c('mapboxkey'); ?>', {
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        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
97
</script>