forked from laysense/laysense.cn
Fix CMS login and restore PAGEPAN feature icons
This commit is contained in:
@@ -79,6 +79,12 @@ final class Database
|
||||
self::json($item['tags']), $item['status'], $item['date'], $item['date'], $item['legacy'],
|
||||
]);
|
||||
}
|
||||
|
||||
$migration = $pdo->prepare('INSERT OR IGNORE INTO schema_migrations (name) VALUES (?)');
|
||||
$migration->execute(['restore_pagepan_feature_icons']);
|
||||
if ($migration->rowCount() === 1) {
|
||||
PagepanFeatureIcons::migrate($pdo);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ final class Html
|
||||
private const TAGS = [
|
||||
'a' => ['href'], 'article' => [], 'b' => [], 'blockquote' => [], 'br' => [],
|
||||
'caption' => [], 'code' => [], 'dd' => [], 'details' => [], 'div' => ['role', 'aria-label', 'tabindex'],
|
||||
'dl' => [], 'dt' => [], 'em' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'hr' => [], 'i' => [],
|
||||
'dl' => [], 'dt' => [], 'em' => [], 'h1' => [], 'h2' => [], 'h3' => [], 'h4' => [], 'h5' => [], 'h6' => [], 'hr' => [], 'i' => [],
|
||||
'img' => ['src', 'alt', 'loading', 'width', 'height'], 'label' => ['for'],
|
||||
'li' => [], 'ol' => [], 'p' => [], 'pre' => [], 'progress' => ['max', 'value'], 's' => [],
|
||||
'small' => [], 'source' => ['src', 'type'], 'span' => [], 'strong' => [], 'summary' => [],
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMXPath;
|
||||
use PDO;
|
||||
|
||||
final class PagepanFeatureIcons
|
||||
{
|
||||
public static function migrate(PDO $pdo): void
|
||||
{
|
||||
$query = $pdo->query("SELECT body FROM sections WHERE page_slug = 'pagepan' AND key = 'features'");
|
||||
$body = $query->fetchColumn();
|
||||
if (!is_string($body) || $body === '') {
|
||||
return;
|
||||
}
|
||||
$paths = json_decode(file_get_contents(dirname(__DIR__, 2) . '/resources/pagepan-feature-icons.json'), true, flags: JSON_THROW_ON_ERROR);
|
||||
$document = new DOMDocument('1.0', 'UTF-8');
|
||||
$document->loadHTML('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>' . $body . '</body></html>', LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_NONET);
|
||||
$xpath = new DOMXPath($document);
|
||||
|
||||
$changed = false;
|
||||
foreach ($xpath->query('//article[contains(concat(" ", normalize-space(@class), " "), " feature-card ")]') as $card) {
|
||||
$heading = $xpath->query('(./h2 | ./h3 | ./h4)[1]', $card)->item(0);
|
||||
if (!$heading instanceof DOMElement || $card->getElementsByTagName('img')->length > 0) {
|
||||
continue;
|
||||
}
|
||||
$title = trim($heading->textContent);
|
||||
if (!isset($paths[$title])) {
|
||||
continue;
|
||||
}
|
||||
$icon = $document->createElement('img');
|
||||
foreach (['src' => $paths[$title], 'alt' => '', 'width' => '50', 'height' => '50', 'loading' => 'lazy'] as $key => $value) {
|
||||
$icon->setAttribute($key, $value);
|
||||
}
|
||||
$card->insertBefore($icon, $card->firstChild);
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if ($changed) {
|
||||
$updated = '';
|
||||
foreach ($document->getElementsByTagName('body')->item(0)->childNodes as $child) {
|
||||
$updated .= $document->saveHTML($child);
|
||||
}
|
||||
$pdo->prepare("UPDATE sections SET body = ?, version = version + 1 WHERE page_slug = 'pagepan' AND key = 'features'")
|
||||
->execute([trim($updated)]);
|
||||
$pdo->prepare("UPDATE pages SET updated_at = ? WHERE slug = 'pagepan'")
|
||||
->execute([(new \DateTimeImmutable('now', new \DateTimeZone('Asia/Shanghai')))->format('Y-m-d H:i:s')]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
final class Passwords
|
||||
{
|
||||
public const MIN_LENGTH = 8;
|
||||
|
||||
public static function isSupported(string $password): bool
|
||||
{
|
||||
return strlen($password) <= 72 && !str_contains($password, "\0") && mb_check_encoding($password, 'UTF-8');
|
||||
}
|
||||
|
||||
public static function isValidNew(string $password): bool
|
||||
{
|
||||
return self::isSupported($password) && mb_strlen($password, 'UTF-8') >= self::MIN_LENGTH;
|
||||
}
|
||||
}
|
||||
+19
-11
@@ -168,28 +168,36 @@ final class Repository
|
||||
return $this->one('SELECT id, email, password_hash FROM users WHERE id = ?', [$id]);
|
||||
}
|
||||
|
||||
public function recordLoginAttempt(string $address): bool
|
||||
public function changePassword(array $user, string $newHash): bool
|
||||
{
|
||||
return Database::write(function (PDO $pdo) use ($address): bool {
|
||||
$now = time();
|
||||
$query = $this->pdo->prepare('UPDATE users SET password_hash = ? WHERE id = ? AND password_hash = ?');
|
||||
$query->execute([$newHash, $user['id'], $user['password_hash']]);
|
||||
|
||||
return $query->rowCount() === 1;
|
||||
}
|
||||
|
||||
public function recordFailedLogin(string $address): int
|
||||
{
|
||||
$now = time();
|
||||
$current = $this->one('SELECT attempts, expires_at FROM login_attempts WHERE address = ?', [$address]);
|
||||
if ($current && $current['attempts'] >= 12 && $current['expires_at'] > $now) {
|
||||
return $current['expires_at'] - $now;
|
||||
}
|
||||
|
||||
return Database::write(function (PDO $pdo) use ($address, $now): int {
|
||||
$pdo->prepare('DELETE FROM login_attempts WHERE expires_at <= ?')->execute([$now]);
|
||||
$current = $this->one('SELECT attempts FROM login_attempts WHERE address = ?', [$address]);
|
||||
$current = $this->one('SELECT attempts, expires_at FROM login_attempts WHERE address = ?', [$address]);
|
||||
if ($current && $current['attempts'] >= 12) {
|
||||
return false;
|
||||
return $current['expires_at'] - $now;
|
||||
}
|
||||
$query = $pdo->prepare('INSERT INTO login_attempts (address, attempts, expires_at) VALUES (?, 1, ?)
|
||||
ON CONFLICT (address) DO UPDATE SET attempts = attempts + 1');
|
||||
$query->execute([$address, $now + 900]);
|
||||
|
||||
return true;
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
public function clearLoginAttempts(string $address): void
|
||||
{
|
||||
$this->pdo->prepare('DELETE FROM login_attempts WHERE address = ?')->execute([$address]);
|
||||
}
|
||||
|
||||
private function one(string $sql, array $args): ?array
|
||||
{
|
||||
$query = $this->pdo->prepare($sql);
|
||||
|
||||
Reference in New Issue
Block a user