<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
viewHead("数据管理"."-" .$menuList[4][0]);
//内容替换表
$RepWordFile="imgs/repword.txt";
//伪原创,文本段
$TextSegmentFile="/inc/textsegment.xml";

$action = getForm("action", "get");
switch ($action) {
	case "saverepword":saverepword();break;
	case "doReplace":doReplace();break;
	case "del":delTextsegment();break;
	case "read":getTextsegment();break;
	case "save":saveTextsegment();break;
	default : main();break;
}
viewFoot();

function main() {
	global $menuList,$RepWordFile;
    $txt="";$A=getTextsegments();
    if (isExistFile($RepWordFile)){
	    $txt=loadFile($RepWordFile);
	}else{
	    $txt="逮捕=拘捕\n恶毒=狠毒\n暗算=暗害";
	}
?>
<div class="container" id="cpcontainer">
<form  method="post" action="?action=saverepword">
<table class="tb">
	<tr class="thead"><td colspan="2">伪原创-同义词批量替换(本功能只替换数据介绍)</td></tr>
	<tr><td width="12%">替换字符设置：</td>
	<td width="88%"><textarea name="txt" style="width:500px;height:100px;">
<?=$txt?></textarea></td></tr>
	<tr><td width="12%">数据起始ID：</td><td width="88%"><input type="text" name="iStart" value="不限" /> 到 <input type="text" name="iEnd" value="不限" /> &nbsp;&nbsp;
    <select name="Days" id="Days">
       <option value="">全部</option>
        <?php for ($i = 1;$i < 7;$i++) {
echo "<option value=\"$i\">$i</option>\n";
}?>

		</select>
		日内内容 <input type="submit" class="btn" value="执行字词替换" /></td></tr>
	<tr><td colspan="2">功能说明：<br />&nbsp;&nbsp;1.每个要替换的字词占一行&nbsp;&nbsp;2.格式：<font color="blue">要替换=替换后</font>,&nbsp;&nbsp;<font color="red">以上文本区域内的替换字符为范例,请删除并根据需要自行设置</font><br />&nbsp;&nbsp;2.采集来的内容雷同太多，不易被搜索引擎收录。可以用本功能把一些特定的词替换成自己的词，如替换成自己的方言或同义词 <br />
	&nbsp;&nbsp;3.举个例子：<font color="blue">广东话(为什么=点解)</font>&nbsp;&nbsp;<font color="blue">广东台山话(为什么=几解)</font>,经过替换，就让你的站拥有独特的风格。<br />注意事项:<br>&nbsp;&nbsp;<font color=red>程序遇到待替换的字符就会立即替换且不可恢复，请小心处理。</font></td></tr>
</table>
</form>
<br />

<table class="tb">
	<tr class="thead"><td colspan="2">伪原创-随机内容<a name="texts" /></td></tr>
<?php
	$l = count($A);
	for ($i = 0;$i < $l;$i++) {
?>
		<tr><td width="90%"><?php echo ($i+1).".".msubstr($A[$i],60).ifthen(strlen($A[$i])>60,"...","")?></td><td><a href="?action=read&id=<?=($i+1)?>" target="hiddensubmit">修改</a> <a href="?action=del&id=<?=($i+1)?>">删除</a></td></tr>
<?php
	}
?>
<form method="post" action="?action=save" target="hiddensubmit">
<input type="hidden" name="id" value="" />
	<tr class="thead"><td colspan="2" id="newhit">添加</td></tr>
	<tr><td colspan="2"><textarea name="newtxt" style="width:100%;height:100px;"></textarea><br /><input type="submit" class="btn" value="确定" /> <input type="reset" class="btn" value="取消" onclick="$('newhit').innerHTML='添加';"/></td></tr>
	<tr><td colspan="2"><p>注意：</p>
	  <p>1.如果收录已经非常良好，无需使用本功能<br>
	    2.请一次性添加20条以上伪原创文本段,可以是20段笑话。别写得太长，建议不超过300字，注意是一次性添加相应数目、之后不要再新增、修改、删除文本段<br>
	      <font color="red">3</font><font color="red">.最好别经常改动文本段设置(添加、修改或删除)。搜索引擎对这个非常敏感。如果它发现每次来收录的内容都不一样，会影响收录或降权<br />
	        4.每段内容由系统随机抽出并随机插入到简介的首部、尾部、或中间，而且并不改动数据库，也不会每次都随机改变</font></p></td></tr>
</form>
</table>
</div>
<iframe frameborder="0" scrolling="no" height="0" width="0" name="hiddensubmit" id="hiddensubmit" src="about:blank"></iframe>
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?=$menuList[4][0]?>&nbsp;&raquo;&nbsp;伪原创工具';</script>
<?php
}

function saveTextsegment(){
	global $TextSegmentFile;
	$ts=getTextsegments();$l=count($ts);$id=getForm("id","post")-1;$newtxt=getForm("newtxt","post");
	if ($newtxt==""){
		alertMsg("请输入内容","");die("");
    }elseif ($id>$l && $l=='-1'){
		alertMsg("修改失败,无效ID","");die("");
    }elseif ($id>'-1'){
		$ts[$id]=$newtxt;
    }else{
		$ts[$l+1]= $newtxt;
    }
	$xmlstr="<?xml version=\"1.0\" encoding=\"gbk\" ?><root>%s</root>";
	$tmp="";
	foreach($ts as $key => $val){
		$tmp .="<item><![CDATA[".$val."]]></item>";
	}
	$xmlstr=replaceStr($xmlstr,"%s",$tmp);
	CreateTextFile($xmlstr,$TextSegmentFile);
?>
<script language="javascript">
alert('保存成功');
parent.$('newhit').innerHTML='添加';
parent.$('id').value='';
parent.$('newtxt').value='';
parent.location.href='<?=getRefer()?>?'+Math.random()+'#texts';
</script>
<?php
}

