-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtnrs_api.php.bak
executable file
·693 lines (597 loc) · 20.6 KB
/
tnrs_api.php.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
<?php
////////////////////////////////////////////////////////
// Accepts batch web service requests and submits to
// nsr_batch.php
//
// Note the use of goto for error handling. Simple.
// Concise. Effective. So there :P
////////////////////////////////////////////////////////
///////////////////////////////////
// Parameters
///////////////////////////////////
require_once 'server_params.php'; // parameters in ALL_CAPS set here
require_once 'params.php'; // parameters in ALL_CAPS set here
require_once($utilities_path."status_codes.inc.php");
/*
// Needed to retrieve $DB (db name) for version-specific options
require_once $CONFIG_DIR.'db_config.php';
*/
// Temporary data directory
$data_dir_tmp = $DATADIR;
$data_dir_tmp = "/tmp/tnrs/";
// Text displayed when no match found
$no_match_message = '[No match found]';
// Input file name & path
// User JSON input saved to this file as pipe-delimited text
// Becomes input for tnrs_batch command (`./controller.pl [...]`)
$basename = "tnrs_" . uniqid(rand(), true);
$filename_tmp = $basename . '_in.tsv';
$file_tmp = $data_dir_tmp . $filename_tmp;
// Results file name & path
// Output of tnrs_batch command will be saved to this file
$results_filename = $basename . "_out.tsv";
# Full path and name of results file
$results_file = $data_dir_tmp . $results_filename;
///////////////////////////////////
// Functions
///////////////////////////////////
function file_to_array_assoc($filepath, $delim) {
/////////////////////////////////////////////////
// Loads results file as an asociative array
//
// Options:
// $filepath: path and name of file to import
// $delim: field delimiter
/////////////////////////////////////////////////
$array = $fields = array(); $i = 0;
$handle = @fopen($filepath, "r");
if ($handle) {
while (($row = fgetcsv($handle, 4096, $delim , '"' , '"')) !== false) {
// Load keys from header row & continue to next
if (empty($fields)) {
$fields = $row;
continue;
}
// Load value for this row
foreach ($row as $k=>$value) {
$array[$i][$fields[$k]] = $value;
}
$i++;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
return $array;
}
function array_unique_multidimensional($array) {
/////////////////////////////////////////////////
// Make multidimensional array unique
/////////////////////////////////////////////////
$array = array_map("unserialize",
array_unique(array_map("serialize", $array)));
$array = fix_keys($array); // Fix named keys screwed up by PHP
return $array;
}
function fix_keys($array) {
/////////////////////////////////////////////////
// Revert conversion of named keys to numeric
// Essential! Repairs mess made by PHP after
// manipulating multi-dimensional associative
// arrays
/////////////////////////////////////////////////
$numberCheck = false;
foreach ($array as $k => $val) {
if (is_array($val)) $array[$k] = fix_keys($val); //recurse
if (is_numeric($k)) $numberCheck = true;
}
if ($numberCheck === true) {
return array_values($array);
} else {
return $array;
}
}
/////////////////////////////////////////////////////
// Send the POST response, with either data or error
// message in body. Body MUST be json.
/////////////////////////////////////////////////////
function send_response($status, $body) {
header("Access-Control-Allow-Origin: *");
header('Content-type: application/json');
http_response_code($status);
echo $body;
}
////////////////////////////////////////
// Receive & validate the POST request
////////////////////////////////////////
// Start by assuming no errors
// Any run time errors and this will be set to true
$err_code=0;
$err_msg="";
$err=false;
// Make sure request is a pre-flight request or POST
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
// Send pre-flight response and quit
//header("Access-Control-Allow-Origin: http://localhost:3000"); // Dev
header("Access-Control-Allow-Origin: *"); // Production
header("Access-Control-Allow-Methods: POST, OPTIONS ");
header("Access-Control-Allow-Headers: Content-type");
header("Access-Control-Max-Age: 86400");
exit;
} else if (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0) {
$err_msg="ERROR: Request method must be POST";
$err_code=400; goto err;
}
// Make sure that the content type of the POST request has been
// set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if (strcasecmp($contentType, 'application/json') != 0) {
$err_msg="ERROR: Content type must be: application/json";
$err_code=400; goto err;
}
// Receive the RAW post data.
$input_json = trim(file_get_contents("php://input"));
///////////////////////////////////////////
// Convert post data to array and separate
// data from options
///////////////////////////////////////////
// Attempt to decode the incoming RAW post data from JSON.
$input_array = json_decode($input_json, true);
// If json_decode failed, the JSON is invalid.
if (!is_array($input_array)) {
$err_msg="ERROR: Received content contains invalid JSON!";
$err_code=400; goto err;
}
///////////////////////////////////
// Inspect the JSON data and run
// safety/security checks
///////////////////////////////////
// UNDER CONSTRUCTION!
///////////////////////////////////////////
// Extract & validate options
///////////////////////////////////////////
// Get options and data from JSON
if ( ! ( $opt_arr = isset($input_array['opts'])?$input_array['opts']:false ) ) {
$err_msg="ERROR: No options (element 'opts') in JSON request!";
$err_code=400; goto err;
}
///////////////////////////////////////////
// Validate options and assign each to its
// own parameter
///////////////////////////////////////////
include $APP_DIR . "validate_options.php";
if ($err) goto err;
///////////////////////////////////////////
// Check option $mode
// If "meta", ignore other options and begin
// processing metadata request. Otherwise
// continue processing tnrs_batch request
///////////////////////////////////////////
//
// $data=$input_array['data'];
// echo "data='$data' ";
//
if ( $mode=="parse" || $mode=="resolve" || $mode=='syn' || $mode=="" ) { // BEGIN mode_if
// tnrs_batch (no indent)
///////////////////////////////////////////
// Extract & validate data
///////////////////////////////////////////
// Get data from JSON
if ( !( $data_arr = isset($input_array['data'])?$input_array['data']:false ) ) {
$err_msg="ERROR: No data (element 'data') in JSON request";
$err_code=400; goto err;
}
# Check payload size
$rows = count($data_arr);
if ( $mode=='syn' ) {
if ( $rows>1 ) {
$err_msg="ERROR: $rows rows exceeds limit of 1 for request 'syn'";
$err_code=413; # 413 Payload Too Large
goto err;
}
} else {
if ( $rows>$MAX_ROWS && $MAX_ROWS>0 ) {
$err_msg="ERROR: Requested $rows rows exceeds $MAX_ROWS row limit";
$err_code=413; # 413 Payload Too Large
goto err;
}
}
# Validate data array structure
# Should have 1 or more rows of exactly 2 elements each
$rows=0;
foreach ($data_arr as $row) {
$rows++;
$values=0;
foreach($row as $value) $values++;
if ($values!= 2) {
$err_msg="ERROR: Data has wrong number of columns, should be exactly 2";
$err_code=400; goto err;
}
}
if ($rows==0) {
$err_msg="ERROR: No data rows!"; $err_code=400; goto err;
}
///////////////////////////////////////////
// Reset selected options for compatibility
// with tnrs_batch command line syntax
///////////////////////////////////////////
// Processing mode
if ( $mode == "parse" ) {
//if(stripos($mode, "parse") !== false) {
$opt_mode = "-mode parse"; // Parse-only mode
} else {
$opt_mode = ""; // Default 'resolve' mode
}
// Match mode
if ( $matches == "all" ) {
//if(stripos($mode, "parse") !== false) {
$opt_matches = "-matches all"; // Return all matches
} else {
$opt_matches = ""; // Returns best match only by default
}
# Parse-only or syn over-ride matches
if ( $mode == "parse" || $mode=='syn' ) {
$opt_matches = "";
}
# Check only one source if this is a 'syn' call
if ( $mode=='syn' ) {
$comma = strpos($sources, ',');
if ($comma !== false) {
$err_msg="ERROR: too many sources submitted, only one allowed for mode='syn'";
$err_code=400; goto err;
}
}
///////////////////////////////////////////
// Save data array as pipe-delimited file,
// to be used as input for TNRS batch app
///////////////////////////////////////////
// Make temporary data directory & file in /tmp
$cmd="mkdir -p $data_dir_tmp";
exec($cmd, $output, $status);
if ($status) {
$err_msg="ERROR: Unable to create temp data directory";
$err_code=500; goto err;
}
// Convert array to pipe-delimited file & save
// tnrs_batch requires pipe-delimited
$fp = fopen($file_tmp, "w");
$i = 0;
foreach ($data_arr as $row) {
//if($i === 0) fputcsv($fp, array_keys($row)); // header
fputcsv($fp, array_values($row), '|'); // data
$i++;
}
fclose($fp);
// Run dos2unix to fix stupid DOS/Mac/Excel/UTF-16 issues, if any
$cmd = "dos2unix $file_tmp";
exec($cmd, $output, $status);
if ($status) {
$err_msg="Failed file conversion: dos2unix";
$err_code=500; goto err;
}
///////////////////////////////////
// Process the CSV file in batch mode
///////////////////////////////////
$data_dir_tmp_full = $data_dir_tmp . "/";
// Form the final command calling the parallel execution controller
$cmd = $BATCH_DIR . "controller.pl $opt_mode $opt_matches -in '$file_tmp' -out '$results_file' -sources '$sources' -class $class -nbatch $NBATCH -d t ";
// Execute the tnrs_batch command
exec($cmd, $output, $status);
if ($status) {
$err_msg="ERROR: tnrs_batch exit status: $status";
$err_code=500; goto err;
}
//if ($status) die("
// \$status=$status
// \$file_tmp='$file_tmp'
// \$results_file='$results_file'
// \$cmd=\"$cmd\"
// ");
///////////////////////////////////
// Retrieve the tab-delimited results
// file and convert to JSON
///////////////////////////////////
// Import the results file (tab-delimitted) to array
$results_array = file_to_array_assoc($results_file, "\t");
// Clean up crap inserted by core service
foreach ( $results_array as $rkey => $row ) {
$str = $row['Name_submitted'];
// Restore double-escaped single quote to single quote
$str = str_replace("'\\''", "'", $str);
// Trim surrounding single quotes added by core service
$start = substr( $str, 0, 1 );
$end = substr( $str, strlen($str)-1, 1 );
if ( $start="'" && $end="'" ) { // both quotes must be present
$str = substr($str, 1 );
$str = substr($str, 0, -1);
}
$results_array[$rkey]['Name_submitted']=$str;
$str = $row['Unmatched_terms'];
// Remove initial single quote
if ( substr( $str, 0, 1 )=="'" ) {
$str = substr($str, 1 );
}
// Remove backslashes
$str = str_replace("\\", "", $str);
$results_array[$rkey]['Unmatched_terms']=$str;
// Convert cryptic warning numbers to plain English
$w_num = $row['Warnings'];
static $warning_text=array(
'0'=>'',
'1'=>'[Partial]',
'2'=>'[Ambiguous]',
'4'=>'[HigherTaxa]',
'8'=>'[Overall]',
'3'=>'[Partial] [Ambiguous]',
'5'=>'[Partial] [HigherTaxa]',
'9'=>'[Partial] [Overall]',
'6'=>'[Ambiguous] [HigherTaxa]',
'10'=>'[Ambiguous] [Overall]',
'12'=>'[HigherTaxa] [Overall]',
'7'=>'[Partial] [Ambiguous] [HigherTaxa]',
'11'=>'[Partial] [Ambiguous] [Overall]',
'13'=>'[Partial] [HigherTaxa] [Overall]',
'14'=>'[Ambiguous] [HigherTaxa] [Overall]',
'15'=>'[Partial] [Ambiguous] [HigherTaxa] [Overall]'
);
$w_txt=$warning_text[$w_num];
$results_array[$rkey]['WarningsEng']=$w_txt;
}
// Filter by match accuracy if applicable
if ( $mode != "parse" ) {
if ( $acc > 0 ) {
foreach ( $results_array as $rkey => $row ) {
$score = $row['Overall_score'];
$fscore = $row['Family_score'];
$gscore = $row['Genus_score'];
$sscore = $row['Specific_epithet_score'];
$i1score = $row['Infraspecific_epithet_score'];
$i2score = $row['Infraspecific_epithet_2_score'];
# Reset match results for scores < threshold ($acc)
if ( $score < $acc &&
$fscore < $acc &&
$gscore < $acc &&
$sscore < $acc &&
$i1score < $acc &&
$i2score < $acc
) {
$results_array[$rkey]['Overall_score']='';
$results_array[$rkey]['Name_matched_id']='';
$results_array[$rkey]['Name_matched']=$no_match_message;
$results_array[$rkey]['Name_score']='';
$results_array[$rkey]['Name_matched_rank']='';
$results_array[$rkey]['Author_matched']='';
$results_array[$rkey]['Author_score']='';
$results_array[$rkey]['Canonical_author']='';
$results_array[$rkey]['Name_matched_accepted_family']='';
$results_array[$rkey]['Genus_matched']='';
$results_array[$rkey]['Genus_score']='';
$results_array[$rkey]['Specific_epithet_matched']='';
$results_array[$rkey]['Specific_epithet_score']='';
$results_array[$rkey]['Family_matched']='';
$results_array[$rkey]['Family_score']='';
$results_array[$rkey]['Infraspecific_rank']='';
$results_array[$rkey]['Infraspecific_epithet_matched']='';
$results_array[$rkey]['Infraspecific_epithet_score']='';
$results_array[$rkey]['Infraspecific_rank_2']='';
$results_array[$rkey]['Infraspecific_epithet_2_matched']='';
$results_array[$rkey]['Infraspecific_epithet_2_score']='';
$results_array[$rkey]['Unmatched_terms']= $results_array[$rkey]['Name_submitted'];
$results_array[$rkey]['Name_matched_url']='';
$results_array[$rkey]['Name_matched_lsid']='';
$results_array[$rkey]['Phonetic']='';
$results_array[$rkey]['Taxonomic_status']='';
$results_array[$rkey]['Accepted_name']='';
$results_array[$rkey]['Accepted_species']='';
$results_array[$rkey]['Accepted_name_author']='';
$results_array[$rkey]['Accepted_name_id']='';
$results_array[$rkey]['Accepted_name_rank']='';
$results_array[$rkey]['Accepted_name_url']='';
$results_array[$rkey]['Accepted_name_lsid']='';
$results_array[$rkey]['Accepted_family']='';
$results_array[$rkey]['Overall_score_order']='';
$results_array[$rkey]['Highertaxa_score_order']='';
$results_array[$rkey]['Source']='';
$results_array[$rkey]['Warnings']='';
}
} # END foreach ( $results_array as $rkey => $row )
// Remove duplicate rows
$results_array = array_unique_multidimensional($results_array);
##############################################
# Delete "no match" rows if matched row exists
##############################################
// Make new array of names matched
$matched_ids = array();
// Save IDs where match found
foreach ( $results_array as $rkey => $row ) {
$matched_val = $row['Name_matched'];
if ( $matched_val != $no_match_message ) {
array_push( $matched_ids, $results_array[$rkey]['ID'] );
}
}
// Remove duplicate values
$matched_ids = array_unique($matched_ids);
// Delete rows in results where "no match" + "ID matched elsewhere"
foreach ( $results_array as $rkey => $row ) {
$matched_val = $row['Name_matched'];
// If no-match result check ID
if ( $matched_val == $no_match_message ) {
$id = $row['ID'];
// If ID (name) matched elsewhere delete row
if ( in_array( $id, $matched_ids ) ) {
unset($results_array[$rkey]);
}
}
} // END foreach ( $results_array as $rkey => $row )
} // END if ( $acc > 0 )
} // END if ( $mode != "parse" )
$results_array = fix_keys($results_array); // Fix named keys screwed up by PHP
if ( $mode=='syn' ) { // BEGIN synonym lookup
// Retrieve synonyms of accepted name, according to source submitted
///////////////////////////////////////////
// Extract accepted name from TNRS results
///////////////////////////////////////////
// Get source
$src = $sources; // Should only be one
// Extract key TNRS results fields
$name_submitted = $results_array[0]['Name_submitted'];
$name_matched = $results_array[0]['Name_matched'];
$name_matched_author = $results_array[0]['Canonical_author'];
$name_matched_status = $results_array[0]['Taxonomic_status'];
$accname = $results_array[0]['Accepted_name'];
$accauth = $results_array[0]['Accepted_name_author'];
# Form the concatenated name plus author for matched and accepted names
$matched_nameauth = trim( $name_matched . ' ' . $name_matched_author );
$acc_nameauth = trim( $accname . ' ' . $accauth );
# Handle exceptions
if ( $name_matched==$no_match_message ) {
# No match found
$err_msg="ERROR: No match found for submitted name '$name_submitted' using source '$src'";
$err_code=400; goto err;
} else if ( trim( $accname )=="" ) {
# No accepted name
$err_msg="ERROR: No accepted name found for submitted name '$name_submitted' using source '$src'";
$err_code=400; goto err;
}
// Form the SQL
// Include submitted name, matched name and matched name taxonomic status
$sql="
SELECT '$name_submitted' AS submitted_name,
'$matched_nameauth' AS matched_nameWithAuthor,
'$name_matched_status' AS matched_taxonomicStatus,
acc.nameID AS accepted_nameID,
acc.scientificNameWithAuthor AS accepted_nameWithAuthor,
acc.sourceName AS accepted_source, acc.nameSourceUrl AS accepted_nameUrl,
n.nameID AS syn_nameID, n.scientificNameWithAuthor AS syn_nameWithAuthor,
syn.acceptance AS syn_taxonomicStatus, s.sourceName as syn_source,
ns.nameSourceUrl AS syn_nameUrl
FROM name n JOIN synonym syn JOIN source s
ON n.nameID=syn.nameID AND syn.sourceID=s.sourceID
JOIN name_source ns
ON n.nameID=ns.nameID AND s.sourceID=ns.sourceID
JOIN (
SELECT n.nameID, n.scientificNameWithAuthor,
syn.synonymID, syn.acceptance AS taxonomicStatus, s.sourceName,
ns.nameSourceUrl
FROM name n JOIN synonym syn JOIN source s
ON n.nameID=syn.nameID AND syn.sourceID=s.sourceID
JOIN name_source ns
ON n.nameID=ns.nameID AND s.sourceID=ns.sourceID
WHERE scientificNameWithAuthor='$acc_nameauth'
AND s.sourceName='$src'
) AS acc
WHERE s.sourceName='$src'
AND syn.acceptedNameID=acc.nameID
;
";
//echo "accname='$accname', src='$src' ";
// Run the query and return results as new $results_array
include("qy_db.php");
} // END synonym lookup
} else { // CONTINUE mode_if
// Metadata requests
if ( $mode=="meta" ) {
/*
$api_ver=shell_exec("echo -n $(git describe --abbrev=0)");
$code_ver=shell_exec("echo -n $(git --git-dir=../tnrs_batch/.git --work-tree=../tnrs_batch describe --abbrev=0)");
$sql="
SELECT db_version, build_date,
'$code_ver' AS code_version,
'$api_ver' AS api_version
FROM meta
;
";
*/
if ( stripos($DB, "tnrs_4_2") !== FALSE ) {
// Old version
$sql="
SELECT db_version,
build_date,
code_version,
api_version
FROM meta
;
";
} else {
# New version, includes app_version
$sql="
SELECT app_version,
db_version,
build_date,
code_version,
api_version
FROM meta
;
";
}
} elseif ( $mode=="sources" ) { // CONTINUE mode_if
$sql="
SELECT sourceID, sourceName, sourceNameFull, sourceUrl,
geographic_scope, taxonomic_scope, `scope`,
description, dataUrl, logo_path, isDefault,
sourceVersion as version, sourceReleaseDate,
dateAccessed AS tnrsDateAccessed
FROM source
;
";
} elseif ( $mode=="classifications" ) { // CONTINUE mode_if
$sql="
SELECT sourceID, sourceName
FROM source
WHERE isHigherClassification=1
;
";
} elseif ( $mode=="citations" ) { // CONTINUE mode_if
$sql="
SELECT 'tnrs_pub' AS source, publication as citation
FROM meta
UNION ALL
SELECT 'tnrs' AS source, citation
FROM meta
UNION ALL
SELECT sourceName AS source, citation
FROM source
WHERE citation IS NOT NULL AND TRIM(citation)<>''
;
";
} elseif ( $mode=="collaborators" ) { // CONTINUE mode_if
$sql="
SELECT collaboratorName, collaboratorNameFull, collaboratorUrl,
description, logo_path
FROM collaborator
;
";
} else if ( $mode=="dd" ) {
// Retrieve output data dictionary
$sql="
SELECT col_name, ordinal_position, data_type, description
FROM dd_output
ORDER BY ordinal_position
;
";
} else {
$err_msg="ERROR: Unknown opt mode '$mode'";
$err_code=400; goto err;
}
// Run the query and save results as $results_array
include("qy_db.php");
} // END mode_if
///////////////////////////////////
// Send the response
///////////////////////////////////
// Send the header
// header("Access-Control-Allow-Origin: *");
// header('Content-type: application/json');
// Send data
$results_json = json_encode($results_array);
send_response($err_code, $results_json);
exit;
///////////////////////////////////
// Error: return http status code
// and error message
///////////////////////////////////
err:
$err_json = json_encode($err_msg);
send_response($err_code, $err_json);
?>