Viewing file: DomainController.php (10 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers\Customer;
use App\Http\Controllers\Controller;
use App\Models\Domain;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use phpseclib3\Crypt\RSA;
class DomainController extends Controller
{
public function index()
{
return view('customer.domain.index');
}
public function getAll()
{
$domains = auth('customer')->user()->domains()->select(['id', 'name', 'dkim_key', 'spf_key','mail_code','dmarc_key','dkim_key_verified','spf_key_verified','text_verified','dmarc_key_verified']);
return datatables()->of($domains)
->addColumn('action', function ($q) {
return '<button class="btn btn-sm btn-dark domain_key_show" data-id='.$q->id.'>Verify</button> '.
'<button class="btn btn-sm btn-danger" data-message="Are you sure you want to delete this domain? This is delete all the senders as well"
data-action=' . route('customer.domain.destroy', [$q]) . '
data-input={"_method":"delete"}
data-toggle="modal" data-target="#modal-confirm">Delete</button>';
})
->addColumn('name', function ($q) {
return $q->name;
})
->addColumn('dkim_key', function ($q) {
return mb_strimwidth($q->dkim_key, 0, 35, '...');
})
->addColumn('spf_key', function ($q) {
return mb_strimwidth($q->spf_key, 0, 35, '...');
})
->addColumn('text', function ($q) {
return mb_strimwidth($q->text, 0, 35, '...');
})
->addColumn('dmarc_key', function ($q) {
return mb_strimwidth($q->dmarc_key, 0, 35, '...');
})
->rawColumns(['action'])
->toJson();
}
public function create(){
return view('customer.domain.create');
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'dkim_public_key' => 'required',
'dkim_private_key' => 'required',
'dkim_signing' => 'required|in:yes,no',
'verified' => 'required|in:yes,no',
]);
$domain = auth('customer')->user()->domains()->create($request->all());
setActivity(auth('customer')->user()->id, 'add', 'domain', $domain->id, 'Sending Domain Created');
return redirect()->route('customer.domain.index')->with('success', 'Sending Domain successfully created');
}
public function domain_keys_show(Request $request)
{
if (!$request->id) {
return response()->json(['status'=>'failed','message' => 'Invalid domain key']);
}
$customer = auth('customer')->user();
$data = $customer->domains()->where('id',$request->id)->first();
return response()->json(['status'=>'success', 'data'=>$data]);
}
public function edit(Domain $domain)
{
$data['domain']= $domain;
return view('customer.domain.edit', $data);
}
public function update(Domain $domain,Request $request)
{
$request->validate([
'name' => 'required',
'dkim_public_key' => 'required',
'dkim_private_key' => 'required',
'dkim_signing' => 'required|in:yes,no',
'verified' => 'required|in:yes,no',
]);
$domain->update($request->all());
setActivity(auth('customer')->user()->id, 'edit', 'domain', $domain->id, 'Sending Domain Updated');
return redirect()->route('customer.domain.index')->with('success', 'Sending Domain successfully updated');
}
public function destroy(Domain $domain)
{
if(auth('customer')->user()->id != $domain->customer_id){
abort(404);
}
$domain->senders()->delete();
$domain->delete();
setActivity(auth('customer')->user()->id, 'delete', 'domain', $domain->id, 'Domain ('.$domain->name.') has been deleted');
return back()->with('success', 'Sending Domain successfully deleted');
}
public function dkimGenerator(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|unique:domains',
]);
if ($validator->fails()) {
return response()->json(['status'=>'failed','message' => 'Invalid domain name']);
}
$domain = $request->name;
if(filter_var(gethostbyname($domain), FILTER_VALIDATE_IP))
{
$configargs = array(
"config"=>public_path('/openssl.cnf'),
'private_key_bits'=> 2048,
'default_md' => "sha256",
);
$res = openssl_pkey_new($configargs);
openssl_pkey_export($res, $privKey,NULL,$configargs);
$pubkey=openssl_pkey_get_details($res);
$pubkey=$pubkey["key"];
$publickey = preg_replace('/^-+.*?-+$/m', '', $pubkey);
$publickey= str_replace("\r", "", $publickey);
$publickey= str_replace("\n", "", $publickey);
$publickey=str_replace(" ", "", $publickey);
$dkim_key = 'v=DKIM1; k=rsa; p='.trim($publickey);
$urlDomainName = $request->domain;
$selector = 'pmail';
$dmarc_key = 'v=DMARC1; p=none; rua=mailto:admin@'.$domain;
$token = Str::random(32);
$mail_code = 'mail-code:'.$token;
$spf_key = 'v=spf1 include:'.$urlDomainName.' mx ~all';
$dnskey = "$selector._domainkey.$domain";
$domain = new Domain();
$domain->customer_id = auth('customer')->user()->id;
$domain->name = $request->name;
$domain->dkim_key = trim($dkim_key);
$domain->dkim_private_key = trim($privKey);
$domain->spf_key = $spf_key;
$domain->dmarc_key= $dmarc_key;
$domain->dnskey = $dnskey;
$domain->mail_code = $mail_code;
$domain->dkim_key_verified = 'no';
$domain->spf_key_verified = 'no';
$domain->text_verified = 'no';
$domain->dmarc_key_verified = 'no';
$domain->save();
return response()->json(['status'=>'success', 'dkim_key'=>trim($dkim_key),'spf_key'=>$spf_key, 'dmarc_key'=>$dmarc_key, 'dnskey'=>$dnskey,'mail_code'=>$mail_code,'id'=>$domain->id]);
}else{
return response()->json(['status'=>'failed','message' => 'Invalid domain name']);
}
}
public function verifyDomain(Request $request){
$selector = $request->domain_key;
$customer = auth('customer')->user();
if(!$selector){
return response()->json(['status'=>'failed', 'message'=>'Key Not Found']);
}
try {
if ($request->name =='dkim_key'){
$data = dns_get_record($selector, DNS_TXT);
if (!$data){
return response()->json(['status'=>'failed','message'=>'Invalid DNS']);
}
$dkim_key = preg_replace('/\s+/','',$request->key);
foreach ($data as $dkim_key_txt){
if (str_replace(' ', '', $dkim_key_txt['txt']) == $dkim_key){
$domain = $customer->domains()->where('id',$request->id)->first();
$domain->dkim_key_verified = 'yes';
$domain->save();
return response()->json(['status'=>'success','message'=>'DNS verify successfully']);
}else{
return response()->json(['status'=>'failed', 'message'=>'Key Not match']);
}
}
}elseif ($request->name =='spf_key' || $request->name =='text' || $request->name =='dmarc_key'){
$data = dns_get_record($selector, DNS_TXT);
if (!$data){
return response()->json(['status'=>'failed','message'=>'Invalid DNS']);
}
$domain_key_record = [];
foreach ($data as $data_record){
$domain_key_record[] = $data_record['txt'];
}
if(in_array($request->key, $domain_key_record)) {
if (str_replace(' ', '',$request->name) =='spf_key'){
$domain = $customer->domains()->where('id',$request->id)->first();
$domain->spf_key_verified = 'yes';
$domain->save();
}elseif (str_replace(' ', '',$request->name) =='text'){
$domain = $customer->domains()->where('id',$request->id)->first();
$domain->text_verified = 'yes';
$domain->save();
}elseif (str_replace(' ', '',$request->name) =='dmarc_key'){
$domain = $customer->domains()->where('id',$request->id)->first();
$domain->dmarc_key_verified = 'yes';
$domain->save();
}
return response()->json(['status'=>'success','message'=>'Key verified successfully']);
}else{
return response()->json(['status'=>'failed','message'=>'Key verification has been failed']);
}
}
}catch (\Exception $ex){
return response()->json(['status'=>'failed', 'message'=>$ex->getMessage()]);
}
}
private function getOnlyCertificate($cert){
$string = str_replace('-----BEGIN PRIVATE KEY-----', '', $cert);
$string = str_replace('-----END PRIVATE KEY-----', '', $string);
$string = str_replace(" ", "", $string);
$string= str_replace("\r", "", $string);
return str_replace("\n", "", $string);
}
}
|