diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index ceecc254e..5ad44a0e9 100644 --- a/fbsync/fbsync.php +++ b/fbsync/fbsync.php @@ -25,14 +25,79 @@ function fbsync_install() { register_hook('connector_settings', 'addon/fbsync/fbsync.php', 'fbsync_settings'); register_hook('connector_settings_post', 'addon/fbsync/fbsync.php', 'fbsync_settings_post'); register_hook('cron', 'addon/fbsync/fbsync.php', 'fbsync_cron'); + register_hook('follow', 'addon/fbsync/fbsync.php', 'fbsync_follow'); } function fbsync_uninstall() { unregister_hook('connector_settings', 'addon/fbsync/fbsync.php', 'fbsync_settings'); unregister_hook('connector_settings_post', 'addon/fbsync/fbsync.php', 'fbsync_settings_post'); unregister_hook('cron', 'addon/fbsync/fbsync.php', 'fbsync_cron'); + unregister_hook('follow', 'addon/fbsync/fbsync.php', 'fbsync_follow'); } +function fbsync_follow($a, &$contact) { + + logger("fbsync_follow: Check if contact is facebook contact. ".$contact["url"], LOGGER_DEBUG); + + if (!strstr($contact["url"], "://www.facebook.com") AND !strstr($contact["url"], "://facebook.com") AND !strstr($contact["url"], "@facebook.com")) + return; + + // contact seems to be a facebook contact, so continue + $nickname = preg_replace("=https?://.*facebook.com/([\w.]*).*=ism", "$1", $contact["url"]); + $nickname = str_replace("@facebook.com", "", $nickname); + + $uid = $a->user["uid"]; + + $access_token = get_pconfig($uid,'facebook','access_token'); + + $fql = array( + "profile" => "SELECT id, pic_square, url, username, name FROM profile WHERE username = '$nickname'", + "avatar" => "SELECT url FROM square_profile_pic WHERE id IN (SELECT id FROM #profile) AND size = 256"); + + $url = "https://graph.facebook.com/fql?q=".urlencode(json_encode($fql))."&access_token=".$access_token; + + $feed = fetch_url($url); + $data = json_decode($feed); + + $id = 0; + + logger("fbsync_follow: Query id for nickname ".$nickname, LOGGER_DEBUG); + + if (!is_array($data->data)) + return; + + $contactdata = new stdClass; + + foreach($data->data AS $query) { + switch ($query->name) { + case "profile": + $contactdata->id = number_format($query->fql_result_set[0]->id, 0, '', ''); + $contactdata->pic_square = $query->fql_result_set[0]->pic_square; + $contactdata->url = $query->fql_result_set[0]->url; + $contactdata->username = $query->fql_result_set[0]->username; + $contactdata->name = $query->fql_result_set[0]->name; + break; + + case "avatar": + $contactdata->pic_square = $query->fql_result_set[0]->url; + break; + } + } + + logger("fbsync_follow: Got contact for nickname ".$nickname." ".print_r($contactdata, true), LOGGER_DEBUG); + + // Create contact + fbsync_fetch_contact($uid, $contactdata, true); + + $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey + FROM `contact` WHERE `uid` = %d AND `alias` = '%s'", + intval($uid), + dbesc("facebook::".$contactdata->id)); + if (count($r)) + $contact["contact"] = $r[0]; +} + + function fbsync_settings(&$a,&$s) { if(! local_user()) @@ -61,12 +126,12 @@ function fbsync_settings(&$a,&$s) { $s .= ''; $s .= ''; $s .= '
'; -/* + $s .= '