fbsync/fbpost: postings to selected facebook addresses now work. (will work for new contacts and step by step for old contacts)

This commit is contained in:
Michael Vogel 2014-03-23 22:40:50 +01:00
parent 81e05e6274
commit 2ee50af605
2 changed files with 134 additions and 131 deletions

View file

@ -96,7 +96,7 @@ function fbpost_init(&$a) {
if(strpos($x,'access_token=') !== false) { if(strpos($x,'access_token=') !== false) {
$token = str_replace('access_token=', '', $x); $token = str_replace('access_token=', '', $x);
if(strpos($token,'&') !== false) if(strpos($token,'&') !== false)
$token = substr($token,0,strpos($token,'&')); $token = substr($token,0,strpos($token,'&'));
set_pconfig($uid,'facebook','access_token',$token); set_pconfig($uid,'facebook','access_token',$token);
set_pconfig($uid,'facebook','post','1'); set_pconfig($uid,'facebook','post','1');
@ -571,6 +571,7 @@ function fbpost_post_hook(&$a,&$b) {
$allow_str = dbesc(implode(', ',$recipients)); $allow_str = dbesc(implode(', ',$recipients));
if($allow_str) { if($allow_str) {
logger("fbpost_post_hook: private post to: ".$allow_str, LOGGER_DEBUG);
$r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $allow_str ) AND `network` = 'face'"); $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $allow_str ) AND `network` = 'face'");
if(count($r)) if(count($r))
foreach($r as $rr) foreach($r as $rr)
@ -962,7 +963,7 @@ function fbpost_get_app_access_token() {
logger('fb_get_app_access_token: returned access token: ' . $x, LOGGER_DATA); logger('fb_get_app_access_token: returned access token: ' . $x, LOGGER_DATA);
$token = str_replace('access_token=', '', $x); $token = str_replace('access_token=', '', $x);
if(strpos($token,'&') !== false) if(strpos($token,'&') !== false)
$token = substr($token,0,strpos($token,'&')); $token = substr($token,0,strpos($token,'&'));
if ($token == "") { if ($token == "") {

View file

@ -423,18 +423,18 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl
// Check if the contact id was blocked // Check if the contact id was blocked
if ($parent_contact > 0) { if ($parent_contact > 0) {
$r = q("SELECT `blocked`, `readonly`, `nick` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1", $r = q("SELECT `blocked`, `readonly`, `nick` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1",
intval($uid), intval($parent_contact)); intval($uid), intval($parent_contact));
// Should only happen if someone deleted the contact manually // Should only happen if someone deleted the contact manually
if(!count($r)) { if(!count($r)) {
logger("fbsync_createcomment: UID ".$uid." - Contact ".$parent_contact." doesn't seem to exist.", LOGGER_DEBUG); logger("fbsync_createcomment: UID ".$uid." - Contact ".$parent_contact." doesn't seem to exist.", LOGGER_DEBUG);
return; return;
} }
// Is blocked? Then return // Is blocked? Then return
if ($r[0]["readonly"] OR $r[0]["blocked"]) { if ($r[0]["readonly"] OR $r[0]["blocked"]) {
logger("fbsync_createcomment: UID ".$uid." - Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); logger("fbsync_createcomment: UID ".$uid." - Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG);
return; return;
} }
$parent_nick = $r[0]["nick"]; $parent_nick = $r[0]["nick"];
@ -522,8 +522,8 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl
if(count($myconv)) { if(count($myconv)) {
$importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname']; $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
$own_contact = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", $own_contact = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
intval($uid), dbesc("facebook::".$self_id)); intval($uid), dbesc("facebook::".$self_id));
if (!count($own_contact)) if (!count($own_contact))
return; return;
@ -570,8 +570,8 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) {
intval($uid) intval($uid)
); );
if (count($r)) if (count($r))
$orig_post = $r[0]; $orig_post = $r[0];
else else
return; return;
@ -597,15 +597,15 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) {
$contact_id = $self[0]["id"]; $contact_id = $self[0]["id"];
$likedata = array(); $likedata = array();
$likedata['parent'] = $orig_post['id']; $likedata['parent'] = $orig_post['id'];
$likedata['verb'] = ACTIVITY_LIKE; $likedata['verb'] = ACTIVITY_LIKE;
$likedate['network'] = dbesc(NETWORK_FACEBOOK); $likedate['network'] = dbesc(NETWORK_FACEBOOK);
$likedata['gravity'] = 3; $likedata['gravity'] = 3;
$likedata['uid'] = $uid; $likedata['uid'] = $uid;
$likedata['wall'] = 0; $likedata['wall'] = 0;
$likedata['uri'] = item_new_uri($a->get_baseurl(), $uid); $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
$likedata['parent-uri'] = $orig_post["uri"]; $likedata['parent-uri'] = $orig_post["uri"];
$likedata['app'] = "Facebook"; $likedata['app'] = "Facebook";
if ($like->user_id != $self_id) { if ($like->user_id != $self_id) {
$likedata['contact-id'] = $contact_id; $likedata['contact-id'] = $contact_id;
@ -625,12 +625,12 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) {
$post_type = t('status'); $post_type = t('status');
$plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]'; $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
$likedata['object-type'] = ACTIVITY_OBJ_NOTE; $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
$likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink); $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
$likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' . $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
'<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . xmlify($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>'; '<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . xmlify($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';
$r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `author-link` = '%s' AND `verb` = '%s' AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `author-link` = '%s' AND `verb` = '%s' AND `uid` = %d LIMIT 1",
@ -640,7 +640,7 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) {
intval($uid) intval($uid)
); );
if (count($r)) if (count($r))
return; return;
$item = item_store($likedata); $item = item_store($likedata);
@ -657,131 +657,133 @@ function fbsync_fetch_contact($uid, $contact, $create_user) {
if (count($r) == 0) if (count($r) == 0)
q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')", q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
dbesc(normalise_link($contact->url)), dbesc(normalise_link($contact->url)),
dbesc($contact->name), dbesc($contact->name),
dbesc($contact->username), dbesc($contact->username),
dbesc($contact->pic_square)); dbesc($contact->pic_square));
else else
q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'", q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
dbesc($contact->name), dbesc($contact->name),
dbesc($contact->username), dbesc($contact->username),
dbesc($contact->pic_square), dbesc($contact->pic_square),
dbesc(normalise_link($contact->url))); dbesc(normalise_link($contact->url)));
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
intval($uid), dbesc("facebook::".$contact->id)); intval($uid), dbesc("facebook::".$contact->id));
if(!count($r) AND !$create_user) if(!count($r) AND !$create_user)
return(0); return(0);
if (count($r) AND ($r[0]["readonly"] OR $r[0]["blocked"])) { if (count($r) AND ($r[0]["readonly"] OR $r[0]["blocked"])) {
logger("fbsync_fetch_contact: Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); logger("fbsync_fetch_contact: Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG);
return(-1); return(-1);
} }
$avatarpicture = $contact->pic_square; $avatarpicture = $contact->pic_square;
if(!count($r)) { if(!count($r)) {
// create contact record // create contact record
q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`, q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
`name`, `nick`, `photo`, `network`, `rel`, `priority`, `name`, `nick`, `photo`, `network`, `rel`, `priority`,
`writable`, `blocked`, `readonly`, `pending`) `writable`, `blocked`, `readonly`, `pending`)
VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0)", VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0)",
intval($uid), intval($uid),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc($contact->url), dbesc($contact->url),
dbesc(normalise_link($contact->url)), dbesc(normalise_link($contact->url)),
dbesc($contact->username."@facebook.com"), dbesc($contact->username."@facebook.com"),
dbesc("facebook::".$contact->id), dbesc("facebook::".$contact->id),
dbesc(''), dbesc($contact->id),
dbesc("facebook::".$contact->id), dbesc("facebook::".$contact->id),
dbesc($contact->name), dbesc($contact->name),
dbesc($contact->username), dbesc($contact->username),
dbesc($avatarpicture), dbesc($avatarpicture),
dbesc(NETWORK_FACEBOOK), dbesc(NETWORK_FACEBOOK),
intval(CONTACT_IS_FRIEND), intval(CONTACT_IS_FRIEND),
intval(1), intval(1),
intval(1) intval(1)
); );
$r = q("SELECT * FROM `contact` WHERE `alias` = '%s' AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `alias` = '%s' AND `uid` = %d LIMIT 1",
dbesc("facebook::".$contact->id), dbesc("facebook::".$contact->id),
intval($uid) intval($uid)
); );
if(! count($r)) if(! count($r))
return(false); return(false);
$contact_id = $r[0]['id']; $contact_id = $r[0]['id'];
$g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1", $g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1",
intval($uid) intval($uid)
); );
if($g && intval($g[0]['def_gid'])) { if($g && intval($g[0]['def_gid'])) {
require_once('include/group.php'); require_once('include/group.php');
group_add_member($uid,'',$contact_id,$g[0]['def_gid']); group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
} }
require_once("Photo.php"); require_once("Photo.php");
$photos = import_profile_photo($avatarpicture,$uid,$contact_id); $photos = import_profile_photo($avatarpicture,$uid,$contact_id);
q("UPDATE `contact` SET `photo` = '%s', q("UPDATE `contact` SET `photo` = '%s',
`thumb` = '%s', `thumb` = '%s',
`micro` = '%s', `micro` = '%s',
`name-date` = '%s', `name-date` = '%s',
`uri-date` = '%s', `uri-date` = '%s',
`avatar-date` = '%s' `avatar-date` = '%s'
WHERE `id` = %d", WHERE `id` = %d",
dbesc($photos[0]), dbesc($photos[0]),
dbesc($photos[1]), dbesc($photos[1]),
dbesc($photos[2]), dbesc($photos[2]),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc(datetime_convert()), dbesc(datetime_convert()),
intval($contact_id) intval($contact_id)
); );
} else { } else {
// update profile photos once every 12 hours as we have no notification of when they change. // 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')); $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 // 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)) { if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
logger("fbsync_fetch_contact: Updating contact ".$contact->username, LOGGER_DEBUG); logger("fbsync_fetch_contact: Updating contact ".$contact->username, LOGGER_DEBUG);
require_once("Photo.php"); require_once("Photo.php");
$photos = import_profile_photo($avatarpicture, $uid, $r[0]['id']); $photos = import_profile_photo($avatarpicture, $uid, $r[0]['id']);
q("UPDATE `contact` SET `photo` = '%s', q("UPDATE `contact` SET `photo` = '%s',
`thumb` = '%s', `thumb` = '%s',
`micro` = '%s', `micro` = '%s',
`name-date` = '%s', `name-date` = '%s',
`uri-date` = '%s', `uri-date` = '%s',
`avatar-date` = '%s', `avatar-date` = '%s',
`url` = '%s', `url` = '%s',
`nurl` = '%s', `nurl` = '%s',
`addr` = '%s', `addr` = '%s',
`name` = '%s', `name` = '%s',
`nick` = '%s' `nick` = '%s',
WHERE `id` = %d", `notify` = '%s'
dbesc($photos[0]), WHERE `id` = %d",
dbesc($photos[1]), dbesc($photos[0]),
dbesc($photos[2]), dbesc($photos[1]),
dbesc(datetime_convert()), dbesc($photos[2]),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc(datetime_convert()), dbesc(datetime_convert()),
dbesc($contact->url), dbesc(datetime_convert()),
dbesc(normalise_link($contact->url)), dbesc($contact->url),
dbesc($contact->username."@facebook.com"), dbesc(normalise_link($contact->url)),
dbesc($contact->name), dbesc($contact->username."@facebook.com"),
dbesc($contact->username), dbesc($contact->name),
intval($r[0]['id']) dbesc($contact->username),
); dbesc($contact->id),
} intval($r[0]['id'])
} );
return($r[0]["id"]); }
}
return($r[0]["id"]);
} }
function fbsync_get_self($uid) { function fbsync_get_self($uid) {
@ -854,9 +856,9 @@ function fbsync_fetchuser($a, $uid, $id) {
if (count($contact)) { if (count($contact)) {
if (($contact[0]["readonly"] OR $contact[0]["blocked"])) { if (($contact[0]["readonly"] OR $contact[0]["blocked"])) {
logger("fbsync_fetchuser: Contact '".$contact[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); logger("fbsync_fetchuser: Contact '".$contact[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG);
$user["contact-id"] = -1; $user["contact-id"] = -1;
} else } else
$user["contact-id"] = $contact[0]["id"]; $user["contact-id"] = $contact[0]["id"];
$user["name"] = $contact[0]["name"]; $user["name"] = $contact[0]["name"];