Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions web-gui/file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

require __DIR__ . '/functions.php';

if (empty($_GET['file'])) {
die('Missing file argument');
}

$filename = basename(clean($_GET['file'])) . '.img';

if (file_exists(__DIR__ . '/img/' . $filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
readfile($filename);
exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="10">
</head>
<body>
<h1>Downloading <?php echo $filename; ?> ...</h1>
<p>Please wait while the map is being generated. This may take up to 10 minutes.</p>.
</body>
</html>
35 changes: 35 additions & 0 deletions web-gui/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

function clean($string) {
$string = str_replace(' ', '_', $string); // Replaces all spaces with hyphens.

return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

function is_valid_squadrats_kml($file): bool {
libxml_use_internal_errors(true);

$dom = new \DOMDocument();
if (!$dom->load($file)) {
return FALSE;
}

// Search for Kml->Document->Placemark elements.
$placemarks = $dom->getElementsByTagName('kml')->item(0)
?->getElementsByTagName('Document')->item(0)
?->getElementsByTagName('Placemark');

if (!$placemarks->length > 0) {
return FALSE;
}

// Search for Placemark->name element with the value of "squadrats".
foreach ($placemarks as $placemark) {
$name = $placemark->getElementsByTagName('name')->item(0)?->textContent;

if ($name === 'squadrats') {
return TRUE;
}
}
return FALSE;
}
25 changes: 0 additions & 25 deletions web-gui/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,6 @@

<div id="map"></div>

<p class="card-header card-header-files">Img files:
<UL class="card-body card-body-files">

<?php

# https://stackoverflow.com/questions/21416793/deleting-3-days-old-file-from-folder-in-php
$imgFiles = glob("img/*");
$shFiles = glob("../../jobs/missing_squadrats/*.kml");
$threshold = strtotime('-1 day');
foreach ($imgFiles as $file) {
if (is_file($file)) {
if ($threshold < filemtime($file)) {
echo "<LI><A href=\"https://oranta.kapsi.fi/missing_squadrats/" . $file . "\">" . str_replace("img/","",$file) . "</A><BR>\r\n";
}
}
}
foreach ($shFiles as $file) {
if (is_file($file)) {
echo "<LI>" . str_replace("../../jobs/missing_squadrats/","",$file) . "<BR>\r\n";
}
}
?>

</UL>

<p class="card-header card-header-instructions">Short instructions:
<ul class="card-body card-body-instructions">
<li>Download and save a kml-file of the visited squadrats and squadratinhos from <a href="https://squadrats.com/">Squadrats</a> (Map - Download KML)
Expand Down
70 changes: 10 additions & 60 deletions web-gui/upload.php
Original file line number Diff line number Diff line change
@@ -1,42 +1,8 @@
<!DOCTYPE html>

<?php

function clean($string) {
$string = str_replace(' ', '_', $string); // Replaces all spaces with hyphens.

return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

function is_valid_squadrats_kml($file): bool {
libxml_use_internal_errors(true);

$dom = new \DOMDocument();
if (!$dom->load($file)) {
return FALSE;
}

// Search for Kml->Document->Placemark elements.
$placemarks = $dom->getElementsByTagName('kml')->item(0)
?->getElementsByTagName('Document')->item(0)
?->getElementsByTagName('Placemark');

if (!$placemarks->length > 0) {
return FALSE;
}

// Search for Placemark->name element with the value of "squadrats".
foreach ($placemarks as $placemark) {
$name = $placemark->getElementsByTagName('name')->item(0)?->textContent;

if ($name === 'squadrats') {
return TRUE;
}
}
return FALSE;
}
require __DIR__ . '/functions.php';

$tmpName = $_FILES['fileToUpload']['tmp_name'];
$tmpName = $_FILES['fileToUpload']['tmp_name'] ?? NULL;

if (empty($tmpName) || !is_valid_squadrats_kml($tmpName)) {
die("Invalid .kml file.");
Expand Down Expand Up @@ -85,26 +51,8 @@ function is_valid_squadrats_kml($file): bool {
setcookie("MissingSquadrats", json_encode($cookieValues), time() + (86400 * 30)); // 86400 = 1 day
}

?>

<html>
<head>
<meta charset="utf-8">
</head>
<body>

<h1>Upload a kml file</h1>

<p>Everything ok. Please go back to the main page</p><br>

<?php

#echo "OSM file name: " . $target_file . "<BR>\r\n";
if (move_uploaded_file($tmpName, $target_dir . $fileName . '.kml')) {
echo "The file ". $fileName . " has been uploaded.<BR>\r\n";
} else {
echo "Sorry, there was an error uploading your file.<BR>\r\n";
exit;
if (!move_uploaded_file($tmpName, $target_dir . $fileName . '.kml')) {
die("Sorry, there was an error uploading your file.");
}

$job = implode(',', [
Expand All @@ -118,8 +66,10 @@ function is_valid_squadrats_kml($file): bool {
'lineColor' => $lineColor,
'zoomLevel' => $zoomLevel,
]);
file_put_contents($target_dir . $fileName . '.csv', $job);
?>

</body>
</html>
if (!file_put_contents($target_dir . $fileName . '.csv', $job)) {
die("Sorry, there was an error uploading your file.");
}

header('Location: file.php?file=' . $fileName);
exit;