revoked && (int) $client->expires > $now && in_array( (int) $vault, array_map("intval", json_decode($client->vaults, true) ?: []), true, ); } public static function password($value, $unused = null) { // Passwords are exact, not trimmed, case-folded or HTML-sanitized. Count Unicode code points. if ( !is_string($value) || !preg_match("//u", $value) || preg_match('/[\x00-\x1f\x7f]/', $value) ) { return null; } $length = preg_match_all("/./us", $value); return $length >= 1 && $length <= 100 ? $value : null; } public static function modes() { return [ "dial" => "Dial only", "phrase" => "Seed words only", "password" => "Password only", "both" => "Dial AND seed words", "either" => "Dial OR seed words", "dial_password" => "Dial AND password", "dial_or_password" => "Dial OR password", "phrase_password" => "Seed words AND password", "phrase_or_password" => "Seed words OR password", ]; } public static function methods($mode) { return [ "dial" => ["dial"], "phrase" => ["phrase"], "password" => ["password"], "both" => ["dial", "phrase"], "either" => ["dial", "phrase"], "dial_password" => ["dial", "password"], "dial_or_password" => ["dial", "password"], "phrase_password" => ["phrase", "password"], "phrase_or_password" => ["phrase", "password"], ][$mode] ?? []; } public static function matches($mode, $dial, $phrase, $password = false) { $values = [ "dial" => (bool) $dial, "phrase" => (bool) $phrase, "password" => (bool) $password, ]; $methods = self::methods($mode); if (!$methods) { return false; } $any = $mode === "either" || strpos($mode, "_or_") !== false; foreach ($methods as $kind) { if ($any && $values[$kind]) { return true; } if (!$any && !$values[$kind]) { return false; } } return !$any; } public static function compatible($v, $g) { $dial = !empty($g->dial_hash) && (int) $v->dial_count === (int) $g->dial_count; $phrase = !empty($g->phrase_hash) && (int) $v->phrase_count === (int) $g->phrase_count; return self::matches( $v->mode, $dial, $phrase, !empty($g->password_hash), ); } public static function configuration($v) { $labels = [ "dial" => $v->dial_count . " dial stops", "phrase" => $v->phrase_count . " seed words", "password" => "Password", ]; $methods = self::methods($v->mode); if (!$methods) { return "Unknown lock"; } return implode( $v->mode === "either" || strpos($v->mode, "_or_") !== false ? " OR " : " AND ", array_map(static function ($k) use ($labels) { return $labels[$k]; }, $methods), ); } public static function hard_expiry($now, $clientExpiry, $hours) { return $hours > 0 ? min($clientExpiry, $now + $hours * 3600) : $clientExpiry; } public static function session_expiry($now, $clientExpiry, $existing = null) { if ( $clientExpiry <= $now || ($existing !== null && $existing <= $now) ) { return null; } return min( ($existing === null ? $now : $existing) + 600, $now + 3600, $clientExpiry, ); } }