<?php

/*
	[Discuz!] (C)2001-2006 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: forum.func.php,v $
	$Revision: 1.17.4.2 $
	$Date: 2006/09/30 03:10:09 $
*/

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

function checkautoclose() {
	global $timestamp, $forum, $thread;

	if(!$forum['ismoderator'] && $forum['autoclose']) {
		$closedby = $forum['autoclose'] > 0 ? 'dateline' : 'lastpost';
		$forum['autoclose'] = abs($forum['autoclose']);
		if($timestamp - $thread[$closedby] > $forum['autoclose'] * 86400) {
			return 'post_thread_closed_by_'.$closedby;
		}
	}
	return FALSE;
}

function forum(&$forum) {
	global $_DCOOKIE, $timestamp, $timeformat, $dateformat, $discuz_uid, $groupid, $lastvisit, $moddisplay, $timeoffset, $hideprivate, $onlinehold;

	if(!$forum['viewperm'] || ($forum['viewperm'] && forumperm($forum['viewperm'])) || !empty($forum['allowview']) || (isset($forum['users']) && strstr($forum['users'], "\t$discuz_uid\t"))) {
		$forum['permission'] = 2;
	} elseif(!$hideprivate) {
		$forum['permission'] = 1;
	} else {
		return FALSE;
	}

	if($forum['icon']) {
		$forum['icon'] = '<a href="forumdisplay.php?fid='.$forum['fid'].'">'.image($forum['icon'], '', 'align="left" alt=""').'</a>';
	}

	$lastpost = array('tid' => 0, 'dateline' => 0, 'subject' => '', 'subjectsc' => '', 'author' => '');
	list($lastpost['tid'], $lastpost['subject'], $lastpost['dateline'], $lastpost['author']) = is_array($forum['lastpost']) ? $forum['lastpost'] : explode("\t", $forum['lastpost']);
	$forum['folder'] = '<img src="'.IMGDIR.'/'.((isset($_DCOOKIE['fid'.$forum['fid']]) && $_DCOOKIE['fid'.$forum['fid']] > $lastvisit ? $_DCOOKIE['fid'.$forum['fid']] : $lastvisit) < $lastpost['dateline'] ? 'red_' : '').'forum.gif" alt="" />';

	if($lastpost['tid']) {
		$lastpost['dateline'] = gmdate("$dateformat $timeformat", $lastpost['dateline'] + $timeoffset * 3600);
		if($lastpost['author']) {
			$lastpost['author'] = '<a href="viewpro.php?username='.rawurlencode($lastpost['author']).'">'.$lastpost['author'].'</a>';
		}
		$forum['lastpost'] = $lastpost;
	} else {
		$forum['lastpost'] = '';
	}

	$forum['moderators'] = moddisplay($forum['moderators'], $moddisplay, !empty($forum['inheritedmod'])).'&nbsp;';

	if(isset($forum['subforums'])) {
		$forum['subforums'] = implode(', ', $forum['subforums']);
	}

	return TRUE;
}

