<?php
define('THIS_SCRIPT', 'forum_savvy');
include_once 'global.php';
$lang = load_lang($lang,'forum_savvy');
include_once CACHE_PATH.'savvy_board.php';
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'51,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $action,$dv,$db,$lang,$savvy_board;
	if($action=='save'){
		Save_SavvyBoardKey();
	}
include printout('forum_savvy');
}

function Save_SavvyBoardKey(){
	global $db,$dv,$lang;
	$getboard=array();
	$getboard=$_POST['getboardid'];
	$boardkey=$_POST['boardkey'];
	$foruwopen=$_POST['foruwopen'];
	$system_put_integral=$_POST['system_put_integral'];
	$high_integral=$_POST['high_integral'];
	$prise_integral=$_POST['prise_integral'];
	$hiddenname=$_POST['hiddenname'];
	$askcount=$_POST['askcount'];
	$geturl=$_SERVER['HTTP_REFERER'];
	if(count($getboard)<=0){
		show_msg($lang['sys.info'],$lang['savvy.info7'],0,'','',$geturl,$lang['comback']);
		exit;
	}
	$getallboard=implode(',',$getboard);
	$i=1;
	$contents.= "<?php\r\n\$savvy_board['boardinfo'] = array(\r\n";
	foreach ($getboard as $getboardarr){
		$contents.=GetBoarderInfo($getboardarr,$i);
		$i++;
	}
	$contents.="\r\n);\r\n\r\n";
	$contents.= GetBoardKey($boardkey,$high_integral,$system_put_integral,$prise_integral,$hiddenname,$askcount);
	$contents.="\r\n);\r\n\r\n";
	$contents.= GetBoardid($getallboard);
	$contents.="\r\n);\r\n\r\n";
	$contents.= hot_question();
	$contents.="\r\n);\r\n\r\n";
	$contents.= best_question();
	$contents.="\r\n);\r\n\r\n";
	$contents.= user_weekmoney();
	$contents.="\r\n);\r\n\r\n";
	$contents.= Getsetup($foruwopen);
	if(empty($contents)){
		return false;
	} else{
		$contents .= "\r\n);\r\n".'?'.'>';
		$currcachefile = CACHE_PATH.'savvy_board.php';
		@chmod($currcachefile,0777);
		putcachefile($contents, $currcachefile);
		show_msg($lang['sys.info'],$lang['savvy.info9'],1,'','',$geturl,$lang['comback']);
		exit;
	}
	
}

