<?php 

//if user denies access to your website, take him to your manual login page    
if($_GET['error']){
        header("Location: index.php");
        exit;
    }

    require_once('konfigurace.php');
    
    
    mysql_select_db($database_name, $database_connection);
    $sql = mysql_query("SELECT * FROM forum_konfigurace WHERE ID = '1';");
    $data = mysql_fetch_array($sql);
    $app_id =$data["FB_AppID"];
    $app_secret =$data["FB_AppSecret"];
    $my_url = "http://www.btbsro.sk/web/confirmlogin_fb_member.php";


    $code = $_GET['code'];

    $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . $app_id ."&redirect_uri=" . urlencode($my_url) . "&client_secret="
        . $app_secret . "&code=" . $code;
        
  
// request access token
//use curl and not file_get_contents()
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $token_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $access_token = curl_exec($ch);
    curl_close($ch);
    
    $graph_url = "https://graph.facebook.com/me?" . $access_token;

    // request user data using the access token
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $graph_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $temp_user = curl_exec($ch);
    curl_close($ch);

    //decode the json array to get user data
    $user = json_decode($temp_user);

    //store user data
    $u = $user->name;
    $e = $user->email;
    $fb_id = $user->id;
    $username = $user->username;

    //check if user has already signed up before
    $insert = true;
        $result = mysql_query("SELECT * FROM forum_uzivatele") or die(mysql_error());

        while($row = mysql_fetch_array($result)){
            //if username already exists, do not insert
            if(( $row['jmeno'] == $u) && ($row['userType'] == "facebook_user")){
                $insert = false;
            }
        }
    
    // if new user, insert user details in your mysql table
    if($insert){
    mysql_query("INSERT INTO forum_uzivatele (jmeno, heslo, email,admin,userType) VALUES('$u','','$e','0', 'facebook_user') ") or die(mysql_error()); 
    }

        //login user
        session_start();
        
        $_SESSION[jmeno_aut] = $u;
        $_SESSION[heslo_aut] = '';
        
        
        if((isset($_SESSION[jmeno_aut])) && (isset($_SESSION[heslo_aut]))) {
                $query = mysql_query("SELECT id,jmeno,admin FROM forum_uzivatele WHERE (heslo = '$_SESSION[heslo_aut]' AND jmeno = '$_SESSION[jmeno_aut]')") or die("Chyba databaze. Zkuste prosim akci opakovat pozdeji.");
                $row = mysql_fetch_array($query);
                if(empty($row)) {
                        session_destroy();
                } else {
                        $prihlaseni = TRUE;
                }
        }
       
       
                        
                                             

        //take user to his/her homepage
        header("Location: index.php");
        

?>