added pargar

This commit is contained in:
2026-07-05 16:43:34 +03:30
parent 694fddc909
commit 7ac7bbdb90
27 changed files with 1341 additions and 493 deletions
-66
View File
@@ -1,66 +0,0 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use App\Models\Pargar;
use App\Jobs\LogRouteCustomerJob;
use Illuminate\Support\Facades\Http;
class LogRequests
{
public function handle(Request $request, Closure $next)
{
// زمان شروع بر حسب میلی‌ثانیه
$start = microtime(true);
try {
// لاگ قبل از اجرای کنترلر
$Pargar = new Pargar();
$Pargar->ip = $request->ip();
$Pargar->m = $request->route()?->getName() ?? 'not_found';
//get action name from controller
if ($route = $request->route()) {
// تلاش برای گرفتن نام اکشن (کنترلر و متد)
$Pargar->function_name = $route->getActionName();
} else {
$Pargar->function_name = 'not_found';
}
$Pargar->finance_data = json_encode($request->all(), JSON_UNESCAPED_UNICODE);
$Pargar->finance_head = json_encode($request->headers->all(), JSON_UNESCAPED_UNICODE);
$Pargar->url = $request->fullUrl();
$Pargar->method = $request->method();
$Pargar->save();
} catch (\Throwable $e) {
$Pargar = null; // برای اطمینان از عدم خطا در ادامه
}
// اجرای درخواست اصلی
$response = $next($request);
try {
if ($Pargar) {
// if ($route = $request->route()) {
// try {
// $Pargar->m = $route
// ? $route->getActionName() . ' (' . (new \ReflectionClass($route->getController()))->getFileName() . ')'
// : 'unknown';
// } catch (\Throwable $e) {
// $Pargar->m = 'unknown';
// }
//
// }
$Pargar->response = $response->getContent();
$Pargar->ms = round((microtime(true) - $start) * 1000, 2); // زمان در میلی‌ثانیه
$Pargar->save();
}
} catch (\Throwable $e) {}
return $response;
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace App\Http\Middleware;
use App\Services\ConfigService;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
class PargarAuth
{
public function handle(Request $request, Closure $next)
{
if (! app(ConfigService::class)->isPargarAccounting()) {
return response()->json([
'success' => false,
'message' => 'Pargar integration is disabled',
'data' => null,
], 503);
}
$auth = $request->header('Authorization') ?? $request->header('pargar');
$token = is_string($auth) ? trim(str_replace('Bearer ', '', $auth)) : '';
if ($token === '' || ! $this->tokenValid($token)) {
return response()->json([
'success' => false,
'message' => 'Invalid credentials!',
'data' => null,
], 401);
}
return $next($request);
}
public static function issueToken(string $username): string
{
$token = bin2hex(random_bytes(32));
Cache::put(self::cacheKey($token), $username, now()->addDay());
return $token;
}
public static function tokenValid(string $token): bool
{
$config = app(ConfigService::class);
$static = trim($config->getString('pargar_api_token', ''));
if ($static !== '' && hash_equals($static, $token)) {
return true;
}
return Cache::has(self::cacheKey($token));
}
private static function cacheKey(string $token): string
{
return 'pargar_api_token:' . $token;
}
}