|
1
|
|
|
<?php namespace crocodicstudio\crudbooster\controllers; |
|
2
|
|
|
|
|
3
|
|
|
use CRUDBooster; |
|
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\DB; |
|
5
|
|
|
use Illuminate\Support\Facades\Hash; |
|
6
|
|
|
use Illuminate\Support\Facades\Request; |
|
7
|
|
|
use Illuminate\Support\Facades\Validator; |
|
8
|
|
|
|
|
9
|
|
|
class ApiController extends Controller |
|
10
|
|
|
{ |
|
11
|
|
|
public $method_type; |
|
12
|
|
|
public $permalink; |
|
13
|
|
|
|
|
14
|
|
|
private $hook_api_status; |
|
15
|
|
|
private $hook_api_message; |
|
16
|
|
|
private $last_id_tmp = []; |
|
17
|
|
|
|
|
18
|
|
|
private $limit = null; |
|
19
|
|
|
private $output = null; |
|
20
|
|
|
|
|
21
|
|
|
public function setLimit($limit) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->limit = $limit; |
|
24
|
|
|
return $this; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function output($array) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->output = $array; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function hook_before(&$postdata) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function hook_after($postdata, &$result) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function hook_query(&$query) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function hook_api_status($boolean) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->hook_api_status = $boolean; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function hook_api_message($message) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->hook_api_message = $message; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function execute_api($output = 'JSON') |
|
58
|
|
|
{ |
|
59
|
|
|
|
|
60
|
|
|
// DB::enableQueryLog(); |
|
61
|
|
|
|
|
62
|
|
|
$posts = Request::all(); |
|
63
|
|
|
$posts_keys = array_keys($posts); |
|
|
|
|
|
|
64
|
|
|
$posts_values = array_values($posts); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$row_api = DB::table('cms_apicustom')->where('permalink', $this->permalink)->first(); |
|
67
|
|
|
|
|
68
|
|
|
$action_type = $row_api->aksi; |
|
69
|
|
|
$table = $row_api->tabel; |
|
70
|
|
|
$pk = CRUDBooster::pk($table); |
|
71
|
|
|
|
|
72
|
|
|
$debug_mode_message = 'You are in debug mode !'; |
|
73
|
|
|
|
|
74
|
|
|
/* |
|
75
|
|
|
| ---------------------------------------------- |
|
76
|
|
|
| Method Type validation |
|
77
|
|
|
| ---------------------------------------------- |
|
78
|
|
|
| |
|
79
|
|
|
*/ |
|
80
|
|
|
|
|
81
|
|
|
if ($row_api->method_type) { |
|
82
|
|
|
$method_type = $row_api->method_type; |
|
83
|
|
|
if ($method_type) { |
|
84
|
|
|
if (! Request::isMethod($method_type)) { |
|
85
|
|
|
$result['api_status'] = 0; |
|
|
|
|
|
|
86
|
|
|
$result['api_message'] = "The requested method is not allowed!"; |
|
87
|
|
|
goto show; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/* |
|
93
|
|
|
| ---------------------------------------------- |
|
94
|
|
|
| Check the row is exists or not |
|
95
|
|
|
| ---------------------------------------------- |
|
96
|
|
|
| |
|
97
|
|
|
*/ |
|
98
|
|
|
if (! $row_api) { |
|
99
|
|
|
$result['api_status'] = 0; |
|
100
|
|
|
$result['api_message'] = 'Sorry this API endpoint is no longer available or has been changed. Please make sure endpoint is correct.'; |
|
101
|
|
|
|
|
102
|
|
|
goto show; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
@$parameters = unserialize($row_api->parameters); |
|
106
|
|
|
@$responses = unserialize($row_api->responses); |
|
107
|
|
|
|
|
108
|
|
|
/* |
|
109
|
|
|
| ---------------------------------------------- |
|
110
|
|
|
| User Data Validation |
|
111
|
|
|
| ---------------------------------------------- |
|
112
|
|
|
| |
|
113
|
|
|
*/ |
|
114
|
|
|
if ($parameters) { |
|
115
|
|
|
$type_except = ['password', 'ref', 'base64_file', 'custom', 'search']; |
|
116
|
|
|
$input_validator = []; |
|
117
|
|
|
$data_validation = []; |
|
118
|
|
|
foreach ($parameters as $param) { |
|
119
|
|
|
$name = $param['name']; |
|
120
|
|
|
$type = $param['type']; |
|
121
|
|
|
$value = $posts[$name]; |
|
122
|
|
|
|
|
123
|
|
|
$required = $param['required']; |
|
124
|
|
|
$config = $param['config']; |
|
125
|
|
|
$used = $param['used']; |
|
126
|
|
|
$format_validation = []; |
|
127
|
|
|
|
|
128
|
|
|
if ($used && ! $required && $value == '') { |
|
129
|
|
|
continue; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
if ($used == '0') { |
|
133
|
|
|
continue; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
if ($config && substr($config, 0, 1) == '*') { |
|
137
|
|
|
continue; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$input_validator[$name] = trim($value); |
|
141
|
|
|
|
|
142
|
|
|
if ($required == '1') { |
|
143
|
|
|
$format_validation[] = 'required'; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if ($type == 'exists') { |
|
147
|
|
|
$config = explode(',', $config); |
|
148
|
|
|
$table_exist = $config[0]; |
|
149
|
|
|
$table_exist = CRUDBooster::parseSqlTable($table_exist)['table']; |
|
150
|
|
|
$field_exist = $config[1]; |
|
151
|
|
|
$config = ($field_exist) ? $table_exist.','.$field_exist : $table_exist; |
|
152
|
|
|
$format_validation[] = 'exists:'.$config; |
|
153
|
|
|
} elseif ($type == 'unique') { |
|
154
|
|
|
$config = explode(',', $config); |
|
155
|
|
|
$table_exist = $config[0]; |
|
156
|
|
|
$table_exist = CRUDBooster::parseSqlTable($table_exist)['table']; |
|
157
|
|
|
$field_exist = $config[1]; |
|
158
|
|
|
$config = ($field_exist) ? $table_exist.','.$field_exist : $table_exist; |
|
159
|
|
|
$format_validation[] = 'unique:'.$config; |
|
160
|
|
|
} elseif ($type == 'date_format') { |
|
161
|
|
|
$format_validation[] = 'date_format:'.$config; |
|
162
|
|
|
} elseif ($type == 'digits_between') { |
|
163
|
|
|
$format_validation[] = 'digits_between:'.$config; |
|
164
|
|
|
} elseif ($type == 'in') { |
|
165
|
|
|
$format_validation[] = 'in:'.$config; |
|
166
|
|
|
} elseif ($type == 'mimes') { |
|
167
|
|
|
$format_validation[] = 'mimes:'.$config; |
|
168
|
|
|
} elseif ($type == 'min') { |
|
169
|
|
|
$format_validation[] = 'min:'.$config; |
|
170
|
|
|
} elseif ($type == 'max') { |
|
171
|
|
|
$format_validation[] = 'max:'.$config; |
|
172
|
|
|
} elseif ($type == 'not_in') { |
|
173
|
|
|
$format_validation[] = 'not_in:'.$config; |
|
174
|
|
|
} elseif ($type == 'image') { |
|
175
|
|
|
$format_validation[] = 'image'; |
|
176
|
|
|
$input_validator[$name] = Request::file($name); |
|
177
|
|
|
} elseif ($type == 'file') { |
|
178
|
|
|
$format_validation[] = 'file'; |
|
179
|
|
|
$input_validator[$name] = Request::file($name); |
|
180
|
|
|
} else { |
|
181
|
|
|
if (! in_array($type, $type_except)) { |
|
182
|
|
|
$format_validation[] = $type; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if ($name == 'id') { |
|
187
|
|
|
$table_exist = CRUDBooster::parseSqlTable($table)['table']; |
|
188
|
|
|
$table_exist_pk = CRUDBooster::pk($table_exist); |
|
189
|
|
|
$format_validation[] = 'exists:'.$table_exist.','.$table_exist_pk; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if (count($format_validation)) { |
|
193
|
|
|
$data_validation[$name] = implode('|', $format_validation); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
$validator = Validator::make($input_validator, $data_validation); |
|
198
|
|
|
if ($validator->fails()) { |
|
199
|
|
|
$message = $validator->errors()->all(); |
|
200
|
|
|
$message = implode(', ', $message); |
|
201
|
|
|
$result['api_status'] = 0; |
|
202
|
|
|
$result['api_message'] = $message; |
|
203
|
|
|
|
|
204
|
|
|
goto show; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$responses_fields = []; |
|
209
|
|
|
foreach ($responses as $r) { |
|
210
|
|
|
if ($r['used']) { |
|
211
|
|
|
$responses_fields[] = $r['name']; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$this->hook_before($posts); |
|
216
|
|
|
if($this->output) { |
|
217
|
|
|
return response()->json($this->output); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$limit = ($this->limit)?:$posts['limit']; |
|
221
|
|
|
$offset = ($posts['offset']) ?: 0; |
|
222
|
|
|
$orderby = ($posts['orderby']) ?: $table.'.'.$pk.',desc'; |
|
223
|
|
|
$uploads_format_candidate = explode(',', config("crudbooster.UPLOAD_TYPES")); |
|
224
|
|
|
$uploads_candidate = explode(',', config('crudbooster.IMAGE_FIELDS_CANDIDATE')); |
|
|
|
|
|
|
225
|
|
|
$password_candidate = explode(',', config('crudbooster.PASSWORD_FIELDS_CANDIDATE')); |
|
|
|
|
|
|
226
|
|
|
$asset = asset('/'); |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
unset($posts['limit']); |
|
229
|
|
|
unset($posts['offset']); |
|
230
|
|
|
unset($posts['orderby']); |
|
231
|
|
|
|
|
232
|
|
|
if ($action_type == 'list' || $action_type == 'detail' || $action_type == 'delete') { |
|
233
|
|
|
$name_tmp = []; |
|
234
|
|
|
$data = DB::table($table); |
|
235
|
|
|
if ($offset) { |
|
236
|
|
|
$data->skip($offset); |
|
237
|
|
|
} |
|
238
|
|
|
if($limit) { |
|
239
|
|
|
$data->take($limit); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
foreach ($responses as $resp) { |
|
243
|
|
|
$name = $resp['name']; |
|
244
|
|
|
$type = $resp['type']; |
|
245
|
|
|
$subquery = $resp['subquery']; |
|
246
|
|
|
$used = intval($resp['used']); |
|
247
|
|
|
|
|
248
|
|
|
if ($used == 0 && ! CRUDBooster::isForeignKey($name)) { |
|
249
|
|
|
continue; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if (in_array($name, $name_tmp)) { |
|
253
|
|
|
continue; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if ($name == 'ref_id') { |
|
257
|
|
|
continue; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
if ($type == 'custom') { |
|
261
|
|
|
continue; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
if ($subquery && $subquery != 'null') { |
|
265
|
|
|
$data->addSelect(DB::raw('('.$subquery.') as '.$name)); |
|
266
|
|
|
$name_tmp[] = $name; |
|
267
|
|
|
continue; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
if ($used) { |
|
271
|
|
|
$data->addSelect($table.'.'.$name); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$name_tmp[] = $name; |
|
275
|
|
|
if (CRUDBooster::isForeignKey($name)) { |
|
276
|
|
|
$jointable = CRUDBooster::getTableForeignKey($name); |
|
277
|
|
|
$jointable_field = CRUDBooster::getTableColumns($jointable); |
|
278
|
|
|
$jointablePK = CRUDBooster::pk($jointable); |
|
279
|
|
|
$data->leftjoin($jointable, $jointable.'.'.$jointablePK, '=', $table.'.'.$name); |
|
280
|
|
|
foreach ($jointable_field as $jf) { |
|
281
|
|
|
$jf_alias = $jointable.'_'.$jf; |
|
282
|
|
|
if (in_array($jf_alias, $responses_fields)) { |
|
283
|
|
|
$data->addselect($jointable.'.'.$jf.' as '.$jf_alias); |
|
284
|
|
|
$name_tmp[] = $jf_alias; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
} //End Responses |
|
289
|
|
|
|
|
290
|
|
|
foreach ($parameters as $param) { |
|
291
|
|
|
$name = $param['name']; |
|
292
|
|
|
$type = $param['type']; |
|
293
|
|
|
$value = $posts[$name]; |
|
294
|
|
|
$used = $param['used']; |
|
295
|
|
|
$required = $param['required']; |
|
296
|
|
|
$config = $param['config']; |
|
297
|
|
|
|
|
298
|
|
|
if ($type == 'password') { |
|
299
|
|
|
$data->addselect($table.'.'.$name); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
if ($type == 'search') { |
|
303
|
|
|
$search_in = explode(',', $config); |
|
304
|
|
|
|
|
305
|
|
|
if ($required == '1') { |
|
306
|
|
|
$data->where(function ($w) use ($search_in, $value) { |
|
307
|
|
|
foreach ($search_in as $k => $field) { |
|
308
|
|
|
if ($k == 0) { |
|
309
|
|
|
$w->where($field, "like", "%$value%"); |
|
310
|
|
|
} else { |
|
311
|
|
|
$w->orWhere($field, "like", "%$value%"); |
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
}); |
|
315
|
|
|
} else { |
|
316
|
|
|
if ($used) { |
|
317
|
|
|
if ($value) { |
|
318
|
|
|
$data->where(function ($w) use ($search_in, $value) { |
|
319
|
|
|
foreach ($search_in as $k => $field) { |
|
320
|
|
|
if ($k == 0) { |
|
321
|
|
|
$w->where($field, "like", "%$value%"); |
|
322
|
|
|
} else { |
|
323
|
|
|
$w->orWhere($field, "like", "%$value%"); |
|
324
|
|
|
} |
|
325
|
|
|
} |
|
326
|
|
|
}); |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
if (CRUDBooster::isColumnExists($table, 'deleted_at')) { |
|
334
|
|
|
$data->where($table.'.deleted_at', null); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
$data->where(function ($w) use ($parameters, $posts, $table, $type_except) { |
|
|
|
|
|
|
338
|
|
|
foreach ($parameters as $param) { |
|
339
|
|
|
$name = $param['name']; |
|
340
|
|
|
$type = $param['type']; |
|
341
|
|
|
$value = $posts[$name]; |
|
342
|
|
|
$used = $param['used']; |
|
343
|
|
|
$required = $param['required']; |
|
344
|
|
|
|
|
345
|
|
|
if (in_array($type, $type_except)) { |
|
346
|
|
|
continue; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
if ($required == '1') { |
|
350
|
|
|
if (CRUDBooster::isColumnExists($table, $name)) { |
|
351
|
|
|
$w->where($table.'.'.$name, $value); |
|
352
|
|
|
} else { |
|
353
|
|
|
$w->having($name, '=', $value); |
|
354
|
|
|
} |
|
355
|
|
|
} else { |
|
356
|
|
|
if ($used) { |
|
357
|
|
|
if ($value) { |
|
358
|
|
|
if (CRUDBooster::isColumnExists($table, $name)) { |
|
359
|
|
|
$w->where($table.'.'.$name, $value); |
|
360
|
|
|
} else { |
|
361
|
|
|
$w->having($name, '=', $value); |
|
362
|
|
|
} |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
}); |
|
368
|
|
|
|
|
369
|
|
|
//IF SQL WHERE IS NOT NULL |
|
370
|
|
|
if ($row_api->sql_where) { |
|
371
|
|
|
$theSql = $row_api->sql_where; |
|
372
|
|
|
//blow it apart at the variables; |
|
373
|
|
|
preg_match_all("/\[([^\]]*)\]/", $theSql, $matches); |
|
374
|
|
|
foreach ($matches[1] as $match) { |
|
375
|
|
|
foreach ($parameters as $param) { |
|
376
|
|
|
if (in_array($match, $param)) { |
|
377
|
|
|
/* it is possible that the where condition |
|
378
|
|
|
* asks for data that's not required |
|
379
|
|
|
* so we're not going to check for that |
|
380
|
|
|
* it's up to the API creator |
|
381
|
|
|
*/ |
|
382
|
|
|
$value = $posts[$match]; |
|
383
|
|
|
/* any password parameter is invalid by default |
|
384
|
|
|
* if they were hashed by Laravel there's no way to retrieve it |
|
385
|
|
|
* and they're handled later through Auth |
|
386
|
|
|
*/ |
|
387
|
|
|
if ($param['type'] === 'password') { |
|
388
|
|
|
Log::error('Password parameters cannot be used in WHERE queries'); |
|
389
|
|
|
|
|
390
|
|
|
return response()->view('errors.500', [], 500); |
|
391
|
|
|
} |
|
392
|
|
|
$value = "'".$value."'"; |
|
393
|
|
|
//insert our $value into its place in the WHERE clause |
|
394
|
|
|
$theSql = preg_replace("/\[([^\]]*".$match.")\]/", $value, $theSql); |
|
395
|
|
|
} |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
$data->whereraw($theSql); |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
$this->hook_query($data); |
|
402
|
|
|
|
|
403
|
|
|
if ($action_type == 'list') { |
|
404
|
|
|
if ($orderby) { |
|
405
|
|
|
$orderby_raw = explode(',', $orderby); |
|
406
|
|
|
$orderby_col = $orderby_raw[0]; |
|
407
|
|
|
$orderby_val = $orderby_raw[1]; |
|
408
|
|
|
} else { |
|
409
|
|
|
$orderby_col = $table.'.'.$pk; |
|
410
|
|
|
$orderby_val = 'desc'; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
$rows = $data->orderby($orderby_col, $orderby_val)->get(); |
|
414
|
|
|
|
|
415
|
|
|
if ($rows) { |
|
416
|
|
|
|
|
417
|
|
|
foreach ($rows as &$row) { |
|
418
|
|
|
foreach ($row as $k => $v) { |
|
419
|
|
|
$ext = \File::extension($v); |
|
420
|
|
|
if (in_array($ext, $uploads_format_candidate)) { |
|
421
|
|
|
$row->$k = asset($v); |
|
422
|
|
|
} |
|
423
|
|
|
|
|
424
|
|
|
if (! in_array($k, $responses_fields)) { |
|
425
|
|
|
unset($row->$k); |
|
426
|
|
|
} |
|
427
|
|
|
} |
|
428
|
|
|
} |
|
429
|
|
|
|
|
430
|
|
|
$result['api_status'] = 1; |
|
431
|
|
|
$result['api_message'] = 'success'; |
|
432
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
433
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
434
|
|
|
} |
|
435
|
|
|
$result['data'] = $rows; |
|
436
|
|
|
} else { |
|
437
|
|
|
$result['api_status'] = 0; |
|
438
|
|
|
$result['api_message'] = 'There is no data found !'; |
|
439
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
440
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
441
|
|
|
} |
|
442
|
|
|
$result['data'] = []; |
|
443
|
|
|
} |
|
444
|
|
|
} elseif ($action_type == 'detail') { |
|
445
|
|
|
|
|
446
|
|
|
$rows = $data->first(); |
|
447
|
|
|
|
|
448
|
|
|
if ($rows) { |
|
449
|
|
|
|
|
450
|
|
|
foreach ($parameters as $param) { |
|
451
|
|
|
$name = $param['name']; |
|
452
|
|
|
$type = $param['type']; |
|
453
|
|
|
$value = $posts[$name]; |
|
454
|
|
|
$used = $param['used']; |
|
455
|
|
|
$required = $param['required']; |
|
456
|
|
|
|
|
457
|
|
|
if ($required) { |
|
458
|
|
|
if ($type == 'password') { |
|
459
|
|
|
if (! Hash::check($value, $rows->{$name})) { |
|
460
|
|
|
$result['api_status'] = 0; |
|
461
|
|
|
$result['api_message'] = 'Invalid credentials. Check your username and password.'; |
|
462
|
|
|
|
|
463
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
464
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
465
|
|
|
} |
|
466
|
|
|
goto show; |
|
467
|
|
|
} |
|
468
|
|
|
} |
|
469
|
|
|
} else { |
|
470
|
|
|
if ($used) { |
|
471
|
|
|
if ($value) { |
|
472
|
|
|
if (! Hash::check($value, $row->{$name})) { |
|
|
|
|
|
|
473
|
|
|
$result['api_status'] = 0; |
|
474
|
|
|
$result['api_message'] = 'Invalid credentials. Check your username and password.'; |
|
475
|
|
|
|
|
476
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
477
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
478
|
|
|
} |
|
479
|
|
|
goto show; |
|
480
|
|
|
} |
|
481
|
|
|
} |
|
482
|
|
|
} |
|
483
|
|
|
} |
|
484
|
|
|
} |
|
485
|
|
|
|
|
486
|
|
|
foreach ($rows as $k => $v) { |
|
487
|
|
|
$ext = \File::extension($v); |
|
488
|
|
|
if (in_array($ext, $uploads_format_candidate)) { |
|
489
|
|
|
$rows->$k = asset($v); |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
if (! in_array($k, $responses_fields)) { |
|
493
|
|
|
unset($rows->$k); |
|
494
|
|
|
} |
|
495
|
|
|
} |
|
496
|
|
|
|
|
497
|
|
|
$result['api_status'] = 1; |
|
498
|
|
|
$result['api_message'] = 'success'; |
|
499
|
|
|
$result['api_response_fields'] = $responses_fields; |
|
500
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
501
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
502
|
|
|
} |
|
503
|
|
|
$rows = (array) $rows; |
|
504
|
|
|
$result = array_merge($result, $rows); |
|
505
|
|
|
} else { |
|
506
|
|
|
$result['api_status'] = 0; |
|
507
|
|
|
$result['api_message'] = 'There is no data found !'; |
|
508
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
509
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
510
|
|
|
} |
|
511
|
|
|
} |
|
512
|
|
|
} elseif ($action_type == 'delete') { |
|
513
|
|
|
|
|
514
|
|
|
if (CRUDBooster::isColumnExists($table, 'deleted_at')) { |
|
515
|
|
|
$delete = $data->update(['deleted_at' => date('Y-m-d H:i:s')]); |
|
516
|
|
|
} else { |
|
517
|
|
|
$delete = $data->delete(); |
|
518
|
|
|
} |
|
519
|
|
|
|
|
520
|
|
|
$result['api_status'] = ($delete) ? 1 : 0; |
|
521
|
|
|
$result['api_message'] = ($delete) ? "success" : "failed"; |
|
522
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
523
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
524
|
|
|
} |
|
525
|
|
|
} |
|
526
|
|
|
} elseif ($action_type == 'save_add' || $action_type == 'save_edit') { |
|
527
|
|
|
|
|
528
|
|
|
$row_assign = []; |
|
529
|
|
|
foreach ($input_validator as $k => $v) { |
|
|
|
|
|
|
530
|
|
|
if (CRUDBooster::isColumnExists($table, $k)) { |
|
531
|
|
|
$row_assign[$k] = $v; |
|
532
|
|
|
} |
|
533
|
|
|
} |
|
534
|
|
|
|
|
535
|
|
|
foreach ($parameters as $param) { |
|
536
|
|
|
$name = $param['name']; |
|
537
|
|
|
$used = $param['used']; |
|
538
|
|
|
$value = $posts[$name]; |
|
539
|
|
|
if ($used == '1' && $value == '') { |
|
540
|
|
|
unset($row_assign[$name]); |
|
541
|
|
|
} |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
if ($action_type == 'save_add') { |
|
545
|
|
|
if (CRUDBooster::isColumnExists($table, 'created_at')) { |
|
546
|
|
|
$row_assign['created_at'] = date('Y-m-d H:i:s'); |
|
547
|
|
|
} |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
if ($action_type == 'save_edit') { |
|
551
|
|
|
if (CRUDBooster::isColumnExists($table, 'updated_at')) { |
|
552
|
|
|
$row_assign['updated_at'] = date('Y-m-d H:i:s'); |
|
553
|
|
|
} |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
$row_assign_keys = array_keys($row_assign); |
|
557
|
|
|
|
|
558
|
|
|
foreach ($parameters as $param) { |
|
559
|
|
|
$name = $param['name']; |
|
560
|
|
|
$value = $posts[$name]; |
|
561
|
|
|
$config = $param['config']; |
|
|
|
|
|
|
562
|
|
|
$type = $param['type']; |
|
563
|
|
|
$required = $param['required']; |
|
|
|
|
|
|
564
|
|
|
$used = $param['used']; |
|
|
|
|
|
|
565
|
|
|
|
|
566
|
|
|
if (! in_array($name, $row_assign_keys)) { |
|
567
|
|
|
|
|
568
|
|
|
continue; |
|
569
|
|
|
} |
|
570
|
|
|
|
|
571
|
|
|
if ($type == 'file' || $type == 'image') { |
|
572
|
|
|
$row_assign[$name] = CRUDBooster::uploadFile($name, true); |
|
573
|
|
|
} elseif ($type == 'base64_file') { |
|
574
|
|
|
$row_assign[$name] = CRUDBooster::uploadBase64($value); |
|
575
|
|
|
} elseif ($type == 'password') { |
|
576
|
|
|
$row_assign[$name] = Hash::make(g($name)); |
|
577
|
|
|
} |
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
//Make sure if saving/updating data additional param included |
|
581
|
|
|
$arrkeys = array_keys($row_assign); |
|
582
|
|
|
foreach ($posts as $key => $value) { |
|
583
|
|
|
if (! in_array($key, $arrkeys)) { |
|
584
|
|
|
$row_assign[$key] = $value; |
|
585
|
|
|
} |
|
586
|
|
|
} |
|
587
|
|
|
|
|
588
|
|
|
if ($action_type == 'save_add') { |
|
589
|
|
|
|
|
590
|
|
|
DB::beginTransaction(); |
|
591
|
|
|
try{ |
|
592
|
|
|
$id = DB::table($table)->insertGetId($row_assign); |
|
593
|
|
|
DB::commit(); |
|
594
|
|
|
}catch (\Exception $e) |
|
595
|
|
|
{ |
|
596
|
|
|
DB::rollBack(); |
|
597
|
|
|
throw new \Exception($e->getMessage()); |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
$result['api_status'] = ($id) ? 1 : 0; |
|
601
|
|
|
$result['api_message'] = ($id) ? 'success' : 'failed'; |
|
602
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
603
|
|
|
|
|
604
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
605
|
|
|
} |
|
606
|
|
|
$result['id'] = $id; |
|
607
|
|
|
} else { |
|
608
|
|
|
|
|
609
|
|
|
try { |
|
610
|
|
|
$pk = CRUDBooster::pk($table); |
|
611
|
|
|
$update = DB::table($table); |
|
612
|
|
|
$update->where($table.'.'.$pk, $row_assign['id']); |
|
613
|
|
|
|
|
614
|
|
|
if ($row_api->sql_where) { |
|
615
|
|
|
$update->whereraw($row_api->sql_where); |
|
616
|
|
|
} |
|
617
|
|
|
|
|
618
|
|
|
$this->hook_query($update); |
|
619
|
|
|
|
|
620
|
|
|
$update = $update->update($row_assign); |
|
|
|
|
|
|
621
|
|
|
$result['api_status'] = 1; |
|
622
|
|
|
$result['api_message'] = 'success'; |
|
623
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
624
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
625
|
|
|
} |
|
626
|
|
|
} catch (\Exception $e) { |
|
627
|
|
|
$result['api_status'] = 0; |
|
628
|
|
|
$result['api_message'] = 'failed, '.$e; |
|
629
|
|
|
|
|
630
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
631
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
632
|
|
|
} |
|
633
|
|
|
} |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
// Update The Child Table |
|
637
|
|
|
foreach ($parameters as $param) { |
|
638
|
|
|
$name = $param['name']; |
|
639
|
|
|
$value = $posts[$name]; |
|
640
|
|
|
$config = $param['config']; |
|
641
|
|
|
$type = $param['type']; |
|
642
|
|
|
if ($type == 'ref') { |
|
643
|
|
|
if (CRUDBooster::isColumnExists($config, 'id_'.$table)) { |
|
644
|
|
|
DB::table($config)->where($name, $value)->update(['id_'.$table => $lastId]); |
|
|
|
|
|
|
645
|
|
|
} elseif (CRUDBooster::isColumnExists($config, $table.'_id')) { |
|
646
|
|
|
DB::table($config)->where($name, $value)->update([$table.'_id' => $lastId]); |
|
647
|
|
|
} |
|
648
|
|
|
} |
|
649
|
|
|
} |
|
650
|
|
|
} |
|
651
|
|
|
|
|
652
|
|
|
show: |
|
653
|
|
|
$result['api_status'] = $this->hook_api_status ?: $result['api_status']; |
|
|
|
|
|
|
654
|
|
|
$result['api_message'] = $this->hook_api_message ?: $result['api_message']; |
|
655
|
|
|
|
|
656
|
|
|
if (CRUDBooster::getSetting('api_debug_mode') == 'true') { |
|
657
|
|
|
$result['api_authorization'] = $debug_mode_message; |
|
658
|
|
|
} |
|
659
|
|
|
|
|
660
|
|
|
$this->hook_after($posts, $result); |
|
661
|
|
|
if($this->output) return response()->json($this->output); |
|
662
|
|
|
|
|
663
|
|
|
if($output == 'JSON') { |
|
664
|
|
|
return response()->json($result, 200); |
|
665
|
|
|
}else{ |
|
666
|
|
|
return $result; |
|
667
|
|
|
} |
|
668
|
|
|
} |
|
669
|
|
|
|
|
670
|
|
|
protected function isJSON($theData) |
|
671
|
|
|
{ |
|
672
|
|
|
//return either the array or JSON decoded array |
|
673
|
|
|
$test = json_decode($theData[0], true); |
|
674
|
|
|
|
|
675
|
|
|
switch (json_last_error()) { |
|
676
|
|
|
case JSON_ERROR_NONE: |
|
677
|
|
|
$error = ''; // JSON is valid // No error has occurred |
|
678
|
|
|
break; |
|
679
|
|
|
case JSON_ERROR_DEPTH: |
|
680
|
|
|
$error = 'The maximum stack depth has been exceeded.'; |
|
681
|
|
|
break; |
|
682
|
|
|
case JSON_ERROR_STATE_MISMATCH: |
|
683
|
|
|
$error = 'Invalid or malformed JSON.'; |
|
684
|
|
|
break; |
|
685
|
|
|
case JSON_ERROR_CTRL_CHAR: |
|
686
|
|
|
$error = 'Control character error, possibly incorrectly encoded.'; |
|
687
|
|
|
break; |
|
688
|
|
|
case JSON_ERROR_SYNTAX: |
|
689
|
|
|
$error = 'Syntax error, malformed JSON.'; |
|
690
|
|
|
break; |
|
691
|
|
|
case JSON_ERROR_UTF8: |
|
692
|
|
|
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded.'; |
|
693
|
|
|
break; |
|
694
|
|
|
case JSON_ERROR_RECURSION: |
|
695
|
|
|
$error = 'One or more recursive references in the value to be encoded.'; |
|
696
|
|
|
break; |
|
697
|
|
|
case JSON_ERROR_INF_OR_NAN: |
|
698
|
|
|
$error = 'One or more NAN or INF values in the value to be encoded.'; |
|
699
|
|
|
break; |
|
700
|
|
|
case JSON_ERROR_UNSUPPORTED_TYPE: |
|
701
|
|
|
$error = 'A value of a type that cannot be encoded was given.'; |
|
702
|
|
|
break; |
|
703
|
|
|
default: |
|
704
|
|
|
$error = 'Unknown JSON error occured.'; |
|
705
|
|
|
break; |
|
706
|
|
|
} |
|
707
|
|
|
|
|
708
|
|
|
if ($error !== '') { |
|
709
|
|
|
Log::info('No JSON'); |
|
710
|
|
|
$result = (is_object($theData)) ? (array) $theData : $theData; |
|
711
|
|
|
} else { |
|
712
|
|
|
Log::info('Is JSON'); |
|
713
|
|
|
$result = $test; |
|
714
|
|
|
} |
|
715
|
|
|
|
|
716
|
|
|
return $result; |
|
717
|
|
|
} |
|
718
|
|
|
} |
|
719
|
|
|
|
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