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