add other files

This commit is contained in:
Simon Rieger 2025-01-19 18:47:49 +01:00
parent 8d3bf8b198
commit 3a043d2b15
6 changed files with 114 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
mysql-data/

2
nginx/Dockerfile Executable file
View file

@ -0,0 +1,2 @@
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf

38
nginx/default.conf Executable file
View file

@ -0,0 +1,38 @@
server {
listen 80 default_server;
root /var/www/html;
index index.html index.php;
charset utf-8;
location / {
try_files $uri $uri/ /track.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/error.log error;
sendfile off;
client_max_body_size 100m;
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /.ht {
deny all;
}
}

3
php_code/Dockerfile Executable file
View file

@ -0,0 +1,3 @@
FROM php:7.0-fpm
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN docker-php-ext-enable mysqli

35
php_code/loc.php Executable file
View file

@ -0,0 +1,35 @@
<?php
# Obtain the JSON payload from an OwnTracks app POSTed via HTTP
# and insert into database table.
header("Content-type: application/json");
$payload = file_get_contents("php://input");
$data = @json_decode($payload, true);
if ($data['_type'] == 'location') {
# CREATE TABLE locations (dt TIMESTAMP, tid CHAR(2), lat DECIMAL(9,6), lon DECIMAL(9,6));
$mysqli = new mysqli("172.28.0.25", "root", "owntracks", "owntracks");
$tst = $data['tst'];
$lat = $data['lat'];
$lon = $data['lon'];
$tid = $data['tid'];
# Convert timestamp to a format suitable for mysql
$dt = date('Y-m-d H:i:s', $tst);
$sql = "INSERT INTO locations (dt, tid, lat, lon) VALUES (?, ?, ?, ?)";
$stmt = $mysqli->prepare($sql);
# bind parameters (s = string, i = integer, d = double, b = blob)
$stmt->bind_param('ssdd', $dt, $tid, $lat, $lon);
$stmt->execute();
$stmt->close();
}
$response = array();
# optionally add objects to return to the app (e.g.
# friends or cards)
print json_encode($response);
?>

35
php_code/track.php Normal file
View file

@ -0,0 +1,35 @@
<?php
$data = file_get_contents('php://input');
$json = json_decode($data);
header("Content-type: application/json");
if ($json->_type !== 'location') {
return;
}
$db = new mysqli('172.28.0.25:3306', 'root', 'owntracks', 'owntracks');
if ($db->connect_error) {
die('Connection failed: ' . $db->connect_error);
}
$stmt = $db->prepare('INSERT INTO recordings (user, device, acc, alt, batt, bs, conn, created_at, lat, lon, t, tid, tst, vac, vel) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);');
if ($stmt === false) {
die('failed to prepare query: ' . $db->error);
}
if ($stmt->bind_param('ssiiiisiddssiii', $user, $device, $json->acc, $json->alt, $json->batt, $json->bs, $json->conn, $json->created_at, $json->lat, $json->lon, $json->t, $json->tid, $json->tst, $json->vac, $json->vel) === false) {
die('failed to bind params: ' . $stmt->error);
}
$user = $_SERVER['HTTP_X_LIMIT_U'];
$device = $_SERVER['HTTP_X_LIMIT_D'];
if ($stmt->execute() === false) {
die('failed to insert: ' . $stmt->error);
}
$stmt->close();
$db->close();
print json_encode(array());