function GetBoarderInfo($bid,$taxit){
	global $dv,$db,$lang;
	$sql=$db->query("select boardid,boardtype,showtype,parentid,parentstr,depth,rootid,child,readme,boardmaster,postnum,topicnum,indeximg,todaynum,boarduser,lastpost,orders,board_setting,board_user,isgroupsetting,boardtopstr,cid,rules,allowpost,ishidden,ischeckout,ads_id,setting_id,audit_settings_id from {$dv}board where boardid={$bid}");
	$tmpContents='';
	$forumnews='';
	//取得广告、版面和审核设置的第一个ID项，避免因找不到对应ID时引起的错误
	$query=$db->query("Select id From {$dv}fsettings Where s_class=1 Order By c_orders");
	while($fs =& $db->fetch_array($query)){
		if ( $setting_id == '' ){
			$setting_id = $fs['id'];
		}else{
			$setting_id .= ',' .$fs['id'];
		}
	}
	$setting_id_array = explode(',',$setting_id);
	$setting_id = ',' .$setting_id. ',';
	$querysql=$db->query("Select id From {$dv}fsettings Where s_class=2 Order By c_orders");
	while($fs =& $db->fetch_array($querysql)){
		if ( $ads_id == '' ){
			$ads_id = $fs['id'];
		}else{
			$ads_id .= ',' .$fs['id'];
		}
	}
	$ads_id_array = explode(',',$ads_id);
	$ads_id = ',' .$ads_id. ',';
	while ($arr=$db->fetch_array($sql)){
		$myquery=$db->query("Select title,addtime From {$dv}bbsnews Where boardid={$arr['boardid']} Order By ID Desc",array('pageSize'=>1));
		if($newsinfo =& $db->fetch_array($myquery)){
			$forumnews = str_replace(array('\\', "'"), array('\\\\', "\\'"), $newsinfo['title']);
			$forumnews = '<b>'.$forumnews.'</b> (<span class="runtime_timestamp">'.$newsinfo['addtime'].'</span>)';
		}else{
			$forumnews = '';
		}
		if ( strpos($setting_id,','.$arr['setting_id'].',') !== false ){
			$mysetting_id = $arr['setting_id'];
		}else{
			$mysetting_id = $setting_id_array[0];
		}
		if ( strpos($ads_id,','.$forum['ads_id'].',') !== false ){
			$myads_id = $forum['ads_id'];
		}else{
			$myads_id = $ads_id_array[0];
		}
		$boardid = strleach($arr['boardid']);
		$boardtype = strleach($arr['boardtype']);
		$parentid = strleach($arr['parentid']);
		$parentstr = strleach($arr['parentstr']);
		$boardmaster = strleach($arr['boardmaster']);
		$hidden = intval($arr['ishidden']);
		$checkout = intval($arr['ischeckout']);
		$depth = intval($arr['depth']);
		$postnum = intval($arr['postnum']);
		$topicnum = $db->scalar("select count(1) from {$dv}savvy_topic where boardid={$bid}");
		$todaynum = intval($arr['todaynum']);
		$child = $arr['child'];
		if( $taxit == 1 ) {
			$tmpContents = '1';
		} else{
			$contents .= ",\r\n";
		}
		if(!empty($boardid)){
			$contents .= <<<EOT
\t'{$taxit}' => array(
\t\t'savvy_boardid' => '{$boardid}',
\t\t'savvy_boardtype' => '{$boardtype}',
\t\t'savvy_parentid' => '{$parentid}',
\t\t'savvy_parentstr' => '{$parentstr}',
\t\t'savvy_child' => '{$child}',
\t\t'savvy_boardmaster' => '{$boardmaster}',
\t\t'savvy_hidden' => '{$hidden}',
\t\t'savvy_checkout' => '{$checkout}',
\t\t'savvy_depth' => '{$depth}',
\t\t'savvy_forumnews' => '{$forumnews}',
\t\t'savvy_setting_id' => '{$mysetting_id}',
\t\t'savvy_ads_id' => '{$myads_id}',
\t\t'savvy_postnum' => '{$postnum}',
\t\t'savvy_topicnum' => '{$topicnum}',
\t\t'savvy_todaynum' => '{$todaynum}'
\t)
EOT;
		}
	}
	$db->free_result($sql);
	if( empty($contents) ){
		return false;
	} else{
		$contents .= "";
	}
	return $contents;
}

function putcachefile(&$contents,$file,$append=false){
	if( function_exists('file_put_contents') ){
		if ($append===false) {
			return ( @file_put_contents($file, $contents) > 0 ? true : false );
		}
		else {
			if( $fp = fopen($file, ($append !== false ? 'ab' : 'wb')) ){
				@flock($fp, 2);
				$writed = @fwrite($fp, $contents, strlen($contents));
				@fclose($fp);
				@chmod($file,0755);
				return ( $writed === false ? false : true );
			} else{
				return false;
			}
		}
	} else{
		if( $fp = fopen($file, ($append !== false ? 'ab' : 'wb')) ){
			@flock($fp, 2);
			$writed = @fwrite($fp, $contents, strlen($contents));
			@fclose($fp);
			@chmod($file,0755);
			return ( $writed === false ? false : true );
		} else{
			return false;
		}
	}
}


