table_common_session.php
4.82 KB
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
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: table_common_session.php 28051 2012-02-21 10:36:56Z zhangguosheng $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class table_common_session extends discuz_table
{
public function __construct() {
$this->_table = 'common_session';
$this->_pk = 'sid';
parent::__construct();
}
public function fetch($sid, $ip = false, $uid = false) {
if(empty($sid)) {
return array();
}
$this->checkpk();
$session = parent::fetch($sid);
if($session && $ip !== false && $ip != "{$session['ip1']}.{$session['ip2']}.{$session['ip3']}.{$session['ip4']}") {
$session = array();
}
if($session && $uid !== false && $uid != $session['uid']) {
$session = array();
}
return $session;
}
public function fetch_member($ismember = 0, $invisible = 0, $start = 0, $limit = 0) {
$sql = array();
if($ismember === 1) {
$sql[] = 'uid > 0';
} elseif($ismember === 2) {
$sql[] = 'uid = 0';
}
if($invisible === 1) {
$sql[] = 'invisible = 1';
} elseif($invisible === 2) {
$sql[] = 'invisible = 0';
}
$wheresql = !empty($sql) && is_array($sql) ? ' WHERE '.implode(' AND ', $sql) : '';
$sql = 'SELECT * FROM %t '.$wheresql.' ORDER BY lastactivity DESC'.DB::limit($start, $limit);
return DB::fetch_all($sql, array($this->_table), $this->_pk);
}
public function count_invisible($type = 1) {
return DB::result_first('SELECT COUNT(*) FROM %t WHERE invisible=%d', array($this->_table, $type));
}
public function count($type = 0) {
$condition = $type == 1 ? ' WHERE uid>0 ' : ($type == 2 ? ' WHERE uid=0 ' : '');
return DB::result_first("SELECT count(*) FROM ".DB::table($this->_table).$condition);
}
public function delete_by_session($session, $onlinehold, $guestspan) {
if(!empty($session) && is_array($session)) {
$onlinehold = time() - $onlinehold;
$guestspan = time() - $guestspan;
$session = daddslashes($session);
$condition = " sid='{$session[sid]}' ";
$condition .= " OR lastactivity<$onlinehold ";
$condition .= " OR (uid='0' AND ip1='{$session['ip1']}' AND ip2='{$session['ip2']}' AND ip3='{$session['ip3']}' AND ip4='{$session['ip4']}' AND lastactivity>$guestspan) ";
$condition .= $session['uid'] ? " OR (uid='{$session['uid']}') " : '';
DB::delete('common_session', $condition);
}
}
public function fetch_by_uid($uid) {
return !empty($uid) ? DB::fetch_first('SELECT * FROM %t WHERE uid=%d', array($this->_table, $uid)) : false;
}
public function fetch_all_by_uid($uids, $start = 0, $limit = 0) {
$data = array();
if(!empty($uids)) {
$data = DB::fetch_all('SELECT * FROM %t WHERE '.DB::field('uid', $uids).DB::limit($start, $limit), array($this->_table), null, 'uid');
}
return $data;
}
public function update_by_ipban($ip1, $ip2, $ip3, $ip4) {
$ip1 = intval($ip1);
$ip2 = intval($ip2);
$ip3 = intval($ip3);
$ip4 = intval($ip4);
return DB::query('UPDATE '.DB::table('common_session')." SET groupid='6' WHERE ('$ip1'='-1' OR ip1='$ip1') AND ('$ip2'='-1' OR ip2='$ip2') AND ('$ip3'='-1' OR ip3='$ip3') AND ('$ip4'='-1' OR ip4='$ip4')");
}
public function update_max_rows($max_rows) {
return DB::query('ALTER TABLE '.DB::table('common_session').' MAX_ROWS='.dintval($max_rows));
}
public function clear() {
return DB::query('DELETE FROM '.DB::table('common_session'));
}
public function count_by_fid($fid) {
return ($fid = dintval($fid)) ? DB::result_first('SELECT COUNT(*) FROM '.DB::table('common_session')." WHERE uid>'0' AND fid='$fid' AND invisible='0'") : 0;
}
public function fetch_all_by_fid($fid, $limit = 12) {
return ($fid = dintval($fid)) ? DB::fetch_all('SELECT uid, groupid, username, invisible, lastactivity FROM '.DB::table('common_session')." WHERE uid>'0' AND fid='$fid' AND invisible='0' ORDER BY lastactivity DESC".DB::limit($limit)) : array();
}
public function update_by_uid($uid, $data){
if(($uid = dintval($uid)) && !empty($data) && is_array($data)) {
return DB::update($this->_table, $data, DB::field('uid', $uid));
}
return 0;
}
public function count_by_ip($ip) {
$count = 0;
if(!empty($ip) && ($ip = explode('.', $ip)) && count($ip) > 2 ) {
$count = DB::result_first('SELECT COUNT(*) FROM '.DB::table('common_session')." WHERE ip1='$ip[0]' AND ip2='$ip[1]' AND ip3='$ip[2]'");
}
return $count;
}
public function fetch_all_by_ip($ip, $start = 0, $limit = 0) {
$data = array();
if(!empty($ip) && ($ip = explode('.', $ip)) && count($ip) > 2 ) {
$data = DB::fetch_all('SELECT * FROM %t WHERE ip1=%d AND ip2=%d AND ip3=%d ORDER BY lastactivity DESC'.DB::limit($start, $limit), array($this->_table, $ip[0], $ip[1], $ip[2]), null);
}
return $data;
}
}
?>