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

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

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

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



if ($mudi != 'auth'){
	//用户检测
	$MB->Open('','login');
}


switch ($mudi){
	case 'getInfo':
		$menuFileID = 171;
//		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		GetInfo();
		break;

	case 'getAuth':
		GetAuth();
		break;

	case 'auth':
		AuthDeal();
		break;

	case 'pluginFileDel':
		PluginFileDel();
		break;

	default:
		die('err');
}

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





// 获取已订购插件信息
function GetInfo(){
	global $DB,$sysAdminArr;

	$mode		= OT::GetStr('mode');
	$backURL	= OT::GetStr('backURL');
//		if (empty($backURL)){
			$backURL = 'appShop.php?dataMode=buy';
//		}
	$beforeURL	= GetUrl::CurrDir(1);

	$packCount = $appCount = 0;
	$updateUrl = SysDiff::UpdateUrl($sysAdminArr['SA_updateUrlMode']);
	$retStr = ReqUrl::UseAuto($sysAdminArr['SA_getUrlMode'], 'GET', $updateUrl . OT_SOFTTYPE .'AppInfo.php?mudi=getInfo&OT_UPDATETIME='. OT_UPDATETIME .'&OT_VERSION='. OT_VERSION .'&dataVer='. OT_UPDATEVER .'&OT_URL='. urlencode($beforeURL) .'&username='. $sysAdminArr['SA_username'] .'&softID='. $sysAdminArr['SA_softID'] .'&softCode='. $sysAdminArr['SA_softCode'] .'&domainID='. $sysAdminArr['SA_domainID'] .'&domainCode='. $sysAdminArr['SA_domainCode'], 'UTF-8', array(), 'note');
	if (strpos($retStr,'[true]') === false){
		$checkAuditNum = $DB->GetOne('select SA_checkAuditNum from '. OT_dbPref .'sysAdmin');
		if ($checkAuditNum >= 10 || strpos($retStr, '该域名下没有订购任何插件') !== false){
			$DB->Delete('paySoft');
		}else{
			$DB->query('update '. OT_dbPref .'sysAdmin set SA_checkAuditNum=SA_checkAuditNum+1 where SA_ID=1');
		}
		if ($mode != 'no'){ JS::AlertHrefEnd(str_replace('"','\"',$retStr), $backURL); }
	}else{
		$DB->query('update '. OT_dbPref .'sysAdmin set SA_checkAuditNum=0 where SA_ID=1');

		$packCount = Str::GetMark($retStr,'<appPackCount>','</appPackCount>');
		$packStr = Str::GetMark($retStr,'<appPackList>','</appPackList>');
		$packArr = unserialize(@$packStr);
		if (! is_array($packArr)){
			if ($mode != 'no'){ JS::AlertHrefEnd('获取的插件套餐信息有误，不是数组信息。', $backURL); }
		}else{
			$stateArr = array();
			$chkexe = $DB->query('select PSP_packID from '. OT_dbPref .'paySoftPack where PSP_state=0');
			while ($rs = $chkexe->fetch()){
				$stateArr[] = $rs['PSP_packID'];
			}
			unset($chkexe);

			$DB->UpdateParam('paySoftPack',array('PSP_state'=>0),'1=1');
			foreach ($packArr as $oneArr){
				$appPackID = intval($oneArr['packID']);
				$dataArr = array(
					'PSP_lastTime'		=> TimeDate::Get(),
					'PSP_buyDate'		=> TimeDate::Get('date',$oneArr['time']),
					'PSP_updateDate'	=> $oneArr['updateDate'],
					'PSP_packID'		=> $appPackID,
					'PSP_theme'			=> $oneArr['theme'],
					'PSP_money'			=> $oneArr['money'],
					'PSP_xufei'			=> $oneArr['xufei'],
					'PSP_list'			=> $oneArr['list'],
					'PSP_nameList'		=> $oneArr['nameList'],
					'PSP_state'			=> 1
					);
					if (strtotime($oneArr['time'])){
						$dataArr['PSP_time'] = $oneArr['time'];
					}
					if (in_array($appPackID,$stateArr)==false){
						$stateArr[] = $appPackID;
					}
				$readexe = $DB->query('select PSP_ID from '. OT_dbPref .'paySoftPack where PSP_packID='. $appPackID);
				if ($row = $readexe->fetch()){
					$DB->UpdateParam('paySoftPack',$dataArr,'PSP_ID='. $row['PSP_ID']);
				}else{
					$DB->InsertParam('paySoftPack',$dataArr);
				}
				$readexe = null;
			}
			$DB->Delete('paySoftPack','PSP_state=0');

			if (count($stateArr) > 0){
				$DB->UpdateParam('paySoftPack',array('PSP_state'=>0),'PSP_packID in ('. implode(',',$stateArr) .')');
			}
		}

		$appCount = Str::GetMark($retStr,'<appInfoCount>','</appInfoCount>');
		$appStr = Str::GetMark($retStr,'<appInfoList>','</appInfoList>');
		$appArr = unserialize(@$appStr);	// print_r($appArr);die();
		if (! is_array($appArr)){
			if ($mode != 'no'){ JS::AlertHrefEnd('获取的插件信息有误，不是数组信息。', $backURL); }
		}else{
			//print_r($appArr);die();
			$stateArr = array();
			$chkexe = $DB->query('select PS_appID from '. OT_dbPref .'paySoft where PS_state=0');
			while ($rs = $chkexe->fetch()){
				$stateArr[] = $rs['PS_appID'];
			}
			unset($chkexe);

			$DB->UpdateParam('paySoft',array('PS_state'=>0),'1=1');
			foreach ($appArr as $oneArr){
				$phpVer		= OT::ToFloat($oneArr['phpVer'],2);
				$phpVerMax	= OT::ToFloat($oneArr['phpVerMax'],2);
				$packID		= intval($oneArr['packID']);
				$appID		= intval($oneArr['appID']);
				$dataArr = array(
					'PS_lastTime'	=> TimeDate::Get(),
					'PS_type'		=> $oneArr['type'],
					'PS_packID'		=> $packID,
					'PS_appID'		=> $appID,
					'PS_appType'	=> $oneArr['appType'],
					'PS_appCode'	=> $oneArr['authCode'],
					'PS_pluginName'	=> $oneArr['pluginName'],
					'PS_theme'		=> $oneArr['theme'],
					'PS_buyCost'	=> $oneArr['buyCost'],
					'PS_cost'		=> $oneArr['cost'],
					'PS_payMode'	=> $oneArr['payMode'],
					'PS_buyDate'	=> $oneArr['buyDate'],
					'PS_useDate'	=> $oneArr['useDate'],
					'PS_updateDate'	=> $oneArr['updateDate'],
					'PS_currVer'	=> $oneArr['currVer'],
					'PS_currTime'	=> $oneArr['currTime'],
					'PS_softVer'	=> $oneArr['softVer'],
					'PS_softTime'	=> $oneArr['softTime'],
					'PS_phpVer'		=> $phpVer,
					'PS_phpVerMax'	=> $phpVerMax,
					'PS_newVer'		=> $oneArr['newVer'],
					'PS_newTime'	=> $oneArr['newTime'],
					'PS_url'		=> $oneArr['url'],
					'PS_event'		=> $oneArr['event'],
					'PS_state'		=> 1
					);
					if (strtotime($oneArr['time'])){
						$dataArr['PS_time'] = $oneArr['time'];
					}
					if ( $phpVer > 0 && OT::VerCompare($phpVer) < 0 && in_array($appID, $stateArr) == false ){
						$stateArr[] = $appID;
					}elseif ( $phpVerMax > 0 && OT::VerCompare($phpVerMax) > 0 && in_array($appID, $stateArr) == false ){
						$stateArr[] = $appID;
					}
				$readexe = $DB->query('select PS_ID from '. OT_dbPref .'paySoft where PS_appID='. $appID);
				if ($row = $readexe->fetch()){
					$DB->UpdateParam('paySoft',$dataArr,'PS_ID='. $row['PS_ID']);
				}else{
					$DB->InsertParam('paySoft',$dataArr);
				}
				$readexe = null;
			}
			$DB->Delete('paySoft','PS_state=0');

			if (count($stateArr) > 0){
				$DB->UpdateParam('paySoft',array('PS_state'=>0),'PS_appID in ('. implode(',',$stateArr) .')');
			}

			// 遍历所有插件状态判断处理
			AdmArea::PaySoftStateDeal();
		}
	}

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

	if ($mode != 'no'){
		if ($mode == 'back'){
			$appUpdateCount = $DB->GetOne('select count(PS_ID) from '. OT_dbPref .'paySoft where PS_currTime<PS_newTime');
			if ($appUpdateCount > 0){
				JS::AlertHrefEnd('更新完毕（套餐：'. $packCount .'个，插件：'. $appCount .'个）\n'. $cacheStr, 'appShop.php?dataMode=buy');
			}else{
				JS::AlertEnd('更新完毕（套餐：'. $packCount .'个，插件：'. $appCount .'个）\n'. $cacheStr);
			}
		}else{
			JS::AlertHrefEnd('更新完毕（套餐：'. $packCount .'个，插件：'. $appCount .'个）\n'. $cacheStr, $backURL);
		}
	}

}



