chengkun
2025-05-22 1a8aea45ebb1582c9f65d9e8dcd520002f83ae12
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<?php
 
 
 
/**
 * phpyun后台公共代码: admin(pc后台)/wapadmin(手机站后台)/siteadmin(分站后台)
 */
class adminCommon extends common{
 
    // admin/siteadmin后台公共部分
    function admin(){
        
        $this -> get_admin_user_shell(); // 权限
                                          
        // 凡是有POST参数过来的都验证token信息
        if ($this -> config['sy_iscsrf'] != '2') {
            
            if (! $_SESSION['pytoken']) {
                
                $_SESSION['pytoken']  =  substr(md5(uniqid() . $_SESSION['auid'] . $_SESSION['ausername'] . $_SESSION['ashell']), 8, 12);
            }
            if ($_POST) {
                if ($_POST['pytoken'] != $_SESSION['pytoken']) {
                    
                    $this -> ACT_layer_msg('来源地址非法!', 8, $this->config['sy_weburl']);
                }
                unset($_POST['pytoken']);
            }
            $this -> yunset('pytoken', $_SESSION['pytoken']);
        }
    }
 
    // wap站后台公共部分
    function wapadmin(){
        
        $UA  =  strtoupper($_SERVER['HTTP_USER_AGENT']);
        
        if (strpos($UA, 'WINDOWS NT') !== false) {
            // header('location:'.Url('index',array('c'=>'wap')));
        }
        
        if ($_SESSION['wuid'] || $_GET['c']) {
            
            $shell  =  $this -> admin_get_user_shell($_SESSION['wuid'], $_SESSION['wshell']);
            
            if (! is_array($shell)) {
                unset($_SESSION['authcode']);
                unset($_SESSION['wuid']);
                unset($_SESSION['wusername']);
                unset($_SESSION['wshell']);
                unset($_SESSION['md']);
                unset($_SESSION['tooken']);
                unset($_SESSION['xsstooken']);
                header('location:' . $this->config['sy_weburl'] . '/wapadmin/');
                exit();
            }
        }
    }
 
    function get_admin_user_shell(){
 
        $shellMsg = '暂无操作权限';
 
        // cache要判断C ajax不需要判断
        if ($_SESSION['auid'] && $_SESSION['ashell']) {
            
            //验证登录信息
            $row  =  $this -> admin_get_user_shell($_SESSION['auid'], $_SESSION['ashell']);
            
            if (!$row) {
                
                $this -> adminlogout();
                
                $this -> ShellMsg('登录超时,请刷新后重新登录!');exit();
            }
            
            
 
            if ($_GET['m'] == '' || $_GET['m'] == 'index' || $_GET['m'] == 'admin_nav') {
                
                $_GET['m'] = 'admin_right';
            }
            if ($this->config['did'] && $_GET['a']) {
                
                $_GET['c'] = $_GET['a'];
            }
            $c   =  $_GET['c'];
            $m   =  $_GET['m'];
            if ($_GET['m'] != 'admin_right') {
            
                //优先验证4级权限池(各类子模块权限)
                
                $moduleNav = $this -> get_shell_module($row['m_id'],$m,$c);
            
                if($moduleNav['error'] == '2'){//子权限验证未通过
                    
                    $this -> ShellMsg($shellMsg);exit();
 
                }elseif($moduleNav['error'] == '1'){//子权限验证通过 直接放行 OR 继续父级权限的认证
                    
                    //return true;
                    
                }
                $url      =  'index.php?m=' . $m;
                
                
                $c_array  =  array(
                    'cache',
                    'markcom',
                    'markuser',
                    'advertise',
                    'zhaopinhui',
                    'admin_user',
                    'wx',
                    'admin_siteadmin'
                );
                
                $wx_c_arr = array(
                    'Getpubtool',
                    'wxPubTempList',
                    'wxPubTemp',
                    'wxPubTempSave',
                    'wxPubTempDel',
                    'twTask',
                    'delTwTask',
                    'getTW',
                    'taskFinish'
                );
                
                $navM    =  $this -> MODEL('navigation');
                
                if ($c && $c != 'savagroup' && in_array($m, $c_array)) {
                    
                    if ($m == 'admin_user' && $c == 'pass') {
                        
                        $c  =  'myuser';
                    }elseif ($m == 'wx' && in_array($c,$wx_c_arr)) {
                        
                        $c  =  'pubtool';
                    }
 
                    $url  .=  '&c=' . $c;
                    
                    $info  =  $navM -> getAdminNav(array('url'=>$url));
                    
                    if (empty($info)) {
                        
                        $url  =  'index.php?m=' . $m;
                    }
                }
 
                if ($m == 'admin_memberlog'){
                    $m      =   'admin_member';
                    $url    =   'index.php?m=' . $m;
                }
                
                $nav  =  $this -> get_shell($row['m_id'], $url);
                if (! $nav) {
                    
                    // $this->adminlogout();
                     $this -> ShellMsg($shellMsg);exit();
                }
                
                if (is_numeric($this->config['did'])) {
                    
                    if ($m == 'admin_user') {
                        
                        $where['url']   =  'index.php?m=admin_user&c=myuser';
                        $where['dids']  =  1;
                        
                    } else {
                        
                        $where['url']   =  $url;
                        $where['dids']  =  1;
                    }
                    $info  =  $navM -> getAdminNav($where);
                } else {
                    $info  =  $navM -> getAdminNav(array('url'=>$url));
                }
                if (! $info) {
                    $this -> ShellMsg($shellMsg);exit();
                }
            }
        } else {
            if ($_GET['m'] != '') {
                $this -> adminlogout();
                $this -> ShellMsg('登录超时,请刷新后重新登录!');exit();
            }
        }
    }
 