function delTextsegment(){
	global $TextSegmentFile;
	$ts=getTextsegments();$l=count($ts);$id=getForm("id","get")-1;
	if ($id<=$l && $l<>'-1' && $id<>'-1'){
		$xmlstr="<?xml version=\"1.0\" encoding=\"gbk\" ?><root>%s</root>";$tmp="";
		foreach($ts as $key => $val){
			if ($key!=$id){ $tmp .="<item><![CDATA[".$val."]]></item>";}
		}
		$xmlstr=replaceStr($xmlstr,"%s",$tmp);
		CreateTextFile($xmlstr,$TextSegmentFile);
    }
	alertMsg("删除成功",getRefer()."#texts");
}

function getTextsegment(){
	$ts=getTextsegments();$i=count($ts);$id=getForm("id","get")-1;
	if ($id>$i || $i=='-1' || $id=='-1'){
		alertMsg("读取数据失败,内容可能已被删除,请刷新页面","");
	}else{
?>
<script language="javascript">
parent.$('newhit').innerHTML='修改';
parent.$('id').value='<?=$id+1?>';
parent.$('newtxt').value='<?=replaceStr(replaceStr($ts[$id],chr(13),""),chr(10),"\n")?>';
</script>
<?php
    }
}

function saverepword(){
	global $RepWordFile;
	$result=trim(getForm("result","get"));$result=ifthen($result<>"",$result,getRefer());$WordText=trim(getForm("txt","post"));$iStart=getForm("iStart","post");$iEnd=getForm("iEnd","post");$Days=getForm("Days","post");
	fwrite(fopen($RepWordFile,"wb"),$WordText);
	echo "<script>location.href='?action=doReplace&iStart=".$iStart."&iEnd=".$iEnd."&Days=".$Days."&result=".URLEncode($result)."';</script>";
}

function doReplace(){
	global $RepWordFile,$action;
    $result=trim(getForm("result","get"));$result=ifthen($result<>"",$result,getRefer());
    if (isExistFile($RepWordFile)){
	    $Text=trim(LoadFile($RepWordFile));
	    if ($Text<>""){
			$days = Str2Num(getForm("Days"));
	        $ntime = gmmktime(0, 0, 0, gmdate('m'), gmdate('d'), gmdate('Y'));
	        $limitday = $ntime - ($days * 24 * 3600);
		    $whereStr ='WHERE 1=1';
			$Words = explode(chr(13).chr(10),$Text);
		    $iStart=Str2Num(getForm("iStart"));
		    $iEnd=Str2Num(getForm("iEnd"));
		    $rCount=Str2Num(getForm("rCount"));
		    $pCount=Str2Num(getForm("pCount"));
		    $page=Str2Num(getForm("page","get"));
		    $page=ifthen($page<1,1,$page);
			if (!empty($days)){ $whereStr .= " AND m_addtime>".$limitday;}
		    if (!empty($iStart)){ $whereStr .= " AND m_id>".($iStart);}
		    if (!empty($iEnd)){ $whereStr .= " AND m_id<".($iEnd);}
		    $number = 1000;
		    $sqlStr = "SELECT m_id,m_name,m_des FROM ".DB::table('data')." $whereStr";
		    $allRecordset = DB::getCount($sqlStr);
	        $tpagecount = ceil($allRecordset / $number);
			if ($allRecordset == 0 || $page > $tpagecount) {
		        alertMsg("操作完成",$result);exit();
	        }
	        if ($page > $tpagecount) $page = $tpagecount;
			echo "<div class=\"container\" id=\"cpcontainer\">正在替换,当前是第<font color='red'>".$page."</font>页,共<font color='red'>".$tpagecount."</font>页,共<font color='red'>".$allRecordset."</font>部数据<hr style=\"border:1px solid #DEEFFA\"/>";
			$page = max(intval($page), 1);
            $offset = $number*($page-1);
            $query = DB::query("$sqlStr LIMIT $offset, $number");
            while($row = DB::fetch($query)) {
				$Text = $row['m_des'];$sText=$Text;
				$l = count($Words);
	            for ($i = 0;$i < $l;$i++) {
					$ss = explode("=",$Words[$i]);
					$Text = str_replace("\r","",$Text);  
		            $Text = str_replace($ss[0],$ss[1],$Text);
				}
				if ($Text<>$sText){
					echo "<font color=blue>成功替换 ID:".$row['m_id']."	".$row['m_name']."</font><br>";
					DB::update('data', array('m_des' => $Text), array('m_id' => $row['m_id']));
				}else{
					echo "<font color=red>跳过替换 ID:".$row['m_id']."	".$row['m_name']."</font><br>";
				}
			}
			echo "<br>暂停3秒后继续替换</div><script language=\"javascript\">setTimeout(function (){location.href='?action=".$action."&page=".($page+1)."&pcount=".$tpagecount."&rcount=".$allRecordset."&iStart=".$iStart."&iEnd=".$iEnd."&Days=".$days."&result=".URLEncode($result)."';},3000);</script>";
		}else{
			alertMsg("操作完成",$result);
		}
	}else{
	    alertMsg("文件不存在: ".$RepWordFile,$result);
	}
}
?>