<?php
if(!defined('DEDEINC')) exit('Request Error!');
/**
 * 軟件相關標籤
 *
 * @version        $Id:softlinks.lib.php 1 9:33 2010年7月8日Z tianya $
 * @package        DedeCMS.Taglib
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */

/**
 *  獲取軟件連接
 *
 * @access    public
 * @param     string  $fvalue  默認值
 * @param     object  $ctag  解析標籤
 * @param     object  $refObj  引用對像
 * @param     bool  $downloadpage  下載頁面
 * @return    string
 */
function ch_softlinks($fvalue, &$ctag, &$refObj, $fname='', $downloadpage=false)
{
    global $dsql;
    $row = $dsql->GetOne("SELECT * FROM `#@__softconfig` ");
    $phppath = $GLOBALS['cfg_phpurl'];
    $downlinks = '';
    if($row['downtype']!='0' && !$downloadpage)
    {
        $tempStr = GetSysTemplets("channel_downlinkpage.htm");
        $links = $phppath."/download.php?open=0&aid=".$refObj->ArcID."&cid=".$refObj->ChannelID;
        $downlinks = str_replace("~link~", $links, $tempStr);
        return $downlinks;
    }
    else
    {
        return ch_softlinks_all($fvalue, $ctag, $refObj, $row);
    }
}

//讀取所有鏈接地址
function ch_softlinks_all($fvalue, &$ctag, &$refObj, &$row)
{
    global $dsql, $cfg_phpurl;
    $phppath = $cfg_phpurl;
    $dtp = new DedeTagParse();
    $dtp->LoadSource($fvalue);
    if( !is_array($dtp->CTags) )
    {
        $dtp->Clear();
        return "無鏈接信息！";
    }
    $tempStr = GetSysTemplets('channel_downlinks.htm');
    $downlinks = '';
    foreach($dtp->CTags as $ctag)
    {
        if($ctag->GetName()=='link')
        {
            $link = trim($ctag->GetInnerText());
            $serverName = trim($ctag->GetAtt('text'));
            $islocal = trim($ctag->GetAtt('islocal'));
            
            //分析本地鏈接
            if(!isset($firstLink) && $islocal==1) $firstLink = $link;
            if($islocal==1 && $row['islocal'] != 1) continue;
    
            //支持http,迅雷下載,ftp,flashget
            if(!preg_match("#^http:\/\/|^thunder:\/\/|^ftp:\/\/|^flashget:\/\/#i", $link))
            {
                    $link = $GLOBALS['cfg_mainsite'].$link;
            }
            $downloads = getDownloads($link);
            $uhash = substr(md5($link), 0, 24);
            if($row['gotojump']==1)
            {
                $link = $phppath."/download.php?open=2&id={$refObj->ArcID}&uhash={$uhash}";
            }
            $temp = str_replace("~link~",$link,$tempStr);
            $temp = str_replace("~server~",$serverName,$temp);
            $temp = str_replace("~downloads~",$downloads,$temp);
            $downlinks .= $temp;
        }
    }
    $dtp->Clear();
    //獲取鏡像功能的地址
    //必須設置為：[根據本地地址和服務器列表自動生成] 的情況
    $linkCount = 1;
    if($row['ismoresite']==1 && $row['moresitedo']==1 && trim($row['sites'])!='' && isset($firstLink))
    {
        $firstLink = preg_replace("#http:\/\/([^\/]*)\/#i", '/', $firstLink);
        $row['sites'] = preg_replace("#[\r\n]{1,}#", "\n", $row['sites']);
        $sites = explode("\n", trim($row['sites']));
        foreach($sites as $site)
        {
            if(trim($site)=='') continue;
            list($link,$serverName) = explode('|', $site);
            $link = trim( preg_replace("#\/$#", "", $link) ).$firstLink;
            $downloads = getDownloads($link);
            $uhash = substr(md5($link), 0, 24);
            if($row['gotojump']==1)
            {
                $link = $phppath."/download.php?open=2&id={$refObj->ArcID}&uhash={$uhash}";
            }
            $temp = str_replace("~link~", $link, $tempStr);
            $temp = str_replace("~server~", $serverName, $temp);
            $temp = str_replace("~downloads~", $downloads, $temp);
            $downlinks .= $temp;
        }
    }
    return $downlinks;
}

function getDownloads($url)
{
    global $dsql;
    $hash = md5($url);
    $query = "SELECT downloads FROM `#@__downloads` WHERE hash='$hash' ";
    $row = $dsql->GetOne($query);
    if(is_array($row))
    {
        $downloads = $row['downloads'];
    }
    else
    {
        $downloads = 0;
    }
    return $downloads;
}