table_home_blog.php
11.3 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
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
<?php
/**
* [Discuz!] (C)2001-2099 Comsenz Inc.
* This is NOT a freeware, use is subject to license terms
*
* $Id: table_home_blog.php 32130 2012-11-14 09:20:40Z zhengqingpeng $
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
class table_home_blog extends discuz_table
{
public function __construct() {
$this->_table = 'home_blog';
$this->_pk = 'blogid';
parent::__construct();
}
public function fetch_by_id_idtype($id) {
if(!$id) {
return null;
}
return DB::fetch_first('SELECT * FROM %t WHERE %i', array($this->_table, DB::field('blogid', $id)));
}
public function update_dateline_by_id_idtype_uid($id, $idtype, $dateline, $uid) {
return DB::update($this->_table, array('dateline' => $dateline), DB::field($idtype, $id).' AND '.DB::field('uid', $uid));
}
public function range($start = 0, $limit = 0, $ordersc = 'DESC', $orderby = 'dateline', $friend = null, $status = null, $uid = null, $dateline = null) {
$wheresql = '1';
$wheresql .= $friend ? ' AND '.DB::field('friend', $friend) : '';
$wheresql .= $uid ? ' AND '.DB::field('uid', $uid) : '';
$wheresql .= $status ? ' AND '.DB::field('status', $status) : '';
$wheresql .= $dateline ? ' AND '.DB::field('dateline', $dateline, '>=') : '';
if(in_array($orderby, array('hot', 'dateline'))) {
$wheresql .= ' ORDER BY '.DB::order($orderby, $ordersc);
}
$wheresql .= ' '.DB::limit($start, $limit);
return DB::fetch_all('SELECT * FROM %t WHERE %i', array($this->_table, $wheresql), $this->_pk);
}
public function fetch_all($blogid, $orderby = '', $ordersc = '', $start = 0, $limit = 0) {
if(!$blogid) {
return null;
}
$wheresql = DB::field('blogid', $blogid);
if($orderby = DB::order($orderby, $ordersc)) {
$wheresql .= ' ORDER BY '.$orderby;
}
if($limit = DB::limit($start, $limit)) {
$wheresql .= ' '.$limit;
}
return DB::fetch_all('SELECT * FROM %t WHERE %i', array($this->_table, $wheresql), $this->_pk);
}
public function increase($blogid, $uid, $setarr) {
$sql = array();
$allowkey = array('hot', 'viewnum', 'replynum', 'favtimes', 'sharetimes');
foreach($setarr as $key => $value) {
if(($value = intval($value)) && in_array($key, $allowkey)) {
$sql[] = "`$key`=`$key`+'$value'";
}
}
$wheresql = DB::field('blogid', $blogid);
if($uid) {
$wheresql .= ' AND '.DB::field('uid', $uid);
}
if(!empty($sql)){
return DB::query('UPDATE %t SET %i WHERE %i', array($this->_table, implode(',', $sql), $wheresql));
}
}
public function update_click($blogid, $clickid, $incclick) {
$clickid = intval($clickid);
if($clickid < 1 || $clickid > 8) {
return null;
}
return DB::query('UPDATE %t SET click'.$clickid.' = click'.$clickid.'+\'%d\' WHERE blogid = %d', array($this->_table, $incclick, $blogid));
}
public function update_classid_by_classid($classid, $newclassid) {
return DB::query('UPDATE %t SET classid = %d WHERE classid = %d', array($this->_table, $newclassid, $classid));
}
public function fetch_blogid_by_subject($keyword, $limit) {
$field = "subject LIKE '%{text}%'";
if(preg_match("(AND|\+|&|\s)", $keyword) && !preg_match("(OR|\|)", $keyword)) {
$andor = ' AND ';
$keywordsrch = '1';
$keyword = preg_replace("/( AND |&| )/is", "+", $keyword);
} else {
$andor = ' OR ';
$keywordsrch = '0';
$keyword = preg_replace("/( OR |\|)/is", "+", $keyword);
}
$keyword = str_replace('*', '%', addcslashes(daddslashes($keyword), '%_'));
foreach(explode('+', $keyword) as $text) {
$text = trim($text);
if($text) {
$keywordsrch .= $andor;
$keywordsrch .= str_replace('{text}', $text, $field);
}
}
$wheresql = " ($keywordsrch)";
if($limit) {
$wheresql .= ' ORDER BY blogid DESC '.DB::limit(0, $limit);
}
return DB::fetch_all('SELECT * FROM %t WHERE %i', array($this->_table, $wheresql), $this->_pk);
}
public function fetch_blogid_by_uid($uid, $start = 0, $limit = 0) {
if(!$uid) {
return null;
}
return DB::fetch_all('SELECT blogid FROM %t WHERE uid IN (%n) %i', array($this->_table, $uid, DB::limit($start, $limit)), $this->_pk);
}
public function fetch_all_by_uid($uid, $orderby = 'dateline', $start = 0, $limit = 0) {
if(!$uid) {
return null;
}
if($orderby = DB::order($orderby, 'DESC')) {
$order = 'ORDER BY '.$orderby;
}
return DB::fetch_all('SELECT * FROM %t WHERE uid IN (%n) %i', array($this->_table, $uid, $order.' '.DB::limit($start, $limit)), $this->_pk);
}
public function fetch_all_by_hot($hot, $orderby = 'dateline', $start = 0, $limit = 0) {
if(!$uid) {
return null;
}
if($orderby = DB::order($orderby, 'DESC')) {
$order = 'ORDER BY '.$orderby;
}
return DB::fetch_all('SELECT * FROM %t WHERE hot>=%d %i', array($this->_table, $hot, $order.' '.DB::limit($start, $limit)), $this->_pk);
}
public function count_by_catid($catid) {
return DB::result_first('SELECT COUNT(*) FROM %t WHERE catid = %d', array($this->_table, $catid));
}
public function count_by_uid($uid) {
return DB::result_first('SELECT COUNT(*) FROM %t WHERE uid = %d', array($this->_table, $uid));
}
public function delete_by_catid($catid) {
if(!$catid) {
return null;
}
return DB::delete($this->_table, DB::field('catid', $catid));
}
public function delete_by_uid($uids) {
if(!$uids) {
return null;
}
return DB::delete($this->_table, DB::field('uid', $uids));
}
public function update_by_catid($catid, $data) {
if(empty($data) || !is_array($data) || !$catid) {
return null;
}
return DB::update($this->_table, $data, DB::field('catid', $catid));
}
public function count_uid_by_blogid($blogid) {
if(!is_array($blogid) || !$blogid) {
return null;
}
return DB::fetch_all('SELECT uid, COUNT(blogid) AS count FROM %t WHERE blogid IN (%n) GROUP BY uid', array($this->_table, $blogid));
}
public function count_all_by_search($blogid = null, $uids = null, $starttime = null, $endtime = null, $hot1 = null, $hot2 = null, $viewnum1 = null, $viewnum2 = null, $replynum1 = null, $replynum2 = null, $friend = null, $ip = null, $keywords = null, $lengthlimit = null, $classid = null, $catid = null, $subject = null, $countwithoutjoin = false, $status = null) {
return $this->fetch_all_by_search(3, $blogid, $uids, $starttime, $endtime, $hot1, $hot2, $viewnum1, $viewnum2, $replynum1, $replynum2, $friend, $ip, $keywords, $lengthlimit, null, null, 0, 0, $classid, $catid, $subject, null, $countwithoutjoin, $status);
}
public function fetch_all_by_search($fetchtype = 1, $blogid = null, $uids = null, $starttime = null, $endtime = null, $hot1 = null, $hot2 = null, $viewnum1 = null, $viewnum2 = null, $replynum1 = null, $replynum2 = null, $friend = null, $ip = null, $keywords = null, $lengthlimit = null, $orderby = null, $ordersc = null, $start = 0, $limit = 0, $classid = null, $catid = null, $subject = null, $findex = null, $countwithoutjoin = false, $status = null) {
$sql = '';
$sql .= $blogid ? ' AND b.'.DB::field('blogid', $blogid) : '';
$sql .= is_array($uids) && count($uids) > 0 ? ' AND b.'.DB::field('uid', $uids) : '';
$sql .= $starttime ? ' AND b.'.DB::field('dateline', $starttime, '>') : '';
$sql .= $endtime ? ' AND b.'.DB::field('dateline', $endtime, '<') : '';
$sql .= $hot1 ? ' AND b.'.DB::field('hot', $hot1, '>=') : '';
$sql .= $hot2 ? ' AND b.'.DB::field('hot', $hot2, '<=') : '';
$sql .= $viewnum1 ? ' AND b.'.DB::field('viewnum', $viewnum1, '>=') : '';
$sql .= $viewnum2 ? ' AND b.'.DB::field('viewnum', $viewnum1, '<=') : '';
$sql .= $replynum1 ? ' AND b.'.DB::field('replynum', $replynum1, '>=') : '';
$sql .= $replynum2 ? ' AND b.'.DB::field('replynum', $replynum2, '<=') : '';
$sql .= $classid ? ' AND b.'.DB::field('classid', $classid) : '';
$sql .= $friend ? ' AND b.'.DB::field('friend', $friend) : '';
$sql .= !is_null($status) ? ' AND b.'.DB::field('status', $status) : '';
$ip = str_replace('*', '', $ip);
if($fetchtype == 1) {
$sql .= $ip ? ' AND bf.'.DB::field('postip', "%$ip%", 'like') : '';
}
$orderby = $orderby ? $orderby : 'dateline';
$ordersc = $ordersc ? $ordersc : 'DESC';
if($fetchtype == 1 && $keywords != '' && !is_array($keywords)) {
$sqlkeywords = '';
$or = '';
$keywords = explode(',', str_replace(' ', '', $keywords));
for($i = 0; $i < count($keywords); $i++) {
$keywords[$i] = daddslashes($keywords[$i]);
if(preg_match("/\{(\d+)\}/", $keywords[$i])) {
$keywords[$i] = preg_replace("/\\\{(\d+)\\\}/", ".{0,\\1}", preg_quote($keywords[$i], '/'));
$sqlkeywords .= " $or b.subject REGEXP '".$keywords[$i]."' OR bf.message REGEXP '".$keywords[$i]."'";
} else {
$sqlkeywords .= " $or b.subject LIKE '%".$keywords[$i]."%' OR bf.message LIKE '%".$keywords[$i]."%'";
}
$or = 'OR';
}
if($sqlkeywords) {
$sql .= " AND ($sqlkeywords)";
}
}
$sql .= $subject ? ' AND b.'.DB::field('subject', "%$subject%", 'like') : '';
$sql .= $catid ? ' AND b.'.DB::field('catid', $catid) : '';
if($fetchtype == 1) {
$sql .= $lengthlimit ? ' AND LENGTH(bf.message) > '.intval($lengthlimit) : '';
}
if($fetchtype == 3) {
$selectfield = 'count(*)';
} elseif ($fetchtype == 2) {
$selectfield = 'b.blogid';
} else {
$selectfield = 'bf.*,b.*';
}
if($findex) {
$findex = 'USE INDEX(dateline)';
} else {
$findex = '';
}
if($fetchtype == 3) {
return DB::result_first("SELECT $selectfield FROM %t b ".(($countwithoutjoin === false) ? 'LEFT JOIN %t bf USING(blogid) ' : '').
"WHERE 1 %i", ($countwithoutjoin === false) ? array($this->_table, 'home_blogfield', $sql) : array($this->_table, $sql));
} else {
if($order = DB::order($orderby, $ordersc)) {
$order = 'ORDER BY b.'.$order;
} else {
$order = '';
}
return DB::fetch_all("SELECT $selectfield FROM %t b {$findex} LEFT JOIN %t bf USING(blogid) " .
"WHERE 1 %i", array($this->_table, 'home_blogfield', $sql.' '.$order.' '.DB::limit($start, $limit)));
}
}
public function fetch_all_by_block($blogids = null, $bannedids = null, $uids = null, $catid = null, $hours = null, $getpic = null, $getsummary = null, $picrequired = null, $orderby = 'dateline', $start = 0, $limit = 0) {
$wheres = array();
if($blogids) {
$wheres[] = 'b.'.DB::field('blogid', $blogids);
}
if($bannedids) {
$val = implode(',', DB::quote($bannedids));
$wheres[] = 'b.blogid NOT IN ('.$val.')';
}
if($uids) {
$wheres[] = 'b.'.DB::field('uid', $uids);
}
if($catid && !in_array('0', $catid)) {
$wheres[] = 'b.'.DB::field('catid', $catid);
}
if($hours) {
$timestamp = TIMESTAMP - 3600 * $hours;
$wheres[] = 'b.'.DB::field('dateline', $timestamp, '>=') ;
}
$tablesql = $fieldsql = '';
if($getpic || $getsummary || $picrequired) {
if($picrequired) {
$wheres[] = "bf.pic != ''";
}
$tablesql = ' LEFT JOIN '.DB::table('home_blogfield')." bf ON b.blogid = bf.blogid";
$fieldsql = ', bf.pic, b.picflag, bf.message';
}
$wheres[] = "b.friend = '0'";
$wheres[] = "b.status='0'";
$wheresql = $wheres ? implode(' AND ', $wheres) : '1';
return DB::fetch_all('SELECT b.* %i FROM %t b %i WHERE %i', array($fieldsql, $this->_table, $tablesql, $wheresql.' ORDER BY b.'.DB::order($orderby, 'DESC').' '.DB::limit($start, $limit)));
}
}
?>