<?php
function edit_site()
{
	global $smarty;
	$site_title = post('site_title');
	$site_name = post('site_name');
	$site_domain = post('site_domain');
	$site_record = post('site_record');
	$site_record_url = post('site_record_url');
	$site_tech = post('site_tech');
	$site_tech_url = post('site_tech_url');
	$site_keywords = post('site_keywords');
	$site_description = cut_str(post('site_description'),250);
	$statistical_code = post('statistical_code','loose');
	$share_code = post('share_code','loose');
	
	$obj = new varia();
	$obj->edit_var_value('site_title',$site_title);
	$obj->edit_var_value('site_name',$site_name);
	$obj->edit_var_value('site_domain',$site_domain);
	$obj->edit_var_value('site_record',$site_record);
	$obj->edit_var_value('site_record_url',$site_record_url);
	$obj->edit_var_value('site_tech',$site_tech);
	$obj->edit_var_value('site_tech_url',$site_tech_url);
	$obj->edit_var_value('site_keywords',$site_keywords);
	$obj->edit_var_value('site_description',$site_description);
	$obj->edit_var_value('statistical_code',$statistical_code,true);
	$obj->edit_var_value('share_code',$share_code,true);
	
	$smarty->assign('info_text','修改网站设置成功');
	$smarty->assign('link_text','返回上一页');
	$smarty->assign('link_href',url(array('channel'=>'site','mod'=>'site_set')));
}
function edit_nav()
{
	global $smarty;
	for($i = 0; $i < count($_POST['id']); $i ++)
	{
		$word = strict($_POST['word'][$i]);
		$link = strict($_POST['link'][$i]);
		$id = strict($_POST['id'][$i]);
		$obj = new menu();
		$obj->set_value('men_name',$word);
		$obj->set_value('men_url',$link);
		$obj->set_where("men_id = $id");
		$obj->edit();
	}
	$smarty->assign('info_text','修改导航成功');
	$smarty->assign('link_text','返回上一页');
	$smarty->assign('link_href',url(array('channel'=>'site','mod'=>'nav_list')));
}
function add_nav()
{
	global $smarty;
	$type = post('type');
	$word = post('word');
	$link = post('link');
	$obj = new menu();
	$obj->set_value('men_type',$type);
	$obj->set_value('men_name',$word);
	$obj->set_value('men_url',$link);
	$obj->set_value('men_lang',S_LANG);
	$obj->add();
	$smarty->assign('info_text','添加导航成功');
	$smarty->assign('link_text','返回列表页');
	$smarty->assign('link_href',url(array('channel'=>'site','mod'=>'nav_list')));
}
function edit_title()
{
	global $smarty;
	for($i = 0; $i < count($_POST['id']); $i ++)
	{
		$site = strict($_POST['site'][$i]);
		$title = strict($_POST['title'][$i]);
		$priority = strict($_POST['priority'][$i]);
		$id = strict($_POST['id'][$i]);
		$val = $site.'{v}'.$title.'{v}'.intval($priority);
		$obj = new varia();
		$obj->set_value('var_value',$val);
		$obj->set_where("var_id = $id");
		$obj->edit();
	}
	$smarty->assign('info_text','编辑标题成功');
	$smarty->assign('link_text','返回列表页');
	$smarty->assign('link_href','?/site/mod-title_list/');
}
function add_title()
{
	global $smarty;
	$site = post('site');
	$title = post('title');
	$priority = post('priority');
	$val = $site.'{v}'.$title.'{v}'.intval($priority);
	$obj = new varia();
	$obj->set_value('var_name','title');
	$obj->set_value('var_value',$val);
	$obj->set_value('var_lang',S_LANG);
	$obj->add();
	$smarty->assign('info_text','添加标题成功');
	$smarty->assign('link_text','返回列表页');
	$smarty->assign('link_href','?/site/mod-title_list/');
}
function edit_show()
{
	$id = post('id');
	$arr = explode('-',$id);
	$xml_path = S_TPL_PATH.'index/xml/'.$arr[0].'.xml';
	$xml = new domdocument();
	$xml->load($xml_path);
	$tag = $xml->getelementsbytagname('module');
	$len = $tag->length;
	for($i = 0; $i < $len; $i ++)
	{
		$node_name = $tag->item($i)->nodeName;
		$node_text = str_replace('*','',$tag->item($i)->nodeValue);
		if($node_text == $arr[1])
		{
			if(intval($arr[2]))
			{
				$tag->item($i)->nodeValue = $node_text;
				edit_tpl_show($arr[0],$arr[1],1);
			}else{
				$tag->item($i)->nodeValue = '*'.$node_text;
				edit_tpl_show($arr[0],$arr[1],0);
			}
		}
	}
	file_put_contents($xml_path,$xml->saveXML());
	echo 1;
}
function edit_tpl_show($page_name,$node_text,$val)
{
	$path = S_TPL_PATH.'index/'.$page_name.'.php';
	$file = file_get_contents($path);
	if($val)
	{
		$file = str_replace('{*include file="module/'.$node_text.'.php"*}','{include file="module/'.$node_text.'.php"}',$file);
	}else{
		$file = str_replace('{include file="module/'.$node_text.'.php"}','{*include file="module/'.$node_text.'.php"*}',$file);
	}
	file_put_contents($path,$file);
}
function admin_power()
{
	global $global,$smarty;
	$adm_id = post('adm_id');
	$obj = new admin();
	$obj->set_where('adm_id = '.$global['admin_id']);
	$a = $obj->get_one();
	$obj->set_where('');
	$obj->set_where("adm_id = $adm_id");
	$b = $obj->get_one();
	$success = 0;
	if($a['adm_grade'] < $b['adm_grade'])
	{
		$adm_power = '';
		for($i = 0; $i < count($_POST['url']); $i ++)
		{
			$adm_power .= strict($_POST['url'][$i]) . '|';
		}
		$adm_power = substr($adm_power,0,-1);
		$obj->set_value('adm_power',$adm_power);
		$obj->set_where('');
		$obj->set_where("adm_id = $adm_id");
		$obj->edit();
		$success = 1;
	}
	if($success)
	{
		$info_text = '修改权限成功';
		$link_text = '返回列表页';
		$link_href = url(array('channel'=>'site','mod'=>'admin_list'));
	}else{
		$info_text = '修改权限失败';
		$link_text = '返回上一页';
		$link_href = url(array('channel'=>'site','mod'=>'admin_power'));
	}
	$smarty->assign('info_text',$info_text);
	$smarty->assign('link_text',$link_text);
	$smarty->assign('link_href',$link_href);
}
function check_admin_name()
{
	$name = post('name');
	$obj = new admin();
	$obj->set_where("adm_username = '$name'");
	if($obj->get_count())
	{
		echo 1;
	}else{
		echo 0;
	}
}
function add_admin()
{
	global $global,$smarty;
	$adm_username = post('adm_username');
	$adm_password = post('adm_password');
	$re_password = post('re_password');	
	$obj = new admin();
	$obj->set_where('adm_id = '.$global['admin_id']);
	$one = $obj->get_one();
	$adm_grade = $one['adm_grade'] + 1;
	$obj->set_where('');
	$obj->set_where("adm_username = '$adm_username'");
	if($obj->get_count() == 0 && strlen($adm_username) >= 5 && strlen($adm_password) >= 5 && $adm_password == $re_password)
	{
		$obj->set_value('adm_username',$adm_username);
		$obj->set_value('adm_password',md5($adm_password));
		$obj->set_value('adm_grade',$adm_grade);
		$obj->add();
		$info_text = '添加管理员帐号成功';
		$link_text = '返回列表页';
		$link_href = url(array('channel'=>'site','mod'=>'admin_list'));
	}else{
		$info_text = '添加管理员帐号失败';
		$link_text = '返回上一页';
		$link_href = url(array('channel'=>'site','mod'=>'admin_add'));
	}
	$smarty->assign('info_text',$info_text);
	$smarty->assign('link_text',$link_text);
	$smarty->assign('link_href',$link_href);
}
function edit_admin()
{
	global $global,$smarty;
	$adm_id = post('adm_id');
	$adm_password = post('adm_password');
	$re_password = post('re_password');	
	$obj = new admin();
	$obj->set_where('adm_id = '.$global['admin_id']);
	$a = $obj->get_one();
	$obj->set_where('');
	$obj->set_where("adm_id = $adm_id");
	$b = $obj->get_one();
	$success = 0;
	if($obj->get_count())
	{
		if($a['adm_id'] == $b['adm_id'] || $a['adm_grade'] < $b['adm_grade'])
		{
			if(strlen($adm_password) >= 5 && $adm_password == $re_password)
			{
				$obj->set_value('adm_password',md5($adm_password));
				$obj->edit();
				$success = 1;
			}
		}
	}
	if($success)
	{
		$info_text = '修改密码成功';
		$link_text = '返回列表页';
		$link_href = url(array('channel'=>'site','mod'=>'admin_list'));
	}else{
		$info_text = '修改密码失败';
		$link_text = '返回上一页';
		$link_href = url(array('channel'=>'site','mod'=>'admin_edit'));
	}
	$smarty->assign('info_text',$info_text);
	$smarty->assign('link_text',$link_text);
	$smarty->assign('link_href',$link_href);
}
function del_admin()
{
	global $global;
	$adm_id = post('id');
	$obj = new admin();
	$obj->set_where('adm_id = '.$global['admin_id']);
	$a = $obj->get_one();
	$obj->set_where('');
	$obj->set_where("adm_id = $adm_id");
	$b = $obj->get_one();
	if($obj->get_count())
	{
		if($a['adm_grade'] < $b['adm_grade'])
		{
			$obj->del();
			set_cookie('result',1);
		}
	}
	echo 1;
}
function add_or_edit_link()
{
	global $smarty;
	$lin_id = post('lin_id');
	$lin_word = post('lin_word');
	$lin_url = post('lin_url');
	$lin_img = post('lin_img');
	$lin_title = post('lin_title');
	if(str_replace('http://','',$lin_img) == '')
	{
		$lin_img = 'none';
	}
	$obj = new link();
	$obj->set_value('lin_word',$lin_word);
	$obj->set_value('lin_url',$lin_url);
	$obj->set_value('lin_img',$lin_img);
	$obj->set_value('lin_title',$lin_title);
	if($lin_id != '')
	{
		$obj->set_where("lin_id = $lin_id");
		$obj->edit();
		$info_text = '修改友情链接成功';
	}else{
		$obj->set_value('lin_lang',S_LANG);
		$obj->add();
		$info_text = '添加友情链接成功';
	}
	$smarty->assign('info_text',$info_text);
	$smarty->assign('link_text','返回列表页');
	$smarty->assign('link_href',url(array('channel'=>'site','mod'=>'link_list')));
}
function set_hot_search()
{
	$val = post('val');
	$obj = new varia();
	$obj->edit_var_value('hot_search',$val);
	echo 1;
}
function set_session_method()
{
	$val = intval(post('val'));
	edit_config('S_SESSION',S_SESSION,$val);
	echo 1;
}
function clear_cache()
{
	$dir = 'admin/compile';
	$list = scandir($dir);
	foreach($list as $file)
	{
		if($file != '.' && $file != '..' && !is_dir($dir.'/'.$file))
		{
			unlink($dir.'/'.$file);
		}
	}
	$dir = 'index/compile';
	$list = scandir($dir);
	foreach($list as $file)
	{
		if($file != '.' && $file != '..' && !is_dir($dir.'/'.$file))
		{
			unlink($dir.'/'.$file);
		}
	}
	echo 1;
}
function save_sentmail()
{
	global $smarty;
	$smtp = post('smtp');
	$send = post('send');
	$password = post('password');
	$receive = post('receive');
	$obj = new varia();
	$obj->edit_var_value('sentmail_smtp',$smtp);
	$obj->edit_var_value('sentmail_send',$send);
	$obj->edit_var_value('sentmail_password',$password);
	$obj->edit_var_value('sentmail_receive',$receive);
	
	$smarty->assign('info_text','修改邮件通知设置成功');
	$smarty->assign('link_text','返回上一页');
	$smarty->assign('link_href',url(array('channel'=>'site','mod'=>'other')));
}
	
//红头船
?>