138 lines
4.1 KiB
PHP
138 lines
4.1 KiB
PHP
<?php
|
|
|
|
use App\Events\EventType;
|
|
use App\Models\System\Verification;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Psr7\Request as GuzzleRequest;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Stevebauman\Location\Facades\Location;
|
|
|
|
/**
|
|
* Check if a client IP is in our Server subnet
|
|
*
|
|
* @param string $server_ip
|
|
*/
|
|
function isLocalIp(string $ip = ''): bool
|
|
{
|
|
return ! filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
|
|
}
|
|
|
|
/**
|
|
* Un mask phone from "+(993)-xx-xx-xx-xx"
|
|
*/
|
|
function unMaskPhone(string|int $phone): string
|
|
{
|
|
return substr(str_replace(['+', '(', ')', '-', '_'], '', $phone), 3);
|
|
}
|
|
|
|
/**
|
|
* Send a sms
|
|
*
|
|
* @return void
|
|
*/
|
|
function sendSMS(string|int $phone, string|int $message)
|
|
{
|
|
$client = new Client();
|
|
$headers = [
|
|
'Content-Type' => 'application/json;charset=utf-8;',
|
|
'Charset' => 'UTF-8',
|
|
];
|
|
$body = 'JSON={
|
|
"SendRequest": {
|
|
"TerminalID": "Online_PANEL",
|
|
"Version": "1",
|
|
"Lang": "EN",
|
|
"MobilePhone": "993'.$phone.'",
|
|
"Text": "'.$message.'"
|
|
}
|
|
}';
|
|
$request = new GuzzleRequest('POST', 'http://10.3.158.103:8080/kpsmsroute/online.request', $headers, $body);
|
|
|
|
try {
|
|
$res = $client->sendAsync($request)->wait();
|
|
|
|
return $res->getBody();
|
|
} catch (Exception $e) {
|
|
Log::error($e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send a sms verification code
|
|
*/
|
|
function sendSMSVerification(string|int $phone_number): ?Verification
|
|
{
|
|
$phone_code = rand(10000, 99999);
|
|
$verification = Verification::where(['username' => $phone_number])->first();
|
|
$verification ? $verification->update(['code' => $phone_code]) : Verification::create(['username' => $phone_number, 'code' => $phone_code]);
|
|
|
|
sendSMS($phone_number, 'Tassyklaýyş belgi: '.$phone_code);
|
|
|
|
return $verification;
|
|
}
|
|
|
|
/**
|
|
* Store auth events
|
|
*/
|
|
function storeAuthEvent(string $name, Request $request): void
|
|
{
|
|
Log::channel('auth_activity')
|
|
->{EventType::logType($name)}(sprintf(
|
|
'%s, APP_NAME: %s, REQUEST_TYPE: %s, SOURCE_IP: %s, SOURCE_PORT: %s, SOURCE_URL: %s, DESTINATION_IP: %s, DESTINATION_PORT: %s, DESTINATION_COUNTRY: %s, USER_ID: %s',
|
|
$name,
|
|
config('app.name'),
|
|
$request->method(),
|
|
$request->ip(),
|
|
$_SERVER['REMOTE_PORT'],
|
|
$request->url(),
|
|
$request->host(),
|
|
$request->getPort(),
|
|
isLocalIp($request->ip()) ? 'TM' : Location::get($request->ip()),
|
|
$request->user()->id ?? '-',
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Store resource events
|
|
*/
|
|
function storeResourceEvent(string $name, array $data, Request $request): void
|
|
{
|
|
if ($name === EventType::laravelNovaActionEvent() || empty($data)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$before = [];
|
|
$after = [];
|
|
foreach ($data[0]->getChanges() as $key => $value) {
|
|
try {
|
|
$before[] = [$key => $data[0]->getOriginal()[$key]];
|
|
$after[] = [$key => $value];
|
|
} catch (Exception $e) {
|
|
Log::error($e->getMessage(), ['trace' => $e->getTraceAsString()]);
|
|
}
|
|
}
|
|
|
|
Log::channel('resource_activity')
|
|
->{EventType::logType($name)}(sprintf(
|
|
'%s, APP_NAME: %s, REQUEST_TYPE: %s, SOURCE_IP: %s, SOURCE_PORT: %s, SOURCE_URL: %s, DESTINATION_IP: %s, DESTINATION_PORT: %s, DESTINATION_COUNTRY: %s, USER_ID: %s, MODEL_NAME: %s, BEFORE: %s, AFTER: %s',
|
|
$name,
|
|
config('app.name'),
|
|
$request->method(),
|
|
$request->ip(),
|
|
$_SERVER['REMOTE_PORT'] ?? '-',
|
|
$request->url(),
|
|
$request->host(),
|
|
$request->getPort(),
|
|
isLocalIp($request->ip()) ? 'TM' : Location::get($request->ip()),
|
|
$request->user()->id ?? '-',
|
|
get_class($data[0]),
|
|
json_encode($before),
|
|
json_encode($after),
|
|
));
|
|
} catch (Exception $e) {
|
|
Log::error('Error in storeResourceEvent() helpers', ['error' => $e]);
|
|
}
|
|
}
|