Skip to content

Instantly share code, notes, and snippets.

@jaggy
Last active December 30, 2015 16:18
Show Gist options
  • Save jaggy/7853464 to your computer and use it in GitHub Desktop.
Save jaggy/7853464 to your computer and use it in GitHub Desktop.
#!/usr/local/php5/bin/php
<?php
function testConnection()
{
$connected = @fsockopen("www.google.com", 80);
$response = false;
if ($connected) {
$response = true;
fclose($connected);
}
return $response;
}
function location($ip) {
$url = "http://freegeoip.net/json/$ip";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
$location = json_decode($data);
$lat = $location->latitude;
$lon = $location->longitude;
return ['latitude' => $lat, 'longitude' => $lon];
}
return false;
}
function fetchIp() {
$ch = curl_init("http://icanhazip.com/");
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
if ($result === FALSE) {
return "ERROR";
} else {
return trim($result);
}
}
$gitshots = '/Volumes/Data/.gitshots';
$imagesnap = exec('which imagesnap');
$project_name = explode('/', dirname(__FILE__))[4];
$now = strtotime(date('Y-m-d H:i:s'));
$root = "{$gitshots}/{$project_name}";
$destination = "{$root}/{$now}jpg";
if(!file_exists($root)) mkdir($root);
echo "Running gitshots! SMILE!\n";
echo "Saving the file into {$destination}.\n";
$command = "$imagesnap -q -w 3 {$destination} &> /dev/null &";
system($command);
$hasConnection = testConnection();
if ($hasConnection) {
$ip = fetchIp();
if ($ip) {
$coordinates = location($ip);
if ($coordinates) {
$mysqli = new mysqli('127.0.0.1', 'root', 'hangin', 'gitshots');
$query = "INSERT INTO `images` values (null, '{$now}', '{$coordinates['latitude']}', '{$coordinates['longitude']}');";
$mysqli->query($query);
$mysqli->close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment