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

dmorley's avatar
dmorley committed
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;

noplanman's avatar
noplanman committed
12
require_once __DIR__ . '/boot.php';
dmorley's avatar
dmorley committed
13 14 15 16

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

dmorley's avatar
dmorley committed
17 18 19 20 21 22 23
if ($country_code) {
    $lat  = country(strtolower($country_code))->getLatitudeDesc();
    $long = country(strtolower($country_code))->getLongitudeDesc();
} else {
    $lat  = 31;
    $long = -99;
}
dmorley's avatar
dmorley committed
24

dmorley's avatar
3.2.1  
dmorley committed
25 26 27 28 29 30 31 32 33 34 35 36 37
define('PODUPTIME', microtime(true));

// 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
38
try {
dmorley's avatar
dmorley committed
39
    $pods = R::getAll("
dmorley's avatar
dmorley committed
40
        SELECT domain, signup, name, lat, long, softwarename, uptime_alltime, active_users_halfyear, services, service_xmpp
dmorley's avatar
dmorley committed
41 42 43
        FROM pods
        WHERE long != ''
            AND lat != ''
dmorley's avatar
dmorley committed
44 45
            AND status < ? 
            AND score > 0
dmorley's avatar
dmorley committed
46
    ", [PodStatus::RECHECK]);
noplanman's avatar
noplanman committed
47
} catch (\RedBeanPHP\RedException $e) {
dmorley's avatar
dmorley committed
48
    die('Error in SQL query: ' . $e->getMessage());
noplanman's avatar
noplanman committed
49
}
50

dmorley's avatar
dmorley committed
51
?>
dmorley's avatar
dmorley committed
52 53 54
<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
55 56
<div id="map"></div>
<script type="text/javascript">
dmorley's avatar
dmorley committed
57 58 59 60
    var geoJsonData = {
        'type': 'FeatureCollection',
        'features': [
            <?php
noplanman's avatar
noplanman committed
61

dmorley's avatar
dmorley committed
62 63 64 65
            $i = 0;
            foreach ($pods as $pod) {
                // If this isn't the first entry, put a comma to separate the entries.
                $i++ > 0 && print ',';
noplanman's avatar
noplanman committed
66

dmorley's avatar
dmorley committed
67
                $feat = '';
dmorley's avatar
dmorley committed
68 69 70 71 72 73
                $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 = [
dmorley's avatar
map too  
dmorley committed
74
                    'facebook'    => ['fa' => 'fab fa-facebook-f', 'title' => 'Publish to Facebook'],
dmorley's avatar
dmorley committed
75 76
                    'twitter'     => ['fa' => 'fab fa-twitter', 'title' => 'Publish to Twitter'],
                    'tumblr'      => ['fa' => 'fab fa-tumblr', 'title' => 'Publish to Tumblr'],
dmorley's avatar
dmorley committed
77
                    'wordpress'   => ['fa' => 'fab fa-wordpress-simple', 'title' => 'Publish to WordPress'],
dmorley's avatar
map too  
dmorley committed
78 79 80 81 82 83 84 85 86 87 88
                    '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'],
dmorley's avatar
dmorley committed
89 90 91 92 93 94 95 96 97
                ];

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

                    $feat .= sprintf(
dmorley's avatar
map too  
dmorley committed
98 99
                        '<div class="smlogo mr-sm-1"><i class="%1$s" data-toggle="tooltip" title="%2$s"></i></div>',
                        $services_list[$service]['fa'],
dmorley's avatar
dmorley committed
100 101 102
                        $services_list[$service]['title']
                    );
                }
noplanman's avatar
noplanman committed
103

dmorley's avatar
dmorley committed
104 105 106
                $pod_name = htmlentities($pod['name'] ?? '', ENT_QUOTES);
                $signup   = $pod['signup'] ? 'yes' : 'no';
                echo <<<EOF
noplanman's avatar
noplanman committed
107 108 109 110
{
  'type': 'Feature',
  'id': '1',
  'properties' : {
noplanman's avatar
noplanman committed
111
    '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}'
noplanman's avatar
noplanman committed
112 113 114
  },
  'geometry': {
    'type': 'Point',
noplanman's avatar
noplanman committed
115
    'coordinates': [{$pod['long']},{$pod['lat']}]
noplanman's avatar
noplanman committed
116
  }
noplanman's avatar
noplanman committed
117
}
dmorley's avatar
dmorley committed
118
EOF;
dmorley's avatar
dmorley committed
119 120 121 122
            }
            ?>
        ]
    };
noplanman's avatar
noplanman committed
123
    var tiles = L.tileLayer('https://{s}.tiles.mapbox.com/v4/mapbox.streets/{z}/{x}/{y}.jpg70?access_token=<?php echo c('mapboxkey'); ?>', {
dmorley's avatar
dmorley committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        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
mapview  
dmorley committed
140
</script>