<?php

class AuthController extends baseController
{
    function validURI($uri)
    {
        return false === strpos("http://", $uri);
    }

    function index() {
    	if ($_POST) {
    		$authModel = new authModel();

            //$redirect_to = get('siteroot');
            //$redirect_to = get('siteroot') . 'home/';
            $redirect_to = get('siteroot') . '';
			if (!empty($_POST['redirect_me']) && $this->validURI($_POST['redirect_me']))
                $redirect_to = rtrim(get('siteroot'), "/") . "/" . ltrim($_POST['redirect_me'], "/");

    		if ($authModel->login($_POST['email'], $_POST['password'], isset($_POST['remember']))) {
				if ($_SESSION['user']['is_admin']) {
					header("Location: /clients/dash");
					exit;
				} elseif ($_SESSION['user']['usertype']="Team") {
					header("Location: /clients/dash");
					exit;
				} else {
					//header("Location: " . $redirect_to);
					header("Location: /profile/edit");
					//header("Location: /profile/edit");
					exit;
				}                
    		} else {
    			$msg = "Wrong password";
    			require 'views/auth/login.tpl.php';
    		}
    	}
        
        // IF GET
		// https://chattrboxx.com/auth/login?email=demo123@demo.com&password=demo123
		// https://chattrboxx.com/auth/login?email=demo@demo.com&password=demo123
        if($_GET) {
            $authModel = new authModel();
            //$redirect_to = get('siteroot') . 'manage/';
			$redirect_to = get('siteroot') . 'messages';
			if (!empty($_POST['redirect_me']) && $this->validURI($_POST['redirect_me']))
				$redirect_to = rtrim(get('siteroot'), "/messages/") . "/" . ltrim($_POST['redirect_me'], "/messages/");

            if ($authModel->login($_GET['email'], $_GET['password'], isset($_GET['remember']))) {
                header("Location: " . $redirect_to);
                    exit;     
            } else {
                $msg = "Something went wrong, please go back and try to access this page again or let support know. Thanks! - ChattrBox Team";
                require 'views/auth/login.tpl.php';
            }       
        }      
        // END OF GET
        require 'views/auth/login.tpl.php';   
    }
    
    function loginas()
    {
    	if	(!empty($_POST['log_as']) && $_SESSION['user']['is_admin'] == 2) {
    		$authModel = new authModel();
    		
    		if ($authModel->loginAs($_POST['log_as'])) {
    			header("Location: " . $_SERVER['HTTP_REFERER']);
                exit;
    		}
    	}
    }
    
    function loginn() {
    	if ($_POST) {
    		$authModel = new authModel();

            //$redirect_to = get('siteroot');
            //$redirect_to = get('siteroot') . 'home/';
            $redirect_to = get('siteroot') . '';
			if (!empty($_POST['redirect_me']) && $this->validURI($_POST['redirect_me']))
                $redirect_to = rtrim(get('siteroot'), "/") . "/" . ltrim($_POST['redirect_me'], "/");

    		if ($authModel->login($_POST['email'], $_POST['password'], isset($_POST['remember']))) {
				if ($_SESSION['user']['is_admin']) {
					header("Location: /clients/dash");
					exit;
				} elseif ($_SESSION['user']['usertype']="Team") {
					header("Location: /clients/dash");
					exit;
				} else {
					//header("Location: " . $redirect_to);
					header("Location: /profile/edit");
					//header("Location: /profile/edit");
					exit;
				}                
    		} else {
    			$msg = "Wrong password";
    			require 'views/auth/login.tpl.php';
    		}
    	}
        
        // IF GET
		// https://base.basepages.com/auth/loginn?email=demo123@demo.com&password=demo123
		// https://base.basepages.com/auth/loginn?email=demo@demo.com&password=demo123
        if($_GET) {
            $authModel = new authModel();
            //$redirect_to = get('siteroot') . 'manage/';
			$redirect_to = get('siteroot') . 'profile/edit';
			if (!empty($_POST['redirect_me']) && $this->validURI($_POST['redirect_me']))
				$redirect_to = rtrim(get('siteroot'), "/profile/edit") . "/" . ltrim($_POST['redirect_me'], "/profile/edit");

            if ($authModel->login($_GET['email'], $_GET['password'], isset($_GET['remember']))) {
                header("Location: " . $redirect_to);
                    exit;     
            } else {
                $msg = "Something went wrong, please go back and try to access this page again or let support know. Thanks! - ChattrBox Team";
                require 'views/auth/login.tpl.php';
            }       
        }      
        // END OF GET
        require 'views/auth/login.tpl.php';   
    }    

	function register() {
		if ($_POST) {
            if (!empty($_POST['location_lat']) && !empty($_POST['location_lng'])) {
                // make sure we save last location as well.
                $_POST['location_last_lat'] = $_POST['location_lat'];
                $_POST['location_last_lng'] = $_POST['location_lng'];
            }

			$authModel = new authModel();
			//$userModel = new userModel();
            //$projectModel = new projectModel();
            $usertypeModel = new usertypeModel();
            //$categoryModel = new categoryModel();
            
            //$usertypes = $user_typeModel->getUsertypes();        
			//$categories = $categoryModel->getList();
            
			$user_id = $authModel->register($_POST);
			//$userModel->assignCard($user_id);
						
            $authModel->login($_POST['email'], $_POST['password']);
			header('Location: /?reg_ok');
			exit;
		}
		require 'views/auth/register.tpl.php';
    }

    function forgot() {
    	$authModel = new authModel();

    	if ($_POST) {
    		if (isset($_POST['email'])) {
	    		$u = $authModel->preparePasswordReset($_POST['email']);

                if (false === $u) {
                    // wrong email address provided.
                    header("Location: /auth/forgot/?msg=" . urlencode("No account is linked to this email address."));
                    exit;
                }

	    		$reset_link = get('siteroot') . 'auth/forgot/' . $u['reset_code'];
	    		require('views/emails/forgotPwd.eml.php');
	    		mail($u['email'], $subject, $body);
	    		$notifSent = 1;
    		} elseif (isset($_POST['password'])) {
    			if ($authModel->resetPassword($_POST['password'], $_POST['code'])) {
    				header("Location: " . get('siteroot') . 'auth/login/?msg=' . urlencode("Your password has been updated."));
    			} else {
    				echo "Failed to reset pwd";
    			}
    		}
    	}
    	if (isset($this->registry->args[2])) {
    		$code = $this->registry->args[2];
    		if ($authModel->checkCode($code)) {
    			$codeCorrect = 1;
    		}
    	}
    	require 'views/auth/forgot.tpl.php';
    }

    function loginFacebook() {
        $authModel = new authModel();
        $authModel->loginFacebook();
        header("Location: /");
        exit;
    }

    function logout() {
    	unset($_SESSION['loggedin']);
        unset($_SESSION['user_id']);
        unset($_SESSION['user']);
		unset($_SESSION['user']['id']);
        header("Location: /");
    	// $this->index();
    }
}
?>