<?php
/**
 * 導入採集規則
 *
 * @version        $Id: co_get_corule.php 1 17:13 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(dirname(__FILE__)."/config.php");
CheckPurview('co_AddNote');
if(empty($job)) $job='';

if($job=='')
{
    require_once(DEDEINC."/../include/oxwindow.class.php");
    $wintitle = "導入採集規則";
    $wecome_info = "<a href='co_main.php'><u>採集點管理</u></a>::導入採集規則";
    $win = new OxWindow();
    $win->Init("co_get_corule.php","js/blank.js","POST");
    $win->AddHidden("job","yes");
    $win->AddTitle("請在下面輸入你要導入的文本配置：(建議用base64編碼[支持不編碼的規則，但不兼容舊版規則])");
    $win->AddMsgItem("<textarea name='notes' style='width:99%;height:300px'></textarea>");
    $winform = $win->GetWindow("ok");
    $win->Display();
    exit();
}
else
{
    CheckPurview('co_AddNote');
    require_once(DEDEINC."/dedetag.class.php");
    $notes = trim($notes);

    //對Base64格式的規則進行解碼
    if(preg_match("#^BASE64:#", $notes))
    {
        if(!preg_match("#:END$#", $notes))
        {
            ShowMsg('該規則不合法，Base64格式的採集規則為：BASE64:base64編碼後的配置:END !','-1');
            exit();
        }
        $notess = explode(':',$notes);
        $notes = $notess[1];
        $notes = base64_decode(preg_replace("#[\r\n\t ]#", '', $notes)) OR die('配置字符串有錯誤！');
    }
    else
    {
        $notes = stripslashes($notes);
    }
    // 進行轉碼
    if ($cfg_soft_lang == 'gb2312')
    {
        $notes = iconv('ucs-2', 'gb18030', $notes);
    } else if($cfg_soft_lang == 'utf-8')
    { 
        $notes = iconv('ucs-2', 'utf-8//ignore', $notes);
    }
    $dtp = new DedeTagParse();
    $dtp->LoadString($notes);
    if(!is_array($dtp->CTags))
    {
        ShowMsg('該規則不合法，無法導入!','-1');
        exit();
    }
    $ctag1 = $dtp->GetTagByName('listconfig');
    $ctag2 = $dtp->GetTagByName('itemconfig');
    $listconfig = $ctag1->GetInnerText();
    $itemconfig = addslashes($ctag2->GetInnerText());
    $dtp->LoadString($listconfig);
    $listconfig = addslashes($listconfig);
    $noteinfo = $dtp->GetTagByName('noteinfo');
    if(!is_object($noteinfo))
    {
        ShowMsg("該規則不合法，無法導入!","-1");
        exit();
    }
    foreach($noteinfo->CAttribute->Items as $k=>$v)
    {
        $$k = addslashes($v);
    }
    $uptime = time();
    
    if(empty($freq)) $freq = 1;
    if(empty($extypeid)) $extypeid = 0;
    if(empty($islisten)) $islisten = 0;

    $inquery = " INSERT INTO `#@__co_note`(`channelid`,`notename`,`sourcelang`,`uptime`,`cotime`,`pnum`,`isok`,`usemore`,`listconfig`,`itemconfig`)
               VALUES ('$channelid','$notename','$sourcelang','$uptime','0','0','0','$usemore','$listconfig','$itemconfig'); ";
    $rs = $dsql->ExecuteNoneQuery($inquery);
    if(!$rs)
    {
        ShowMsg("保存信息時出現錯誤！".$dsql->GetError(),"-1");
        exit();
    }
    ShowMsg("成功導入一個規則!","co_main.php");
    exit();
}