showmap.php 5.33 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
    $pods = R::getAll("
David Morley's avatar
David Morley committed
27
        SELECT domain, signup, name, lat, long, softwarename, uptime_alltime, active_users_halfyear, services, service_xmpp
28
        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
                $feat = '';
David Morley's avatar
David Morley committed
55 56 57 58 59 60
                $services = json_decode($pod['services'] ?? '[]') ?: [];

                // Special case for XMPP, which is not an official service, but listed in the same cell.
                ($pod['service_xmpp'] ?? false) && $services[] = 'xmpp';

                $services_list = [
David Morley's avatar
David Morley committed
61
                    'facebook'    => ['fa' => 'fab fa-facebook-f', 'title' => 'Publish to Facebook'],
David Morley's avatar
David Morley committed
62 63
                    'twitter'     => ['fa' => 'fab fa-twitter', 'title' => 'Publish to Twitter'],
                    'tumblr'      => ['fa' => 'fab fa-tumblr', 'title' => 'Publish to Tumblr'],
David Morley's avatar
David Morley committed
64
                    'wordpress'   => ['fa' => 'fab fa-wordpress-simple', 'title' => 'Publish to WordPress'],
David Morley's avatar
David Morley committed
65 66 67 68 69 70 71 72 73 74 75
                    'xmpp'        => ['fa' => 'fas fa-comments', 'title' => 'XMPP chat server'],
                    'appnet'      => ['fa' => 'fas fa-th', 'title' => 'Publish to App.net'],
                    'atom1.0'     => ['fa' => 'fas fa-rss', 'title' => 'Publish to Atom'],
                    'blogger'     => ['fa' => 'fab fa-blogger-b', 'title' => 'Publish to Blogger'],
                    'gnusocial'   => ['fa' => 'fab fa-grunt', 'title' => 'Publish to GNUSocial'],
                    'google'      => ['fa' => 'fab fa-google', 'title' => 'Publish to Google+'],
                    'libertree'   => ['fa' => 'fas fa-tree', 'title' => 'Publish to Libertree'],
                    'linkedin'    => ['fa' => 'fab fa-linkedin-in', 'title' => 'Publish to LinkedIn'],
                    'livejournal' => ['fa' => 'fas fa-pencil-alt', 'title' => 'Publish to Live Journal'],
                    'pinterest'   => ['fa' => 'fab fa-pinterest-p', 'title' => 'Publish to Pinterest'],
                    'pumpio'      => ['fa' => 'fas fa-greater-than', 'title' => 'Publish to Pump.io'],
David Morley's avatar
David Morley committed
76 77 78 79 80 81 82 83 84
                ];

                foreach ($services as $service) {
                    // Make sure we have this service in the list.
                    if (!isset($services_list[$service])) {
                        continue;
                    }

                    $feat .= sprintf(
David Morley's avatar
David Morley committed
85 86
                        '<div class="smlogo mr-sm-1"><i class="%1$s" data-toggle="tooltip" title="%2$s"></i></div>',
                        $services_list[$service]['fa'],
David Morley's avatar
David Morley committed
87 88 89
                        $services_list[$service]['title']
                    );
                }
90

91
                $pod_name = htmlentities($pod['name'] ?? '', ENT_QUOTES);
92 93
                $signup   = $pod['signup'] ? 'yes' : 'no';
                echo <<<EOF
94 95 96 97
{
  'type': 'Feature',
  'id': '1',
  'properties' : {
98
    '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}'
99 100 101
  },
  'geometry': {
    'type': 'Point',
102
    'coordinates': [{$pod['long']},{$pod['lat']}]
103
  }
104
}
dmorley's avatar
dmorley committed
105
EOF;
106 107 108 109
            }
            ?>
        ]
    };
110
    var tiles = L.tileLayer('https://{s}.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=<?php echo c('mapboxkey'); ?>', {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        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
127
</script>