<?php
/**
 * 數據庫操作替換
 *
 * @version        $Id: sys_data_replace.php 1 22:28 2010年7月20日Z tianya $
 * @package        DedeCMS.Administrator
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__).'/config.php');
CheckPurview('sys_Data');
if(empty($action)) $action = '';
if(empty($action))
{
    require_once(DEDEADMIN."/templets/sys_data_replace.htm");
    exit();
}

/*-------------------------------
//列出數據庫表裡的字段
function __getfields()
--------------------------------*/
else if($action=='getfields')
{
    AjaxHead();
    $dsql->GetTableFields($exptable);
    echo "<div style='border:1px solid #ababab;background-color:#FEFFF0;margin-top:6px;padding:3px;line-height:160%'>";
    echo "表(".$exptable.")含有的字段：<br>";
    while($row = $dsql->GetFieldObject())
    {
        echo "<a href=\"javascript:pf('{$row->name}')\"><u>".$row->name."</u></a>\r\n";
    }
    echo "</div>";
    exit();
}
/*-------------------------------
//保存用戶設置，清空會員數據
function __Apply()
--------------------------------*/
else if($action=='apply')
{
    $validate = empty($validate) ? '' : strtolower($validate);
    $svali = GetCkVdValue();
    if($validate == "" || $validate != $svali)
    {
        ShowMsg("安全確認碼不正確!", "javascript:;");
        exit();
    }
    if($exptable == '' || $rpfield == '')
    {
        ShowMsg("請指定數據表和字段！", "javascript:;");
        exit();
    }
    if($rpstring=='')
    {
        ShowMsg("請指定被替換內容！", "javascript:;");
        exit();
    }
    if($rptype=='replace')
    {
        $condition = empty($condition) ? '' : " WHERE $condition ";
        $rs = $dsql->ExecuteNoneQuery("UPDATE $exptable SET $rpfield=REPLACE($rpfield,'$rpstring','$tostring') $condition ");
        $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `$exptable`");
        if($rs)
        {
            ShowMsg("成功完成數據替換！", "javascript:;");
            exit();
        }
        else
        {
            ShowMsg("數據替換失敗！", "javascript:;");
            exit();
        }
    }
    else
    {
        $condition = empty($condition) ? '' : " And $condition ";
        $rpstring = stripslashes($rpstring);
        $rpstring2 = str_replace("\\","\\\\",$rpstring);
        $rpstring2 = str_replace("'","\\'",$rpstring2);
        $dsql->SetQuery("SELECT $keyfield,$rpfield FROM $exptable WHERE $rpfield REGEXP '$rpstring2'  $condition ");
        $dsql->Execute();
        $tt = $dsql->GetTotalRow();
        if($tt==0)
        {
            ShowMsg("根據你指定的正則，找不到任何東西！","javascript:;");
            exit();
        }
        $oo = 0;
        while($row = $dsql->GetArray())
        {
            $kid = $row[$keyfield];
            $rpf = preg_replace("#".$rpstring."#i", $tostring, $row[$rpfield]);
            $rs = $dsql->ExecuteNoneQuery("UPDATE $exptable SET $rpfield='$rpf' WHERE $keyfield='$kid' ");
            if($rs)
            {
                $oo++;
            }
        }
        $dsql->ExecuteNoneQuery("OPTIMIZE TABLE `$exptable`");
        ShowMsg("共找到 $tt 條記錄，成功替換了 $oo 條！", "javascript:;");
        exit();
    }
}