<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();

/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：QQ877873666
 邮箱：877873666@qq.com

 **** 版权所有 **** */


//打开用户表，并检测用户是否登录
$MB->Open('','login',2);

$skin->WebTop();


	$tplPcDir='../template/';
	$tplWapDir='../wap/template/';


switch($mudi){
	case 'add':
		$menuFileID = 207;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		AddOrRev();
		break;

	case 'rev':
		$menuFileID = 208;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		AddOrRev();
		break;

	case 'del':
		$menuFileID = 209;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		del();
		break;

	case 'addFile': case 'revFile':
		AddOrRevFile();
		break;

	case 'delFile':
		$menuFileID = 209;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		DelFile();
		break;

	case 'change':
		$menuFileID = 208;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		change();
		break;

	default:
		die('err');
}

$skin->WebBottom();

$MB->Close();
$DB->Close();





// 修改
function AddOrRev(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataMode		= OT::PostStr('dataMode');
	$dataModeStr	= OT::PostStr('dataModeStr');
	$dataID			= OT::PostInt('dataID');

	$type		= OT::PostStr('type');
	$theme		= OT::PostStr('theme');
	$dir		= OT::PostStr('dir');
	$rank		= OT::PostInt('rank');
	$state		= OT::PostInt('state');

	if (strpos($dir,'..') !== false){
		JS::AlertBackEnd('禁止目录名中出现“..”字符串');
	}

	$record = array();
	$record['TP_type']	= $type;
	$record['TP_theme']	= $theme;
	$record['TP_dir']	= $dir;
	$record['TP_rank']	= $rank;
	$record['TP_state']	= $state;

	if ($mudi == 'add'){
		$alertMode = '添加';
		$judRes = $DB->InsertParam('template',$record);
	}else{
		$alertMode = '修改';
		$judRes = $DB->UpdateParam('template',$record,'TP_ID='. $dataID);
	}
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'title'	=> '模板',
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】'. $alertMode . $alertRes .'！',
		));

	JS::AlertHrefEnd($alertMode . $alertRes,'template.php?mudi=manage&sel='. $type .'&dataType=&dataTypeCN='. urlencode('模板') .'');

}



// 添加、修改文件
function AddOrRevFile(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN,$tplPcDir,$tplWapDir;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataMode		= OT::PostStr('dataMode');
	$dataModeStr	= OT::PostStr('dataModeStr');

	$sel			= OT::GetStr('sel');
	$isBak			= OT::PostInt('isBak');
	$isEmptyRow		= OT::PostInt('isEmptyRow');
	$isNoEmptyRow	= OT::PostInt('isNoEmptyRow');
	$filePath		= OT::PostStr('filePath');
	$fileName		= OT::PostStr('fileName');
	$fileContent	= str_replace(array('%'. chr(62), chr(60) .'%', '?'. chr(62), chr(60) .'?'), array('%&gt;', '&lt;%', '?&gt;', '&lt;?'), OT::PostStr('fileContent'));

	if ($filePath=='' || $fileName=='' || $fileContent==''){
		JS::AlertBackEnd('表单内容接收不全');
	}
	if (strpos($filePath,'..') !== false){
		JS::AlertBackEnd('禁止查询路径中出现“..”字符串');
	}

	if ($sel == 'wap'){
		$templateDir = $tplWapDir;
	}else{
		$templateDir = $tplPcDir;
	}

	$filePath = $templateDir . $filePath;
//	$fileName	= Mid(filePath,InstrRev(filePath,'/')+1)
	if (! File::IsExists($filePath)){
		JS::AlertBackEnd('找不到该文件，无法修改');
	}
	if ($fileName != Str::Filter($fileName,'fileName')){
		JS::AlertBackEnd('文件名除了“空格”“.”“-”“_”外不允许出现其他特殊符号');
	}
	$revExtStr = 'htm|html|shtm|shtml|css|js|txt|xml|wml';
	$fileExt = File::GetExt($filePath);
	if (strpos('|'. $revExtStr .'|','|'. $fileExt .'|') === false){
		JS::AlertBackEnd('当前仅允许修改 '. $revExtStr .' 这些格式('. $fileExt .')。');
	}

	if ($mudi == 'add'){
		$menuFileID = 207;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		$alertMode = '新增';
	}else{
		$menuFileID = 208;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		$alertMode = '修改';
		if ($isBak == 1){
			File::Copy($filePath,substr($filePath,0,(strlen($fileExt)+1)*(-1)) .'_bak'. TimeDate::Get('datetimeStr') .'.'. $fileExt);
		}
	}

	if ($isNoEmptyRow == 1){ $fileContent = str_replace(PHP_EOL . PHP_EOL, PHP_EOL, $fileContent); }
	elseif ($isEmptyRow == 1){ $fileContent = str_replace(PHP_EOL . PHP_EOL . PHP_EOL, PHP_EOL . PHP_EOL, $fileContent); }
	$judRes = File::Write($filePath,$fileContent);
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'title'	=> '文件名',
		'theme'	=> $fileName,
		'note'	=> '【'. $dataTypeCN .'】'. $alertMode . $alertRes .'！',
		));

	JS::AlertHrefEnd($alertMode . $alertRes,$backURL);

}



