<?php
  
class AdminLoginController extends Controller {
    public function __construct(){
        $this->beforeFilter('csrf', array('on' => 'post')); 
    }
    
    public function getIndex()
    {
                  
        if(Auth::backend()->check()){
            return Redirect::to('/admin/home');    
        }
        
        return View::make('/admin/login');
    }
    
    /**
     * Login.
     *
     * @return void
     */
    public function postIndex()
    {
        $rules = array (
            'username' => 'required',
            'password' => 'required',
            'g-recaptcha-response' => 'required'
        );

        $validator = Validator::make(Input::all(), $rules);
        
        if ($validator->fails()) {
            Input::flash(); 
            return Redirect::to('/admin/login')->withErrors($validator);
        }

        $recaptcha = Input::get('g-recaptcha-response');
        $recaptcha = $this->_checkRecaptcha($recaptcha);

        if($recaptcha) {
            $auth_data = array(
                'username' => strtolower(Input::get('username')),
                'password' => Input::get('password')
            );
            if (Auth::backend()->attempt(array('username' => $auth_data["username"], 'password' => $auth_data["password"]))) {
                return Redirect::intended('/admin/home');
            } else {
                Input::flash();
                return Redirect::to('/admin/login')->withErrors(array (
                    'error' => 'Invalid Credentials'
                ));
            }
        } else {
            Input::flash();
            return Redirect::to('/admin/login')->withErrors(array (
                'error' => 'Invalid Recaptcha'
            ));
        }
    }
   
    public function getLogout()
    {
        Auth::backend()->logout();
        Session::forget("backend");
        return Redirect::to('/admin/login');
    }

    private function _checkRecaptcha($response)
    {
        $parameters = http_build_query([
            'secret'   => Config::get('backoffice/recaptcha.private_key'),
            'remoteip' => app('request')->getClientIp(),
            'response' => $response,
        ]);
        $url           = 'https://www.google.com/recaptcha/api/siteverify?' . $parameters;
        $checkResponse = null;
        // prefer curl, but fall back to file_get_contents
        if ('curl' === Config::get('backoffice/recaptcha.driver') && function_exists('curl_version')) {
            $curl = curl_init($url);
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl, CURLOPT_TIMEOUT, Config::get('backoffice/recaptcha.options.curl_timeout', 1));
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            $checkResponse = curl_exec($curl);
        } else {
            $checkResponse = file_get_contents($url);
        }
        if (is_null($checkResponse) || empty( $checkResponse )) {
            return false;
        }
        $decodedResponse = json_decode($checkResponse, true);
        return $decodedResponse['success'];
    }

}
