<?php
namespace Passport\Src;

use Dever;
use Passport\Lib\Base;

class Login extends Base
{
    public function test()
    {
        return Dever::login(-1);
    }

    public function quit()
    {
        if ($this->info()) {
            $this->save->un(self::NAME);
        }

        $refer = Dever::input('refer', 1);

        $home = Dever::url('home', 'main');
        if ($refer == 2) {
            $refer = $home;
        } else {
            $refer = isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : Dever::url('home', 'main');
        }

        Dever::location($refer);
    }

    public function action()
    {
        $account = Dever::config('base', 'project')->account;

        $param['option_' . $account] = Dever::input($account);

        $password = sha1(Dever::input('password'));

        $user = Dever::load('passport/user-login', $param);

        if ($user && $password == $user['password']) {
            $this->save($user);

            $this->refer();
        } else {
            Dever::alert('您的账号或密码错误');
        }
    }

    public function get()
    {
        return $this->info();
    }

    public function url()
    {
        return Dever::url('login?' . $this->createRefer(), 'main');
    }

    public function location()
    {
        return Dever::location($this->url());
    }

    public function oauth($url = false)
    {
        if (!$url) {
            $url = Dever::url();
        }
        $link = Dever::url('get.request?refer=' . urlencode($url), 'oauth');

        return $link;
    }
}
