forked from OpenCorpora/opencorpora
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.php
55 lines (49 loc) · 1.78 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
<?php
require_once('lib/header_ajax.php');
require_once('lib/lib_annot.php');
require_once('lib/lib_books.php');
header('Content-type: application/json');
define('API_VERSION', '0.3');
$action = $_GET['action'];
$answer = array(
'api_version' => API_VERSION,
'answer' => null,
'error' => null
);
function json_encode_readable($arr)
{
//convmap since 0x80 char codes so it takes all multibyte codes (above ASCII 127). So such characters are being "hidden" from normal json_encoding
array_walk_recursive($arr, function (&$item, $key) { if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8'); });
return mb_decode_numericentity(json_encode($arr), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
}
switch ($action) {
case 'search':
if (isset($_GET['all_forms']))
$all_forms = (bool)$_GET['all_forms'];
else
$all_forms = false;
$answer['answer'] = get_search_results($_GET['query'], !$all_forms);
foreach ($answer['answer']['results'] as &$res) {
$parts = array();
foreach (get_book_parents($res['book_id'], true) as $p) {
$parts[] = $p['title'];
}
$res['text_fullname'] = join(': ', array_reverse($parts));
}
break;
case 'login':
include_once('lib/lib_users.php');
$user_id = user_check_password($_POST['login'], $_POST['password']);
if ($user_id) {
$token = remember_user($user_id, false, false);
$answer['answer'] = array('token' => $token);
}
else
$answer['error'] = 'Incorrect login or password';
break;
default:
$answer['error'] = 'Unknown action';
}
log_timing();
die(json_encode_readable($answer));
?>