+/**
+ * @param int $uid
+ * @param string $access_token
+ * @param array $persons
+ */
+function fb_get_friends_sync_new($uid, $access_token, $persons) {
+ $persons_todo = array();
+ foreach ($persons as $person) {
+ $link = 'http://facebook.com/profile.php?id=' . $person->id;
+
+ $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
+ intval($uid),
+ dbesc($link)
+ );
+
+ if (count($r) == 0) {
+ logger('fb_get_friends: new contact found: ' . $link, LOGGER_DEBUG);
+ $persons_todo[] = $person;
+ }
+
+ if (count($persons_todo) > 0) fb_get_friends_sync_full($uid, $access_token, $persons_todo);
+ }
+}
+
+/**
+ * @param int $uid
+ * @param object $contact
+ */
+function fb_get_friends_sync_parsecontact($uid, $contact) {
+ $contact->link = 'http://facebook.com/profile.php?id=' . $contact->id;
+
+ // If its a page then set the first name from the username
+ if (!$contact->first_name and $contact->username)
+ $contact->first_name = $contact->username;
+
+ // check if we already have a contact
+
+ $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
+ intval($uid),
+ dbesc($contact->link)
+ );
+
+ if(count($r)) {
+
+ // check that we have all the photos, this has been known to fail on occasion
+
+ if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro'])) {
+ require_once("Photo.php");
+
+ $photos = import_profile_photo('https://graph.facebook.com/' . $contact->id . '/picture', $uid, $r[0]['id']);
+
+ q("UPDATE `contact` SET `photo` = '%s',
+ `thumb` = '%s',
+ `micro` = '%s',
+ `name-date` = '%s',
+ `uri-date` = '%s',
+ `avatar-date` = '%s'
+ WHERE `id` = %d LIMIT 1
+ ",
+ dbesc($photos[0]),
+ dbesc($photos[1]),
+ dbesc($photos[2]),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ intval($r[0]['id'])
+ );
+ }
+ return;
+ }
+ else {
+
+ // create contact record
+ q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
+ `name`, `nick`, `photo`, `network`, `rel`, `priority`,
+ `writable`, `blocked`, `readonly`, `pending` )
+ VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
+ intval($uid),
+ dbesc(datetime_convert()),
+ dbesc($contact->link),
+ dbesc(normalise_link($contact->link)),
+ dbesc(''),
+ dbesc(''),
+ dbesc($contact->id),
+ dbesc('facebook ' . $contact->id),
+ dbesc($contact->name),
+ dbesc(($contact->nickname) ? $contact->nickname : strtolower($contact->first_name)),
+ dbesc('https://graph.facebook.com/' . $contact->id . '/picture'),
+ dbesc(NETWORK_FACEBOOK),
+ intval(CONTACT_IS_FRIEND),
+ intval(1),
+ intval(1)
+ );
+ }
+
+ $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc($contact->link),
+ intval($uid)
+ );
+
+ if(! count($r)) {
+ return;
+ }
+
+ $contact_id = $r[0]['id'];
+
+ require_once("Photo.php");
+
+ $photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
+
+ q("UPDATE `contact` SET `photo` = '%s',
+ `thumb` = '%s',
+ `micro` = '%s',
+ `name-date` = '%s',
+ `uri-date` = '%s',
+ `avatar-date` = '%s'
+ WHERE `id` = %d LIMIT 1
+ ",
+ dbesc($photos[0]),
+ dbesc($photos[1]),
+ dbesc($photos[2]),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ intval($contact_id)
+ );
+}
+
+/**
+ * @param int $uid
+ * @param string $access_token
+ * @param array $persons
+ */
+function fb_get_friends_sync_full($uid, $access_token, $persons) {
+ if (count($persons) == 0) return;
+ $nums = Ceil(count($persons) / 50);
+ for ($i = 0; $i < $nums; $i++) {
+ $batch_request = array();
+ for ($j = $i * 50; $j < ($i+1) * 50 && $j < count($persons); $j++) $batch_request[] = array('method'=>'GET', 'relative_url'=>$persons[$j]->id);
+ $s = post_url('https://graph.facebook.com/', array('access_token' => $access_token, 'batch' => json_encode($batch_request)));
+ if($s) {
+ $results = json_decode($s);
+ logger('fb_get_friends: info: ' . print_r($results,true), LOGGER_DATA);
+ foreach ($results as $contact) {
+ if ($contact->code != 200) logger('fb_get_friends: not found: ' . print_r($contact,true), LOGGER_DEBUG);
+ else fb_get_friends_sync_parsecontact($uid, json_decode($contact->body));
+ }
+ }
+ }
+}