1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Kalizi\LaravelSpyhole\Http\Controllers;
|
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse;
|
6
|
|
|
use Illuminate\Routing\Controller;
|
7
|
|
|
use Illuminate\Support\Facades\Auth;
|
8
|
|
|
use Illuminate\Support\Str;
|
9
|
|
|
use Kalizi\LaravelSpyhole\Http\Requests\StoreEntryRequest;
|
10
|
|
|
use Kalizi\LaravelSpyhole\Models\SessionRecording;
|
11
|
|
|
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
|
12
|
|
|
|
13
|
|
|
class EntryController extends Controller
|
14
|
|
|
{
|
15
|
|
|
public function store(StoreEntryRequest $request): JsonResponse
|
16
|
|
|
{
|
17
|
|
|
$recordingId = null;
|
18
|
|
|
if ($request->has('recording')) {
|
19
|
|
|
$recordingId = (int)decrypt($request->get('recording'));
|
20
|
|
|
|
21
|
|
|
if (SessionRecording::whereId($recordingId)->count() === 0) {
|
22
|
|
|
throw new NotAcceptableHttpException();
|
23
|
|
|
}
|
24
|
|
|
}
|
25
|
|
|
|
26
|
|
|
if (config('laravel-spyhole.track_request_session_id')) {
|
27
|
|
|
$sessionId = $request->session()->getId();
|
|
|
|
|
28
|
|
|
} else {
|
29
|
|
|
if (session()->has('spyhole_session_id')) {
|
30
|
|
|
$sessionId = session()->get('spyhole_session_id');
|
31
|
|
|
} else {
|
32
|
|
|
do {
|
33
|
|
|
$sessionId = Str::uuid()->toString();
|
34
|
|
|
} while (
|
35
|
|
|
SessionRecording::whereSessionId($sessionId)->count() > 0 &&
|
36
|
|
|
$sessionId !== $request->session()->getId()
|
|
|
|
|
37
|
|
|
);
|
38
|
|
|
session()->put('spyhole_session_id', $sessionId);
|
39
|
|
|
}
|
40
|
|
|
}
|
41
|
|
|
|
42
|
|
|
$userId = null;
|
43
|
|
|
if (config('laravel-spyhole.record_user_id')) {
|
44
|
|
|
$user = Auth::user();
|
45
|
|
|
$userId = $user ? $user->getAuthIdentifier() : null;
|
46
|
|
|
}
|
47
|
|
|
|
48
|
|
|
if ($recordingId === null) {
|
49
|
|
|
$recording = new SessionRecording();
|
50
|
|
|
$recording->session_id = $sessionId;
|
51
|
|
|
$recording->user_id = $userId;
|
52
|
|
|
$recording->path = $request->get('path');
|
53
|
|
|
$recording->recordings = $request->get('frames');
|
54
|
|
|
} else {
|
55
|
|
|
$recording = SessionRecording::wherePath($request->get('path'))
|
56
|
|
|
->whereId($recordingId)
|
57
|
|
|
->first();
|
58
|
|
|
|
59
|
|
|
if ($recording === null) {
|
60
|
|
|
throw new NotAcceptableHttpException();
|
61
|
|
|
}
|
62
|
|
|
|
63
|
|
|
// Merge frames from the same session
|
64
|
|
|
$recording->recordings = array_merge(
|
65
|
|
|
$recording->recordings,
|
66
|
|
|
$request->get('frames')
|
67
|
|
|
);
|
68
|
|
|
}
|
69
|
|
|
|
70
|
|
|
$recording->save();
|
71
|
|
|
|
72
|
|
|
return response()->json([
|
|
|
|
|
73
|
|
|
'success' => true,
|
74
|
|
|
'recording' => encrypt($recording->id),
|
75
|
|
|
]);
|
76
|
|
|
}
|
77
|
|
|
}
|
78
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.