showmap.php 3.57 KB
Newer Older
dmorley's avatar
dmorley committed
1
<?php
dmorley's avatar
dmorley committed
2
//focus map to the users side of the globe
3 4
// Cloudflare country code pull.
$country_code = $_SERVER['HTTP_CF_IPCOUNTRY'] ?? '';
5

dmorley's avatar
dmorley committed
6 7
$csv = array_map('str_getcsv', file('db/country_latlon.csv'));
foreach ($csv as $cords) {
8
  if ($cords[0] === $country_code) {
9
    $lat  = $cords[1];
dmorley's avatar
dmorley committed
10
    $long = $cords[2];
dmorley's avatar
dmorley committed
11 12 13
  } else {
    $lat = 31;
    $long = -99;
dmorley's avatar
dmorley committed
14
  }
dmorley's avatar
dmorley committed
15
}
dmorley's avatar
dmorley committed
16
?>
17
<link rel="stylesheet" href="bower_components/leaflet/dist/leaflet.css"/>
18 19
<script src="bower_components/leaflet/dist/leaflet.js"></script>
<script type="text/javascript" src="bower_components/leaflet.markercluster/dist/leaflet.markercluster.js"></script>
dmorley's avatar
dmorley committed
20
<div id="map"></div>
21
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
dmorley's avatar
dmorley committed
22
<script type="text/javascript">
23 24 25 26 27
  var geoJsonData = {
    'type': 'FeatureCollection',
    'features': [
      <?php
      require_once __DIR__ . '/config.php';
28

29
      $dbh = pg_connect("dbname=$pgdb user=$pguser password=$pgpass");
30 31
      $dbh || die('Error in connection: ' . pg_last_error());

dmorley's avatar
dmorley committed
32
      $sql = "SELECT * FROM pods WHERE NOT hidden";
33
      $result = pg_query($dbh, $sql);
34
      $result || die('Error in SQL query: ' . pg_last_error());
35

36
      $numrows = pg_num_rows($result);
37
      $i = 0;
38
      while ($row = pg_fetch_array($result)) {
39 40 41
        // If this isn't the first entry, put a comma to separate the entries.
        $i++ > 0 && print ',';

42
        $feat = '';
43 44 45 46
        $row['service_facebook'] === 't' && $feat .= '<div class="smlogo smlogo-facebook"></div>';
        $row['service_twitter'] === 't' && $feat .= '<div class="smlogo smlogo-twitter"></div>';
        $row['service_tumblr'] === 't' && $feat .= '<div class="smlogo smlogo-tumblr"></div>';
        $row['service_wordpress'] === 't' && $feat .= '<div class="smlogo smlogo-wordpress"></div>';
dmorley's avatar
dmorley committed
47
        $row['service_xmpp'] === 't' && $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>';
48

49
        $pod_name = htmlentities($row['name'], ENT_QUOTES);
dmorley's avatar
dmorley committed
50 51
        $scheme   = $row['secure'] ? 'https://' : 'http://';
        $signup   = $row['signup'] ? 'yes' : 'no';
52
        echo <<<EOF
53 54 55 56
{
  'type': 'Feature',
  'id': '1',
  'properties' : {
dmorley's avatar
dmorley committed
57
    'html': '{$pod_name}<br><a href="/go.php?url={$scheme}{$row['domain']}">Visit</a><br> Open Signup: {$signup}<br> Users: {$row['active_users_halfyear']}<br> Uptime: {$row['uptime_alltime']}%<br> Services:{$feat}'
58 59 60
  },
  'geometry': {
    'type': 'Point',
61
    'coordinates': [{$row['long']},{$row['lat']}]
62
  }
63
}
dmorley's avatar
dmorley committed
64
EOF;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
      }
      ?>
    ]
  };
  var tiles = L.tileLayer('https://{s}.tiles.mapbox.com/v4/diasporg.l615e519/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZGlhc3BvcmciLCJhIjoibTVBaldtayJ9.HdGPBIFeZyNKKQqCmU11nA', {
    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: 5, 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
86
</script>