From 0a8a4d3b992e1c2053455bf7738d0bea9414d203 Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 16 Jul 2018 00:59:14 +0200 Subject: [PATCH] Use shorthand database operations everywhere. --- misuzu.php | 14 ++++------- public/auth.php | 3 +-- public/changelog.php | 5 ++-- public/forum/forum.php | 2 -- public/forum/index.php | 2 -- public/forum/posting.php | 7 +++--- public/manage/changelog.php | 49 ++++++++++++++++++------------------- public/manage/users.php | 35 +++++++++++++------------- public/members.php | 14 +++++------ public/news.php | 15 ++++++------ public/profile.php | 2 +- server.php | 13 ---------- src/Database.php | 5 ++++ src/Forum/forum.php | 14 +++++------ src/Forum/post.php | 10 +++----- src/Forum/topic.php | 14 +++++------ src/Users/login_attempt.php | 4 +-- src/Users/profile.php | 2 +- src/Users/role.php | 8 +++--- src/Users/session.php | 4 +-- src/Users/user.php | 7 +++--- src/Users/validation.php | 4 +-- src/changelog.php | 18 +++++--------- src/perms.php | 8 +++--- 24 files changed, 111 insertions(+), 148 deletions(-) delete mode 100644 server.php diff --git a/misuzu.php b/misuzu.php index 15df144f..90c0b128 100644 --- a/misuzu.php +++ b/misuzu.php @@ -34,10 +34,8 @@ if (PHP_SAPI === 'cli') { if ($argv[0] === basename(__FILE__)) { switch ($argv[1] ?? null) { case 'cron': - $db = Database::connection(); - // Ensure main role exists. - $db->query(" + Database::exec(" INSERT IGNORE INTO `msz_roles` (`role_id`, `role_name`, `role_hierarchy`, `role_colour`, `role_description`, `created_at`) VALUES @@ -45,7 +43,7 @@ if (PHP_SAPI === 'cli') { "); // Ensures all users are in the main role. - $db->query(' + Database::exec(' INSERT INTO `msz_user_roles` (`user_id`, `role_id`) SELECT `user_id`, 1 FROM `msz_users` as u @@ -58,7 +56,7 @@ if (PHP_SAPI === 'cli') { '); // Ensures all display_role values are correct with `msz_user_roles` - $db->query(' + Database::exec(' UPDATE `msz_users` as u SET `display_role` = ( SELECT ur.`role_id` @@ -152,9 +150,7 @@ if (PHP_SAPI === 'cli') { $app->startSession((int)$_COOKIE['msz_uid'], $_COOKIE['msz_sid']); if ($app->hasActiveSession()) { - $db = Database::connection(); - - $bumpUserLast = $db->prepare(' + $bumpUserLast = Database::prepare(' UPDATE `msz_users` SET `last_seen` = NOW(), `last_ip` = INET6_ATON(:last_ip) @@ -164,7 +160,7 @@ if (PHP_SAPI === 'cli') { $bumpUserLast->bindValue('user_id', $app->getUserId()); $bumpUserLast->execute(); - $getUserDisplayInfo = $db->prepare(' + $getUserDisplayInfo = Database::prepare(' SELECT u.`user_id`, u.`username`, COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `colour` diff --git a/public/auth.php b/public/auth.php index 4e75706d..9ad1f001 100644 --- a/public/auth.php +++ b/public/auth.php @@ -6,7 +6,6 @@ use Misuzu\Users\Session; require_once __DIR__ . '/../misuzu.php'; -$db = Database::connection(); $config = $app->getConfig(); $templating = $app->getTemplating(); @@ -76,7 +75,7 @@ switch ($authMode) { $username = $_POST['username'] ?? ''; $password = $_POST['password'] ?? ''; - $getUser = $db->prepare(' + $getUser = Database::prepare(' SELECT `user_id`, `password` FROM `msz_users` WHERE LOWER(`email`) = LOWER(:email) diff --git a/public/changelog.php b/public/changelog.php index 7bbf4e17..899d7b5a 100644 --- a/public/changelog.php +++ b/public/changelog.php @@ -3,7 +3,6 @@ use Misuzu\Database; require_once __DIR__ . '/../misuzu.php'; -$db = Database::connection(); $tpl = $app->getTemplating(); $changelogOffset = max((int)($_GET['o'] ?? 0), 0); @@ -20,7 +19,7 @@ $tpl->vars([ ]); if ($changelogChange > 0) { - $getChange = $db->prepare(' + $getChange = Database::prepare(' SELECT c.`change_id`, c.`change_created`, c.`change_log`, c.`change_text`, a.`action_name`, a.`action_colour`, a.`action_class`, @@ -43,7 +42,7 @@ if ($changelogChange > 0) { if (!$change) { http_response_code(404); } else { - $getTags = $db->prepare(' + $getTags = Database::prepare(' SELECT t.`tag_id`, t.`tag_name`, t.`tag_description` FROM `msz_changelog_tags` as t diff --git a/public/forum/forum.php b/public/forum/forum.php index 87d0b9a5..c3b7abaf 100644 --- a/public/forum/forum.php +++ b/public/forum/forum.php @@ -1,6 +1,4 @@ getTemplating(); if (!$app->hasActiveSession()) { @@ -29,7 +28,7 @@ if (empty($postId) && empty($topicId) && empty($forumId)) { } if (!empty($postId)) { - $getPost = $db->prepare(' + $getPost = Database::prepare(' SELECT `post_id`, `topic_id` FROM `msz_forum_posts` WHERE `post_id` = :post_id @@ -43,7 +42,7 @@ if (!empty($postId)) { } if (!empty($topicId)) { - $getTopic = $db->prepare(' + $getTopic = Database::prepare(' SELECT `topic_id`, `forum_id`, `topic_title`, `topic_locked` FROM `msz_forum_topics` WHERE `topic_id` = :topic_id @@ -57,7 +56,7 @@ if (!empty($topicId)) { } if (!empty($forumId)) { - $getForum = $db->prepare(' + $getForum = Database::prepare(' SELECT `forum_id`, `forum_name`, `forum_type`, `forum_archived` FROM `msz_forum_categories` WHERE `forum_id` = :forum_id diff --git a/public/manage/changelog.php b/public/manage/changelog.php index a31def0d..d93a8bec 100644 --- a/public/manage/changelog.php +++ b/public/manage/changelog.php @@ -3,7 +3,6 @@ use Misuzu\Database; require_once __DIR__ . '/../../misuzu.php'; -$db = Database::connection(); $tpl = $app->getTemplating(); $changelogPerms = perms_get_user(MSZ_PERMS_CHANGELOG, $app->getUserId()); @@ -19,12 +18,12 @@ switch ($_GET['v'] ?? null) { } $changesTake = 20; - $changesCount = (int)$db->query(' + $changesCount = (int)Database::query(' SELECT COUNT(`change_id`) FROM `msz_changelog_changes` ')->fetchColumn(); - $getChanges = $db->prepare(' + $getChanges = Database::prepare(' SELECT c.`change_id`, c.`change_log`, c.`change_created`, a.`action_name`, a.`action_colour`, a.`action_class`, @@ -44,7 +43,7 @@ switch ($_GET['v'] ?? null) { $getChanges->bindValue('offset', $queryOffset); $changes = $getChanges->execute() ? $getChanges->fetchAll(PDO::FETCH_ASSOC) : []; - $getTags = $db->prepare(' + $getTags = Database::prepare(' SELECT t.`tag_id`, t.`tag_name`, t.`tag_description` FROM `msz_changelog_change_tags` as ct @@ -78,7 +77,7 @@ switch ($_GET['v'] ?? null) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && tmp_csrf_verify($_POST['csrf'] ?? '')) { if (!empty($_POST['change']) && is_array($_POST['change'])) { if ($changeId > 0) { - $postChange = $db->prepare(' + $postChange = Database::prepare(' UPDATE `msz_changelog_changes` SET `change_log` = :log, `change_text` = :text, @@ -89,7 +88,7 @@ switch ($_GET['v'] ?? null) { '); $postChange->bindValue('change_id', $changeId); } else { - $postChange = $db->prepare(' + $postChange = Database::prepare(' INSERT INTO `msz_changelog_changes` ( `change_log`, `change_text`, `action_id`, @@ -114,20 +113,20 @@ switch ($_GET['v'] ?? null) { $postChange->execute(); if ($changeId < 1) { - header('Location: ?v=change&c=' . $db->lastInsertId()); + header('Location: ?v=change&c=' . Database::lastInsertId()); return; } } if (!empty($_POST['add_tag']) && is_numeric($_POST['add_tag'])) { - $addTag = $db->prepare('REPLACE INTO `msz_changelog_change_tags` VALUES (:change_id, :tag_id)'); + $addTag = Database::prepare('REPLACE INTO `msz_changelog_change_tags` VALUES (:change_id, :tag_id)'); $addTag->bindValue('change_id', $changeId); $addTag->bindValue('tag_id', $_POST['add_tag']); $addTag->execute(); } if (!empty($_POST['remove_tag']) && is_numeric($_POST['remove_tag'])) { - $removeTag = $db->prepare(' + $removeTag = Database::prepare(' DELETE FROM `msz_changelog_change_tags` WHERE `change_id` = :change_id AND `tag_id` = :tag_id @@ -138,14 +137,14 @@ switch ($_GET['v'] ?? null) { } } - $actions = $db->query(' + $actions = Database::query(' SELECT `action_id`, `action_name` FROM `msz_changelog_actions` ')->fetchAll(PDO::FETCH_ASSOC); $tpl->var('changelog_actions', $actions); if ($changeId > 0) { - $getChange = $db->prepare(' + $getChange = Database::prepare(' SELECT `change_id`, `change_log`, `change_text`, `user_id`, `action_id`, `change_created` @@ -158,7 +157,7 @@ switch ($_GET['v'] ?? null) { if ($change) { $tpl->var('edit_change', $change); - $assignedTags = $db->prepare(' + $assignedTags = Database::prepare(' SELECT `tag_id`, `tag_name` FROM `msz_changelog_tags` WHERE `tag_id` IN ( @@ -170,7 +169,7 @@ switch ($_GET['v'] ?? null) { $assignedTags->bindValue('change_id', $change['change_id']); $assignedTags = $assignedTags->execute() ? $assignedTags->fetchAll(PDO::FETCH_ASSOC) : []; - $availableTags = $db->prepare(' + $availableTags = Database::prepare(' SELECT `tag_id`, `tag_name` FROM `msz_changelog_tags` WHERE `tag_archived` IS NULL @@ -204,12 +203,12 @@ switch ($_GET['v'] ?? null) { $tagsTake = 32; - $tagsCount = (int)$db->query(' + $tagsCount = (int)Database::query(' SELECT COUNT(`tag_id`) FROM `msz_changelog_tags` ')->fetchColumn(); - $getTags = $db->prepare(' + $getTags = Database::prepare(' SELECT t.`tag_id`, t.`tag_name`, t.`tag_description`, t.`tag_created`, ( @@ -244,7 +243,7 @@ switch ($_GET['v'] ?? null) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && tmp_csrf_verify($_POST['csrf'] ?? '')) { if (!empty($_POST['tag']) && is_array($_POST['tag'])) { if ($tagId > 0) { - $updateTag = $db->prepare(' + $updateTag = Database::prepare(' UPDATE `msz_changelog_tags` SET `tag_name` = :name, `tag_description` = :description, @@ -253,7 +252,7 @@ switch ($_GET['v'] ?? null) { '); $updateTag->bindValue('id', $tagId); } else { - $updateTag = $db->prepare(' + $updateTag = Database::prepare(' INSERT INTO `msz_changelog_tags` (`tag_name`, `tag_description`, `tag_archived`) VALUES @@ -268,14 +267,14 @@ switch ($_GET['v'] ?? null) { $updateTag->execute(); if ($tagId < 1) { - header('Location: ?v=tag&t=' . $db->lastInsertId()); + header('Location: ?v=tag&t=' . Database::lastInsertId()); return; } } } if ($tagId > 0) { - $getTag = $db->prepare(' + $getTag = Database::prepare(' SELECT `tag_id`, `tag_name`, `tag_description`, `tag_archived`, `tag_created` FROM `msz_changelog_tags` WHERE `tag_id` = :tag_id @@ -302,12 +301,12 @@ switch ($_GET['v'] ?? null) { $actionTake = 32; - $actionCount = (int)$db->query(' + $actionCount = (int)Database::query(' SELECT COUNT(`action_id`) FROM `msz_changelog_actions` ')->fetchColumn(); - $getActions = $db->prepare(' + $getActions = Database::prepare(' SELECT a.`action_id`, a.`action_name`, a.`action_colour`, ( @@ -342,7 +341,7 @@ switch ($_GET['v'] ?? null) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && tmp_csrf_verify($_POST['csrf'] ?? '')) { if (!empty($_POST['action']) && is_array($_POST['action'])) { if ($actionId > 0) { - $updateAction = $db->prepare(' + $updateAction = Database::prepare(' UPDATE `msz_changelog_actions` SET `action_name` = :name, `action_colour` = :colour, @@ -351,7 +350,7 @@ switch ($_GET['v'] ?? null) { '); $updateAction->bindValue('id', $actionId); } else { - $updateAction = $db->prepare(' + $updateAction = Database::prepare(' INSERT INTO `msz_changelog_actions` (`action_name`, `action_colour`, `action_class`) VALUES @@ -375,14 +374,14 @@ switch ($_GET['v'] ?? null) { $updateAction->execute(); if ($actionId < 1) { - header('Location: ?v=action&a=' . $db->lastInsertId()); + header('Location: ?v=action&a=' . Database::lastInsertId()); return; } } } if ($actionId > 0) { - $getAction = $db->prepare(' + $getAction = Database::prepare(' SELECT `action_id`, `action_name`, `action_colour`, `action_class` FROM `msz_changelog_actions` WHERE `action_id` = :action_id diff --git a/public/manage/users.php b/public/manage/users.php index 3d59a217..9fe2b47f 100644 --- a/public/manage/users.php +++ b/public/manage/users.php @@ -3,7 +3,6 @@ use Misuzu\Database; require_once __DIR__ . '/../../misuzu.php'; -$db = Database::connection(); $tpl = $app->getTemplating(); $userPerms = perms_get_user(MSZ_PERMS_USER, $app->getUserId()); @@ -26,12 +25,12 @@ switch ($_GET['v'] ?? null) { } $usersTake = 32; - $manageUsersCount = $db->query(' + $manageUsersCount = Database::query(' SELECT COUNT(`user_id`) FROM `msz_users` ')->fetchColumn(); - $getManageUsers = $db->prepare(' + $getManageUsers = Database::prepare(' SELECT u.`user_id`, u.`username`, COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `colour` @@ -66,7 +65,7 @@ switch ($_GET['v'] ?? null) { echo 'no'; break; } - $getUser = $db->prepare(' + $getUser = Database::prepare(' SELECT u.*, INET6_NTOA(u.`register_ip`) as `register_ip_decoded`, @@ -87,7 +86,7 @@ switch ($_GET['v'] ?? null) { break; } - $getHasRoles = $db->prepare(' + $getHasRoles = Database::prepare(' SELECT `role_id`, `role_name` FROM `msz_roles` WHERE `role_id` IN ( @@ -99,7 +98,7 @@ switch ($_GET['v'] ?? null) { $getHasRoles->bindValue('user_id', $manageUser['user_id']); $hasRoles = $getHasRoles->execute() ? $getHasRoles->fetchAll() : []; - $getAvailableRoles = $db->prepare(' + $getAvailableRoles = Database::prepare(' SELECT `role_id`, `role_name` FROM `msz_roles` WHERE `role_id` NOT IN ( @@ -125,7 +124,7 @@ switch ($_GET['v'] ?? null) { && user_validate_username($_POST['user']['username']) === '' && user_validate_email($_POST['user']['email']) === '' && strlen($_POST['user']['country']) === 2) { - $updateUserDetails = $db->prepare(' + $updateUserDetails = Database::prepare(' UPDATE `msz_users` SET `username` = :username, `email` = LOWER(:email), @@ -158,7 +157,7 @@ switch ($_GET['v'] ?? null) { && !empty($_POST['password']['confirm']) && user_validate_password($_POST['password']['new']) === '' && $_POST['password']['new'] === $_POST['password']['confirm']) { - $updatePassword = $db->prepare(' + $updatePassword = Database::prepare(' UPDATE `msz_users` SET `password` = :password WHERE `user_id` = :user_id @@ -195,7 +194,7 @@ switch ($_GET['v'] ?? null) { if ($perms !== null) { $permKeys = array_keys($perms); - $setPermissions = $db->prepare(' + $setPermissions = Database::prepare(' REPLACE INTO `msz_permissions` (`role_id`, `user_id`, `' . implode('`, `', $permKeys) . '`) VALUES @@ -209,7 +208,7 @@ switch ($_GET['v'] ?? null) { $setPermissions->execute(); } else { - $deletePermissions = $db->prepare(' + $deletePermissions = Database::prepare(' DELETE FROM `msz_permissions` WHERE `role_id` IS NULL AND `user_id` = :user_id @@ -239,12 +238,12 @@ switch ($_GET['v'] ?? null) { } $rolesTake = 10; - $manageRolesCount = $db->query(' + $manageRolesCount = Database::query(' SELECT COUNT(`role_id`) FROM `msz_roles` ')->fetchColumn(); - $getManageRoles = $db->prepare(' + $getManageRoles = Database::prepare(' SELECT `role_id`, `role_colour`, `role_name`, ( @@ -352,7 +351,7 @@ switch ($_GET['v'] ?? null) { } if ($roleId < 1) { - $updateRole = $db->prepare(' + $updateRole = Database::prepare(' INSERT INTO `msz_roles` ( `role_name`, `role_hierarchy`, `role_secret`, `role_colour`, @@ -365,7 +364,7 @@ switch ($_GET['v'] ?? null) { ) '); } else { - $updateRole = $db->prepare(' + $updateRole = Database::prepare(' UPDATE `msz_roles` SET `role_name` = :role_name, `role_hierarchy` = :role_hierarchy, @@ -387,7 +386,7 @@ switch ($_GET['v'] ?? null) { $updateRole->execute(); if ($roleId < 1) { - $roleId = (int)$db->lastInsertId(); + $roleId = (int)Database::lastInsertId(); } if (!empty($permissions) && !empty($_POST['perms']) && is_array($_POST['perms'])) { @@ -395,7 +394,7 @@ switch ($_GET['v'] ?? null) { if ($perms !== null) { $permKeys = array_keys($perms); - $setPermissions = $db->prepare(' + $setPermissions = Database::prepare(' REPLACE INTO `msz_permissions` (`role_id`, `user_id`, `' . implode('`, `', $permKeys) . '`) VALUES @@ -409,7 +408,7 @@ switch ($_GET['v'] ?? null) { $setPermissions->execute(); } else { - $deletePermissions = $db->prepare(' + $deletePermissions = Database::prepare(' DELETE FROM `msz_permissions` WHERE `role_id` = :role_id AND `user_id` IS NULL @@ -429,7 +428,7 @@ switch ($_GET['v'] ?? null) { break; } - $getEditRole = $db->prepare(' + $getEditRole = Database::prepare(' SELECT * FROM `msz_roles` WHERE `role_id` = :role_id diff --git a/public/members.php b/public/members.php index 93eb045a..b47114fc 100644 --- a/public/members.php +++ b/public/members.php @@ -58,10 +58,9 @@ if (empty($orderDir)) { return; } -$db = Database::connection(); $tpl = $app->getTemplating(); -$getRole = $db->prepare(' +$getRole = Database::prepare(' SELECT `role_id`, `role_name`, `role_colour`, `role_description`, `created_at`, ( @@ -73,22 +72,21 @@ $getRole = $db->prepare(' WHERE `role_id` = :role_id '); $getRole->bindValue('role_id', $roleId); -$role = $getRole->execute() ? $getRole->fetch() : []; +$role = $getRole->execute() ? $getRole->fetch(PDO::FETCH_ASSOC) : []; if (!$role) { echo render_error(404); return; } -$getRoles = $db->prepare(' +$roles = Database::query(' SELECT `role_id`, `role_name`, `role_colour` FROM `msz_roles` WHERE `role_secret` = 0 ORDER BY `role_id` -'); -$roles = $getRoles->execute() ? $getRoles->fetchAll() : []; +')->fetchAll(PDO::FETCH_ASSOC); -$getUsers = $db->prepare(" +$getUsers = Database::prepare(" SELECT u.`user_id`, u.`username`, u.`user_country`, u.`created_at` as `user_joined`, u.`last_seen` as `user_last_seen`, @@ -118,7 +116,7 @@ $getUsers = $db->prepare(" $getUsers->bindValue('role_id', $role['role_id']); $getUsers->bindValue('offset', $usersOffset); $getUsers->bindValue('take', $usersTake); -$users = $getUsers->execute() ? $getUsers->fetchAll() : []; +$users = $getUsers->execute() ? $getUsers->fetchAll(PDO::FETCH_ASSOC) : []; echo $tpl->render('user.listing', [ 'roles' => $roles, diff --git a/public/news.php b/public/news.php index ccfa3ca1..170480c1 100644 --- a/public/news.php +++ b/public/news.php @@ -3,7 +3,6 @@ use Misuzu\Database; require_once __DIR__ . '/../misuzu.php'; -$db = Database::connection(); $templating = $app->getTemplating(); $categoryId = isset($_GET['c']) ? (int)$_GET['c'] : null; @@ -17,7 +16,7 @@ $templating->vars([ ]); if ($postId !== null) { - $getPost = $db->prepare(' + $getPost = Database::prepare(' SELECT p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, c.`category_id`, c.`category_name`, @@ -45,7 +44,7 @@ if ($postId !== null) { } if ($categoryId !== null) { - $getCategory = $db->prepare(' + $getCategory = Database::prepare(' SELECT c.`category_id`, c.`category_name`, c.`category_description`, COUNT(p.`post_id`) AS `posts_count` @@ -63,7 +62,7 @@ if ($categoryId !== null) { return; } - $getPosts = $db->prepare(' + $getPosts = Database::prepare(' SELECT p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, c.`category_id`, c.`category_name`, @@ -85,7 +84,7 @@ if ($categoryId !== null) { $getPosts->bindValue('category_id', $category['category_id'], PDO::PARAM_INT); $posts = $getPosts->execute() ? $getPosts->fetchAll() : false; - $getFeatured = $db->prepare(' + $getFeatured = Database::prepare(' SELECT `post_id`, `post_title` FROM `msz_news_posts` WHERE `category_id` = :category_id @@ -100,7 +99,7 @@ if ($categoryId !== null) { return; } -$getCategories = $db->prepare(' +$getCategories = Database::prepare(' SELECT c.`category_id`, c.`category_name`, COUNT(p.`post_id`) AS count @@ -113,7 +112,7 @@ $getCategories = $db->prepare(' '); $categories = $getCategories->execute() ? $getCategories->fetchAll() : []; -$postsCount = (int)$db->query(' +$postsCount = (int)Database::query(' SELECT COUNT(p.`post_id`) as `posts_count` FROM `msz_news_posts` as p LEFT JOIN `msz_news_categories` as c @@ -129,7 +128,7 @@ if ($postsOffset < 0 || $postsOffset >= $postsCount) { return; } -$getPosts = $db->prepare(' +$getPosts = Database::prepare(' SELECT p.`post_id`, p.`post_title`, p.`post_text`, p.`created_at`, c.`category_id`, c.`category_name`, diff --git a/public/profile.php b/public/profile.php index 47483ee6..a548b982 100644 --- a/public/profile.php +++ b/public/profile.php @@ -42,7 +42,7 @@ switch ($mode) { default: $templating = $app->getTemplating(); - $getProfile = Database::connection()->prepare(' + $getProfile = Database::prepare(' SELECT u.*, COALESCE(u.`user_title`, r.`role_title`) as `user_title`, diff --git a/server.php b/server.php deleted file mode 100644 index 30b270c1..00000000 --- a/server.php +++ /dev/null @@ -1,13 +0,0 @@ -exec($statement); } + public static function lastInsertId(?string $name = null, ?string $connection = null): string + { + return self::connection($connection)->lastInsertId($name); + } + public static function queryCount(?string $connection = null): int { return (int)Database::query('SHOW SESSION STATUS LIKE "Questions"', $connection)->fetch()['Value']; diff --git a/src/Forum/forum.php b/src/Forum/forum.php index 7bf60da5..bb0a860a 100644 --- a/src/Forum/forum.php +++ b/src/Forum/forum.php @@ -41,7 +41,7 @@ function forum_may_have_topics(int $forumType): bool function forum_fetch(int $forumId): array { - $getForum = Database::connection()->prepare(' + $getForum = Database::prepare(' SELECT `forum_id`, `forum_name`, `forum_type`, `forum_link`, `forum_link_clicks`, `forum_parent`, ( @@ -61,9 +61,7 @@ function forum_fetch(int $forumId): array function forum_get_root_categories(): array { - $dbc = Database::connection(); - - $categories = $dbc->query(' + $categories = Database::query(' SELECT f.`forum_id`, f.`forum_name`, f.`forum_type`, ( @@ -80,7 +78,7 @@ function forum_get_root_categories(): array $categories = array_merge([MSZ_FORUM_ROOT_DATA], $categories); - $categories[0]['forum_children'] = (int)$dbc->query(' + $categories[0]['forum_children'] = (int)Database::query(' SELECT COUNT(`forum_id`) FROM `msz_forum_categories` WHERE `forum_parent` = ' . MSZ_FORUM_ROOT . ' @@ -95,7 +93,7 @@ function forum_get_breadcrumbs( array $indexLink = ['Forums' => '/forum/'] ): array { $breadcrumbs = []; - $getBreadcrumb = Database::connection()->prepare(' + $getBreadcrumb = Database::prepare(' SELECT `forum_id`, `forum_name`, `forum_parent` FROM `msz_forum_categories` WHERE `forum_id` = :forum_id @@ -118,7 +116,7 @@ function forum_get_breadcrumbs( function forum_increment_clicks(int $forumId): void { - $incrementLinkClicks = Database::connection()->prepare(' + $incrementLinkClicks = Database::prepare(' UPDATE `msz_forum_categories` SET `forum_link_clicks` = `forum_link_clicks` + 1 WHERE `forum_id` = :forum_id @@ -247,7 +245,7 @@ define('MSZ_FORUM_GET_CHILDREN_QUERY_STANDARD', ' function forum_get_children(int $parentId, int $userId, bool $small = false): array { - $getListing = Database::connection()->prepare( + $getListing = Database::prepare( $small ? MSZ_FORUM_GET_CHILDREN_QUERY_SMALL : MSZ_FORUM_GET_CHILDREN_QUERY_STANDARD diff --git a/src/Forum/post.php b/src/Forum/post.php index 7ba3720e..44d0ae29 100644 --- a/src/Forum/post.php +++ b/src/Forum/post.php @@ -23,9 +23,7 @@ function forum_post_create( string $text, int $parser = MSZ_FORUM_POST_PARSER_PLAIN ): int { - $dbc = Database::connection(); - - $createPost = $dbc->prepare(' + $createPost = Database::prepare(' INSERT INTO `msz_forum_posts` (`topic_id`, `forum_id`, `user_id`, `post_ip`, `post_text`, `post_parse`) VALUES @@ -38,12 +36,12 @@ function forum_post_create( $createPost->bindValue('post_text', $text); $createPost->bindValue('post_parse', $parser); - return $createPost->execute() ? $dbc->lastInsertId() : 0; + return $createPost->execute() ? Database::lastInsertId() : 0; } function forum_post_find(int $postId): array { - $getPostInfo = Database::connection()->prepare(' + $getPostInfo = Database::prepare(' SELECT :post_id as `target_post_id`, ( @@ -86,7 +84,7 @@ define('MSZ_FORUM_POST_LISTING_QUERY_PAGINATED', MSZ_FORUM_POST_LISTING_QUERY_ST function forum_post_listing(int $topicId, int $offset = 0, int $take = 0): array { $hasPagination = $offset >= 0 && $take > 0; - $getPosts = Database::connection()->prepare( + $getPosts = Database::prepare( $hasPagination ? MSZ_FORUM_POST_LISTING_QUERY_PAGINATED : MSZ_FORUM_POST_LISTING_QUERY_STANDARD diff --git a/src/Forum/topic.php b/src/Forum/topic.php index c38cdd55..eb175b77 100644 --- a/src/Forum/topic.php +++ b/src/Forum/topic.php @@ -12,9 +12,7 @@ define('MSZ_TOPIC_TYPES', [ function forum_topic_create(int $forumId, int $userId, string $title): int { - $dbc = Database::connection(); - - $createTopic = $dbc->prepare(' + $createTopic = Database::prepare(' INSERT INTO `msz_forum_topics` (`forum_id`, `user_id`, `topic_title`) VALUES @@ -24,12 +22,12 @@ function forum_topic_create(int $forumId, int $userId, string $title): int $createTopic->bindValue('user_id', $userId); $createTopic->bindValue('topic_title', $title); - return $createTopic->execute() ? (int)$dbc->lastInsertId() : 0; + return $createTopic->execute() ? (int)Database::lastInsertId() : 0; } function forum_topic_fetch(int $topicId): array { - $getTopic = Database::connection()->prepare(' + $getTopic = Database::prepare(' SELECT t.`topic_id`, t.`forum_id`, t.`topic_title`, t.`topic_type`, t.`topic_locked`, f.`forum_archived` as `topic_archived`, @@ -58,7 +56,7 @@ function forum_topic_fetch(int $topicId): array function forum_topic_bump(int $topicId): bool { - $bumpTopic = Database::connection()->prepare(' + $bumpTopic = Database::prepare(' UPDATE `msz_forum_topics` SET `topic_bumped` = NOW() WHERE `topic_id` = :topic_id @@ -73,7 +71,7 @@ function forum_topic_mark_read(int $userId, int $topicId, int $forumId): void return; } - $markAsRead = Database::connection()->prepare(' + $markAsRead = Database::prepare(' REPLACE INTO `msz_forum_topics_track` (`user_id`, `topic_id`, `forum_id`, `track_last_read`) VALUES @@ -147,7 +145,7 @@ define('MSZ_TOPIC_LISTING_QUERY_PAGINATED', MSZ_TOPIC_LISTING_QUERY_STANDARD . ' function forum_topic_listing(int $forumId, int $userId, int $offset = 0, int $take = 0): array { $hasPagination = $offset >= 0 && $take > 0; - $getTopics = Database::connection()->prepare( + $getTopics = Database::prepare( $hasPagination ? MSZ_TOPIC_LISTING_QUERY_PAGINATED : MSZ_TOPIC_LISTING_QUERY_STANDARD diff --git a/src/Users/login_attempt.php b/src/Users/login_attempt.php index 491e0743..23896fef 100644 --- a/src/Users/login_attempt.php +++ b/src/Users/login_attempt.php @@ -3,7 +3,7 @@ use Misuzu\Database; function user_login_attempt_record(bool $success, ?int $userId, string $ipAddress, string $userAgent): void { - $storeAttempt = Database::connection()->prepare(' + $storeAttempt = Database::prepare(' INSERT INTO `msz_login_attempts` (`was_successful`, `attempt_ip`, `attempt_country`, `user_id`, `user_agent`, `created_at`) VALUES @@ -20,7 +20,7 @@ function user_login_attempt_record(bool $success, ?int $userId, string $ipAddres function user_login_attempts_remaining(string $ipAddress): int { - $getRemaining = Database::connection()->prepare(' + $getRemaining = Database::prepare(' SELECT 5 - COUNT(`attempt_id`) FROM `msz_login_attempts` WHERE `was_successful` = false diff --git a/src/Users/profile.php b/src/Users/profile.php index 054209c8..eed57459 100644 --- a/src/Users/profile.php +++ b/src/Users/profile.php @@ -125,7 +125,7 @@ function user_profile_fields_set(int $userId, array $fields): array } if (count($values) > 0) { - $updateFields = Database::connection()->prepare(' + $updateFields = Database::prepare(' UPDATE `msz_users` SET ' . pdo_prepare_array_update($values, true) . ' WHERE `user_id` = :user_id diff --git a/src/Users/role.php b/src/Users/role.php index e62c5e49..35a168cd 100644 --- a/src/Users/role.php +++ b/src/Users/role.php @@ -5,7 +5,7 @@ define('MSZ_ROLE_MAIN', 1); function user_role_add(int $userId, int $roleId): bool { - $addRole = Database::connection()->prepare(' + $addRole = Database::prepare(' INSERT INTO `msz_user_roles` (`user_id`, `role_id`) VALUES @@ -18,7 +18,7 @@ function user_role_add(int $userId, int $roleId): bool function user_role_remove(int $userId, int $roleId): bool { - $removeRole = Database::connection()->prepare(' + $removeRole = Database::prepare(' DELETE FROM `msz_user_roles` WHERE `user_id` = :user_id AND `role_id` = :role_id @@ -30,7 +30,7 @@ function user_role_remove(int $userId, int $roleId): bool function user_role_has(int $userId, int $roleId): bool { - $hasRole = Database::connection()->prepare(' + $hasRole = Database::prepare(' SELECT COUNT(`role_id`) > 0 FROM `msz_user_roles` WHERE `user_id` = :user_id @@ -47,7 +47,7 @@ function user_role_set_display(int $userId, int $roleId): bool return false; } - $setDisplay = Database::connection()->prepare(' + $setDisplay = Database::prepare(' UPDATE `msz_users` SET `display_role` = :role_id WHERE `user_id` = :user_id diff --git a/src/Users/session.php b/src/Users/session.php index 993cc908..b3572704 100644 --- a/src/Users/session.php +++ b/src/Users/session.php @@ -10,7 +10,7 @@ function user_session_create( ): string { $sessionKey = user_session_generate_key(); - $createSession = Database::connection()->prepare(' + $createSession = Database::prepare(' INSERT INTO `msz_sessions` ( `user_id`, `session_ip`, `session_country`, @@ -33,7 +33,7 @@ function user_session_create( function user_session_delete(int $sessionId): bool { - $deleteSession = Database::connection()->prepare(' + $deleteSession = Database::prepare(' DELETE FROM `msz_sessions` WHERE `session_id` = :session_id '); diff --git a/src/Users/user.php b/src/Users/user.php index 5c9cf1cb..09600c5e 100644 --- a/src/Users/user.php +++ b/src/Users/user.php @@ -21,8 +21,7 @@ function user_create( string $email, string $ipAddress ): int { - $dbc = Database::connection(); - $createUser = $dbc->prepare(' + $createUser = Database::prepare(' INSERT INTO `msz_users` ( `username`, `password`, `email`, `register_ip`, @@ -41,7 +40,7 @@ function user_create( $createUser->bindValue('last_ip', $ipAddress); $createUser->bindValue('user_country', get_country_code($ipAddress)); - return $createUser->execute() ? (int)$dbc->lastInsertId() : 0; + return $createUser->execute() ? (int)Database::lastInsertId() : 0; } function user_password_hash(string $password): string @@ -55,7 +54,7 @@ function user_generate_chat_key(int $userId): string { $chatKey = bin2hex(random_bytes(16)); - $setChatKey = Database::connection()->prepare(' + $setChatKey = Database::prepare(' UPDATE `msz_users` SET `user_chat_key` = :user_chat_key WHERE `user_id` = :user_id diff --git a/src/Users/validation.php b/src/Users/validation.php index cfa8ec3d..5a49532c 100644 --- a/src/Users/validation.php +++ b/src/Users/validation.php @@ -42,7 +42,7 @@ function user_validate_username(string $username, bool $checkInUse = false): str } if ($checkInUse) { - $getUser = Database::connection()->prepare(' + $getUser = Database::prepare(' SELECT COUNT(`user_id`) FROM `msz_users` WHERE LOWER(`username`) = LOWER(:username) @@ -69,7 +69,7 @@ function user_validate_email(string $email, bool $checkInUse = false): string } if ($checkInUse) { - $getUser = Database::connection()->prepare(' + $getUser = Database::prepare(' SELECT COUNT(`user_id`) FROM `msz_users` WHERE LOWER(`email`) = LOWER(:email) diff --git a/src/changelog.php b/src/changelog.php index 5f5dde0a..b3dff49c 100644 --- a/src/changelog.php +++ b/src/changelog.php @@ -7,15 +7,13 @@ define('MSZ_CHANGELOG_PERM_MANAGE_ACTIONS', 1 << 2); function changelog_action_add(string $name, ?int $colour = null, ?string $class = null): int { - $dbc = Database::connection(); - if ($colour === null) { $colour = colour_none(); } $class = preg_replace('#[^a-z]#', '', strtolower($class ?? $name)); - $addAction = $dbc->prepare(' + $addAction = Database::prepare(' INSERT INTO `msz_changelog_actions` (`action_name`, `action_colour`, `action_class`) VALUES @@ -25,14 +23,12 @@ function changelog_action_add(string $name, ?int $colour = null, ?string $class $addAction->bindValue('action_colour', $colour); $addAction->bindValue('action_class', $class); - return $addAction->execute() ? (int)$dbc->lastInsertId() : 0; + return $addAction->execute() ? (int)Database::lastInsertId() : 0; } function changelog_entry_create(int $userId, int $actionId, string $log, string $text = null): int { - $dbc = Database::connection(); - - $createChange = $dbc->prepare(' + $createChange = Database::prepare(' INSERT INTO `msz_changelog_changes` (`user_id`, `action_id`, `change_log`, `change_text`) VALUES @@ -43,7 +39,7 @@ function changelog_entry_create(int $userId, int $actionId, string $log, string $createChange->bindValue('change_log', $log); $createChange->bindValue('change_text', $text); - return $createChange->execute() ? (int)$dbc->lastInsertId() : 0; + return $createChange->execute() ? (int)Database::lastInsertId() : 0; } define('MSZ_CHANGELOG_GET_QUERY', ' @@ -80,8 +76,7 @@ function changelog_get_changes(string $date, int $user, int $offset, int $take): !$hasDate ? 'LIMIT :offset, :take' : '' ); - $dbc = Database::connection(); - $prep = $dbc->prepare($query); + $prep = Database::prepare($query); if (!$hasDate) { $prep->bindValue('offset', $offset); @@ -115,8 +110,7 @@ function changelog_count_changes(string $date, int $user): int $hasUser ? '`user_id` = :user' : '1' ); - $dbc = Database::connection(); - $prep = $dbc->prepare($query); + $prep = Database::prepare($query); if ($hasDate) { $prep->bindValue('date', $date); diff --git a/src/perms.php b/src/perms.php index 31324998..c98f41f0 100644 --- a/src/perms.php +++ b/src/perms.php @@ -89,7 +89,7 @@ function perms_get_user(string $prefix, int $user): int $permsAllow = 0; $permsDeny = 0; - $getPerms = Database::connection()->prepare(" + $getPerms = Database::prepare(" SELECT `{$prefix}_perms_allow` as `allow`, `{$prefix}_perms_deny` as `deny` FROM `msz_permissions` WHERE (`user_id` = :user_id_1 AND `role_id` IS NULL) @@ -124,7 +124,7 @@ function perms_get_role(string $prefix, int $role): int return perms_get_cache($prefix, 'role', $user); } - $getPerms = Database::connection()->prepare(" + $getPerms = Database::prepare(" SELECT `{$prefix}_perms_allow` &~ `{$prefix}_perms_deny` FROM `msz_permissions` WHERE `role_id` = :role_id @@ -142,7 +142,7 @@ function perms_get_user_raw(int $user): array return $emptyPerms; } - $getPerms = Database::connection()->prepare(' + $getPerms = Database::prepare(' SELECT `' . implode('`, `', perms_get_keys()) . '` FROM `msz_permissions` @@ -172,7 +172,7 @@ function perms_get_role_raw(int $role): array return $emptyPerms; } - $getPerms = Database::connection()->prepare(' + $getPerms = Database::prepare(' SELECT `' . implode('`, `', perms_get_keys()) . '` FROM `msz_permissions`