- if(!count($r)) {
- // 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->url),
- dbesc(normalise_link($contact->url)),
- dbesc($contact->username."@facebook.com"),
- dbesc("facebook::".$contact->id),
- dbesc(''),
- dbesc("facebook::".$contact->id),
- dbesc($contact->name),
- dbesc($contact->username),
- dbesc($avatarpicture),
- dbesc(NETWORK_FACEBOOK),
- intval(CONTACT_IS_FRIEND),
- intval(1),
- intval(1)
- );
-
- $r = q("SELECT * FROM `contact` WHERE `alias` = '%s' AND `uid` = %d LIMIT 1",
- dbesc("facebook::".$contact->id),
- intval($uid)
- );
-
- if(! count($r))
- return(false);
-
- $contact_id = $r[0]['id'];
-
- $g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1",
- intval($uid)
- );
-
- if($g && intval($g[0]['def_gid'])) {
- require_once('include/group.php');
- group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
- }
-
- require_once("Photo.php");
-
- $photos = import_profile_photo($avatarpicture,$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",
- dbesc($photos[0]),
- dbesc($photos[1]),
- dbesc($photos[2]),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- intval($contact_id)
- );
- } else {
- // update profile photos once every 12 hours as we have no notification of when they change.
- $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
-
- // 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']) || ($update_photo)) {
-
- logger("fbsync_fetch_contact: Updating contact ".$contact->username, LOGGER_DEBUG);
-
- require_once("Photo.php");
-
- $photos = import_profile_photo($avatarpicture, $uid, $r[0]['id']);
-
- q("UPDATE `contact` SET `photo` = '%s',
- `thumb` = '%s',
- `micro` = '%s',
- `name-date` = '%s',
- `uri-date` = '%s',
- `avatar-date` = '%s',
- `url` = '%s',
- `nurl` = '%s',
- `addr` = '%s',
- `name` = '%s',
- `nick` = '%s'
- WHERE `id` = %d",
- dbesc($photos[0]),
- dbesc($photos[1]),
- dbesc($photos[2]),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- dbesc(datetime_convert()),
- dbesc($contact->url),
- dbesc(normalise_link($contact->url)),
- dbesc($contact->username."@facebook.com"),
- dbesc($contact->name),
- dbesc($contact->username),
- intval($r[0]['id'])
- );
- }
- }
- return($r[0]["id"]);
+ if(!count($r)) {
+ // 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->url),
+ dbesc(normalise_link($contact->url)),
+ dbesc($contact->username."@facebook.com"),
+ dbesc("facebook::".$contact->id),
+ dbesc($contact->id),
+ dbesc("facebook::".$contact->id),
+ dbesc($contact->name),
+ dbesc($contact->username),
+ dbesc($avatarpicture),
+ dbesc(NETWORK_FACEBOOK),
+ intval(CONTACT_IS_FRIEND),
+ intval(1),
+ intval(1)
+ );
+
+ $r = q("SELECT * FROM `contact` WHERE `alias` = '%s' AND `uid` = %d LIMIT 1",
+ dbesc("facebook::".$contact->id),
+ intval($uid)
+ );
+
+ if(! count($r))
+ return(false);
+
+ $contact_id = $r[0]['id'];
+
+ $g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1",
+ intval($uid)
+ );
+
+ if($g && intval($g[0]['def_gid'])) {
+ require_once('include/group.php');
+ group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
+ }
+
+ require_once("Photo.php");
+
+ $photos = import_profile_photo($avatarpicture,$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",
+ dbesc($photos[0]),
+ dbesc($photos[1]),
+ dbesc($photos[2]),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ intval($contact_id)
+ );
+ } else {
+ // update profile photos once every 12 hours as we have no notification of when they change.
+ $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
+
+ // 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']) || ($update_photo)) {
+
+ logger("fbsync_fetch_contact: Updating contact ".$contact->username, LOGGER_DEBUG);
+
+ require_once("Photo.php");
+
+ $photos = import_profile_photo($avatarpicture, $uid, $r[0]['id']);
+
+ q("UPDATE `contact` SET `photo` = '%s',
+ `thumb` = '%s',
+ `micro` = '%s',
+ `name-date` = '%s',
+ `uri-date` = '%s',
+ `avatar-date` = '%s',
+ `url` = '%s',
+ `nurl` = '%s',
+ `addr` = '%s',
+ `name` = '%s',
+ `nick` = '%s',
+ `notify` = '%s'
+ WHERE `id` = %d",
+ dbesc($photos[0]),
+ dbesc($photos[1]),
+ dbesc($photos[2]),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc(datetime_convert()),
+ dbesc($contact->url),
+ dbesc(normalise_link($contact->url)),
+ dbesc($contact->username."@facebook.com"),
+ dbesc($contact->name),
+ dbesc($contact->username),
+ dbesc($contact->id),
+ intval($r[0]['id'])
+ );
+ }
+ }
+ return($r[0]["id"]);