Skip to content

Instantly share code, notes, and snippets.

@restah
Last active May 15, 2022
Embed
What would you like to do?
<?php
/**
* Config, add µLogger Credentials and the relative Path to µLogger
*/
define('USER', 'UloggerUsername');
define('PASSWORD', 'UloggerPassword');
define('ULOGGER_PATH', 'ulogger');
/**
* https://arjunphp.com/php-multidimensional-array-searching/
*/
function recursiveArraySearch($haystack, $needle, $index = null){
$aIt = new RecursiveArrayIterator($haystack);
$it = new RecursiveIteratorIterator($aIt);
while($it->valid()){
if(((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)){
return $aIt->key();
}
$it->next();
}
return false;
}
if(file_exists(ULOGGER_PATH . '/helpers/auth.php')){
if(include_once(ULOGGER_PATH . '/helpers/auth.php')){
$auth = new uAuth();
$loggedIn = $auth->checkLogin(USER, PASSWORD);
if($loggedIn){
/**
* display all Track IDs
*/
if(!isset($_GET['id'])){
if(!class_exists('uTrack')){
include_once(ULOGGER_PATH . '/helpers/track.php');
}
$tracks = (array) uTrack::getAll();
$key = recursiveArraySearch($tracks, '38', 'id');
if(!empty($tracks)){
echo '<ul>';
foreach($tracks as $key => $track){
$track = (array) $track;
echo '<li><span class="trackid">ID: <code>' . $track['id'] . '</code></span> - <span class="trackname">Name: <code>' . $track['name'] . '</code></span></li>';
}
echo '</ul>';
}
} else {
/**
* Retrieve Data
*/
if(isset($_GET['id'])) {
if(isset($_GET['id'])){
$trackId = $_GET['id'];
} else {
$error = TRUE;
}
if(isset($_GET['lat'])){
$lat = floatval($_GET['lat']);
} else {
$error = TRUE;
}
if(isset($_GET['lon'])){
$lon = floatval($_GET['lon']);
} else {
$error = TRUE;
}
if(isset($_GET['timestamp'])){
$timestamp = $_GET['timestamp'];
} else {
$error = TRUE;
}
if(isset($_GET['altitude'])){
$altitude = $_GET['altitude'];
}
if(isset($_GET['speed'])){
$speed = $_GET['speed'];
}
if(isset($_GET['bearing'])){
$bearing = $_GET['bearing'];
}
if(isset($_GET['accuracy'])){
$accuracy = $_GET['accuracy'];
}
} elseif(isset($_POST['id'])){
if(isset($_POST['id'])){
$trackId = $_POST['id'];
} else {
$error = TRUE;
}
if(isset($_POST['lat'])){
$lat = floatval($_POST['lat']);
} else {
$error = TRUE;
}
if(isset($_POST['lon'])){
$lon = floatval($_POST['lon']);
} else {
$error = TRUE;
}
if(isset($_POST['timestamp'])){
$timestamp = $_POST['timestamp'];
} else {
$error = TRUE;
}
if(isset($_POST['altitude'])){
$altitude = $_POST['altitude'];
}
if(isset($_POST['speed'])){
$speed = $_POST['speed'];
}
if(isset($_POST['bearing'])){
$bearing = $_POST['bearing'];
}
if(isset($_POST['accuracy'])){
$accuracy = $_POST['accuracy'];
}
}
if(!isset($error)){
if(!isset($speed)){
$speed = 0;
}
if(!isset($bearing)){
$bearing = 0;
}
if(!isset($accuracy)){
$accuracy = 0;
}
if(!isset($altitude)){
$altitude = 0;
}
$lat = filter_var($lat, FILTER_VALIDATE_FLOAT);
$lon = filter_var($lon, FILTER_VALIDATE_FLOAT);
$altitude = filter_var($altitude, FILTER_VALIDATE_FLOAT);
$speed = filter_var($speed, FILTER_VALIDATE_FLOAT);
$bearing = filter_var($bearing, FILTER_VALIDATE_FLOAT);
$trackId = filter_var($trackId, FILTER_VALIDATE_INT);
$provider = trim($provider);
$imageMeta = '';
$image = '';
$comment = '';
if (empty($lat) || empty($lon) || empty($timestamp) || empty($trackId)) {
print_r("Missing required parameter");
} else {
if(!class_exists('uPosition')){
require_once(ULOGGER_PATH . "/helpers/position.php");
}
$tracks = (array) uTrack::getAll();
$key = recursiveArraySearch($tracks, $trackId, 'id');
if(empty($key)){
$trackName = 'Auto_ID_' . $trackId . '_' . date('Y_m_d_H_i_s');
if(!class_exists('uTrack')){
require_once(ULOGGER_PATH . "/helpers/track.php");
}
$NewTrackId = uTrack::add($auth->user->id, $trackName);
if ($NewTrackId === false) {
exitWithError("Server error");
} else {
$trackId = $NewTrackId;
}
}
$positionId = uPosition::add($auth->user->id, $trackId, $timestamp, $lat, $lon, $altitude, $speed, $bearing, $accuracy, $provider, $comment, $image);
if ($positionId === false) {
print_r("Server error");
} else {
$response = [];
$params = [];
$response['error'] = false;
header('Content-Type: application/json');
echo json_encode(array_merge($response, $params));
}
}
} else {
print_r('Something went wrong.<br>Please check the Client Parameters.');
}
}
} else {
print_r('Hm ... your credentials are wrong. Please check again.');
}
} else {
print_r('Seems like a permission Problem.');
}
} else {
print_r('Please check the Config Section and fix the ULOGGER_PATH.');
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment