+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];
+}
+
+