|
1
|
|
|
<?php namespace crocodicstudio\crudbooster\controllers; |
|
2
|
|
|
|
|
3
|
|
|
use CRUDbooster; |
|
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\DB; |
|
5
|
|
|
use Illuminate\Support\Facades\Excel; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Facades\PDF; |
|
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Request; |
|
8
|
|
|
use Illuminate\Support\Facades\Route; |
|
9
|
|
|
|
|
10
|
|
|
class ApiCustomController extends CBController |
|
11
|
|
|
{ |
|
12
|
|
|
public function cbInit() |
|
13
|
|
|
{ |
|
14
|
|
|
$this->table = 'cms_apicustom'; |
|
15
|
|
|
$this->primary_key = 'id'; |
|
16
|
|
|
$this->title_field = "nama"; |
|
17
|
|
|
$this->button_show = false; |
|
18
|
|
|
$this->button_new = false; |
|
|
|
|
|
|
19
|
|
|
$this->button_delete = false; |
|
20
|
|
|
$this->button_add = false; |
|
21
|
|
|
$this->button_import = false; |
|
22
|
|
|
$this->button_export = false; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
function getIndex() |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->cbLoader(); |
|
28
|
|
|
|
|
29
|
|
|
if (! CRUDBooster::isSuperadmin()) { |
|
30
|
|
|
CRUDBooster::insertLog(trans("crudbooster.log_try_view", ['name' => 'API Index', 'module' => 'API'])); |
|
31
|
|
|
CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access')); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$data = []; |
|
35
|
|
|
|
|
36
|
|
|
$data['page_title'] = 'API Generator'; |
|
37
|
|
|
$data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
|
38
|
|
|
$data['apis'] = DB::table('cms_apicustom')->orderby('nama', 'asc')->get(); |
|
39
|
|
|
|
|
40
|
|
|
return view('crudbooster::api_documentation', $data); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
function apiDocumentation() |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
$this->cbLoader(); |
|
46
|
|
|
$data = []; |
|
47
|
|
|
|
|
48
|
|
|
$data['apis'] = DB::table('cms_apicustom')->orderby('nama', 'asc')->get(); |
|
49
|
|
|
|
|
50
|
|
|
return view('crudbooster::api_documentation_public', $data); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
function getDownloadPostman() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$this->cbLoader(); |
|
56
|
|
|
$data = []; |
|
57
|
|
|
$data['variables'] = []; |
|
58
|
|
|
$data['info'] = [ |
|
59
|
|
|
'name' => CRUDBooster::getSetting('appname').' - API', |
|
60
|
|
|
'_postman_id' => "1765dd11-73d1-2978-ae11-36921dc6263d", |
|
61
|
|
|
'description' => '', |
|
62
|
|
|
'schema' => 'https://schema.getpostman.com/json/collection/v2.0.0/collection.json', |
|
63
|
|
|
]; |
|
64
|
|
|
$items = []; |
|
65
|
|
|
$apis = DB::table('cms_apicustom')->orderby('nama', 'asc')->get(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($apis as $a) { |
|
68
|
|
|
$parameters = unserialize($a->parameters); |
|
69
|
|
|
$formdata = []; |
|
70
|
|
|
$httpbuilder = []; |
|
71
|
|
|
if ($parameters) { |
|
72
|
|
|
foreach ($parameters as $p) { |
|
73
|
|
|
$enabled = ($p['used'] == 0) ? false : true; |
|
74
|
|
|
$name = $p['name']; |
|
75
|
|
|
$httpbuilder[$name] = ''; |
|
76
|
|
|
if ($enabled) { |
|
77
|
|
|
$formdata[] = ['key' => $name, 'value' => '', 'type' => 'text', 'enabled' => $enabled]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (strtolower($a->method_type) == 'get') { |
|
83
|
|
|
if ($httpbuilder) { |
|
84
|
|
|
$httpbuilder = "?".http_build_query($httpbuilder); |
|
85
|
|
|
} else { |
|
86
|
|
|
$httpbuilder = ''; |
|
87
|
|
|
} |
|
88
|
|
|
} else { |
|
89
|
|
|
$httpbuilder = ''; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$items[] = [ |
|
93
|
|
|
'name' => $a->nama, |
|
94
|
|
|
'request' => [ |
|
95
|
|
|
'url' => url('api/'.$a->permalink).$httpbuilder, |
|
96
|
|
|
'method' => $a->method_type ?: 'GET', |
|
97
|
|
|
'header' => [], |
|
98
|
|
|
'body' => [ |
|
99
|
|
|
'mode' => 'formdata', |
|
100
|
|
|
'formdata' => $formdata, |
|
101
|
|
|
], |
|
102
|
|
|
'description' => $a->keterangan, |
|
103
|
|
|
], |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
$data['item'] = $items; |
|
107
|
|
|
|
|
108
|
|
|
$json = json_encode($data); |
|
109
|
|
|
|
|
110
|
|
|
return \Response::make($json, 200, [ |
|
111
|
|
|
'Content-Type' => 'application/json', |
|
112
|
|
|
'Content-Disposition' => 'attachment; filename='.CRUDBooster::getSetting('appname').' - API For POSTMAN.json', |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function getScreetKey() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->cbLoader(); |
|
119
|
|
|
$data['page_title'] = 'API Generator'; |
|
|
|
|
|
|
120
|
|
|
$data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
|
121
|
|
|
$data['apikeys'] = DB::table('cms_apikey')->get(); |
|
122
|
|
|
|
|
123
|
|
|
return view('crudbooster::api_key', $data); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getGenerator() |
|
127
|
|
|
{ |
|
128
|
|
|
$this->cbLoader(); |
|
129
|
|
|
|
|
130
|
|
|
if (! CRUDBooster::isSuperadmin()) { |
|
131
|
|
|
CRUDBooster::insertLog(trans("crudbooster.log_try_view", ['name' => 'API Index', 'module' => 'API'])); |
|
132
|
|
|
CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access')); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$data['page_title'] = 'API Generator'; |
|
|
|
|
|
|
136
|
|
|
$data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
|
137
|
|
|
|
|
138
|
|
|
$tables = CRUDBooster::listTables(); |
|
139
|
|
|
$tables_list = []; |
|
140
|
|
|
foreach ($tables as $tab) { |
|
141
|
|
|
foreach ($tab as $key => $value) { |
|
142
|
|
|
$tables_list[] = $value; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$data['tables'] = $tables_list; |
|
147
|
|
|
|
|
148
|
|
|
return view('crudbooster::api_generator', $data); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function getEditApi($id) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->cbLoader(); |
|
154
|
|
|
|
|
155
|
|
|
if (! CRUDBooster::isSuperadmin()) { |
|
156
|
|
|
CRUDBooster::insertLog(trans("crudbooster.log_try_view", ['name' => 'API Edit', 'module' => 'API'])); |
|
157
|
|
|
CRUDBooster::redirect(CRUDBooster::adminPath(), trans('crudbooster.denied_access')); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$row = DB::table('cms_apicustom')->where('id', $id)->first(); |
|
161
|
|
|
|
|
162
|
|
|
$data['row'] = $row; |
|
|
|
|
|
|
163
|
|
|
$data['parameters'] = json_encode(unserialize($row->parameters)); |
|
164
|
|
|
$data['responses'] = json_encode(unserialize($row->responses)); |
|
165
|
|
|
$data['page_title'] = 'API Generator'; |
|
166
|
|
|
$data['page_menu'] = Route::getCurrentRoute()->getActionName(); |
|
167
|
|
|
|
|
168
|
|
|
$tables = CRUDBooster::listTables(); |
|
169
|
|
|
$tables_list = []; |
|
170
|
|
|
foreach ($tables as $tab) { |
|
171
|
|
|
foreach ($tab as $key => $value) { |
|
172
|
|
|
$tables_list[] = $value; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$data['tables'] = $tables_list; |
|
177
|
|
|
|
|
178
|
|
|
return view('crudbooster::api_generator', $data); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
function getGenerateScreetKey() |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
$this->cbLoader(); |
|
184
|
|
|
//Generate a random string. |
|
185
|
|
|
$token = openssl_random_pseudo_bytes(16); |
|
186
|
|
|
|
|
187
|
|
|
//Convert the binary data into hexadecimal representation. |
|
188
|
|
|
$token = bin2hex($token); |
|
189
|
|
|
|
|
190
|
|
|
$id = DB::table('cms_apikey')->insertGetId([ |
|
191
|
|
|
'screetkey' => $token, |
|
192
|
|
|
'created_at' => date('Y-m-d H:i:s'), |
|
193
|
|
|
'status' => 'active', |
|
194
|
|
|
'hit' => 0, |
|
195
|
|
|
]); |
|
196
|
|
|
|
|
197
|
|
|
$response = []; |
|
198
|
|
|
$response['key'] = $token; |
|
199
|
|
|
$response['id'] = $id; |
|
200
|
|
|
|
|
201
|
|
|
return response()->json($response); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function getStatusApikey() |
|
205
|
|
|
{ |
|
206
|
|
|
CRUDBooster::valid(['id', 'status'], 'view'); |
|
207
|
|
|
|
|
208
|
|
|
$id = Request::get('id'); |
|
209
|
|
|
$status = (Request::get('status') == 1) ? "active" : "non active"; |
|
210
|
|
|
|
|
211
|
|
|
DB::table('cms_apikey')->where('id', $id)->update(['status' => $status]); |
|
212
|
|
|
|
|
213
|
|
|
return redirect()->back()->with(['message' => 'You have been update api key status !', 'message_type' => 'success']); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function getDeleteApiKey() |
|
217
|
|
|
{ |
|
218
|
|
|
|
|
219
|
|
|
$id = Request::get('id'); |
|
220
|
|
|
if (DB::table('cms_apikey')->where('id', $id)->delete()) { |
|
221
|
|
|
return response()->json(['status' => 1]); |
|
222
|
|
|
} else { |
|
223
|
|
|
return response()->json(['status' => 0]); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
function getColumnTable($table, $type = 'list') |
|
|
|
|
|
|
228
|
|
|
{ |
|
229
|
|
|
$this->cbLoader(); |
|
230
|
|
|
$result = []; |
|
|
|
|
|
|
231
|
|
|
|
|
232
|
|
|
$cols = CRUDBooster::getTableColumns($table); |
|
233
|
|
|
|
|
234
|
|
|
$except = ['created_at', 'deleted_at', 'updated_at']; |
|
235
|
|
|
|
|
236
|
|
|
$result = $cols; |
|
237
|
|
|
$new_result = []; |
|
238
|
|
|
foreach ($result as $ro) { |
|
239
|
|
|
|
|
240
|
|
|
if (in_array($ro, $except)) { |
|
241
|
|
|
continue; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$type_field = CRUDBooster::getFieldType($table, $ro); |
|
245
|
|
|
|
|
246
|
|
|
$type_field = (array_search($ro, explode(',', config('crudbooster.EMAIL_FIELDS_CANDIDATE'))) !== false) ? "email" : $type_field; |
|
247
|
|
|
$type_field = (array_search($ro, explode(',', config('crudbooster.IMAGE_FIELDS_CANDIDATE'))) !== false) ? "image" : $type_field; |
|
248
|
|
|
$type_field = (array_search($ro, explode(',', config('crudbooster.PASSWORD_FIELDS_CANDIDATE'))) !== false) ? "password" : $type_field; |
|
249
|
|
|
|
|
250
|
|
|
$type_field = (substr($ro, -3) == '_id') ? "integer" : $type_field; |
|
251
|
|
|
$type_field = (substr($ro, 0, 3) == 'id_') ? "integer" : $type_field; |
|
252
|
|
|
|
|
253
|
|
|
$new_result[] = ['name' => $ro, 'type' => $type_field]; |
|
254
|
|
|
|
|
255
|
|
|
if ($type == 'list' || $type == 'detail') { |
|
256
|
|
|
if (substr($ro, 0, 3) == 'id_') { |
|
257
|
|
|
$table2 = substr($ro, 3); |
|
258
|
|
|
$t2 = DB::getSchemaBuilder()->getColumnListing($table2); |
|
259
|
|
|
foreach ($t2 as $t) { |
|
260
|
|
|
if ($t != 'id' && $t != 'created_at' && $t != 'updated_at' && $t != 'deleted_at') { |
|
261
|
|
|
|
|
262
|
|
|
if (substr($t, 0, 3) == 'id_') { |
|
263
|
|
|
continue; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
$type_field = CRUDBooster::getFieldType($table2, $t); |
|
267
|
|
|
$t = str_replace("_$table2", "", $t); |
|
268
|
|
|
$new_result[] = ['name' => $table2.'_'.$t, 'type' => $type_field]; |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return response()->json($new_result); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
function postSaveApiCustom() |
|
|
|
|
|
|
279
|
|
|
{ |
|
280
|
|
|
$this->cbLoader(); |
|
281
|
|
|
$posts = Request::all(); |
|
282
|
|
|
|
|
283
|
|
|
$a = []; |
|
284
|
|
|
|
|
285
|
|
|
$a['nama'] = g('nama'); |
|
286
|
|
|
$a['tabel'] = $posts['tabel']; |
|
287
|
|
|
$a['aksi'] = $posts['aksi']; |
|
288
|
|
|
$a['permalink'] = g('permalink'); |
|
289
|
|
|
$a['method_type'] = g('method_type'); |
|
290
|
|
|
|
|
291
|
|
|
$params_name = g('params_name'); |
|
292
|
|
|
$params_type = g('params_type'); |
|
293
|
|
|
$params_config = g('params_config'); |
|
294
|
|
|
$params_required = g('params_required'); |
|
295
|
|
|
$params_used = g('params_used'); |
|
296
|
|
|
$json = []; |
|
297
|
|
|
|
|
298
|
|
|
for ($i = 0; $i <= count($params_name); $i++) { |
|
|
|
|
|
|
299
|
|
|
if ($params_name[$i]) { |
|
300
|
|
|
$json[] = [ |
|
301
|
|
|
'name' => $params_name[$i], |
|
302
|
|
|
'type' => $params_type[$i], |
|
303
|
|
|
'config' => $params_config[$i], |
|
304
|
|
|
'required' => $params_required[$i], |
|
305
|
|
|
'used' => $params_used[$i], |
|
306
|
|
|
]; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
$json = array_filter($json); |
|
311
|
|
|
$a['parameters'] = serialize($json); |
|
312
|
|
|
|
|
313
|
|
|
$a['sql_where'] = g('sql_where'); |
|
314
|
|
|
|
|
315
|
|
|
$responses_name = g('responses_name'); |
|
316
|
|
|
$responses_type = g('responses_type'); |
|
317
|
|
|
$responses_subquery = g('responses_subquery'); |
|
318
|
|
|
$responses_used = g('responses_used'); |
|
319
|
|
|
$json = []; |
|
320
|
|
|
for ($i = 0; $i <= count($responses_name); $i++) { |
|
|
|
|
|
|
321
|
|
|
if ($responses_name[$i]) { |
|
322
|
|
|
$json[] = [ |
|
323
|
|
|
'name' => $responses_name[$i], |
|
324
|
|
|
'type' => $responses_type[$i], |
|
325
|
|
|
'subquery' => $responses_subquery[$i], |
|
326
|
|
|
'used' => $responses_used[$i], |
|
327
|
|
|
]; |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
$json = array_filter($json); |
|
332
|
|
|
$a['responses'] = serialize($json); |
|
333
|
|
|
$a['keterangan'] = g('keterangan'); |
|
334
|
|
|
|
|
335
|
|
|
if (Request::get('id')) { |
|
336
|
|
|
DB::table('cms_apicustom')->where('id', g('id'))->update($a); |
|
337
|
|
|
} else { |
|
338
|
|
|
|
|
339
|
|
|
$controllerName = ucwords(str_replace('_', ' ', $a['permalink'])); |
|
340
|
|
|
$controllerName = str_replace(' ', '', $controllerName); |
|
341
|
|
|
CRUDBooster::generateAPI($controllerName, $a['tabel'], $a['permalink'], $a['method_type']); |
|
342
|
|
|
|
|
343
|
|
|
DB::table('cms_apicustom')->insert($a); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
return redirect(CRUDBooster::mainpath())->with(['message' => 'Yeay, your api has been saved successfully !', 'message_type' => 'success']); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
function getDeleteApi($id) |
|
|
|
|
|
|
350
|
|
|
{ |
|
351
|
|
|
$this->cbLoader(); |
|
352
|
|
|
$row = DB::table('cms_apicustom')->where('id', $id)->first(); |
|
353
|
|
|
DB::table('cms_apicustom')->where('id', $id)->delete(); |
|
354
|
|
|
|
|
355
|
|
|
$controllername = ucwords(str_replace('_', ' ', $row->permalink)); |
|
356
|
|
|
$controllername = str_replace(' ', '', $controllername); |
|
357
|
|
|
@unlink(base_path("app/Http/Controllers/Api".$controllername."Controller.php")); |
|
|
|
|
|
|
358
|
|
|
|
|
359
|
|
|
return response()->json(['status' => 1]); |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths