From 3bc5f280268f2259d26407f24778e938516209a1 Mon Sep 17 00:00:00 2001 From: fokosun Date: Thu, 21 Sep 2023 15:15:35 -0400 Subject: [PATCH] fix up keywords --- app/Http/Controllers/SearchController.php | 42 ++++++++++++++-- app/Utils/LocationHelper.php | 60 +++++++++++++++++++++++ public/storage | 1 + 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 app/Utils/LocationHelper.php create mode 120000 public/storage diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index a20f2636..5b4053a1 100755 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -6,10 +6,13 @@ use App\Http\Requests\SearchRequest; use App\Services\SearchService; +use App\Utils\LocationHelper; use Illuminate\Http\Request; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; +use League\Flysystem\Visibility; use Symfony\Component\HttpFoundation\Response as ResponseAlias; class SearchController extends Controller @@ -176,9 +179,42 @@ private function jsonResponse(Collection $collection) public function writeToCsv(Request $request) { - $data = $request->only(['city', 'country', 'ip', 'keyword', 'loc', 'timezone']); - //TODO: if any data is null, fill up, default keyword to "" + $payload = $request->only(['city', 'country', 'ip', 'keyword', 'loc', 'timezone']); - Storage::disk('public')->append('keywords.txt', json_encode($data), "," . PHP_EOL); + if (array_key_exists('keyword', $payload)) { + + if (!array_key_exists('ip', $payload)) { + $payload['ip'] = $request->getClientIp(); + } + + if (!array_key_exists('timezone', $payload)) { + $payload['timezone'] = "Unknown"; + } + + if (!array_key_exists('loc', $payload)) { + $payload['loc'] = LocationHelper::getLocFromIpAddress($payload['ip']); + } + + if (!array_key_exists('country', $payload)) { + $payload['country'] = LocationHelper::getCountryCodeFromIpAddress($payload['ip']); + } + + $dataToWrite = []; + + if (Storage::disk('public')->get('keywords.txt')) { + $contents = json_decode(Storage::disk('public')->get('keywords.txt'), true); + + if (count($contents) > 0) { + $contents[] = $payload; + $dataToWrite = $contents; + } + + } else { + $dataToWrite = [$payload]; + } + + Storage::disk('public') + ->put('keywords.txt', json_encode($dataToWrite),['visibility' => Visibility::PUBLIC]); + } } } diff --git a/app/Utils/LocationHelper.php b/app/Utils/LocationHelper.php new file mode 100644 index 00000000..31800bc2 --- /dev/null +++ b/app/Utils/LocationHelper.php @@ -0,0 +1,60 @@ +leftJoin( + 'ip2nationCountries', + 'ip2nationCountries.iso_code_2', + '=', + 'ip2nation.country' + ) + ->where('ip2nation.ip', '=', $sanitizeIp) + ->get(['ip2nationCountries.lon', 'ip2nationCountries.lat']) + ->first(); + + if ($lonLat->lon !== null && $lonLat->lat !== null) { + return sprintf("%s,%s", number_format($lonLat->lon, 4), number_format($lonLat->lat, 4)); + } + + return sprintf("%s,%s", "unknown", "unknown"); + } + + public static function getCountryCodeFromIpAddress(string $ipAddress): string + { + $sanitizeIp = Str::replace(".", "", $ipAddress); + + if (self::isPrivate($ipAddress)) { + $country = "Private"; + } else { + $country = DB::table('ip2nation') + ->where('ip', '=', $sanitizeIp) + ->pluck('country') + ->first(); + } + + return sprintf("%s", $country); + } + + private static function isPrivate(string $ipAddress) + { + $privateIps = ["172.21.0.1"]; + + return in_array($ipAddress, $privateIps); + } +} diff --git a/public/storage b/public/storage new file mode 120000 index 00000000..69db7b08 --- /dev/null +++ b/public/storage @@ -0,0 +1 @@ +/var/www/storage/app/public \ No newline at end of file