forked from chaosarium/lwt
-
Notifications
You must be signed in to change notification settings - Fork 20
/
api.php
909 lines (842 loc) · 27.3 KB
/
api.php
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
<?php
namespace Lwt\Ajax;
require_once __DIR__ . '/inc/session_utility.php';
require_once __DIR__ . '/inc/simterms.php';
require_once 'do_test_test.php';
require_once __DIR__ . '/inc/ajax_add_term_transl.php';
require_once __DIR__ . '/inc/ajax_check_regexp.php';
require_once __DIR__ . '/inc/ajax_chg_term_status.php';
require_once __DIR__ . '/inc/ajax_save_impr_text.php';
require_once __DIR__ . '/inc/ajax_save_text_position.php';
require_once __DIR__ . '/inc/ajax_show_imported_terms.php';
require_once __DIR__ . '/inc/ajax_edit_impr_text.php';
require_once __DIR__ . '/inc/langdefs.php';
/**
* @var string Version of this current LWT API.
*/
define('LWT_API_VERSION', "0.1.1");
/**
* @var string Date of the last released change of the LWT API.
*/
define('LWT_API_RELEASE_DATE', "2023-12-29");
/**
* Send JSON response and exit.
*
* @param int $status Status code to display
* @param mixed $data Any data to return
*
* @return never
*/
function send_response($status = 200, $data = null)
{
header('Content-Type: application/json');
http_response_code($status);
echo json_encode($data);
exit;
}
/**
* Check if an API endpoint exists.
*
* @param string $method Method name (e.g. 'GET' or 'POST')
* @param string $requestURI The URI being requested.
*
* @return string The first matching endpoint
*/
function endpoint_exits($method, $requestUri)
{
// Set up API endpoints
$endpoints = [
'languages' => [ 'GET' ],
//'languages/(?<lang-id>\d+)/reading-configuration' => [ 'GET' ],
'media-files' => [ 'GET' ],
'phonetic-reading' => [ 'GET' ],
'review/next-word' => [ 'GET' ],
'review/tomorrow-count' => [ 'GET' ],
'sentences-with-term' => [ 'GET' ],
//'sentences-with-term/(?<term-id>\d+)' => [ 'GET' ],
'similar-terms' => [ 'GET' ],
'settings' => [ 'POST' ],
'settings/theme-path' => [ 'GET' ],
'terms' => [ 'GET', 'POST' ],
'terms/imported' => [ 'GET' ],
'terms/new' => [ 'POST' ],
//'terms/(?<term-id>\d+)/translations' => [ 'GET', 'POST' ],
//'terms/(?<term-id>\d+)/status/down' => [ 'POST' ],
//'terms/(?<term-id>\d+)/status/up' => [ 'POST' ],
//'terms/(?<term-id>\d+)/status/(?<new-status>\d+)' => [ 'POST' ],
'texts' => [ 'POST' ],
//'texts/(?<text-id>\d+)/annotation' => [ 'POST' ],
//'texts/(?<text-id>\d+)/audio-position' => [ 'POST' ],
//'texts/(?<text-id>\d+)/reading-position' => [ 'POST' ],
'texts/statistics' => [ 'GET' ],
'version' => [ 'GET' ],
// 'regexp/test' => [ 'POST' ], as of LWT 2.9.0, no usage was found
];
// Extract requested endpoint from URI
$uri_query = parse_url($requestUri, PHP_URL_PATH);
$matching = preg_match('/(.*?\/api\.php\/v\d\/).+/', $uri_query, $matches);
if (!$matching) {
send_response(400, ['error' => 'Unrecognized URL format ' . $uri_query]);
}
if (count($matches) == 0) {
send_response(404, ['error' => 'Wrong API Location: ' . $uri_query]);
}
// endpoint without prepending URL, like 'version'
$req_endpoint = rtrim(str_replace($matches[1], '', $uri_query), '/');
$methods_allowed = array();
if (array_key_exists($req_endpoint, $endpoints)) {
$methods_allowed = $endpoints[$req_endpoint];
} else {
$first_elem = preg_split('/\//', $req_endpoint)[0];
if (array_key_exists($first_elem, $endpoints)) {
$methods_allowed = $endpoints[$first_elem];
} else {
send_response(404, ['error' => 'Endpoint Not Found: ' . $req_endpoint]);
}
}
// Validate request method for the req_endpoint
if (!in_array($method, $methods_allowed)) {
send_response(405, ['error' => 'Method Not Allowed']);
}
return $req_endpoint;
}
// -------------------------- GET REQUESTS -------------------------
/**
* Return the API version.
*
* @param array $get_req GET request, unnused
*
* @return string[] JSON-encoded version
*
* @psalm-return array{version: '0.1.1', release_date: '2023-12-29'}
*/
function rest_api_version($get_req): array
{
return array(
"version" => LWT_API_VERSION,
"release_date" => LWT_API_RELEASE_DATE
);
}
/**
* List the audio and video files in the media folder.
*
* @param array $get_req Unnused
*
* @return string[] Path of media files
*/
function media_files($get_req)
{
return get_media_paths();
}
/**
* The way text should be read
*/
function readingConfiguration($get_req): array
{
global $tbpref;
// language, voiceAPI, abbr
$req = do_mysqli_query(
"SELECT LgName, LgTTSVoiceAPI, LgRegexpWordCharacters FROM {$tbpref}languages
WHERE LgID = " . $get_req["lang_id"]
);
$record = mysqli_fetch_assoc($req);
$abbr = getLanguageCode($get_req["lang_id"], LWT_LANGUAGES_ARRAY);
if ($record["LgTTSVoiceAPI"] != '') {
$readingMode = "external";
} elseif ($record["LgRegexpWordCharacters"] == "mecab") {
$readingMode = "internal";
} else {
$readingMode = "direct";
}
return array(
"name" => $record["LgName"],
"voiceapi" => $record["LgTTSVoiceAPI"],
"word_parsing" => $record["LgRegexpWordCharacters"],
"abbreviation" => $abbr,
"reading_mode" => $readingMode
);
}
/**
* Get the phonetic reading of a word based on it's language.
*
* @param array $get_req Array with the fields "text" and "lang" (short language name)
*
* @return string[] JSON-encoded result
*
* @psalm-return array{phonetic_reading: string}
*
* @since 2.10.0-fork Can also accept a language ID with "lgid" parameter
*/
function get_phonetic_reading($get_req): array
{
if (array_key_exists("lang_id", $get_req)) {
$data = phoneticReading($get_req['text'], $get_req['lang_id']);
} else {
$data = phonetic_reading($get_req['text'], $get_req['lang']);
}
return array("phonetic_reading" => $data);
}
/**
* Retun the next word to test as JSON
*
* @param string $testsql SQL projection query
* @param bool $word_mode Test is in word mode
* @param int $lgid Language ID
* @param string $wordregex Word selection regular expression
* @param int $testtype Test type
*
* @return (int|mixed|string)[] Next word formatted as an array.
*
* @psalm-return array{word_id: 0|mixed, solution?: string, word_text: string, group: string}
*/
function get_word_test_ajax($testsql, $word_mode, $lgid, $wordregex, $testtype): array
{
$word_record = do_test_get_word($testsql);
if (empty($word_record)) {
$output = array(
"word_id" => 0,
"word_text" => '',
"group" => ''
);
return $output;
}
if ($word_mode) {
// Word alone
$sent = "{" . $word_record['WoText'] . "}";
} else {
// Word surrounded by sentence
list($sent, $_) = do_test_test_sentence(
$word_record['WoID'],
$lgid,
$word_record['WoTextLC']
);
if ($sent === null) {
$sent = "{" . $word_record['WoText'] . "}";
}
}
list($html_sentence, $save) = do_test_get_term_test(
$word_record,
$sent,
$testtype,
$word_mode,
$wordregex
);
$solution = get_test_solution($testtype, $word_record, $word_mode, $save);
return array(
"word_id" => $word_record['WoID'],
"solution" => $solution,
"word_text" => $save,
"group" => $html_sentence
);
}
/**
* Return the next word to test.
*
* @param array $get_req Array with the fields {
* test_key: string, selection: string, word_mode: bool,
* lg_id: int, word_regex: string, type: int
* }
*
* @return array Next word formatted as JSON.
*/
function word_test_ajax($get_req): array
{
$test_sql = do_test_test_get_projection(
$get_req['test_key'],
$get_req['selection']
);
return get_word_test_ajax(
$test_sql,
filter_var($get_req['word_mode'], FILTER_VALIDATE_BOOLEAN),
$get_req['lg_id'],
$get_req['word_regex'],
$get_req['type']
);
}
/**
* Return the number of reviews for tomorrow by using the suplied query.
*
* @param array $get_req Array with the fields "test_key" and "selection"
*
* @return array JSON-encoded result
*
* @psalm-return array{count: int}
*/
function tomorrow_test_count($get_req): array
{
$test_sql = do_test_test_get_projection(
$get_req['test_key'],
$get_req['selection']
);
$output = array(
"count" => do_test_get_tomorrow_tests_count($test_sql)
);
return $output;
}
/**
* Get the file path using theme.
*
* @param array $get_req Get request with field "path", relative filepath using theme.
*
* @return array JSON-encoded result
*
* @psalm-return array{theme_path: string}
*/
function get_theme_path($get_req): array
{
return array("theme_path" => get_file_path($get_req['path']));
}
/**
* Return statistics about a group of text.
*
* @param array $get_req Get request with field "texts_id", texts ID.
*/
function get_texts_statistics($get_req): array
{
return return_textwordcount($get_req["texts_id"]);
}
/**
* Sentences containing an input word.
*
* @param array $get_req Get request with fields "lg_id", "word_lc" and "word_id".
*/
function sentences_with_registred_term($get_req): array
{
return sentences_with_word(
(int) $get_req["lg_id"],
$get_req["word_lc"],
(int) $get_req["word_id"]
);
}
/**
* Return the example sentences containing an input word.
*
* @param array $get_req Get request with fields "lg_id" and "advanced_search" (optional).
*/
function sentences_with_new_term($get_req): array
{
$advanced = null;
if (array_key_exists("advanced_search", $get_req)) {
$advanced = -1;
}
return sentences_with_word(
(int) $get_req["lg_id"],
$get_req["word_lc"],
$advanced
);
}
/**
* Get terms similar to a given term.
*
* @param array $get_req Get request with fields "lg_id" and "term".
*
* @return array Similar terms in HTML format.
*
* @psalm-return array{similar_terms: string}
*/
function similar_terms($get_req): array
{
return array("similar_terms" => print_similar_terms(
(int)$get_req["lg_id"],
(string) $get_req["term"]
));
}
/**
* Return the list of imported terms.
*
* @param array $get_req Get request with fields "last_update", "page" and "count".
*
* @return array
*/
function imported_terms($get_req)
{
return imported_terms_list(
$get_req["last_update"],
$get_req["page"],
$get_req["count"]
);
}
/**
* Translations for a term to choose an annotation.
*
* @param array $get_req Get request with fields "text_id" and "term_lc".
*/
function term_translations($get_req): array
{
return \Lwt\Ajax\Improved_Text\get_term_translations(
(string)$get_req["term_lc"],
(int)$get_req["text_id"]
);
}
/**
* Error message when the provided action_type does not match anything known.
*
* @param array $post_req GET request used
* @param bool $action_exists Set to true if the action is recognized but not
* the action_type
*
* @return array JSON-encoded error message.
*
* @psalm-return array{error: string}
*/
function unknown_get_action_type($get_req, $action_exists = false): array
{
if ($action_exists) {
$message = 'action_type with value "' . $get_req["action_type"] .
'" with action "' . $get_req["action"] . '" does not exist!';
} else {
$message = 'action_type with value "' . $get_req["action_type"] .
'" with default action (' . $get_req["action"] . ') does not exist';
}
return array("error" => $message);
}
// --------------------------------- POST REQUESTS ---------------------
/**
* Save a setting to the database.
*
* @param array $post_req Array with the fields "key" (setting name) and "value"
*
* @return string[] Setting save status
*
* @psalm-return array{error?: string, message?: string}
*/
function save_setting($post_req): array
{
$status = saveSetting($post_req['key'], $post_req['value']);
$raw_answer = array();
if (str_starts_with($status, "OK: ")) {
$raw_answer["message"] = substr($status, 4);
} else {
$raw_answer["error"] = $status;
}
return $raw_answer;
}
/**
* Save the annotation for a term.
*
* @param array $post_req Post request with keys "text_id", "elem" and "data".
*
* @return string[] JSON-encoded result
*
* @psalm-return array{save_impr_text?: string, error?: string}
*/
function set_annotation($post_req): array
{
$result = save_impr_text(
(int)$post_req["text_id"],
$post_req['elem'],
json_decode($post_req['data'])
);
$raw_answer = array();
if (array_key_exists("error", $result)) {
$raw_answer["error"] = $result["error"];
} else {
$raw_answer["save_impr_text"] = $result["success"];
}
return $raw_answer;
}
/**
* Set audio position.
*
* @param array $post_req Array with the fields "text_id" (int) and "position"
*
* @return string[] Success message
*
* @psalm-return array{audio: 'Audio position set'}
*/
function set_audio_position($post_req): array
{
save_audio_position(
(int)$post_req["text_id"],
(int)$post_req["position"]
);
return array(
"audio" => "Audio position set"
);
}
/**
* Set text reading position.
*
* @param array $post_req Array with the fields "text_id" (int) and "position"
*
* @return string[] Success message
*
* @psalm-return array{text: 'Reading position set'}
*/
function set_text_position($post_req): array
{
save_text_position(
(int)$post_req["text_id"],
(int)$post_req["position"]
);
return array("text" => "Reading position set");
}
/**
* Change the status of a term by one unit.
*
* @param array $post_req Array with the fields "term_id" (int) and "status_up" (1 or 0)
*
* @return string[] Status message
*
* @psalm-return array{increment?: string, error?: ''}
*/
function increment_term_status($post_req): array
{
$result = ajax_increment_term_status(
(int)$post_req['term_id'],
(bool)$post_req['status_up']
);
$raw_answer = array();
if ($result == '') {
$raw_answer["error"] = '';
} else {
$raw_answer["increment"] = $result;
}
return $raw_answer;
}
/**
* Set the status of a term.
*
* @param array $post_req Array with the fields "term_id" (int) and "status" (0-5|98|99)
*
* @return (int|string)[]
*
* @psalm-return array{error?: string, set?: int}
*/
function set_term_status($post_req): array
{
$result = set_word_status((int)$post_req['term_id'], (int)$post_req['status']);
$raw_answer = array();
if (is_numeric($result)) {
$raw_answer["set"] = (int)$result;
} else {
$raw_answer["error"] = $result;
}
return $raw_answer;
}
/**
* Edit the translation of an existing term.
*
* @param array $post_req Array with the fields "term_id" (int) and "translation".
*
* @return string[] Term in lower case, or "" if term does not exist
*
* @psalm-return array{update?: string, error?: string}
*/
function update_translation($post_req): array
{
$result = do_ajax_check_update_translation(
(int)$post_req['term_id'],
trim($post_req['translation'])
);
$raw_answer = array();
if (str_starts_with($result, "Error")) {
$raw_answer["error"] = $result;
} else {
$raw_answer["update"] = $result;
}
return $raw_answer;
}
/**
* Create the translation for a new term.
*
* @param array $post_req Array with the fields "term_text", "lg_id" (int) and "translation".
*
* @return (int|string)[] Error message in case of failure, lowercase term otherwise
*
* @psalm-return array{error?: string, add?: string, term_id?: mixed, term_lc?: mixed}
*/
function add_translation($post_req): array
{
$text = trim($post_req['term_text']);
$result = add_new_term_transl(
$text,
(int)$post_req['lg_id'],
trim($post_req['translation'])
);
$raw_answer = array();
if (is_array($result)) {
$raw_answer["term_id"] = (int) $result[0];
$raw_answer["term_lc"] = (string) $result[1];
} elseif ($result == mb_strtolower($text, 'UTF-8')) {
$raw_answer["add"] = $result;
} else {
$raw_answer["error"] = $result;
}
return $raw_answer;
}
/**
* Notify of an error on POST method.
*
* @param array $post_req POST request used
* @param bool $action_exists Set to true if the action is recognized but not
* the action_type
*
* @return string[] JSON-encoded error message
*
* @psalm-return array{error: string}
*/
function unknown_post_action_type($post_req, $action_exists = false): array
{
if ($action_exists) {
$message = 'action_type with value "' . $post_req["action_type"] .
'" with action "' . $post_req["action"] . '" does not exist!';
} else {
$message = 'action_type with value "' . $post_req["action_type"] .
'" with default action (' . $post_req["action"] . ') does not exist';
}
return array("error" => $message);
}
/**
* Main handler for any provided request, while answer the result.
*
* @param string $method Method name (e.g. 'GET' or 'POST')
* @param string $requestURI The URI being requested.
* @param array|null $post_param Post arguments, usually equal to $_POST
*
* @return never
*/
function request_handler($method, $requestUri, $post_param)
{
// Extract requested endpoint from URI
$req_endpoint = endpoint_exits($method, $requestUri);
$endpoint_fragments = preg_split("/\//", $req_endpoint);
// Process endpoint request
if ($method === 'GET') {
// Handle GET request for each endpoint
$uri_query = parse_url($requestUri, PHP_URL_QUERY);
if ($uri_query == null) {
$req_param = array();
} else {
parse_str($uri_query, $req_param);
}
switch ($endpoint_fragments[0]) {
case 'languages':
if (ctype_digit($endpoint_fragments[1])) {
if ($endpoint_fragments[2] == 'reading-configuration') {
$req_param['lang_id'] = (int) $endpoint_fragments[1];
$answer = readingConfiguration($req_param);
send_response(200, $answer);
} else {
send_response(
404,
['error' => 'Expected "reading-configuration", Got ' .
$endpoint_fragments[2]]
);
}
} else {
send_response(
404,
['error' => 'Expected Language ID, found ' .
$endpoint_fragments[1]]
);
}
break;
case 'media-files':
$answer = media_files($req_param);
send_response(200, $answer);
break;
case 'phonetic-reading':
$answer = get_phonetic_reading($req_param);
send_response(200, $answer);
break;
case 'review':
switch ($endpoint_fragments[1]) {
case 'next-word':
$answer = word_test_ajax($req_param);
send_response(200, $answer);
break;
case 'tomorrow-count':
$answer = tomorrow_test_count($req_param);
send_response(200, $answer);
break;
default:
send_response(
404,
['error' => 'Endpoint Not Found' .
$endpoint_fragments[1]]
);
}
break;
case 'sentences-with-term':
if (ctype_digit($endpoint_fragments[1])) {
$req_param['word_id'] = (int) $endpoint_fragments[1];
$answer = sentences_with_registred_term($req_param);
} else {
$answer = sentences_with_new_term($req_param);
}
send_response(200, $answer);
break;
case 'similar-terms':
$answer = similar_terms($req_param);
send_response(200, $answer);
break;
case 'settings':
switch ($endpoint_fragments[1]) {
case 'theme-path':
$answer = get_theme_path($req_param);
send_response(200, $answer);
break;
default:
send_response(
404,
['error' => 'Endpoint Not Found: ' .
$endpoint_fragments[1]]
);
}
break;
case 'terms':
if ($endpoint_fragments[1] == "imported") {
$answer = imported_terms($req_param);
send_response(200, $answer);
} elseif (ctype_digit($endpoint_fragments[1])) {
if ($endpoint_fragments[2] == 'translations') {
$req_param['term_id'] = $endpoint_fragments[1];
$answer = term_translations($req_param);
send_response(200, $answer);
} else {
send_response(
404,
['error' => 'Expected "translation", Got ' .
$endpoint_fragments[2]]
);
}
} else {
send_response(
404,
['error' => 'Endpoint Not Found' .
$endpoint_fragments[1]]
);
}
break;
case 'texts':
if ($endpoint_fragments[1] == 'statistics') {
$answer = get_texts_statistics($req_param);
send_response(200, $answer);
} else {
send_response(
404,
['error' => 'Expected "statistics", Got ' .
$endpoint_fragments[1]]
);
}
break;
case 'version':
$answer = rest_api_version($req_param);
send_response(200, $answer);
break;
// Add more GET handlers for other endpoints
default:
send_response(
404,
['error' => 'Endpoint Not Found: ' .
$endpoint_fragments[0]]
);
}
} elseif ($method === 'POST') {
// Handle POST request for each endpoint
switch ($endpoint_fragments[0]) {
case 'settings':
$answer = save_setting($post_param);
send_response(200, $answer);
break;
case 'texts':
if (!ctype_digit($endpoint_fragments[1])) {
send_response(
404,
['error' => 'Text ID (Integer) Expected, Got ' .
$endpoint_fragments[1]]
);
}
$post_param["text_id"] = (int) $endpoint_fragments[1];
switch ($endpoint_fragments[2]) {
case 'annotation':
$answer = set_annotation($post_param);
send_response(200, $answer);
break;
case 'audio-position':
$answer = set_audio_position($post_param);
send_response(200, $answer);
break;
case 'reading-position':
$answer = set_text_position($post_param);
send_response(200, $answer);
break;
default:
send_response(
404,
['error' => 'Endpoint Not Found: ' .
$endpoint_fragments[2]]
);
}
break;
case 'terms':
if (ctype_digit($endpoint_fragments[1])) {
$post_param['term_id'] = (int) $endpoint_fragments[1];
if ($endpoint_fragments[2] == "status") {
if ($endpoint_fragments[3] == 'down') {
$post_param['status_up'] = 0;
$answer = increment_term_status($post_param);
send_response(200, $answer);
} elseif ($endpoint_fragments[3] == 'up') {
$post_param['status_up'] = 1;
$answer = increment_term_status($post_param);
send_response(200, $answer);
} elseif (ctype_digit($endpoint_fragments[3])) {
$post_param['status'] = (int) $endpoint_fragments[3];
$answer = set_term_status($post_param);
send_response(200, $answer);
} else {
send_response(
404,
['error' => 'Endpoint Not Found: ' .
$endpoint_fragments[3]]
);
}
} elseif ($endpoint_fragments[2] == 'translations') {
$answer = update_translation($post_param);
send_response(200, $answer);
} else {
send_response(
404,
[
'error' =>
'"status" or "translations"' .
' Expected, Got ' . $endpoint_fragments[2]
]
);
}
} elseif ($endpoint_fragments[1] == 'new') {
$answer = add_translation($post_param);
send_response(200, $answer);
} else {
send_response(
404,
[
'error' =>
'Term ID (Integer) or "new" Expected,' .
' Got ' . $endpoint_fragments[1]
]
);
}
break;
default:
send_response(
404,
['error' => 'Endpoint Not Found On POST: ' .
$endpoint_fragments[0]]
);
}
}
}
// Validate request method
if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== 'POST') {
send_response(405, ['error' => 'Method Not Allowed']);
} else {
request_handler(
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI'],
$_POST
);
}
?>