From c8eb678db96bc48bb0fa8578fb4c99c88846c1f4 Mon Sep 17 00:00:00 2001 From: ruan Date: Sat, 22 Sep 2018 14:22:19 +0200 Subject: [PATCH] Add #39 --- check.php | 10 ++++++++-- inc/page.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/check.php b/check.php index bb5d699..bdbc17b 100644 --- a/check.php +++ b/check.php @@ -4,15 +4,21 @@ require_once './inc/page.php'; class Check { public function run($name, $from) { $page = new Page("check", false); + + $column = "name"; + // validate user input - if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) { + if ($page->is_uuid($name) && preg_match("/^[0-9a-zA-Z-]{32,36}$/", $name)) { + $column = "uuid"; + $name = $page->uuid_dashify($name); + } else if (strlen($name) > 16 || !preg_match("/^[0-9a-zA-Z_]{1,16}$/", $name)) { $this->println($page->t("error.name.invalid")); return; } $history = $page->settings->table['history']; try { - $stmt = $page->conn->prepare("SELECT name,uuid FROM $history WHERE name=? ORDER BY date LIMIT 1"); + $stmt = $page->conn->prepare("SELECT name,uuid FROM $history WHERE $column=? ORDER BY date LIMIT 1"); if ($stmt->execute(array($name))) { if ($row = $stmt->fetch()) { $name = $row['name']; diff --git a/inc/page.php b/inc/page.php index 232bda5..56eda0a 100644 --- a/inc/page.php +++ b/inc/page.php @@ -396,6 +396,41 @@ class Page { return ($millis > $until); } + /** + * Returns true if a string should be treated as a UUID. + * @param $str + * @return bool + */ + function is_uuid($str) { + $len = strlen($str); + return $len == 32 || $len == 36; + } + + function uuid_dashify($str) { + $len = strlen($str); + if ($len !== 32) return $str; + $newstr = ""; + $total = 0; // current character (all chars) + $cur = 0; // current position in chunk, resets to 0 when it reaches limit + $chunk = 0; // current amount of "-" characters, 5 chunks are in a UUID (1-2-3-4-5) + $limit = 8; // maximum amount of characters in the current chunk (8-4-4-4-12) + for ($i = 0; $i < $len; $i++) { + $chr = $str[$i]; + $total++; + $newstr .= $chr; + if (++$cur >= $limit) { + $cur = 0; + if ($total < 32) { + $newstr .= '-'; + } + $chunk++; + if ($chunk == 1) $limit = 4; + if ($chunk >= 4) $limit = 12; + } + } + return $newstr; + } + function print_title() { $title = $this->title; $name = $this->settings->name;