DeleteUserDomainBlock.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\User;
  5. use App\Models\DefaultDomainBlock;
  6. use App\Models\UserDomainBlock;
  7. use function Laravel\Prompts\text;
  8. use function Laravel\Prompts\confirm;
  9. use function Laravel\Prompts\progress;
  10. class DeleteUserDomainBlock extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'app:delete-user-domain-block';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Remove a domain block for all users';
  24. /**
  25. * Execute the console command.
  26. */
  27. public function handle()
  28. {
  29. $domain = text('Enter domain you want to unblock');
  30. $domain = strtolower($domain);
  31. $domain = $this->validateDomain($domain);
  32. $this->processUnblocks($domain);
  33. return;
  34. }
  35. protected function validateDomain($domain)
  36. {
  37. if(!strpos($domain, '.')) {
  38. $this->error('Invalid domain');
  39. return;
  40. }
  41. if(str_starts_with($domain, 'https://')) {
  42. $domain = str_replace('https://', '', $domain);
  43. }
  44. if(str_starts_with($domain, 'http://')) {
  45. $domain = str_replace('http://', '', $domain);
  46. }
  47. $valid = filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME|FILTER_NULL_ON_FAILURE);
  48. if(!$valid) {
  49. $this->error('Invalid domain');
  50. return;
  51. }
  52. $domain = strtolower(parse_url('https://' . $domain, PHP_URL_HOST));
  53. if($domain === config('pixelfed.domain.app')) {
  54. $this->error('Invalid domain');
  55. return;
  56. }
  57. $confirmed = confirm('Are you sure you want to unblock ' . $domain . '?');
  58. if(!$confirmed) {
  59. return;
  60. }
  61. return $domain;
  62. }
  63. protected function processUnblocks($domain)
  64. {
  65. DefaultDomainBlock::whereDomain($domain)->delete();
  66. progress(
  67. label: 'Updating user domain blocks...',
  68. steps: UserDomainBlock::whereDomain($domain)->lazyById(500),
  69. callback: fn ($domainBlock) => $this->performTask($domainBlock),
  70. );
  71. }
  72. protected function performTask($domainBlock)
  73. {
  74. $domainBlock->deleteQuietly();
  75. }
  76. }