2019-01-20 23:26:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use DB, Cache;
|
|
|
|
use App\{Instance, Profile};
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Http\Request;
|
2019-01-21 04:25:52 +00:00
|
|
|
use Illuminate\Validation\Rule;
|
2019-01-20 23:26:03 +00:00
|
|
|
|
|
|
|
trait AdminInstanceController
|
|
|
|
{
|
|
|
|
|
|
|
|
public function instances(Request $request)
|
|
|
|
{
|
2019-01-21 04:25:52 +00:00
|
|
|
$this->validate($request, [
|
2021-04-21 04:03:41 +00:00
|
|
|
|
2019-01-21 04:25:52 +00:00
|
|
|
'filter' => [
|
|
|
|
'nullable',
|
|
|
|
'string',
|
|
|
|
'min:1',
|
|
|
|
'max:20',
|
2021-04-21 04:03:41 +00:00
|
|
|
Rule::in([
|
|
|
|
'cw',
|
|
|
|
'unlisted',
|
|
|
|
'banned',
|
|
|
|
// 'popular',
|
|
|
|
'new',
|
|
|
|
'all'
|
|
|
|
])
|
2019-01-21 04:25:52 +00:00
|
|
|
],
|
|
|
|
]);
|
2021-04-21 04:03:41 +00:00
|
|
|
if($request->has('q') && $request->filled('q')) {
|
|
|
|
$instances = Instance::where('domain', 'like', '%' . $request->input('q') . '%')->simplePaginate(10);
|
|
|
|
} else if($request->has('filter') && $request->filled('filter')) {
|
2019-01-21 04:25:52 +00:00
|
|
|
switch ($request->filter) {
|
2021-04-21 04:03:41 +00:00
|
|
|
case 'cw':
|
|
|
|
$instances = Instance::select('id', 'domain', 'unlisted', 'auto_cw', 'banned')->whereAutoCw(true)->orderByDesc('id')->simplePaginate(10);
|
2019-01-21 04:25:52 +00:00
|
|
|
break;
|
|
|
|
case 'unlisted':
|
2021-04-21 04:03:41 +00:00
|
|
|
$instances = Instance::select('id', 'domain', 'unlisted', 'auto_cw', 'banned')->whereUnlisted(true)->orderByDesc('id')->simplePaginate(10);
|
2019-01-21 04:25:52 +00:00
|
|
|
break;
|
|
|
|
case 'banned':
|
2021-04-21 04:03:41 +00:00
|
|
|
$instances = Instance::select('id', 'domain', 'unlisted', 'auto_cw', 'banned')->whereBanned(true)->orderByDesc('id')->simplePaginate(10);
|
|
|
|
break;
|
|
|
|
case 'new':
|
|
|
|
$instances = Instance::select('id', 'domain', 'unlisted', 'auto_cw', 'banned')->latest()->simplePaginate(10);
|
2019-01-21 04:25:52 +00:00
|
|
|
break;
|
2021-04-21 04:03:41 +00:00
|
|
|
// case 'popular':
|
|
|
|
// $popular = Profile::selectRaw('*, count(domain) as count')
|
|
|
|
// ->whereNotNull('domain')
|
|
|
|
// ->groupBy('domain')
|
|
|
|
// ->orderByDesc('count')
|
|
|
|
// ->take(10)
|
|
|
|
// ->get()
|
|
|
|
// ->pluck('domain')
|
|
|
|
// ->toArray();
|
|
|
|
// $instances = Instance::whereIn('domain', $popular)->simplePaginate(10);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$instances = Instance::select('id', 'domain', 'unlisted', 'auto_cw', 'banned')->orderByDesc('id')->simplePaginate(10);
|
|
|
|
break;
|
2019-01-21 04:25:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-21 04:03:41 +00:00
|
|
|
$instances = Instance::select('id', 'domain', 'unlisted', 'auto_cw', 'banned')->orderByDesc('id')->simplePaginate(10);
|
2019-01-21 04:25:52 +00:00
|
|
|
}
|
2021-04-21 04:03:41 +00:00
|
|
|
|
2019-01-20 23:26:03 +00:00
|
|
|
return view('admin.instances.home', compact('instances'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function instanceScan(Request $request)
|
|
|
|
{
|
2019-10-15 03:31:57 +00:00
|
|
|
Profile::whereNotNull('domain')
|
|
|
|
->latest()
|
2019-10-17 03:08:46 +00:00
|
|
|
->groupBy(['domain', 'id'])
|
2019-10-15 03:31:57 +00:00
|
|
|
->where('created_at', '>', now()->subMonths(2))
|
|
|
|
->chunk(100, function($domains) {
|
|
|
|
foreach($domains as $domain) {
|
|
|
|
Instance::firstOrCreate([
|
|
|
|
'domain' => $domain->domain
|
|
|
|
]);
|
|
|
|
}
|
2019-01-20 23:26:03 +00:00
|
|
|
});
|
2019-10-15 03:31:57 +00:00
|
|
|
|
2019-01-20 23:26:03 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2019-01-21 04:25:52 +00:00
|
|
|
public function instanceShow(Request $request, $id)
|
|
|
|
{
|
|
|
|
$instance = Instance::findOrFail($id);
|
|
|
|
return view('admin.instances.show', compact('instance'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function instanceEdit(Request $request, $id)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'action' => [
|
|
|
|
'required',
|
|
|
|
'string',
|
|
|
|
'min:1',
|
|
|
|
'max:20',
|
|
|
|
Rule::in(['autocw', 'unlist', 'ban'])
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$instance = Instance::findOrFail($id);
|
|
|
|
$unlisted = $instance->unlisted;
|
|
|
|
$autocw = $instance->auto_cw;
|
|
|
|
$banned = $instance->banned;
|
|
|
|
|
|
|
|
switch ($request->action) {
|
|
|
|
case 'autocw':
|
|
|
|
$instance->auto_cw = $autocw == true ? false : true;
|
|
|
|
$instance->save();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'unlist':
|
|
|
|
$instance->unlisted = $unlisted == true ? false : true;
|
|
|
|
$instance->save();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'ban':
|
|
|
|
$instance->banned = $banned == true ? false : true;
|
|
|
|
$instance->save();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-04-21 04:31:11 +00:00
|
|
|
Cache::forget('instances:banned:domains');
|
|
|
|
|
2019-01-21 04:25:52 +00:00
|
|
|
return response()->json([]);
|
|
|
|
}
|
2021-04-21 04:03:41 +00:00
|
|
|
}
|