From fc4d692847cf7f5629539df9d3f44ed4044d1300 Mon Sep 17 00:00:00 2001 From: Jacek Pyziak Date: Tue, 12 Aug 2025 23:40:25 +0200 Subject: [PATCH] update --- autoload/.DS_Store | Bin 0 -> 6148 bytes autoload/front/.DS_Store | Bin 0 -> 6148 bytes devel.html | 614 +++++++++++---------------------------- geocode-cache.php | 211 ++++++++++++++ 4 files changed, 377 insertions(+), 448 deletions(-) create mode 100644 autoload/.DS_Store create mode 100644 autoload/front/.DS_Store create mode 100644 geocode-cache.php diff --git a/autoload/.DS_Store b/autoload/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..3239922a7e1693c4aee135bf32fa7086814704fc GIT binary patch literal 6148 zcmeHK%}N6?5T4Nr3%&H{F|W`!h-Lc(1-(iw>Y-~&(R-f8*YM`WC-UYuKZ=V1!Glzp zf#jQHX7ckbn`DTH7Y~~`(Ts>X6hRiHA=2G69l7%a$U4UT_GZ4iD#NZZ%wcxP+E3`3 z_EhMOTI>I09ZKJ?w`Gl(?&9_F<>~zSY#cw7%{Lyq+dsz8VScw*RWJ|?1Ovgq&l$j- zEz(U5qYegwfneZ+0XZKMiePrEhI(|M(i8wF&uA6sl1oTTa?Fm^5EckqD9}RLQw+9n z^e6Yrj@8h@i9PvXpZPs`VVxc0Cv_*zhEWFt!N8b-V{6Xj{$JvkX(svOkZ1)1!N5Ob zfG7Q`Utm*yw|?24+_eei5=BJ(swfcXqelQ5a*mv;(dmab_*8*1HOwQge!$xjr$bq1UPXP{v~z7GK^ zSQyrd>C=HGl>opD<|OD#Eg><%urRC@F#=%?1!^c;iNP8UdoaJkuvXM?Vk-3~NPLApRqeXz<1v_)`YH0K&6M2mk;8 literal 0 HcmV?d00001 diff --git a/devel.html b/devel.html index 4b26bf4..35783ec 100644 --- a/devel.html +++ b/devel.html @@ -1,468 +1,186 @@ - + - - - - - [TITLE] - - - [META_INDEX] - [CSS] - [JAVA_SCRIPT] - - -
-
-
-
- [KONTENER:1] -
-
- [UZYTKOWNIK_MINI_LOGOWANIE] -
-
-
-
- - -
- -
-
-
-
-
-
Kategorie
- [KATEGORIE] -
-
- [ZAWARTOSC] -
-
-
-
-
-
-
-
-
[PHP] global $lang; echo $lang['strefa-rodzica'];[/PHP]
-
[PHP] global $lang; echo ucfirst( $lang['najnowsze-porady'] );[/PHP]
-
-
-
- -
-
- [AKTUALNOSCI:9:3] -
-
-
-
-
-
-
- [KONTENER:2] -
-
-
-
- - - - \ No newline at end of file + // autofocus + $('#query').focus(); + + + diff --git a/geocode-cache.php b/geocode-cache.php new file mode 100644 index 0000000..b13641d --- /dev/null +++ b/geocode-cache.php @@ -0,0 +1,211 @@ + $raw, + 'norm' => $norm, + 'hash' => hash('sha256', $norm), + 'mode' => $mode, + ]; +} + +// DB +try { + $pdo = new PDO(DB_DSN, DB_USER, DB_PASS, [ + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + ]); +} catch (Throwable $e) { + http_response_code(500); + echo json_encode(['error' => 'DB connection failed']); + exit; +} + +// Wejście +$q = isset($_GET['q']) ? trim((string)$_GET['q']) : ''; +$lat = isset($_GET['lat']) ? (float)$_GET['lat'] : null; +$lng = isset($_GET['lng']) ? (float)$_GET['lng'] : null; + +if ($q === '' && ($lat === null || $lng === null)) { + http_response_code(400); + echo json_encode(['error' => 'Provide q=address or lat & lng']); + exit; +} + +$key = build_cache_key($q, $lat, $lng); +$now = now_mysql(); + +// ===== 1) CACHE LOOKUP (forward i reverse) ===== +$stmt = $pdo->prepare("SELECT * FROM geocode_cache WHERE query_hash = :h AND (expires_at IS NULL OR expires_at > NOW()) LIMIT 1"); +$stmt->execute([':h' => $key['hash']]); +if ($row = $stmt->fetch()) { + $pdo->prepare("UPDATE geocode_cache SET hits = hits + 1, updated_at = NOW() WHERE id = :id")->execute([':id' => $row['id']]); + echo json_encode([ + 'from_cache' => true, + 'source' => 'cache', + 'stale' => false, + 'lat' => (float)$row['lat'], + 'lng' => (float)$row['lng'], + 'formatted_address' => $row['formatted_address'], + 'place_id' => $row['place_id'], + 'provider' => $row['provider'], + ], JSON_UNESCAPED_UNICODE); + exit; +} + +// ===== 2) PYTANIE DO GOOGLE ===== +function google_request(array $params): array { + $base = 'https://maps.googleapis.com/maps/api/geocode/json'; + $params['key'] = GOOGLE_GEOCODE_KEY; + $params += ['language' => GOOGLE_LANGUAGE, 'region' => GOOGLE_REGION]; + $url = $base . '?' . http_build_query($params); + + $ch = curl_init($url); + curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, CURLOPT_CONNECTTIMEOUT => 3]); + $res = curl_exec($ch); $err = curl_error($ch); curl_close($ch); + if ($res === false) throw new RuntimeException('cURL error: ' . $err); + $data = json_decode($res, true); + if (!is_array($data)) throw new RuntimeException('Invalid JSON from Google'); + if (($data['status'] ?? '') !== 'OK' || empty($data['results'][0])) { + $status = $data['status'] ?? 'UNKNOWN'; + throw new RuntimeException('Geocoding failed: ' . $status); + } + return $data; +} + +try { + $data = ($key['mode'] === 'forward') + ? google_request(['address' => $q]) + : google_request(['latlng' => $lat . ',' . $lng]); + + $top = $data['results'][0]; + $latV = (float)$top['geometry']['location']['lat']; + $lngV = (float)$top['geometry']['location']['lng']; + $fmt = (string)($top['formatted_address'] ?? ''); + $pid = (string)($top['place_id'] ?? ''); + + // ===== 3) ZAPIS DO CACHE (forward i reverse) ===== + $ttlDays = ($key['mode'] === 'forward') ? CACHE_TTL_DAYS_FORWARD : CACHE_TTL_DAYS_REVERSE; + $expires = add_days($now, $ttlDays); + + $stmt = $pdo->prepare("INSERT INTO geocode_cache + (query_hash, query_raw, query_norm, lat, lng, formatted_address, place_id, provider, hits, created_at, updated_at, expires_at, raw_json) + VALUES (:h, :raw, :norm, :lat, :lng, :fmt, :pid, 'google', 1, :now, :now, :exp, :json) + ON DUPLICATE KEY UPDATE + lat = VALUES(lat), + lng = VALUES(lng), + formatted_address = VALUES(formatted_address), + place_id = VALUES(place_id), + updated_at = VALUES(updated_at), + expires_at = VALUES(expires_at), + raw_json = VALUES(raw_json), + hits = hits + 1"); + + $stmt->execute([ + ':h' => $key['hash'], + ':raw' => $key['raw'], + ':norm'=> $key['norm'], + ':lat' => $latV, + ':lng' => $lngV, + ':fmt' => $fmt, + ':pid' => $pid, + ':now' => $now, + ':exp' => $expires, + ':json'=> json_encode($data, JSON_UNESCAPED_UNICODE), + ]); + + echo json_encode([ + 'from_cache' => false, + 'source' => 'google', + 'stale' => false, + 'lat' => $latV, + 'lng' => $lngV, + 'formatted_address' => $fmt, + 'place_id' => $pid, + 'provider' => 'google', + ], JSON_UNESCAPED_UNICODE); + exit; + +} catch (Throwable $e) { + // ===== 4) Fallback: zwróć najświeższe co mamy (też dla reverse) ===== + try { + $stmt = $pdo->prepare("SELECT * FROM geocode_cache WHERE query_hash = :h LIMIT 1"); + $stmt->execute([':h' => $key['hash']]); + if ($row = $stmt->fetch()) { + http_response_code(200); + echo json_encode([ + 'from_cache' => true, + 'source' => 'cache', + 'stale' => true, + 'lat' => (float)$row['lat'], + 'lng' => (float)$row['lng'], + 'formatted_address' => $row['formatted_address'], + 'place_id' => $row['place_id'], + 'provider' => $row['provider'], + ], JSON_UNESCAPED_UNICODE); + exit; + } + } catch (Throwable $ignored) {} + + http_response_code(502); + echo json_encode(['error' => $e->getMessage()]); + exit; +} \ No newline at end of file