Skip to content

Commit

Permalink
fix up keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Sep 21, 2023
1 parent c51bbd9 commit 3bc5f28
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
42 changes: 39 additions & 3 deletions app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
}
}
}
60 changes: 60 additions & 0 deletions app/Utils/LocationHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace App\Utils;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class LocationHelper
{
public static function getLocFromIpAddress(string $ipAddress): string
{
if (self::isPrivate($ipAddress)) {
return sprintf("%s,%s", number_format(0, 4), number_format(0, 4));
}

$sanitizeIp = Str::replace(".", "", $ipAddress);

$lonLat = DB::table('ip2nation')

Check failure on line 20 in app/Utils/LocationHelper.php

View workflow job for this annotation

GitHub Actions / php-cs

Called 'first' on Laravel collection, but could have been retrieved as a query.
->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')

Check failure on line 45 in app/Utils/LocationHelper.php

View workflow job for this annotation

GitHub Actions / php-cs

Called 'first' on Laravel collection, but could have been retrieved as a query.
->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);
}
}
1 change: 1 addition & 0 deletions public/storage

0 comments on commit 3bc5f28

Please sign in to comment.