( ′∀`)σ≡σ☆))Д′)レ(゚∀゚;)ヘ=З=З=Зε≡(ノ´_ゝ`)ノ
<?php
/*
* @author: Mahmud Ahsan (http://thinkdiff.net)
*/
//facebook application
$fbconfig['appid' ] = "";
$fbconfig['secret'] = "";
$fbconfig['baseurl'] = ""; //"http://thinkdiff.net/demo/newfbconnect1/php/sdk3/share.php";
//
if (isset($_GET['request_ids'])){
//user comes from invitation
//track them if you need
}
$user = null; //facebook user uid
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$user = $facebook->getUser();
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don�t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => $fbconfig['baseurl']
)
);
}
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
mysqlc();
$name = GetSQLValueString($user_profile['name'], "text");
$email = GetSQLValueString($user_profile['email'], "text");
$gender = GetSQLValueString($user_profile['gender'], "text");
$bio = GetSQLValueString($user_profile['bio'], "text");
$query = sprintf("SELECT * FROM users WHERE user_email = %s",$email);
$res = mysqli_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if(mysql_num_rows($res) == 0)
{
$md5pass = md5(rand(1000,9999));
$user_ip = $_SERVER['REMOTE_ADDR'];
$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$activ_code = rand(1000,9999);
$slug = preg_replace( '/[��""!?,.!@�$%^&*{};:()]+/', '', $name);
$slug = strtolower($slug);
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $slug);
$sql_insert = "INSERT into `users` (`catalogname`,`description`,`user_email`,`pwd`,`f4`,`date`,`users_ip`,`activation_code`,`user_name`,`slug`
)
VALUES
('$name','$email','$bio','$md5pass','$gender',now(),'$user_ip','$activ_code','$name','$slug'
)
";
/*
$iquery = sprintf("INSERT INTO users values('',%s,%s,%s,%s,'yes')",$name,$email,$gender,$bio);*/
$ires = mysqli_query(sprintf($sql_insert)) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
$_SESSION['euserid'] = $user_profile['email'];
$_SESSION['id'] = $user_profile['id'];
}
else
{
$row = mysql_fetch_array($res);
$_SESSION['euserid'] = $row['user_email'];
$_SESSION['id'] = $user_profile['id'];
}
} catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
d($e); // d is a debug function defined at the end of this file
$user = null;
}
}
//if user is logged in and session is valid.
if ($user){
//get user basic description
$userInfo = $facebook->api("/$user");
$userInfoname = GetSQLValueString($userInfo['name'], "text");
$userInfoemail = GetSQLValueString($userInfo['email'], "text");
$userInfogender = GetSQLValueString($userInfo['gender'], "text");
$userInfobio = GetSQLValueString($userInfo['bio'], "text");
//Retriving movies those are user like using graph api
try{
$movies = $facebook->api("/$user/movies");
}
catch(Exception $o){
d($o);
}
//update user's status using graph api
//http://developers.facebook.com/docs/reference/dialogs/feed/
if (isset($_GET['publish'])){
try {
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => "I love thinkdiff.net for facebook app development tutorials. :)",
'link' => 'http://ithinkdiff.net',
'picture' => 'http://thinkdiff.net/ithinkdiff.png',
'name' => 'iOS Apps & Games',
'description'=> 'Checkout iOS apps and games from iThinkdiff.net. I found some of them are just awesome!'
)
);
//as $_GET['publish'] is set so remove it by redirecting user to the base url
} catch (FacebookApiException $e) {
d($e);
}
$redirectUrl = $fbconfig['baseurl'] . '/share.php?success=1';
header("Location: $redirectUrl");
}
//update user's status using graph api
//http://developers.facebook.com/docs/reference/dialogs/feed/
if (isset($_POST['tt'])){
try {
$statusUpdate = $facebook->api("/$user/feed", 'post', array('message'=> $_POST['tt']));
} catch (FacebookApiException $e) {
d($e);
}
}
//fql query example using legacy method call and passing parameter
try{
$fql = "select name, hometown_location, sex, pic_square from user where uid=" . $user;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => ''
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>