Add support for simple URLs (e.g. /bans/)

This commit is contained in:
ruan 2020-06-21 00:39:16 +02:00
parent 2333fda8df
commit 9630eaa52b
22 changed files with 156 additions and 49 deletions

View File

@ -35,7 +35,7 @@ class Check {
if (!isset($uuid)) {
if (filter_var($name, FILTER_VALIDATE_FLOAT)) {
echo "<br>";
redirect("info.php?id=$name&type=$type");
redirect($page->link("info.php?id=$name&type=$type"));
return;
}
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
@ -50,7 +50,7 @@ class Check {
}
echo "<br>";
redirect($href);
redirect($page->link($href));
/*
$table = $page->settings->table['bans'];

View File

@ -1,6 +1,5 @@
<?php
require_once './inc/page.php';
require_once './info.php';
class History {
/**
@ -69,11 +68,19 @@ class History {
$page = new Page("history");
isset($_GET['uuid']) && is_string($_GET['uuid']) or die($page->t("error.missing-args"));
$args = $page->args;
$staffhistory = (isset($_GET['staffhistory']) && $_GET['staffhistory'] === "1");
count($args) > 0 && is_string($args[0]) or die($page->t("error.missing-args"));
$uuid = $page->uuid_dashify($_GET['uuid']);
//$staffhistory = (count($args) >= 2 && $args[1] === "1");
$staffhistory=false;
$arg0 = $args[0];
if (strstr($arg0, ":issued")) {
$staffhistory=true;
$arg0 = substr($arg0, 0, -strlen(":issued"));
}
$uuid = $page->uuid_dashify($arg0);
$name = $page->get_name($uuid);
$name !== null or die(str_replace("{name}", $name, $page->t("error.name.unseen")));
@ -99,7 +106,7 @@ if (isset($_GET['from'])) {
if ($info['type'] !== null) {
$from_title = $info['title'];
$from = Page::lc_first($from_title);
$from_href = $info['page'];
$from_href = $page->link($info['page']);
}
}
@ -197,7 +204,7 @@ try {
$player_name = $page->get_name($row['uuid']);
$label_type = $page->type;
$label_name = Info::create($row, $player_name, $page, $label_type)->name(); //ucfirst($label_type);
$label_name = $page->t("generic." . $label_type);
$label = "<span class='$bc litebans-label-history litebans-label-$label_type'>$label_name</span>";
$page->print_table_rows($row, array(
@ -217,12 +224,18 @@ try {
// print pager
if ($page->settings->show_pager) {
$page->name = "history";
$args = "&uuid=$uuid";
if ($from !== null) {
$args .= "&from=$from";
}
$target = null;
if ($staffhistory) {
$args .= "&staffhistory=1";
// $args .= "&staffhistory=1";
// $args .= ":issued";
$uuid .= ":issued";
}
$args = "?uuid=$uuid";
if ($page->settings->simple_urls) {
$target = $page->name . "/$uuid";
$args = "";
}
$prevargs = $args;
@ -233,7 +246,13 @@ try {
$prevargs .= "&after=$after";
}
$page->print_pager($total, $args, $prevargs);
if ($from !== null) {
$args .= "&from=$from";
$prevargs .= "&from=$from";
}
$page->print_pager($total, $args, $prevargs, $target, false);
}
} else {
echo $page->t("history.error.uuid.no-result") . "<br>";

View File

@ -1,3 +1,3 @@
<script src="<?php echo $this->autoversion('inc/js/jquery-3.3.1.min.js'); ?>"></script>
<script src="<?php echo $this->autoversion('inc/js/bootstrap.min.js'); ?>"></script>
<script src="<?php echo $this->resource('inc/js/jquery-3.3.1.min.js'); ?>"></script>
<script src="<?php echo $this->resource('inc/js/bootstrap.min.js'); ?>"></script>
<?php echo "</html>"; ?>

View File

@ -34,10 +34,15 @@ function __construct($page) {
function navbar($links) {
echo '<ul class="navbar-nav mr-auto">';
$request = $this->page->get_requested_page();
foreach ($links as $page => $title) {
$li = "li";
$class = "nav-item";
if ((substr($_SERVER['SCRIPT_NAME'], -strlen($page))) === $page) {
if ($this->page->settings->simple_urls) {
if ("$request.php" === $page) {
$class .= " active navbar-active";
}
} else if ((substr($_SERVER['SCRIPT_NAME'], -strlen($page))) === $page) {
$class .= " active navbar-active";
}
$li .= " class=\"$class\"";
@ -47,6 +52,7 @@ function navbar($links) {
$title .= $this->count[$page];
$title .= "</span>";
}
$page = $this->page->link($page);
echo "<$li><a class=\"nav-link\" href=\"$page\">$title</a></li>";
}
echo '</ul>';
@ -62,11 +68,11 @@ $settings = $this->page->settings;
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="LiteBans">
<link href="<?php echo $this->page->autoversion('inc/img/favicon.ico'); ?>" rel="shortcut icon">
<link href="<?php echo $this->page->resource('inc/img/favicon.ico'); ?>" rel="shortcut icon">
<!-- CSS -->
<link href="<?php echo $this->page->autoversion('inc/css/bootstrap.min.css'); ?>" rel="stylesheet">
<link href="<?php echo $this->page->autoversion('inc/css/glyphicons.min.css'); ?>" rel="stylesheet">
<link href="<?php echo $this->page->autoversion('inc/css/custom.css'); ?>" rel="stylesheet">
<link href="<?php echo $this->page->resource('inc/css/bootstrap.min.css'); ?>" rel="stylesheet">
<link href="<?php echo $this->page->resource('inc/css/glyphicons.min.css'); ?>" rel="stylesheet">
<link href="<?php echo $this->page->resource('inc/css/custom.css'); ?>" rel="stylesheet">
<script type="text/javascript">
function withjQuery(tries, f) {
if (window.jQuery) f();

View File

@ -26,13 +26,7 @@ class Page {
$this->conn = $settings->conn;
$this->settings = $settings;
$this->uuid_name_cache = array();
$this->page = 1;
if (isset($_GET['page'])) {
$page = $_GET['page']; // user input
if (filter_var($page, FILTER_VALIDATE_INT)) {
$this->page = max(0, (int)$page);
}
}
$this->name = $name;
$this->type = null;
@ -67,12 +61,48 @@ class Page {
'kick' => $this->t("generic.kicked.by"),
);
$this->table_headers_printed = false;
$this->args = array_values($_GET);
$this->is_index = ((substr($_SERVER['SCRIPT_NAME'], -strlen("index.php"))) === "index.php");
if ($this->is_index) {
$this->index_base_path = substr($_SERVER["PHP_SELF"], 0, -strlen("index.php"));
if ($settings->simple_urls) {
$keys = array_keys($_GET);
$request_path = $keys[0];
$local_path = substr($request_path, strlen($this->index_base_path));
$this->args = explode("/", substr($local_path, strpos($local_path, "/") + 1));
}
}
$argc = count($this->args);
$this->page = 1;
$page = "1";
if (isset($_GET['page'])) {
$page = $_GET['page']; // user input
} else {
if ($argc > 1) {
$page = $this->args[$argc-2];
}
}
if (filter_var($page, FILTER_VALIDATE_INT)) {
$this->page = max(0, (int)$page);
}
if ($header) {
$h = new Header($this);
$this->header = $h;
$h->print_header();
}
$this->table_headers_printed = false;
}
public function get_requested_page() {
$keys = array_keys($_GET);
if (count($keys) == 0) return "";
$request_path = $keys[0];
$local_path = substr($request_path, strlen($this->index_base_path));
return substr($local_path, 0, strpos($local_path, "/"));
}
public function t($str) {
@ -215,7 +245,7 @@ class Page {
$uuid = $this->uuid_undashify($uuid);
$src = str_replace('{name}', $name, str_replace('{uuid}', $uuid, $avatar_source));
if (in_array($name, $this->settings->console_aliases) || $name === $this->settings->console_name) {
$src = $this->settings->console_image;
$src = $this->resource($this->settings->console_image);
$name = $this->settings->console_name;
}
if ($name_repl !== null) {
@ -473,7 +503,8 @@ class Page {
$a .= " class=\"glyphicon $icon\" aria-hidden=true";
$text = "";
}
echo "<td><$a href=\"info.php?type=$type&id=$id\">$text</a></td>";
$href = $this->link("info.php?type=$type&id=$id");
echo "<td><$a href=\"$href\">$text</a></td>";
}
echo "</tr>";
}
@ -501,6 +532,7 @@ class Page {
}
function print_check_form() {
$link = $this->link('check.php');
$table = $this->name;
echo '
<div class="row litebans-check">
@ -513,17 +545,19 @@ class Page {
<button type="submit" class="btn btn-primary" style="margin-left: 5px;">' . $this->t("action.check") . '</button>
</form>
</div>
<script type="text/javascript">function captureForm(b){var o=$(".litebans-check-output");o.removeClass("show");var x=setTimeout(function(){o.html("<br>")}, 150);$.ajax({type:"GET",url:"check.php?name="+$("#user").val()+"&table=' . $table . '"}).done(function(c){clearTimeout(x);o.html(c);o.addClass("show")});b.preventDefault();return false};</script>
<script type="text/javascript">function captureForm(b){var o=$(".litebans-check-output");o.removeClass("show");var x=setTimeout(function(){o.html("<br>")}, 150);$.ajax({type:"GET",url:"' . $link . '?name="+$("#user").val()+"&table=' . $table . '"}).done(function(c){clearTimeout(x);o.html(c);o.addClass("show")});b.preventDefault();return false};</script>
</div>
<div class="litebans-check litebans-check-output fade" class="success fade" data-alert="alert"></div>
<br>
';
}
function print_pager($total = -1, $args = "", $prevargs = "") {
function print_pager($total = -1, $args = "", $prevargs = "", $page = null, $simple=true) {
if (!$this->settings->show_pager) return;
$table = $this->table;
$page = $this->name . ".php";
if ($page === null) {
$page = $this->name . ".php";
}
if ($total === -1) {
$where = $this->where_append($this->name === "kicks" ? "" : $this->settings->active_query);
@ -548,13 +582,21 @@ class Page {
$next_class = "litebans-" . ($next_active ? "pager-active" : "pager-inactive");
$pager_prev = "<div class=\"litebans-pager litebans-pager-left $prev_class\">«</div>";
if ($prev_active) {
$pager_prev = "<a href=\"$page?page={$prev}{$prevargs}\">$pager_prev</a>";
$pager_next = "<div class=\"litebans-pager litebans-pager-right $next_class\">»</div>";
if ($simple) {
$pager_prev_href = $this->link("$page{$prevargs}&page={$prev}");
$pager_next_href = $this->link("$page{$args}&page={$next}");
} else {
$pager_prev_href = $this->link("$page") . "{$prevargs}&page={$prev}";
$pager_next_href = $this->link("$page") . "{$args}&page={$next}";
}
$pager_next = "<div class=\"litebans-pager litebans-pager-right $next_class\">»</div>";
if ($prev_active) {
$pager_prev = "<a href=\"$pager_prev_href\">$pager_prev</a>";
}
if ($next_active) {
$pager_next = "<a href=\"$page?page={$next}{$args}\">$pager_next</a>";
$pager_next = "<a href=\"$pager_next_href\">$pager_next</a>";
}
$pager_count = '<div><div class="litebans-pager-number">' . $this->t("table.pager.number") . ' ' . $cur . '/' . $pages . '</div></div>';
echo "$pager_prev $pager_next $pager_count";
@ -570,8 +612,24 @@ class Page {
include_once './inc/footer.php';
}
function autoversion($file) {
return "$file?" . filemtime($file);
function link($url) {
if ($this->settings->simple_urls && $this->is_index) {
$url = preg_replace("/\.php/", "", $url, 1);
$url = preg_replace("/\?[a-z]+=/", "/", $url);
$url = preg_replace("/&[a-z]+=/", "/", $url);
$url = $this->index_base_path . $url . "/";
}
return $url;
}
function resource($file) {
if (!file_exists($file)) return $file;
$mtime = filemtime($file);
if ($this->settings->simple_urls && $this->is_index) {
$file = $this->index_base_path . $file;
}
return "$file?" . $mtime;
}
function table_begin() {

View File

@ -98,6 +98,13 @@ final class Settings {
// Enable error pages.
$this->error_pages = true;
// Enable simple URLs?
// This will convert URLs like "example.com/punishments/bans.php" to "example.com/punishments/bans/"
// It will also simplify URL parameters: "example.com/punishments/info.php?type=mute&id=94" -> "example.com/punishments/info/mute/94/"
// Your web server must be configured correctly to allow this to work, otherwise you will get a 404 error.
// Web server configuration: https://gitlab.com/ruany/litebans-php/-/wikis/Simple-URLs
$this->simple_urls = false;
$this->date_month_translations = null;
// If your system locale doesn't automatically translate month names, you can set them manually here.

View File

@ -2,6 +2,18 @@
require_once './inc/page.php';
$page = new Page("index");
if ($page->settings->simple_urls && count($_GET) !== 0) {
$target = $page->get_requested_page();
if ($target !== "index" && strlen($target) <= 16 && preg_match("/^[a-z]+$/", $target)) {
$local_script = "./${target}.php";
if (file_exists($local_script)) {
include_once $local_script;
return;
}
}
}
$page->print_title();
?>
<br>

View File

@ -41,13 +41,14 @@ abstract class Info {
function history_link($name, $uuid, $args = "") {
$uuid = $this->page->uuid_undashify($uuid);
return "<a href=\"history.php?uuid=$uuid$args\">$name</a>";
$href = $this->page->link("history.php?uuid=$uuid$args");
return "<a href=\"$href\">$name</a>";
}
function moderator_avatar() {
$row = $this->row;
$banner_name = $this->page->get_banner_name($row);
return $this->page->get_avatar($banner_name, $row['banned_by_uuid'], true, $this->history_link($banner_name, $row['banned_by_uuid'], "&staffhistory=1"), $name_left = false);
return $this->page->get_avatar($banner_name, $row['banned_by_uuid'], true, $this->history_link($banner_name, $row['banned_by_uuid'], ":issued"), $name_left = false);
}
function basic_info() {
@ -78,15 +79,19 @@ class KickInfo extends Info {
}
// check if info.php is requested, otherwise it's included
if ((substr($_SERVER['SCRIPT_NAME'], -strlen("info.php"))) !== "info.php") {
return;
}
//if ((substr($_SERVER['SCRIPT_NAME'], -strlen("info.php"))) !== "info.php" && ((substr($_SERVER['SCRIPT_NAME'], -strlen("index.php"))) !== "index.php")) {
// return;
//}
isset($_GET['type'], $_GET['id']) && is_string($_GET['type']) && is_string($_GET['id']) or die($page->t("error.missing-args"));
$page = new Page("info");
$args = $page->args;
$type = $_GET['type'];
$id = $_GET['id'];
$page = new Page($type);
count($args) >= 2 && is_string($args[0]) && is_string($args[1]) or die($page->t("error.missing-args"));
$type = $args[0];
$id = $args[1];
$page->set_info($page->type_info($type));
($page->type !== null) or die("Unknown page type requested");

0
lang/README.md Normal file → Executable file
View File

0
lang/cs_CZ.utf8.php Normal file → Executable file
View File

0
lang/da_DK.utf8.php Normal file → Executable file
View File

0
lang/de_DE.utf8.php Normal file → Executable file
View File

0
lang/en_US.utf8.php Normal file → Executable file
View File

0
lang/es_ES.utf8.php Normal file → Executable file
View File

0
lang/fr_FR.utf8.php Normal file → Executable file
View File

0
lang/it_IT.utf8.php Normal file → Executable file
View File

0
lang/ja_JP.utf8.php Normal file → Executable file
View File

0
lang/nl_NL.utf8.php Normal file → Executable file
View File

0
lang/pl_PL.utf8.php Normal file → Executable file
View File

0
lang/ru_RU.utf8.php Normal file → Executable file
View File

0
lang/zh_CN.utf8.php Normal file → Executable file
View File

0
lang/zh_HK.utf8.php Normal file → Executable file
View File