Skip to content

Commit a4e1bb8

Browse files
perform extra checks to ensure user/resource exists before trying to pull up resources by user/updates by resource; also check if listResources has any results and if the array is empty (i.e. no more results) then send a 4
04 instead of a plain empty array off to infinity; closes #48.
1 parent 87cb9b0 commit a4e1bb8

File tree

1 file changed

+77
-25
lines changed

1 file changed

+77
-25
lines changed

src/support/Database.php

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ public static function initializeViaConfig() {
3939
}
4040

4141
public function listResources($category, $page) {
42-
$page = $page == 1 ? 0 : 10 * ($page - 1);
42+
if ($page <= 0) {
43+
return NULL;
44+
}
45+
46+
$offset = $page == 1 ? 0 : 10 * ($page - 1);
4347

4448
if (!is_null($this->conn)) {
4549
$categoryClause = is_null($category) ? '' : 'AND r.resource_category_id = :resource_category_id';
4650

4751
$resStmt = $this->conn->prepare($this->_resource(sprintf('%s LIMIT 10 OFFSET :offset', $categoryClause)));
48-
$resStmt->bindParam(':offset', $page, \PDO::PARAM_INT);
52+
$resStmt->bindParam(':offset', $offset, \PDO::PARAM_INT);
4953

5054
if (!empty($categoryClause)) {
5155
$resStmt->bindParam(':resource_category_id', $category);
@@ -54,6 +58,10 @@ public function listResources($category, $page) {
5458
if ($resStmt->execute()) {
5559
$resources = $resStmt->fetchAll();
5660

61+
if (is_null($resources) || $resources == false || empty($resources)) {
62+
return NULL;
63+
}
64+
5765
for ($i = 0; $i < count($resources); $i++) {
5866
$resource = $resources[$i];
5967
$resource['fields'] = $this->_resource_fields($resource['resource_id']);
@@ -74,7 +82,11 @@ public function getResource($resource_id) {
7482

7583
if ($resStmt->execute()) {
7684
$resource = $resStmt->fetch();
77-
$resource['fields'] = $this->_resource_fields($resource['resource_id']);
85+
86+
if (!is_null($resource) && $resource !== false) {
87+
$resource['fields'] = $this->_resource_fields($resource['resource_id']);
88+
}
89+
7890
return $resource;
7991
}
8092
}
@@ -83,12 +95,16 @@ public function getResource($resource_id) {
8395
}
8496

8597
public function getResourcesByUser($user_id, $page) {
86-
$page = $page == 1 ? 0 : 10 * ($page - 1);
98+
if ($page <= 0 || !$this->_user_exists($user_id)) {
99+
return NULL;
100+
}
87101

102+
$offset = $page == 1 ? 0 : 10 * ($page - 1);
103+
88104
if (!is_null($this->conn)) {
89105
$resStmt = $this->conn->prepare($this->_resource('AND r.user_id = :user_id LIMIT 10 OFFSET :offset'));
90106
$resStmt->bindParam(':user_id', $user_id);
91-
$resStmt->bindParam(':offset', $page, \PDO::PARAM_INT);
107+
$resStmt->bindParam(':offset', $offset, \PDO::PARAM_INT);
92108

93109
if ($resStmt->execute()) {
94110
$resources = $resStmt->fetchAll();
@@ -132,12 +148,16 @@ public function getResourceUpdate($update_id) {
132148
}
133149

134150
public function getResourceUpdates($resource_id, $page) {
135-
$page = $page == 1 ? 0 : 10 * ($page - 1);
151+
if ($page <= 0 || !$this->_resource_exists($resource_id)) {
152+
return NULL;
153+
}
154+
155+
$offset = $page == 1 ? 0 : 10 * ($page - 1);
136156

137157
if (!is_null($this->conn)) {
138158
$updatesStmt = $this->conn->prepare($this->_resource_update('AND r.resource_id = :resource_id LIMIT 10 OFFSET :offset'));
139159
$updatesStmt->bindParam(':resource_id', $resource_id);
140-
$updatesStmt->bindParam(':offset', $page, \PDO::PARAM_INT);
160+
$updatesStmt->bindParam(':offset', $offset, \PDO::PARAM_INT);
141161

142162
if ($updatesStmt->execute()) {
143163
return $updatesStmt->fetchAll();
@@ -157,26 +177,32 @@ public function getUser($user_id) {
157177
WHERE u.user_id = :user_id
158178
GROUP BY u.user_id"
159179
);
160-
161180
$userStmt->bindParam(':user_id', $user_id);
162181

163-
$identStmt = $this->conn->prepare(
164-
"SELECT ufv.field_id, ufv.field_value
165-
FROM xf_user_field_value ufv
166-
INNER JOIN xf_user u
167-
ON u.user_id = ufv.user_id
168-
INNER JOIN xf_user_field uf
169-
ON uf.field_id = ufv.field_id AND uf.display_group = 'contact'
170-
WHERE ufv.user_id = :user_id AND ufv.field_value IS NOT NULL AND ufv.field_value != ''"
171-
);
172-
173-
$identStmt->bindParam(':user_id', $user_id);
174-
175-
if ($userStmt->execute() && $identStmt->execute()) {
176-
$out = new \stdClass();
177-
$out->user = $userStmt->fetch();
178-
$out->ident = $identStmt->fetchAll();
179-
return $out;
182+
if ($userStmt->execute()) {
183+
$user = $userStmt->fetch();
184+
if (!is_null($user) && $user != false) {
185+
$identStmt = $this->conn->prepare(
186+
"SELECT ufv.field_id, ufv.field_value
187+
FROM xf_user_field_value ufv
188+
INNER JOIN xf_user u
189+
ON u.user_id = ufv.user_id
190+
INNER JOIN xf_user_field uf
191+
ON uf.field_id = ufv.field_id AND uf.display_group = 'contact'
192+
WHERE ufv.user_id = :user_id AND ufv.field_value IS NOT NULL AND ufv.field_value != ''"
193+
);
194+
$identStmt->bindParam(':user_id', $user_id);
195+
196+
$identities = new \stdClass();
197+
if ($identStmt->execute()) {
198+
$identities = $identStmt->fetchAll();
199+
}
200+
201+
$out = new \stdClass();
202+
$out->user = $userStmt->fetch();
203+
$out->ident = $identities;
204+
return $out;
205+
}
180206
}
181207
}
182208

@@ -243,4 +269,30 @@ private function _resource_update($suffix) {
243269
$suffix
244270
);
245271
}
272+
273+
private function _resource_exists($resource_id) {
274+
if (!is_null($this->conn)) {
275+
$stmt = $this->conn->prepare("SELECT EXISTS(SELECT 1 FROM xf_resource WHERE resource_id = :resource_id) AS 'exists'");
276+
$stmt->bindParam(":resource_id", $resource_id);
277+
278+
if ($stmt->execute()) {
279+
return (bool) $stmt->fetch()['exists'];
280+
}
281+
}
282+
283+
return FALSE;
284+
}
285+
286+
private function _user_exists($user_id) {
287+
if (!is_null($this->conn)) {
288+
$stmt = $this->conn->prepare("SELECT EXISTS(SELECT 1 FROM xf_user WHERE user_id = :user_id) AS 'exists'");
289+
$stmt->bindParam(":user_id", $user_id);
290+
291+
if ($stmt->execute()) {
292+
return (bool) $stmt->fetch()['exists'];
293+
}
294+
}
295+
296+
return FALSE;
297+
}
246298
}

0 commit comments

Comments
 (0)