diff --git a/app/Http/Controllers/FrontIndexController.php b/app/Http/Controllers/FrontIndexController.php index fb75022..96723e5 100644 --- a/app/Http/Controllers/FrontIndexController.php +++ b/app/Http/Controllers/FrontIndexController.php @@ -3,25 +3,29 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; -use App\Models\Source; use App\Repositories\LocationRepository; +use App\Repositories\SourceRepository; class FrontIndexController extends Controller { - protected $locationRepository; + protected $location; + protected $source; - public function __construct(LocationRepository $locationRepository) - { - $this->locationRepository = $locationRepository; + public function __construct( + LocationRepository $locationRepository, + SourceRepository $sourceRepository + ) { + $this->location = $locationRepository; + $this->source = $sourceRepository; } public function start() { return view('front.index', [ - 'count' => count($this->locationRepository->getActiveLocations()), - 'sources' => count(Source::where("active", true)->get()), - 'recent' => $this->locationRepository->getRecent(), - 'latest_date' => $this->locationRepository->getRecent()[0]->updated_at->format('Y M d'), + 'count' => count($this->location->getActiveLocations()), + 'sources' => count($this->source->getActive()), + 'recent' => $this->location->getRecent(), + 'latest_date' => $this->location->getRecent()[0]->updated_at->format('Y M d'), 'title' => "The Bad Space" ]); } @@ -29,19 +33,19 @@ class FrontIndexController extends Controller public function indexSearch(Request $request) { return view('front.index', [ - 'count' => count($this->locationRepository->getActiveLocations()), - 'sources' => count(Source::where("active", true)->get()), - 'recent' => $this->locationRepository->getRecent(), - 'results' => $this->locationRepository->search($request), + 'count' => count($this->location->getActiveLocations()), + 'sources' => count($this->source->getActive()), + 'recent' => $this->location->getRecent(), + 'results' => $this->location->search($request), 'terms' => $request->index_search, - 'latest_date' => $this->locationRepository->getRecent()[0]->updated_at->format('Y M d'), + 'latest_date' => $this->location->getRecent()[0]->updated_at->format('Y M d'), 'title' => "Search Results", ]); } public function about() { - $sources = Source::where("active", true)->get(); + $sources = $this->source->getActive(); return view('front.about', [ 'title' => "ABOUT", 'sources' => $sources @@ -57,8 +61,8 @@ class FrontIndexController extends Controller public function location(string $uuid = "1") { - $location = $this->locationRepository->getLocation($uuid); - $sources = Source::where("active", true)->get(); + $location = $this->location->getLocation($uuid); + $sources = $this->source->getActive(); $name = "NO LOCATION FOUND"; if ($location) { $name = $location->name; @@ -75,11 +79,11 @@ class FrontIndexController extends Controller public function listings(int $pageNum = 1) { - $listing = $this->locationRepository->getPage($pageNum); + $listing = $this->location->getPage($pageNum); return view('front.listing', [ 'title' => "Listings", - 'sources' => count(Source::where("active", true)->get()), + 'sources' => count($this->source->getActive()), "totalPages" => $listing[1], "prev" => $listing[2], "next" => $listing[3], diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 3930631..c3940bf 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -4,9 +4,11 @@ namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Repositories\LocationRepository; +use App\Repositories\SourceRepository; use App\Services\UpdateService; use App\Services\MaintenanceService; use App\Models\Location; +use App\Models\Source; class AppServiceProvider extends ServiceProvider { @@ -19,8 +21,15 @@ class AppServiceProvider extends ServiceProvider return new LocationRepository(new Location()); }); + $this->app->bind(SourceRepository::class, function ($app) { + return new SourceRepository(new Source()); + }); + $this->app->bind(UpdateService::class, function ($app) { - return new UpdateService(new LocationRepository(new Location())); + return new UpdateService( + new LocationRepository(new Location()), + new SourceRepository(new Source()) + ); }); $this->app->bind(MaintenanceService::class, function ($app) { diff --git a/app/Repositories/SourceRepository.php b/app/Repositories/SourceRepository.php new file mode 100644 index 0000000..341b568 --- /dev/null +++ b/app/Repositories/SourceRepository.php @@ -0,0 +1,70 @@ +source = $source; + } + + public function getActive() + { + return $this->source::where("active", true)->get(); + } + + public function updateSourceData() + { + $sources = $this->getActive(); + $missing = []; + $checked = []; + //checks all the sources to refresh data + foreach ($sources as $source) { + $result = []; + if ($source['type'] == 'mastodon') { + if ($source['token'] == null) { + try { + $result = \Mastodon::domain('https://' . $source['url']) + ->get('/instance/domain_blocks'); + array_push($checked, ['source' => $source->url]); + } catch (ConnectException $e) { + array_push($missing, ['source' => $source->url]); + } + } else { + try { + $result = \Mastodon::domain('https://' . $source['url']) + ->token($source['token']) + ->get('/instance/domain_blocks'); + array_push($checked, ['source' => $source->url]); + } catch (ConnectException $e) { + } + } + } 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]); + } + } + + $source->list_data = json_encode($result); + $source->last_updated = Carbon::now(); + $source->save(); + } + return ['checked' => $checked, 'notchecked' => $missing]; + } +} diff --git a/app/Services/UpdateService.php b/app/Services/UpdateService.php index 335c827..95436ef 100644 --- a/app/Services/UpdateService.php +++ b/app/Services/UpdateService.php @@ -4,68 +4,27 @@ namespace App\Services; use App\Models\Location; use App\Repositories\LocationRepository; -use App\Models\Source; +use App\Repositories\SourceRepository; use Ramsey\Uuid\Uuid; -use Carbon\Carbon; -use GuzzleHttp\Exception\ConnectException; class UpdateService { - private $limit = 15; - protected $model; - protected $locationRepository; + protected $location; + protected $source; - public function __construct(LocationRepository $locationRepository) - { - $this->locationRepository = $locationRepository; + public function __construct( + LocationRepository $locationRepository, + SourceRepository $sourceRepository + ) { + $this->location = $locationRepository; + $this->source = $sourceRepository; } public function data() { - $sources = Source::where("active", true)->get(); - $missing = []; - $checked = []; - //checks source url to make sure they valid - foreach ($sources as $source) { - $result = []; - if ($source['type'] == 'mastodon') { - if ($source['token'] == null) { - try { - $result = \Mastodon::domain('https://' . $source['url']) - ->get('/instance/domain_blocks'); - array_push($checked, ['source' => $source->url]); - } catch (ConnectException $e) { - array_push($missing, ['source' => $source->url]); - } - } else { - try { - $result = \Mastodon::domain('https://' . $source['url']) - ->token($source['token']) - ->get('/instance/domain_blocks'); - array_push($checked, ['source' => $source->url]); - } catch (ConnectException $e) { - } - } - } 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]); - } - } - - $source->list_data = json_encode($result); - $source->last_updated = Carbon::now(); - $source->save(); - } - return count($checked) . ' SOURCES UPDATED - ' . count($missing) . ' SOURCES NOT CHECKED'; + $response = $this->source->updateSourceData(); + return count($response['checked']) . ' SOURCES UPDATED - ' . + count($response['notchecked']) . ' SOURCES NOT CHECKED'; } public function list() @@ -74,7 +33,7 @@ class UpdateService $fresh = 0; $unified = []; - $sources = Source::where("active", true)->get(); + $sources = $this->source->getActive(); foreach ($sources as $source) { //$listData = json_decode(); @@ -116,7 +75,7 @@ class UpdateService } foreach ($unified as $item) { - $location = $this->locationRepository->getLocation($item['url']); + $location = $this->location->getLocation($item['url']); if ($location) { ++$duplicates; //update block count for existing item @@ -173,23 +132,4 @@ class UpdateService //TODO: Send update post to TBS social account return $duplicates . ' LOCATIONS UPDATED | ' . $fresh . ' NEW LOCATIONS CREATED'; } - - public function urlExists($url) - { - // Remove all illegal characters from a url - $url = filter_var($url, FILTER_SANITIZE_URL); - // Validate URI - if ( - filter_var($url, FILTER_VALIDATE_URL) === false || // check only for http/https schemes. - !in_array( - strtolower(parse_url($url, PHP_URL_SCHEME)), - ["http", "https"], - true - ) - ) { - return false; - } // Check that URL exists - $file_headers = @get_headers($url); - return !(!$file_headers || $file_headers[0] === "HTTP/1.1 404 Not Found"); - } }