Skip to content

Commit

Permalink
improvements on worlds queries and serverinfo page. improvement on pa…
Browse files Browse the repository at this point in the history
…ge rules, updating regexes as array.
  • Loading branch information
elsongabriel committed Sep 7, 2024
1 parent e791447 commit e929203
Show file tree
Hide file tree
Showing 12 changed files with 589 additions and 495 deletions.
2 changes: 1 addition & 1 deletion admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

// set worlds in global php and twig
$worlds = $db->hasTable('worlds')
? $db->query('SELECT * FROM `worlds` ORDER BY `id` ASC')->fetchAll()
? $db->query('SELECT * FROM `worlds` ORDER BY `id` ASC')->fetchAll(PDO::FETCH_ASSOC)
: [];
define('WORLDS', $worlds);
$twig->addGlobal('worlds', $worlds);
Expand Down
8 changes: 4 additions & 4 deletions admin/pages/worlds.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function echo_error($message)
$motd = $_POST['motd'] ?? '';

if ($id = $_POST['world_id'] ?? null) {
$world = $id > 0 ? $db->query("SELECT * FROM `worlds` WHERE `id` = {$id}")->fetch() : null;
$world = $id > 0 ? $db->query("SELECT * FROM `worlds` WHERE `id` = {$id}")->fetch(PDO::FETCH_ASSOC) : null;
if (!$world) {
echo_error("World with this id doesn't exist.");
}
Expand All @@ -69,11 +69,11 @@ function echo_error($message)
echo_success("World {$name} saved at: " . date('G:i'));
}
} else {
if ($db->query("SELECT `id` FROM `worlds` WHERE `name` = {$db->quote($name)}")->fetch()) {
if ($db->query("SELECT `id` FROM `worlds` WHERE `name` = {$db->quote($name)}")->fetch(PDO::FETCH_ASSOC)) {
echo_error("World name is already in use!");
} else if ($port == 7171) {
echo_error("World port is unavailable!");
} else if ($db->query("SELECT `id` FROM `worlds` WHERE `port` = {$port}")->fetch()) {
} else if ($db->query("SELECT `id` FROM `worlds` WHERE `port` = {$port}")->fetch(PDO::FETCH_ASSOC)) {
echo_error("World port is already in use!");
}

Expand Down Expand Up @@ -110,7 +110,7 @@ function echo_error($message)
$id = $_REQUEST['id'] ?? 0;

if ($id > 0 || $action === 'add') {
$world = $id > 0 ? $db->query("SELECT * FROM `worlds` WHERE `id` = {$id}")->fetch() : null;
$world = $id > 0 ? $db->query("SELECT * FROM `worlds` WHERE `id` = {$id}")->fetch(PDO::FETCH_ASSOC) : null;
?>
<form action="<?= $base . ($id > 0 ? '&id=' . $id : '') . (!empty($action) ? '&action=' . $action : ''); ?>"
method="post" class="form-horizontal">
Expand Down
2 changes: 1 addition & 1 deletion clientcreateaccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function getWorldsData(): array
{
global $db;
$response = [];
foreach ($db->query("SELECT * from `worlds`")->fetchAll() as $world) {
foreach ($db->query("SELECT * from `worlds`")->fetchAll(PDO::FETCH_ASSOC) as $world) {
$playersOnline = $db->query("SELECT COUNT(*) FROM `players_online` WHERE `world_id` = {$world['id']}")->fetchAll()[0][0] ?? 0;
$response[] = [
"Name" => $world['name'],
Expand Down
109 changes: 59 additions & 50 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,56 +84,65 @@
$_REQUEST['p'] = $uri;
$found = true;
} else {
$regexes = [
'number' => "[0-9]+", // only digits (one or more).
'text' => "[a-zA-Z]+", // only words (one or more).
'alphanumeric' => "[a-zA-Z0-9]+", // words and/or numbers (one or more).
'url' => "[A-Za-z0-9-_]+", // words, numbers, hyphen e underscore (one or more).
'special' => "[A-Za-z0-9-_%+\']+", // words, numbers, hyphen, underscore, percentage, plus signal and apostrophe (one or more).
];

$rules = array(
'/^account\/manage\/?$/' => array('subtopic' => 'accountmanagement'),
'/^account\/create\/?$/' => array('subtopic' => 'createaccount'),
'/^account\/lost\/?$/' => array('subtopic' => 'lostaccount'),
'/^account\/logout\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'logout'),
'/^account\/password\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_password'),
'/^account\/register\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'register'),
'/^account\/register\/new\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'register_new'),
'/^account\/resend\/verify\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'resend_verify'),
'/^account\/email\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_email'),
'/^account\/info\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_info'),
'/^account\/character\/create\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'create_character'),
'/^account\/character\/name\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_name'),
'/^account\/character\/sex\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_sex'),
'/^account\/character\/main\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_main'),
'/^account\/character\/delete\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'delete_character'),
'/^account\/character\/comment\/[A-Za-z0-9-_%+\']+\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_comment', 'name' => '$3'),
'/^account\/character\/comment\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'change_comment'),
'/^account\/confirm_email\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'accountmanagement', 'action' => 'confirm_email', 'v' => '$2'),
'/^characters\/[A-Za-z0-9-_%+\']+$/' => array('subtopic' => 'characters', 'name' => '$1'),
'/^changelog\/[0-9]+\/?$/' => array('subtopic' => 'changelog', 'page' => '$1'),
'/^commands\/add\/?$/' => array('subtopic' => 'commands', 'action' => 'add'),
'/^commands\/edit\/?$/' => array('subtopic' => 'commands', 'action' => 'edit'),
'/^faq\/add\/?$/' => array('subtopic' => 'faq', 'action' => 'add'),
'/^faq\/edit\/?$/' => array('subtopic' => 'faq', 'action' => 'edit'),
'/^forum\/add_board\/?$/' => array('subtopic' => 'forum', 'action' => 'add_board'),#
'/^forum\/edit_board\/?$/' => array('subtopic' => 'forum', 'action' => 'edit_board'),
'/^forum\/board\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_board', 'id' => '$2'),
'/^forum\/board\/[0-9]+\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_board', 'id' => '$2', 'page' => '$3'),
'/^forum\/thread\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_thread', 'id' => '$2'),
'/^forum\/thread\/[0-9]+\/[0-9]+\/?$/' => array('subtopic' => 'forum', 'action' => 'show_thread', 'id' => '$2', 'page' => '$3'),
'/^gallery\/add\/?$/' => array('subtopic' => 'gallery', 'action' => 'add'),
'/^gallery\/edit\/?$/' => array('subtopic' => 'gallery', 'action' => 'edit'),
'/^gallery\/[0-9]+\/?$/' => array('subtopic' => 'gallery', 'image' => '$1'),
'/^gifts\/history\/?$/' => array('subtopic' => 'gifts', 'action' => 'show_history'),
'/^guilds\/[A-Za-z0-9-_%+\']+$/' => array('subtopic' => 'guilds', 'action' => 'show', 'guild' => '$1'),
'/^online\/[A-Za-z0-9-_%+\']+$/' => array('subtopic' => 'online', 'world' => '$1'),
'/^highscores\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/[0-9]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1', 'world' => '$2', 'vocation' => '$3', 'page' => '$4'),
'/^highscores\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/[0-9]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1', 'world' => '$2', 'page' => '$3'),
'/^highscores\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1', 'world' => '$2', 'vocation' => '$3'),
'/^highscores\/[A-Za-z0-9-_]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1', 'world' => '$2'),
'/^highscores\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'highscores', 'list' => '$1'),
'/^lastkills\/[A-Za-z0-9-_%+\']+$/' => array('subtopic' => 'lastkills', 'world' => '$1'),
'/^news\/add\/?$/' => array('subtopic' => 'news', 'action' => 'add'),
'/^news\/edit\/?$/' => array('subtopic' => 'news', 'action' => 'edit'),
'/^news\/archive\/?$/' => array('subtopic' => 'newsarchive'),
'/^news\/archive\/[0-9]+\/?$/' => array('subtopic' => 'newsarchive', 'id' => '$2'),
'/^polls\/[0-9]+\/?$/' => array('subtopic' => 'polls', 'id' => '$1'),
'/^spells\/[A-Za-z0-9-_%]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'spells', 'vocation' => '$1', 'order' => '$2'),
'/^houses\/view\/?$/' => array('subtopic' => 'houses', 'page' => 'view')
"/^account\/manage\/?$/" => array("subtopic" => "accountmanagement"),
"/^account\/create\/?$/" => array("subtopic" => "createaccount"),
"/^account\/lost\/?$/" => array("subtopic" => "lostaccount"),
"/^account\/logout\/?$/" => array("subtopic" => "accountmanagement", "action" => "logout"),
"/^account\/password\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_password"),
"/^account\/register\/?$/" => array("subtopic" => "accountmanagement", "action" => "register"),
"/^account\/register\/new\/?$/" => array("subtopic" => "accountmanagement", "action" => "register_new"),
"/^account\/resend\/verify\/?$/" => array("subtopic" => "accountmanagement", "action" => "resend_verify"),
"/^account\/email\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_email"),
"/^account\/info\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_info"),
"/^account\/character\/create\/?$/" => array("subtopic" => "accountmanagement", "action" => "create_character"),
"/^account\/character\/name\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_name"),
"/^account\/character\/sex\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_sex"),
"/^account\/character\/main\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_main"),
"/^account\/character\/delete\/?$/" => array("subtopic" => "accountmanagement", "action" => "delete_character"),
"/^account\/character\/comment\/{$regexes['special']}\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_comment", "name" => "$3"),
"/^account\/character\/comment\/?$/" => array("subtopic" => "accountmanagement", "action" => "change_comment"),
"/^account\/confirm_email\/{$regexes['url']}\/?$/" => array("subtopic" => "accountmanagement", "action" => "confirm_email", "v" => "$2"),
"/^characters\/{$regexes['special']}$/" => array("subtopic" => "characters", "name" => "$1"),
"/^changelog\/{$regexes['number']}\/?$/" => array("subtopic" => "changelog", "page" => "$1"),
"/^commands\/add\/?$/" => array("subtopic" => "commands", "action" => "add"),
"/^commands\/edit\/?$/" => array("subtopic" => "commands", "action" => "edit"),
"/^faq\/add\/?$/" => array("subtopic" => "faq", "action" => "add"),
"/^faq\/edit\/?$/" => array("subtopic" => "faq", "action" => "edit"),
"/^forum\/add_board\/?$/" => array("subtopic" => "forum", "action" => "add_board"),#
"/^forum\/edit_board\/?$/" => array("subtopic" => "forum", "action" => "edit_board"),
"/^forum\/board\/{$regexes['number']}\/?$/" => array("subtopic" => "forum", "action" => "show_board", "id" => "$2"),
"/^forum\/board\/{$regexes['number']}\/{$regexes['number']}\/?$/" => array("subtopic" => "forum", "action" => "show_board", "id" => "$2", "page" => "$3"),
"/^forum\/thread\/{$regexes['number']}\/?$/" => array("subtopic" => "forum", "action" => "show_thread", "id" => "$2"),
"/^forum\/thread\/{$regexes['number']}\/{$regexes['number']}\/?$/" => array("subtopic" => "forum", "action" => "show_thread", "id" => "$2", "page" => "$3"),
"/^gallery\/add\/?$/" => array("subtopic" => "gallery", "action" => "add"),
"/^gallery\/edit\/?$/" => array("subtopic" => "gallery", "action" => "edit"),
"/^gallery\/{$regexes['number']}\/?$/" => array("subtopic" => "gallery", "image" => "$1"),
"/^gifts\/history\/?$/" => array("subtopic" => "gifts", "action" => "show_history"),
"/^guilds\/{$regexes['special']}$/" => array("subtopic" => "guilds", "action" => "show", "guild" => "$1"),
"/^online\/{$regexes['special']}$/" => array("subtopic" => "online", "world" => "$1"),
"/^highscores\/{$regexes['url']}\/{$regexes['special']}\/{$regexes['text']}\/{$regexes['number']}\/?$/" => array("subtopic" => "highscores", "list" => "$1", "world" => "$2", "vocation" => "$3", "page" => "$4"),
"/^highscores\/{$regexes['url']}\/{$regexes['special']}\/{$regexes['number']}\/?$/" => array("subtopic" => "highscores", "list" => "$1", "world" => "$2", "page" => "$3"),
"/^highscores\/{$regexes['url']}\/{$regexes['special']}\/{$regexes['text']}\/?$/" => array("subtopic" => "highscores", "list" => "$1", "world" => "$2", "vocation" => "$3"),
"/^highscores\/{$regexes['url']}\/{$regexes['text']}\/?$/" => array("subtopic" => "highscores", "list" => "$1", "vocation" => "$2"),
"/^highscores\/{$regexes['url']}\/{$regexes['special']}\/?$/" => array("subtopic" => "highscores", "list" => "$1", "world" => "$2"),
"/^highscores\/{$regexes['url']}\/?$/" => array("subtopic" => "highscores", "list" => "$1"),
"/^lastkills\/{$regexes['special']}$/" => array("subtopic" => "lastkills", "world" => "$1"),
"/^news\/add\/?$/" => array("subtopic" => "news", "action" => "add"),
"/^news\/edit\/?$/" => array("subtopic" => "news", "action" => "edit"),
"/^news\/archive\/?$/" => array("subtopic" => "newsarchive"),
"/^news\/archive\/{$regexes['number']}\/?$/" => array("subtopic" => "newsarchive", "id" => "$2"),
"/^polls\/{$regexes['number']}\/?$/" => array("subtopic" => "polls", "id" => "$1"),
"/^spells\/{$regexes['text']}\/{$regexes['url']}\/?$/" => array("subtopic" => "spells", "vocation" => "$1", "order" => "$2"),
"/^houses\/view\/?$/" => array("subtopic" => "houses", "page" => "view")
);

