|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\Terminal\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
|
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
8
|
|
|
use Illuminate\Http\JsonResponse; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Http\Response; |
|
11
|
|
|
use Illuminate\Routing\Controller; |
|
12
|
|
|
use Illuminate\Support\Str; |
|
13
|
|
|
use Recca0120\Terminal\Kernel; |
|
14
|
|
|
|
|
15
|
|
|
class TerminalController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* index. |
|
19
|
|
|
* |
|
20
|
|
|
* @param Kernel $kernel |
|
21
|
|
|
* @param Request $request |
|
22
|
|
|
* @param ResponseFactory $responseFactory |
|
23
|
|
|
* @param string $view |
|
24
|
|
|
* @return Response |
|
25
|
|
|
* @throws Exception |
|
26
|
|
|
*/ |
|
27
|
|
|
public function index(Kernel $kernel, Request $request, ResponseFactory $responseFactory, $view = 'index') |
|
28
|
|
|
{ |
|
29
|
|
|
$token = $request->hasSession() ? $request->session()->token() : ''; |
|
|
|
|
|
|
30
|
|
|
$kernel->call('list --ansi'); |
|
31
|
|
|
|
|
32
|
|
|
$options = json_encode(array_merge($kernel->getConfig(), ['csrfToken' => $token, 'helpInfo' => $kernel->output()])); |
|
33
|
|
|
$id = ($view === 'panel') ? Str::random(30) : null; |
|
34
|
3 |
|
|
|
35
|
|
|
return $responseFactory->view('terminal::'.$view, compact('options', 'id')); |
|
36
|
3 |
|
} |
|
37
|
3 |
|
|
|
38
|
3 |
|
/** |
|
39
|
|
|
* rpc response. |
|
40
|
|
|
* |
|
41
|
|
|
* @param Kernel $kernel |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @param ResponseFactory $responseFactory |
|
44
|
|
|
* @return JsonResponse |
|
45
|
|
|
* @throws Exception |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function endpoint(Kernel $kernel, Request $request, ResponseFactory $responseFactory) |
|
48
|
|
|
{ |
|
49
|
1 |
|
$code = $kernel->call($request->get('method'), $request->get('params', [])); |
|
50
|
1 |
|
|
|
51
|
1 |
|
$attributes = $code === 0 |
|
52
|
|
|
? ['jsonrpc' => $request->get('jsonrpc'), 'id' => $request->get('id'), 'result' => $kernel->output()] |
|
53
|
|
|
: ['jsonrpc' => $request->get('jsonrpc'), 'id' => null, 'error' => ['code' => -32600, 'message' => 'Invalid Request', 'data' => $kernel->output()]]; |
|
54
|
1 |
|
|
|
55
|
1 |
|
return $responseFactory->json($attributes); |
|
56
|
1 |
|
} |
|
57
|
1 |
|
|
|
58
|
|
|
/** |
|
59
|
1 |
|
* media. |
|
60
|
|
|
* |
|
61
|
1 |
|
* @param Request $request |
|
62
|
|
|
* @param ResponseFactory $responseFactory |
|
63
|
|
|
* @param Filesystem $files |
|
64
|
|
|
* @param string $file |
|
65
|
|
|
* @return Response |
|
66
|
|
|
*/ |
|
67
|
|
|
public function media(Request $request, ResponseFactory $responseFactory, Filesystem $files, $file) |
|
68
|
|
|
{ |
|
69
|
|
|
$filename = __DIR__.'/../../../public/'.$file; |
|
70
|
2 |
|
$mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript'; |
|
71
|
|
|
$lastModified = $files->lastModified($filename); |
|
72
|
2 |
|
$eTag = sha1_file($filename); |
|
73
|
2 |
|
$headers = [ |
|
74
|
2 |
|
'content-type' => $mimeType, |
|
75
|
|
|
'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT', |
|
76
|
|
|
]; |
|
77
|
2 |
|
|
|
78
|
1 |
|
if (@strtotime($request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified || |
|
79
|
1 |
|
trim($request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag |
|
80
|
|
|
) { |
|
81
|
|
|
$response = $responseFactory->make(null, 304, $headers); |
|
82
|
|
|
} else { |
|
83
|
1 |
|
$response = $responseFactory->stream(function () use ($filename) { |
|
84
|
1 |
|
$out = fopen('php://output', 'wb'); |
|
85
|
|
|
$file = fopen($filename, 'rb'); |
|
86
|
|
|
stream_copy_to_stream($file, $out, filesize($filename)); |
|
87
|
|
|
fclose($out); |
|
88
|
|
|
fclose($file); |
|
89
|
1 |
|
}, 200, $headers); |
|
90
|
1 |
|
} |
|
91
|
1 |
|
|
|
92
|
1 |
|
return $response->setEtag($eTag); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.