// 删除文件
function DelFile(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN,$tplPcDir,$tplWapDir;

	$num		= OT::GetInt('num');
	$sel		= OT::GetStr('sel');
	$filePath	= OT::GetStr('filePath');

	if (strpos(substr($filePath,3),'..') !== false){
		JS::AlertBackEnd('禁止查询路径中出现“..”字符串');
	}

	if ($sel == 'wap'){
		$templateDir = $tplWapDir;
	}else{
		$templateDir = $tplPcDir;
	}

	$judRes = File::Del($templateDir . $filePath);
		if ($judRes){
			$alertRes = '成功';
			echo('
			<script language="javascript" type="text/javascript">
			parent.$id("data'. $num .'").style.display="none";
			</script>
			');
		}else{
			$alertRes = '失败';
			JS::AlertEnd('删除失败（'. $templateDir . $filePath .'）');
		}

	Adm::AddLog(array(
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】删除'. $alertRes .'！',
		));

}



// 删除模板
function del(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN;

	$dataID	= OT::GetInt2( OT::ParaArr('id') );
	$theme	= OT::GetStr('theme');

	if ($dataID <= 0){
		JS::AlertEnd('指定ID错误！');
	}

	$judRes = $DB->query('delete from '. OT_dbPref .'template where TP_ID='. $dataID);
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】删除'. $alertRes .'！',
		));

	JS::ParentHiddenEnd('data'. $dataID);
}



// 更改模板
function change(){
	global $DB,$MB,$skin,$mudi,$dataType,$dataTypeCN;

	$dataID		= OT::GetInt2( OT::ParaArr('id') );
	$backURL	= OT::GetStr('backURL');
	$sel		= OT::GetStr('sel');

	if ($dataID <= 0){
		JS::AlertBackEnd('指定ID错误！');
	}

	$dataexe = $DB->query('select * from '. OT_dbPref .'template where TP_ID='. $dataID .' and TP_type='. $DB->ForStr($sel));
		if (! $row = $dataexe->fetch()){
			JS::AlertBackEnd('找不到相关模板信息！');
		}
		$TP_theme	= $row['TP_theme'];
		$TP_dir		= $row['TP_dir'];
	unset($dataexe);

	$alertStr = '';
	if ($sel == 'wap'){
		$selCN = '手机端';
		$judRes = $DB->query('update '. OT_dbPref .'wap set WAP_templateDir='. $DB->ForStr($TP_dir) .'');
			if ($judRes){
				$alertRes = '成功';
			}else{
				$alertRes = '失败';
			}

		$Cache = new Cache();
		$judRes = $Cache->Php('wap');
		if (! $judRes){ $alertStr = '\n../cache/php/wap.php 生成失败，请检查该目录或者文件是否有写入/修改权限！'; }
	}else{
		$selCN = '电脑端';
		$judRes = $DB->query('update '. OT_dbPref .'system set SYS_templateDir='. $DB->ForStr($TP_dir) .'');
			if ($judRes){
				$alertRes = '成功';
			}else{
				$alertRes = '失败';
			}

		$Cache = new Cache();
		$judRes = $Cache->Php('system');
		if (! $judRes){ $alertStr = '\n../cache/php/system.php 生成失败，请检查该目录或者文件是否有写入/修改权限！'; }
	}

	Adm::AddLog(array(
		'theme'	=> $TP_theme,
		'note'	=> '【'. $dataTypeCN .'】更换应用'. $selCN .'模板'. $alertRes .'！',
		));

	JS::AlertHref('更换应用'. $selCN .'模板'. $alertRes .'\n'. $alertStr, $backURL);

}

?>