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
|
|
|
use Validator, Image, Session, File, Response, Redirect, Exception; |
16
|
|
|
|
17
|
|
|
class HostkeysController extends Controller |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Display a listing of the resource. |
22
|
|
|
* |
23
|
|
|
* @return Response |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function index(Request $request) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
try { |
28
|
|
|
if($request->get('search') != ''){ |
29
|
|
|
$data['data'] = Hostkeys::with('getState') |
|
|
|
|
30
|
|
|
->with('getTransition') |
31
|
|
|
->with('getUserName') |
32
|
|
|
->where('hostname', 'like', '%'.$request->get('search').'%') |
33
|
|
|
->orderBy('id', 'desc') |
34
|
|
|
->paginate(env('PAGINATE', 10)); |
35
|
|
|
} else{ |
36
|
|
|
$data['data'] = Hostkeys::with('getState') |
|
|
|
|
37
|
|
|
->with('getTransition') |
38
|
|
|
->with('getUserName') |
39
|
|
|
->orderBy('id', 'desc') |
40
|
|
|
->paginate(env('PAGINATE', 10)); |
41
|
|
|
} |
42
|
|
|
} catch (Exception $e) { |
43
|
|
|
$data['data'] = []; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
return view('host_keys.index', $data); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Show the form for creating a new resource. |
50
|
|
|
* |
51
|
|
|
* @return Response |
52
|
|
|
*/ |
53
|
|
|
public function create() |
54
|
|
|
{ |
55
|
|
|
return view('host_keys.create'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Store a newly created resource in storage. |
60
|
|
|
* |
61
|
|
|
* @return Response |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function store(Request $request) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
if($request->keys != NULL){ |
67
|
|
|
$keys = $request->keys; |
68
|
|
|
}else { |
69
|
|
|
$keys = ""; |
70
|
|
|
} |
71
|
|
|
$hostkey = New Hostkeys; |
72
|
|
|
$hostkey->hostname = str_replace(array('https://', 'http://'), array('',''),$request->hostname); |
73
|
|
|
$hostkey->keys = $keys; |
74
|
|
|
$hostkey->state = $request->state; |
75
|
|
|
$hostkey->transition = $request->transition; |
76
|
|
|
$hostkey->user_id = $request->user_id; |
77
|
|
|
$hostkey->save(); |
78
|
|
|
|
79
|
|
|
$error = false; |
80
|
|
|
$statusCode = 200; |
81
|
|
|
$title = 'Success'; |
82
|
|
|
$type = 'success'; |
83
|
|
|
$message = 'Data Saved Successfuly.'; |
84
|
|
|
$result = $hostkey; |
85
|
|
|
} catch (Exception $e) { |
86
|
|
|
$error = true; |
87
|
|
|
$statusCode = 404; |
88
|
|
|
$title = 'Error'; |
89
|
|
|
$type = 'error'; |
90
|
|
|
$message = 'Error'; |
91
|
|
|
$result = 'Not Found'; |
92
|
|
|
} finally { |
93
|
|
|
return Response::json(array( |
94
|
|
|
'error' => $error, |
|
|
|
|
95
|
|
|
'status' => $statusCode, |
|
|
|
|
96
|
|
|
'title' => $title, |
|
|
|
|
97
|
|
|
'type' => $type, |
|
|
|
|
98
|
|
|
'message' => $message, |
|
|
|
|
99
|
|
|
'result' => $result |
|
|
|
|
100
|
|
|
)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Display the specified resource. |
106
|
|
|
* |
107
|
|
|
* @param int $id |
108
|
|
|
* @return Response |
109
|
|
|
*/ |
110
|
|
|
public function show($id) |
111
|
|
|
{ |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function get($hostname) |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
|
|
$data = Hostkeys::with('getState') |
119
|
|
|
->with('getTransition') |
120
|
|
|
->with('getUserName') |
121
|
|
|
->where('hostname', 'like', '%'.$hostname.'%') |
122
|
|
|
->orderBy('id', 'desc') |
123
|
|
|
->first(); |
124
|
|
|
|
125
|
|
|
$error = false; |
126
|
|
|
$statusCode = 200; |
127
|
|
|
$title = 'Success'; |
128
|
|
|
$type = 'success'; |
129
|
|
|
$message = 'Success'; |
130
|
|
|
$result = $data->hostname; |
131
|
|
|
$resultid = $data->id; |
132
|
|
|
} catch (Exception $e) { |
133
|
|
|
$error = true; |
134
|
|
|
$statusCode = 404; |
135
|
|
|
$title = 'Error'; |
136
|
|
|
$type = 'error'; |
137
|
|
|
$message = 'Error'; |
138
|
|
|
$result = 'Not Found'; |
139
|
|
|
$resultid = 'Not Found'; |
140
|
|
|
} finally { |
141
|
|
|
return Response::json(array( |
142
|
|
|
'error' => $error, |
|
|
|
|
143
|
|
|
'status' => $statusCode, |
|
|
|
|
144
|
|
|
'title' => $title, |
|
|
|
|
145
|
|
|
'type' => $type, |
|
|
|
|
146
|
|
|
'message' => $message, |
|
|
|
|
147
|
|
|
'result' => $result, |
|
|
|
|
148
|
|
|
'id' => $resultid |
|
|
|
|
149
|
|
|
)); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function request(Request $request){ |
154
|
|
|
Validator::extend('without_spaces', function($attr, $value){ |
155
|
|
|
return preg_match('/^\S*$/u', $value); |
156
|
|
|
}); |
157
|
|
|
$validator = Validator::make($request->all(), [ |
158
|
|
|
'host' => 'required|without_spaces|unique:host_keys,hostname', |
159
|
|
|
'description' => 'required', |
160
|
|
|
]); |
161
|
|
|
if($validator->fails()) |
162
|
|
|
{ |
163
|
|
|
Session::flash('message', 'Please fix the error(s) below'); |
164
|
|
|
return redirect()->back() |
165
|
|
|
->withErrors($validator) |
166
|
|
|
->withInput(); |
167
|
|
|
} |
168
|
|
View Code Duplication |
if(Auth::guest()){ $current_user = 1; } |
|
|
|
|
169
|
|
|
else{ $current_user = (Auth::user())?((Auth::user()->id)?Auth::user()->id:1):1; } |
170
|
|
|
$headers = ['Content-Type' => 'application/json']; |
171
|
|
|
$data = [ |
172
|
|
|
'host' => $request->host, |
173
|
|
|
'client' => $request->client, |
174
|
|
|
'request' => 'Request', |
175
|
|
|
'deskripsi' => $request->description, |
176
|
|
|
'user_id' => $current_user |
177
|
|
|
]; |
178
|
|
|
$body = json_encode($data); |
179
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$request->host); |
180
|
|
|
|
181
|
|
|
try { |
182
|
|
|
$url = "https://".$host."/api/v1/api-manager/request"; |
183
|
|
|
$client = new \GuzzleHttp\Client(); |
184
|
|
|
$res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
185
|
|
|
$response = $res->getBody(); |
186
|
|
|
$responses = json_decode($response); |
187
|
|
|
$msg = "success"; |
188
|
|
|
} catch (GuzzleException $e) { |
189
|
|
|
$msg = "error"; |
190
|
|
|
$responses = $e; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if($msg == "success"){ |
194
|
|
|
$responses = $responses; |
|
|
|
|
195
|
|
|
}else { |
196
|
|
|
try { |
197
|
|
|
$urlz = "http://".$host."/api/v1/api-manager/request"; |
198
|
|
|
$clientz = new \GuzzleHttp\Client(); |
199
|
|
|
$resz = $clientz->request('POST', $urlz,['headers'=>$headers,'body'=>$body]); |
200
|
|
|
$responsez = $resz->getBody(); |
201
|
|
|
$responsesz = json_decode($responsez); |
202
|
|
|
$msgz = "success"; |
203
|
|
|
} catch (GuzzleException $er) { |
204
|
|
|
$msgz = "error"; |
205
|
|
|
$responsesz = $er; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if($msgz == "success"){ |
209
|
|
|
$responses = $responsesz; |
210
|
|
|
}else { |
211
|
|
|
$message = $msgz.' - '.$responsesz->getMessage(); |
212
|
|
|
Session::flash('message', $message); |
213
|
|
|
return Redirect::to('host-keys'); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if($responses->status == 200){ |
218
|
|
|
$hostkey = New Hostkeys; |
219
|
|
|
$hostkey->hostname = $host; |
220
|
|
|
$hostkey->keys = ""; |
221
|
|
|
$hostkey->state = $responses->result->request; |
222
|
|
|
$hostkey->transition = "Propose To ".$responses->result->request; |
223
|
|
|
$hostkey->user_id = $responses->result->user_id; |
224
|
|
|
$hostkey->save(); |
225
|
|
View Code Duplication |
if(env('URL_APIMANAGER') != NULL){ |
|
|
|
|
226
|
|
|
$url_apimanager = str_replace('"', '',env('URL_APIMANAGER')); |
227
|
|
|
if($url_apimanager != "" || $url_apimanager != NULL || $url_apimanager != false || !empty($url_apimanager)){ |
228
|
|
|
$this->send_apimanager($url_apimanager,$request,$current_user,$hostkey->transition); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
Session::flash('message', 'Send Request Api Keys Successfuly'); |
232
|
|
|
}else { |
233
|
|
|
Session::flash('message', $responses->message); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return Redirect::to('host-keys'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
private function send_apimanager($url_apimanager,$request,$current_user,$keterangan){ |
240
|
|
|
$headers = ['Content-Type' => 'application/json']; |
241
|
|
|
$host = str_replace(array('https://', 'http://'), array('',''),$request->host); |
242
|
|
|
$client = str_replace(array('https://', 'http://'), array('',''),$request->client); |
243
|
|
|
$data = [ |
244
|
|
|
'host' => $host, |
245
|
|
|
'client' => $client, |
246
|
|
|
'keterangan' => $keterangan, |
247
|
|
|
'user_id' => $current_user |
248
|
|
|
]; |
249
|
|
|
$body = json_encode($data); |
250
|
|
|
$url_apimanager = str_replace(array('https://', 'http://'), array('',''),$url_apimanager); |
251
|
|
|
|
252
|
|
|
try { |
253
|
|
|
$url = "https://".$url_apimanager."/api/store"; |
254
|
|
|
$client = new \GuzzleHttp\Client(); |
255
|
|
|
$res = $client->request('POST', $url,['headers'=>$headers,'body'=>$body]); |
256
|
|
|
$response = $res->getBody(); |
257
|
|
|
$responses = json_decode($response); |
258
|
|
|
$msg = "success"; |
259
|
|
|
} catch (GuzzleException $e) { |
260
|
|
|
$msg = "error"; |
261
|
|
|
$responses = $e; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
View Code Duplication |
if($msg == "success"){ |
|
|
|
|
265
|
|
|
$responses = $responses; |
|
|
|
|
266
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$keterangan; |
|
|
|
|
267
|
|
|
}else { |
268
|
|
|
try { |
269
|
|
|
$urlz = "http://".$url_apimanager."/api/store"; |
270
|
|
|
$clientz = new \GuzzleHttp\Client(); |
271
|
|
|
$resz = $clientz->request('POST', $urlz,['headers'=>$headers,'body'=>$body]); |
272
|
|
|
$responsez = $resz->getBody(); |
273
|
|
|
$responsesz = json_decode($responsez); |
274
|
|
|
$msgz = "success"; |
275
|
|
|
} catch (GuzzleException $er) { |
276
|
|
|
$msgz = "error"; |
277
|
|
|
$responsesz = $er; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
if($msgz == "success"){ |
281
|
|
|
$responses = $responsesz; |
282
|
|
|
$message = 'Send Apikey to Host Api Manager successfully with description is '.$keterangan; |
|
|
|
|
283
|
|
|
}else { |
284
|
|
|
$responses = $responsesz->getMessage(); |
285
|
|
|
$message = $msgz.' - '.$responsesz->getMessage(); |
|
|
|
|
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $responses; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Show the form for editing the specified resource. |
294
|
|
|
* |
295
|
|
|
* @param int $id |
296
|
|
|
* @return Response |
297
|
|
|
*/ |
298
|
|
|
public function edit($id) |
299
|
|
|
{ |
300
|
|
|
|
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Update the specified resource in storage. |
305
|
|
|
* |
306
|
|
|
* @param int $id |
307
|
|
|
* @return Response |
308
|
|
|
*/ |
309
|
|
View Code Duplication |
public function update(Request $request, $id) |
|
|
|
|
310
|
|
|
{ |
311
|
|
|
try { |
312
|
|
|
if($request->keys != NULL){ |
313
|
|
|
$keys = $request->keys; |
314
|
|
|
}else { |
315
|
|
|
$keys = ""; |
316
|
|
|
} |
317
|
|
|
$hostkey = Hostkeys::findOrFail($id); |
318
|
|
|
$hostkey->hostname = str_replace(array('https://', 'http://'), array('',''),$request->hostname); |
319
|
|
|
$hostkey->keys = $keys; |
320
|
|
|
$hostkey->state = $request->state; |
321
|
|
|
$hostkey->transition = $request->transition; |
322
|
|
|
$hostkey->user_id = $request->user_id; |
323
|
|
|
$hostkey->save(); |
324
|
|
|
|
325
|
|
|
$error = false; |
326
|
|
|
$statusCode = 200; |
327
|
|
|
$title = 'Success'; |
328
|
|
|
$type = 'success'; |
329
|
|
|
$message = 'Data Saved Successfuly.'; |
330
|
|
|
$result = $hostkey; |
331
|
|
|
} catch (Exception $e) { |
332
|
|
|
$error = true; |
333
|
|
|
$statusCode = 404; |
334
|
|
|
$title = 'Error'; |
335
|
|
|
$type = 'error'; |
336
|
|
|
$message = 'Error'; |
337
|
|
|
$result = 'Not Found'; |
338
|
|
|
} finally { |
339
|
|
|
return Response::json(array( |
340
|
|
|
'error' => $error, |
|
|
|
|
341
|
|
|
'status' => $statusCode, |
|
|
|
|
342
|
|
|
'title' => $title, |
|
|
|
|
343
|
|
|
'type' => $type, |
|
|
|
|
344
|
|
|
'message' => $message, |
|
|
|
|
345
|
|
|
'result' => $result |
|
|
|
|
346
|
|
|
)); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Remove the specified resource from storage. |
352
|
|
|
* |
353
|
|
|
* @param int $id |
354
|
|
|
* @return Response |
355
|
|
|
*/ |
356
|
|
|
public function destroy($id) |
357
|
|
|
{ |
358
|
|
|
|
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
?> |
|
|
|
|
364
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.