gpstrack.php
                        
                             · 6.2 KiB · PHP
                        
                    
                    
                      
                        Raw
                      
                      
                        
                          
                        
                    
                    
                
                
            <?php
$passwdfile = "/path/to/file";
$logpath = "/path/to/file"; // should be a csv file with date,lat,long,hor-acc
// For json
foreach (apache_request_headers() as $header => $value) { if ($header == "Accept") { $accept = $value; } }
// Variable checks
$allowed = isset($_GET['allowed']);
if ($allowed && !$_GET['allowed'] == file_get_contents($passwdfile)) {
  die("Sorry, this needs a password now.<form><input type=text name=allowed><input type=submit></form>");
}
$nocir = !isset($_GET['nocircles']);
if (isset($_GET['count'])) {
  $count = intval($_GET['count']);
} else {
  $count = 10;
}
// Getting data
if (isset($_GET['date']) && intval($_GET['date']) != 0 && strlen($_GET['date']) == 8) {
  $date = intval($_GET['date']);
  $datarr = explode("\n", shell_exec("grep $date $logpath --color=never"));
  array_pop($datarr);
} else {
  $datarr = explode("\n", shell_exec("tail -n$count $logpath"));
}
// Correcting for allowed data
$last = str_getcsv(array_pop($datarr));
if (!$allowed) $last = array($last[0], substr($last[1],0,6), substr($last[2],0,6));
// Creating static image url
$imageurl = "https://maps.googleapis.com/maps/api/staticmap?scale=2&format=jpg&zoom=";
if ($allowed) $imageurl .= "18";
else $imageurl .= "15";
$imageurl .= "¢er=" . $last[1] . "," . $last[2];
if ($allowed) $imageurl .= "&markers=" . $last[1] . "," . $last[2];
// Json returning
if (isset($_GET['json'])) {
    header("Content-Type: application/json");
    $arr = array();
    foreach ($datarr as $item) {
        $tmp = str_getcsv($item);
        if (!$allowed) $tmp = array($tmp[0], substr($tmp[1],0,6), substr($tmp[2],0,6), "1111");
        $arr[] = $tmp;
    }
    echo json_encode(array("markers" => $arr, "imageurl" => $imageurl));
    die();
}
// Mobile support {
require_once 'Mobile_Detect.php'; // http://mobiledetect.net/ remove this block if you don't need this functionality
$detect = new Mobile_Detect;
if (isset($_GET['mobile']) || $detect->isMobile()) { ?>
  <head>
    <meta http-equiv="refresh" content="60">
    <script>
      document.addEventListener('DOMContentLoaded',function(){
        var w = Math.floor(Math.max(document.documentElement.clientWidth, window.innerWidth || 0) / 2)
        var h = Math.floor(Math.max(document.documentElement.clientHeight, window.innerHeight || 0) / 2)
        document.getElementById('i').src = "<?php echo $imageurl; ?>&size=" + w + "x" + h;
      });
    </script>
  </head>
  <body>
    <img src="nothing" id="i" />
  </body>
<?php die();}
// }
// Generating lists of markers/circles
if ($allowed) {
  $markers = "[";
  if ($nocir) {
    $circles = "[";
    $ciroptions = "{strokeColor:'#FF0000',strokeOpacity:0.8,strokeWeight:2,fillColor:'#AA0000',fillOpacity:0.2,map:map";
  }
  foreach ($datarr as $item) {
    $item = str_getcsv($item);
    $markers .= 'new google.maps.Marker({map: map, position: new google.maps.LatLng(' . $item[1] . ', ' . $item[2] . '),title: "' . $item[0] . ' to ' . $item[3] . 'm",icon: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"}),';
    if ($nocir) {
      $circles .= 'new google.maps.Circle(' . $ciroptions . ',radius: ' . $item[3] . '}),';
    }
  }
  $markers .= ']';
  if ($nocir) {
    $circles .= ']';
  }
}
// Start html
?>
<!DOCTYPE html>
<html>
  <head>
