This commit is contained in:
ruan 2018-09-22 14:22:19 +02:00
parent eaee415b66
commit c8eb678db9
No known key found for this signature in database
GPG Key ID: 0D2EC1C52E469C0B
2 changed files with 43 additions and 2 deletions

View File

@ -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'];

View File

@ -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;