    // 用户退出
    function adminlogout(){
        unset($_SESSION['authcode']);
        unset($_SESSION['auid']);
        unset($_SESSION['ausername']);
        unset($_SESSION['ashell']);
        unset($_SESSION['md']);
        unset($_SESSION['tooken']);
        unset($_SESSION['xsstooken']);
       
    }
    
    function ShellMsg($msg = '您暂无操作权限!'){
        
        //三种提醒方式 JS/AJAX:自定义错误状态 表单POST:ACT_MSG GET:普通提示
        header('HTTP/1.1 777 Not Authorization'); 
 
        if($_POST){
            $this->ACT_layer_msg( $msg ,8);
        }else{
            echo $msg;
        }
        
        exit();
    }
    /**
     *@desc  后台用户登录
     */
    function admin_get_user_login($username, $password, $url = 'index.php'){
        
        global $config;
        
        $username  =  str_replace(' ', '', $username);
        
        $adminM    =  $this -> MODEL('admin');
        
        if ($config['sy_web_site'] == '1') {
            
            $where['username']        =  $username;
            $where['PHPYUNBTWSTART']  =  '';
            $where['did']             =  $config['did'];
            $where['isdid']           =  array('=',1,'OR');
            $where['PHPYUNBTWEND']    =  '';
            $where['status']          =  1;
            
            $user  =  $adminM -> getAdminUser($where, array());
        } else {
            
            $user  =  $adminM -> getAdminUser(array('username' => $username,'status' => 1), array());
        }
        //判断是否有登录时间控制
        if($user['control_login']){
            $timeArr = explode(" - ",$user['control_login']);
            $currtime = time();
            $stime = strtotime(date('Y-m-d ',time()).$timeArr[0]) ;
            $etime = strtotime(date('Y-m-d ',time()).$timeArr[1]);
            if($stime>$currtime  || $etime<$currtime){
                $this -> ACT_layer_msg('不在规定登录时间内', 8, $url);
            }
        }
        $verify  =  $adminM -> verifyPass($password, $user['password']);
        
        $logM      =  $this -> MODEL('log');
 
        if ($verify) {
            
            $_SESSION['auid']       =  $user['uid'];
            $_SESSION['ausername']  =  $user['username'];
            $_SESSION['xsstooken']  =  sha1($config['sy_safekey']);
            $_SESSION['ashell']     =  md5($user['username'] . $user['password'] . $this -> md);
            
            
            
            $adminLog  =  $logM -> getAdminLog(array('uid' => $_SESSION['auid'],'content' => '登录成功','orderby' => 'ctime'));
            
            $time      =  time();
            
            if ($adminLog) {
                
                $this -> cookie -> setCookie('lasttime', $adminLog['ctime'], $time + 80000);
            } else {
                
                $this -> cookie -> setCookie('lasttime', $time, $time + 80000);
            }
            
            $this -> cookie -> setCookie('ashell', md5($user['username'] . $user['password'] . $this -> md), $time + 80000);
            
            $adminM -> upAdminUser(array('lasttime' => $time), array('uid' => $user['uid']));
           
            $logM -> addAdminLog('登录成功', '', '', '');
            
            $this -> ACT_layer_msg('登录成功!', 9, $url);
        } else {
 
            $logM -> addAdminLog("管理员:".$username.' 登录失败,登录密码:'.$password, '', '', '',array('auid'=>$user['uid'],'ausername'=>$username));
            
            $this -> ACT_layer_msg('用户名或密码不正确!', 8, $url);
        }
    }
 
