Skip to content

Commit

Permalink
Insert player play events to DB
Browse files Browse the repository at this point in the history
- Also this commit fixes rrze-mmz#123 and rrze-mmz#124

Signed-off-by: Stefanos Georgopoulos <[email protected]>
  • Loading branch information
stefanosgeo committed Aug 6, 2024
1 parent 8e429c1 commit bf71c32
Show file tree
Hide file tree
Showing 13 changed files with 273 additions and 60 deletions.
62 changes: 62 additions & 0 deletions app/Http/Controllers/Frontend/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
use App\Models\Organization;
use App\Models\Presenter;
use App\Models\Role;
use App\Models\Stats\AssetViewLog;
use App\Models\Tag;
use App\Models\User;
use http\Env\Response;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;

class ApiController extends Controller
{
Expand Down Expand Up @@ -129,4 +133,62 @@ public function images(ApiRequest $request): JsonResponse
];
}));
}

public function logPlayEvent(Request $request): JsonResponse
{
if (! $request->isJson()) {
return abort(404);
}

$remote_addr = array_key_exists('REMOTE_ADDR', $_SERVER) ? $_SERVER['REMOTE_ADDR'] : '';
if (empty($remote_addr)) {
return abort(404);
}

//get the assetID and clip acls from html page,
// instead of using always eloquent queries to find the data for each request
$validated = $request->validate([
'mediaID' => 'required|integer',
'serviceIDs' => 'required|array',
]);

$requestMethod = array_key_exists('REQUEST_METHOD', $_SERVER) ? $_SERVER['REQUEST_METHOD'] : '';
$userAgent = array_key_exists('HTTP_USER_AGENT', $_SERVER) ? $_SERVER['HTTP_USER_AGENT'] : '';
$ipString = @getenv('HTTP_X_FORWARDED_FOR');
$addr = explode(',', $ipString);
$ip_client = $addr[0] ?? '';
$ip_addr = ($ip_client != '') ? $ip_client : $remote_addr;
$ip_byte = explode('.', $ip_addr);
$ip_num =
(16777216 * (int) $ip_byte[0]) + (65536 * (int) $ip_byte[1]) + (256 * (int) $ip_byte[2]) + ((int) $ip_byte[3]);
if (check_valid_statistic_insert($ip_num, $requestMethod, $userAgent)) {
AssetViewLog::create([
'resource_id' => $validated['mediaID'],
'service_id' => collect($validated['serviceIDs'])->contains(1) ? 2 : 6,
'access_date' => Carbon::now()->format('Y-m-d'),
'access_time' => Carbon::now()->format('Y-m-d H:i:s'),
'remote_addr' => $remote_addr,
'remote_host' => $userAgent,
'remote_user' => $requestMethod,
'script_name' => array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '',
'is_counted' => '1',
'is_valid' => '1',
'referer' => array_key_exists('HTTP_REFERER', $_SERVER) ? $_SERVER['HTTP_REFERER'] : '',
'query' => array_key_exists('QUERY_STRING', $_SERVER) ? $_SERVER['QUERY_STRING'] : '',
'is_akamai' => '0',
'in_range' => '0',
'server' => env('APP_ENV'),
'range' => $_SERVER['HTTP_RANGE'] ?? '',
'response' => '200 OK',
'real_ip' => $ip_addr,
'num_ip' => $ip_num,
'is_bot' => '0',
'is_get' => '0',
]);

return response()->json();
} else {
return response()->json('Not valid IP address', 400);
}
}
}
13 changes: 13 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,16 @@ function removeHtmlElements(?string $text): string

return html_entity_decode($plainText, ENT_QUOTES, 'UTF-8');
}

function check_valid_statistic_insert($numIP, $requestMethod, $userAgent): bool
{
if (is_null($numIP) || is_null($requestMethod) || is_null($userAgent)) {
return false;
}

return match (true) {
$requestMethod !== 'POST' => false,
str_contains(strtolower($userAgent), 'bot') => false,
default => true,
};
}
50 changes: 25 additions & 25 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions database/factories/Stats/AssetViewLogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function definition(): array
'service_id' => Acl::PORTAL,
'access_date' => Carbon::now()->toDate(),
'access_time' => Carbon::now(),
'remove_addr' => $this->faker->ipv4,
'remote_addr' => $this->faker->ipv4,
'remote_host' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)',
'remote_user' => 'GET',
'script_name' => '/get/player'.$assetID,
Expand All @@ -34,7 +34,7 @@ public function definition(): array
'in_range' => false,
'referer' => 'localhost/clip/testClip-1',
'query' => '',
'is_akami' => false,
'is_akamai' => false,
'server' => env('app_env'),
'range' => '',
'response' => '200 OK',
Expand All @@ -46,6 +46,7 @@ public function definition(): array
'city' => $this->faker->city,
'country' => $this->faker->countryCode,
'counter3' => '',
'is_get' => false,
'is_bot' => false,
'region' => 'BY',
'region_name' => 'Bavaria',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Carbon\Carbon;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Expand All @@ -18,7 +19,7 @@ public function up(): void
$table->unsignedInteger('service_id');
$table->date('access_date');
$table->dateTime('access_time');
$table->ipAddress('remove_addr');
$table->ipAddress('remote_addr');
$table->string('remote_host');
$table->string('remote_user');
$table->string('script_name');
Expand All @@ -28,21 +29,22 @@ public function up(): void
$table->boolean('in_range');
$table->string('referer')->nullable();
$table->text('query')->nullable();
$table->boolean('is_akami');
$table->boolean('is_akamai');
$table->string('server')->default(env('APP_ENV', 'production'))->nullable();
$table->string('range')->nullable();
$table->string('response');
$table->ipAddress('real_ip');
$table->string('num_ip');
$table->dateTime('last_modified_at');
$table->dateTime('last_modified_at')->default(Carbon::now());
$table->string('last_modified_from')->nullable();
$table->string('bot_name')->nullable();
$table->string('city')->nullable();
$table->char('country', 2)->nullable();
$table->string('counter3')->nullable();
$table->boolean('is_bot');
$table->string('region');
$table->string('region_name');
$table->boolean('is_get');
$table->string('region')->nullable();
$table->string('region_name')->nullable();
});
}
}
Expand Down
3 changes: 3 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
@import '../../node_modules/pikaday/css/pikaday.css';
@import 'filepond/dist/filepond.min.css';
@import 'filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css';
@import 'vidstack/player/styles/default/theme.css';
@import 'vidstack/player/styles/default/layouts/video.css';
@import 'vidstack/player/styles/default/layouts/audio.css';

/*Dark mode switcher smooth transition*/
[x-cloak] {
Expand Down
Loading

0 comments on commit bf71c32

Please sign in to comment.