PageRenderTime 205ms CodeModel.GetById 111ms app.highlight 64ms RepoModel.GetById 16ms app.codeStats 1ms

/source/function/function_forum.php

https://github.com/kuaileshike/upload
PHP | 985 lines | 897 code | 82 blank | 6 comment | 268 complexity | b5f5fd5927e49d4afbf104386e11e3a6 MD5 | raw file
  1<?php
  2
  3/**
  4 *      [Discuz!] (C)2001-2099 Comsenz Inc.
  5 *      This is NOT a freeware, use is subject to license terms
  6 *
  7 *      $Id: function_forum.php 30837 2012-06-25 08:24:29Z zhangguosheng $
  8 */
  9
 10if(!defined('IN_DISCUZ')) {
 11	exit('Access Denied');
 12}
 13
 14function discuz_uc_avatar($uid, $size = '', $returnsrc = FALSE) {
 15	global $_G;
 16	return avatar($uid, $size, $returnsrc, FALSE, $_G['setting']['avatarmethod'], $_G['setting']['ucenterurl']);
 17}
 18
 19function dunlink($attach) {
 20	global $_G;
 21	$filename = $attach['attachment'];
 22	$havethumb = $attach['thumb'];
 23	$remote = $attach['remote'];
 24	if($remote) {
 25		ftpcmd('delete', $_G['setting']['ftp']['attachdir'].'/forum/'.$filename);
 26		$havethumb && ftpcmd('delete', $_G['setting']['ftp']['attachdir'].'/forum/'.getimgthumbname($filename));
 27	} else {
 28		@unlink($_G['setting']['attachdir'].'/forum/'.$filename);
 29		$havethumb && @unlink($_G['setting']['attachdir'].'/forum/'.getimgthumbname($filename));
 30	}
 31	if($attach['aid']) {
 32		@unlink($_G['setting']['attachdir'].'image/'.$attach['aid'].'_100_100.jpg');
 33	}
 34}
 35
 36function formulaperm($formula) {
 37	global $_G;
 38	if($_G['forum']['ismoderator']) {
 39		return TRUE;
 40	}
 41
 42	$formula = dunserialize($formula);
 43	$medalperm = $formula['medal'];
 44	$permusers = $formula['users'];
 45	$permmessage = $formula['message'];
 46	if($_G['setting']['medalstatus'] && $medalperm) {
 47		$exists = 1;
 48		$_G['forum_formulamessage'] = '';
 49		$medalpermc = $medalperm;
 50		if($_G['uid']) {
 51			$memberfieldforum = C::t('common_member_field_forum')->fetch($_G['uid']);
 52			$medals = explode("\t", $memberfieldforum['medals']);
 53			unset($memberfieldforum);
 54			foreach($medalperm as $k => $medal) {
 55				foreach($medals as $r) {
 56					list($medalid) = explode("|", $r);
 57					if($medalid == $medal) {
 58						$exists = 0;
 59						unset($medalpermc[$k]);
 60					}
 61				}
 62			}
 63		} else {
 64			$exists = 0;
 65		}
 66		if($medalpermc) {
 67			loadcache('medals');
 68			foreach($medalpermc as $medal) {
 69				if($_G['cache']['medals'][$medal]) {
 70					$_G['forum_formulamessage'] .= '<img src="'.STATICURL.'image/common/'.$_G['cache']['medals'][$medal]['image'].'" style="vertical-align:middle;" />&nbsp;'.$_G['cache']['medals'][$medal]['name'].'&nbsp; ';
 71				}
 72			}
 73			showmessage('forum_permforum_nomedal', NULL, array('forum_permforum_nomedal' => $_G['forum_formulamessage']), array('login' => 1));
 74		}
 75	}
 76	$formulatext = $formula[0];
 77	$formula = $formula[1];
 78	if($_G['adminid'] == 1 || $_G['forum']['ismoderator'] || in_array($_G['groupid'], explode("\t", $_G['forum']['spviewperm']))) {
 79		return FALSE;
 80	}
 81	if($permusers) {
 82		$permusers = str_replace(array("\r\n", "\r"), array("\n", "\n"), $permusers);
 83		$permusers = explode("\n", trim($permusers));
 84		if(!in_array($_G['member']['username'], $permusers)) {
 85			showmessage('forum_permforum_disallow', NULL, array(), array('login' => 1));
 86		}
 87	}
 88	if(!$formula) {
 89		return FALSE;
 90	}
 91	if(strexists($formula, '$memberformula[')) {
 92		preg_match_all("/\\\$memberformula\['(\w+?)'\]/", $formula, $a);
 93		$profilefields = array();
 94		foreach($a[1] as $field) {
 95			switch($field) {
 96				case 'regdate':
 97					$formula = preg_replace("/\{(\d{4})\-(\d{1,2})\-(\d{1,2})\}/e", "'\'\\1-'.sprintf('%02d', '\\2').'-'.sprintf('%02d', '\\3').'\''", $formula);
 98				case 'regday':
 99					break;
100				case 'regip':
101				case 'lastip':
102					$formula = preg_replace("/\{([\d\.]+?)\}/", "'\\1'", $formula);
103					$formula = preg_replace('/(\$memberformula\[\'(regip|lastip)\'\])\s*=+\s*\'([\d\.]+?)\'/', "strpos(\\1, '\\3')===0", $formula);
104				case 'buyercredit':
105				case 'sellercredit':
106					space_merge($_G['member'], 'status');break;
107				case substr($field, 0, 5) == 'field':
108					space_merge($_G['member'], 'profile');
109					$profilefields[] = $field;break;
110			}
111		}
112		$memberformula = array();
113		if($_G['uid']) {
114			$memberformula = $_G['member'];
115			if(in_array('regday', $a[1])) {
116				$memberformula['regday'] = intval((TIMESTAMP - $memberformula['regdate']) / 86400);
117			}
118			if(in_array('regdate', $a[1])) {
119				$memberformula['regdate'] = date('Y-m-d', $memberformula['regdate']);
120			}
121			$memberformula['lastip'] = $memberformula['lastip'] ? $memberformula['lastip'] : $_G['clientip'];
122		} else {
123			if(isset($memberformula['regip'])) {
124				$memberformula['regip'] = $_G['clientip'];
125			}
126			if(isset($memberformula['lastip'])) {
127				$memberformula['lastip'] = $_G['clientip'];
128			}
129		}
130	}
131	@eval("\$formulaperm = ($formula) ? TRUE : FALSE;");
132	if(!$formulaperm) {
133		if(!$permmessage) {
134			$language = lang('forum/misc');
135			$search = array('regdate', 'regday', 'regip', 'lastip', 'buyercredit', 'sellercredit', 'digestposts', 'posts', 'threads', 'oltime');
136			$replace = array($language['formulaperm_regdate'], $language['formulaperm_regday'], $language['formulaperm_regip'], $language['formulaperm_lastip'], $language['formulaperm_buyercredit'], $language['formulaperm_sellercredit'], $language['formulaperm_digestposts'], $language['formulaperm_posts'], $language['formulaperm_threads'], $language['formulaperm_oltime']);
137			for($i = 1; $i <= 8; $i++) {
138				$search[] = 'extcredits'.$i;
139				$replace[] = $_G['setting']['extcredits'][$i]['title'] ? $_G['setting']['extcredits'][$i]['title'] : $language['formulaperm_extcredits'].$i;
140			}
141			if($profilefields) {
142				loadcache(array('fields_required', 'fields_optional'));
143				foreach($profilefields as $profilefield) {
144					$search[] = $profilefield;
145					$replace[] = !empty($_G['cache']['fields_optional']['field_'.$profilefield]) ? $_G['cache']['fields_optional']['field_'.$profilefield]['title'] : $_G['cache']['fields_required']['field_'.$profilefield]['title'];
146				}
147			}
148			$i = 0;$_G['forum_usermsg'] = '';
149			foreach($search as $s) {
150				if(in_array($s, array('digestposts', 'posts', 'threads', 'oltime', 'extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8'))) {
151					$_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />&nbsp;&nbsp;&nbsp;'.$replace[$i].': '.(@eval('return intval(getuserprofile(\''.$s.'\'));')) : '';
152				} elseif(in_array($s, array('regdate', 'regip', 'regday'))) {
153					$_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />&nbsp;&nbsp;&nbsp;'.$replace[$i].': '.(@eval('return $memberformula[\''.$s.'\'];')) : '';
154				}
155				$i++;
156			}
157			$search = array_merge($search, array('and', 'or', '>=', '<=', '=='));
158			$replace = array_merge($replace, array('&nbsp;&nbsp;<b>'.$language['formulaperm_and'].'</b>&nbsp;&nbsp;', '&nbsp;&nbsp;<b>'.$language['formulaperm_or'].'</b>&nbsp;&nbsp;', '&ge;', '&le;', '='));
159			$_G['forum_formulamessage'] = str_replace($search, $replace, $formulatext);
160		} else {
161			$_G['forum_formulamessage'] = $permmessage;
162		}
163
164		if(!$permmessage) {
165			showmessage('forum_permforum_nopermission', NULL, array('formulamessage' => $_G['forum_formulamessage'], 'usermsg' => $_G['forum_usermsg']), array('login' => 1));
166		} else {
167			showmessage('forum_permforum_nopermission_custommsg', NULL, array('formulamessage' => $_G['forum_formulamessage']), array('login' => 1));
168		}
169	}
170	return TRUE;
171}
172
173function medalformulaperm($formula, $type) {
174	global $_G;
175
176	$formula = dunserialize($formula);
177	$permmessage = $formula['message'];
178	$formula = $formula['medal'];
179	if(!empty($formula['usergroupallow']) && is_array($formula['usergroups']) && !in_array($_G['groupid'], $formula['usergroups'])) {
180		loadcache('usergroups');
181		$message = array();
182		foreach($formula['usergroups'] as $groupid) {
183			$message[] = $_G['cache']['usergroups'][$groupid]['grouptitle'].' ';
184		}
185		$_G['forum_formulamessage'] = implode(', ', $message);
186		$_G['forum_usermsg'] = $_G['cache']['usergroups'][$_G['groupid']]['grouptitle'];
187		return FALSE;
188	}
189	$formulatext = $formula[0];
190	$formula = $formula[1];
191	if(!$formula) {
192		return FALSE;
193	}
194	if(strexists($formula, '$memberformula[')) {
195		preg_match_all("/\\\$memberformula\['(\w+?)'\]/", $formula, $a);
196		$profilefields = array();
197		foreach($a[1] as $field) {
198			switch($field) {
199				case 'regdate':
200					$formula = preg_replace("/\{(\d{4})\-(\d{1,2})\-(\d{1,2})\}/e", "'\'\\1-'.sprintf('%02d', '\\2').'-'.sprintf('%02d', '\\3').'\''", $formula);
201				case 'regday':
202					break;
203				case 'regip':
204				case 'lastip':
205					$formula = preg_replace("/\{([\d\.]+?)\}/", "'\\1'", $formula);
206					$formula = preg_replace('/(\$memberformula\[\'(regip|lastip)\'\])\s*=+\s*\'([\d\.]+?)\'/', "strpos(\\1, '\\3')===0", $formula);
207				case 'buyercredit':
208				case 'sellercredit':
209					space_merge($_G['member'], 'status');break;
210				case substr($field, 0, 5) == 'field':
211					space_merge($_G['member'], 'profile');
212					$profilefields[] = $field;break;
213			}
214		}
215		$memberformula = array();
216		if($_G['uid']) {
217			$memberformula = $_G['member'];
218			if(in_array('regday', $a[1])) {
219				$memberformula['regday'] = intval((TIMESTAMP - $memberformula['regdate']) / 86400);
220			}
221			if(in_array('regdate', $a[1])) {
222				$memberformula['regdate'] = date('Y-m-d', $memberformula['regdate']);
223			}
224			$memberformula['lastip'] = $memberformula['lastip'] ? $memberformula['lastip'] : $_G['clientip'];
225		} else {
226			if(isset($memberformula['regip'])) {
227				$memberformula['regip'] = $_G['clientip'];
228			}
229			if(isset($memberformula['lastip'])) {
230				$memberformula['lastip'] = $_G['clientip'];
231			}
232		}
233	}
234	@eval("\$formulaperm = ($formula) ? TRUE : FALSE;");
235	if(!$formulaperm || $type == 2) {
236		if(!$permmessage) {
237			$language = lang('forum/misc');
238			$search = array('regdate', 'regday', 'regip', 'lastip', 'buyercredit', 'sellercredit', 'digestposts', 'posts', 'threads', 'oltime');
239			$replace = array($language['formulaperm_regdate'], $language['formulaperm_regday'], $language['formulaperm_regip'], $language['formulaperm_lastip'], $language['formulaperm_buyercredit'], $language['formulaperm_sellercredit'], $language['formulaperm_digestposts'], $language['formulaperm_posts'], $language['formulaperm_threads'], $language['formulaperm_oltime']);
240			for($i = 1; $i <= 8; $i++) {
241				$search[] = 'extcredits'.$i;
242				$replace[] = $_G['setting']['extcredits'][$i]['title'] ? $_G['setting']['extcredits'][$i]['title'] : $language['formulaperm_extcredits'].$i;
243			}
244			if($profilefields) {
245				loadcache(array('fields_required', 'fields_optional'));
246				foreach($profilefields as $profilefield) {
247					$search[] = $profilefield;
248					$replace[] = !empty($_G['cache']['fields_optional']['field_'.$profilefield]) ? $_G['cache']['fields_optional']['field_'.$profilefield]['title'] : $_G['cache']['fields_required']['field_'.$profilefield]['title'];
249				}
250			}
251			$i = 0;$_G['forum_usermsg'] = '';
252			foreach($search as $s) {
253				if(in_array($s, array('digestposts', 'posts', 'threads', 'oltime', 'extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8'))) {
254					$_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />&nbsp;&nbsp;&nbsp;'.$replace[$i].': '.(@eval('return intval(getuserprofile(\''.$s.'\'));')) : '';
255				} elseif(in_array($s, array('regdate', 'regip'))) {
256					$_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />&nbsp;&nbsp;&nbsp;'.$replace[$i].': '.(@eval('return $memberformula[\''.$s.'\'];')) : '';
257				}
258				$i++;
259			}
260			$search = array_merge($search, array('and', 'or', '>=', '<=', '=='));
261			$replace = array_merge($replace, array('&nbsp;&nbsp;<b>'.$language['formulaperm_and'].'</b>&nbsp;&nbsp;', '&nbsp;&nbsp;<b>'.$language['formulaperm_or'].'</b>&nbsp;&nbsp;', '&ge;', '&le;', '='));
262			$_G['forum_formulamessage'] = str_replace($search, $replace, $formulatext);
263		} else {
264			$_G['forum_formulamessage'] = $permmessage;
265		}
266
267		return $_G['forum_formulamessage'];
268	} elseif($formulaperm && $type == 1) {
269		return FALSE;
270	}
271	return TRUE;
272}
273
274function groupexpiry($terms) {
275	$terms = is_array($terms) ? $terms : dunserialize($terms);
276	$groupexpiry = isset($terms['main']['time']) ? intval($terms['main']['time']) : 0;
277	if(is_array($terms['ext'])) {
278		foreach($terms['ext'] as $expiry) {
279			if((!$groupexpiry && $expiry) || $expiry < $groupexpiry) {
280				$groupexpiry = $expiry;
281			}
282		}
283	}
284	return $groupexpiry;
285}
286
287
288function typeselect($curtypeid = 0) {
289	global $_G;
290	if($threadtypes = $_G['forum']['threadtypes']) {
291		$html = '<select name="typeid" id="typeid"><option value="0">&nbsp;</option>';
292		foreach($threadtypes['types'] as $typeid => $name) {
293			$html .= '<option value="'.$typeid.'" '.($curtypeid == $typeid ? 'selected' : '').'>'.strip_tags($name).'</option>';
294		}
295		$html .= '</select>';
296		return $html;
297	} else {
298		return '';
299	}
300}
301
302function updatemodworks($modaction, $posts = 1) {
303	global $_G;
304	$today = dgmdate(TIMESTAMP, 'Y-m-d');
305	if($_G['setting']['modworkstatus'] && $modaction && $posts) {
306		$affect_rows = C::t('forum_modwork')->increase_count_posts_by_uid_modaction_dateline(1, $posts, $_G['uid'], $modaction, $today);
307		if(!$affect_rows) {
308			C::t('forum_modwork')->insert(array(
309				'uid' => $_G['uid'],
310				'modaction' => $modaction,
311				'dateline' => $today,
312				'count' => 1,
313				'posts' => $posts,
314			));
315		}
316	}
317}
318
319function buildbitsql($fieldname, $position, $value) {
320	$t = " `$fieldname`=`$fieldname`";
321	if($value) {
322		$t .= ' | '.setstatus($position, 1);
323	} else {
324		$t .= ' & '.setstatus($position, 0);
325	}
326	return $t.' ';
327}
328
329function showmessagenoperm($type, $fid, $formula = '') {
330	global $_G;
331	loadcache('usergroups');
332	if($formula) {
333		$formula = dunserialize($formula);
334		$permmessage = stripslashes($formula['message']);
335	}
336
337	$usergroups = $nopermgroup = $forumnoperms = array();
338	$nopermdefault = array(
339		'viewperm' => array(),
340		'getattachperm' => array(),
341		'postperm' => array(7),
342		'replyperm' => array(7),
343		'postattachperm' => array(7),
344	);
345	$perms = array('viewperm', 'postperm', 'replyperm', 'getattachperm', 'postattachperm');
346
347	foreach($_G['cache']['usergroups'] as $gid => $usergroup) {
348		$usergroups[$gid] = $usergroup['type'];
349		$grouptype = $usergroup['type'] == 'member' ? 0 : 1;
350		$nopermgroup[$grouptype][] = $gid;
351	}
352	if($fid == $_G['forum']['fid']) {
353		$forum = $_G['forum'];
354	} else {
355		$forum = C::t('forum_forumfield')->fetch($fid);
356	}
357
358	foreach($perms as $perm) {
359		$permgroups = explode("\t", $forum[$perm]);
360		$membertype = $forum[$perm] ? array_intersect($nopermgroup[0], $permgroups) : TRUE;
361		$forumnoperm = $forum[$perm] ? array_diff(array_keys($usergroups), $permgroups) : $nopermdefault[$perm];
362		foreach($forumnoperm as $groupid) {
363			$nopermtype = $membertype && $groupid == 7 ? 'login' : ($usergroups[$groupid] == 'system' || $usergroups[$groupid] == 'special' ? 'none' : ($membertype ? 'upgrade' : 'none'));
364			$forumnoperms[$fid][$perm][$groupid] = array($nopermtype, $permgroups);
365		}
366	}
367
368	$v = $forumnoperms[$fid][$type][$_G['groupid']][0];
369	$gids = $forumnoperms[$fid][$type][$_G['groupid']][1];
370	$comma = $permgroups = '';
371	if(is_array($gids)) {
372		foreach($gids as $gid) {
373			if($gid && $_G['cache']['usergroups'][$gid]) {
374				$permgroups .= $comma.$_G['cache']['usergroups'][$gid]['grouptitle'];
375				$comma = ', ';
376			} elseif($_G['setting']['verify']['enabled'] && substr($gid, 0, 1) == 'v') {
377				$vid = substr($gid, 1);
378				$permgroups .= $comma.$_G['setting']['verify'][$vid]['title'];
379				$comma = ', ';
380			}
381
382		}
383	}
384
385	$custom = 0;
386	if($permmessage) {
387		$message = $permmessage;
388		$custom = 1;
389	} else {
390		if($v) {
391			$message = $type.'_'.$v.'_nopermission';
392		} else {
393			$message = 'group_nopermission';
394		}
395	}
396
397	showmessage($message, NULL, array('fid' => $fid, 'permgroups' => $permgroups, 'grouptitle' => $_G['group']['grouptitle']), array('login' => 1), $custom);
398}
399
400function loadforum() {
401	global $_G;
402	$tid = intval(getgpc('tid'));
403	$fid = getgpc('fid');
404	if(!$fid && getgpc('gid')) {
405		$fid = intval(getgpc('gid'));
406	}
407	if(!empty($_GET['archiver'])) {//X1.5的Archiver兼容
408		if($fid) {
409			dheader('location: archiver/?fid-'.$fid.'.html');
410		} elseif($tid) {
411			dheader('location: archiver/?tid-'.$tid.'.html');
412		} else {
413			dheader('location: archiver/');
414		}
415	}
416	if(defined('IN_ARCHIVER') && $_G['setting']['archiverredirect'] && !IS_ROBOT) {
417		dheader('location: ../forum.php'.($_G['mod'] ? '?mod='.$_G['mod'].(!empty($_GET['fid']) ? '&fid='.$_GET['fid'] : (!empty($_GET['tid']) ? '&tid='.$_GET['tid'] : '')) : ''));
418	}
419	if($_G['setting']['forumpicstyle']) {
420		$_G['setting']['forumpicstyle'] = dunserialize($_G['setting']['forumpicstyle']);
421		empty($_G['setting']['forumpicstyle']['thumbwidth']) && $_G['setting']['forumpicstyle']['thumbwidth'] = 214;
422		empty($_G['setting']['forumpicstyle']['thumbheight']) && $_G['setting']['forumpicstyle']['thumbheight'] = 160;
423	} else {
424		$_G['setting']['forumpicstyle'] = array('thumbwidth' => 214, 'thumbheight' => 160);
425	}
426	if($fid) {
427		$fid = is_numeric($fid) ? intval($fid) : (!empty($_G['setting']['forumfids'][$fid]) ? $_G['setting']['forumfids'][$fid] : 0);
428	}
429
430	$modthreadkey = isset($_GET['modthreadkey']) && $_GET['modthreadkey'] == modauthkey($tid) ? $_GET['modthreadkey'] : '';
431	$_G['forum_auditstatuson'] = $modthreadkey ? true : false;
432
433	$metadescription = $hookscriptmessage = '';
434	$adminid = $_G['adminid'];
435
436	if(!empty($tid) || !empty($fid)) {
437
438		if(!empty ($tid)) {
439			$archiveid = !empty($_GET['archiveid']) ? intval($_GET['archiveid']) : null;
440			$_G['thread'] = get_thread_by_tid($tid, $archiveid);
441			if(!$_G['forum_auditstatuson'] && !empty($_G['thread'])
442					&& !($_G['thread']['displayorder'] >= 0 || (in_array($_G['thread']['displayorder'], array(-4,-3,-2)) && $_G['uid'] && $_G['thread']['authorid'] == $_G['uid']))) {
443				$_G['thread'] = null;
444			}
445
446			$_G['forum_thread'] = & $_G['thread'];
447
448			if(empty($_G['thread'])) {
449				$fid = $tid = 0;
450			} else {
451				$fid = $_G['thread']['fid'];
452				$tid = $_G['thread']['tid'];
453			}
454		}
455
456		if($fid) {
457			$forum = C::t('forum_forum')->fetch_info_by_fid($fid);
458		}
459
460		if($forum) {
461			if($_G['uid']) {
462				if($_G['member']['accessmasks']) {
463					$query = C::t('forum_access')->fetch_all_by_fid_uid($fid, $_G['uid']);
464					$forum['allowview'] = $query[0]['allowview'];
465					$forum['allowpost'] = $query[0]['allowpost'];
466					$forum['allowreply'] = $query[0]['allowreply'];
467					$forum['allowgetattach'] = $query[0]['allowgetattach'];
468					$forum['allowgetimage'] = $query[0]['allowgetimage'];
469					$forum['allowpostattach'] = $query[0]['allowpostattach'];
470					$forum['allowpostimage'] = $query[0]['allowpostimage'];
471				}
472				if($adminid == 3) {
473					$forum['ismoderator'] = C::t('forum_moderator')->fetch_uid_by_fid_uid($fid, $_G['uid']);
474				}
475			}
476			$forum['ismoderator'] = !empty($forum['ismoderator']) || $adminid == 1 || $adminid == 2 ? 1 : 0;
477			$fid = $forum['fid'];
478			$gorup_admingroupids = $_G['setting']['group_admingroupids'] ? dunserialize($_G['setting']['group_admingroupids']) : array('1' => '1');
479
480			if($forum['status'] == 3) {
481				if(!empty($forum['moderators'])) {
482					$forum['moderators'] = dunserialize($forum['moderators']);
483				} else {
484					require_once libfile('function/group');
485					$forum['moderators'] = update_groupmoderators($fid);
486				}
487				if($_G['uid'] && $_G['adminid'] != 1) {
488					$forum['ismoderator'] = !empty($forum['moderators'][$_G['uid']]) ? 1 : 0;
489					$_G['adminid'] = 0;
490					if($forum['ismoderator'] || $gorup_admingroupids[$_G['groupid']]) {
491						$_G['adminid'] = $_G['adminid'] ? $_G['adminid'] : 3;
492						if(!empty($gorup_admingroupids[$_G['groupid']])) {
493							$forum['ismoderator'] = 1;
494							$_G['adminid'] = 2;
495						}
496
497						$group_userperm = dunserialize($_G['setting']['group_userperm']);
498						if(is_array($group_userperm)) {
499							$_G['group'] = array_merge($_G['group'], $group_userperm);
500							$_G['group']['allowmovethread'] = $_G['group']['allowcopythread'] = $_G['group']['allowedittypethread']= 0;
501						}
502					}
503				}
504			}
505			foreach(array('threadtypes', 'threadsorts', 'creditspolicy', 'modrecommend') as $key) {
506				$forum[$key] = !empty($forum[$key]) ? dunserialize($forum[$key]) : array();
507				if(!is_array($forum[$key])) {
508					$forum[$key] = array();
509				}
510			}
511
512			if($forum['status'] == 3) {
513				$_G['isgroupuser'] = 0;
514				$_G['basescript'] = 'group';
515				if($forum['level'] == 0) {
516					$levelinfo = C::t('forum_grouplevel')->fetch_by_credits($forum['commoncredits']);
517					$levelid = $levelinfo['levelid'];
518					$forum['level'] = $levelid;
519					C::t('forum_forum')->update_group_level($levelid, $fid);
520				}
521				if($forum['level'] != -1) {
522					loadcache('grouplevels');
523					$grouplevel = $_G['grouplevels'][$forum['level']];
524					if(!empty($grouplevel['icon'])) {
525						$valueparse = parse_url($grouplevel['icon']);
526						if(!isset($valueparse['host'])) {
527							$grouplevel['icon'] = $_G['setting']['attachurl'].'common/'.$grouplevel['icon'];
528						}
529					}
530				}
531
532				$group_postpolicy = $grouplevel['postpolicy'];
533				if(is_array($group_postpolicy)) {
534					$forum = array_merge($forum, $group_postpolicy);
535				}
536				$forum['allowfeed'] = $_G['setting']['group_allowfeed'];
537				if($_G['uid']) {
538					if(!empty($forum['moderators'][$_G['uid']])) {
539						$_G['isgroupuser'] = 1;
540					} else {
541						$groupuserinfo = C::t('forum_groupuser')->fetch_userinfo($_G['uid'], $fid);
542						$_G['isgroupuser'] = $groupuserinfo['level'];
543						if($_G['isgroupuser'] <= 0 && empty($forum['ismoderator'])) {
544							$_G['group']['allowrecommend'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowrecommend'] = 0;
545							$_G['group']['allowcommentpost'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentpost'] = 0;
546							$_G['group']['allowcommentitem'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentitem'] = 0;
547							$_G['group']['raterange'] = $_G['cache']['usergroup_'.$_G['groupid']]['raterange'] = array();
548							$_G['group']['allowvote'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowvote'] = 0;
549						} else {
550							$_G['isgroupuser'] = 1;
551						}
552					}
553				}
554			}
555		} else {
556			$fid = 0;
557		}
558	}
559
560	$_G['fid'] = $fid;
561	$_G['tid'] = $tid;
562	$_G['forum'] = &$forum;
563	$_G['current_grouplevel'] = &$grouplevel;
564
565	if(!empty($_G['forum']['widthauto'])) {
566		$_G['widthauto'] = $_G['forum']['widthauto'];
567	}
568}
569
570function get_thread_by_tid($tid, $forcetableid = null) {
571	global $_G;
572
573	$ret = array();
574	if(!is_numeric($tid)) {
575		return $ret;
576	}
577	loadcache('threadtableids');
578	$threadtableids = array(0);
579	if(!empty($_G['cache']['threadtableids'])) {
580		if($forcetableid === null || ($forcetableid > 0 && !in_array($forcetableid, $_G['cache']['threadtableids']))) {
581			$threadtableids = array_merge($threadtableids, $_G['cache']['threadtableids']);
582		} else {
583			$threadtableids = array(intval($forcetableid));
584		}
585	}
586	$threadtableids = array_unique($threadtableids);
587	foreach($threadtableids as $tableid) {
588		$tableid = $tableid > 0 ? $tableid : 0;
589		$ret = C::t('forum_thread')->fetch($tid, $tableid);
590		if($ret) {
591			$ret['threadtable'] = C::t('forum_thread')->get_table_name($tableid);
592			$ret['threadtableid'] = $tableid;
593			$ret['posttable'] = 'forum_post'.($ret['posttableid'] ? '_'.$ret['posttableid'] : '');
594			break;
595		}
596	}
597
598	if(!is_array($ret)) {
599		$ret = array();
600	} elseif($_G['setting']['optimizeviews']) {
601		$row = C::t('forum_threadaddviews')->fetch($tid);
602		$ret['addviews'] = intval($row['addviews']);
603		$ret['views'] += $ret['addviews'];
604	}
605
606	return $ret;
607}
608
609function get_post_by_pid($pid, $fields = '*', $addcondiction = '', $forcetable = null) {
610	global $_G;
611
612	$ret = array();
613	if(!is_numeric($pid)) {
614		return $ret;
615	}
616
617	loadcache('posttable_info');
618
619	$posttableids = array(0);
620	if($_G['cache']['posttable_info']) {
621		if(isset($forcetable)) {
622			if(is_numeric($forcetable) && array_key_exists($forcetable, $_G['cache']['posttable_info'])) {
623				$posttableids[] = $forcetable;
624			} elseif(substr($forcetable, 0, 10) == 'forum_post') {
625				$posttableids[] = $forcetable;
626			}
627		} else {
628			$posttableids = array_keys($_G['cache']['posttable_info']);
629		}
630	}
631
632	foreach ($posttableids as $id) {
633		$table = empty($id) ? 'forum_post' : (is_numeric($id) ? 'forum_post_'.$id : $id);
634		$ret = C::t('forum_post')->fetch_by_pid_condition($id, $pid, $addcondiction, $fields);
635		if($ret) {
636			$ret['posttable'] = $table;
637			break;
638		}
639	}
640
641	if(!is_array($ret)) {
642		$ret = array();
643	}
644
645	return $ret;
646}
647
648function set_rssauth() {
649	global $_G;
650	if($_G['setting']['rssstatus'] && $_G['uid']) {
651		$auth = authcode($_G['uid']."\t".($_G['fid'] ? $_G['fid'] : '').
652		"\t".substr(md5($_G['member']['password']), 0, 8), 'ENCODE', md5($_G['config']['security']['authkey']));
653	} else {
654		$auth = '0';
655	}
656	$_G['rssauth'] = rawurlencode($auth);
657}
658
659function rssforumperm($forum) {
660	$is_allowed = $forum['type'] != 'group' && (!$forum['viewperm'] || ($forum['viewperm'] && forumperm($forum['viewperm'], 7)));
661	return $is_allowed;
662}
663
664function upload_icon_banner(&$data, $file, $type) {
665	global $_G;
666	$data['extid'] = empty($data['extid']) ? $data['fid'] : $data['extid'];
667	if(empty($data['extid'])) return '';
668
669	if($data['status'] == 3 && $_G['setting']['group_imgsizelimit']) {
670		$file['size'] > ($_G['setting']['group_imgsizelimit'] * 1024) && showmessage('file_size_overflow', '', array('size' => $_G['setting']['group_imgsizelimit'] * 1024));
671	}
672	$upload = new discuz_upload();
673	$uploadtype = $data['status'] == 3 ? 'group' : 'common';
674
675	if(!$upload->init($file, $uploadtype, $data['extid'], $type)) {
676		return false;
677	}
678
679	if(!$upload->save()) {
680		if(!defined('IN_ADMINCP')) {
681			showmessage($upload->errormessage());
682		} else {
683			cpmsg($upload->errormessage(), '', 'error');
684		}
685	}
686	if($data['status'] == 3 && $type == 'icon') {
687		require_once libfile('class/image');
688		$img = new image;
689		$img->Thumb($upload->attach['target'], './'.$uploadtype.'/'.$upload->attach['attachment'], 48, 48, 'fixwr');
690	}
691	return $upload->attach['attachment'];
692}
693
694function arch_multi($total, $perpage, $page, $link) {
695	$pages = @ceil($total / $perpage) + 1;
696	$pagelink = '';
697	if($pages > 1) {
698		$pagelink .= lang('forum/archiver', 'page') . ": \n";
699		$pagestart = $page - 10 < 1 ? 1 : $page - 10;
700		$pageend = $page + 10 >= $pages ? $pages : $page + 10;
701		for($i = $pagestart; $i < $pageend; $i++) {
702			$pagelink .= ($i == $page ? "<strong>[$i]</strong>" : "<a href=\"$link&page=$i\">$i</a>")." \n";
703		}
704	}
705	return $pagelink;
706}
707
708function loadarchiver($path) {
709	global $_G;
710	if(!$_G['setting']['archiver']) {
711		require_once DISCUZ_ROOT . "./source/archiver/common/header.php";
712		echo '<div id="content">'.lang('message', 'forum_archiver_disabled').'</div>';
713		require_once DISCUZ_ROOT . "./source/archiver/common/footer.php";
714		exit;
715	}
716	$filename = $path . '.php';
717	return DISCUZ_ROOT . "./source/archiver/$filename";
718}
719
720function update_threadpartake($tid, $getsetarr = false) {
721	global $_G;
722	$setarr = array();
723	if($_G['uid'] && $tid) {
724		if($_G['setting']['heatthread']['period']) {
725			$partaked = C::t('forum_threadpartake')->fetch($tid, $_G['uid']);
726			$partaked = $partaked['uid'];
727			if(!$partaked) {
728				C::t('forum_threadpartake')->insert(array('tid' => $tid, 'uid' => $_G['uid'], 'dateline' => TIMESTAMP));
729				$setarr = C::t('forum_thread')->increase($tid, array('heats' => 1), false, 0, $getsetarr);
730			}
731		} else {
732			$setarr = C::t('forum_thread')->increase($tid, array('heats' => 1), false, 0, $getsetarr);
733
734		}
735	}
736	if($getsetarr) {
737		return $setarr;
738	}
739}
740
741function getthreadcover($tid, $cover = 0, $getfilename = 0) {
742	global $_G;
743	if(empty($tid)) {
744		return '';
745	}
746	$coverpath = '';
747	$covername = 'threadcover/'.substr(md5($tid), 0, 2).'/'.substr(md5($tid), 2, 2).'/'.$tid.'.jpg';
748	if($getfilename) {
749		return $covername;
750	}
751	if($cover) {
752		$coverpath = ($cover < 0 ? $_G['setting']['ftp']['attachurl'] : $_G['setting']['attachurl']).'forum/'.$covername;
753	}
754	return $coverpath;
755}
756
757function convertunusedattach($aid, $tid, $pid) {
758	if(!$aid) {
759		return;
760	}
761	global $_G;
762	$attach = C::t('forum_attachment_n')->fetch_by_aid_uid(127, $aid, $_G['uid']);
763	if(!$attach) {
764		return;
765	}
766	$attach = daddslashes($attach);
767	$attach['tid'] = $tid;
768	$attach['pid'] = $pid;
769	C::t('forum_attachment_n')->insert('tid:'.$tid, $attach);
770	C::t('forum_attachment')->update($attach['aid'], array('tid' => $tid, 'pid' => $pid, 'tableid' => getattachtableid($tid)));
771	C::t('forum_attachment_unused')->delete($attach['aid']);
772}
773
774function updateattachtid($idtype, $ids, $oldtid, $newtid) {
775		foreach(C::t('forum_attachment_n')->fetch_all_by_id('tid:'.$oldtid, $idtype, $ids) as $attach) {
776			$attach['tid'] = $newtid;
777			C::t('forum_attachment_n')->insert('tid:'.$newtid, $attach);
778		}
779		C::t('forum_attachment_n')->delete_by_id('tid:'.$oldtid, $idtype, $ids);
780	C::t('forum_attachment')->update_by_id($idtype, $ids, $newtid);
781}
782
783function updatepost($data, $condition, $unbuffered = false, $posttableid = false) {
784	return false;
785}
786
787function insertpost($data) {
788	if(isset($data['tid'])) {
789		$thread = C::t('forum_thread')->fetch($data['tid']);
790		$tableid = $thread['posttableid'];
791	} else {
792		$tableid = $data['tid'] = 0;
793	}
794	$pid = C::t('forum_post_tableid')->insert(array('pid' => null), true);
795
796
797	$data = array_merge($data, array('pid' => $pid));
798
799	C::t('forum_post')->insert($tableid, $data);
800	if($pid % 1024 == 0) {
801		C::t('forum_post_tableid')->delete_by_lesspid($pid);
802	}
803	savecache('max_post_id', $pid);
804	return $pid;
805}
806
807function threadmodstatus($string) {
808	global $_G;
809	$postmodperiods = periodscheck('postmodperiods', 0);
810	if($postmodperiods) {
811		$modnewthreads = $modnewreplies = 1;
812	} else {
813		$censormod = censormod($string);
814		$modnewthreads = (!$_G['group']['allowdirectpost'] || $_G['group']['allowdirectpost'] == 1) && $_G['forum']['modnewposts'] || $censormod ? 1 : 0;
815		$modnewreplies = (!$_G['group']['allowdirectpost'] || $_G['group']['allowdirectpost'] == 2) && $_G['forum']['modnewposts'] == 2 || $censormod ? 1 : 0;
816
817		if($_G['forum']['status'] == 3) {
818			$modnewthreads = !$_G['group']['allowgroupdirectpost'] || $_G['group']['allowgroupdirectpost'] == 1 || $censormod ? 1 : 0;
819			$modnewreplies = !$_G['group']['allowgroupdirectpost'] || $_G['group']['allowgroupdirectpost'] == 2 || $censormod ? 1 : 0;
820		}
821	}
822
823	$_G['group']['allowposturl'] = $_G['forum']['status'] != 3 ? $_G['group']['allowposturl'] : $_G['group']['allowgroupposturl'];
824	if($_G['group']['allowposturl'] == 1) {
825		if(!$postmodperiods) {
826			$censormod = censormod($string);
827		}
828		if($censormod) {
829			$modnewthreads = $modnewreplies = 1;
830		}
831	}
832	return array($modnewthreads, $modnewreplies);
833}
834
835function threadpubsave($tid, $passapproval = false) {
836	global $_G;
837	if($_G['setting']['plugins']['func'][HOOKTYPE]['threadpubsave']) {
838		$hookparam = func_get_args();
839		hookscript('threadpubsave', 'global', 'funcs', array('param' => $hookparam, 'step' => 'check'), 'threadpubsave');
840	}
841	$thread = C::t('forum_thread')->fetch_by_tid_displayorder($tid, -4, '=', !$passapproval ? $_G['uid'] : null);
842	if(!$thread) {
843		return 0;
844	}
845	$threadpost = C::t('forum_post')->fetch_threadpost_by_tid_invisible($tid);
846	$thread['message'] = $threadpost['message'];
847
848	$modworksql = 0;
849	$displayorder = 0;
850	$dateline = $_G['timestamp'];
851	$moderatepids = $saveposts = array();
852	$return = 1;
853
854	list($modnewthreads) = threadmodstatus($thread['subject']."\t".$thread['message']);
855	if($modnewthreads && $passapproval === false) {
856		updatemoderate('tid', $tid);
857		manage_addnotify('verifythread');
858		$displayorder = -2;
859		$modworksql = 1;
860		$return = -1;
861	} else {
862		C::t('forum_post')->update_by_tid('tid:'.$tid, $tid, array('dateline' => $dateline, 'invisible' => '0'), false, false, 1);
863	}
864
865	C::t('forum_thread')->update($tid, array('displayorder'=>$displayorder, 'dateline'=>$_G['timestamp'], 'lastpost'=>$_G['timestamp']));
866	$posts = $thread['replies'] + 1;
867	if($thread['replies']) {
868		$saveposts = C::t('forum_post')->fetch_all_by_tid('tid:'.$tid, $tid, true, '', 0, 0, 0);
869		foreach($saveposts as $post) {
870			$dateline++;
871			$invisible = 0;
872			list(, $modnewreplies) = threadmodstatus($post['subject']."\t".$post['message']);
873			if($modnewreplies) {
874				$moderatepids[] = $post['pid'];
875				$verifypost = true;
876				$invisible = -2;
877				$modworksql = 1;
878				$return = -2;
879			}
880			C::t('forum_post')->update('tid:'.$tid, $post['pid'], array('dateline' => $dateline, 'invisible' => $invisible));
881			updatepostcredits('+', $thread['authorid'], 'reply', $thread['fid']);
882		}
883	}
884	if($moderatepids) {
885		updatemoderate('pid', $moderatepids);
886		manage_addnotify('verifypost');
887	}
888	updatepostcredits('+', $thread['authorid'], 'post', $thread['fid']);
889	$attachcount = C::t('forum_attachment_n')->count_by_id('tid:'.$thread['tid'], 'tid', $thread['tid']);
890	updatecreditbyaction('postattach', $thread['authorid'], array(), '', $attachcount, 1, $thread['fid']);
891	if($_G['forum']['status'] == 3) {
892		C::t('forum_groupuser')->update_counter_for_user($thread['authorid'], $thread['fid'], 1);
893	}
894
895	$subject = str_replace("\t", ' ', $thread['subject']);
896	$lastpost = $thread['tid']."\t".$subject."\t".$thread['lastpost']."\t".$thread['lastposter'];
897	C::t('forum_forum')->update($_G['fid'], array('lastpost' => $lastpost));
898	C::t('forum_forum')->update_forum_counter($thread['fid'], 1, $posts, $posts, $modworksql);
899	if($_G['forum']['type'] == 'sub') {
900		C::t('forum_forum')->update($_G['forum']['fup'], array('lastpost' => $lastpost));
901	}
902	if($_G['setting']['plugins']['func'][HOOKTYPE]['threadpubsave']) {
903		hookscript('threadpubsave', 'global', 'funcs', array('param' => $hookparam, 'step' => 'save', 'posts' => $saveposts), 'threadpubsave');
904	}
905	return $return;
906}
907
908function getrelatecollection($tid, $all = false, &$num, &$more) {
909	global $_G;
910
911	$maxdisplay = $_G['setting']['collectionnum'];
912	if(!$maxdisplay) return;
913
914	$tidrelate = C::t('forum_collectionrelated')->fetch($tid);
915	$ctids = explode("\t", $tidrelate['collection'], -1);
916	$num = count($ctids);
917
918	if(!$ctids || !$num) {
919		$more = $num = 0;
920		return null;
921	}
922	if($all !== true && $num > $maxdisplay) {
923		$more = 1;
924	} else {
925		$maxdisplay = 0;
926	}
927	return C::t('forum_collection')->fetch_all($ctids, 'follownum', 'DESC', 0, $maxdisplay, '', $tid);
928}
929
930function set_atlist_cookie($uids) {
931	global $_G;
932	$atlist = $tmp = array();
933	$num = 0;
934	$maxlist = 10;
935	if(empty($uids)) {
936		return;
937	}
938	$newnum = count($uids);
939	if($newnum >= $maxlist) {
940		$uids = array_slice($uids, 0, $maxlist);
941		dsetcookie('atlist', implode(',', $uids), 86400 * 360);
942		return;
943	}
944	if($_G['cookie']['atlist']) {
945		$atlist = explode(',', $_G['cookie']['atlist']);
946		foreach($atlist as $key => $val) {
947			if(!in_array($val, $uids)) {
948				$num++;
949				if($num == ($maxlist - $newnum)) {
950					break;
951				}
952				$tmp[$key] = $val;
953			}
954		}
955	}
956	dsetcookie('atlist', implode(',', $uids).($tmp ? ','.implode(',', $tmp) : ''), 86400 * 360);
957}
958
959function cloud_referer_related() {
960	global $_G;
961	$my_search_data = $_G['setting']['my_search_data'];
962	if (viewthread_is_search_referer() && $my_search_data['status']) {
963		$appService = Cloud::loadClass('Service_App');
964		if($appService->getCloudAppStatus('search')) {
965			$_params = array('s_site_gid' => $_G['groupid'],
966							'response_type' => 'js',
967							'referer' => $_SERVER['HTTP_REFERER'],
968						);
969			$utilService = Cloud::loadClass('Service_Util');
970			$signUrl = $utilService->generateSiteSignUrl($_params);
971			$my_search_se_url = 'http://search.discuz.qq.com/api/site/se?' . $signUrl . "";
972		}
973	}
974	return $my_search_se_url;
975}
976
977function viewthread_is_search_referer() {
978	$regex = "((http|https)\:\/\/)?";
979	$regex .= "([a-z]*.)?(ask.com|yahoo.com|cn.yahoo.com|bing.com|baidu.com|soso.com|google.com|google.cn)(.[a-z]{2,3})?\/";
980	if(preg_match("/^$regex/", $_SERVER['HTTP_REFERER'])) {
981		return true;
982	}
983	return false;
984}
985?>