This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
129 lines (129 loc) · 3.86 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
<?php
class api
{
//used to limit the results returned by a query
const LIMIT = 10;
//maximum allowed keywords
const MAXKEYWORD = 10;
public static function main( $path = NULL, $db = NULL)
{
if(is_null($path))
{
require_once('parse.php');
$path = Parse::parsepath();
}
if(is_null($db))
{
$settings = json_decode(file_get_contents('settings.json'), true);
//dateabase
$db = new PDO($settings['database']['driver'].':'.
'host=' .$settings['database']['host'].';'.
'dbname=' .$settings['database']['name'].';'.
'charset='.$settings['database']['charset'],
$settings['database']['username'],
$settings['database']['password']
);
}
if (isset($path[1]))
{
switch($path[1])
{
case 'search':
$limit = self::LIMIT;
$input = urldecode($path[2]);
//exact match
$stmt = $db->prepare('SELECT `cardno`, `name` FROM
`ws_cards` m1 WHERE `name` LIKE ? AND `cardno` =
(SELECT MIN(m2.cardno) FROM `ws_cards` m2 WHERE m1.name = m2.name)
ORDER BY m1.name LIMIT '.$limit);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute(array($input.'%'));
$results = $stmt->fetchAll();
$limit -= count($results);
$keywords = explode(' ', $input);
if (count($keywords) > self::MAXKEYWORD)
$keywords = array_splice($keywords, 0, self::MAXKEYWORD);
if(is_array($keywords))
$keywords = array_map(function($v) { return '%'.$v.'%'; }, $keywords);
//all keywords
if($limit > 0 && count($keywords) > 1)
{
$query = 'SELECT `cardno`, `name` FROM `ws_cards` m1 WHERE ';
for ($i=0; $i < count($keywords); $i++) {
$query .= '`name` LIKE ? AND ';
}
$query .= '`cardno` = (SELECT MIN(m2.cardno) FROM
`ws_cards` m2 WHERE m1.name = m2.name)';
if(count($results) >= 1)
{
$query .= ' AND `cardno` NOT IN (?';
for ($i=1; $i < count($results); $i++) {
$query .= ', ?';
}
$query .= ')';
}
$query .= 'ORDER BY m1.name LIMIT '.$limit;
$stmt = $db->prepare($query);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$param = array_merge($keywords,
array_map(function($v){
return $v['cardno'];
}, $results));
$stmt->execute($param);
$results = array_merge($results, $stmt->fetchAll());
$limit = self::LIMIT - count($results);
}
//any keywords
if ($limit > 0)
{
$query = 'SELECT `cardno`, `name` FROM `ws_cards` m1 WHERE `name` LIKE ? ';
for ($i=1; $i < count($keywords); $i++) {
$query .= 'OR `name` LIKE ? ';
}
$query .= ' AND `cardno` = (SELECT MIN(m2.cardno) FROM
`ws_cards` m2 WHERE m1.name = m2.name)';
if(count($results) >= 1)
{
$query .= ' AND `cardno` NOT IN (?';
for ($i=1; $i < count($results); $i++) {
$query .= ', ?';
}
$query .= ')';
}
$query .= 'ORDER BY m1.name LIMIT '.$limit;
$stmt = $db->prepare($query);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$param = array_merge($keywords,
array_map(function($v){
return $v['cardno'];
}, $results));
$stmt->execute($param);
$results = array_merge($results, $stmt->fetchAll());
}
echo json_encode($results);
break;
case 'username':
if (!isset($_POST['username']))
{
echo 'false';
break;
}
if (strlen($_POST['username']) < 3) {
echo 'false';
break;
}
$stmt = $db->prepare('SELECT username FROM ws_users WHERE username = ?');
$stmt->execute(array($_POST['username']));
if ($stmt->fetch(PDO::FETCH_ASSOC))
{
echo 'false';
}
else
{
echo 'true';
}
break;
}
}
}
}