2021-05-03 12:53:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2023-11-24 10:49:38 +00:00
|
|
|
use Carbon\Carbon;
|
2021-06-24 10:16:37 +00:00
|
|
|
use Illuminate\Http\Request;
|
2021-06-26 14:34:15 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2021-06-17 14:08:30 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-08-19 13:35:48 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Gate;
|
2021-06-17 14:08:30 +00:00
|
|
|
|
2022-11-13 13:29:55 +00:00
|
|
|
use App\Classes\File;
|
2023-11-24 11:48:01 +00:00
|
|
|
use App\Classes\FTN\{Message,Packet};
|
2023-07-02 13:40:08 +00:00
|
|
|
use App\Http\Requests\SetupRequest;
|
2023-07-19 05:16:25 +00:00
|
|
|
use App\Models\File as FileModel;
|
2023-10-05 11:59:59 +00:00
|
|
|
use App\Models\{Address,Echomail,Netmail,Setup,System};
|
2021-06-14 11:33:18 +00:00
|
|
|
|
2021-05-03 12:53:40 +00:00
|
|
|
class HomeController extends Controller
|
|
|
|
{
|
2021-10-26 12:19:55 +00:00
|
|
|
public function home()
|
|
|
|
{
|
|
|
|
return redirect(Auth::check() ? 'dashboard' : 'about');
|
|
|
|
}
|
|
|
|
|
2023-07-19 05:16:25 +00:00
|
|
|
public function file_contents(System $o,string $date)
|
|
|
|
{
|
|
|
|
$f = FileModel::select('files.*')
|
|
|
|
->distinct()
|
|
|
|
->leftJoin('file_seenby',['file_seenby.file_id'=>'files.id'])
|
|
|
|
->where(function($query) use ($o,$date) {
|
|
|
|
return $query
|
|
|
|
->where('created_at',$date)
|
|
|
|
->whereIn('fftn_id',$o->addresses->pluck('id'));
|
|
|
|
})
|
|
|
|
->Orwhere(function($query) use ($o,$date) {
|
|
|
|
return $query
|
|
|
|
->where('sent_at',$date)
|
|
|
|
->whereIn('address_id',$o->addresses->pluck('id'));
|
|
|
|
})
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return view('file')
|
|
|
|
->with('f',$f);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function packet_contents(System $o,string $packet)
|
2023-07-15 14:41:36 +00:00
|
|
|
{
|
2023-11-23 10:55:39 +00:00
|
|
|
$nm = Netmail::select(['netmails.id','fftn_id','tftn_id','netmails.datetime'])
|
2023-07-15 14:41:36 +00:00
|
|
|
->leftJoin('netmail_path',['netmail_path.netmail_id'=>'netmails.id'])
|
2023-07-19 05:16:25 +00:00
|
|
|
->where(function($query) use ($o,$packet) {
|
2023-07-15 14:41:36 +00:00
|
|
|
return $query
|
|
|
|
->where('sent_pkt',$packet)
|
|
|
|
->orWhere('netmail_path.recv_pkt',$packet);
|
|
|
|
})
|
|
|
|
->get();
|
|
|
|
|
2023-11-23 10:55:39 +00:00
|
|
|
$em = Echomail::select(['echomails.id','fftn_id','echoarea_id','msgid','datetime'])
|
2023-07-15 14:41:36 +00:00
|
|
|
->leftJoin('echomail_seenby',['echomail_seenby.echomail_id'=>'echomails.id'])
|
2023-07-19 05:16:25 +00:00
|
|
|
->where(function($query) use ($o,$packet) {
|
2023-07-15 14:41:36 +00:00
|
|
|
return $query
|
|
|
|
->where('sent_pkt',$packet)
|
2023-07-19 05:16:25 +00:00
|
|
|
->whereIn('echomail_seenby.address_id',$o->addresses->pluck('id'));
|
2023-11-23 10:55:39 +00:00
|
|
|
})
|
|
|
|
->union(
|
|
|
|
Echomail::select(['echomails.id','fftn_id','echoarea_id','msgid','datetime'])
|
|
|
|
->leftJoin('echomail_path',['echomail_path.echomail_id'=>'echomails.id'])
|
|
|
|
->where(function($query) use ($o,$packet) {
|
|
|
|
return $query
|
|
|
|
->where('recv_pkt',$packet)
|
|
|
|
->whereIn('echomail_path.address_id',$o->addresses->pluck('id'));
|
|
|
|
})
|
|
|
|
)
|
|
|
|
->with(['echoarea'])
|
2023-07-15 14:41:36 +00:00
|
|
|
->get();
|
|
|
|
|
|
|
|
return view('packet')
|
|
|
|
->with('nm',$nm)
|
|
|
|
->with('em',$em);
|
|
|
|
}
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
/**
|
|
|
|
* Render a view that summarises the users permissions
|
|
|
|
*/
|
|
|
|
public function permissions()
|
|
|
|
{
|
|
|
|
return view('auth.permissions')
|
|
|
|
->with('user',Auth::user());
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:19:50 +00:00
|
|
|
/**
|
|
|
|
* Show a packet dump
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2021-06-29 10:43:29 +00:00
|
|
|
public function pkt(Request $request)
|
|
|
|
{
|
2022-11-13 13:29:55 +00:00
|
|
|
$pkt = collect();
|
2021-06-29 10:43:29 +00:00
|
|
|
$file = NULL;
|
2022-11-13 13:29:55 +00:00
|
|
|
$f = NULL;
|
2021-06-29 10:43:29 +00:00
|
|
|
|
|
|
|
if ($request->post()) {
|
|
|
|
$request->validate([
|
|
|
|
'file' => 'required|filled|min:1',
|
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($request->allFiles() as $key => $filegroup) {
|
|
|
|
if ($key !== 'file')
|
|
|
|
continue;
|
2022-12-03 05:00:38 +00:00
|
|
|
|
2021-06-29 10:43:29 +00:00
|
|
|
foreach ($filegroup as $file) {
|
2021-06-29 13:42:35 +00:00
|
|
|
try {
|
2022-11-13 13:29:55 +00:00
|
|
|
$f = new File($file);
|
|
|
|
|
|
|
|
foreach ($f as $packet)
|
|
|
|
$pkt->push([$f->itemName()=>Packet::process($packet,$f->itemName(),$f->itemSize())]);
|
2021-06-29 13:42:35 +00:00
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
2021-07-15 14:54:23 +00:00
|
|
|
return redirect()->back()->withErrors(sprintf('%s (%s:%d)',$e->getMessage(),$e->getFile(),$e->getLine()));
|
2021-06-29 13:42:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 10:43:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-08 02:01:10 +00:00
|
|
|
return view('pkt')
|
2022-11-13 13:29:55 +00:00
|
|
|
->with('file',$f)
|
|
|
|
->with('filename',$f ? $file->getClientOriginalName() : NULL)
|
|
|
|
->with('results',$pkt)
|
2021-08-20 14:33:41 +00:00
|
|
|
->with('hexdump',$file ? hex_dump(file_get_contents($file)) : '');
|
2021-06-29 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 13:19:50 +00:00
|
|
|
/**
|
|
|
|
* Process searching
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2021-06-26 14:34:15 +00:00
|
|
|
public function search(Request $request): Collection
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
|
|
|
|
$result = collect();
|
|
|
|
|
|
|
|
list($zone_id,$host_id,$node_id,$point_id,$domain) = sscanf($request->query('term'),'%d:%d/%d.%d@%s');
|
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
// Look for Systems
|
2021-07-02 13:19:50 +00:00
|
|
|
foreach (Address::select(['systems.name',DB::raw('systems.id AS system_id'),'zones.zone_id','region_id','host_id','node_id','point_id'])
|
2021-06-26 14:34:15 +00:00
|
|
|
->join('zones',['zones.id'=>'addresses.zone_id'])
|
2021-07-02 13:19:50 +00:00
|
|
|
->rightjoin('systems',['systems.id'=>'addresses.system_id'])
|
|
|
|
->when($zone_id || $host_id || $node_id,function($query) use ($zone_id,$host_id,$node_id) {
|
|
|
|
return $query
|
2021-07-17 07:15:00 +00:00
|
|
|
->when($zone_id,function($q,$zone_id) { return $q->where('zones.zone_id',$zone_id); })
|
2021-07-02 13:19:50 +00:00
|
|
|
->where(function($q) use ($host_id) {
|
|
|
|
return $q
|
2021-07-17 07:15:00 +00:00
|
|
|
->when($host_id,function($q,$host_id) { return $q->where('region_id',$host_id); })
|
|
|
|
->when($host_id,function($q,$host_id) { return $q->orWhere('host_id',$host_id); });
|
2021-07-02 13:19:50 +00:00
|
|
|
})
|
2022-01-20 06:51:40 +00:00
|
|
|
->when($node_id,function($q,$node_id) { return $q->where('node_id',$node_id); });
|
2021-07-02 13:19:50 +00:00
|
|
|
})
|
2021-06-26 14:34:15 +00:00
|
|
|
->orWhere('systems.name','ilike','%'.$request->query('term').'%')
|
|
|
|
->orWhere('systems.sysop','ilike','%'.$request->query('term').'%')
|
|
|
|
->OrderBy('systems.name')
|
|
|
|
->get() as $o)
|
|
|
|
{
|
2021-07-02 13:19:50 +00:00
|
|
|
$ftn = NULL;
|
|
|
|
|
|
|
|
if ($o->zone_id && ($o->region_id||$o->host_id) && is_numeric($o->node_id) && is_numeric($o->point_id))
|
|
|
|
$ftn = sprintf('%d:%d/%d.%d',$o->zone_id,$o->host_id ?: $o->region_id,$o->node_id,$o->point_id);
|
|
|
|
|
2023-07-26 09:44:25 +00:00
|
|
|
$result->push(['id'=>$o->system_id,'name'=>htmlspecialchars($o->name).($ftn ? ' '.$ftn : ''),'value'=>url('system/view',[$o->system_id]),'category'=>'Systems']);
|
2021-06-26 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
// Look for Echomail
|
2021-08-29 01:48:27 +00:00
|
|
|
foreach (Echomail::select(['id','fftn_id','from'])
|
|
|
|
->where('msgid','like','%'.$request->query('term').'%')
|
2022-01-01 05:59:35 +00:00
|
|
|
->orWhere('replyid','like','%'.$request->query('term').'%')
|
2021-08-29 01:48:27 +00:00
|
|
|
->get() as $o)
|
|
|
|
{
|
|
|
|
$result->push(['id'=>$o->id,'name'=>sprintf('%s (%s)',$o->from,$o->fftn->ftn3d),'value'=>url('echomail/view',[$o->id]),'category'=>'Echomail']);
|
|
|
|
}
|
|
|
|
|
2023-06-18 13:33:26 +00:00
|
|
|
// Look for Netmail
|
|
|
|
if (Gate::check('admin'))
|
|
|
|
foreach (Netmail::select(['id','fftn_id','from'])
|
|
|
|
->where('msgid','like','%'.$request->query('term').'%')
|
|
|
|
->orWhere('replyid','like','%'.$request->query('term').'%')
|
|
|
|
->get() as $o)
|
|
|
|
{
|
|
|
|
$result->push(['id'=>$o->id,'name'=>sprintf('%s (%s)',$o->from,$o->fftn->ftn3d),'value'=>url('netmail/view',[$o->id]),'category'=>'Netmail']);
|
|
|
|
}
|
|
|
|
|
2021-07-02 13:19:50 +00:00
|
|
|
return $result->unique(['id'])->take(10)->values();
|
2021-06-26 14:34:15 +00:00
|
|
|
}
|
|
|
|
|
2021-06-15 12:19:14 +00:00
|
|
|
/**
|
|
|
|
* System Setup
|
|
|
|
*
|
|
|
|
* @note: Protected by Route
|
|
|
|
*/
|
2023-07-02 13:40:08 +00:00
|
|
|
public function setup(SetupRequest $request)
|
2021-06-15 12:19:14 +00:00
|
|
|
{
|
2021-06-24 10:16:37 +00:00
|
|
|
$o = Setup::findOrNew(config('app.id'));
|
|
|
|
|
|
|
|
if ($request->post()) {
|
2023-07-05 12:42:59 +00:00
|
|
|
if (! $o->exists)
|
2021-06-24 10:16:37 +00:00
|
|
|
$o->id = config('app.id');
|
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
$servers = collect();
|
2023-07-05 12:42:59 +00:00
|
|
|
$options = collect();
|
|
|
|
|
|
|
|
$x = collect();
|
|
|
|
$x->put('options',collect($request->post('binkp'))->sum());
|
|
|
|
$x->put('port',$request->post('binkp_port'));
|
|
|
|
$x->put('bind',$request->post('binkp_bind'));
|
|
|
|
$x->put('active',(bool)$request->post('binkp_active'));
|
|
|
|
$servers->put('binkp',$x);
|
|
|
|
|
|
|
|
$x = collect();
|
|
|
|
$x->put('options',collect($request->post('emsi'))->sum());
|
|
|
|
$x->put('port',$request->post('emsi_port'));
|
|
|
|
$x->put('bind',$request->post('emsi_bind'));
|
|
|
|
$x->put('active',(bool)$request->post('emsi_active'));
|
|
|
|
$servers->put('emsi',$x);
|
|
|
|
|
|
|
|
$x = collect();
|
|
|
|
$x->put('options',collect($request->post('dns'))->sum());
|
|
|
|
$x->put('port',$request->post('dns_port'));
|
|
|
|
$x->put('bind',$request->post('dns_bind'));
|
|
|
|
$x->put('active',(bool)$request->post('dns_active'));
|
|
|
|
$servers->put('dns',$x);
|
|
|
|
|
|
|
|
$options->put('options',collect($request->post('options'))->sum());
|
|
|
|
$options->put('msgs_pkt',$request->post('msgs_pkt'));
|
2023-07-02 13:40:08 +00:00
|
|
|
|
|
|
|
$o->servers = $servers;
|
2023-07-05 12:42:59 +00:00
|
|
|
$o->options = $options;
|
|
|
|
$o->system_id = $request->post('system_id');
|
2021-06-24 10:16:37 +00:00
|
|
|
$o->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('setup')
|
|
|
|
->with('o',$o);
|
2021-06-15 12:19:14 +00:00
|
|
|
}
|
2023-11-24 10:49:38 +00:00
|
|
|
|
|
|
|
public function status()
|
|
|
|
{
|
|
|
|
$date = Carbon::now()->yesterday()->endOfday();
|
|
|
|
|
|
|
|
// Nodes with uncollected mail
|
|
|
|
$e = Address::select(['addresses.id',DB::raw('count(*) as e'),DB::raw('0 as n'),DB::raw('0 as f')])
|
|
|
|
->join('echomail_seenby',['echomail_seenby.address_id'=>'addresses.id'])
|
|
|
|
->join('echomails',['echomails.id'=>'echomail_seenby.echomail_id'])
|
|
|
|
->whereNotNull('export_at')
|
|
|
|
->whereNull('sent_at')
|
|
|
|
->where('datetime','<',$date)
|
|
|
|
->groupBy('addresses.id');
|
|
|
|
|
|
|
|
$n = Address::select(['addresses.id',DB::raw('0 as e'),DB::raw('count(*) as n'),DB::raw('0 as f')])
|
|
|
|
->join('netmails',['netmails.tftn_id'=>'addresses.id'])
|
2023-11-24 11:48:01 +00:00
|
|
|
->where(function($query) {
|
|
|
|
return $query->whereRaw(sprintf('(flags & %d) > 0',Message::FLAG_INTRANSIT))
|
|
|
|
->orWhereRaw(sprintf('(flags & %d) > 0',Message::FLAG_LOCAL));
|
|
|
|
})
|
|
|
|
->whereRaw(sprintf('(flags & %d) = 0',Message::FLAG_SENT))
|
2023-11-24 10:49:38 +00:00
|
|
|
->where('datetime','<',$date)
|
|
|
|
->groupBy('addresses.id');
|
|
|
|
|
|
|
|
$f = Address::select(['addresses.id',DB::raw('0 as e'),DB::raw('0 as n'),DB::raw('count(*) as f')])
|
|
|
|
->join('file_seenby',['file_seenby.address_id'=>'addresses.id'])
|
|
|
|
->join('files',['files.id'=>'file_seenby.file_id'])
|
|
|
|
->whereNotNull('export_at')
|
|
|
|
->whereNull('sent_at')
|
|
|
|
->where('datetime','<',$date)
|
|
|
|
->groupBy('addresses.id');
|
|
|
|
|
|
|
|
$r = Address::select([
|
|
|
|
'a.id',
|
2023-11-24 11:26:31 +00:00
|
|
|
'addresses.system_id',
|
|
|
|
'addresses.zone_id',
|
|
|
|
'addresses.host_id',
|
|
|
|
'addresses.node_id',
|
2023-11-24 10:49:38 +00:00
|
|
|
DB::raw('sum(a.e) as e'),
|
|
|
|
DB::raw('sum(a.n) as n'),
|
|
|
|
DB::raw('sum(a.f) as f')
|
|
|
|
])
|
|
|
|
->from($e->union($n)->union($f),'a')
|
2023-11-24 11:26:31 +00:00
|
|
|
->where('addresses.active',TRUE)
|
|
|
|
->where('zones.active',TRUE)
|
|
|
|
->where('domains.active',TRUE)
|
2023-11-24 10:49:38 +00:00
|
|
|
->join('addresses',['addresses.id'=>'a.id'])
|
2023-11-24 11:26:31 +00:00
|
|
|
->join('zones',['zones.id'=>'addresses.zone_id'])
|
|
|
|
->join('domains',['domains.id'=>'zones.domain_id'])
|
|
|
|
->groupBy('addresses.system_id','a.id','addresses.zone_id','addresses.host_id','addresses.node_id')
|
2023-11-24 10:49:38 +00:00
|
|
|
->with(['system','zone.domain']);
|
|
|
|
|
|
|
|
return view('status')
|
|
|
|
->with('date',$date)
|
|
|
|
->with('uncollected',$r->get());
|
|
|
|
}
|
2021-05-03 12:53:40 +00:00
|
|
|
}
|