<?php

function odz_loadforum() {

	global $_G;

	$tid = intval(getgpc('tid'));

	$fid = getgpc('fid');

	if(!$fid && getgpc('gid')) {

		$fid = intval(getgpc('gid'));

	}



	if($fid) {

		$fid = is_numeric($fid) ? intval($fid) : (!empty($_G['setting']['forumfids'][$fid]) ? $_G['setting']['forumfids'][$fid] : 0);

	}



	$modthreadkey = isset($_GET['modthreadkey']) && $_GET['modthreadkey'] == modauthkey($tid) ? $_GET['modthreadkey'] : '';

	$_G['forum_auditstatuson'] = $modthreadkey ? true : false;



	$metadescription = $hookscriptmessage = '';

	$adminid = $_G['adminid'];



	if(!empty($tid) || !empty($fid)) {



		if(!empty ($tid)) {

			$archiveid = !empty($_GET['archiveid']) ? intval($_GET['archiveid']) : null;

			$_G['thread'] = get_thread_by_tid($tid, $archiveid);

			if(!$_G['forum_auditstatuson'] && !empty($_G['thread'])

					&& !($_G['thread']['displayorder'] >= 0 || (in_array($_G['thread']['displayorder'], array(-4,-3,-2)) && $_G['uid'] && $_G['thread']['authorid'] == $_G['uid']))) {

				$_G['thread'] = null;

			}



			$_G['forum_thread'] = & $_G['thread'];



			if(empty($_G['thread'])) {

				$fid = $tid = 0;

			} else {

				$fid = $_G['thread']['fid'];

				$tid = $_G['thread']['tid'];

			}

		}



		if($fid) {

			$forum = C::t('forum_forum')->fetch_info_by_fid($fid);

		}



		if($forum) {

			if($_G['uid']) {

				if($_G['member']['accessmasks']) {

					$query = C::t('forum_access')->fetch_all_by_fid_uid($fid, $_G['uid']);

					$forum['allowview'] = $query[0]['allowview'];

					$forum['allowpost'] = $query[0]['allowpost'];

					$forum['allowreply'] = $query[0]['allowreply'];

					$forum['allowgetattach'] = $query[0]['allowgetattach'];

					$forum['allowgetimage'] = $query[0]['allowgetimage'];

					$forum['allowpostattach'] = $query[0]['allowpostattach'];

					$forum['allowpostimage'] = $query[0]['allowpostimage'];

				}

				if($adminid == 3) {

					$forum['ismoderator'] = C::t('forum_moderator')->fetch_uid_by_fid_uid($fid, $_G['uid']);

				}

			}

			$forum['ismoderator'] = !empty($forum['ismoderator']) || $adminid == 1 || $adminid == 2 ? 1 : 0;

			$fid = $forum['fid'];

			$gorup_admingroupids = $_G['setting']['group_admingroupids'] ? dunserialize($_G['setting']['group_admingroupids']) : array('1' => '1');



			if($forum['status'] == 3) {

				if(!empty($forum['moderators'])) {

					$forum['moderators'] = dunserialize($forum['moderators']);

				} else {

					require_once libfile('function/group');

					$forum['moderators'] = update_groupmoderators($fid);

				}

				if($_G['uid'] && $_G['adminid'] != 1) {

					$forum['ismoderator'] = !empty($forum['moderators'][$_G['uid']]) ? 1 : 0;

					$_G['adminid'] = 0;

					if($forum['ismoderator'] || $gorup_admingroupids[$_G['groupid']]) {

						$_G['adminid'] = $_G['adminid'] ? $_G['adminid'] : 3;

						if(!empty($gorup_admingroupids[$_G['groupid']])) {

							$forum['ismoderator'] = 1;

							$_G['adminid'] = 2;

						}



						$group_userperm = dunserialize($_G['setting']['group_userperm']);

						if(is_array($group_userperm)) {

							$_G['group'] = array_merge($_G['group'], $group_userperm);

							$_G['group']['allowmovethread'] = $_G['group']['allowcopythread'] = $_G['group']['allowedittypethread']= 0;

						}

					}

				}

			}

			foreach(array('threadtypes', 'threadsorts', 'creditspolicy', 'modrecommend') as $key) {

				$forum[$key] = !empty($forum[$key]) ? dunserialize($forum[$key]) : array();

				if(!is_array($forum[$key])) {

					$forum[$key] = array();

				}

			}



			if($forum['status'] == 3) {

				$_G['isgroupuser'] = 0;

				$_G['basescript'] = 'group';

				if($forum['level'] == 0) {

					$levelinfo = C::t('forum_grouplevel')->fetch_by_credits($forum['commoncredits']);

					$levelid = $levelinfo['levelid'];

					$forum['level'] = $levelid;

					C::t('forum_forum')->update_group_level($levelid, $fid);

				}

				if($forum['level'] != -1) {

					loadcache('grouplevels');

					$grouplevel = $_G['grouplevels'][$forum['level']];

					if(!empty($grouplevel['icon'])) {

						$valueparse = parse_url($grouplevel['icon']);

						if(!isset($valueparse['host'])) {

							$grouplevel['icon'] = $_G['setting']['attachurl'].'common/'.$grouplevel['icon'];

						}

					}

				}



				$group_postpolicy = $grouplevel['postpolicy'];

				if(is_array($group_postpolicy)) {

					$forum = array_merge($forum, $group_postpolicy);

				}

				$forum['allowfeed'] = $_G['setting']['group_allowfeed'];

				if($_G['uid']) {

					if(!empty($forum['moderators'][$_G['uid']])) {

						$_G['isgroupuser'] = 1;

					} else {

						$groupuserinfo = C::t('forum_groupuser')->fetch_userinfo($_G['uid'], $fid);

						$_G['isgroupuser'] = $groupuserinfo['level'];

						if($_G['isgroupuser'] <= 0 && empty($forum['ismoderator'])) {

							$_G['group']['allowrecommend'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowrecommend'] = 0;

							$_G['group']['allowcommentpost'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentpost'] = 0;

							$_G['group']['allowcommentitem'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowcommentitem'] = 0;

							$_G['group']['raterange'] = $_G['cache']['usergroup_'.$_G['groupid']]['raterange'] = array();

							$_G['group']['allowvote'] = $_G['cache']['usergroup_'.$_G['groupid']]['allowvote'] = 0;

						} else {

							$_G['isgroupuser'] = 1;

						}

					}

				}

			}

		} else {

			$fid = 0;

		}

	}



	//回帖仅作者可见

	if($_G['thread']['authorid'] == $_G['uid']){

		$forum['ismoderator'] = 1;

	}



	//未登录，ismoderator为0

	if(!$_G['uid']){

		$forum['ismoderator'] = 0;

	}



	$_G['fid'] = $fid;

	$_G['tid'] = $tid;

	$_G['forum'] = &$forum;

	$_G['current_grouplevel'] = &$grouplevel;



	if(!empty($_G['forum']['widthauto'])) {

		$_G['widthauto'] = $_G['forum']['widthauto'];

	}

}



function odzFormulaperm($formula) {

	global $_G;

	if($_G['forum']['ismoderator']) {

		return TRUE;

	}



	$formula = dunserialize($formula);

	$medalperm = $formula['medal'];

	$permusers = $formula['users'];

	$permmessage = $formula['message'];

	if($_G['setting']['medalstatus'] && $medalperm) {

		$exists = 1;

		$_G['forum_formulamessage'] = '';

		$medalpermc = $medalperm;

		if($_G['uid']) {

			$memberfieldforum = C::t('common_member_field_forum')->fetch($_G['uid']);

			$medals = explode("\t", $memberfieldforum['medals']);

			unset($memberfieldforum);

			foreach($medalperm as $k => $medal) {

				foreach($medals as $r) {

					list($medalid) = explode("|", $r);

					if($medalid == $medal) {

						$exists = 0;

						unset($medalpermc[$k]);

					}

				}

			}

		} else {

			$exists = 0;

		}

		if($medalpermc) {

			loadcache('medals');

			foreach($medalpermc as $medal) {

				if($_G['cache']['medals'][$medal]) {

					$_G['forum_formulamessage'] .= '<img src="'.STATICURL.'image/common/'.$_G['cache']['medals'][$medal]['image'].'" style="vertical-align:middle;" />&nbsp;'.$_G['cache']['medals'][$medal]['name'].'&nbsp; ';

				}

			}

			// showmessage('forum_permforum_nomedal', NULL, array('forum_permforum_nomedal' => $_G['forum_formulamessage']), array('login' => 1));

			echo '<div style="background-color:#f7f7f7;text-align:center;padding:5px;color:#666666;">'.odz_lang('forum_permforum_nomedal', array('forum_permforum_nomedal' => $_G['forum_formulamessage'])).'</div>';

        	exit;

		}

	}

	$formulatext = $formula[0];

	$formula = $formula[1];

	if($_G['adminid'] == 1 || $_G['forum']['ismoderator'] || in_array($_G['groupid'], explode("\t", $_G['forum']['spviewperm']))) {

		return FALSE;

	}

	if($permusers) {

		$permusers = str_replace(array("\r\n", "\r"), array("\n", "\n"), $permusers);

		$permusers = explode("\n", trim($permusers));

		if(!in_array($_G['member']['username'], $permusers)) {

			// showmessage('forum_permforum_disallow', NULL, array(), array('login' => 1));

			echo '<div style="background-color:#f7f7f7;text-align:center;padding:5px;color:#666666;">'.odz_lang('forum_permforum_disallow', array()).'</div>';

        	exit;

		}

	}

	if(!$formula) {

		return FALSE;

	}

	if(strexists($formula, '$memberformula[')) {

		preg_match_all("/\\\$memberformula\['(\w+?)'\]/", $formula, $a);

		$profilefields = array();

		foreach($a[1] as $field) {

			switch($field) {

				case 'regdate':

					$formula = preg_replace("/\{(\d{4})\-(\d{1,2})\-(\d{1,2})\}/e", "'\'\\1-'.sprintf('%02d', '\\2').'-'.sprintf('%02d', '\\3').'\''", $formula);

				case 'regday':

					break;

				case 'regip':

				case 'lastip':

					$formula = preg_replace("/\{([\d\.]+?)\}/", "'\\1'", $formula);

					$formula = preg_replace('/(\$memberformula\[\'(regip|lastip)\'\])\s*=+\s*\'([\d\.]+?)\'/', "strpos(\\1, '\\3')===0", $formula);

				case 'buyercredit':

				case 'sellercredit':

					space_merge($_G['member'], 'status');break;

				case substr($field, 0, 5) == 'field':

					space_merge($_G['member'], 'profile');

					$profilefields[] = $field;break;

			}

		}

		$memberformula = array();

		if($_G['uid']) {

			$memberformula = $_G['member'];

			if(in_array('regday', $a[1])) {

				$memberformula['regday'] = intval((TIMESTAMP - $memberformula['regdate']) / 86400);

			}

			if(in_array('regdate', $a[1])) {

				$memberformula['regdate'] = date('Y-m-d', $memberformula['regdate']);

			}

			$memberformula['lastip'] = $memberformula['lastip'] ? $memberformula['lastip'] : $_G['clientip'];

		} else {

			if(isset($memberformula['regip'])) {

				$memberformula['regip'] = $_G['clientip'];

			}

			if(isset($memberformula['lastip'])) {

				$memberformula['lastip'] = $_G['clientip'];

			}

		}

	}

	@eval("\$formulaperm = ($formula) ? TRUE : FALSE;");

	if(!$formulaperm) {

		if(!$permmessage) {

			$language = lang('forum/misc');

			$search = array('regdate', 'regday', 'regip', 'lastip', 'buyercredit', 'sellercredit', 'digestposts', 'posts', 'threads', 'oltime');

			$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']);

			for($i = 1; $i <= 8; $i++) {

				$search[] = 'extcredits'.$i;

				$replace[] = $_G['setting']['extcredits'][$i]['title'] ? $_G['setting']['extcredits'][$i]['title'] : $language['formulaperm_extcredits'].$i;

			}

			if($profilefields) {

				loadcache(array('fields_required', 'fields_optional'));

				foreach($profilefields as $profilefield) {

					$search[] = $profilefield;

					$replace[] = !empty($_G['cache']['fields_optional']['field_'.$profilefield]) ? $_G['cache']['fields_optional']['field_'.$profilefield]['title'] : $_G['cache']['fields_required']['field_'.$profilefield]['title'];

				}

			}

			$i = 0;$_G['forum_usermsg'] = '';

			foreach($search as $s) {

				if(in_array($s, array('digestposts', 'posts', 'threads', 'oltime', 'extcredits1', 'extcredits2', 'extcredits3', 'extcredits4', 'extcredits5', 'extcredits6', 'extcredits7', 'extcredits8'))) {

					$_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />&nbsp;&nbsp;&nbsp;'.$replace[$i].': '.(@eval('return intval(getuserprofile(\''.$s.'\'));')) : '';

				} elseif(in_array($s, array('regdate', 'regip', 'regday'))) {

					$_G['forum_usermsg'] .= strexists($formulatext, $s) ? '<br />&nbsp;&nbsp;&nbsp;'.$replace[$i].': '.(@eval('return $memberformula[\''.$s.'\'];')) : '';

				}

				$i++;

			}

			$search = array_merge($search, array('and', 'or', '>=', '<=', '=='));

			$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;', '='));

			$_G['forum_formulamessage'] = str_replace($search, $replace, $formulatext);

		} else {

			$_G['forum_formulamessage'] = $permmessage;

		}



		if(!$permmessage) {

			// showmessage('forum_permforum_nopermission', NULL, array('formulamessage' => $_G['forum_formulamessage'], 'usermsg' => $_G['forum_usermsg']), array('login' => 1));

			echo '<div style="background-color:#f7f7f7;text-align:center;padding:5px;color:#666666;">'.odz_lang('forum_permforum_nopermission', array('formulamessage' => $_G['forum_formulamessage'], 'usermsg' => $_G['forum_usermsg'])).'</div>';

        	exit;

		} else {

			// showmessage('forum_permforum_nopermission_custommsg', NULL, array('formulamessage' => $_G['forum_formulamessage']), array('login' => 1));

			echo '<div style="background-color:#f7f7f7;text-align:center;padding:5px;color:#666666;">'.odz_lang('forum_permforum_nopermission_custommsg', array('formulamessage' => $_G['forum_formulamessage'])).'</div>';

        	exit;

		}

	}

	return TRUE;

}

?>