1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers;
|
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller;
|
6
|
|
|
use Illuminate\Http\Request;
|
7
|
|
|
use App\Hostkeys;
|
8
|
|
|
use That0n3guy\Transliteration;
|
9
|
|
|
|
10
|
|
|
use GuzzleHttp\Exception\GuzzleException;
|
11
|
|
|
use GuzzleHttp\Client;
|
12
|
|
|
use GuzzleHttp\Pool;
|
13
|
|
|
use GuzzleHttp\Psr7;
|
14
|
|
|
use Auth;
|
15
|
|
|
|
16
|
|
|
use Validator, Image, Session, File, Response, Redirect, Exception;
|
17
|
|
|
|
18
|
|
|
class HostkeysController extends Controller
|
19
|
|
|
{
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Display a listing of the resource.
|
23
|
|
|
*
|
24
|
|
|
* @return Response
|
25
|
|
|
*/
|
26
|
|
|
public function index(Request $request)
|
27
|
|
|
{
|
28
|
|
|
try {
|
29
|
|
|
if($request->get('search') != ''){
|
30
|
|
|
$data['data'] = Hostkeys::with('getState')
|
|
|
|
|
31
|
|
|
->with('getTransition')
|
32
|
|
|
->with('getUserName')
|
33
|
|
|
->where('hostname', 'like', '%'.$request->get('search').'%')
|
34
|
|
|
->orderBy('id', 'desc')
|
35
|
|
|
->paginate(env('PAGINATE', 10));
|
36
|
|
|
} else{
|
37
|
|
|
$data['data'] = Hostkeys::with('getState')
|
|
|
|
|
38
|
|
|
->with('getTransition')
|
39
|
|
|
->with('getUserName')
|
40
|
|
|
->orderBy('id', 'desc')
|
41
|
|
|
->paginate(env('PAGINATE', 10));
|
42
|
|
|
}
|
43
|
|
|
} catch (Exception $e) {
|
44
|
|
|
$data['data'] = [];
|
|
|
|
|
45
|
|
|
}
|
46
|
|
|
return view('host_keys.index', $data);
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* Show the form for creating a new resource.
|
51
|
|
|
*
|
52
|
|
|
* @return Response
|
53
|
|
|
*/
|
54
|
|
|
public function create()
|
55
|
|
|
{
|
56
|
|
|
|
57
|
|
|
}
|
58
|
|
|
|
59
|
|
|
/**
|
60
|
|
|
* Store a newly created resource in storage.
|
61
|
|
|
*
|
62
|
|
|
* @return Response
|
63
|
|
|
*/
|
64
|
|
View Code Duplication |
public function store(Request $request)
|
|
|
|
|
65
|
|
|
{
|
66
|
|
|
try {
|
67
|
|
|
$token = $request->keys;
|
68
|
|
|
$hostkey = New Hostkeys;
|
69
|
|
|
$hostkey->hostname = str_replace(array('https://', 'http://'), array('',''),$request->hostname);
|
70
|
|
|
$hostkey->keys = $token;
|
71
|
|
|
$hostkey->state = $request->state;
|
72
|
|
|
$hostkey->transition = $request->transition;
|
73
|
|
|
$hostkey->user_id = $request->user_id;
|
74
|
|
|
if($request->state == 'Approved' || $request->state == 'Rejected'){
|
75
|
|
|
$hostkey->save();
|
76
|
|
|
|
77
|
|
|
$error = false;
|
78
|
|
|
$statusCode = 200;
|
79
|
|
|
$title = 'Success';
|
80
|
|
|
$type = 'success';
|
81
|
|
|
$message = 'Data Saved Successfuly.';
|
82
|
|
|
$result = $hostkey;
|
83
|
|
|
}else {
|
84
|
|
|
$error = true;
|
85
|
|
|
$statusCode = 404;
|
86
|
|
|
$title = 'Error';
|
87
|
|
|
$type = 'error';
|
88
|
|
|
$message = 'Error';
|
89
|
|
|
$result = 'Not Found';
|
90
|
|
|
}
|
91
|
|
|
} catch (Exception $e) {
|
92
|
|
|
$error = true;
|
93
|
|
|
$statusCode = 404;
|
94
|
|
|
$title = 'Error';
|
95
|
|
|
$type = 'error';
|
96
|
|
|
$message = 'Error';
|
97
|
|
|
$result = 'Not Found';
|
98
|
|
|
} finally {
|
99
|
|
|
return Response::json(array(
|
100
|
|
|
'error' => $error,
|
|
|
|
|
101
|
|
|
'status' => $statusCode,
|
|
|
|
|
102
|
|
|
'title' => $title,
|
|
|
|
|
103
|
|
|
'type' => $type,
|
|
|
|
|
104
|
|
|
'message' => $message,
|
|
|
|
|
105
|
|
|
'result' => $result
|
|
|
|
|
106
|
|
|
));
|
107
|
|
|
}
|
108
|
|
|
}
|
109
|
|
|
|
110
|
|
|
/**
|
111
|
|
|
* Display the specified resource.
|
112
|
|
|
*
|
113
|
|
|
* @param int $id
|
114
|
|
|
* @return Response
|
115
|
|
|
*/
|
116
|
|
|
public function show($id)
|
117
|
|
|
{
|
118
|
|
|
|
119
|
|
|
}
|
120
|
|
|
|
121
|
|
|
public function get($hostname)
|
122
|
|
|
{
|
123
|
|
|
try {
|
124
|
|
|
$data = Hostkeys::with('getState')
|
125
|
|
|
->with('getTransition')
|
126
|
|
|
->with('getUserName')
|
127
|
|
|
->where('hostname', 'like', '%'.$hostname.'%')
|
128
|
|
|
->orderBy('id', 'desc')
|
129
|
|
|
->first();
|
130
|
|
|
|
131
|
|
|
$error = false;
|
132
|
|
|
$statusCode = 200;
|
133
|
|
|
$title = 'Success';
|
134
|
|
|
$type = 'success';
|
135
|
|
|
$message = 'Success';
|
136
|
|
|
$result = $data->hostname;
|
137
|
|
|
$resultid = $data->id;
|
138
|
|
|
} catch (Exception $e) {
|
139
|
|
|
$error = true;
|
140
|
|
|
$statusCode = 404;
|
141
|
|
|
$title = 'Error';
|
142
|
|
|
$type = 'error';
|
143
|
|
|
$message = 'Error';
|
144
|
|
|
$result = 'Not Found';
|
145
|
|
|
$resultid = 'Not Found';
|
146
|
|
|
} finally {
|
147
|
|
|
return Response::json(array(
|
148
|
|
|
'error' => $error,
|
|
|
|
|
149
|
|
|
'status' => $statusCode,
|
|
|
|
|
150
|
|
|
'title' => $title,
|
|
|
|
|
151
|
|
|
'type' => $type,
|
|
|
|
|
152
|
|
|
'message' => $message,
|
|
|
|
|
153
|
|
|
'result' => $result,
|
|
|
|
|
154
|
|
|
'id' => $resultid
|
|
|
|
|
155
|
|
|
));
|
156
|
|
|
}
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
public function request(Request $request){
|
160
|
|
|
if(Auth::guest()){ $current_user = 1; }
|
161
|
|
|
else{ $current_user = Auth::user()->id; }
|
162
|
|
|
$headers = ['Content-Type' => 'application/json'];
|
163
|
|
|
$data = [
|
164
|
|
|
'client' => $request->client,
|
165
|
|
|
'request' => $request->requests,
|
|
|
|
|
166
|
|
|
'user_id' => $current_user
|
167
|
|
|
];
|
168
|
|
|
$body = json_encode($data);
|
169
|
|
|
|
170
|
|
|
//kalo udah rilis
|
171
|
|
|
$url = "https://dashboard-01.dev.bantenprov.go.id/api/v1/api-manager/request";
|
172
|
|
|
|
173
|
|
|
//untuk local
|
174
|
|
|
// $url = "dashboard.local/api/v1/api-manager/receive";
|
|
|
|
|
175
|
|
|
|
176
|
|
|
$client = new \GuzzleHttp\Client();
|
177
|
|
|
$res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]);
|
178
|
|
|
$response = $res->getBody();
|
179
|
|
|
$responses = json_decode($response);
|
180
|
|
|
|
181
|
|
|
$hostkey = New Hostkeys;
|
182
|
|
|
$hostkey->hostname = str_replace(array('https://', 'http://'), array('',''),$responses->result->client);
|
183
|
|
|
$hostkey->keys = "";
|
184
|
|
|
$hostkey->state = $responses->result->request;
|
185
|
|
|
$hostkey->transition = "Propose To ".$responses->result->request;
|
186
|
|
|
$hostkey->user_id = $responses->result->user_id;
|
187
|
|
|
if($responses->status == 200){
|
188
|
|
|
$hostkey->save();
|
189
|
|
|
|
190
|
|
|
Session::flash('message', 'Send Request Api Keys Successfuly');
|
191
|
|
|
}else {
|
192
|
|
|
Session::flash('message', $responses->message);
|
193
|
|
|
}
|
194
|
|
|
|
195
|
|
|
return Redirect::to('host-keys');
|
196
|
|
|
}
|
197
|
|
|
|
198
|
|
|
/**
|
199
|
|
|
* Show the form for editing the specified resource.
|
200
|
|
|
*
|
201
|
|
|
* @param int $id
|
202
|
|
|
* @return Response
|
203
|
|
|
*/
|
204
|
|
|
public function edit($id)
|
205
|
|
|
{
|
206
|
|
|
|
207
|
|
|
}
|
208
|
|
|
|
209
|
|
|
/**
|
210
|
|
|
* Update the specified resource in storage.
|
211
|
|
|
*
|
212
|
|
|
* @param int $id
|
213
|
|
|
* @return Response
|
214
|
|
|
*/
|
215
|
|
View Code Duplication |
public function update(Request $request, $id)
|
|
|
|
|
216
|
|
|
{
|
217
|
|
|
try {
|
218
|
|
|
$token = $request->keys;
|
219
|
|
|
$hostkey = Hostkeys::findOrFail($id);
|
220
|
|
|
$hostkey->hostname = str_replace(array('https://', 'http://'), array('',''),$request->hostname);
|
221
|
|
|
$hostkey->keys = $token;
|
222
|
|
|
$hostkey->state = $request->state;
|
223
|
|
|
$hostkey->transition = $request->transition;
|
224
|
|
|
$hostkey->user_id = $request->user_id;
|
225
|
|
|
if($request->state == 'Approved' || $request->state == 'Rejected'){
|
226
|
|
|
$hostkey->save();
|
227
|
|
|
|
228
|
|
|
$error = false;
|
229
|
|
|
$statusCode = 200;
|
230
|
|
|
$title = 'Success';
|
231
|
|
|
$type = 'success';
|
232
|
|
|
$message = 'Data Saved Successfuly.';
|
233
|
|
|
$result = $hostkey;
|
234
|
|
|
}else {
|
235
|
|
|
$error = true;
|
236
|
|
|
$statusCode = 404;
|
237
|
|
|
$title = 'Error';
|
238
|
|
|
$type = 'error';
|
239
|
|
|
$message = 'Error';
|
240
|
|
|
$result = 'Not Found';
|
241
|
|
|
}
|
242
|
|
|
} catch (Exception $e) {
|
243
|
|
|
$error = true;
|
244
|
|
|
$statusCode = 404;
|
245
|
|
|
$title = 'Error';
|
246
|
|
|
$type = 'error';
|
247
|
|
|
$message = 'Error';
|
248
|
|
|
$result = 'Not Found';
|
249
|
|
|
} finally {
|
250
|
|
|
return Response::json(array(
|
251
|
|
|
'error' => $error,
|
|
|
|
|
252
|
|
|
'status' => $statusCode,
|
|
|
|
|
253
|
|
|
'title' => $title,
|
|
|
|
|
254
|
|
|
'type' => $type,
|
|
|
|
|
255
|
|
|
'message' => $message,
|
|
|
|
|
256
|
|
|
'result' => $result
|
|
|
|
|
257
|
|
|
));
|
258
|
|
|
}
|
259
|
|
|
}
|
260
|
|
|
|
261
|
|
|
/**
|
262
|
|
|
* Remove the specified resource from storage.
|
263
|
|
|
*
|
264
|
|
|
* @param int $id
|
265
|
|
|
* @return Response
|
266
|
|
|
*/
|
267
|
|
|
public function destroy($id)
|
268
|
|
|
{
|
269
|
|
|
|
270
|
|
|
}
|
271
|
|
|
|
272
|
|
|
}
|
273
|
|
|
|
274
|
|
|
?>
|
|
|
|
|
275
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.