<?php

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

	$RCSfile: qihoo.inc.php,v $
	$Revision: 1.31.2.2 $
	$Date: 2007/05/08 15:12:33 $
*/

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

cpheader();

if($action == 'qihoo_config') {

	if(!submitcheck('qihoosubmit')) {

		$settings = $checks = array();
		$query = $db->query("SELECT variable, value FROM {$tablepre}settings
				WHERE variable IN ('qihoo_status', 'qihoo_searchbox', 'qihoo_summary', 'qihoo_keywords', 'qihoo_relatedthreads', 'qihoo_validity', 'qihoo_maxtopics', 'qihoo_adminemail', 'qihoo_jammer', 'qihoo_location', 'qihoo_relatedsort')");
		while($setting = $db->fetch_array($query)) {
			$settings[$setting['variable']] = $setting['value'];
		}
		$checkstatus = array($settings['qihoo_status'] => 'checked');
		$checklocation = array($settings['qihoo_location'] => 'checked');
		$checkrelatedsort = array($settings['qihoo_relatedsort'] => 'checked');
		$settings['qihoo_searchbox'] = sprintf('%03b', $settings['qihoo_searchbox']);
		for($i = 1; $i <= 3; $i++) {
			$checks[$i] = $settings['qihoo_searchbox'][3 - $i] ? 'checked' : '';
		}

		shownav('qihoo_settings_basic');
		showtips('qihoo_tips');

?>
<form method="post" name="settings" action="admincp.php?action=qihoo_config">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<?

		showtype('qihoo','top');
		showsetting('qihoo_status', '', '', '<input class="radio" type="radio" name="settingsnew[qihoo_status]" value="1" '.$checkstatus[1].'> '.$lang['qihoo_status_enable'].'<br><input class="radio" type="radio" name="settingsnew[qihoo_status]" value="2" '.$checkstatus[2].'> '.$lang['qihoo_status_enable_default'].'<br><input class="radio" type="radio" name="settingsnew[qihoo_status]" value="0" '.$checkstatus[0].'> '.$lang['qihoo_status_disable']);
		showsetting('qihoo_searchbox', '', '', '<input class="checkbox" type="checkbox" name="settingsnew[qihoo_searchbox][1]" value="1" '.$checks[1].'> '.$lang['qihoo_searchbox_index'].'<br><input class="checkbox" type="checkbox" name="settingsnew[qihoo_searchbox][2]" value="1" '.$checks[2].'> '.$lang['qihoo_searchbox_forumdisplay'].'<br><input class="checkbox" type="checkbox" name="settingsnew[qihoo_searchbox][3]" value="1" '.$checks[3].'> '.$lang['qihoo_searchbox_viewthread']);
		showsetting('qihoo_summary', 'settingsnew[qihoo_summary]', $settings['qihoo_summary'], 'radio');
		showsetting('qihoo_jammer_allow', 'settingsnew[qihoo_jammer]', $settings['qihoo_jammer'], 'radio');
		showsetting('qihoo_maxtopics', 'settingsnew[qihoo_maxtopics]', $settings['qihoo_maxtopics'], 'text');
		showsetting('qihoo_keywords', 'settingsnew[qihoo_keywords]', $settings['qihoo_keywords'], 'textarea');
		showsetting('qihoo_adminemail', 'settingsnew[qihoo_adminemail]', $settings['qihoo_adminemail'], 'text');
		showtype('', 'bottom');

		echo '<br><center><input class="button" type="submit" name="qihoosubmit" value="'.$lang['submit'].'"></form>';

	} else {

		$settingsnew['qihoo_searchbox'] = bindec(intval($settingsnew['qihoo_searchbox'][3]).intval($settingsnew['qihoo_searchbox'][2]).intval($settingsnew['qihoo_searchbox'][1]));
		$settingsnew['qihoo_validity'] = $settingsnew['qihoo_validity'] < 1 ? 1 : intval($settingsnew['qihoo_validity']);

		if($settingsnew['qihoo_status'] && $settingsnew['qihoo_adminemail']) {

			if(!isemail($settingsnew['qihoo_adminemail'])) {
				cpmsg('qihoo_adminemail_invalid');
			}

			$key = md5(site().'qihoo_discuz'.gmdate("Ymd", $timestamp));
			@fopen('http://search.qihoo.com/corp/discuz.html?site='.site().'&key='.$key.'&email='.$settingsnew['qihoo_adminemail'].'', 'r');
		}

		if(is_array($settingsnew)) {
			foreach($settingsnew as $variable => $value) {
				$value = $variable != 'qihoo_keywords' && $variable != 'qihoo_adminemail' ? intval($value) : $value;
				$db->query("UPDATE {$tablepre}settings SET value='$value' WHERE variable='$variable'");
			}
		}

		updatecache('settings');
		cpmsg('qihoo_succeed');

	}

} elseif($action == 'qihoo_relatedthreads') {

	if(!submitcheck('qihoosubmit')) {

		$settings = $checktype = array();
		$query = $db->query("SELECT value FROM {$tablepre}settings WHERE variable='qihoo_relatedthreads'");
		$settings = unserialize($db->result($query, 0));

		$checkposition = array($settings['position'] => 'checked');
		$checkrelatedsort = array($settings['order'] => 'checked');
		if($settings['type']) {
			foreach($settings['type'] as $type) {
				$checktype[$type] = 'checked';
			}
		}

		shownav('qihoo_settings_basic');
		showtips('qihoo_tips');

?>
<form method="post" name="settings" action="admincp.php?action=qihoo_relatedthreads">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<?

		showtype('qihoo_relatedthreads','top');
		showsetting('qihoo_relatedthreads', 'settingsnew[bbsnum]', $settings['bbsnum'], 'text');
		showsetting('qihoo_relatedthreads_web', 'settingsnew[webnum]', $settings['webnum'], 'text');
		showsetting('qihoo_relatedthreads_type', '', '', '<input class="checkbox" type="checkbox" name="settingsnew[type][blog]" value="blog" '.$checktype['blog'].'> '.$lang['qihoo_relatedthreads_type_blog'].'<br><input class="checkbox" type="checkbox" name="settingsnew[type][news]" value="news" '.$checktype['news'].'> '.$lang['qihoo_relatedthreads_type_news'].'<br><input class="checkbox" type="checkbox" name="settingsnew[type][bbs]" value="bbs" '.$checktype['bbs'].'> '.$lang['qihoo_relatedthreads_type_bbs']);
		showsetting('qihoo_relatedthreads_banurl', 'settingsnew[banurl]', $settings['banurl'], 'textarea');
		showsetting('qihoo_relatedthreads_position', '', '', '<input class="radio" type="radio" name="settingsnew[position]" value="0" '.$checkposition[0].'> '.$lang['qihoo_relatedthreads_position_mode_top'].'<br><input class="radio" type="radio" name="settingsnew[position]" value="1" '.$checkposition[1].'> '.$lang['qihoo_relatedthreads_position_mode_under']);
		showsetting('qihoo_relatedthreads_order', '', '', '<input class="radio" type="radio" name="settingsnew[order]" value="0" '.$checkrelatedsort[0].'> '.$lang['qihoo_relatedthreads_order_mode_realated'].'<br><input class="radio" type="radio" name="settingsnew[order]" value="1" '.$checkrelatedsort[1].'> '.$lang['qihoo_relatedthreads_order_mode_posttime'].'<br><input class="radio" type="radio" name="settingsnew[order]" value="2" '.$checkrelatedsort[2].'> '.$lang['qihoo_relatedthreads_order_mode_lastpost']);
		showsetting('qihoo_relatedthreads_validity', 'settingsnew[validity]', $settings['validity'], 'text');

		showtype('', 'bottom');

		echo '<br><center><input class="button" type="submit" name="qihoosubmit" value="'.$lang['submit'].'"></form>';

	} else {
		foreach($settingsnew as $type => $value) {
			if(in_array($type, array('bbsnum', 'webnum', 'position', 'order', 'validity'))) {
				$settingsnew['qihoo_relatedthreads'][$type] = intval($value);
			}
		}

		$settingsnew = serialize($settingsnew);
		$db->query("UPDATE {$tablepre}settings SET value='$settingsnew' WHERE variable='qihoo_relatedthreads'");

		updatecache('settings');
		cpmsg('qihoo_succeed');

	}

} elseif($action == 'qihoo_topics') {

	if(!submitcheck('topicsubmit')) {
		$topics = '';
		$query = $db->query("SELECT value FROM {$tablepre}settings WHERE variable='qihoo_topics'");
		$setting = $db->fetch_array($query);
		if(is_array($setting['qihoo_topics'] = unserialize($setting['value']))) {
			foreach($setting['qihoo_topics'] as $key => $value) {
				$checkstype = array($value['stype'] => 'selected="selected"');
				$checkrelate = array($value['relate'] => 'selected="selected"');

				$topics .= "<tr align=\"center\">\n".
					"<td class=\"altbg1\"><input class=\"checkbox\" type=\"checkbox\" name=\"delete[$key]\" value=\"".$value['topic']."\"></td>\n".
					"<td class=\"altbg2\"><input type=\"text\" size=\"20\" name=\"settingsnew[qihoo_topics][$key][topic]\" id=\"settingsnew[qihoo_topics][$key][topic]\" value=\"$value[topic]\"></td>\n".
					"<td class=\"altbg1\"><input type=\"text\" size=\"30\" name=\"settingsnew[qihoo_topics][$key][keyword]\" id=\"settingsnew[qihoo_topics][$key][keyword]\" value=\"$value[keyword]\"></td>\n".
					"<td class=\"altbg2\"><input type=\"text\" size=\"10\" name=\"settingsnew[qihoo_topics][$key][length]\" id=\"settingsnew[qihoo_topics][$key][length]\" value=\"$value[length]\"></td>\n".
					"<td class=\"altbg1\"><select name=\"settingsnew[qihoo_topics][$key][stype]\" id=\"settingsnew[qihoo_topics][$key][stype]\"><option value=\"0\" $checkstype[0]>$lang[qihoo_topics_type_fulltext]</option><option value=\"title\" $checkstype[title]>$lang[qihoo_topics_type_title]</option></select></td>\n".
					"<td class=\"altbg2\"><select name=\"settingsnew[qihoo_topics][$key][relate]\" id=\"settingsnew[qihoo_topics][$key][relate]\"><option value=\"score\" $checkrelate[score]>$lang[qihoo_topics_orderby_relation]</option><option value=\"pdate\" $checkrelate[pdate]>$lang[qihoo_topics_orderby_dateline]</option><option value=\"rdate\" $checkrelate[rdate]>$lang[qihoo_topics_orderby_lastpost]</option></select></tr>\n".
					"<td class=\"altbg1\"><a href=\"###\" onClick=\"window.open('topic.php?topic='+$('settingsnew[qihoo_topics][$key][topic]').value+'&keyword='+$('settingsnew[qihoo_topics][$key][keyword]').value+'&stype='+$('settingsnew[qihoo_topics][$key][stype]').value+'&length='+$('settingsnew[qihoo_topics][$key][length]').value+'&relate='+$('settingsnew[qihoo_topics][$key][relate]').value+'');\">[$lang[preview]]</a></tr>\n";
			}
		}
		shownav('qihoo_settings_special');
		showtips('qihoo_topics_tips');

?>
<form method="post" action="admincp.php?action=qihoo_topics">
<input type="hidden" name="formhash" value="<?=FORMHASH?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="tableborder">
<tr class="header">
<td><input class="checkbox" type="checkbox" name="chkall" class="header" onclick="checkall(this.form)"><?=$lang['del']?></td>
<td><?=$lang['qihoo_topics_name']?></td><td><?=$lang['qihoo_topics_keywords']?></td><td><?=$lang['qihoo_topics_length']?></td><td><?=$lang['qihoo_topics_type']?></td><td><?=$lang['qihoo_topics_orderby']?></td><td><?=$lang['preview']?></td></tr>
<?=$topics?>
<tr class="altbg1" align="center">
<td><?=$lang['add_new']?></td>
<td><input type="text" size="20" name="newtopic" id="newtopic"></td>
<td><input type="text" size="30" name="newkeyword" id="newkeyword"></td>
<td><input type="text" size="10" name="newlength" id="newlength" value="0"></td>
<td><select name="newstype" id="newstype"><option value="0" selected><?=$lang['qihoo_topics_type_fulltext']?></option><option value="1"><?=$lang['qihoo_topics_type_title']?></option></select></td>
<td><select name="newrelate" id="newrelate"><option value="score"><?=$lang['qihoo_topics_orderby_relation']?></option><option value="pdate"><?=$lang['qihoo_topics_orderby_dateline']?></option><option value="rdate"><?=$lang['qihoo_topics_orderby_lastpost']?></option></select></td>
<td><a href="###" onClick="window.open('topic.php?topic='+$('newtopic').value+'&keyword='+$('newkeyword').value+'&stype='+$('newstype').value+'&length='+$('newlength').value+'&relate='+$('newrelate').value+'');">[<?=$lang['preview']?>]</a></td>
</tr></table><br>
<center><input class="button" type="submit" name="topicsubmit" value="<?=$lang['submit']?>"></center></form></td></tr>
<?

	} else {

		if(is_array($settingsnew['qihoo_topics'])) {
			foreach($settingsnew['qihoo_topics'] as $key => $value) {
				if($delete[$key]) {
					unset($topicarray[$key]);
				} else {
					$topicarray[$key] = array
						(
						'topic'		=> dhtmlspecialchars(stripslashes($value['topic'])),
						'keyword'	=> $value['keyword'] = trim($value['keyword']) ? dhtmlspecialchars(stripslashes($value['keyword'])) : $value['topic'],
						'length'	=> intval($value['length']),
						'stype'		=> $value['stype'],
						'relate'	=> $value['relate']
						);
				}
			}
		} else {
			$topicarray = array();
		}

		if($newtopic) {
			$topicarray[] = array
				(
				'topic'		=> dhtmlspecialchars(stripslashes($newtopic)),
				'keyword'	=> $newkeyword = trim($newkeyword) ? dhtmlspecialchars(stripslashes($newkeyword)) : $newtopic,
				'length'	=> intval($newlength),
				'stype'		=> $newstype > 1 ? 1 : intval($newstype),
				'relate'	=> $newrelate
				);
		}

		$settingsnew['qihoo_topics'] = addslashes(serialize($topicarray));
		$db->query("UPDATE {$tablepre}settings SET value='$settingsnew[qihoo_topics]' WHERE variable='qihoo_topics'");
		updatecache('settings');
		cpmsg('qihoo_topics_succeed', 'admincp.php?action=qihoo_topics');

	}

}

?>