<?php

class ping_RPC2{
	
	var $url;
	var $param1;
	var $param2;
	var $param3;
	var $param4;
	var $xml,$headers;

	function makexml(){
		$this->xml = '<?xml version="1.0" encoding="UTF-8"?><methodCall>
			<methodName>weblogUpdates.extendedPing</methodName>
			<params>
				<param>
					<value><string>'.$this->param1.'</string></value>
				</param>
				<param>
					<value><string>'.$this->param2.'</string></value>
				</param>
				<param>
					<value><string>'.$this->param3.'</string></value>
				</param>
				<param>
					<value><string>'.$this->param4.'</string></value>
				</param>
			</params>
		</methodCall>';
	}
	
	function headers(){
		$this->headers = array( 
			"POST ".$this->url." HTTP/1.0",
			"User-Agent: request",
			"Content-type: text/xml; charset=\"utf-8\"", 
			"Accept: text/xml", 
			"Content-length: ".strlen($this->xml) 
		);
	}
	
	function ping(){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 1);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $this->xml);
		$output = curl_exec($ch);
		curl_close($ch);
		return $output;
	}
	
	function dedeping($id){
		global $dsql,$cfg_basehost,$cfg_cmspath,$cfg_multi_site,$cfg_ping_urls;
		$id = intval($id);
		$time = time();
		$dsql->ExecuteNoneQuery("insert into #@__pinglog (aid, status, uptime) values ($id, 1, $time)");
		$one = GetOneArchive($id);
		$arcurl = $cfg_multi_site=='Y'?$one['arcurl']:$cfg_basehost.$one['arcurl'];
		$this->param1 = $one['title'];
		$this->param2 = $cfg_basehost;
		$this->param3 = $arcurl;
		$this->param4 = $cfg_basehost.$cfg_cmspath.'/sitemap.xml';
		
		$urls = explode("\n",$cfg_ping_urls);
		foreach($urls as $key => $url){
			$this->url = trim($url);
			$this->makexml();
			$this->headers();
			$rs = $this->ping();
			if(strpos($rs,'200 OK')===false){
				$dsql->ExecuteNoneQuery("update #@__pinglog set status=0 where aid=$id");
			}
		}
	}
	
}

if(defined('DEDEADMIN') || mt_rand(0,10)==0){
	$test_install = DEDEDATA."/module/f8386d67d2a0abe1340f31168481c175-readme.php";
	if(is_file($test_install)){
		$pgr = $dsql->GetOne("select * from #@__pinglog order by aid desc");
		if($pgr){
			$pgr_max_aid = $pgr['aid'];
			$pgr_dedeping = $dsql->GetOne("select * from #@__arctiny where id>$pgr_max_aid AND arcrank=0 order by id asc");
		}else{
			$pgr_dedeping = $dsql->GetOne("select * from #@__arctiny where arcrank=0 order by id desc");
		}
		if($pgr_dedeping){
			$rpc = new ping_RPC2;
			$rpc->dedeping($pgr_dedeping['id']);
		}
	}
}

