forked from opencaching/opencaching-pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_cache_multi.php
439 lines (375 loc) · 15.8 KB
/
log_cache_multi.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
use src\Models\ApplicationContainer;
use src\Utils\Database\XDb;
if (isset($_POST['submitDownloadGpx'])) {
$fd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
$fd .= "<gpx xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \r\n";
$fd .= " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" version=\"1.0\" \r\n";
$fd .= " creator=\"tATO ;)\" \r\n";
$fd .= " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 \r\n";
$fd .= " http://www.topografix.com/GPX/1/0/gpx.xsd \r\n";
$fd .= " http://genpol.com/temp/geocache/ http://genpol.com/temp/geocache/geocache.xsd\" \r\n";
$fd .= " xmlns=\"http://www.topografix.com/GPX/1/0\">\r\n\r\n";
$fd .= "<name>Opencaching.pl Oregon multiload</name>\r\n";
session_start();
if (isset($_SESSION['log_cache_multi_filteredData'])) {
$tempList = [];
foreach ($_SESSION['log_cache_multi_filteredData'] as $k => $v) {
$v['cache_creator'] = str_replace('&', ' ', $v['cache_creator']);
$fd .= '<wpt lat="' . $v['latitude'] . '" lon="' . $v['longitude'] . "\">\r\n";
$fd .= ' <time>' . $v['rok'] . '-' . $v['msc'] . '-' . $v['dzien'] . 'T' . $v['godz'] . ':' . $v['min'] . ":00Z</time>\r\n";
$fd .= ' <name>' . $v['kod_str'] . ' ' . mb_substr($v['cache_name'], 0, 20, 'UTF-8') . "</name>\r\n";
$fd .= ' <desc>' . $v['cache_name'] . ' by ' . $v['cache_creator'] . "</desc>\r\n";
$fd .= ' <url>https://opencaching.pl/viewcache.php?cacheid=' . $v['cache_id'] . "</url>\r\n";
$fd .= ' <urlname>' . $v['kod_str'] . ' ' . mb_substr($v['cache_name'], 0, 20, 'UTF-8') . "</urlname>\r\n";
if ($v['status'] == 1 || $v['status'] == 5) { // found, need maintenance
$fd .= " <sym>Geocache found</sym>\r\n";
} else {
$fd .= " <sym>Geocache</sym>\r\n";
}
// $fd .= " <sym>Flag, ".($v['status'] == "1" ? "Blue" : "Red")."</sym>\r\n";
$fd .= " <geocache xmlns=\"http://genpol.com/temp/geocache/\">\r\n";
$fd .= ' <name>' . $v['kod_str'] . ' ' . mb_substr($v['cache_name'], 0, 20, 'UTF-8') . "</name>\r\n";
$fd .= ' <owner>' . $v['cache_creator'] . "</owner>\r\n";
switch ($v['cache_type']) {
case 2:
$fd .= " <type>Traditional</type>\r\n";
break;
case 3:
$fd .= " <type>Multi</type>\t\n";
break;
case 4:
$fd .= " <type>Virtual</type>\r\n";
break;
case 5:
$fd .= " <type>Webcam</type>\r\n";
break;
case 6:
$fd .= " <type>Event</type>\r\n";
break;
default:
$fd .= " <type>Other</type>\r\n";
}
$fd .= " <container>0</container>\r\n";
$fd .= ' <difficulty>' . $v['cache_difficulty'] . "</difficulty>\r\n";
$fd .= ' <terrain>' . $v['cache_terrain'] . "</terrain>\r\n";
$fd .= " </geocache>\r\n";
$fd .= "</wpt>\r\n";
// pre-define TRK
$tmpDate = $v['timestamp'] - 4 * 60 * 60; // -4 godziny do daty.
$td = date('Ymd', $tmpDate);
if (! isset($tempList[$td])) { // zaloz dla daty:
$tempList[$td]['name'] = 'OC-PL ' . date('Y-m-d', $tmpDate);
$tempList[$td]['pts'] = [];
}
$tempList[$td]['pts'][] = ' <trkpt lat="' . $v['latitude'] . '" lon="' . $v['longitude'] . '"><time>' . $v['rok'] . '-' . $v['msc'] . '-' . $v['dzien'] . 'T' . $v['godz'] . ':' . $v['min'] . ":00Z</time></trkpt>\r\n";
}
// EOF waypointy - zdefiniowane.
foreach ($tempList as $k => $v) {
$fd .= "\r\n<trk>\r\n";
$fd .= ' <name>' . $v['name'] . "</name>\r\n";
$fd = " <trkseg>\r\n" . implode('', $v['pts']);
$fd .= " </trkseg>\r\n";
$fd .= "</trk>\r\n";
}
$fd .= '</gpx>';
$filname = date('Ymd-Hi') . '.gpx';
header('Content-type: application/x-download');
header('Content-type: text/plain; charset=utf-8');
header('Cache-Control: no-cache');
// header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . $filname . '"');
echo pack('ccc', 0xef, 0xbb, 0xbf);
echo $fd;
exit();
}
exit('No data');
}
require_once __DIR__ . '/lib/common.inc.php';
$no_tpl_build = false;
$loggedUser = ApplicationContainer::GetAuthorizedUser();
if (! $loggedUser || (! isset($_FILES['userfile']) && ! isset($_SESSION['log_cache_multi_data']))) {
tpl_redirect('log_cache_multi_send.php');
exit;
}
$tplname = 'log_cache_multi';
$myHtml = '';
$statusy = [];
$statusy = fcGetStatusyEn();
// moje dane o skrzynkach z pliku...
$dane = [];
if (isset($_FILES['userfile'])) {
// usuwam zapamietane a nieaktualne juz dane...
unset($_SESSION['log_cache_multi_data'], $_SESSION['log_cache_multi_filteredData'], $_SESSION['filter_to'], $_SESSION['filter_from']);
// czy wyslalo się ok?
if ($_FILES['userfile']['error'] != 0) {
// jesli nie to jaki blad?
if ($_FILES['userfile']['error'] == 2) {
exit('Plik zbyt duzy');
}
exit;
}
// czy ktos cos nie kombinuje?
if (! is_uploaded_file($_FILES['userfile']['tmp_name'])) {
exit('Cos nie tak z wysylaniem pliku, sprobuj ponownie...');
}
// wczytuje plik
$some_file = $_FILES['userfile']['tmp_name'];
$filesize = filesize($some_file);
$fp = fopen($some_file, 'r');
$filecontent = fread($fp, $filesize);
fclose($fp);
// kasuje tymczasowy plik uploadu
unlink($_FILES['userfile']['tmp_name']);
unset($_FILES['userfile']);
$encoding = mb_detect_encoding($filecontent, ['UTF-8', 'UTF-16', 'UTF-16LE', 'UTF-16BE'], true);
if ($encoding !== false && $encoding !== 'UTF-8') {
$filecontent = mb_convert_encoding($filecontent, 'UTF-8', $encoding);
}
// // sprawdz czy utf16 i konwert jesli tak
// if (($filesize >= 2)
// && (
// ($filecontent[0] == 0x00 || $filecontent[1] == 0x00)
// || ($filecontent[0] == 0xFF && $filecontent[1] == 0xFE)
// || ($filecontent[0] == 0xFE || $filecontent[1] == 0xFF)
// )
// ) {
// $filecontent = utf16_to_utf8($filecontent);
// }
if (strlen($filecontent) >= 3 && ord($filecontent[0]) == 0xEF && ord($filecontent[1]) == 0xBB && ord($filecontent[2]) == 0xBF) {
// cut UTF-8 BOM
$filecontent = substr($filecontent, 3);
}
$filecontent = explode("\n", $filecontent);
$dane_i = -1;
// parsuje plik
$listaKodowOP = [];
foreach ($filecontent as $line) {
$rec = explode(',', trim($line), 4);
if (count($rec) >= 4) {
// wyglada na skrzynke
if (substr($rec[0], 0, 2) == $GLOBALS['oc_waypoint']) {
$dane_i++;
$dane[$dane_i]['kod_str'] = $rec[0];
$dane[$dane_i]['typ_kodu'] = $GLOBALS['oc_waypoint'];
$listaKodowOP[] = $dane[$dane_i]['kod_str'];
// kod
// czas
$regex = '/^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2})[:Z]/';
if (preg_match($regex, trim($rec[1]), $matches)) {
$dane[$dane_i]['timestamp'] = mktime(
$matches[4],
$matches[5],
0,
$matches[2],
$matches[3],
$matches[1]
);
} else {
// default to current date/time if date/time information is invalid
$dane[$dane_i]['timestamp'] = time();
}
unset($matches);
$dane[$dane_i] = UstawDatyZTimeStampa($dane[$dane_i]);
//status
$dane[$dane_i]['status'] = $statusy[trim($rec[2])] ?? 0;
// komentarz
$dane[$dane_i]['koment'] = str_replace('"', '', trim($rec[3]));
}
}
}
// plik sparsowany...
$_SESSION['log_cache_multi_data'] = $dane;
}// EOF jesli jest plik wyslany to parsowanie...
if (isset($_SESSION['log_cache_multi_data'])) {
$dane = $_SESSION['log_cache_multi_data'];
// pomocna lista do WHEREa
$listaKodowOP = [];
$minTimeStamp = time();
$maxTimeStamp = 1;
foreach ($dane as $k => $v) {
$listaKodowOP[] = XDb::xEscape($v['kod_str']);
if ($v['timestamp'] < $minTimeStamp) {
$minTimeStamp = $v['timestamp'];
}
if ($v['timestamp'] > $maxTimeStamp) {
$maxTimeStamp = $v['timestamp'];
}
}
// lista identyfikatorow cache ktore znalazlem w bazie
$cacheIdList = [];
// dociagam informacje o nazwie i id skrzynki...
if (count($listaKodowOP) > 0) {
$rs = XDb::xSql(
"SELECT c.*,u.`username`
FROM `caches` as c LEFT JOIN `user` as u ON u.`user_id` = c.`user_id`
WHERE c.`wp_oc` IN ('" . implode("','", $listaKodowOP) . "')"
);
while ($record = XDb::xFetchArray($rs)) {
// dodanie dodatkowych info do odpowiedniej skrzynki:
foreach ($dane as $k => $v) {
if ($v['kod_str'] == $record['wp_oc']) {
$v['got_sql_info'] = true;
$v['cache_id'] = $record['cache_id'];
$v['cache_type'] = $record['type'];
$v['cache_name'] = $record['name'];
$v['longitude'] = $record['longitude'];
$v['latitude'] = $record['latitude'];
$v['cache_creator'] = $record['username'];
$v['cache_difficulty'] = $record['difficulty'];
$v['cache_terrain'] = $record['terrain'];
$dane[$k] = $v;
$cacheIdList[] = $record['cache_id'];
}
}
}//while
}
// dociagam info o ostatniej aktywnosci dla kazdej skrzynki
if (count($cacheIdList) > 0) {
$rs = XDb::xSql(
'SELECT c.*
FROM (
SELECT cache_id, MAX(date) date FROM `cache_logs`
WHERE user_id= ?
AND cache_id IN (' . XDb::xEscape(implode(',', $cacheIdList)) . ')
GROUP BY cache_id
) as x
INNER JOIN `cache_logs` as c ON c.cache_id = x.cache_id
AND c.date = x.date',
$loggedUser->getUserId()
);
while ($record = XDb::xFetchArray($rs)) {
foreach ($dane as $k => $v) {
if (isset($v['cache_id']) && $v['cache_id'] == $record['cache_id']) {
$v['got_last_activity'] = true;
$v['last_date'] = substr($record['date'], 0, strlen($record['date']) - 3);
$v['last_status'] = $record['type'];
$dane[$k] = $v;
}
}
}//while
}//if
// filtrowanie...
// wczytanie wartosci filtrow, a jesli nie ma to odpowiednio min i max wartosc z pliku tak by wszystkie byly.
if (isset($_POST['filter_from']) && false !== strtotime($_POST['filter_from'])) {
$filter_from = strtotime($_POST['filter_from']);
} elseif (isset($_SESSION['filter_from'])) {
$filter_from = $_SESSION['filter_from'];
} else {
$filter_from = $minTimeStamp;
}
//jesli odjęcie godziny to odejmij z filter_from tez:
if (isset($_POST['SubmitShiftTimeMinusOne'])) {
$filter_from = $filter_from - (60 * 60);
}
$_SESSION['filter_from'] = $filter_from;
if (isset($_POST['filter_to']) && false !== strtotime($_POST['filter_to'])) {
$filter_to = strtotime($_POST['filter_to']);
} elseif (isset($_SESSION['filter_to'])) {
$filter_to = $_SESSION['filter_to'];
} else {
$filter_to = $maxTimeStamp;
}
// jesli dodanie godziny to dodaje do filter_to tez.
if (isset($_POST['SubmitShiftTimePlusOne'])) {
$filter_to = $filter_to + (60 * 60);
}
$_SESSION['filter_to'] = $filter_to;
// lece po wszystkim i kolejne opracje:
$daneFiltrowane = [];
foreach ($dane as $k => $v) {
if (isset($_POST['SubmitShiftTimeMinusOne'])) {
$v['timestamp'] = $v['timestamp'] - (60 * 60);
$v = UstawDatyZTimeStampa($v);
}
if (isset($_POST['SubmitShiftTimePlusOne'])) {
$v['timestamp'] = $v['timestamp'] + (60 * 60);
$v = UstawDatyZTimeStampa($v);
}
if ($v['timestamp'] <= $filter_to && $v['timestamp'] >= $filter_from) {
$doFiltra = true;
} else {
$doFiltra = false;
}
if ($doFiltra) {
// dodaje mass komentarze dla filtrowanych skrzynek:
if (isset($_POST['submitCommentsForm'], $_POST['logtext'])) {
$v['koment'] .= ' ' . $_POST['logtext'];
}
}
$dane[$k] = $v;
if ($doFiltra) {
$daneFiltrowane[$k] = $v; // uzywam $k by miec te same klucze co oryginalna tablica, przyda się pozniej.
}
}
// odswiezone dane do sesji:
$_SESSION['log_cache_multi_data'] = $dane;
$_SESSION['log_cache_multi_filteredData'] = $daneFiltrowane;
// oryginalna tablice mam zapisana w sesji, wiec tu spokojnie moge nadpisac do prezentacji.
$dane = $daneFiltrowane;
}
tpl_set_var('filter_from', date('d-m-Y H:i', $filter_from));
tpl_set_var('filter_to', date('d-m-Y H:i', $filter_to));
tpl_set_var('log_cache_multi_html', $myHtml);
//make the template and send it out
tpl_BuildTemplate();
function UstawDatyZTimeStampa($rekord)
{
$rekord['rok'] = date('Y', $rekord['timestamp']);
$rekord['msc'] = date('m', $rekord['timestamp']);
$rekord['dzien'] = date('d', $rekord['timestamp']);
$rekord['godz'] = date('H', $rekord['timestamp']);
$rekord['min'] = date('i', $rekord['timestamp']);
$rekord['data'] = date('d-m-Y H:i', $rekord['timestamp']);
return $rekord;
}
//function utf16_to_utf8($str)
//{
// $c0 = ord($str[0]);
// $c1 = ord($str[1]);
//
// if ($c0 == 0xFE && $c1 == 0xFF) {
// $str = substr($str, 2);
// $be = true;
// } elseif ($c0 == 0xFF && $c1 == 0xFE) {
// $str = substr($str, 2);
// $be = false;
// } elseif ($c0 != 0x00 && $c1 == 0x00) {
// $be = false;
// } elseif ($c0 == 0x00 && $c1 != 0x00) {
// $be = true;
// } else {
// return $str;
// }
//
// $len = strlen($str);
// $dec = '';
//
// for ($i = 0; $i < $len; $i += 2) {
// $c = ($be) ? ord($str[$i]) << 8 | ord($str[$i + 1]) : ord($str[$i + 1]) << 8 | ord($str[$i]);
//
// if ($c == 0xFEFF) {
// // Blad w GeoBeagle - wstawia BOM w srodku pliku - trzeba go ignorowac
// } elseif ($c >= 0x0001 && $c <= 0x007F) {
// $dec .= chr($c);
// } elseif ($c > 0x07FF) {
// $dec .= chr(0xE0 | (($c >> 12) & 0x0F));
// $dec .= chr(0x80 | (($c >> 6) & 0x3F));
// $dec .= chr(0x80 | (($c >> 0) & 0x3F));
// } else {
// $dec .= chr(0xC0 | (($c >> 6) & 0x1F));
// $dec .= chr(0x80 | (($c >> 0) & 0x3F));
// }
// }
//
// return $dec;
//}
function fcGetStatusyEn(): array
{
$statusy = [];
$statusy['Found it'] = 1; // Znaleziona
$statusy['Didn\'t find it'] = 2; // Nie znaleziona
$statusy['Unattempted'] = 3; // Komentarz
$statusy['Needs Maintenance'] = 5; // Potrzebny serwis
return $statusy;
}