forked from hypercities/hypercities
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueryCollectionName.php
More file actions
131 lines (109 loc) · 3.76 KB
/
queryCollectionName.php
File metadata and controls
131 lines (109 loc) · 3.76 KB
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
<?php
/**
* Return Array of tuple {id, name, ownerId} of matched result
* If func is set to 'k', returns the collections which's name contains key
* If func is set to 'u', returns the collections that owned by current user
* If func is set to 'p', returns the collections which's parent of cid
*/
include_once("includes/serverSession.inc");
include_once("includes/dbUtil.inc");
include_once("includes/user.inc");
// Start session
cServerSession::start();
HC_checkReferer();
//test case
/*
$_GET['func'] = "k";
$_GET['key'] = "berling";
$_GET['cid'] = 9860;
*/
$HC_GET = HC_sanitizeInput($_GET,
array('func'=>'str', 'key'=>'str', 'cid'=>'int'),
array('func'), array('key', 'cid'));
$queryType = $HC_GET['func'];
$objectId = $HC_GET['cid'];
$keyword = $HC_GET['key'];
$userId = cServerSession::getUserId();
try {
$result = array();
$baseCollectionId = NULL;
if (cServerSession::getVar("baseCollection")) {
$baseCollectionId = cServerSession::getVar("baseCollection");
// load from database
$rsc = mysql_query("SELECT id, title, owner_id, object_state_id"
." FROM objects WHERE id = $baseCollectionId;"
) or die("Could not get $baseCollectionId: ".mysql_error());
if ($rsc) {
$collection = mysql_fetch_array($rsc);
}
// If the user is the collection's owner, don't include it here, because
// it will be included in the regular search
//
if ($collection['owner_id'] != cServerSession::getUserId()
|| $collection['object_state_id'] == 1) {
// append to results
$result[] = array(
'id' => $collection['id'],
'title' => "<b>" . $collection['title'] . "</b>",
'owner' => $collection['owner_id'],
);
}
unset($collection);
}
// If key is given, always query by key
if (!strncmp($queryType, "k", 1) && $keyword) {
$resultCollections = searchCollections("%$keyword%");
// trim whitespaces in collection name
foreach ($resultCollections as &$collection) {
if ($collection['id'] == $baseCollectionId) continue;
// Hiehlight search keyword
$title = preg_replace("/($keyword)/i",
"<b>\${1}</b>",
trim($collection['title']));
if ($collection['id'] == $baseCollectionId) $title = "<b>".$title."</b>";
$result[] = array('id' => $collection['id'],
'title' => $title,
'owner' => $collection['owner_id']);
}
unset($collection);
unset($title);
}
// If user is login, query user's owned collection
else if (!strncmp($queryType, "u", 1) && $userId) {
$myCollections = getUserCollections($userId);
// trim whitespaces in collection name
foreach ($myCollections as &$collection) {
$result[] = array('id' => $collection['id'],
'title' => trim($collection['title']),
'owner' => $collection['owner_id'],
'state' => $collection['object_state_id'],
'hasPwd' => !empty($collection['password']));
}
unset($collection);
}
// If objectId is given, query object's parent collection
else if (!strncmp($queryType, "p", 1) && $objectId) {
$parentCollections = getParentCollections($objectId);
// trim whitespaces in collection name
foreach ($parentCollections as &$collection) {
if ($collection['id'] == $baseCollectionId) continue;
$result[] = array('id' => $collection['id'],
'title' => trim($collection['title']),
'owner' => $collection['owner_id'],
'state' => $collection['object_state_id'],
'hasPwd' => !empty($collection['password']));
}
unset($collection);
}
header('Content-type: application/json');
echo json_encode(array("collections" => $result));
} catch (MysqlException $e) {
$message = 'Caught exception: '.$e->getMessage();
HC_errorLog($message);
HC_reportDBError("query collection names");
} catch (Exception $e) {
$message = 'Caught exception: '.$e->getMessage();
HC_errorLog($message);
HC_reportGeneralError("query collection names");
}
?>