<?php   if(!defined('DEDEMEMBER')) exit("dedecms");
/**
 * 文档操作处理函数
 * 
 * @version        $Id: inc_batchup.php 1 13:52 2010年7月9日Z tianya $
 * @package        DedeCMS.Member
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(DEDEINC."/channelunit.func.php");


/**
 *  删除不带主表内容模型的数据
 *
 * @access    public
 * @param     int  $aid  文档ID
 * @return    string
 */
function DelArcSg($aid)
{
    global $dsql,$cfg_cookie_encode,$cfg_ml,$cfg_upload_switch,$cfg_medias_dir;
    $aid = intval($aid);

    //读取文档信息
    $arctitle = '';
    $arcurl = '';

    $arcQuery = "Select arc.id,arc.typeid,arc.senddate,arc.arcrank,ch.addtable,ch.nid,
        tp.typedir,tp.typename,tp.namerule,tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
          from `#@__arctiny` arc
          left join `#@__arctype` tp on tp.id=arc.typeid
          left join `#@__channeltype` ch on ch.id=arc.channel
        where arc.id='$aid' ";
    $arcRow = $dsql->GetOne($arcQuery);

    if(!is_array($arcRow))
    {
        return FALSE;
    }

    //删除数据库的内容
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__arctiny` where id='$aid' ");
    $dsql->ExecuteNoneQuery("DELETE FROM `".$arcRow['addtable']."` where aid='$aid' ");
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__feedback` where aid='$aid'");
    $dsql->ExecuteNoneQuery("DELETE FROM `#@__webtaglist ` where aid='$aid'");
	$dsql->ExecuteNoneQuery("UPDATE `#@__member_tj` SET site=site-1 where mid=".$arcRow['mid']." ");
//更新文档TAGS
		$tagi=GetSiteTags($aid);
		$atags=$dsql->GetOne("SELECT total FROM `#@__webtagindex` WHERE tag LIKE '$tagi' ");
		if($atags=1){
		$dsql->ExecuteNoneQuery("DELETE FROM `#@__webtagindex` WHERE tag LIKE '$tagi' ");
		}
		else{
			$dsql->ExecuteNoneQuery("UPDATE `#@__webtagindex` SET total=total-1 WHERE tag LIKE '$tagi' ");
			}
    //删除HTML
    if($arcRow['arcrank']!=0 ||$arcRow['typeid']==0)
    {
        return TRUE;
    }
    $arcurl = GetFileUrl($arcRow['id'],$arcRow['typeid'],$arcRow['senddate'],'',1,
              $arcRow['arcrank'],$arcRow['namerule'],$arcRow['typedir'],0,'');
    if(!preg_match("#\?#", $arcurl))
    {
        $htmlfile = GetTruePath().str_replace($GLOBALS['cfg_basehost'],'',$arcurl);
        if(file_exists($htmlfile) && !is_dir($htmlfile))
        {
             @unlink($htmlfile);
             $arcurls = explode(".", $htmlfile);
             $sname = $arcurls[count($arcurls)-1];
             $fname = preg_replace("#(\.$sname)$#", "", $htmlfile);
             for($i=2;$i<=100;$i++)
             {
                   $htmlfile = $fname."_$i".".".$sname;
                   if(file_exists($htmlfile) && !is_dir($htmlfile)) @unlink($htmlfile);
                   else break;
             }
        }
    }
    //删除文本文件
    $filenameh = DEDEDATA."/textdata/".(ceil($aid/5000))."/{$aid}-".substr(md5($cfg_cookie_encode),0,16).".txt";
    return TRUE;
}

/**
 *  获取真实路径
 *
 * @return    string
 */
function GetTruePath()
{
    $truepath = $GLOBALS["cfg_basedir"];
    return $truepath;
}