<?php
/**
 * 採集操作
 *
 * @version        $Id: co_do.php 1 14:31 2010年7月12日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");
require_once(DEDEINC."/oxwindow.class.php");
if(!isset($nid)) $nid=0;
$ENV_GOBACK_URL = empty($_COOKIE["ENV_GOBACK_URL"]) ? "co_url.php" : $_COOKIE["ENV_GOBACK_URL"];

//刪除節點
//刪除節點將刪除所有舊的網址索引
/*
function co_delete()
*/
if($dopost=="delete")
{
    CheckPurview('co_Del');
    $nid = intval($nid);
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__co_htmls` WHERE nid='$nid'");
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__co_note` WHERE nid='$nid'");
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__co_urls` WHERE nid='$nid'");
    ShowMsg("成功刪除一個節點!","co_main.php");
    exit();
}

//清空採集內容
//清空採集內容時仍會保留舊的網址索引，在監控模式下始終採集新的內容
/*
function url_clear()
*/
else if($dopost=="clear")
{
    CheckPurview('co_Del');
    if(!isset($ids)) $ids='';
    if(empty($ids))
    {
        if(!empty($nid))
        {
            $nid = intval($nid);
            $dsql->ExecuteNoneQuery("DELETE FROM `#@__co_htmls` WHERE nid='$nid'");
        }
        ShowMsg("成功清空一個節點採集的內容!","co_main.php");
        exit();
    }
    else
    {
        if(!empty($clshash))
        {
            $dsql->SetQuery("SELECT nid,url FROM `#@__co_htmls` WHERE aid IN($ids) ");
            $dsql->Execute();
            while($arr = $dsql->GetArray())
            {
                $nhash = md5($arr['url']);
                $nid = $row['nid'];
                $dsql->ExecuteNoneQuery("DELETE FROM `#@__co_urls ` WHERE nid='$nid' AND hash='$nhash' ");
            }
        }
        $dsql->ExecuteNoneQuery("DELETE FROM `#@__co_htmls` WHERE aid IN($ids) ");
        ShowMsg("成功刪除指定的網址內容!",$ENV_GOBACK_URL);
        exit();
    }
}
else if($dopost=="clearct")
{
    CheckPurview('co_Del');
    if(!empty($ids))
    {
        $dsql->ExecuteNoneQuery("UPDATE `#@__co_htmls` SET isdown=0,result='' WHERE aid IN($ids) ");
    }
    ShowMsg("成功清除所有內容!",$ENV_GOBACK_URL);
    exit();
}
/*
function url_clearall()
*/
else if($dopost=="clearall")
{
    CheckPurview('co_Del');
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__co_htmls` ");
    ShowMsg("成功清空所有採集的臨時內容!","co_main.php");
    exit();
}
//內容替換
/*
function co_replace() { }
*/
else if($dopost=="replace")
{
    //if()
    //$nid $aid $regtype $fdstring $rpstring
    $rpstring = trim($rpstring);
    if($regtype=='string')
    {
        $dsql->ExecuteNoneQuery("UPDATE `#@__co_htmls` SET `result`=REPLACE(`result`,'$fdstring','$rpstring') WHERE nid='$nid' ");
    }
    else
    {
        //返回一條測試結果，並要求用戶確認操作
        if(empty($rpok))
        {
            $fdstring = stripslashes($fdstring);
            $rpstring = stripslashes($rpstring);
            $hiddenrpvalue = "<textarea name='fdstring' style='display:none'>{$fdstring}</textarea>\r\n<textarea name='rpstring' style='display:none'>{$rpstring}</textarea>\r\n";
            $fdstring = str_replace("\\/","#ASZZ#",$fdstring);
            $fdstring = str_replace('/',"\\/",$fdstring);
            $fdstring = str_replace('#ASZZ#',"\\/",$fdstring);
            $result = $rs = stripslashes($rs);
            if($fdstring!='')
            {
                $result = trim(preg_replace("/$fdstring/isU",$rpstring,$rs));
            }
            $wintitle = "採集管理-內容替換";
            $wecome_info = "<a href='co_main.php'>採集管理</a>::內容替換";
            $win = new OxWindow();
            $win->Init("co_do.php","js/blank.js","POST");
            $win->AddHidden('dopost',$dopost);
            $win->AddHidden('nid',$nid);
            $win->AddHidden('regtype','regex');
            $win->AddHidden('aid',$aid);
            $win->AddHidden('rpok','ok');
            $win->AddTitle("內容替換操作確認：如果下面結果正確，點擊確認，系統將替換當前節點所有內容！{$hiddenrpvalue}");
            $win->AddItem("原來的內容：","<textarea name='rs' style='width:90%;height:250px'>{$rs}</textarea>\r\n");
            $win->AddItem("按規則替換後的內容：","<textarea name='okrs' style='width:90%;height:250px'>{$result}</textarea>\r\n");
            $winform = $win->GetWindow("ok");
            $win->Display();
            exit();
        }
        else
        {
            if($fdstring!='')
            {
                $dsql->SetQuery("SELECT `aid`,`result` FROM `#@__co_htmls` WHERE nid='$nid' ");
                $dsql->Execute();
                while($row = $dsql->GetArray())
                {
                    $fdstring = stripslashes($fdstring);
                    $rpstring = stripslashes($rpstring);
                    $fdstring = str_replace("\\/","#ASZZ#",$fdstring);
                    $fdstring = str_replace('/',"\\/",$fdstring);
                    $fdstring = str_replace('#ASZZ#',"\\/",$fdstring);
                    $result = trim(preg_replace("/$fdstring/isU",$rpstring,$row['result']));
                    $result = addslashes($result);
                    $dsql->ExecuteNoneQuery("UPDATE `#@__co_htmls` SET `result`='$result' WHERE aid='{$row['aid']}' ");
                }
            }
        }
    }
    ShowMsg("成功替換當前節點所有數據！","co_view.php?aid=$aid");
    exit();
}
//複製節點
/*
function co_copy()
*/
else if($dopost=="copy")
{
    CheckPurview('co_AddNote');
    if(empty($mynotename))
    {
        $wintitle = "採集管理-複製節點";
        $wecome_info = "<a href='co_main.php'>採集管理</a>::複製節點";
        $win = new OxWindow();
        $win->Init("co_do.php","js/blank.js","POST");
        $win->AddHidden("dopost",$dopost);
        $win->AddHidden("nid",$nid);
        $win->AddTitle("請輸入新節點名稱：");
        $win->AddItem("新節點名稱：","<input type='text' name='mynotename' value='' size='30' />");
        $winform = $win->GetWindow("ok");
        $win->Display();
        exit();
    }
    $row = $dsql->GetOne("SELECT * FROM `#@__co_note` WHERE nid='$nid'");
    foreach($row as $k=>$v)
    {
        if(!isset($$k))
        {
            $$k = addslashes($v);
        }
    }
    $usemore = (empty($usemore) ? '0' : $usemore);
    $inQuery = " INSERT INTO `#@__co_note`(`channelid`,`notename`,`sourcelang`,`uptime`,`cotime`,`pnum`,`isok`,`listconfig`,`itemconfig`,`usemore`)
               VALUES ('$channelid','$mynotename','$sourcelang','".time()."','0','0','0','$listconfig','$itemconfig','$usemore'); ";
    $dsql->ExecuteNoneQuery($inQuery);
    ShowMsg("成功複製一個節點!",$ENV_GOBACK_URL);
    exit();
}
//測試Rss源是否正確
/*-----------------------
function co_testrss()
-------------------------*/
else if($dopost=="testrss")
{
    CheckPurview('co_AddNote');
    $msg = '';
    if($rssurl=='')
    {
        $msg = '你沒有指定RSS地址！';
    }
    else
    {
        include(DEDEINC."/dedecollection.func.php");
        $arr = GetRssLinks($rssurl);
        $msg = "從 {$rssurl} 發現的網址：<br />";
        $i=1;
        if(is_array($arr))
        {
            foreach($arr as $ar)
            {
                $msg .= "<hr size='1' />\r\n";
                $msg .= "link: {$ar['link']}<br />title: {$ar['title']}<br />image: {$ar['image']}\r\n";
                $i++;
            }
        }
    }
    $wintitle = "採集管理-測試";
    $wecome_info = "<a href='co_main.php'>採集管理</a>::RSS地址測試";
    $win = new OxWindow();
    $win->AddMsgItem($msg);
    $winform = $win->GetWindow("hand");
    $win->Display();
    exit();
}
//測試批量網址是否正確
/*-----------------------
function co_testregx()
-------------------------*/
else if($dopost=="testregx")
{
    CheckPurview('co_AddNote');
    $msg = '';
    if($regxurl=='')
    {
        $msg = '你沒有指定匹配的網址！';
    }
    else
    {
        include(DEDEINC."/dedecollection.func.php");
        $msg = "匹配的網址：<br />";
        $lists = GetUrlFromListRule($regxurl, '', $startid, $endid, $addv);
        foreach($lists as $surl)
        {
            $msg .= $surl[0]."<br />\r\n";
        }
    }
    $wintitle = "採集管理-測試匹配規則";
    $wecome_info = "<a href='co_main.php'>採集管理</a>::測試匹配列表網址規則";
    $win = new OxWindow();
    $win->AddMsgItem($msg);
    $winform = $win->GetWindow("hand");
    $win->Display();
    exit();
}

//採集未下載內容
/*--------------------
function co_all()
---------------------*/
else if($dopost=="coall")
{
    CheckPurview('co_PlayNote');
    $mrow = $dsql->GetOne("SELECT COUNT(*) AS dd FROM `#@__co_htmls` ");
    $totalnum = $mrow['dd'];
    if($totalnum==0)
    {
        ShowMsg("沒發現可下載的內容！","-1");
        exit();
    }
    $wintitle = "採集管理-採集未下載內容";
    $wecome_info = "<a href='co_main.php'>採集管理</a>::採集未下載內容";
    $win = new OxWindow();
    $win->Init("co_gather_start_action.php","js/blank.js","GET");
    $win->AddHidden('startdd','0');
    $win->AddHidden('pagesize','5');
    $win->AddHidden('sptime','0');
    $win->AddHidden('nid','0');
    $win->AddHidden('totalnum',$totalnum);
    $win->AddMsgItem("本操作會檢測並下載『<a href='co_url.php'><u>臨時內容</u></a>』中所有未下載的內容，是否繼續？");
    $winform = $win->GetWindow("ok");
    $win->Display();
    exit();
}