-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathautocomplete.php
48 lines (39 loc) · 968 Bytes
/
autocomplete.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
<?php
require_once 'utils/bm_database.php';
require 'stats/utils/stats_utils.php';
function response($arr)
{
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($arr);
exit;
}
if(!isset($_COOKIE[session_name()]))
{
response([]);
}
$field = $_GET['field'] ?? null;
$term = $_GET['term'] ?? null;
$min_len_config = [ 'genre' => 2, 'authors' => 3, 'title' => 3 ];
$min_len = $min_len_config[$field];
if($min_len == null)
response([]);
$term = trim($term);
$term_len = mb_strlen($term);
if($term_len < $min_len || $term_len > 3000)
response([]);
$bm_db = new bm_database((new stats_utils(stats_utils::bm_actual_edition))->get_bm_db_filepath());
$ret_vals = [];
switch($field)
{
case 'genre':
$ret_vals = $bm_db->get_first_genres($term);
break;
case 'authors':
$ret_vals = $bm_db->get_first_authors($term);
break;
case 'title':
$ret_vals = $bm_db->get_first_titles($term);
break;
}
response($ret_vals);
?>