showmap.php 6.11 KB
Newer Older
dmorley's avatar
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;

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;
}
24

dmorley's avatar
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
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 ',';
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
dmorley committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                    'facebook'    => ['class' => 'fa fa-facebook', 'title' => 'Publish to Facebook'],
                    'twitter'     => ['class' => 'fa fa-twitter', 'title' => 'Publish to Twitter'],
                    'tumblr'      => ['class' => 'fa fa-tumblr', 'title' => 'Publish to Tumblr'],
                    'wordpress'   => ['class' => 'fa fa-wordpress', 'title' => 'Publish to WordPress'],
                    'xmpp'        => ['class' => 'fa fa-xmpp', 'title' => 'XMPP chat server'],
                    'atom1.0'     => ['class' => 'fa fa-rss-square', 'title' => 'Publish to Atom'],
                    'rss2.0'      => ['class' => 'fa fa-rss', 'title' => 'Publish to RSS'],
                    'blogger'     => ['class' => 'fa fa-bold', 'title' => 'Publish to Blogger'],
                    'gnusocial'   => ['class' => 'fa fa-gnu-social', 'title' => 'Publish to GNUSocial'],
                    'google'      => ['class' => 'fa fa-google-plus', 'title' => 'Publish to Google+'],
                    'libertree'   => ['class' => 'fa fa-tree', 'title' => 'Publish to Libertree'],
                    'medium'      => ['class' => 'fa fa-medium', 'title' => 'Publish to Medium'],
                    'linkedin'    => ['class' => 'fa fa-linkedin', 'title' => 'Publish to LinkedIn'],
                    'livejournal' => ['class' => 'fa fa-book', 'title' => 'Publish to Live Journal'],
                    'pinterest'   => ['class' => 'fa fa-pinterest', 'title' => 'Publish to Pinterest'],
                    'friendica'   => ['class' => 'fa fa-friendica', 'title' => 'Publish to Friendica'],
                    'mastodon'    => ['class' => 'fa fa-mastodon', 'title' => 'Publish to Mastodon'],
                    'smtp'        => ['class' => 'fa fa-envelope-o', 'title' => 'smtp'],
                    'pumpio'      => ['class' => 'fa fa-chevron-circle-right', 'title' => 'Publish to Pump.io'],
dmorley's avatar
dmorley committed
93 94 95 96 97 98 99 100 101
                ];

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

                    $feat .= sprintf(
dmorley's avatar
dmorley committed
102 103 104 105
                        '<div class="smlogo mr-sm-1 %1$s" data-toggle="tooltip" title="%2$s"><div class="hidden">%3$s</div></div>',
                        $services_list[$service]['class'],
                        $services_list[$service]['title'],
                        $service
dmorley's avatar
dmorley committed
106 107
                    );
                }
108

dmorley's avatar
dmorley committed
109 110 111
                $pod_name = htmlentities($pod['name'] ?? '', ENT_QUOTES);
                $signup   = $pod['signup'] ? 'yes' : 'no';
                echo <<<EOF
112 113 114 115
{
  'type': 'Feature',
  'id': '1',
  'properties' : {
noplanman's avatar
noplanman committed
116
    '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}'
117 118 119
  },
  'geometry': {
    'type': 'Point',
noplanman's avatar
noplanman committed
120
    'coordinates': [{$pod['long']},{$pod['lat']}]
121
  }
122
}
dmorley's avatar
dmorley committed
123
EOF;
dmorley's avatar
dmorley committed
124 125 126 127
            }
            ?>
        ]
    };
128
    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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        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
145
</script>