1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Terminal\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Routing\Controller; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use Recca0120\Terminal\Kernel; |
11
|
|
|
|
12
|
|
|
class TerminalController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* $request. |
16
|
|
|
* |
17
|
|
|
* @var \Illuminate\Http\Request |
18
|
|
|
*/ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* $responseFactory. |
23
|
|
|
* |
24
|
|
|
* @var \Illuminate\Contracts\Routing\ResponseFactory |
25
|
|
|
*/ |
26
|
|
|
protected $responseFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* __construct. |
30
|
|
|
* |
31
|
|
|
* @param \Illuminate\Http\Request $request |
32
|
|
|
* @param \Illuminate\Contracts\Routing\ResponseFactory $responseFactory |
33
|
|
|
*/ |
34
|
3 |
|
public function __construct(Request $request, ResponseFactory $responseFactory) |
|
|
|
|
35
|
|
|
{ |
36
|
3 |
|
$this->request = $request; |
37
|
3 |
|
$this->responseFactory = $responseFactory; |
38
|
3 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* index. |
42
|
|
|
* |
43
|
|
|
* @param \Recca0120\Terminal\Kernel $kernel |
44
|
|
|
* @param string $view |
45
|
|
|
* @return \Illuminate\Http\Response |
46
|
|
|
*/ |
47
|
1 |
|
public function index(Kernel $kernel, $view = 'index') |
48
|
|
|
{ |
49
|
1 |
|
$token = null; |
50
|
1 |
|
if ($this->request->hasSession() === true) { |
51
|
1 |
|
$token = $this->request->session()->token(); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
$kernel->call('list --ansi'); |
55
|
1 |
|
$options = json_encode(array_merge($kernel->getConfig(), [ |
56
|
1 |
|
'csrfToken' => $token, |
57
|
1 |
|
'helpInfo' => $kernel->output(), |
58
|
|
|
])); |
59
|
1 |
|
$id = ($view === 'panel') ? Str::random(30) : null; |
60
|
|
|
|
61
|
1 |
|
return $this->responseFactory->view('terminal::'.$view, compact('options', 'id')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* rpc response. |
66
|
|
|
* |
67
|
|
|
* @param \Recca0120\Terminal\Kernel $kernel |
68
|
|
|
* @return \Illuminate\Http\JsonResponse |
69
|
|
|
*/ |
70
|
2 |
|
public function endpoint(Kernel $kernel) |
71
|
|
|
{ |
72
|
2 |
|
$code = $kernel->call( |
73
|
2 |
|
$this->request->get('method'), |
74
|
2 |
|
$this->request->get('params', []) |
75
|
|
|
); |
76
|
|
|
|
77
|
2 |
|
if ($code !== 0) { |
78
|
1 |
|
return $this->responseFactory->json([ |
79
|
1 |
|
'jsonrpc' => $this->request->get('jsonrpc'), |
80
|
|
|
'id' => null, |
81
|
|
|
'error' => [ |
82
|
|
|
'code' => -32600, |
83
|
1 |
|
'message' => 'Invalid Request', |
84
|
1 |
|
'data' => $kernel->output(), |
85
|
|
|
], |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return $this->responseFactory->json([ |
90
|
1 |
|
'jsonrpc' => $this->request->get('jsonrpc'), |
91
|
1 |
|
'id' => $this->request->get('id'), |
92
|
1 |
|
'result' => $kernel->output(), |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* media. |
98
|
|
|
* |
99
|
|
|
* @param \Illuminate\Filesystem\Filesystem $files |
100
|
|
|
* @param string $file |
101
|
|
|
* @return \Illuminate\Http\Response |
102
|
|
|
*/ |
103
|
|
|
public function media(Filesystem $files, $file) |
104
|
|
|
{ |
105
|
|
|
$filename = __DIR__.'/../../../public/'.$file; |
106
|
|
|
$mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript'; |
107
|
|
|
$lastModified = $files->lastModified($filename); |
108
|
|
|
$eTag = sha1_file($filename); |
109
|
|
|
$headers = [ |
110
|
|
|
'content-type' => $mimeType, |
111
|
|
|
'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT', |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
if (@strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified || |
115
|
|
|
trim($this->request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag |
116
|
|
|
) { |
117
|
|
|
$response = $this->responseFactory->make(null, 304, $headers); |
118
|
|
|
} else { |
119
|
|
|
$response = $this->responseFactory->stream(function () use ($filename) { |
120
|
|
|
$out = fopen('php://output', 'wb'); |
121
|
|
|
$file = fopen($filename, 'rb'); |
122
|
|
|
stream_copy_to_stream($file, $out, filesize($filename)); |
123
|
|
|
fclose($out); |
124
|
|
|
fclose($file); |
125
|
|
|
}, 200, $headers); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $response->setEtag($eTag); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|