mirror of https://github.com/pixelfed/pixelfed
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.6 KiB
PHTML
110 lines
2.6 KiB
PHTML
|
3 years ago
|
<?php
|
||
|
|
|
||
|
4 weeks ago
|
namespace App\Console\Commands\User;
|
||
|
3 years ago
|
|
||
|
3 years ago
|
use App\Models\DefaultDomainBlock;
|
||
|
4 weeks ago
|
use App\Models\User;
|
||
|
3 years ago
|
use App\Models\UserDomainBlock;
|
||
|
9 months ago
|
use Illuminate\Console\Command;
|
||
|
|
|
||
|
3 years ago
|
use function Laravel\Prompts\confirm;
|
||
|
|
use function Laravel\Prompts\progress;
|
||
|
9 months ago
|
use function Laravel\Prompts\text;
|
||
|
3 years ago
|
|
||
|
|
class AddUserDomainBlock extends Command
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The name and signature of the console command.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $signature = 'app:add-user-domain-block';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The console command description.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $description = 'Apply a domain block to all users';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the console command.
|
||
|
|
*/
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$domain = text('Enter domain you want to block');
|
||
|
3 years ago
|
$domain = strtolower($domain);
|
||
|
3 years ago
|
$domain = $this->validateDomain($domain);
|
||
|
9 months ago
|
if (! $domain) {
|
||
|
3 years ago
|
$this->error('Invalid domain');
|
||
|
9 months ago
|
|
||
|
3 years ago
|
return;
|
||
|
|
}
|
||
|
3 years ago
|
$this->processBlocks($domain);
|
||
|
9 months ago
|
|
||
|
3 years ago
|
}
|
||
|
|
|
||
|
|
protected function validateDomain($domain)
|
||
|
|
{
|
||
|
9 months ago
|
if (! strpos($domain, '.')) {
|
||
|
3 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
9 months ago
|
if (str_starts_with($domain, 'https://')) {
|
||
|
3 years ago
|
$domain = str_replace('https://', '', $domain);
|
||
|
|
}
|
||
|
|
|
||
|
9 months ago
|
if (str_starts_with($domain, 'http://')) {
|
||
|
3 years ago
|
$domain = str_replace('http://', '', $domain);
|
||
|
|
}
|
||
|
|
|
||
|
9 months ago
|
$domain = strtolower(parse_url('https://'.$domain, PHP_URL_HOST));
|
||
|
3 years ago
|
|
||
|
9 months ago
|
$valid = filter_var($domain, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME | FILTER_NULL_ON_FAILURE);
|
||
|
|
if (! $valid) {
|
||
|
3 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
9 months ago
|
if ($domain === config('pixelfed.domain.app')) {
|
||
|
3 years ago
|
$this->error('Invalid domain');
|
||
|
9 months ago
|
|
||
|
3 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
9 months ago
|
$confirmed = confirm('Are you sure you want to block '.$domain.'?');
|
||
|
|
if (! $confirmed) {
|
||
|
3 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $domain;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function processBlocks($domain)
|
||
|
|
{
|
||
|
3 years ago
|
DefaultDomainBlock::updateOrCreate([
|
||
|
9 months ago
|
'domain' => $domain,
|
||
|
3 years ago
|
]);
|
||
|
3 years ago
|
progress(
|
||
|
|
label: 'Updating user domain blocks...',
|
||
|
|
steps: User::lazyById(500),
|
||
|
|
callback: fn ($user) => $this->performTask($user, $domain),
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function performTask($user, $domain)
|
||
|
|
{
|
||
|
9 months ago
|
if (! $user->profile_id || $user->delete_after) {
|
||
|
3 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
9 months ago
|
if ($user->status != null && $user->status != 'disabled') {
|
||
|
3 years ago
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
UserDomainBlock::updateOrCreate([
|
||
|
|
'profile_id' => $user->profile_id,
|
||
|
9 months ago
|
'domain' => $domain,
|
||
|
3 years ago
|
]);
|
||
|
|
}
|
||
|
|
}
|