<?php
@session_start();
ob_start();

/*if (!isset($_SESSION['USER']) && !isset($LOGIN_PAGE)) {
    header("Location:login.php");
    exit();
}*/
set_include_path(get_include_path().PATH_SEPARATOR.'/var/www/LIB/'.PATH_SEPARATOR.'/var/www/LIB/aspidenet/');
require_once('aspidenet.inc.php');
require_once('Smarty/Smarty.class.php');

$SYS = array();
define("BASE_URL", "http://amicidigiulita.it/");
define("TEST", false);
$SYS['TEST'] = TEST;

/*******************************************************************************
   DATABASE
*******************************************************************************/
function aspide_get_db() {
    $db = new DB('postgres9'); #ADONewConnection('postgres'); # eg 'mysql' or 'postgres'
    $db->Connetti("localhost", "amicidigiulita", "giuli", "amicidigiulita");
    return $db;
}
$db = aspide_get_db();

/*******************************************************************************
   SMARTY
*******************************************************************************/
$smarty = new Smarty;
$smarty->template_dir = "templates";
$smarty->compile_dir = "templates_c";
$smarty->cache_dir = "cache";
$smarty->config_dir = "configs";
#$smarty->debug = true;


require_once("costanti.inc.php");
require_once("funzioni.inc.php");

#----- AUTOLOAD ------------------------------------------------------------------------------------------------------------------
spl_autoload_register( 'autoloader' );
 
function autoloader( $class ) {
    #error_log ("Cerco '{$class}' in autoloader!", 0);
    $class = strtolower($class);
    if ( file_exists ('classes/' . $class . '.class.php' ) ){
        include_once('classes/' . $class . '.class.php' );
    }
}

#----- UTENTE --------------------------------------------------------------------------------------------------------------------
if (isset($_SESSION['USER']))
    $utente = new Utente($_SESSION['USER']);
else
    $utente = new Utente();
$code_utente = $utente->code; #temporaneo
$smarty->assign("utente", $utente);

#----- FACEBOOK ------------------------------------------------------------------------------------------------------------------
if (!is_user_logged_in()) {
    require_once('fb/facebook.php');

    $facebook_config = array(
        'appId' => '774258785965360',
        'secret' => 'd3e7307abcabc424920d1c3fc900ef64',
        'fileUpload' => false, // optional
        'allowSignedRequest' => false, // optional, but should be set to false for non-canvas apps
    );

    $facebook = new Facebook($facebook_config);
    $user = $facebook->getUser();

    $url_after = getVar("POST", "url", getVar("SESSION", "URL_AFTER"));
    if (strlen($url_after) == 0)
        $url_after = "index.php";

    if ($user) {
        try {
            $user_profile = $facebook->api('/me');
        } 
        catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }
        $_SESSION["FACEBOOK"] = $user_profile;
        
        if (try_login(array('fb_id' => $user_profile['id']))) {
            if (stripos($_SERVER['PHP_SELF'], $url_after) === false) {
                header("Location:".$url_after);
                exit();
            }
        }
        else {
            if (stripos($_SERVER['PHP_SELF'], "form-nuovo-iscritto.php") === false) {
                header("Location:form-nuovo-iscritto.php");
                exit();
            }
        }
    }
    else {
        $login_url = $facebook->getLoginUrl(array('scope' => 'public_profile,email,user_hometown,user_location', 'onlogin' => BASE_URL.$url_after));
    }
}

$smarty->assign("HOMEPAGE", homepage());
$smarty->assign("TEST", TEST);
$smarty->assign("BASE_URL", BASE_URL);
$smarty->assign("pubblicita", true);

$utils = new Utils();
?>
