mirror of
https://gitlab.com/ruany/litebans-php.git
synced 2025-05-23 16:32:45 +00:00
Add #39
This commit is contained in:
parent
eaee415b66
commit
c8eb678db9
10
check.php
10
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'];
|
||||
|
35
inc/page.php
35
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user