From 7d2849ca00d17a338117e3d76a4f159c796cec4d Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 3 Nov 2013 02:23:52 +0100 Subject: [PATCH 1/8] fbsync: Shared posts --- fbsync/fbsync.php | 94 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 18 deletions(-) diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index 7babfcb2..19cb99c1 100644 --- a/fbsync/fbsync.php +++ b/fbsync/fbsync.php @@ -135,23 +135,44 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $postarray['parent-uri'] = $postarray['uri']; $postarray['plink'] = $post->permalink; - $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); - - if ($contact_id < 0) - return; - elseif ($contact_id == 0) - $contact_id = $self[0]["id"]; - - $postarray['contact-id'] = $contact_id; - - $postarray['owner-name'] = $contacts[$post->source_id]->name; - $postarray['owner-link'] = $contacts[$post->source_id]->url; - $postarray['owner-avatar'] = $contacts[$post->source_id]->pic_square; - $postarray['author-name'] = $contacts[$post->actor_id]->name; $postarray['author-link'] = $contacts[$post->actor_id]->url; $postarray['author-avatar'] = $contacts[$post->actor_id]->pic_square; + $contact_id = 0; + + //if (($post->parent_post_id != "") AND ($post->source_id != $post->source_id)) { + if ($post->parent_post_id != "") { + $pos = strpos($post->parent_post_id, "_"); + + if ($pos != 0) { + $user_id = substr($post->parent_post_id, 0, $pos); + + $userdata = fbsync_fetchuser($a, $uid, $user_id); + + $contact_id = $userdata["contact-id"]; + + $postarray['contact-id'] = $contact_id; + $postarray['owner-name'] = $userdata["name"]; + $postarray['owner-link'] = $userdata["link"]; + $postarray['owner-avatar'] = $userdata["avatar"]; + } + } + + if ($contact_id == 0) { + $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); + + if ($contact_id < 0) + return; + elseif ($contact_id == 0) + $contact_id = $self[0]["id"]; + + $postarray['contact-id'] = $contact_id; + $postarray['owner-name'] = $contacts[$post->source_id]->name; + $postarray['owner-link'] = $contacts[$post->source_id]->url; + $postarray['owner-avatar'] = $contacts[$post->source_id]->pic_square; + } + $postarray["body"] = (isset($post->message) ? escape_tags($post->message) : ''); $msgdata = fbsync_convertmsg($a, $postarray["body"]); @@ -227,7 +248,9 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr //$types = array(46, 80, 237, 247, 308); //if (!in_array($post->type, $types)) // $postarray["body"] = "Type: ".$post->type."\n".$postarray["body"]; + //print_r($post); //print_r($postarray); + $item = item_store($postarray); logger('fbsync_createpost: User '.$self[0]["nick"].' posted feed item '.$item, LOGGER_DEBUG); } @@ -638,6 +661,46 @@ function fbsync_convertmsg($a, $body) { } +function fbsync_fetchuser($a, $uid, $id) { + $access_token = get_pconfig($uid,'facebook','access_token'); + $self_id = get_pconfig($uid,'fbsync','self_id'); + + $user = array(); + + $contact = q("SELECT `id`, `name`, `url`, `photo` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", + intval($uid), dbesc("facebook::".$id)); + + if (count($contact)) { + $user["contact-id"] = $contact[0]["id"]; + $user["name"] = $contact[0]["name"]; + $user["link"] = $contact[0]["url"]; + $user["avatar"] = $contact[0]["photo"]; + + return($user); + } + + $own_contact = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", + intval($uid), dbesc("facebook::".$self_id)); + + if (!count($own_contact)) + return($user); + + $fql = "SELECT name, url, pic_square FROM profile WHERE id = ".$id; + + $url = "https://graph.facebook.com/fql?q=".urlencode($fql)."&access_token=".$access_token; + + $feed = fetch_url($url); + $data = json_decode($feed); + + if (is_array($data->data)) { + $user["contact-id"] = $own_contact[0]["id"]; + $user["name"] = $data->data[0]->name; + $user["link"] = $data->data[0]->url; + $user["avatar"] = $data->data[0]->pic_square; + } + return($user); +} + function fbsync_fetchfeed($a, $uid) { $access_token = get_pconfig($uid,'facebook','access_token'); $last_updated = get_pconfig($uid,'fbsync','last_updated'); @@ -764,10 +827,6 @@ function fbsync_fetchfeed($a, $uid) { if ($post->updated_time > $last_updated) $last_updated = $post->updated_time; - //print_r($post); - - // parent_post_id - Erkennen von geteilten Posts? - fbsync_createpost($a, $uid, $self, $contacts, $application_data, $post, $create_user); } @@ -779,7 +838,6 @@ function fbsync_fetchfeed($a, $uid) { $like->user_id = number_format($like->user_id, 0, '', ''); fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like); - } set_pconfig($uid,'fbsync','last_updated', $last_updated); From 4cdc81a2b142430052c31e248643c3f0f9d7e537 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 3 Nov 2013 02:55:05 +0100 Subject: [PATCH 2/8] fbsync: Shared posts now respect the settings for the way it is displayed --- fbsync/fbsync.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index 19cb99c1..ceecc254 100644 --- a/fbsync/fbsync.php +++ b/fbsync/fbsync.php @@ -8,7 +8,6 @@ /* To-Do FBSync: -- A: Make shared posts look like shared posts - B: Threading for incoming comments - C: Receiving likes for comments @@ -139,6 +138,10 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $postarray['author-link'] = $contacts[$post->actor_id]->url; $postarray['author-avatar'] = $contacts[$post->actor_id]->pic_square; + $postarray['owner-name'] = $contacts[$post->source_id]->name; + $postarray['owner-link'] = $contacts[$post->source_id]->url; + $postarray['owner-avatar'] = $contacts[$post->source_id]->pic_square; + $contact_id = 0; //if (($post->parent_post_id != "") AND ($post->source_id != $post->source_id)) { @@ -153,9 +156,21 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $contact_id = $userdata["contact-id"]; $postarray['contact-id'] = $contact_id; + $postarray['owner-name'] = $userdata["name"]; $postarray['owner-link'] = $userdata["link"]; $postarray['owner-avatar'] = $userdata["avatar"]; + + if (!intval(get_config('system','wall-to-wall_share'))) { + + $prebody = "[share author='".$postarray['author-name']. + "' profile='".$postarray['author-link']. + "' avatar='".$postarray['author-avatar']."']". + + $postarray['author-name'] = $postarray['owner-name']; + $postarray['author-link'] = $postarray['owner-link']; + $postarray['author-avatar'] = $postarray['owner-avatar']; + } } } @@ -168,9 +183,6 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $contact_id = $self[0]["id"]; $postarray['contact-id'] = $contact_id; - $postarray['owner-name'] = $contacts[$post->source_id]->name; - $postarray['owner-link'] = $contacts[$post->source_id]->url; - $postarray['owner-avatar'] = $contacts[$post->source_id]->pic_square; } $postarray["body"] = (isset($post->message) ? escape_tags($post->message) : ''); @@ -227,6 +239,9 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr if (trim($postarray["body"]) == "") return; + if ($prebody != "") + $postarray["body"] = $prebody.$postarray["body"]."[/share]"; + $postarray['created'] = datetime_convert('UTC','UTC',date("c", $post->created_time)); $postarray['edited'] = datetime_convert('UTC','UTC',date("c", $post->updated_time)); From 31e246a5521f39a2851c7689892e49beceb961e5 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 3 Nov 2013 16:27:12 +0100 Subject: [PATCH 3/8] fbsync: Now you can add contacts and you can import content from selected contacts --- fbsync/fbsync.php | 80 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index ceecc254..5ad44a0e 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 .= '
'; $s .= ''; $s .= ''; $s .= '
'; -*/ + /* provide a submit button */ $s .= '
'; @@ -77,7 +142,7 @@ function fbsync_settings_post(&$a,&$b) { if(x($_POST,'fbsync-submit')) { set_pconfig(local_user(),'fbsync','sync',intval($_POST['fbsync'])); - //set_pconfig(local_user(),'fbsync','create_user',intval($_POST['create_user'])); + set_pconfig(local_user(),'fbsync','create_user',intval($_POST['create_user'])); } } @@ -144,8 +209,8 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $contact_id = 0; - //if (($post->parent_post_id != "") AND ($post->source_id != $post->source_id)) { - if ($post->parent_post_id != "") { + //if ($post->parent_post_id != "") { + if (($post->parent_post_id != "") AND ($post->actor_id == $post->source_id)) { $pos = strpos($post->parent_post_id, "_"); if ($pos != 0) { @@ -177,7 +242,7 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr if ($contact_id == 0) { $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); - if ($contact_id < 0) + if (($contact_id <= 0) AND !$create_user) return; elseif ($contact_id == 0) $contact_id = $self[0]["id"]; @@ -721,8 +786,7 @@ function fbsync_fetchfeed($a, $uid) { $last_updated = get_pconfig($uid,'fbsync','last_updated'); $self_id = get_pconfig($uid,'fbsync','self_id'); - //$create_user = get_pconfig($uid, 'fbsybc', 'create_user'); - $create_user = true; + $create_user = get_pconfig($uid, 'fbsync', 'create_user'); $do_likes = get_config('fbsync', 'do_likes'); $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", From e4cf951a6ebfd5305a4f15aecce4aca43afc2198 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 3 Nov 2013 16:29:25 +0100 Subject: [PATCH 4/8] twitter: One unregister hook was missing --- twitter/twitter.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index e18b7445..3cabbff3 100755 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -84,6 +84,7 @@ function twitter_uninstall() { unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets'); unregister_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron'); unregister_hook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook'); + unregister_hook('follow', 'addon/twitter/twitter.php', 'twitter_follow'); // old setting - remove only unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook'); @@ -1411,7 +1412,8 @@ function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing $postarray['tag'] = $converted["tags"]; - if (!intval(get_config('system','wall-to-wall_share'))) { + // Deactivated at the moment, since there are problems with answers to retweets + if (false AND !intval(get_config('system','wall-to-wall_share'))) { $postarray['body'] = "[share author='".$post->retweeted_status->user->name. "' profile='https://twitter.com/".$post->retweeted_status->user->screen_name. "' avatar='".$post->retweeted_status->user->profile_image_url_https. From 34765b3b76bea3295281fdf768626360cddcc4db Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 3 Nov 2013 16:29:53 +0100 Subject: [PATCH 5/8] fromgplus: settings moved to connector settings --- fromgplus/fromgplus.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/fromgplus/fromgplus.php b/fromgplus/fromgplus.php index 4c271873..a6d3d84f 100644 --- a/fromgplus/fromgplus.php +++ b/fromgplus/fromgplus.php @@ -10,15 +10,19 @@ define('FROMGPLUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes function fromgplus_install() { - register_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings'); - register_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post'); + register_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings'); + register_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post'); register_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron'); } function fromgplus_uninstall() { + unregister_hook('connector_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings'); + unregister_hook('connector_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post'); + unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron'); + + // Old hooks unregister_hook('plugin_settings', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings'); unregister_hook('plugin_settings_post', 'addon/fromgplus/fromgplus.php', 'fromgplus_addon_settings_post'); - unregister_hook('cron', 'addon/fromgplus/fromgplus.php', 'fromgplus_cron'); } function fromgplus_addon_settings(&$a,&$s) { From acdd79cdabb5dccd4c49823f15f8c905e2da5e6b Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 3 Nov 2013 17:04:02 +0100 Subject: [PATCH 6/8] fbsync: error correction for shared posts --- fbsync/fbsync.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index 5ad44a0e..000aeaa1 100644 --- a/fbsync/fbsync.php +++ b/fbsync/fbsync.php @@ -209,7 +209,6 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $contact_id = 0; - //if ($post->parent_post_id != "") { if (($post->parent_post_id != "") AND ($post->actor_id == $post->source_id)) { $pos = strpos($post->parent_post_id, "_"); @@ -222,19 +221,21 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $postarray['contact-id'] = $contact_id; - $postarray['owner-name'] = $userdata["name"]; - $postarray['owner-link'] = $userdata["link"]; - $postarray['owner-avatar'] = $userdata["avatar"]; + if (array_key_exists("name", $userdata) AND ($userdata["name"] != "") AND !link_compare($userdata["link"], $postarray['author-link'])) { + $postarray['owner-name'] = $userdata["name"]; + $postarray['owner-link'] = $userdata["link"]; + $postarray['owner-avatar'] = $userdata["avatar"]; - if (!intval(get_config('system','wall-to-wall_share'))) { + if (!intval(get_config('system','wall-to-wall_share'))) { - $prebody = "[share author='".$postarray['author-name']. - "' profile='".$postarray['author-link']. - "' avatar='".$postarray['author-avatar']."']". + $prebody = "[share author='".$postarray['author-name']. + "' profile='".$postarray['author-link']. + "' avatar='".$postarray['author-avatar']."']"; - $postarray['author-name'] = $postarray['owner-name']; - $postarray['author-link'] = $postarray['owner-link']; - $postarray['author-avatar'] = $postarray['owner-avatar']; + $postarray['author-name'] = $postarray['owner-name']; + $postarray['author-link'] = $postarray['owner-link']; + $postarray['author-avatar'] = $postarray['owner-avatar']; + } } } } @@ -920,6 +921,5 @@ function fbsync_fetchfeed($a, $uid) { } set_pconfig($uid,'fbsync','last_updated', $last_updated); - } ?> From e89bab99add903f3a9c32b98a3c5c6749d37baf7 Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Sun, 3 Nov 2013 21:31:52 +0100 Subject: [PATCH 7/8] twitter: Imported links sometime vanished --- twitter/twitter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/twitter/twitter.php b/twitter/twitter.php index 3cabbff3..e90f36df 100755 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1785,7 +1785,7 @@ function twitter_convertmsg($a, $body, $no_tags = false) { } } - if (($footerlink != "") AND ($footer != "")) { + if (($footerlink != "") AND (trim($footer) != "")) { $removedlink = trim(str_replace($footerlink, "", $body)); if (strstr($body, $removedlink)) From 97a66751dbd9fd2476f9f694aa19442e168d553a Mon Sep 17 00:00:00 2001 From: Michael Vogel Date: Fri, 8 Nov 2013 08:23:00 +0100 Subject: [PATCH 8/8] fbsync: Importing posts only from contacts you have in your contact list should work now --- fbsync/fbsync.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index 000aeaa1..13a504dc 100644 --- a/fbsync/fbsync.php +++ b/fbsync/fbsync.php @@ -243,10 +243,14 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr if ($contact_id == 0) { $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); - if (($contact_id <= 0) AND !$create_user) + if (($contact_id <= 0) AND !$create_user) { + logger('fbsync_createpost: No matching contact found. Post not imported '.print_r($post, true), LOGGER_DEBUG); return; - elseif ($contact_id == 0) + } elseif ($contact_id == 0) { + // This case should never happen + logger('fbsync_createpost: No matching contact found. Using own id. (Should never happen) '.print_r($post, true), LOGGER_DEBUG); $contact_id = $self[0]["id"]; + } $postarray['contact-id'] = $contact_id; }