forked from opencaching/opencaching-pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newcacheAjaxWaypointUploader.php
100 lines (78 loc) · 2.77 KB
/
newcacheAjaxWaypointUploader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
use src\Models\OcConfig\OcConfig;
/**
* This script allow to upload GPX file with new cache description
*/
require_once __DIR__ . '/lib/common.inc.php';
$destination_path = OcConfig::getPicUploadFolder(true) . DIRECTORY_SEPARATOR;
$result = 'No Data';
$valid_formats = ['gpx'];
$name = $_FILES['myfile']['name'];
$size = $_FILES['myfile']['size'];
if (strlen($name)) {
[$txt, $ext] = explode('.', $name);
$ext = strtolower($ext);
if (in_array($ext, $valid_formats)) {
if ($size < (1024 * 1024 * 2)) { // Image size max 2 MB
$actual_image_name = 'tempgpx.' . $ext;
$result = 0;
$target_path = $destination_path . $actual_image_name;
$wpts = simplexml_load_file($_FILES['myfile']['tmp_name']);
@unlink($_FILES['myfile']['tmp_name']);
$result = json_encode(loadWaypointFromGpx($wpts));
}
}
}
function loadWaypointFromGpx($wpts)
{
$arr = (array) $wpts;
if (count($wpts->wpt) == 1) {
$tmp = $arr['wpt'];
unset($arr);
$arr['wpt'][0] = $tmp;
}
foreach ($arr['wpt'] as $key => $waypoint) {
$coordsLon = (float) $waypoint->attributes()->lon;
$coordsLat = (float) $waypoint->attributes()->lat;
if ($coordsLon < 0) {
$coords_lonEW = 'W';
$coordsLon = -$coordsLon;
} else {
$coords_lonEW = 'E';
}
if ($coordsLat < 0) {
$coords_latNS = 'S';
$coordsLat = -$coordsLat;
} else {
$coords_latNS = 'N';
}
$coords_lat_h = floor($coordsLat);
$coords_lon_h = floor($coordsLon);
$coords_lat_min = sprintf('%02.3f', round(($coordsLat - $coords_lat_h) * 60, 3));
$coords_lon_min = sprintf('%02.3f', round(($coordsLon - $coords_lon_h) * 60, 3));
$result[$key] = [
'name' => (string) $waypoint->name,
'coords_latNS' => $coords_latNS,
'coords_lonEW' => $coords_lonEW,
'coords_lat_h' => $coords_lat_h,
'coords_lon_h' => $coords_lon_h,
'coords_lat_min' => $coords_lat_min,
'coords_lon_min' => $coords_lon_min,
'time' => (string) $waypoint->time,
'altitude' => (float) $waypoint->ele,
'desc' => '',
'cmt' => '',
];
//insert waypoint description in result array
if (isset($waypoint->cmt) && $wpts->wpt->cmt != '') {
$result[$key]['desc'] .= (string) $wpts->wpt->desc;
}
if (isset($waypoint->cmt) && $wpts->wpt->cmt != '') {
$result[$key]['cmt'] .= (string) $wpts->wpt->cmt;
}
}
//var_dump($result);
return $result;
}
?>
<script>window.top.window.stopUpload(<?php echo "'" . $result . "'"; ?>);</script>