| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Recca0120\Terminal\Http\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Recca0120\Terminal\Kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Routing\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Contracts\Routing\ResponseFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 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 | 2 |  |     public function __construct(Request $request, ResponseFactory $responseFactory) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |         $this->request = $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |         $this->responseFactory = $responseFactory; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 | 2 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 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 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 1 |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 1 |  |     public function endpoint(Kernel $kernel) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |         $error = $kernel->call( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |             $this->request->get('command'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |             $this->request->get('parameters', []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |         $key = $error === 0 ? 'result' : 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         return $this->responseFactory->json([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |             'jsonrpc' => $this->request->get('jsonrpc'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |             'id' => $this->request->get('id'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |             $key => $kernel->output(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 1 |  |         ]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * media. | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @param \Illuminate\Filesystem\Filesystem $files | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @param string $file | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * @return \Illuminate\Http\Response | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     public function media(Filesystem $files, $file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $filename = __DIR__.'/../../../public/'.$file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $mimeType = strpos($filename, '.css') !== false ? 'text/css' : 'application/javascript'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $lastModified = $files->lastModified($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $eTag = sha1_file($filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $headers = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'content-type' => $mimeType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             'last-modified' => date('D, d M Y H:i:s ', $lastModified).'GMT', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         if (@strtotime($this->request->server('HTTP_IF_MODIFIED_SINCE')) === $lastModified || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             trim($this->request->server('HTTP_IF_NONE_MATCH'), '"') === $eTag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $response = $this->responseFactory->make(null, 304, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $response = $this->responseFactory->stream(function () use ($filename) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $out = fopen('php://output', 'wb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $file = fopen($filename, 'rb'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 stream_copy_to_stream($file, $out, filesize($filename)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                 fclose($out); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 fclose($file); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             }, 200, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         return $response->setEtag($eTag); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 120 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  |