function GetBoardKey($bkey,$high,$sysput,$prise_num,$hiddennamefen,$askcountstr){
	global $db,$dv,$db;	
	$boardkey= preg_split('/\r\n|\n/i',$bkey);
	$boardkey_contents=implode(',',$boardkey);
	$contents .= "\r\n\$savvy_board['boardkey'] = array(\r\n";
	if( $tmpContents == '' ) {
		$tmpContents = '1';
	} else{
		$contents .= ",\r\n";
	}
	$beendboardkey_contents = substr($boardkey_contents, -1, 1);
	$boardkey_contents=($beendboardkey_contents==',')? substr(strleach($boardkey_contents),0,-1):strleach($boardkey_contents);
	$high=intval($high);
	$highsetup=(empty($high))? 100:$high;
	$sysput=intval($sysput);
	$sysputintegral=(empty($sysput))? 2:$sysput;
	$prise_num=intval($prise_num);
	$prise_integral=(empty($prise_num))? 2:$prise_num;
	$hiddennamefen=intval($hiddennamefen);
	$hiddenname_integral=(empty($hiddennamefen))? 10:$hiddennamefen;
	$askcountstr=intval($askcountstr);
	$askcountvalue=(empty($askcountstr))? 3:$askcountstr;
	$contents .= <<<EOT
\t\t'board_key' => '{$boardkey_contents}',
\t\t'highsetup' => '{$highsetup}',
\t\t'sysputintegral' => '{$sysputintegral}',
\t\t'prise_integral' => '{$prise_integral}',
\t\t'hiddenname' => '{$hiddenname_integral}',
\t\t'askcount' => '{$askcountvalue}'
EOT;
	if( empty($contents) ){
		return false;
	} else{
		$contents .= "\r\n";
	}
	return $contents;
}

function GetBoardid($bidlist){
	global $db,$dv,$db;	
	$contents .= "\r\n\$savvy_board['boardidset'] = array(\r\n";
	if( $tmpContents == '' ) {
		$tmpContents = '1';
	} else{
		$contents .= ",\r\n";
	}
	$contents .= <<<EOT
\t\t'boardid_set' => '{$bidlist}'
EOT;
	if( empty($contents) ){
		return false;
	} else{
		$contents .= "\r\n";
	}
	return $contents;
}


function Getsetup($ins){
	global $db,$dv,$db;	
	$contents .= "\r\n\$savvy_board['setup'] = array(\r\n";
	if( $tmpContents == '' ) {
		$tmpContents = '1';
	} else{
		$contents .= ",\r\n";
	}
	if(empty($ins)){
		$forumins='0';
	}
	else{
		$forumins=$ins;
	}
	$contents .= <<<EOT
\t\t'isInsertSavvy' => '{$forumins}'
EOT;
	if( empty($contents) ){
		return false;
	} else{
		$contents .= "\r\n";
	}
	return $contents;
}

function strleach($str){
	$content=str_replace(array('\\',"'"),array('\\\\',"\\'"),$str);
	return $content;
}


function boardkey_list(){
	global $dv,$db,$lang,$savvy_board;
	$tmpboardkey=explode(",",$savvy_board['boardkey']['board_key']);
	foreach($tmpboardkey as $key){
		$return_val .= $key."\r\n";
	}
	return $return_val;
}


function hot_question(){
	global $db,$dv,$lang;
	$usersql=$db->query("select topicid,title,boardid,pollid,locktopic,child,postusername,postuserid,dateandtime,hits,expression,votetotal,lastpost,lastposttime,istop,isvote,isbest,posttable,smsuserlist,issmstopic,lastsmstime,topicmode,mode,getmoney,usetools,getmoneytype,hidename,solve,anonymity,againmoney from {$dv}savvy_topic order by child desc",array('absolutePage'=>0,'pageSize'=>15));			
	$contents.= "\r\n\$savvy_board['tmphotinfo'] = array(\r\n";
	$tmpContents='';
	$j=0;
	while ($arrlist=$db->fetch_array($usersql)){
		$userid=$arrlist['postuserid'];
		$username=strleach($arrlist['postusername']);
		$topicid=strleach($arrlist['topicid']);
		$boardid=strleach($arrlist['boardid']);
		$title=strleach($arrlist['title']);
		$dateandtime=strleach($arrlist['dateandtime']);
		$child=strleach($arrlist['child']);
		$j++;
		if( $tmpContents == '' ) {
			$tmpContents = '1';
		} else{
			$contents.= ",\r\n";
		}
		$contents .= <<<EOT
\t'{$j}' => array(
\t\t'userid' => '{$userid}',
\t\t'username' => '{$username}',
\t\t'topicid' => '{$topicid}',
\t\t'boardid' => '{$boardid}',
\t\t'title' => '{$title}',
\t\t'dateandtime' => '{$dateandtime}',
\t\t'child' => '{$child}'
\t)
EOT;
	}
	$db->free_result($usersql);
	if( empty($contents) ){
		return false;
	} else{
		$contents .= "\r\n";
	}
	return $contents;
}


