<?php
/*
   +----------------------------------------------------------------------+
   | Lock Global Page                                                     |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Control security access for every page displayed                     |
   +----------------------------------------------------------------------+
*/

require_once 'Authenticate.inc.php';

#Allow the requireAdmin function to be called after
#this page is included
function requireAdmin(){
	global $_INFO;
	if ($_INFO['admin'] !== '1'){
		header('Location: '.$CONFIG['full_site']);
		exit(0);
	}
}

$show_page = true;
if (isset($_GET['url'])) { $url = $_GET['url']; } else { $url = ''; }

#If authenticating through a verid
if (isset($_GET['verid'])) {
	$a = new Authenticate($_GET['verid']);
        if ($a->setUniqueID()){
                if (isset($_GET['url'])){
                        header('Location: '.$_GET['url']);
                        exit(0);
                } else {
			header('Location: /');
			exit(0);
                }
	#If there's an invalid varid specified ...
        } else {
		header('Location: /');
		exit(0);
        }
}

#If an id is specified in the url
else if (isset($_GET['id'])) {
	$a = new Authenticate($_GET['id']);
	if ($a->isValid($_GET['url'],$_GET['ip'])) {
		$_USER = $a->getAll();
		print serialize($_USER);
		exit(0);
	}
}

#If an ip is specified in the url.  This is basically insecure since
#it allows for multiple machines from the same ip to be authenticated.
#Only use this when absolutely necessary (winamp streaming)
else if (isset($_GET['ip'])) {
	if (Authenticate::isAuthenticatedIP($_GET['ip'])) {
                print 'valid';
		exit(0);
	}
}
	
#Check if a user is already logged in
else if (isset($_COOKIE['uniqueid']) AND strlen($_COOKIE['uniqueid']) == 32) {

	$show_page = false;
	$uniqueid = $_COOKIE['uniqueid'];
	$a = new Authenticate($uniqueid);

	if (isset($_GET['logout'])) {
	        $a->unsetUniqueID();
                header('Location:  '.$CONFIG['full_site']);
                exit(0);
        }

	else if ($a->isValid()){

		#Assign the global variable
		$_USER = $a->getAll();

		#Load Levels List
		$_LEVELS = $a->getLevelsList();

		if (isset($_GET['info'])) {
                        print serialize($_USER);
                        exit(0);
                }

		if (isset($_GET['url'])){
			header('Location: '.$_GET['url']);
			exit(0);
		} 
	
	} else {

		$a->unsetUniqueID();

		if ($_SERVER['HTTP_HOST'] !== $CONFIG['domain']) {
			header('Location:  '.$CONFIG['full_site'].'?url=http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
			exit(0);
		} else {
			$show_page = true;
		}

	}
}

#User is not logged in, authenticate his email
else if (isset($_POST['verifyme'])) {
	$email = $_POST['email'];
	$a = new Authenticate($email);
	if ($a->notAbused(false,$url) AND $a->setUniqueID() AND $a->isValid()){ #It's important that notAbused() goes first to prevent pass thrus
		if (isset($_GET['url'])){
                        header('Location: '.$url);
                        exit(0);
		} else if (isset($_SERVER['REQUEST_URI'])){
                        header('Location: '.$_SERVER['REQUEST_URI']);
                        exit(0);
                } else {
                        $info = $a->getAll();
                        $smarty->assign('info',$info);
			header('Location: /');
                        exit(0);
                }
	} else {
	}
}

elseif (isset($CONFIG['anonymous_ok']) AND !$_USER['level']) {
	$_USER['level'] = 1;
	$_USER['first'] = 'Anonymous';
} else {

	if ($_SERVER['SERVER_NAME'] !== $CONFIG['domain']){
		header('Location: '.$CONFIG['full_site'].'?url=http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
		exit(0);
	}
}

if ($show_page AND !isset($_USER) AND !isset($CONFIG['anonymous_ok'])){
	$predicted_name = Authenticate::getNameByIP();
	$predicted_name = $predicted_name['first'].' '.$predicted_name['last'];
	$smarty->assign('predicted_name',$predicted_name);

	if (isset($ERROR)) {
		$smarty->assign('ERROR',$ERROR);
	}
	$smarty->display($CONFIG['smarty_templates'].'/authenticate.tpl');

	exit(0);
}
?>
