|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Domain; |
|
6
|
|
|
use App\Http\Requests\DomainAddRequest; |
|
7
|
|
|
use App\Siweocs\Models\DomainAddResponse; |
|
8
|
|
|
use App\Siweocs\Models\DomainListResponse; |
|
9
|
|
|
use App\Siweocs\Models\SiwecosBaseReponse; |
|
10
|
|
|
use App\Token; |
|
11
|
|
|
use Illuminate\Database\QueryException; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
use Keygen\Keygen; |
|
14
|
|
|
|
|
15
|
|
|
class DomainController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param DomainAddRequest $request |
|
19
|
|
|
* |
|
20
|
|
|
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\Response |
|
21
|
|
|
*/ |
|
22
|
|
|
public function add(DomainAddRequest $request) |
|
23
|
|
|
{ |
|
24
|
|
|
$domainFilter = parse_url($request->get('domain')); |
|
25
|
|
|
$domain = $domainFilter['scheme'].'://'.$domainFilter['host']; |
|
26
|
|
|
|
|
27
|
|
|
/** @var Domain $exisitingDomain */ |
|
28
|
|
|
$exisitingDomain = Domain::whereDomain($domain)->first(); |
|
29
|
|
|
if ($exisitingDomain instanceof Domain) { |
|
|
|
|
|
|
30
|
|
|
if ($exisitingDomain->verified === 1) { |
|
|
|
|
|
|
31
|
|
|
return response('Domain already there', 500); |
|
32
|
|
|
} |
|
33
|
|
|
/** @var Token $token */ |
|
34
|
|
|
$token = Token::whereToken($request->header('siwecosToken'))->first(); |
|
35
|
|
|
$exisitingDomain->token_id = $token->id; |
|
36
|
|
|
$exisitingDomain->domain_token = Keygen::alphanum(64)->generate(); |
|
37
|
|
|
|
|
38
|
|
|
try { |
|
39
|
|
|
$exisitingDomain->save(); |
|
40
|
|
|
|
|
41
|
|
|
return response()->json(new DomainAddResponse($exisitingDomain)); |
|
42
|
|
|
} catch (QueryException $queryException) { |
|
43
|
|
|
return response($queryException->getMessage(), 500); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$newDomain = new Domain([ |
|
48
|
|
|
'domain' => $request->json('domain'), |
|
49
|
|
|
'token' => $request->header('siwecosToken'), |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
try { |
|
53
|
|
|
$newDomain->save(); |
|
54
|
|
|
|
|
55
|
|
|
return response()->json(new DomainAddResponse($newDomain)); |
|
56
|
|
|
} catch (QueryException $queryException) { |
|
57
|
|
|
return response($queryException->getMessage(), 500); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function verify(Request $request) |
|
62
|
|
|
{ |
|
63
|
|
|
$token = Token::getTokenByString($request->header('siwecosToken')); |
|
64
|
|
|
$domain = Domain::getDomainOrFail($request->json('domain'), $token->id); |
|
|
|
|
|
|
65
|
|
|
if (!$domain->checkHtmlPage()) { |
|
66
|
|
|
if (!$domain->checkMetatags()) { |
|
67
|
|
|
return response('Page not validate', 417); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return response()->json(new SiwecosBaseReponse('Page successful validated')); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function list(Request $request) |
|
75
|
|
|
{ |
|
76
|
|
|
$token = Token::getTokenByString($request->header('siwecosToken')); |
|
77
|
|
|
$domains = $token->domains()->get(); |
|
78
|
|
|
|
|
79
|
|
|
return response()->json(new DomainListResponse($domains)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function remove(DomainAddRequest $request) |
|
83
|
|
|
{ |
|
84
|
|
|
$domainFilter = parse_url($request->json('domain')); |
|
|
|
|
|
|
85
|
|
|
$domain = $domainFilter['scheme'].'://'.$domainFilter['host']; |
|
86
|
|
|
|
|
87
|
|
|
$token = Token::getTokenByString($request->header('siwecosToken')); |
|
88
|
|
|
$domain = Domain::getDomainOrFail($domain, $token->id); |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
|
|
$domain->delete(); |
|
92
|
|
|
} catch (\Exception $e) { |
|
93
|
|
|
return response($e->getMessage(), 500); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return response()->json(new SiwecosBaseReponse('Domain removed')); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|