<?php
/**
 *
 * 關於文章權限設置的說明
 * 文章權限設置限制形式如下：
 * 如果指定了會員等級，那麼必須到達這個等級才能瀏覽
 * 如果指定了金幣，瀏覽時會扣指點的點數，並保存記錄到用戶業務記錄中
 * 如果兩者同時指定，那麼必須同時滿足兩個條件
 *
 * @version        $Id: view.php 1 15:38 2010年7月8日Z tianya $
 * @package        DedeCMS.Site
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.dedecms.com/usersguide/license.html
 * @link           http://www.dedecms.com
 */
require_once(dirname(__FILE__)."/../include/common.inc.php");
require_once(DEDEINC.'/arc.archives.class.php');

$t1 = ExecTime();

if(empty($okview)) $okview = '';
if(isset($arcID)) $aid = $arcID;
if(!isset($dopost)) $dopost = '';

$arcID = $aid = (isset($aid) && is_numeric($aid)) ? $aid : 0;
if($aid==0) die(" Request Error! ");

$arc = new Archives($aid);
if($arc->IsError) ParamError();


//檢查閱讀權限
$needMoney = $arc->Fields['money'];
$needRank = $arc->Fields['arcrank'];

//設置了權限限制的文章
//arctitle msgtitle moremsg
if($needMoney>0 || $needRank>1)
{
    require_once(DEDEINC.'/memberlogin.class.php');
    $cfg_ml = new MemberLogin();
    $arctitle = $arc->Fields['title'];
    /*
    $arclink = GetFileUrl($arc->ArcID,$arc->Fields["typeid"],$arc->Fields["senddate"],
                             $arc->Fields["title"],$arc->Fields["ismake"],$arc->Fields["arcrank"]);
    */                        
    $arclink = $cfg_phpurl.'/view.php?aid='.$arc->ArcID;                         
    $arcLinktitle = "<a href=\"{$arclink}\"><u>".$arctitle."</u></a>";
    
    $description =  $arc->Fields["description"];
    $pubdate = GetDateTimeMk($arc->Fields["pubdate"]);
    
    //會員級別不足
    if(($needRank>1 && $cfg_ml->M_Rank < $needRank && $arc->Fields['mid']!=$cfg_ml->M_ID))
    {
        $dsql->Execute('me' , "SELECT * FROM `#@__arcrank` ");
        while($row = $dsql->GetObject('me'))
        {
            $memberTypes[$row->rank] = $row->membername;
        }
        $memberTypes[0] = "遊客或沒權限會員";
        $msgtitle = "你沒有權限瀏覽文檔：{$arctitle} ！";
        $moremsg = "這篇文檔需要 <font color='red'>".$memberTypes[$needRank]."</font> 才能訪問，你目前是：<font color='red'>".$memberTypes[$cfg_ml->M_Rank]."</font> ！";
        include_once(DEDETEMPLATE.'/plus/view_msg.htm');
        exit();
    }

    //需要金幣的情況
    if($needMoney > 0  && $arc->Fields['mid'] != $cfg_ml->M_ID)
    {
        $sql = "SELECT aid,money FROM `#@__member_operation` WHERE buyid='ARCHIVE".$aid."' AND mid='".$cfg_ml->M_ID."'";
        $row = $dsql->GetOne($sql);
        //未購買過此文章
        if(!is_array($row))
        {
            if($cfg_ml->M_Money=='' || $needMoney > $cfg_ml->M_Money)
            {
                    $msgtitle = "你沒有權限瀏覽文檔：{$arctitle} ！";
                    $moremsg = "這篇文檔需要 <font color='red'>".$needMoney." 金幣</font> 才能訪問，你目前擁有金幣：<font color='red'>".$cfg_ml->M_Money." 個</font> ！";
                    include_once(DEDETEMPLATE.'/plus/view_msg.htm');
                    $arc->Close();
                    exit();
            }
            else
            {
                if($dopost=='buy')
                {
                    $inquery = "INSERT INTO `#@__member_operation`(mid,oldinfo,money,mtime,buyid,product,pname)
                              VALUES ('".$cfg_ml->M_ID."','$arctitle','$needMoney','".time()."', 'ARCHIVE".$aid."', 'archive',''); ";
                    if($dsql->ExecuteNoneQuery($inquery))
                    {
                        $inquery = "UPDATE `#@__member` SET money=money-$needMoney WHERE mid='".$cfg_ml->M_ID."'";
                        if(!$dsql->ExecuteNoneQuery($inquery))
                        {
                            showmsg('購買失敗, 請返回', -1);
                            exit;
                        }
                        #api{{
                        if(defined('UC_APPID'))
                        {
                            include_once DEDEROOT.'/api/uc.func.php';
                            $row = $dsql->GetOne("SELECT `scores`,`userid` FROM `#@__member` WHERE `mid`='".$cfg_ml->M_ID."'");
                            uc_credit_note($row['userid'],-$needMoney,'money');
                        }
                        #/aip}}
    
                        showmsg('購買成功，購買扣點不會重扣金幣，謝謝！', '/plus/view.php?aid='.$aid);
                        exit;
                    } else {
                        showmsg('購買失敗, 請返回', -1);
                        exit;
                    }
                }
                
                $msgtitle = "扣金幣購買閱讀！";
                $moremsg = "閱讀該文檔內容需要付費！<br>這篇文檔需要 <font color='red'>".$needMoney." 金幣</font> 才能訪問，你目前擁有金幣 <font color='red'>".$cfg_ml->M_Money." </font>個！<br>確認閱讀請點 [<a href='/plus/view.php?aid=".$aid."&dopost=buy' target='_blank'>確認付點閱讀</a>]" ;
                include_once($cfg_basedir.$cfg_templets_dir."/plus/view_msg.htm");
                $arc->Close();
                exit();
            }
        }
    }//金幣處理付處理
}

$arc->Display();