<?php if ($count < 50) { ?>
    <meta http-equiv="refresh" content="60">
<?php } ?>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCE7QCD1wjjjspuEa7ABtKGpJxttNOgtIU&sensor=false">
    </script>
    <script type="text/javascript">
      var geocoder;
      var map;
      var markers;
      var circles;
      var topcircle;
<?php if (!$allowed) { ?>
      var data = <?php echo json_encode($last); ?>;
      var coords = [new google.maps.LatLng(data[1], data[2])];
<?php } ?>
      function initialize() {
        geocoder = new google.maps.Geocoder();
        var mapOptions = {
          center: <?php echo 'new google.maps.LatLng(' . $last[1] . ', ' . $last[2] . ')'; ?>,
          zoom: <?php if (isset($_GET['zoom'])) { echo intval($_GET['zoom']); } else { if ($allowed) { echo "18"; } else { echo "15"; } }; echo "\n"; ?>
        };
        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
<?php   if ($allowed) { ?>
        markers = <?php echo $markers; ?>;
        markers[markers.length-1].setOptions({animation:google.maps.Animation.DROP,icon:"http://maps.google.com/mapfiles/ms/icons/blue-dot.png",zIndex:100});
        var coords = [];
        for (i=0;i<markers.length;i++) {
            coords.push(markers[i].getPosition());
        }
        var recentPath = new google.maps.Polyline({
          path: coords,
          geodesic: true,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 2
        });
        recentPath.setMap(map);
<?php   if ($nocir) { ?>
        circles = <?php echo $circles; ?>;
        for (i=0;i<circles.length;i++) {
            circles[i].bindTo('center', markers[i], 'position');
            markers[i]._myCircle = circles[i];
            circles[i].title = markers[i].title;
        }
        topcircle = circles[circles.length-1];
        topcircle.setOptions({fillColor: '#0000AA', strokeColor: '#0000FF', url: "<?php echo $_SERVER['REQUEST_URI']."&nocircles" ?>"});
        google.maps.event.addListener(topcircle, 'click', function() {
                window.location = topcircle.url;
        });
<?php   } ?>
<?php   } else { ?>
        var infowindow = new google.maps.InfoWindow({
            content: "blha303's last recorded location (accurate to 1.1km approx)"
        });
        var circle = new google.maps.Circle({
            map: map,
            center: coords[coords.length-1],
            radius: 1100,
            fillColor: '#AA0000',
            fillOpacity: 0.1
        });
        google.maps.event.addListener(circle, 'click', function() {
            infowindow.open(map,circle);
        });
<?php   } ?>
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"/>
  </body>
</html>
                | 1 | <?php | 
| 2 | |
| 3 | $passwdfile = "/path/to/file"; | 
| 4 | $logpath = "/path/to/file"; // should be a csv file with date,lat,long,hor-acc | 
| 5 | |
| 6 | // For json | 
| 7 | foreach (apache_request_headers() as $header => $value) { if ($header == "Accept") { $accept = $value; } } | 
| 8 | |
| 9 | // Variable checks | 
| 10 | $allowed = isset($_GET['allowed']); | 
| 11 | if ($allowed && !$_GET['allowed'] == file_get_contents($passwdfile)) { | 
| 12 | die("Sorry, this needs a password now.<form><input type=text name=allowed><input type=submit></form>"); | 
| 13 | } | 
| 14 | $nocir = !isset($_GET['nocircles']); | 
| 15 | if (isset($_GET['count'])) { | 
| 16 | $count = intval($_GET['count']); | 
| 17 | } else { | 
| 18 | $count = 10; | 
| 19 | } | 
| 20 | |
| 21 | // Getting data | 
| 22 | if (isset($_GET['date']) && intval($_GET['date']) != 0 && strlen($_GET['date']) == 8) { | 
| 23 | $date = intval($_GET['date']); | 
| 24 | $datarr = explode("\n", shell_exec("grep $date $logpath --color=never")); | 
| 25 | array_pop($datarr); | 
| 26 | } else { | 
| 27 | $datarr = explode("\n", shell_exec("tail -n$count $logpath")); | 
| 28 | } | 
| 29 | |
| 30 | // Correcting for allowed data | 
| 31 | $last = str_getcsv(array_pop($datarr)); | 
| 32 | if (!$allowed) $last = array($last[0], substr($last[1],0,6), substr($last[2],0,6)); | 
| 33 | |
| 34 | // Creating static image url | 
| 35 | $imageurl = "https://maps.googleapis.com/maps/api/staticmap?scale=2&format=jpg&zoom="; | 
| 36 | if ($allowed) $imageurl .= "18"; | 
| 37 | else $imageurl .= "15"; | 
| 38 | $imageurl .= "¢er=" . $last[1] . "," . $last[2]; | 
| 39 | if ($allowed) $imageurl .= "&markers=" . $last[1] . "," . $last[2]; | 
| 40 | |
| 41 | // Json returning | 
| 42 | if (isset($_GET['json'])) { | 
| 43 | header("Content-Type: application/json"); | 
| 44 | $arr = array(); | 
| 45 | foreach ($datarr as $item) { | 
| 46 | $tmp = str_getcsv($item); | 
| 47 | if (!$allowed) $tmp = array($tmp[0], substr($tmp[1],0,6), substr($tmp[2],0,6), "1111"); | 
| 48 | $arr[] = $tmp; | 
| 49 | } | 
| 50 | echo json_encode(array("markers" => $arr, "imageurl" => $imageurl)); | 
| 51 | die(); | 
| 52 | } | 
| 53 | |
| 54 | // Mobile support { | 
| 55 | require_once 'Mobile_Detect.php'; // http://mobiledetect.net/ remove this block if you don't need this functionality | 
| 56 | $detect = new Mobile_Detect; | 
| 57 | if (isset($_GET['mobile']) || $detect->isMobile()) { ?> | 
| 58 | <head> | 
| 59 | <meta http-equiv="refresh" content="60"> | 
| 60 | <script> | 
| 61 | document.addEventListener('DOMContentLoaded',function(){ | 
| 62 | var w = Math.floor(Math.max(document.documentElement.clientWidth, window.innerWidth || 0) / 2) | 
| 63 | var h = Math.floor(Math.max(document.documentElement.clientHeight, window.innerHeight || 0) / 2) | 
| 64 | document.getElementById('i').src = "<?php echo $imageurl; ?>&size=" + w + "x" + h; | 
| 65 | }); | 
| 66 | </script> | 
| 67 | </head> | 
| 68 | <body> | 
| 69 | <img src="nothing" id="i" /> | 
| 70 | </body> | 
| 71 | <?php die();} | 
| 72 | // } | 
| 73 | |
| 74 | // Generating lists of markers/circles | 
| 75 | if ($allowed) { | 
| 76 | $markers = "["; | 
| 77 | if ($nocir) { | 
| 78 | $circles = "["; | 
| 79 | $ciroptions = "{strokeColor:'#FF0000',strokeOpacity:0.8,strokeWeight:2,fillColor:'#AA0000',fillOpacity:0.2,map:map"; | 
| 80 | } | 
| 81 | foreach ($datarr as $item) { | 
| 82 | $item = str_getcsv($item); | 
| 83 | $markers .= 'new google.maps.Marker({map: map, position: new google.maps.LatLng(' . $item[1] . ', ' . $item[2] . '),title: "' . $item[0] . ' to ' . $item[3] . 'm",icon: "http://maps.google.com/mapfiles/ms/icons/red-dot.png"}),'; | 
| 84 | if ($nocir) { | 
| 85 | $circles .= 'new google.maps.Circle(' . $ciroptions . ',radius: ' . $item[3] . '}),'; | 
| 86 | } | 
| 87 | } | 
| 88 | $markers .= ']'; | 
| 89 | if ($nocir) { | 
| 90 | $circles .= ']'; | 
| 91 | } | 
| 92 | } | 
| 93 | |
| 94 | // Start html | 
| 95 | ?> | 
| 96 | <!DOCTYPE html> | 
| 97 | <html> | 
| 98 | <head> | 
| 99 | <?php if ($count < 50) { ?> | 
| 100 | <meta http-equiv="refresh" content="60"> | 
| 101 | <?php } ?> | 
| 102 | <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> | 
| 103 | <style type="text/css"> | 
| 104 | html { height: 100% } | 
| 105 | body { height: 100%; margin: 0; padding: 0 } | 
| 106 | #map-canvas { height: 100% } | 
| 107 | </style> | 
| 108 | <script type="text/javascript" | 
| 109 | src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCE7QCD1wjjjspuEa7ABtKGpJxttNOgtIU&sensor=false"> | 
| 110 | </script> | 
| 111 | <script type="text/javascript"> | 
| 112 | var geocoder; | 
| 113 | var map; | 
| 114 | var markers; | 
| 115 | var circles; | 
| 116 | var topcircle; | 
| 117 | <?php if (!$allowed) { ?> | 
| 118 | var data = <?php echo json_encode($last); ?>; | 
| 119 | var coords = [new google.maps.LatLng(data[1], data[2])]; | 
| 120 | <?php } ?> | 
| 121 | function initialize() { | 
| 122 | geocoder = new google.maps.Geocoder(); | 
| 123 | var mapOptions = { | 
| 124 | center: <?php echo 'new google.maps.LatLng(' . $last[1] . ', ' . $last[2] . ')'; ?>, | 
| 125 | zoom: <?php if (isset($_GET['zoom'])) { echo intval($_GET['zoom']); } else { if ($allowed) { echo "18"; } else { echo "15"; } }; echo "\n"; ?> | 
| 126 | }; | 
| 127 | map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); | 
| 128 | <?php if ($allowed) { ?> | 
| 129 | markers = <?php echo $markers; ?>; | 
| 130 | markers[markers.length-1].setOptions({animation:google.maps.Animation.DROP,icon:"http://maps.google.com/mapfiles/ms/icons/blue-dot.png",zIndex:100}); | 
| 131 | var coords = []; | 
| 132 | for (i=0;i<markers.length;i++) { | 
| 133 | coords.push(markers[i].getPosition()); | 
| 134 | } | 
| 135 | var recentPath = new google.maps.Polyline({ | 
| 136 | path: coords, | 
| 137 | geodesic: true, | 
| 138 | strokeColor: '#FF0000', | 
| 139 | strokeOpacity: 1.0, | 
| 140 | strokeWeight: 2 | 
| 141 | }); | 
| 142 | recentPath.setMap(map); | 
| 143 | <?php if ($nocir) { ?> | 
| 144 | circles = <?php echo $circles; ?>; | 
| 145 | for (i=0;i<circles.length;i++) { | 
| 146 | circles[i].bindTo('center', markers[i], 'position'); | 
| 147 | markers[i]._myCircle = circles[i]; | 
| 148 | circles[i].title = markers[i].title; | 
| 149 | } | 
| 150 | topcircle = circles[circles.length-1]; | 
| 151 | topcircle.setOptions({fillColor: '#0000AA', strokeColor: '#0000FF', url: "<?php echo $_SERVER['REQUEST_URI']."&nocircles" ?>"}); | 
| 152 | google.maps.event.addListener(topcircle, 'click', function() { | 
| 153 | window.location = topcircle.url; | 
| 154 | }); | 
| 155 | <?php } ?> | 
| 156 | <?php } else { ?> | 
| 157 | var infowindow = new google.maps.InfoWindow({ | 
| 158 | content: "blha303's last recorded location (accurate to 1.1km approx)" | 
| 159 | }); | 
| 160 | var circle = new google.maps.Circle({ | 
| 161 | map: map, | 
| 162 | center: coords[coords.length-1], | 
| 163 | radius: 1100, | 
| 164 | fillColor: '#AA0000', | 
| 165 | fillOpacity: 0.1 | 
| 166 | }); | 
| 167 | google.maps.event.addListener(circle, 'click', function() { | 
| 168 | infowindow.open(map,circle); | 
| 169 | }); | 
| 170 | <?php } ?> | 
| 171 | } | 
| 172 | google.maps.event.addDomListener(window, 'load', initialize); | 
| 173 | </script> | 
| 174 | </head> | 
| 175 | <body> | 
| 176 | <div id="map-canvas"/> | 
| 177 | </body> | 
| 178 | </html> |