2024-02-19 20:30:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Models\Source;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
|
|
|
|
|
class SourceRepository
|
|
|
|
{
|
|
|
|
protected $source;
|
2024-09-13 03:20:13 +02:00
|
|
|
protected $missing;
|
|
|
|
protected $updated;
|
|
|
|
protected $sources;
|
2024-02-19 20:30:00 +01:00
|
|
|
|
|
|
|
public function __construct(Source $source)
|
|
|
|
{
|
2024-09-13 03:20:13 +02:00
|
|
|
$this->source = $source;
|
|
|
|
$this->missing = [];
|
|
|
|
$this->updated = [];
|
|
|
|
$this->sources = $source::where("active", true)->get();
|
2024-02-19 20:30:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getActive()
|
|
|
|
{
|
|
|
|
return $this->source::where("active", true)->get();
|
|
|
|
}
|
|
|
|
|
2024-09-13 03:20:13 +02:00
|
|
|
public function updateSourceData($index = 0)
|
2024-02-19 20:30:00 +01:00
|
|
|
{
|
|
|
|
//checks all the sources to refresh data
|
2024-09-13 03:20:13 +02:00
|
|
|
$count = count($this->sources);
|
|
|
|
if ($count == 0) {
|
|
|
|
return [
|
|
|
|
'checked' => $this->updated,
|
|
|
|
'notchecked' => $this->missing,
|
|
|
|
'count' => $count,
|
|
|
|
'updated' => 'false',
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
//check index
|
|
|
|
if ($index <= $count - 1) {
|
|
|
|
$source = $this->sources[$index];
|
|
|
|
$result = $this->getMastoBlocklist($source);
|
|
|
|
if (count($result) > 0) {
|
|
|
|
$source->list_data = json_encode($result);
|
|
|
|
$source->last_updated = Carbon::now();
|
|
|
|
$source->save();
|
|
|
|
array_push($this->updated, ['source' => $source->url]);
|
|
|
|
$index++;
|
|
|
|
$result = $this->updateSourceData($index);
|
2024-02-19 20:30:00 +01:00
|
|
|
} else {
|
2024-09-13 03:20:13 +02:00
|
|
|
//if empty run the same index again
|
|
|
|
array_push($this->missing, ['source' => $source->url]);
|
|
|
|
$result = $this->updateSourceData($index);
|
2024-02-19 20:30:00 +01:00
|
|
|
}
|
2024-09-13 03:20:13 +02:00
|
|
|
} else {
|
|
|
|
//continue
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
'checked' => $this->updated,
|
|
|
|
'notchecked' => $this->missing,
|
|
|
|
'count' => $count,
|
|
|
|
'updated' => 'true',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMastoBlocklist($source)
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
if ($source['type'] == 'mastodon') {
|
|
|
|
if ($source['token'] == null) {
|
2024-02-19 20:30:00 +01:00
|
|
|
try {
|
2024-09-13 03:20:13 +02:00
|
|
|
$result = \Mastodon::domain('https://' . $source['url'])
|
|
|
|
->get('/instance/domain_blocks');
|
|
|
|
} catch (ConnectException $e) {
|
2024-09-13 23:12:43 +02:00
|
|
|
//TODO: Logo Errors
|
2024-09-13 03:20:13 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$result = \Mastodon::domain('https://' . $source['url'])
|
|
|
|
->token($source['token'])
|
|
|
|
->get('/instance/domain_blocks');
|
|
|
|
} catch (ConnectException $e) {
|
2024-09-13 23:12:43 +02:00
|
|
|
//TODO: Logo Errors
|
2024-02-19 20:30:00 +01:00
|
|
|
}
|
|
|
|
}
|
2024-09-13 03:20:13 +02:00
|
|
|
} elseif ($source['type'] == 'custom' && $source['format'] == 'csv') {
|
|
|
|
try {
|
|
|
|
$denylist = array_map('str_getcsv', file('https://' . $source['url']));
|
|
|
|
foreach ($denylist as $item) {
|
|
|
|
array_push($result, [
|
|
|
|
'domain' => $item[0],
|
|
|
|
'severity' => $item[1],
|
|
|
|
'comment' => $item[2]]);
|
|
|
|
}
|
|
|
|
array_push($checked, ['source' => $source->url]);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
array_push($missing, ['source' => $source->url]);
|
|
|
|
}
|
2024-02-19 20:30:00 +01:00
|
|
|
}
|
2024-09-13 03:20:13 +02:00
|
|
|
return $result;
|
2024-02-19 20:30:00 +01:00
|
|
|
}
|
|
|
|
}
|