<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class VisitAction extends ForumAction
{
    public function run()
    {
        if (!IS_SUPER)
        {
            $this->NoPermission();
        }
        
        $uid = S::gp('uid','G',2);
        if (isset($_POST['member']))
        {
            $type = S::gp('type','P');
            $expiry = S::gp('expiry','P');
            $m = S::gp('member','P');
            $reason = trim(S::gp('reason','P'));

            if (empty($reason))
            {
                throw new iSystemException('请输入封号原因');
            }
            
            switch ($type)
            {
                case 'username':
                    $ban = 'm.username='.S::sqlEscape($m);
                    break;
                case 'uid':
                    $ban = 'm.uid='.S::sqlEscape(intval($m));
                    break;
            }
            $sql = "SELECT m.*, mf.*, u.gptype AS grouptype FROM ".Members::table()." AS m
                 	LEFT JOIN ".Memberfields::table()." mf ON mf.uid=m.uid
                 	LEFT JOIN ".UserGroups::table()." u ON u.groupid=m.groupid
                 	WHERE {$ban}";
            $member = $this->db()->setQueryString($sql)->find(true);
            if(!$member) 
            {
                throw new iSystemException('用户不存在');
            }
            if (isGM($member['uid']) || in_array($member['groupid'],array(4,5)))
            {
                throw new iSystemException('您无权操作此等级用户');
            }
            $expiry = $expiry ? CTIME + ($expiry * 24 * 3600) : 0;
            groupOperate($member,$expiry);
            
            if (isGM())
            {
                if ($_POST['cleanbbs'])
                {
                    cleanBbsPost($member['uid']);
                }
                if ($_POST['cleanforum'])
                {
                    //cleanForumPost($member['uid']);
                }
            }

            call_user_func_array(array(Members::single(),'ban'),array('member'=>$member,'reason'=>$reason,'uid'=>$this->uid(),'type'=>'visit','module' => 'all', 'id'=>0,'expiry'=>$expiry - CTIME));
            $message[] = '封号成功';
            $this->showMessage($message);
        }

        $this->getController()->render('visit', array(
        		'bookid'=>$this->id(),
                'uid' => $uid,
            )
        );
    }
}