<?php
/**
* @package phpBB-WAP
* @copyright (c) phpBB Group
* @Оптимизация под WAP: Гутник Игорь ( чел ).
* @简体中文：中文phpBB-WAP团队
* @license http://opensource.org/licenses/gpl-license.php
**/

/**
* 这是一款自由软件, 您可以在 Free Software Foundation 发布的
* GNU General Public License 的条款下重新发布或修改; 您可以
* 选择目前 version 2 这个版本（亦可以选择任何更新的版本，由
* 你喜欢）作为新的牌照.
**/

if ( !defined('IN_PHPBB') )
{
	die("Hacking attempt");
}

if( $is_rules_auth )
{
	$rule_id = ( isset($_GET['r']) ) ? abs(intval($_GET['r'])) : 0;
	$subj = ( isset($_POST['subject']) ) ? htmlspecialchars(trim($_POST['subject'])) : '';
	$name = ( isset($_POST['name']) ) ? htmlspecialchars(trim($_POST['name'])) : '';
	$moder = ( isset($_POST['moder']) ) ? ( ( $_POST['moder'] == 1 ) ? 1 : 0 ) : 0;

	$rule_base_id = '';
	
	if ( $rule_id )
	{
		$sql = "SELECT * FROM " . RULES_TABLE . " WHERE rule_id = $rule_id";
		if( !($result = $db->sql_query($sql)) )
		{
			trigger_error('Could not obtain rules information.', E_USER_WARNING);
		}
		$r = $db->sql_fetchrow($result);
		$rn = $r['rule_name'];
		$rs = $r['rule_subj'];
		$cr = $r['rule_cat_id'];
		$rm = $r['rule_moder'];
		$rule_base_id = $r['rule_id'];
		$rule_cat_id = ( $cr ) ? $cr : $rule_cat_id;
	}

	if($rule_cat_id)
	{
		$sql = "SELECT * FROM " . RULES_CAT_TABLE . " WHERE cat_r_id = $rule_cat_id";
		if( !($result = $db->sql_query($sql)) )
		{
			trigger_error('Could not obtain rules information.', E_USER_WARNING);
		}
		$cid = $db->sql_fetchrow($result);
		$cid_name = $cid['cat_r_name'];
		$cid = $cid['cat_r_id'];
	}
	else
	{
		$cid = '';
	}

	if( $cid != '' && $rule_base_id == '' && $subj != '' && $name != '' )
	{
		$sql = "SELECT MAX(rule_id) AS max_id FROM " . RULES_TABLE;
		if(!($result = $db->sql_query($sql)))
		{
			trigger_error('Error rules table!', E_USER_WARNING);
		}
		$result = $db->sql_fetchrow($result);
		$new_id = $result['max_id'] + 1;

		$sql = "INSERT INTO " . RULES_TABLE . " (rule_id, rule_cat_id, rule_name, rule_subj, rule_moder)
				VALUES ($new_id, $cid, '" . str_replace("\'", "''", $name) . "', '" . str_replace("\'", "''", $subj) . "', '" . $moder . "')";
		if(!($result=$db->sql_query($sql)))
		{
			trigger_error('Error rules table!', E_USER_WARNING);
		}
		redirect(append_sid("rules.php?crid=$cid", 1));
	}
	elseif ( $cid != '' && $rule_base_id && $subj != '' && $name != '' )
	{
		$sql = "UPDATE " . RULES_TABLE . "
			SET rule_name = '" . str_replace("\'", "''", $name) . "', rule_subj = '" . str_replace("\'", "''", $subj) . "', rule_moder = '" . $moder . "'
			WHERE rule_id = $rule_base_id";
		if(!($result=$db->sql_query($sql)))
		{
			trigger_error('db error !!!', E_USER_WARNING);
		}
		redirect(append_sid("rules.php?crid=$cid", 1));
	}
	elseif ( $cid != '' )
	{
		if ( $mode == 'editrule' )
		{
			$page_title = '编辑规则';
			$s_rules_action = append_sid("rules.php?mode=editrule&amp;r=$rule_base_id");
			$name = $rn;
			$text = $rs;
			$moder = ( $rm == 0 ) ? '' : ' checked="checked"';
			$template->assign_block_vars('r_edit', array());
			$title = '编辑';
		}
		else
		{
			$page_title = '新增规则';
			$s_rules_action = append_sid("rules.php?mode=addrule&amp;crid=$cid");
			$moder = ' checked="checked"';
			$name = '';
			$text = '';
			$template->assign_block_vars('r_add', array());
			$title = '添加';
		}

		page_header($page_title);

		$template->set_filenames(array(
			'body' => 'rules/rule_edit.tpl')
		);

		$template->assign_vars(array(
			'TITLE' => $title,
			'NAME' => $name,
			'TEXT' => $text,
			'MODER' => $moder,
			'U_CAT_RULES' => append_sid("rules.php?crid=$cid"),
			'CAT_NAME'	=> $cid_name, 
			'S_RULES_ACTION' => $s_rules_action)
		);

		$template->pparse('body');

		page_footer();
	}
	else
	{
		redirect(append_sid("rules.php", 1));
	}
}
else
{
	redirect(append_sid("rules.php", 1));
}
?>