login-facebook.php 3.99 KB
<?php


require_once 'facebook/facebook.php';
require_once 'config/fbconfig.php';
require_once 'config/functions.php';




/*
$token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . APP_ID . "&redirect_uri=" . urlencode('http://extremstyle.ua/social/login-facebook.php')
       . "&client_secret=" . APP_SECRET . "&code=" . $_GET['code'];

//echo $token_url;
     $response = file_get_contents($token_url);
     $params = null;
     parse_str($response, $params);

     $graph_url = "https://graph.facebook.com/me?access_token=". $params['access_token'];

     $user = json_decode(file_get_contents($graph_url));

//print_r($user);





    if (!empty($user->id)) {



if(trim(@$user->id)!="")
$id_a = @$db->super_query("SELECT * FROM `zlo_users` WHERE `UID`='".@$user->id."' LIMIT 1;");
else @header("Location: /");


if(!@$id_a['id'] && trim(@$user->id)!=''){
$user2['name'] = iconv("UTF-8","cp1251",@$user->name);
$db->query("INSERT INTO `zlo_users` (`username`,`group`,`registered`,`mailing`,`UID`) VALUES ('".@$user2['name']."',2,'".date("Y-m-d H:i:s")."',1,'".trim(@$user->id)."');");
session_start();
$_SESSION['user'] = array('id'=>$db->insert_id(),'login'=>@$user->name);
@header("Location: /account/info/");
}else{
session_start();
$_SESSION['user'] = array('id'=>@$id_a['id'],'login'=>@$user->username);
@header("Location: /account/info/");
}



}


*/

















require_once 'facebook/facebook.php';
require_once 'config/fbconfig.php';
require_once 'config/functions.php';

$facebook = new Facebook(array(
            'appId' => APP_ID,
            'secret' => APP_SECRET,
            'cookie' => true
        ));

$session = $facebook->getUser();
$user = $session;


if (!empty($session)) {
    # Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
    try {
        $uid = $facebook->getUser();
        $user_profile = $facebook->api('/me');
		//print_r($user);
    } catch (Exception $e) {




    }


//print_r($user);
//exit;

 function password(){
	$chars="qazxswedcvfrtgbnhyujmkiolp1234567890QAZXSWEDCVFRTGBNHYUJMKIOLP"; 
	$max=10; 
	$size=StrLen($chars)-1; 
	$password=null; 

    while($max--) 
    $password.=$chars[rand(0,$size)];

	return $password;	
 }

    if (!empty($user)) {



if(trim(@$user_profile['id'])!="")
$id_a = @$db->super_query("SELECT * FROM `zlo_users` WHERE `UID`='".@$user_profile['id']."' LIMIT 1;");
else @header("Location: /");
//echo "1";




if(!@$id_a['id'] && trim(@$user_profile['id'])!=''){
$user_profile['name'] = iconv("UTF-8","cp1251",@$user_profile['name']);
$u = explode(' ',$user_profile['name']);
$u_arr = array();
foreach($u as $a){
if(strlen(trim($a))>0)$u_arr[] = trim($a);
}
$user_profile['name'] = implode(' ',$u_arr);
$pass = password();
$db->query("INSERT INTO `zlo_users` (`username`,login,pass,`group`,`registered`,`mailing`,`UID`) VALUES ('".@$user_profile['name']."','".@$user_profile['name']."','".$pass."',1,'".date("Y-m-d H:i:s")."',1,'".trim(@$user_profile['id'])."');");
session_start();
$_SESSION['user'] = array('id'=>$db->insert_id(),'login'=>@$user_profile['name'],'psw'=>$pass);
@header("Location: /account/info/");
}else{
session_start();
$_SESSION['user'] = array('id'=>@$id_a['id'],'login'=>@$id_a['username'],'psw'=>@$id_a['pass']);
@header("Location: /account/info/");
}



        $username = $user_profile['name'];
        $user = new User();
        ////$userdata = $user->checkUser($uid, 'facebook', $username);
        if(!empty($userdata)){
            //session_start();
            //$_SESSION['id'] = $userdata['id'];
 $_SESSION['oauth_id'] = $uid;

            //$_SESSION['username'] = $userdata['username'];
            //$_SESSION['oauth_provider'] = $userdata['oauth_provider'];
            //header("Location: home.php");
        }
    } else {
        # For testing purposes, if there was an error, let's kill the script
        die("There was an error.");
    }
} else {
    # There's no active session, let's generate one
    $login_url = $facebook->getLoginUrl();
    header("Location: " . $login_url);
}
?>