// 快捷授权登录获取
function GetAuth(){
	global $DB,$systemArr,$sysAdminArr,$authUrlArr;

	$softID		= OT::PostInt('softID');
	$softCode	= OT::PostStr('softCode');
	$domainID	= OT::PostInt('domainID');
	$domainCode	= OT::PostStr('domainCode');
	$username	= OT::PostStr('username');
	$userpwd	= OT::PostStr('userpwd');
	$agent		= OT::PostStr('agent');

	if ($username == '' || $userpwd == ''){
		JS::AlertEnd('表单填写不全');
	}

	$beforeURL	= GetUrl::CurrDir(1);
	$updateUrl = SysDiff::UpdateUrl($sysAdminArr['SA_updateUrlMode']);
	$reqUrl = $updateUrl . OT_SOFTTYPE .'UserInfo.php?mudi=getInfo&OT_UPDATETIME='. OT_UPDATETIME .'&OT_VERSION='. OT_VERSION .'&dataVer='. OT_UPDATEVER .'&phpVer='. PHP_VERSION .'&OT_URL='. urlencode($beforeURL) .'&username='. $username .'&userpwd='. $userpwd .'&softAgent='. $agent .'&softID='. $sysAdminArr['SA_softID'] .'&softCode='. $sysAdminArr['SA_softCode'] .'&domainID='. $sysAdminArr['SA_domainID'] .'&domainCode='. $sysAdminArr['SA_domainCode'] .'&siteName='. urlencode($systemArr['SYS_title']);
	$retStr = ReqUrl::UseAuto($sysAdminArr['SA_getUrlMode'], 'GET', $reqUrl, 'UTF-8', array(), 'note');
	if (strpos($retStr,'[true]') === false){
		JS::AlertEnd('授权获取出错：'. $retStr);
	}
	$softID			= OT::ToInt(Str::GetMark($retStr,'[field:softID]','[/field:softID]'));
	$softCode		= Str::GetMark($retStr,'[field:softCode]','[/field:softCode]');
	$domain			= Str::GetMark($retStr,'[field:domain]','[/field:domain]');
	$domainID		= OT::ToInt(Str::GetMark($retStr,'[field:domainID]','[/field:domainID]'));
	$domainCode		= Str::GetMark($retStr,'[field:domainCode]','[/field:domainCode]');
	$money			= Str::GetMark($retStr,'[field:money]','[/field:money]');
	$agentUserID	= OT::ToInt(Str::GetMark($retStr,'[field:agentUserID]','[/field:agentUserID]'));
	$agentUsername	= Str::GetMark($retStr,'[field:agentUsername]','[/field:agentUsername]');
	$addiStr		= Str::GetMark($retStr,'[field:addiStr]','[/field:addiStr]');

	if ($softID == 0 || $softCode == '' || $domain == '' || $domainID == 0 || $domainCode == ''){
		JS::AlertEnd('授权反馈信息不全（'. $softID .'|'. strlen($softCode) .'|'. $domain .'|'. $domainID .'|'. strlen($domainCode) .'）');
	}

	$record=array();
	$record['SA_softID']		= $softID;
	$record['SA_softCode']		= $softCode;
	$record['SA_username']		= $username;
	$record['SA_domain']		= $domain;
	$record['SA_domainID']		= $domainID;
	$record['SA_domainCode']	= $domainCode;
	$record['SA_agentUserID']	= $agentUserID;
	$record['SA_agentUsername']	= $agentUsername;

	$judRes = $DB->UpdateParam('sysAdmin',$record,'SA_ID=1');
		if ($judRes){
			$Cache = new Cache();
			$result = $Cache->Php('sysAdmin');	// 更新缓存文件
			if ($result){
				$cacheStr = '';
			}else{
				$cacheStr = '\n../cache/php/sysAdmin.php 生成失败，请检查该目录或者文件是否有写入/修改权限！';
			}
			JS::DiyEnd('alert("授权获取成功'. $cacheStr .'");$id("loginBox").innerHTML="登录用户名：'. $username .'，余额：<span style=\'color:red;\'>'. $money .'</span> 元，销售代理：'. ($agentUserID > 0 ? '<span style=\'color:blue;\'>'. $agentUsername .'</span>' : '无') .'。'. $addiStr .'";');
		}else{
			JS::AlertEnd('授权获取失败');
		}
}



