1
0

DeleteUserDomainBlock.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. if(!$domain || empty($domain)) {
  33. $this->error('Invalid domain');
  34. return;
  35. }
  36. $this->processUnblocks($domain);
  37. return;
  38. }
  39. protected function validateDomain($domain)
  40. {
  41. if(!strpos($domain, '.')) {
  42. return;
  43. }
  44. if(str_starts_with($domain, 'https://')) {
  45. $domain = str_replace('https://', '', $domain);
  46. }
  47. if(str_starts_with($domain, 'http://')) {
  48. $domain = str_replace('http://', '', $domain);
  49. }
  50. $domain = strtolower(parse_url('https://' . $domain, PHP_URL_HOST));
  51. $valid = filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME|FILTER_NULL_ON_FAILURE);
  52. if(!$valid) {
  53. return;
  54. }
  55. if($domain === config('pixelfed.domain.app')) {
  56. return;
  57. }
  58. $confirmed = confirm('Are you sure you want to unblock ' . $domain . '?');
  59. if(!$confirmed) {
  60. return;
  61. }
  62. return $domain;
  63. }
  64. protected function processUnblocks($domain)
  65. {
  66. DefaultDomainBlock::whereDomain($domain)->delete();
  67. if(!UserDomainBlock::whereDomain($domain)->count()) {
  68. $this->info('No results found!');
  69. return;
  70. }
  71. progress(
  72. label: 'Updating user domain blocks...',
  73. steps: UserDomainBlock::whereDomain($domain)->lazyById(500),
  74. callback: fn ($domainBlock) => $this->performTask($domainBlock),
  75. );
  76. }
  77. protected function performTask($domainBlock)
  78. {
  79. $domainBlock->deleteQuietly();
  80. }
  81. }