From 9505cb1f0bab26945a0cfcc38ef8938169c602f7 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 9 Jan 2019 20:06:02 +0100 Subject: [PATCH] Added more typesafe shorthands for fetching database operations. --- misuzu.php | 2 +- public/auth.php | 6 +++--- public/changelog.php | 5 ++--- public/forum/posting.php | 4 ++-- public/manage/changelog.php | 18 +++++++++--------- public/manage/forum.php | 2 +- public/manage/users.php | 16 ++++++++-------- public/members.php | 6 +++--- public/profile.php | 4 ++-- public/settings.php | 2 +- src/Forum/forum.php | 13 +++++-------- src/Forum/perms.php | 29 ++++++++--------------------- src/Forum/post.php | 8 +++----- src/Forum/topic.php | 5 ++--- src/Net/ip.php | 3 +-- src/Users/login_attempt.php | 3 +-- src/Users/relations.php | 4 +--- src/Users/session.php | 6 ++---- src/Users/warning.php | 6 ++---- src/audit_log.php | 4 +--- src/changelog.php | 2 +- src/chat_quotes.php | 11 ++++------- src/comments.php | 16 +++++++--------- src/db.php | 14 +++++++++++++- src/news.php | 14 ++++---------- src/perms.php | 30 ++++++++---------------------- 26 files changed, 95 insertions(+), 138 deletions(-) diff --git a/misuzu.php b/misuzu.php index a1cd8252..4e399d40 100644 --- a/misuzu.php +++ b/misuzu.php @@ -317,7 +317,7 @@ MIG; WHERE `user_id` = :user_id '); $getUserDisplayInfo->bindValue('user_id', $mszUserId); - $userDisplayInfo = $getUserDisplayInfo->execute() ? $getUserDisplayInfo->fetch(\PDO::FETCH_ASSOC) : []; + $userDisplayInfo = db_fetch($getUserDisplayInfo); if ($userDisplayInfo) { $userDisplayInfo['general_perms'] = perms_get_user(MSZ_PERMS_GENERAL, $userDisplayInfo['user_id']); diff --git a/public/auth.php b/public/auth.php index 5c6048c4..76cef1a5 100644 --- a/public/auth.php +++ b/public/auth.php @@ -82,7 +82,7 @@ switch ($authMode) { WHERE `user_id` = :user_id '); $getResetUser->bindValue('user_id', $resetUser); - $resetUser = $getResetUser->execute() ? $getResetUser->fetch(PDO::FETCH_ASSOC) : []; + $resetUser = db_fetch($getResetUser); if (empty($resetUser)) { header('Location: /auth.php?m=forgot'); @@ -156,7 +156,7 @@ switch ($authMode) { WHERE LOWER(`email`) = LOWER(:email) '); $forgotUser->bindValue('email', $authEmail); - $forgotUser = $forgotUser->execute() ? $forgotUser->fetch(PDO::FETCH_ASSOC) : []; + $forgotUser = db_fetch($forgotUser); if (empty($forgotUser)) { tpl_var('auth_forgot_error', 'This user is not registered with us.'); @@ -239,7 +239,7 @@ MSG; '); $getUser->bindValue('email', $authUsername); $getUser->bindValue('username', $authUsername); - $userData = $getUser->execute() ? $getUser->fetch() : []; + $userData = db_fetch($getUser); $userId = (int)($userData['user_id'] ?? 0); $loginFailedError = sprintf( diff --git a/public/changelog.php b/public/changelog.php index b5e88245..96d74d75 100644 --- a/public/changelog.php +++ b/public/changelog.php @@ -27,7 +27,7 @@ if ($changelogChange > 0) { WHERE `change_id` = :change_id '); $getChange->bindValue('change_id', $changelogChange); - $change = $getChange->execute() ? $getChange->fetch(PDO::FETCH_ASSOC) : []; + $change = db_fetch($getChange); if (!$change) { echo render_error(404); @@ -42,8 +42,7 @@ if ($changelogChange > 0) { WHERE ct.`change_id` = :change_id '); $getTags->bindValue('change_id', $change['change_id']); - $tags = $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : []; - tpl_var('tags', $tags); + tpl_var('tags', db_fetch_all($getTags)); } echo tpl_render('changelog.change', [ diff --git a/public/forum/posting.php b/public/forum/posting.php index 01b34355..1be34891 100644 --- a/public/forum/posting.php +++ b/public/forum/posting.php @@ -61,7 +61,7 @@ if (!empty($forumId)) { WHERE `forum_id` = :forum_id '); $getForum->bindValue('forum_id', $forumId); - $forum = $getForum->execute() ? $getForum->fetch(PDO::FETCH_ASSOC) : false; + $forum = db_fetch($getForum); } if (empty($forum)) { @@ -236,7 +236,7 @@ $getDisplayInfo = db_prepare(' WHERE `user_id` = :user_id '); $getDisplayInfo->bindValue('user_id', user_session_current('user_id')); -$displayInfo = $getDisplayInfo->execute() ? $getDisplayInfo->fetch(PDO::FETCH_ASSOC) : []; +$displayInfo = db_fetch($getDisplayInfo); echo tpl_render('forum.posting', [ 'posting_breadcrumbs' => forum_get_breadcrumbs($forumId), diff --git a/public/manage/changelog.php b/public/manage/changelog.php index 04ecfa71..5e8d90b8 100644 --- a/public/manage/changelog.php +++ b/public/manage/changelog.php @@ -44,7 +44,7 @@ switch ($_GET['v'] ?? null) { '); $getChanges->bindValue('take', $changelogPagination['range']); $getChanges->bindValue('offset', $changelogOffset); - $changes = $getChanges->execute() ? $getChanges->fetchAll(PDO::FETCH_ASSOC) : []; + $changes = db_fetch_all($getChanges); $getTags = db_prepare(' SELECT @@ -58,7 +58,7 @@ switch ($_GET['v'] ?? null) { // grab tags for ($i = 0; $i < count($changes); $i++) { $getTags->bindValue('change_id', $changes[$i]['change_id']); - $changes[$i]['tags'] = $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : []; + $changes[$i]['tags'] = db_fetch_all($getTags); } echo tpl_render('manage.changelog.changes', [ @@ -170,7 +170,7 @@ switch ($_GET['v'] ?? null) { WHERE `change_id` = :change_id '); $getChange->bindValue('change_id', $changeId); - $change = $getChange->execute() ? $getChange->fetch(PDO::FETCH_ASSOC) : []; + $change = db_fetch($getChange); if ($change) { tpl_var('edit_change', $change); @@ -185,7 +185,7 @@ switch ($_GET['v'] ?? null) { ) '); $assignedTags->bindValue('change_id', $change['change_id']); - $assignedTags = $assignedTags->execute() ? $assignedTags->fetchAll(PDO::FETCH_ASSOC) : []; + $assignedTags = db_fetch_all($assignedTags); $availableTags = db_prepare(' SELECT `tag_id`, `tag_name` @@ -198,7 +198,7 @@ switch ($_GET['v'] ?? null) { ) '); $availableTags->bindValue('change_id', $change['change_id']); - $availableTags = $availableTags->execute() ? $availableTags->fetchAll(PDO::FETCH_ASSOC) : []; + $availableTags = db_fetch_all($availableTags); tpl_vars([ 'edit_change_assigned_tags' => $assignedTags, @@ -234,7 +234,7 @@ switch ($_GET['v'] ?? null) { FROM `msz_changelog_actions` as a ORDER BY a.`action_id` ASC '); - tpl_var('changelog_actions', $getActions->execute() ? $getActions->fetchAll(PDO::FETCH_ASSOC) : []); + tpl_var('changelog_actions', db_fetch_all($getActions)); } if ($canManageTags) { @@ -249,7 +249,7 @@ switch ($_GET['v'] ?? null) { FROM `msz_changelog_tags` as t ORDER BY t.`tag_id` ASC '); - tpl_var('changelog_tags', $getTags->execute() ? $getTags->fetchAll(PDO::FETCH_ASSOC) : []); + tpl_var('changelog_tags', db_fetch_all($getTags)); } echo tpl_render('manage.changelog.actions_tags'); @@ -307,7 +307,7 @@ switch ($_GET['v'] ?? null) { WHERE `tag_id` = :tag_id '); $getTag->bindValue('tag_id', $tagId); - $tag = $getTag->execute() ? $getTag->fetch(PDO::FETCH_ASSOC) : []; + $tag = db_fetch($getTag); if ($tag) { tpl_var('edit_tag', $tag); @@ -381,7 +381,7 @@ switch ($_GET['v'] ?? null) { WHERE `action_id` = :action_id '); $getAction->bindValue('action_id', $actionId); - $action = $getAction->execute() ? $getAction->fetch(PDO::FETCH_ASSOC) : []; + $action = db_fetch($getAction); if ($action) { tpl_var('edit_action', $action); diff --git a/public/manage/forum.php b/public/manage/forum.php index cc8d1150..e2bb8065 100644 --- a/public/manage/forum.php +++ b/public/manage/forum.php @@ -23,7 +23,7 @@ switch ($_GET['v'] ?? null) { WHERE `forum_id` = :forum_id '); $getForum->bindValue('forum_id', (int)($_GET['f'] ?? 0)); - $forum = $getForum->execute() ? $getForum->fetch(PDO::FETCH_ASSOC) : false; + $forum = db_fetch($getForum); if (!$forum) { echo render_error(404); diff --git a/public/manage/users.php b/public/manage/users.php index 782e7d77..b9ec5abc 100644 --- a/public/manage/users.php +++ b/public/manage/users.php @@ -45,7 +45,7 @@ switch ($_GET['v'] ?? null) { '); $getManageUsers->bindValue('offset', $usersOffset); $getManageUsers->bindValue('take', $usersPagination['range']); - $manageUsers = $getManageUsers->execute() ? $getManageUsers->fetchAll() : []; + $manageUsers = db_fetch_all($getManageUsers); tpl_vars([ 'manage_users' => $manageUsers, @@ -80,9 +80,9 @@ switch ($_GET['v'] ?? null) { '); $getUser->bindValue('user_id', $userId); $getUser->execute(); - $manageUser = $getUser->execute() ? $getUser->fetch() : []; + $manageUser = db_fetch($getUser); - if (!$manageUser) { + if (empty($manageUser)) { echo 'Could not find that user.'; break; } @@ -97,7 +97,7 @@ switch ($_GET['v'] ?? null) { ) '); $getHasRoles->bindValue('user_id', $manageUser['user_id']); - $hasRoles = $getHasRoles->execute() ? $getHasRoles->fetchAll() : []; + $hasRoles = db_fetch_all($getHasRoles); $getAvailableRoles = db_prepare(' SELECT `role_id`, `role_name` @@ -109,7 +109,7 @@ switch ($_GET['v'] ?? null) { ) '); $getAvailableRoles->bindValue('user_id', $manageUser['user_id']); - $availableRoles = $getAvailableRoles->execute() ? $getAvailableRoles->fetchAll() : []; + $availableRoles = db_fetch_all($getAvailableRoles); if ($canManagePerms) { tpl_var('permissions', $permissions = manage_perms_list(perms_get_user_raw($userId))); @@ -283,7 +283,7 @@ switch ($_GET['v'] ?? null) { '); $getManageRoles->bindValue('offset', $rolesOffset); $getManageRoles->bindValue('take', $rolesPagination['range']); - $manageRoles = $getManageRoles->execute() ? $getManageRoles->fetchAll() : []; + $manageRoles = db_fetch_all($getManageRoles); echo tpl_render('manage.users.roles', [ 'manage_roles' => $manageRoles, @@ -458,9 +458,9 @@ switch ($_GET['v'] ?? null) { WHERE `role_id` = :role_id '); $getEditRole->bindValue('role_id', $roleId); - $editRole = $getEditRole->execute() ? $getEditRole->fetch() : []; + $editRole = db_fetch($getEditRole); - if (!$editRole) { + if (empty($editRole)) { echo 'invalid role'; break; } diff --git a/public/members.php b/public/members.php index 836cec63..d1fa9964 100644 --- a/public/members.php +++ b/public/members.php @@ -70,9 +70,9 @@ $getRole = db_prepare(' WHERE `role_id` = :role_id '); $getRole->bindValue('role_id', $roleId); -$role = $getRole->execute() ? $getRole->fetch(PDO::FETCH_ASSOC) : []; +$role = db_fetch($getRole); -if (!$role) { +if (empty($role)) { echo render_error(404); return; } @@ -115,7 +115,7 @@ $getUsers = db_prepare(sprintf( $getUsers->bindValue('role_id', $role['role_id']); $getUsers->bindValue('offset', $usersOffset); $getUsers->bindValue('take', $usersPagination['range']); -$users = $getUsers->execute() ? $getUsers->fetchAll(PDO::FETCH_ASSOC) : []; +$users = db_fetch_all($getUsers); echo tpl_render('user.listing', [ 'roles' => $roles, diff --git a/public/profile.php b/public/profile.php index cb234ae5..4aa795b4 100644 --- a/public/profile.php +++ b/public/profile.php @@ -101,7 +101,7 @@ switch ($mode) { ) as `user_id` '); $getUserId->bindValue('user_id', $_GET['u'] ?? 0); - $userId = $getUserId->execute() ? ($getUserId->fetch(PDO::FETCH_ASSOC)['user_id'] ?? 0) : 0; + $userId = (int)($getUserId->execute() ? $getUserId->fetchColumn(1) : 0); if ($userId < 1) { http_response_code(404); @@ -347,7 +347,7 @@ switch ($mode) { ) ); $getProfile->bindValue('user_id', $userId); - $profile = $getProfile->execute() ? $getProfile->fetch(PDO::FETCH_ASSOC) : []; + $profile = db_fetch($getProfile); $backgroundPath = build_path(MSZ_STORAGE, 'backgrounds/original', "{$profile['user_id']}.msz"); diff --git a/public/settings.php b/public/settings.php index ec9b4756..53f923a2 100644 --- a/public/settings.php +++ b/public/settings.php @@ -182,7 +182,7 @@ $getUserRoles = db_prepare(' ORDER BY r.`role_hierarchy` DESC '); $getUserRoles->bindValue('user_id', user_session_current('user_id')); -$userRoles = $getUserRoles->execute() ? $getUserRoles->fetchAll(PDO::FETCH_ASSOC) : []; +$userRoles = db_fetch_all($getUserRoles); echo tpl_render('user.settings', [ 'errors' => $errors, diff --git a/src/Forum/forum.php b/src/Forum/forum.php index 2fe6a639..d3eb2e62 100644 --- a/src/Forum/forum.php +++ b/src/Forum/forum.php @@ -98,9 +98,7 @@ function forum_fetch(int $forumId, bool $showDeleted = false): array $showDeleted ? '' : 'AND `topic_deleted` IS NULL' )); $getForum->bindValue('forum_id', $forumId); - $forums = $getForum->execute() ? $getForum->fetch(PDO::FETCH_ASSOC) : false; - - return $forums ? $forums : []; + return db_fetch($getForum); } function forum_get_root_categories(int $userId): array @@ -129,8 +127,7 @@ function forum_get_root_categories(int $userId): array )); $getCategories->bindValue('perm_user_id_user', $userId); $getCategories->bindValue('perm_user_id_role', $userId); - $categories = $getCategories->execute() ? $getCategories->fetchAll(PDO::FETCH_ASSOC) : []; - $categories = array_merge([MSZ_FORUM_ROOT_DATA], $categories); + $categories = array_merge([MSZ_FORUM_ROOT_DATA], db_fetch_all($getCategories)); $getRootForumCount = db_prepare(sprintf( " @@ -173,7 +170,7 @@ function forum_get_breadcrumbs( SELECT * FROM breadcrumbs '); $getBreadcrumbs->bindValue('forum_id', $forumId); - $breadcrumbsDb = $getBreadcrumbs->execute() ? $getBreadcrumbs->fetchAll(PDO::FETCH_ASSOC) : []; + $breadcrumbsDb = db_fetch_all($getBreadcrumbs); if (!$breadcrumbsDb) { return [$indexLink]; @@ -208,7 +205,7 @@ function forum_get_colour(int $forumId): int SELECT * FROM breadcrumbs '); $getColours->bindValue('forum_id', $forumId); - $colours = $getColours->execute() ? $getColours->fetchAll(PDO::FETCH_ASSOC) : []; + $colours = db_fetch_all($getColours); if ($colours) { foreach ($colours as $colour) { @@ -380,7 +377,7 @@ function forum_get_children(int $parentId, int $userId, bool $showDeleted = fals $getListing->bindValue('user_for_check', $userId); $getListing->bindValue('parent_id', $parentId); - return $getListing->execute() ? $getListing->fetchAll(PDO::FETCH_ASSOC) : []; + return db_fetch_all($getListing); } function forum_timeout(int $forumId, int $userId): int diff --git a/src/Forum/perms.php b/src/Forum/perms.php index 053ab706..29709c81 100644 --- a/src/Forum/perms.php +++ b/src/Forum/perms.php @@ -93,10 +93,8 @@ function forum_perms_get_role(string $prefix, int $forum, int $role): int function forum_perms_get_user_raw(?int $forum, int $user): array { - $emptyPerms = forum_perms_create(); - if ($user < 1) { - return $emptyPerms; + return forum_perms_create(); } $getPerms = db_prepare(sprintf(' @@ -113,15 +111,10 @@ function forum_perms_get_user_raw(?int $forum, int $user): array } $getPerms->bindValue('user_id', $user); + $perms = db_fetch($getPerms); - if (!$getPerms->execute()) { - return $emptyPerms; - } - - $perms = $getPerms->fetch(PDO::FETCH_ASSOC); - - if (!$perms) { - return $emptyPerms; + if (empty($perms)) { + return forum_perms_create(); } return $perms; @@ -129,10 +122,8 @@ function forum_perms_get_user_raw(?int $forum, int $user): array function forum_perms_get_role_raw(?int $forum, ?int $role): array { - $emptyPerms = forum_perms_create(); - if ($role < 1 && $role !== null) { - return $emptyPerms; + return forum_perms_create(); } $getPerms = db_prepare(sprintf( @@ -156,14 +147,10 @@ function forum_perms_get_role_raw(?int $forum, ?int $role): array $getPerms->bindValue('role_id', $role); } - if (!$getPerms->execute()) { - return $emptyPerms; - } + $perms = db_fetch($getPerms); - $perms = $getPerms->fetch(PDO::FETCH_ASSOC); - - if (!$perms) { - return $emptyPerms; + if (empty($perms)) { + return forum_perms_create(); } return $perms; diff --git a/src/Forum/post.php b/src/Forum/post.php index fc0aab08..0c7d6584 100644 --- a/src/Forum/post.php +++ b/src/Forum/post.php @@ -77,8 +77,7 @@ function forum_post_find(int $postId, int $userId): array $getPostInfo->bindValue('post_id', $postId); $getPostInfo->bindValue('perm_user_id_user', $userId); $getPostInfo->bindValue('perm_user_id_role', $userId); - $postInfo = $getPostInfo->execute() ? $getPostInfo->fetch(PDO::FETCH_ASSOC) : false; - return $postInfo ? $postInfo : []; + return db_fetch($getPostInfo); } function forum_post_get(int $postId, bool $allowDeleted = false): array @@ -117,8 +116,7 @@ function forum_post_get(int $postId, bool $allowDeleted = false): array $allowDeleted ? '' : 'AND `post_deleted` IS NULL' )); $getPost->bindValue('post_id', $postId); - $post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false; - return $post ? $post : []; + return db_fetch($getPost); } function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $showDeleted = false): array @@ -166,5 +164,5 @@ function forum_post_listing(int $topicId, int $offset = 0, int $take = 0, bool $ $getPosts->bindValue('take', $take); } - return $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : []; + return db_fetch_all($getPosts); } diff --git a/src/Forum/topic.php b/src/Forum/topic.php index bb0ebe13..e73b379f 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -100,8 +100,7 @@ function forum_topic_fetch(int $topicId, int $userId = 0): array $getTopic->bindValue('topic_id', $topicId); $getTopic->bindValue('perm_user_id_user', $userId); $getTopic->bindValue('perm_user_id_role', $userId); - $topic = $getTopic->execute() ? $getTopic->fetch(PDO::FETCH_ASSOC) : false; - return $topic ? $topic : []; + return db_fetch($getTopic); } function forum_topic_bump(int $topicId): bool @@ -223,5 +222,5 @@ function forum_topic_listing(int $forumId, int $userId, int $offset = 0, int $ta $getTopics->bindValue('take', $take); } - return $getTopics->execute() ? $getTopics->fetchAll() : []; + return db_fetch_all($getTopics); } diff --git a/src/Net/ip.php b/src/Net/ip.php index b01c4063..ab6692e7 100644 --- a/src/Net/ip.php +++ b/src/Net/ip.php @@ -197,6 +197,5 @@ function ip_blacklist_list(): array CONCAT(INET6_NTOA(`ip_subnet`), '/', `ip_mask`) as `ip_cidr` FROM `msz_ip_blacklist` "); - $blacklist = $getBlacklist->execute() ? $getBlacklist->fetchAll(PDO::FETCH_ASSOC) : false; - return $blacklist ? $blacklist : []; + return db_fetch_all($getBlacklist); } diff --git a/src/Users/login_attempt.php b/src/Users/login_attempt.php index 0bb44ae1..bc9e386e 100644 --- a/src/Users/login_attempt.php +++ b/src/Users/login_attempt.php @@ -68,7 +68,6 @@ function user_login_attempts_list(int $offset, int $take, int $userId = 0): arra $getAttempts->bindValue('offset', $offset); $getAttempts->bindValue('take', $take); - $attempts = $getAttempts->execute() ? $getAttempts->fetchAll(PDO::FETCH_ASSOC) : false; - return $attempts ? $attempts : []; + return db_fetch_all($getAttempts); } diff --git a/src/Users/relations.php b/src/Users/relations.php index edc554d9..bfcfaa1f 100644 --- a/src/Users/relations.php +++ b/src/Users/relations.php @@ -79,7 +79,5 @@ function user_relation_info(int $userId, int $subjectId): array '); $getRelationInfo->bindValue('user_id', $userId); $getRelationInfo->bindValue('subject_id', $subjectId); - $relationInfo = $getRelationInfo->execute() ? $getRelationInfo->fetch(PDO::FETCH_ASSOC) : false; - - return $relationInfo ? $relationInfo : []; + return db_fetch($getRelationInfo); } diff --git a/src/Users/session.php b/src/Users/session.php index 4b1650ec..428f09b8 100644 --- a/src/Users/session.php +++ b/src/Users/session.php @@ -44,8 +44,7 @@ function user_session_find($sessionId, bool $byKey = false): array WHERE `%s` = :session_id ', $byKey ? 'session_key' : 'session_id')); $findSession->bindValue('session_id', $sessionId); - $session = $findSession->execute() ? $findSession->fetch(PDO::FETCH_ASSOC) : false; - return $session ? $session : []; + return db_fetch($findSession); } function user_session_delete(int $sessionId): void @@ -109,9 +108,8 @@ function user_session_list(int $offset, int $take, int $userId = 0): array $getSessions->bindValue('offset', $offset); $getSessions->bindValue('take', $take); - $sessions = $getSessions->execute() ? $getSessions->fetchAll(PDO::FETCH_ASSOC) : false; - return $sessions ? $sessions : []; + return db_fetch_all($getSessions); } function user_session_bump_active(int $sessionId): void diff --git a/src/Users/warning.php b/src/Users/warning.php index 58b3c92b..78c63537 100644 --- a/src/Users/warning.php +++ b/src/Users/warning.php @@ -158,8 +158,7 @@ function user_warning_fetch( $fetchWarnings->bindValue('days', $days); } - $warnings = $fetchWarnings->execute() ? $fetchWarnings->fetchAll(PDO::FETCH_ASSOC) : false; - return $warnings ? $warnings : []; + return db_fetch_all($fetchWarnings); } function user_warning_global_count(?int $userId = null): int @@ -204,8 +203,7 @@ function user_warning_global_fetch(int $offset = 0, int $take = 50, ?int $userId $fetchWarnings->bindValue('user_id', $userId); } - $warnings = $fetchWarnings->execute() ? $fetchWarnings->fetchAll(PDO::FETCH_ASSOC) : false; - return $warnings ? $warnings : []; + return db_fetch_all($fetchWarnings); } function user_warning_check_ip(string $address): bool diff --git a/src/audit_log.php b/src/audit_log.php index 0debf19c..664aab00 100644 --- a/src/audit_log.php +++ b/src/audit_log.php @@ -118,7 +118,5 @@ function audit_log_list(int $offset, int $take, int $userId = 0): array $getLogs->bindValue('offset', $offset); $getLogs->bindValue('take', $take); - $logs = $getLogs->execute() ? $getLogs->fetchAll(PDO::FETCH_ASSOC) : false; - - return $logs ? $logs : []; + return db_fetch_all($getLogs); } diff --git a/src/changelog.php b/src/changelog.php index 8dbaa102..32e305e0 100644 --- a/src/changelog.php +++ b/src/changelog.php @@ -87,7 +87,7 @@ function changelog_get_changes(string $date, int $user, int $offset, int $take): $prep->bindValue('user', $user); } - return $prep->execute() ? $prep->fetchAll(PDO::FETCH_ASSOC) : []; + return db_fetch_all($prep); } define('CHANGELOG_COUNT_QUERY', ' diff --git a/src/chat_quotes.php b/src/chat_quotes.php index cd958dc2..2410e82a 100644 --- a/src/chat_quotes.php +++ b/src/chat_quotes.php @@ -64,8 +64,7 @@ function chat_quotes_single(int $quoteId): array WHERE `quote_id` = :quote '); $getSingle->bindValue('quote', $quoteId); - $single = $getSingle->execute() ? $getSingle->fetch(PDO::FETCH_ASSOC) : []; - return $single ? $single : []; + return db_fetch($getSingle); } function chat_quotes_parents(int $offset = 0, int $take = MSZ_CHAT_QUOTES_TAKE): array @@ -85,8 +84,7 @@ function chat_quotes_parents(int $offset = 0, int $take = MSZ_CHAT_QUOTES_TAKE): $getParents->bindValue('offset', $offset); } - $parents = $getParents->execute() ? $getParents->fetchAll() : []; - return $parents ? $parents : []; + return db_fetch_all($getParents); } function chat_quotes_set(int $parentId): array @@ -98,7 +96,7 @@ function chat_quotes_set(int $parentId): array AND `quote_id` = :parent '); $getParent->bindValue('parent', $parentId); - $parent = $getParent->execute() ? $getParent->fetch(PDO::FETCH_ASSOC) : []; + $parent = db_fetch($getParent); return $parent ? array_merge([$parent], chat_quotes_children($parent['quote_id'])) : []; } @@ -110,8 +108,7 @@ function chat_quotes_children(int $parentId): array WHERE `quote_parent` = :parent '); $getChildren->bindValue('parent', $parentId); - $children = $getChildren->execute() ? $getChildren->fetchAll(PDO::FETCH_ASSOC) : []; - return $children ? $children : []; + return db_fetch($getChildren); } function chat_quotes_random(): array diff --git a/src/comments.php b/src/comments.php index d1a7a9c6..117bf27c 100644 --- a/src/comments.php +++ b/src/comments.php @@ -47,9 +47,9 @@ function comments_parse_for_display(string $text): string WHERE `user_id` = :user_id '); $getInfo->bindValue('user_id', $matches[1]); - $info = $getInfo->execute() ? $getInfo->fetch(PDO::FETCH_ASSOC) : []; + $info = db_fetch($getInfo); - if (!$info) { + if (empty($info)) { return $matches[0]; } @@ -134,8 +134,7 @@ function comments_votes_get(int $commentId): array ) as `dislikes` '); $getVotes->bindValue('id', $commentId); - $votes = $getVotes->execute() ? $getVotes->fetch(PDO::FETCH_ASSOC) : false; - return $votes ? $votes : []; + return db_fetch($getVotes); } function comments_category_create(string $name): array @@ -193,7 +192,7 @@ function comments_category_info($category, bool $createIfNone = false): array } $getCategory->bindValue('category', $category); - $categoryInfo = $getCategory->execute() ? $getCategory->fetch(PDO::FETCH_ASSOC) : false; + $categoryInfo = db_fetch($getCategory); return $categoryInfo ? $categoryInfo : ( @@ -253,7 +252,7 @@ function comments_category_get(int $category, int $user, ?int $parent = null): a $getComments->bindValue('user', $user); $getComments->bindValue('category', $category); - $comments = $getComments->execute() ? $getComments->fetchAll(PDO::FETCH_ASSOC) : []; + $comments = db_fetch_all($getComments); $commentsCount = count($comments); for ($i = 0; $i < $commentsCount; $i++) { @@ -319,8 +318,7 @@ function comments_post_get(int $commentId, bool $parse = true): array WHERE `comment_id` = :id '); $fetch->bindValue('id', $commentId); - $comment = $fetch->execute() ? $fetch->fetch(PDO::FETCH_ASSOC) : false; - $comment = $comment ? $comment : []; // prevent type errors + $comment = db_fetch($fetch); if ($comment && $parse) { $comment['comment_html'] = nl2br(comments_parse_for_display(htmlentities($comment['comment_text']))); @@ -357,5 +355,5 @@ function comments_post_replies(int $commentId): array WHERE `comment_reply_to` = :id '); $getComments->bindValue('id', $commentId); - return $getComments->execute() ? $getComments->fetchAll(PDO::FETCH_ASSOC) : []; + return db_fetch_all($getComments); } diff --git a/src/db.php b/src/db.php index 20f7c656..84ad75c7 100644 --- a/src/db.php +++ b/src/db.php @@ -52,7 +52,19 @@ function db_last_insert_id(?string $name = null, ?string $connection = null): st function db_query_count(?string $connection = null): int { - return (int)db_query('SHOW SESSION STATUS LIKE "Questions"', $connection)->fetch()['Value']; + return (int)db_query('SHOW SESSION STATUS LIKE "Questions"', $connection)->fetchColumn(1); +} + +function db_fetch(PDOStatement $stmt, $default = []) +{ + $out = $stmt->execute() ? $stmt->fetch(PDO::FETCH_ASSOC) : false; + return $out ? $out : $default; +} + +function db_fetch_all(PDOStatement $stmt, $default = []) +{ + $out = $stmt->execute() ? $stmt->fetchAll(PDO::FETCH_ASSOC) : false; + return $out ? $out : $default; } // starting at 2 diff --git a/src/news.php b/src/news.php index c8fc7391..bca5e0e3 100644 --- a/src/news.php +++ b/src/news.php @@ -126,8 +126,7 @@ function news_categories_get( $getCats->bindValue('take', $take); } - $cats = $getCats->execute() ? $getCats->fetchAll(PDO::FETCH_ASSOC) : false; - return $cats ? $cats : []; + return db_fetch_all($getCats); } function news_categories_count(bool $includeHidden = false): int @@ -180,8 +179,7 @@ function news_category_get( $getCategory = db_prepare($query); $getCategory->bindValue('category', $category); - $category = $getCategory->execute() ? $getCategory->fetch(PDO::FETCH_ASSOC) : false; - return $category ? $category : []; + return db_fetch($getCategory); } function news_posts_count( @@ -263,8 +261,7 @@ function news_posts_get( $getPosts->bindValue('offset', $offset); } - $posts = $getPosts->execute() ? $getPosts->fetchAll(PDO::FETCH_ASSOC) : false; - return $posts ? $posts : []; + return db_fetch_all($getPosts); } function news_post_comments_set(int $postId, int $sectionId): void @@ -297,9 +294,6 @@ function news_post_get(int $postId): array ON u.`display_role` = r.`role_id` WHERE `post_id` = :post_id '); - $getPost->bindValue(':post_id', $postId); - $post = $getPost->execute() ? $getPost->fetch(PDO::FETCH_ASSOC) : false; - - return $post ? $post : []; + return db_fetch($getPost); } diff --git a/src/perms.php b/src/perms.php index ab5300e8..88201af0 100644 --- a/src/perms.php +++ b/src/perms.php @@ -96,10 +96,8 @@ function perms_get_role(string $prefix, int $role): int function perms_get_user_raw(int $user): array { - $emptyPerms = perms_create(); - if ($user < 1) { - return $emptyPerms; + return perms_create(); } $getPerms = db_prepare(sprintf(' @@ -109,15 +107,10 @@ function perms_get_user_raw(int $user): array AND `role_id` IS NULL ', implode('`, `', perms_get_keys()))); $getPerms->bindValue('user_id', $user); + $perms = db_fetch($getPerms); - if (!$getPerms->execute()) { - return $emptyPerms; - } - - $perms = $getPerms->fetch(PDO::FETCH_ASSOC); - - if (!$perms) { - return $emptyPerms; + if (empty($perms)) { + return perms_create(); } return $perms; @@ -125,10 +118,8 @@ function perms_get_user_raw(int $user): array function perms_get_role_raw(int $role): array { - $emptyPerms = perms_create(); - if ($role < 1) { - return $emptyPerms; + return perms_create(); } $getPerms = db_prepare(sprintf(' @@ -138,15 +129,10 @@ function perms_get_role_raw(int $role): array AND `role_id` = :role_id ', implode('`, `', perms_get_keys()))); $getPerms->bindValue('role_id', $role); + $perms = db_fetch($getPerms); - if (!$getPerms->execute()) { - return $emptyPerms; - } - - $perms = $getPerms->fetch(PDO::FETCH_ASSOC); - - if (!$perms) { - return $emptyPerms; + if (empty($perms)) { + return perms_create(); } return $perms;