<?php
require_once(dirname(__FILE__)."/config.php");
require_once(dirname(__FILE__)."/../sitedata/site_data.php");
if(empty($dopost)) $dopost = '';
$action = isset($_GET['act']) ? $_GET['act'] : 'one';
/** 生成指定长度的随机字符串 */
function random($length = 16, $isnum = false){
	$seed = base_convert(md5(microtime().$_SERVER['DOCUMENT_ROOT']), 16, $isnum ? 10 : 35);
	$seed = $isnum ? $seed.'zZ'.strtoupper($seed) : str_replace('0', '', $seed).'01234056789';
	
	$randstr = '';
	$max = strlen($seed) - 1;
	for ($i = 0; $i < $length; $i++) {
		$randstr .= $seed{mt_rand(0, $max)};
	}
	return $randstr;
}
if($dopost =='next')
{
	$domain = strtolower(trim($_POST['domain']));
	if (empty($domain)) {
		ShowMsg("请输入要认领的域名 ！","-1","1",5000);
		exit();
		}
		else {
			if (!is_valid_domain($domain)) {
				ShowMsg("请输入正确的域名 ！","-1","1",5000);
				exit();
				}
		$row = $dsql->GetOne("SELECT aid FROM `#@__website` WHERE weburl='$domain'");
    	 if(!is_array($row)) {
			ShowMsg("此网站还未被提交！","$cfg_memberurl/site_add.php","0",5000);
			exit();
    	}
			}
		$action = 'two'; 
		$token = random(32);
		$weburl = format_url($domain);
}
if($dopost =='verify')
{
	$vtype = trim($_POST['vtype']);
		$domain = strtolower(trim($_POST['domain']));
		$token = trim($_POST['token']);
		
		if (empty($vtype)) {
			ShowMsg("请选择验证类型 ！","-1","1",5000);
				exit();
		}
		$weburl = format_url($domain);
		if ($vtype == 'file') {
			$content = get_url_content($weburl.'xiangtui-verification.txt');
			if ($content == $token) {
				$oldweb = $dsql->GetOne("SELECT * FROM `#@__website` WHERE weburl='$domain'; ");
				$dsql->ExecuteNoneQuery("Update `#@__member_tj` set site=site-1 where mid='{$oldweb['mid']}' ; ");
				$dsql->ExecuteNoneQuery("Update `#@__arctiny` set mid='".$cfg_ml->M_ID."' where id='{$oldweb['aid']}' ; ");
				$dsql->ExecuteNoneQuery("Update `#@__archives` set mid='".$cfg_ml->M_ID."' where id='{$oldweb['aid']}' ; ");
				$dsql->ExecuteNoneQuery("Update `#@__member_tj` set site=site+1 where mid='".$cfg_ml->M_ID."' ; ");
				ShowMsg("网站认领成功 ！","$cfg_memberurl/site_list.php","0",5000);
				exit();
			} else {
				ShowMsg("网站验证失败，重新验证？","$cfg_memberurl/site_claim.php","0",5000);
				exit();
			}
		}
				if ($vtype == 'meta') {
			$content = get_url_content($weburl);
			if (preg_match('/<meta\s+name=\"xiangtui-verification\"\s+content=\"(.*?)\"/si', $content, $matches)) {
				if ($matches[1] == $token) {
				$oldweb = $dsql->GetOne("SELECT * FROM `#@__website` WHERE weburl='$domain'; ");
				$dsql->ExecuteNoneQuery("Update `#@__member_tj` set site=site-1 where mid='{$oldweb['mid']}' ; ");
				$dsql->ExecuteNoneQuery("Update `#@__arctiny` set mid='".$cfg_ml->M_ID."' where id='{$oldweb['aid']}' ; ");
				$dsql->ExecuteNoneQuery("Update `#@__archives` set mid='".$cfg_ml->M_ID."' where id='{$oldweb['aid']}' ; ");
				$dsql->ExecuteNoneQuery("Update `#@__member_tj` set site=site+1 where mid='".$cfg_ml->M_ID."' ; ");
				ShowMsg("网站认领成功 ！","$cfg_memberurl/site_list.php","0",5000);
				exit();				
				} else {
					ShowMsg("网站验证失败，重新验证？","$cfg_memberurl/site_claim.php","0",5000);
					exit();
				exit();
				}
			} else {
				ShowMsg("您还未向首页添加元标记？","$cfg_memberurl/site_claim.php","0",5000);
				exit();
			}
		}
}
require_once(DEDEMEMBER."/templets/site_claim.htm");
