chengkun
2025-05-26 8f3df543230cd4403368b39b9bbe5726d11a0284
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
 
define('UC_USER_REG_ILLEGAL_USERNAME', -1);
define('UC_USER_REG_USERNAME_SAME', -2);
define('UC_USER_REG_ILLEGAL_EMAIL', -3);
define('UC_USER_REG_EMAIL_SAME', -4);
 
define('UC_USER_NOT_EXISTS', -1);
define('UC_USER_CHECK_ERROR', -2);
 
class usercontrol {
 
    var $base;
    var $db;
    var $user;
 
    function __construct(&$base) {
        $this->usercontrol($base);
    }
 
    function usercontrol(&$base) {
        $this->base = $base;
        $this->db = $base->db;
        $this->user = $base->load('user');
    }
 
    function register($username, $pwd, $email) {
        if (($status = $this->checkName($username)) < 0) {
            return $status;
        }
        if(!empty($email)){
            if (($status = $this->checkEmail($email)) < 0) {
                return $status;
            }
        }
        $uid = $this->user->add($username, $pwd, $email);
        /*注册接口不发送邮件
        $this->base->init_cache();
        if ($this->base->cache['rg_mailcheck']) {
            $mmail = $this->base->load('mail');
            $mmail->send($email, $this->base->cache['rg_mail_subject'], $this->base->cache['rg_mail_content']);
        }*/
        return $uid;
        //return $this->user->add($username, $pwd, $email);
    }
 
    function login($username, $password, $logintype) {
        $num = 1;
        switch ($logintype) {
            case 1:
                $user = $this->user->get_by_uid($username);break;
            case 2:
                list($user, $num) = $this->user->get_by_email($username);break;
            default:
                $user = $this->user->get_by_username($username);
        }
        $synlogin = '';
        if (empty($user)) {
            $status = -1;
        } elseif ($logintype == 2 && $num > 1) {
            $status = -3;
        } elseif ($password != $user['password'] && substr($password, 8, 16) != $user['password']) {
            $status = -2;
        } else {
            $status = 1;
            $this->base->appid && $synlogin = $this->synlogin($user['uid'], $user['username'], $password);
        }
        unset($user['password']);//返回除password外的所有用户信息,以便应用端注册
        $user['synlogin'] = $synlogin;
        $user['status'] = $status;
        return $user;
    }
 
    function synlogin($uid, $username, $password) {
 
        $synlogin = '';
        $mapp = $this->base->load('app');
        $list = $mapp->applist();
        foreach ($list as $appid => $app) {
            if ($appid != $this->base->appid) {
                $url = $mapp->urlformat($app['siteurl'], $app['interface'], $app['secretkey'], 'User', 'synlogin', array('user' => $this->base->strcode($uid . "\t" . $username . "\t" . $password . "\t" . $this->base->time, $app['secretkey'])));
                $synlogin .= "<script type=\"text/javascript\" src=\"$url\"></script>";
            }
        }
        return $synlogin;
    }
 
    function synlogout() {
        $synlogout = '';
        $mapp = $this->base->load('app');
        $list = $mapp->applist();
        foreach ($list as $appid => $app) {
            if ($appid != $this->base->appid) {
                $url = $mapp->urlformat($app['siteurl'], $app['interface'], $app['secretkey'], 'User', 'synlogout');
                $synlogout .= "<script type=\"text/javascript\" src=\"$url\"></script>";
            }
        }
        return $synlogout;
    }
 
    function get($username,$bytype) {
        switch ($bytype) {
            case 1:
                $user = $this->user->get_by_uid($username);break;
            case 2:
                list($user) = $this->user->get_by_email($username);break;
            default:
                $user = $this->user->get_by_username($username);
        }
        if ($user) {
            return array('uid' => $user['uid'], 'username' => $user['username'],'avatar' => $user['icon'], 'email' => $user['email']);
        }
        return array();
    }
 
    function edit($uid, $oldname, $pwd, $email) {
 
 
        if ($email && ($status = $this->checkEmail($email, $oldname)) < 0) {
            return $status;
        }
 
        if (($status = $this->user->edit($uid, $newname, $pwd, $email)) == 2) {
            $notify = $this->base->load('notify');
            $nid = $notify->add('altername', array('uid' => $uid, 'newname' => $newname), true);
            //$notify->send_by_id($nid);
        }
        return $status;
    }
 
    function delete($uids) {
        $this->user->delete($uids);
        $notify = $this->base->load('notify');
        $nid = $notify->add('deluser', array('uids' => $uids), true);
    }
 
    function checkName($username) {
        $S_key = array("\\",'&',' ',"'",'"','/','*',',','<','>',"\r","\t","\n",'#','%','?');
        if (str_replace($S_key, '', $username) != $username) {
            return UC_USER_REG_ILLEGAL_USERNAME;
        }
        if ($this->user->get_by_username($username)) {
            return UC_USER_REG_USERNAME_SAME;
        }
        return 1;
    }
 
    function checkEmail($email, $username = '') {
        if (empty($email) || !preg_match("/^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$/", $email)) {
            return UC_USER_REG_ILLEGAL_EMAIL;
        }
        if ($this->user->check_email($email, $username)) {
            return UC_USER_REG_EMAIL_SAME;
        }
        return 1;
    }
 
    function check($uid,$checkstr){
        $user = $this->user->get_by_uid($uid);
        if ($user) {
            $app = $this->base->load('app');
            $myapp = $app->applist($this->base->appid);
            if (md5($myapp['secretkey'] . $user['password']) != $checkstr) {
                $user['uid'] = UC_USER_CHECK_ERROR;
            }
        } else {
            $user['uid'] = UC_USER_NOT_EXISTS;
        }
        return $user;
    }
 
    function CheckMaxUid(){
        return $this->user->get_by_maxuid();
    }
}
?>