<?php
/*
* 项目: iOrange(橘子)
* 作者: 离家出走(yzxh24)
* Email: yzxh24@gmail.com
* 主页: www.iorange.cc
*/
/* vim: set ts=4 sw=4: */
class PasswordAction extends MemberAction
{
    public function run()
    {
        if (isset($_POST['mobile']))
        {
            $mobile = S::gp('mobile','P');
            $oldPassword = S::gp('oldpassword','P');
            $password1 = S::gp('password1','P');
            $password2 = S::gp('password2','P');
            
            $member = $this->db()->select(array('mobile','password'))->from(Members::table())->where('uid='.$this->uid())->find(true);
            if ($mobile != $member['mobile'])
            {
                throw new iSystemException('您输入的手机号与注册时填写的不一致');
            }
            if (md5($oldPassword) != $member['password'])
            {
                throw new iSystemException('旧密码不正确');
            }
            if (empty($password1) || empty($password2))
            {
                throw new iSystemException('新密码不能为空');
            }
            if ($password1 != $password2)
            {
                throw new iSystemException('确认密码不一样，请检查两个新密码填写是否一致');
            }
            if (mb_strlen($password1,'utf8') > 20 || mb_strlen($password1,'utf8') < 6)
            {
                throw new iSystemException('密码长度不能超过20位且不能小于6位');
            }
            $newPassword = md5($password1);
            do
            {
                $newIorange = $this->_iorange();
                $iorangeCheck = $this->db()->getValue('SELECT COUNT(*) FROM '.Members::table().' WHERE iorange='.S::sqlEscape($newIorange));
            } while ($iorangeCheck);
            
            $this->db()->update(Members::table())->value(array('password'=>$newPassword,'iorange'=>$newIorange))->where('uid='.$this->uid())->q();
            $message[] = "恭喜您,密码修改成功!<br/>原先的自动登录书签已经失效,请点击下面的链接返回首页,将首页重新保存为书签即可实现自动登录!<br/><a href='".setting('basehost'). setting('path') . "index.php?sid=".$newIorange."'>返回首页</a>";
            $GLOBALS['sid'] = $newIorange;
            $this->showMessage($message);
        }
        $this->getController()->render('password');
    }
}