//论坛知道精华问题
//
//
//
function best_question(){
	global $db,$dv,$lang;
	$usersql=$db->query("select topicid,title,boardid,pollid,locktopic,child,postusername,postuserid,dateandtime,hits,expression,votetotal,lastpost,lastposttime,istop,isvote,isbest,posttable,smsuserlist,issmstopic,lastsmstime,topicmode,mode,getmoney,usetools,getmoneytype,hidename,solve,anonymity,againmoney from {$dv}savvy_topic where isbest=1 order by dateandtime desc",array('absolutePage'=>0,'pageSize'=>15));			
	$contents.= "\r\n\$savvy_board['tmpbestinfo'] = array(\r\n";
	$tmpContents='';
	$j=0;
	while ($arrlist=$db->fetch_array($usersql)){
		$userid=$arrlist['postuserid'];
		$username=strleach($arrlist['postusername']);
		$topicid=strleach($arrlist['topicid']);
		$boardid=strleach($arrlist['boardid']);
		$title=strleach($arrlist['title']);
		$dateandtime=strleach($arrlist['dateandtime']);
		$j++;
		if( $tmpContents == '' ) {
			$tmpContents = '1';
		} else{
			$contents.= ",\r\n";
		}
		$contents .= <<<EOT
\t'{$j}' => array(
\t\t'userid' => '{$userid}',
\t\t'username' => '{$username}',
\t\t'topicid' => '{$topicid}',
\t\t'boardid' => '{$boardid}',
\t\t'title' => '{$title}',
\t\t'dateandtime' => '{$dateandtime}'
\t)
EOT;
	}
	$db->free_result($usersql);
	if( empty($contents) ){
		return false;
	} else{
		$contents .= "\r\n";
	}
	return $contents;
}


//论坛知道用户上周积分排行
//

function user_weekmoney(){
	global $db,$dv,$lang;
	$front_weekdata=time()-7*24*3600;
	$usersql=$db->query("select id,userid,username,forumid,title,sum(wealth) as wealth,dateandtime,position from {$dv}savvy_wealth where dateandtime > {$front_weekdata} group by userid order by wealth desc",array('absolutePage'=>0,'pageSize'=>10));			
	$contents.= "\r\n\$savvy_board['userweekmoney'] = array(\r\n";
	$tmpContents='';
	$j=0;
	while ($arrlist=$db->fetch_array($usersql)){
		$userid=$arrlist['userid'];
		$username=strleach($arrlist['username']);
		$wealth=strleach($arrlist['wealth']);
		$position=strleach($arrlist['position']);
		$dateandtime=strleach($arrlist['dateandtime']);
		$j++;
		if( $tmpContents == '' ) {
			$tmpContents = '1';
		} else{
			$contents.= ",\r\n";
		}
		$contents .= <<<EOT
\t'{$j}' => array(
\t\t'userid' => '{$userid}',
\t\t'username' => '{$username}',
\t\t'wealth' => '{$wealth}',
\t\t'position' => '{$position}',
\t\t'dateandtime' => '{$dateandtime}'
\t)
EOT;
	}
	$db->free_result($usersql);
	if( empty($contents) ){
		return false;
	} else{
		$contents .= "\r\n";
	}
	return $contents;
}
?>