    /**
     * 手机WAP后台用户登录
     */
    function wapadmin_get_user_login($username, $password, $url = 'index.php'){
        global $config;
        $username  =  str_replace(' ', '', $username);
        
        $adminM    =  $this -> MODEL('admin');
        
        if ($config['sy_web_site'] == '1') {
            
            $where['username']        =  $username;
            $where['PHPYUNBTWSTART']  =  '';
            $where['did']             =  $config['did'];
            $where['isdid']           =  array('=',1,'OR');
            $where['PHPYUNBTWEND']    =  '';
            $where['status']          =  1;
            
            $user  =  $adminM -> getAdminUser($where);
        } else {
            
            $user  =  $adminM -> getAdminUser(array('username' => $username,'status' => 1));
        }
        
        $verify  =  $adminM -> verifyPass($password, $user['password']);
        
        if ($verify) {
            
            $_SESSION['wuid']       =  $user['uid'];
            $_SESSION['wusername']  =  $user['username'];
            $_SESSION['xsstooken']  =  sha1($config['sy_safekey']);
            $_SESSION['wshell']     =  md5($user['username'] . $user['password'] . $this -> md);
            
            $time  =  time();
            
            $this -> cookie -> setCookie('wshell', md5($user['username'] . $user['password'] . $this -> md), $time + 80000);
            
            $adminM -> upAdminUser(array('lasttime' => $time), array('uid' => $user['uid']));
            
            $this -> MODEL('log') -> addAdminLog('登录成功');
            
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 管理员用户登录权限验证
     */
    function admin_get_user_shell($uid, $shell){
        
        global $config;
        
        if (! preg_match("/^\d*$/", $uid)) {
            return false;
        }
        
        $adminM    =  $this -> MODEL('admin');
        
        if ($config['sy_web_site'] == '1') {
            
            $where['uid']             =  $uid;
            $where['PHPYUNBTWSTART']  =  '';
            $where['did']             =  $config['did'];
            $where['isdid']           =  array('=',1,'OR');
            $where['PHPYUNBTWEND']    =  '';
            
            $user  =  $adminM -> getAdminUser($where);
        } else {
            
            $user  =  $adminM -> getAdminUser(array('uid' => $uid));
        }
        
        $ps = !empty($user) ? $shell == md5($user['username'] . $user['password'] . $this->md) : FALSE;
        
        return $ps ? $user : NULL;
    }
 
    /**
     * 检查访问地址
     */
    function check_token(){
        if ($this->config['sy_iscsrf'] != '2') {
            
            if ($_SESSION['pytoken'] != $_GET['pytoken'] || ! $_SESSION['pytoken']) {
                
                unset($_SESSION['pytoken']);
                
                $this -> ACT_layer_msg('来源地址非法!', 8, 'index.php');
                
                exit();
            }
        }
    }
 
    /**
     * 后台顶部导航条默认页面
     * @param $id     
     * @param $power
     */
    function GET_web_default($id, $power){
        
        if ($this->config['sy_web_site'] == '1' && $this->config['did']) {
            
            $where['dids'] = 1;
        }
        
        $navM  =  $this -> MODEL('navigation');
        
        $web   =  $navM -> getAdminNavList(array('keyid'=>array('in',pylode(',', $id)),'orderby'=>'sort,asc'));
        
        if (is_array($web['list'])) {
            
            foreach ($web['list'] as $v) {
                
                if (@in_array($v['id'], $power)) {
                    
                    $arr[]           =  $v['id'];
                    
                    $arr2[$v['id']]  =  $v['keyid'];
                }
            }
            
            $where['keyid']    =  array('in',pylode(',', $arr));
            $where['orderby']  =  'sort,asc';
            
            $webaa  =  $navM -> getAdminNavList($where);
            
            if (is_array($webaa)) {
                
                foreach ($webaa['list'] as $va) {
                     
                    if (@in_array($va['id'], $power)) {
                        
                        $value[$arr2[$va['keyid']]]  =  $va['url'];
                    }
                }
            }
        }
        
        return $value;
    }
    /**
     * 管理员组操作模块权限认证
     * @param string $mid  管理员类型id
     * @param string $web  要检测的链接url
     * @param string $type
     * @return boolean
     */
    function get_shell_module($mid, $m,$c){
        
        
        
        $navM    =  $this -> MODEL('navigation');
        //首先检测当前链接是否在操作模块权限池中
        $nav     =  $navM -> getAdminNav(array('url'=>'index.php?m='.$m.'&c='.$c,'level'=>4));
            
        //不在权限池就无需验证
        if(!empty($nav)){
            $adminM  =  $this->MODEL('admin');
            $row     =  $adminM -> getPower(array('id'=>$mid));
            //判断当前管理员类型有权限、且该类型下有用户
            if ($row['power'] && $row['username']){
                
                
        
                if(in_array($nav['id'], $row['power'])){
 
                    $return['error'] = 1;
                    $return['power'] = $row;
                }else{    
                    $return['error'] = 2;
                }
            } else {
                $return['error'] = 2;
            }
        }else{
 
            $return['error'] = 3;
            
        }
    
        
        
        
        return $return;
    }
    /**
     * 管理员组导航菜单权限检测
     * @param string $mid  管理员类型id
     * @param string $web  要检测的链接url
     * @param string $type
     * @return boolean
     */
    function get_shell($mid, $web, $type = ''){
        // $web=str_replace('&','&amp;',$web);
        
        $adminM  =  $this->MODEL('admin');
        
        $row     =  $adminM -> getPower(array('id'=>$mid),1);
        
        //判断当前管理员类型有权限、且该类型下有用户
        if ($row['power'] && $row['group_name']){
            
            $navM    =  $this -> MODEL('navigation');
            
            $nav     =  $navM -> getAdminNav(array('url'=>$web));
           
            $return  =  @in_array($nav['id'], $row['power']) ? true : false;
        } else {
            $return  =  false;
        }
        return $return;
    }
    /**
     * 获取页面名称
     * @param string $id
     * @return string
     */
    function GET_web_check($id){
        
        $navM  =  $this -> MODEL('navigation');
        
        $nav   =  $navM -> getAdminNav(array('id'=>$id));
        
        if (is_array($nav)) {
            
            $value  =  $this -> GET_web_check($nav['keyid']);
            
            $value  .=  $nav['name'] . ' - ';
        }
        return $value;
    }
    /**
     * 获取微信/公众号/小程序绑定状态
     * @param string $id
     * @return string
     */
    function wxBindState($wxBind){
        
        if(!empty($wxBind['wxid'])){
            if(!empty($wxBind['unionid'])){
                $wxGzhmsg = '公众号已绑定,微信开放平台已绑定';
            }else{
                $wxGzhmsg = '公众号已绑定';
            }
        }else{
            $wxGzhmsg= '公众号未绑定';
        }
        $wxBindmsg = $wxGzhmsg;
        if(!empty($this->config['sy_xcxappid'])){
            if(!empty($wxBind['wxopenid'])){
                if($this->config['mini_wxopen']==1){
                    $wxXcxmsg = '微信小程序已绑定,微信开放平台已绑定';
                }else{
                    $wxXcxmsg = '微信小程序已绑定';
                }
            }else{
                $wxXcxmsg= '微信小程序未绑定';
            }
            $wxBindmsg .=  '</br>'.$wxXcxmsg;
        }
        if($this->config['sy_app_open'] ==1){
            if(!empty($wxBind['app_wxid'])){
                $wxAppmsg = 'APP微信已绑定';
            }else{
                $wxAppmsg = 'APP微信未绑定';
            }
            $wxBindmsg .=  '</br>'.$wxAppmsg;
        }
        return $wxBindmsg;
    }
}
?>