11
11
use Illuminate \Support \Arr ;
12
12
use Hyde \Pages \MarkdownPage ;
13
13
use Hyde \Pages \MarkdownPost ;
14
+ use Desilva \Microserve \Request ;
14
15
use Desilva \Microserve \Response ;
15
16
use Hyde \Pages \Concerns \HydePage ;
16
17
use Hyde \Pages \DocumentationPage ;
20
21
use Desilva \Microserve \JsonResponse ;
21
22
use Hyde \Support \Filesystem \MediaFile ;
22
23
use Illuminate \Support \Facades \Process ;
23
- use Hyde \RealtimeCompiler \ConsoleOutput ;
24
24
use Hyde \Framework \Actions \StaticPageBuilder ;
25
25
use Hyde \Framework \Actions \AnonymousViewCompiler ;
26
- use Desilva \Microserve \Request ;
27
26
use Composer \InstalledVersions ;
28
27
use Hyde \Framework \Actions \CreatesNewPageSourceFile ;
29
28
use Hyde \Framework \Exceptions \FileConflictException ;
33
32
/**
34
33
* @internal This class is not intended to be edited outside the Hyde Realtime Compiler.
35
34
*/
36
- class DashboardController
35
+ class DashboardController extends BaseController
37
36
{
38
37
public string $ title ;
39
38
40
- protected Request $ request ;
41
- protected ConsoleOutput $ console ;
39
+ protected bool $ withConsoleOutput = true ;
40
+ protected bool $ withSession = true ;
41
+
42
42
protected JsonResponse $ response ;
43
43
44
44
protected bool $ isAsync = false ;
@@ -52,14 +52,11 @@ class DashboardController
52
52
'The dashboard update your project files. You can disable this by setting `server.dashboard.interactive` to `false` in `config/hyde.php`. ' ,
53
53
];
54
54
55
- public function __construct ()
55
+ public function __construct (? Request $ request = null )
56
56
{
57
- $ this ->title = config ('hyde.name ' ).' - Dashboard ' ;
58
- $ this ->request = Request::capture ();
57
+ parent ::__construct ($ request );
59
58
60
- if (((bool ) env ('HYDE_SERVER_REQUEST_OUTPUT ' , false )) === true ) {
61
- $ this ->console = new ConsoleOutput ();
62
- }
59
+ $ this ->title = config ('hyde.name ' ).' - Dashboard ' ;
63
60
64
61
$ this ->loadFlashData ();
65
62
@@ -75,11 +72,9 @@ public function handle(): Response
75
72
return $ this ->sendJsonErrorResponse (403 , 'Enable `server.editor` in `config/hyde.php` to use interactive dashboard features. ' );
76
73
}
77
74
78
- if ($ this ->shouldUnsafeRequestBeBlocked ()) {
79
- return $ this ->sendJsonErrorResponse (403 , "Refusing to serve request from address {$ _SERVER ['REMOTE_ADDR ' ]} (must be on localhost) " );
80
- }
81
-
82
75
try {
76
+ $ this ->authorizePostRequest ();
77
+
83
78
return $ this ->handlePostRequest ();
84
79
} catch (HttpException $ exception ) {
85
80
if (! $ this ->isAsync ) {
@@ -98,7 +93,7 @@ public function handle(): Response
98
93
protected function show (): string
99
94
{
100
95
return AnonymousViewCompiler::handle (__DIR__ .'/../../resources/dashboard.blade.php ' , array_merge (
101
- (array ) $ this , ['dashboard ' => $ this , 'request ' => $ this ->request ],
96
+ (array ) $ this , ['dashboard ' => $ this , 'request ' => $ this ->request , ' csrfToken ' => $ this -> generateCSRFToken () ],
102
97
));
103
98
}
104
99
@@ -451,38 +446,13 @@ protected static function getPackageVersion(string $packageName): string
451
446
return $ prettyVersion ?? 'unreleased ' ;
452
447
}
453
448
454
- protected function shouldUnsafeRequestBeBlocked (): bool
455
- {
456
- // As the dashboard is not password-protected, and it can make changes to the file system,
457
- // we block any requests that are not coming from the host machine. While we are clear
458
- // in the documentation that the realtime compiler should only be used for local
459
- // development, we still want to be extra careful in case someone forgets.
460
-
461
- $ requestIp = $ _SERVER ['REMOTE_ADDR ' ];
462
- $ allowedIps = ['::1 ' , '127.0.0.1 ' , 'localhost ' ];
463
-
464
- return ! in_array ($ requestIp , $ allowedIps , true );
465
- }
466
-
467
449
protected function setJsonResponse (int $ statusCode , string $ body ): void
468
450
{
469
451
$ this ->response = new JsonResponse ($ statusCode , $ this ->matchStatusCode ($ statusCode ), [
470
452
'body ' => $ body ,
471
453
]);
472
454
}
473
455
474
- protected function sendJsonErrorResponse (int $ statusCode , string $ message ): JsonResponse
475
- {
476
- return new JsonResponse ($ statusCode , $ this ->matchStatusCode ($ statusCode ), [
477
- 'error ' => $ message ,
478
- ]);
479
- }
480
-
481
- protected function abort (int $ code , string $ message ): never
482
- {
483
- throw new HttpException ($ code , $ message );
484
- }
485
-
486
456
protected function findGeneralOpenBinary (): string
487
457
{
488
458
return match (PHP_OS_FAMILY ) {
@@ -496,28 +466,8 @@ protected function findGeneralOpenBinary(): string
496
466
};
497
467
}
498
468
499
- protected function matchStatusCode (int $ statusCode ): string
500
- {
501
- return match ($ statusCode ) {
502
- 200 => 'OK ' ,
503
- 201 => 'Created ' ,
504
- 400 => 'Bad Request ' ,
505
- 403 => 'Forbidden ' ,
506
- 404 => 'Not Found ' ,
507
- 409 => 'Conflict ' ,
508
- default => 'Internal Server Error ' ,
509
- };
510
- }
511
-
512
469
protected function hasAsyncHeaders (): bool
513
470
{
514
471
return (getallheaders ()['X-RC-Handler ' ] ?? getallheaders ()['x-rc-handler ' ] ?? null ) === 'Async ' ;
515
472
}
516
-
517
- protected function writeToConsole (string $ message , string $ context = 'dashboard ' ): void
518
- {
519
- if (isset ($ this ->console )) {
520
- $ this ->console ->printMessage ($ message , $ context );
521
- }
522
- }
523
473
}
0 commit comments