function AuthDeal(){
	global $DB,$sysAdminArr,$authUrlArr;

	$mudi2		= OT::GetStr('mudi2');
	$softID		= OT::GetInt('softID');
	$softCode	= OT::GetStr('softCode');
	$username	= OT::GetStr('username');
	$domainID	= OT::GetInt('domainID');
	$domainCode	= OT::GetStr('domainCode');
	$domain		= OT::GetStr('domain');

	$record=array();
	if ($mudi2 == 'user'){

		if ($softID==0 || $username=='' || $softCode==''){
			die('[false]empty');
		}

		$record['SA_softID']		= $softID;
		$record['SA_username']		= $username;
		$record['SA_softCode']		= $softCode;

	}elseif ($mudi2 == 'domain'){

		if ($domainID==0 || $domainCode==''){ // $softID==0 || $username=='' || $softCode=='' || 
			die('[false]empty');
		}

		$record['SA_domainID']		= $domainID;
		$record['SA_domainCode']	= $domainCode;

	}elseif ($mudi2 == 'all'){

		if ($softID > 0){
			$record['SA_softID']		= $softID;
			if (strlen($softCode) > 0){
				$record['SA_softCode']		= $softCode;
			}
		}
		if (strlen($username) > 0){
			$record['SA_username']		= $username;
		}
		if ($domainID > 0){
			$record['SA_domainID']		= $domainID;
			if (strlen($domainCode) > 0){
				$record['SA_domainCode']	= $domainCode;
			}
		}
		if (strlen($domain) > 0){
			$record['SA_domain']		= $domain;
		}

	}else{
		die('[false]mudi2不明确');

	}

	$judRes = $DB->UpdateParam('sysAdmin',$record,'SA_ID=1');
		if ($judRes){
			$alertRes = '[true]';

			$Cache = new Cache();
			$result = $Cache->Php('sysAdmin');	// 更新缓存文件
		}else{
			$alertRes = '[false]';
		}

	die($alertRes);
}




// 插件文件删除处理
function PluginFileDel(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

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

	$chkRow = $DB->GetRow('select HF_mode,HF_filePath from '. OT_dbPref .'htmlFile where HF_ID='. $dataID);
		if (! $chkRow){
			JS::AlertEnd('查找不到该记录');
		}
		if ($chkRow['HF_mode'] != 'plugin'){
			JS::AlertEnd('类型不对（'. $chkRow['HF_mode'] .'）');
		}

		$delJud = File::Del(OT_ROOT .'plugin/'. $chkRow['HF_filePath']);
		if ($delJud){
			$alertRes = '成功';
			$DB->query('delete from '. OT_dbPref .'htmlFile where HF_ID='. $dataID);
		}else{
			$alertRes = '失败';
		}

		Adm::AddLog(array(
			'note'	=> '【'. $dataTypeCN .'】删除文件“plugin/'. $chkRow['HF_filePath'] .'”'. $alertRes .'！',
			));

		echo('
		<script language="javascript" type="text/javascript">
		parent.$id("pluginFile'. $dataID .'").style.display="none";
		</script>
		');	
}

?>