Skip to content

Commit acd5ef6

Browse files
authored
Merge pull request #4299 from Laravel-Backpack/add-fix-command-in-4.1
Add fix command in 4.1
2 parents cf2080c + 3cd1fd4 commit acd5ef6

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/BackpackServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BackpackServiceProvider extends ServiceProvider
2323
\Backpack\CRUD\app\Console\Commands\PublishBackpackMiddleware::class,
2424
\Backpack\CRUD\app\Console\Commands\PublishView::class,
2525
\Backpack\CRUD\app\Console\Commands\RequireDevTools::class,
26+
\Backpack\CRUD\app\Console\Commands\Fix::class,
2627
];
2728

2829
// Indicates if loading of the provider is deferred.

src/app/Console/Commands/Fix.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Console\Commands;
4+
5+
use Artisan;
6+
use Illuminate\Console\Command;
7+
8+
class Fix extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'backpack:fix';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Fix known Backpack issues.';
23+
24+
/**
25+
* Execute the console command.
26+
*
27+
* @return mixed
28+
*/
29+
public function handle()
30+
{
31+
$this->fixErrorViews();
32+
33+
if ($this->confirm('[SUGGESTION] Would you like to publish updated JS & CSS dependencies to public/packages?', false)) {
34+
Artisan::call('vendor:publish', [
35+
'--provider' => 'Backpack\CRUD\BackpackServiceProvider',
36+
'--tag' => 'assets',
37+
'--force' => 'true',
38+
]);
39+
$this->info('Published latest CSS and JS assets to your public/packages directory.');
40+
}
41+
}
42+
43+
private function fixErrorViews()
44+
{
45+
$errorsDirectory = base_path('resources/views/errors');
46+
47+
$this->line('Checking error views...');
48+
49+
// check if the `resources/views/errors` directory exists
50+
if (! is_dir($errorsDirectory)) {
51+
$this->info('Your error views are not vulnerable. Nothing to do here.');
52+
53+
return;
54+
}
55+
56+
$views = scandir($errorsDirectory);
57+
$views = array_filter($views, function ($file) {
58+
// eliminate ".", ".." and any hidden files like .gitignore or .gitkeep
59+
return substr($file, 0, 1) != '.';
60+
});
61+
62+
// check if there are actually views inside the directory
63+
if (! count($views)) {
64+
$this->info('Your error views are not vulnerable. Nothing to do here.');
65+
66+
return;
67+
}
68+
69+
$autofixed = true;
70+
foreach ($views as $key => $view) {
71+
$contents = file_get_contents($errorsDirectory.'/'.$view);
72+
73+
// does it even work with exception messages?
74+
if (strpos($contents, '->getMessage()') == false) {
75+
continue;
76+
}
77+
78+
// does it already escape the exception message?
79+
if (strpos($contents, 'e($exception->getMessage())') !== false) {
80+
$this->info($view.' was ok.');
81+
continue;
82+
}
83+
84+
// cover the most likely scenario, where the file has not been edited at all
85+
$new_contents = str_replace('$exception->getMessage()?$exception->getMessage():$default_error_message', '$exception->getMessage()?e($exception->getMessage()):$default_error_message', $contents);
86+
87+
if ($new_contents != $contents) {
88+
file_put_contents($errorsDirectory.'/'.$view, $new_contents);
89+
$this->warn($view.' has been fixed.');
90+
continue;
91+
}
92+
93+
$this->error($view.' could not be fixed automatically.');
94+
$autofixed = false;
95+
}
96+
97+
if ($autofixed == false) {
98+
$this->error('Some error views could not be fixed automatically. Please look inside your "resources/views/errors" directory and make sure exception messages are escaped before outputting. It should be e($exception->getMessage()) instead of $exception->getMessage(). Alternatively, outputting should be done using {{ }} instead of {!! !!}');
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)