foreach ($rules as $rule => $redirect) {
Expand Down Expand Up @@ -184,7 +193,7 @@
}

// set worlds in global php and twig
$worlds = $db->hasTable('worlds') ? $db->query("SELECT `id`, `name` FROM `worlds` ORDER BY `name` ASC")->fetchAll() : [];
$worlds = $db->hasTable('worlds') ? $db->query("SELECT `id`, `name` FROM `worlds` ORDER BY `name` ASC")->fetchAll(PDO::FETCH_ASSOC) : [];
define('WORLDS', $worlds);
$twig->addGlobal('worlds', $worlds);

Expand Down
2 changes: 1 addition & 1 deletion system/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ function getWorldName($id): string
{
global $db;
if ($db->hasTable('worlds')) {
if ($world = $db->query("SELECT `name` FROM `worlds` WHERE `id` = {$id}")->fetch()) {
if ($world = $db->query("SELECT `name` FROM `worlds` WHERE `id` = {$id}")->fetch(PDO::FETCH_ASSOC)) {
return $world['name'];
}
}
Expand Down
2 changes: 1 addition & 1 deletion system/libs/CreateCharacter.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function doCreate($worldName, $name, $sex, $vocation, $town, $account, &$
return false;
}

if (!$worldId = $db->query("SELECT `id` FROM `worlds` WHERE `name` = {$db->quote($worldName)}")->fetch()['id'] ?? null) {
if (!$worldId = $db->query("SELECT `id` FROM `worlds` WHERE `name` = {$db->quote($worldName)}")->fetch(PDO::FETCH_ASSOC)['id'] ?? null) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion system/pages/guilds.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

$world = null;
if ($worldId = $_POST['world_id'] ?? null) {
$world = $db->query("SELECT `id`, `name` FROM `worlds` WHERE `id` = $worldId")->fetch();
$world = $db->query("SELECT `id`, `name` FROM `worlds` WHERE `id` = $worldId")->fetch(PDO::FETCH_ASSOC);
}

if (!empty($action) && in_array($action, ['create']) && !$world) {
Expand Down
2 changes: 1 addition & 1 deletion system/pages/highscores.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
}

if (!empty($world)) {
if (!$world = $db->query("SELECT `id`, `name` FROM `worlds` WHERE `name` = {$db->quote(urldecode($world))}")->fetch() ?? null) {
if (!$world = $db->query("SELECT `id`, `name` FROM `worlds` WHERE `name` = {$db->quote(urldecode($world))}")->fetch(PDO::FETCH_ASSOC) ?? null) {
header('Location: ' . "?highscores");
return;
}
Expand Down
2 changes: 1 addition & 1 deletion system/pages/lastkills.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}

if ($world = $_GET['world'] ?? null) {
$world = $db->query("SELECT `id`, `name` FROM `worlds` WHERE `name` = {$db->quote(urldecode($world))}")->fetch() ?? null;
$world = $db->query("SELECT `id`, `name` FROM `worlds` WHERE `name` = {$db->quote(urldecode($world))}")->fetch(PDO::FETCH_ASSOC) ?? null;
} else {
$world = count(WORLDS) == 1 ? WORLDS[0] : $world;
}
Expand Down
2 changes: 1 addition & 1 deletion system/pages/online.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

if ($world = $_GET['world'] ?? null) {
$world = $db->query("SELECT * FROM `worlds` WHERE `name` = {$db->quote(urldecode($world))}")->fetch() ?? null;
$world = $db->query("SELECT * FROM `worlds` WHERE `name` = {$db->quote(urldecode($world))}")->fetch(PDO::FETCH_ASSOC) ?? null;
} else {
$world = count(WORLDS) == 1 ? WORLDS[0] : $world;
}
Expand Down
Loading

0 comments on commit e929203

Please sign in to comment.