blessing-skin-server/app/Services/Validate.php

65 lines
1.9 KiB
PHP
Raw Normal View History

2016-07-21 22:01:57 +08:00
<?php
namespace App\Services;
2016-07-24 15:56:23 +08:00
use App\Exceptions\E;
2016-07-21 22:01:57 +08:00
class Validate
{
2016-08-06 19:38:37 +08:00
/**
* Check POST values in a simple way
*
* @param array $keys
* @return void
*/
2016-08-06 19:54:03 +08:00
public static function checkPost(Array $keys, $silent = false)
2016-08-06 19:38:37 +08:00
{
foreach ($keys as $key) {
2016-08-06 19:54:03 +08:00
if (!isset($_POST[$key])) {
if ($silent) return false;
throw new E('非法参数', 1);
2016-08-06 19:54:03 +08:00
}
2016-08-06 19:38:37 +08:00
}
2016-08-06 19:54:03 +08:00
return true;
2016-08-06 19:38:37 +08:00
}
public static function email($email)
2016-07-28 15:09:29 +08:00
{
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
2016-08-16 13:27:06 +08:00
public static function nickname($nickname)
{
return $nickname != Utils::convertString($nickname);
}
2016-08-06 19:38:37 +08:00
public static function playerName($player_name)
2016-07-28 15:09:29 +08:00
{
$regx = (\Option::get('allow_chinese_playername') == "1") ?
2016-07-21 22:01:57 +08:00
"/^([A-Za-z0-9\x{4e00}-\x{9fa5}_]+)$/u" : "/^([A-Za-z0-9_]+)$/";
return preg_match($regx, $player_name);
}
2016-08-06 19:38:37 +08:00
public static function textureName($texture_name)
2016-07-28 15:09:29 +08:00
{
2016-07-21 22:01:57 +08:00
if (strlen($texture_name) > 32 || strlen($texture_name) < 1) {
throw new E('无效的材质名称。材质名长度应该小于 32。', 2);
} else if (Utils::convertString($texture_name) != $texture_name) {
throw new E('无效的材质名称。材质名称中包含了奇怪的字符。', 2);
}
return true;
}
2016-08-06 19:54:03 +08:00
public static function password($password, $silent = false)
2016-07-28 15:09:29 +08:00
{
2016-08-06 19:38:37 +08:00
if (strlen($password) > 16 || strlen($password) < 8) {
2016-08-06 19:54:03 +08:00
if ($silent) return false;
2016-07-21 22:01:57 +08:00
throw new E('无效的密码。密码长度应该大于 8 并小于 16。', 2);
2016-08-06 19:38:37 +08:00
} else if (Utils::convertString($password) != $password) {
2016-08-06 19:54:03 +08:00
if ($silent) return false;
2016-07-21 22:01:57 +08:00
throw new E('无效的密码。密码中包含了奇怪的字符。', 2);
}
return true;
}
}