|
| 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