AddUserDomainBlock.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 AddUserDomainBlock extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'app:add-user-domain-block';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Apply a domain block to all users';
  24. /**
  25. * Execute the console command.
  26. */
  27. public function handle()
  28. {
  29. $domain = text('Enter domain you want to block');
  30. $domain = strtolower($domain);
  31. $domain = $this->validateDomain($domain);
  32. $this->processBlocks($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 block ' . $domain . '?');
  58. if(!$confirmed) {
  59. return;
  60. }
  61. return $domain;
  62. }
  63. protected function processBlocks($domain)
  64. {
  65. DefaultDomainBlock::updateOrCreate([
  66. 'domain' => $domain
  67. ]);
  68. progress(
  69. label: 'Updating user domain blocks...',
  70. steps: User::lazyById(500),
  71. callback: fn ($user) => $this->performTask($user, $domain),
  72. );
  73. }
  74. protected function performTask($user, $domain)
  75. {
  76. if(!$user->profile_id || $user->delete_after) {
  77. return;
  78. }
  79. if($user->status != null && $user->status != 'disabled') {
  80. return;
  81. }
  82. UserDomainBlock::updateOrCreate([
  83. 'profile_id' => $user->profile_id,
  84. 'domain' => $domain
  85. ]);
  86. }
  87. }