bibis/validate.php

87 行
2.1 KiB
PHP

<?php
// Validation, and sanitize
function validate_register_id($s) {
$s = mbtrim($s);
$len = mb_strlen(mbtrim($s));
if ($len < 3) { return ['ID は必須、3文字以上。']; }
if ($len > 12) { return ['ID は 12文字以内。']; }
if (!preg_match('/^[a-zA-Z0-9]+$/', $s)) { return ['ID は半角英數のみ。']; }
return [];
}
function validate_register_username($s) {
$len = mb_strlen(mbtrim($s));
if ($len < 1) { return ['名前は必須。']; }
if ($len > 50) { return ['名前は 50文字以内。']; }
return [];
}
function sanitize_register_username($s) {
// Escape fake trip (◆embcMFBoog -> ◇embcMFBoog)
return str_replace('◆', '◇', sanitize_oneline($s));
}
function validate_register_password($s, $confirm) {
if (mb_strlen($s) <= 0) { return ['パスワードは必須。']; }
if ($s !== $confirm) { return ['パスワードが確認用と不一致。']; }
return [];
}
function validate_bio($s) {
if (mb_strlen($s) > 1000) { return ['自己紹介は 1000 文字以内。']; }
return [];
}
function sanitize_bio($s) {
return sanitize_multiline($s);
}
function validate_post_id($s) {
if (
((string)(int)$s !== $s || $s < 0)
&& !preg_match('/[0-9a-z]{32}/', $s)
) {
return ['投稿 ID が不正。'];
}
return [];
}
function validate_post_title($s) {
$len = mb_strlen(mbtrim($s));
if ($len > 50) { return ['件名は 50 文字以内。']; }
return [];
}
function sanitize_post_title($s) {
return sanitize_oneline($s);
}
function validate_post_body($s) {
$len = mb_strlen(mbtrim($s));
if ($len <= 0) { return ['本文は必須。']; }
if ($len > 5000) { return ['本文は 500 文字以内。']; }
return [];
}
function sanitize_post_body($s) {
return sanitize_multiline($s);
}
// Common
// See: https://www.php.net/manual/ja/regexp.reference.unicode.php
function sanitize_multiline($s) {
$s = preg_replace('/[^\p{L}\{M}\p{N}\p{P}\p{Sc}\p{S}\p{Z}\012\015\040-\176]/u', '', $s);
$s = mbtrim($s);
return $s;
}
function sanitize_oneline($s) {
// Newlines will be removed
$s = preg_replace('/[^\p{L}\{M}\p{N}\p{P}\p{Sc}\p{S}\p{Z}\040-\176]/u', '', $s);
$s = mbtrim($s);
return $s;
}