Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Script] A ready to use tracker script for apps that support HTTP GET requests. #194

Open
adegans opened this issue Jun 16, 2022 · 2 comments

Comments

@adegans
Copy link

adegans commented Jun 16, 2022

A tracker receiver for other apps that support HTTP requests.

Place the files as track.php in the /client/ folder.
Uses internal auth functions and DB connections.

I've used it for a few days as a test and all seems to work on my Samsung Galaxy A52 w/ Android 11.
I then realized that I didn't want to track my phone constantly.
So I'll post it here for others to use.

See PHP comments for usage:

<?php
/* μlogger - 3rd party tracker via $_GET
 *
 * Copyright(C) 2022 Arnan de Gans (www.arnan.me)
 * Based on original code by Bartek Fabiszewski (www.fabiszewski.net)
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
/* USAGE:
 * user = uLogger username (Required, string[15], follows original auth routine)
 * pass = uLogger password (Required, string[15], follows original auth routine)
 * track = The track name you see and use in the uLogger web UI. (Required, string[255] (preferably alphanumeric and _-))
 * lat = Latitude (Required, float)
 * lon = Longtitude (Required, float) 
 * time = UNIX timestamp (Required, int)
 * alt = Altitude (Optional, float)
 * acc = GPS accuracy (Optional, int)
 * spd = Speed of device (Optional, float)
 * dir = Compass bearing (Optional, float)
 * batt = Will be formatted to go into $comment and indicates the device battery level if supported/added (Optional, float)
 * txt = Add a comment to each waypoint if desired (Optional, string[230] (Leave space for battery indicator))
 * 
 * NOTES:
 * If the track name provided through 'track' does not exist it will be created on the first request/sync. 
 * Track names must be unique for the user. Different users can use the same track name.
 * 'track' is required to populate $trackId.
 * 
 * EXAMPLES:
 * Essentials: https://domain.tld/client/track.php?user=[string]&pass=[string]&track=[string]&lat=[float]&lon=[float]&time=[integer]
 * PhoneTrack for Android: https://domain.tld/client/track.php?user=demo&pass=demo&track=track-name&lat=%LAT&lon=%LON&time=%TIMESTAMP&alt=%ALT&acc=%ACC&spd=%SPD&dir=%DIR&batt=%BATT
 */ 

/**
 * Exit with error status and message
 *
 * @param string $message Message
 */
function exitWithError($message) {
  $response = [];
  $response['error'] = true;
  $response['message'] = $message;
  header('Content-Type: application/json');
  echo json_encode($response);
  exit();
}

/**
 * Exit with success status
 *
 * @param array $params Optional params
 * @return void
 */
function exitWithSuccess($params = []) {
  $response = [];
  $response['error'] = false;
  header('Content-Type: application/json');
  echo json_encode(array_merge($response, $params));
  exit();
}

require_once(dirname(__DIR__) . "/helpers/auth.php");

// Set up login
$auth = new uAuth();
$login = uUtils::getString('user');
$pass = uUtils::getString('pass');

if ($auth->checkLogin($login, $pass)) {
	$trackName = uUtils::getString('track');
	if (empty($trackName)) {
		exitWithError("Missing required parameter - track");
	}

	// Find tracks for user
	require_once(ROOT_DIR . "/helpers/track.php");
	if ($auth->hasReadAccess($auth->user->id)) {
		$tracksArr = uTrack::getAll($auth->user->id);
	}
	
	if (count($tracksArr) > 0) {
		$result = array();

		foreach ($tracksArr as $track) {
			$result[$track->id] = $track->name;
		}

		// Fetch track ID if it exists for the track name
		$trackId = array_search($trackName, $result);
	}	

	// No track ID found, create one
	if ($trackId === false) {
		$trackId = uTrack::add($auth->user->id, $trackName);
	}

	// Record new waypoint
    $latitude = uUtils::getFloat('lat');
    $longitude = uUtils::getFloat('lon');
    $timestamp = uUtils::getInt('time');
    // Optional
    $altitude = uUtils::getFloat('alt');
    $accuracy = uUtils::getInt('acc');
    $speed = uUtils::getFloat('spd');
    $bearing = uUtils::getFloat('dir');
    $battery = uUtils::getFloat('batt');
    $comment = uUtils::getString('txt');
    
    if (!is_null($battery)) {
	    if (strlen($comment) > 0) {
	    	$comment = $comment.' ';
	    }
    	$comment = $comment.'Battery: '.$battery.'%';
    }

    if (!is_float($latitude) || !is_float($longitude) || !is_int($timestamp)) {
		exitWithError("Missing required parameter - lat lon time");
    }

    require_once(ROOT_DIR . "/helpers/position.php");
    $positionId = uPosition::add($auth->user->id, $trackId, $timestamp, $latitude, $longitude, $altitude, $speed, $bearing, $accuracy, null, $comment);

    if ($positionId === false) {
		exitWithError("Server error");
    }
    exitWithSuccess();
} else {
	// Fail if user is unknown
	$auth->sendUnauthorizedHeader();
	exitWithError("Unauthorized");
}
?>
@alex000090
Copy link

Thank you very much! It's really helpfull for me!

@TheLancashireman
Copy link

Thanks very much. This script is working well for me with gpsLogger.

@adegans adegans changed the title A tracker for other apps support HTTP GET requests. [Script] A ready to use tracker script for apps that support HTTP GET requests. Jul 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants