<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class LoginAction extends MemberAction
{
    public function run()
    {
        if (isset($_POST['mobile']))
        {
            $mobile = S::gp('mobile','P');
            $password = S::gp('password','P');
            $hidden = S::gp('hidden', 'P', 2);

            if (empty($mobile))
            {
                $this->_showMessage('请输入手机号');
            }
            if (empty($password))
            {
                $this->_showMessage('请输入密码');
            }
            
            $password = md5($password);
            $member = $this->db()->setQueryString('SELECT uid,username,password,mobile,newpm,iorange FROM '.Members::table().' WHERE mobile='.S::sqlEscape($mobile))->find(true);
            if (empty($member) || $password != $member['password'])
            {
                $this->_showMessage('登录失败，请检查手机号或者密码是否正确');
            }

            if ($hidden)
            {
                try
                {
                    Members::single()->hasMoney($member['uid'], 100);
                }
                catch (Exception $exce)
                {
                    throw new iSystemException(setting('creditname').'不够，不能隐身登录');
                }
                iCredits::single($member['uid'], $member['username'])->consume(100);
                Members::hiddenLogin($member['uid']);
            }
            
            $sql = 'UPDATE '.Members::table().' SET lastvisit='.CTIME.',lastip='.S::sqlEscape(ONLINE_IP).' WHERE uid='.$member['uid'];
            $this->db()->query($sql);
            $sid = $member['iorange'];
            if (!empty($_SESSION['user']['returnUrl']))
            {
                $returnUrl = urldecode($_SESSION['user']['returnUrl']);
                if (false !== strpos($returnUrl, 'sid='))
                {
                    $returnUrl = preg_replace('/sid=(\w+){0,}/is', 'sid='.$sid, $returnUrl);
                }
                else
                {
                    if (false === strpos($returnUrl, '?'))
                    {
                        $returnUrl .= '?sid='.$sid;
                    }
                    else
                    {
                        $tmp = explode('?', $returnUrl);
                        if (count($tmp) > 1 && !empty($tmp[1]))
                        {
                            $returnUrl .= '&amp;sid='.$sid;
                        }
                        else
                        {
                            $returnUrl .= 'sid='.$sid;
                        }
                    }
                }
                $message[] = '返回'.ilink('刚才页面', $returnUrl);
            }
            
            $feed = '{{'.$member['uid'].'}}圣驾光临'.setting('webname');
            iFeed::single()->create($feed, $member['uid'], 'member');

            if ($this->getController()->isWebRequest())
            {
                setCookieValue('iOrangeUser', strCode($member['uid']."\t".$member['password']."\t".$member['mobile']));
                if ($this->getController()->isAjaxRequest())
                {
                    die(json_encode(array('status' => 1, 'msg' => '恭喜，登陆成功！', 'msgCount' => $member['newpm'], 'userName' => $member['username'])));
                }
                
                $forwardUrl = url('index');
                if (!empty($returnUrl))
                {
                    $forwardUrl = $returnUrl;
                }
                $forwardUrl = cleanSid($forwardUrl);
                iforward($forwardUrl);
            }
            $GLOBALS['iorange_id'] = $member['uid'];
            $GLOBALS['sid'] = $member['iorange'];
            $path = setting('basehost') . setting('path');
			$message[] = "您已成功登陆,以下两种方法可实现-自动登录书签:<br/>1、请把".$path . ltrim(url('index', null, $sid),'/')."设为'".setting('webname')."'书签地址.<br/>2、点返回首页,把首页存为书签.两种方法都可下次从贵手机书签进入本站无需再次登陆<br/><a href='".url('index',null, $sid)."'>返回首页</a>";
			
			$this->showMessage($message);
        }

        $loginForm = iLoginForm::getInstance();

        $this->getController()->render('login', array('loginForm' => $loginForm));
    }
}