function forumselect($groupselectable = FALSE, $tableformat = 0) {
	global $_DCACHE, $discuz_uid, $groupid, $fid, $gid, $indexname;

	if(!isset($_DCACHE['forums'])) {
		require_once DISCUZ_ROOT.'./forumdata/cache/cache_forums.php';
	}

	$forumlist = $tableformat ? '' : '<optgroup label="&nbsp;">';
	foreach($_DCACHE['forums'] as $key => $forum) {
		if(!$forum['viewperm'] || ($forum['viewperm'] && forumperm($forum['viewperm'])) || strstr($forum['users'], "\t$discuz_uid\t")) {
			if($forum['type'] == 'group') {
				$forumtree[$forum['type']][$forum['fup']][$forum['fid']] = $tableformat ?
					'\'<tr><td class="popupmenu_option"><a\'.(\$gid == \$_DCACHE[\'forums\']['.$key.'][\'fid\'] ? \' class="bold"\' : \'\').\' href="\'.\$indexname.\'?gid=\'.\$_DCACHE[\'forums\']['.$key.'][\'fid\'].\'">\'.\$_DCACHE[\'forums\']['.$key.'][\'name\'].\'</a></td></tr>\'' :
					'(\$groupselectable ? \'<option value="\'.\$_DCACHE[\'forums\']['.$key.'][\'fid\'].\'">\'.\$_DCACHE[\'forums\']['.$key.'][\'name\'].\'</option>\' : \'</optgroup><optgroup label="\'.\$_DCACHE[\'forums\']['.$key.'][\'name\'].\'">\')';
				$forumtree[$forum['type']][$forum['fup']][$forum['fid']] .= '.eval(\'return \'.@implode(\'.\', \$forumtree[\'forum\'][\$_DCACHE[\'forums\']['.$key.'][\'fid\']]).\';\')';
			} elseif($forum['type'] == 'forum') {
				$forumtree[$forum['type']][$forum['fup']][$forum['fid']] = $tableformat ?
					'\'<tr><td class="popupmenu_option"><a\'.(\$fid == \$_DCACHE[\'forums\']['.$key.'][\'fid\'] ? \' class="bold"\' : \'\').\' href="forumdisplay.php?fid=\'.\$_DCACHE[\'forums\']['.$key.'][\'fid\'].\'">&nbsp; &gt; \'.\$_DCACHE[\'forums\']['.$key.'][\'name\'].\'</a></td></tr>\'' :
					'\'<option value="\'.\$_DCACHE[\'forums\']['.$key.'][\'fid\'].\'">&nbsp; &gt; \'.\$_DCACHE[\'forums\']['.$key.'][\'name\'].\'</option>\'';
				$forumtree[$forum['type']][$forum['fup']][$forum['fid']] .= '.eval(\'return \'.@implode(\'.\', \$forumtree[\'sub\'][\$_DCACHE[\'forums\']['.$key.'][\'fid\']]).\';\')';
			} else {
				$forumtree[$forum['type']][$forum['fup']][] = $tableformat ?
					'\'<tr><td class="popupmenu_option"><a\'.(\$fid == \$_DCACHE[\'forums\']['.$key.'][\'fid\'] ? \' class="bold"\' : \'\').\' href="forumdisplay.php?fid=\'.\$_DCACHE[\'forums\']['.$key.'][\'fid\'].\'">&nbsp; &nbsp; &nbsp; &gt; \'.\$_DCACHE[\'forums\']['.$key.'][\'name\'].\'</a></td></tr>\'' :
					'\'<option value="\'.\$_DCACHE[\'forums\']['.$key.'][\'fid\'].\'">&nbsp; &nbsp; &nbsp; &gt; \'.\$_DCACHE[\'forums\']['.$key.'][\'name\'].\'</option>\'';
			}
		}
	}
	$forumlist .= $forumtree['group'][0] ? eval('return '.implode('.', $forumtree['group'][0]).';') : '';
	$forumlist .= $tableformat ? '' : '</optgroup>';
	$forumlist = $tableformat ? $forumlist : str_replace('<optgroup label="&nbsp;"></optgroup>', '', $forumlist);

	return $forumlist;
}

function visitedforums() {
	global $_DCACHE, $_DCOOKIE, $forum;

	$count = 0;
	$visitedforums = '';
	$fidarray = array($forum['fid']);
	foreach(explode('D', $_DCOOKIE['visitedfid']) as $fid) {
		if(isset($_DCACHE['forums'][$fid]) && !in_array($fid, $fidarray)) {
			$fidarray[] = $fid;
			if($fid != $forum['fid']) {
				$visitedforums .= '<option value="'.$fid.'">'.$_DCACHE['forums'][$fid]['name'].'</option>';
				if(++$count >= $GLOBALS['visitedforums']) {
					break;
				}

			}
		}
	}
	if(($visitedfid = implode('D', $fidarray)) != $_DCOOKIE['visitedfid']) {
		dsetcookie('visitedfid', $visitedfid, 2592000);
	}
	return $visitedforums;
}

function moddisplay($moderators, $type, $inherit = 0) {
	if($type == 'selectbox') {
		$modlist .= '<img src="images/common/online_moderator.gif" class="absmiddle" alt="" /><select '.($moderators ? '' : 'disabled').' name="modlist" style="width: 100px'.($inherit ? '; font-weight: bold"' : '').'">';

		if($moderators) {
			foreach(explode("\t", $moderators) as $moderator) {
				$modlist .= '<option value="'.rawurlencode($moderator).'">'.$moderator.'</option>';
			}
		} else {
			$modlist .= '<option></option>';
		}
		$modlist .= '</select>';
		return $modlist;
	} else {
		if($moderators) {
			$modlist = $comma = '';
			foreach(explode("\t", $moderators) as $moderator) {
				$modlist .= $comma.'<a href="viewpro.php?username='.rawurlencode($moderator).'">'.($inherit ? $moderator : $moderator).'</a>';
				$comma = ', ';
			}
		} else {
			$modlist = '';
		}
		return $modlist;
	}
}

?>