File size: 1,831 Bytes
a4a352b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace app\common\validate;
use think\Validate;

class User extends Validate
{
    protected $rule =   [
        'user_name'  => 'require|min:6',
        'user_pwd'   => 'require',
    ];

    protected $message  =   [
        'user_name.require' => 'validate/require_name',
        'user_name.min'     => 'validate/require_name_min',
        'user_pwd.require'   => 'validate/require_pass',
    ];

    protected $scene = [
        'add'  =>  ['user_name','user_pwd'],
        'edit'  =>  ['user_name'],
    ];

    /**
     * 校验邮箱
     * @param $email
     */
    public static function validateEmail($email)
    {
        list(, $email_host) = explode('@', $email, 2);
        // 不在白名单内,报错
        $email_white_host_sets = self::formatEmailHostSets('white');
        if (!empty($email_white_host_sets) && !isset($email_white_host_sets[$email_host])) {
            return ['code' => 1001, 'msg' => lang('model/user/email_host_not_allowed')];
        }
        // 在黑名单内,报错
        $email_black_host_sets = self::formatEmailHostSets('black');
        if (isset($email_black_host_sets[$email_host])) {
            return ['code' => 1002, 'msg' => lang('model/user/email_host_not_allowed')];
        }
        return ['code' => 1, 'msg' => 'ok'];
    }

    private static function formatEmailHostSets($type) {
        $config_string = isset($GLOBALS['config']['user']['email_' . $type . '_hosts']) ? $GLOBALS['config']['user']['email_' . $type . '_hosts'] : '';
        $email_host_sets = [];
        foreach (explode(',', str_replace("\n", ',', $config_string)) as $host) {
            $host = trim($host);
            if (strlen($host) == 0) {
                continue;
            }
            $email_host_sets[$host] = true;
        }
        return $email_host_sets;
    }
}