X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=fbsync%2Ffbsync.php;h=07f6dcf11254e98fa6be9dc2e25f54980ce9f5ef;hb=18784a484b04b5f74e6d4dca516b2dc20cffe953;hp=7babfcb272859fb8165172d41aa174b1c9a3c43a;hpb=3d70505565ccb699519a57b73bfaf483e7bc130b;p=friendica-addons.git diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index 7babfcb2..07f6dcf1 100644 --- a/fbsync/fbsync.php +++ b/fbsync/fbsync.php @@ -2,13 +2,13 @@ /** * Name: Facebook Sync * Description: Synchronizes the Facebook Newsfeed - * Version: 0.0.1 alpha + * Version: 1.0 * Author: Michael Vogel + * Status: Unsupported */ /* To-Do FBSync: -- A: Make shared posts look like shared posts - B: Threading for incoming comments - C: Receiving likes for comments @@ -26,16 +26,86 @@ 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'); + register_hook('expire', 'addon/fbsync/fbsync.php', 'fbsync_expire'); } 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'); + unregister_hook('expire', 'addon/fbsync/fbsync.php', 'fbsync_expire'); } +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") && !strstr($contact["url"], "://facebook.com") && !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) { + // Settings are done inside the fbpost addon + return; + if(! local_user()) return; @@ -55,22 +125,27 @@ function fbsync_settings(&$a,&$s) { /* Add some HTML to the existing form */ - $s .= '
'; - $s .= '

' . t('Facebook Import Settings') . '

'; + $s .= ''; + $s .= '

'. t('Facebook Import').'

'; + $s .= '
'; + $s .= ''; + $s .= '
'; } @@ -78,7 +153,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'])); } } @@ -98,9 +173,24 @@ function fbsync_cron($a,$b) { } logger('fbsync_cron: cron_start'); + $abandon_days = intval(get_config('system','account_abandon_days')); + if ($abandon_days < 1) + $abandon_days = 0; + + $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400); + $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fbsync' AND `k` = 'sync' AND `v` = '1' ORDER BY RAND()"); if(count($r)) { foreach($r as $rr) { + + if ($abandon_days != 0) { + $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit); + if (!count($user)) { + logger('abandoned account: timeline from user '.$rr['uid'].' will not be imported'); + continue; + } + } + fbsync_get_self($rr['uid']); logger('fbsync_cron: importing timeline from user '.$rr['uid']); @@ -108,13 +198,43 @@ function fbsync_cron($a,$b) { } } - logger('fbsync: cron_end'); + logger('fbsync_cron: cron_end'); set_config('fbsync','last_poll', time()); } +function fbsync_expire($a,$b) { + + $days = get_config('fbsync', 'expire'); + + if ($days == 0) + return; + + $r = q("DELETE FROM `item` WHERE `deleted` AND `network` = '%s'", dbesc(NETWORK_FACEBOOK)); + + require_once("include/items.php"); + + logger('fbsync_expire: expire_start'); + + $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fbsync' AND `k` = 'sync' AND `v` = '1' ORDER BY RAND()"); + if(count($r)) { + foreach($r as $rr) { + logger('fbsync_expire: user '.$rr['uid']); + item_expire($rr['uid'], $days, NETWORK_FACEBOOK, true); + } + } + + logger('fbsync_expire: expire_end'); +} + function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $create_user) { + $access_token = get_pconfig($uid,'facebook','access_token'); + + require_once("include/oembed.php"); + require_once("include/network.php"); + require_once("include/items.php"); + // check if it was already imported $r = q("SELECT * FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", intval($uid), @@ -129,28 +249,84 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $postarray['wall'] = 0; $postarray['verb'] = ACTIVITY_POST; + $postarray['object-type'] = ACTIVITY_OBJ_NOTE; // default value - is maybe changed later when media is attached + $postarray['network'] = dbesc(NETWORK_FACEBOOK); $postarray['uri'] = "fb::".$post->post_id; $postarray['thr-parent'] = $postarray['uri']; $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['author-name'] = $contacts[$post->actor_id]->name; + $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; - $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 != "") && ($post->actor_id == $post->source_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; + + if (array_key_exists("name", $userdata) && ($userdata["name"] != "") && !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'))) { + + $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']; + } + } + } + } + + if ($contact_id <= 0) { + if ($post->actor_id != $post->source_id) { + // Testing if we know the source or the actor + $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], false); + + if (($contact_id == 0) && array_key_exists($post->actor_id, $contacts)) + $contact_id = fbsync_fetch_contact($uid, $contacts[$post->actor_id], false); + + // If we don't know anyone, we guess we should know the source. Could be the wrong decision + if ($contact_id == 0) + $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); + } else + $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user); + + + if ($contact_id == -1) { + logger('fbsync_createpost: Contact is blocked. Post not imported '.print_r($post, true), LOGGER_DEBUG); + return; + } elseif (($contact_id <= 0) && !$create_user) { + logger('fbsync_createpost: No matching contact found. Post not imported '.print_r($post, true), LOGGER_DEBUG); + return; + } 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; + } $postarray["body"] = (isset($post->message) ? escape_tags($post->message) : ''); @@ -159,53 +335,122 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $postarray["body"] = $msgdata["body"]; $postarray["tag"] = $msgdata["tags"]; - if(isset($post->attachment->name) and isset($post->attachment->href)) - $postarray["body"] .= "\n\n[bookmark=".$post->attachment->href."]".$post->attachment->name."[/bookmark]"; - elseif (isset($post->attachment->name) AND ($post->attachment->name != "")) - $postarray["body"] .= "\n\n[b]" . $post->attachment->name."[/b]"; + // Change the object type when an attachment is present + if (isset($post->attachment->fb_object_type)) + logger('fb_object_type: '.$post->attachment->fb_object_type." ".print_r($post->attachment, true), LOGGER_DEBUG); + switch ($post->attachment->fb_object_type) { + case 'photo': + $postarray['object-type'] = ACTIVITY_OBJ_IMAGE; // photo is deprecated: http://activitystrea.ms/head/activity-schema.html#image + break; + case 'video': + $postarray['object-type'] = ACTIVITY_OBJ_VIDEO; + break; + case '': + //$postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK; + break; + default: + logger('Unknown object type '.$post->attachment->fb_object_type, LOGGER_DEBUG); + break; + } + + $pagedata = array(); + $content = ""; + $pagedata["type"] = ""; + + if (isset($post->attachment->name) && isset($post->attachment->href)) { + $post->attachment->href = original_url($post->attachment->href); + $oembed_data = oembed_fetch_url($post->attachment->href); + $pagedata["type"] = $oembed_data->type; + if ($pagedata["type"] == "rich") + $pagedata["type"] = "link"; - $quote = ""; - if(isset($post->attachment->description) and ($post->attachment->fb_object_type != "photo")) - $quote = $post->attachment->description; + $pagedata["url"] = $post->attachment->href; + $pagedata["title"] = $post->attachment->name; + $content = "[bookmark=".$post->attachment->href."]".$post->attachment->name."[/bookmark]"; - if(isset($post->attachment->caption) and ($post->attachment->fb_object_type == "photo")) - $quote = $post->attachment->caption; + // If a link is not only attached but also added in the body, look if it can be removed in the body. + $removedlink = trim(str_replace($post->attachment->href, "", $postarray["body"])); - if ($quote.$post->attachment->href.$postarray["body"] == "") + if (($removedlink == "") || strstr($postarray["body"], $removedlink)) + $postarray["body"] = $removedlink; + + } elseif (isset($post->attachment->name) && ($post->attachment->name != "")) + $content = "[b]" . $post->attachment->name."[/b]"; + + $pagedata["text"] = ""; + if (isset($post->attachment->description) && ($post->attachment->fb_object_type != "photo")) + $pagedata["text"] = $post->attachment->description; + + if (isset($post->attachment->caption) && ($post->attachment->fb_object_type == "photo")) + $pagedata["text"] = $post->attachment->caption; + + if ($pagedata["text"].$post->attachment->href.$content.$postarray["body"] == "") return; - if (isset($post->attachment->media) AND !strstr($post->attachment->href, "://www.youtube.com/") - AND !strstr($post->attachment->href, "://youtu.be/") - AND !strstr($post->attachment->href, ".vimeo.com/")) { + if (isset($post->attachment->media) && (($pagedata["type"] == "") || ($pagedata["type"] == "link"))) { foreach ($post->attachment->media AS $media) { - //$media->photo->owner = number_format($media->photo->owner, 0, '', ''); - //if ($media->photo->owner != '') { - // $postarray['author-name'] = $contacts[$media->photo->owner]->name; - // $postarray['author-link'] = $contacts[$media->photo->owner]->url; - // $postarray['author-avatar'] = $contacts[$media->photo->owner]->pic_square; - //} - - if(isset($media->src) && isset($media->href) AND ($media->src != "") AND ($media->href != "")) - $postarray["body"] .= "\n".'[url='.$media->href.'][img]'.fpost_cleanpicture($media->src).'[/img][/url]'; - else { - if (isset($media->src) AND ($media->src != "")) - $postarray["body"] .= "\n".'[img]'.fpost_cleanpicture($media->src).'[/img]'; + + if (isset($media->type)) + $pagedata["type"] = $media->type; + + if (isset($media->src)) + $pagedata["images"][0]["src"] = $media->src; + + if (isset($media->photo)) { + if (isset($media->photo->images) && (count($media->photo->images) > 1)) + $pagedata["images"][0]["src"] = $media->photo->images[1]->src; + + if (isset($media->photo->fbid)) { + logger('fbsync_createpost: fetching fbid '.$media->photo->fbid, LOGGER_DEBUG); + $url = "https://graph.facebook.com/".$media->photo->fbid."?access_token=".$access_token; + $feed = fetch_url($url); + $data = json_decode($feed); + if (isset($data->images)) { + $pagedata["images"][0]["src"] = $data->images[0]->source; + logger('fbsync_createpost: got fbid '.$media->photo->fbid.' image '.$pagedata["images"][0]["src"], LOGGER_DEBUG); + } else + logger('fbsync_createpost: error fetching fbid '.$media->photo->fbid.' '.print_r($data, true), LOGGER_DEBUG); + } + } + + $pagedata["images"][0]["src"] = fbpost_cleanpicture($pagedata["images"][0]["src"]); + + if (isset($media->href) && ($pagedata["images"][0]["src"] != "") && ($media->href != "")) { + $media->href = original_url($media->href); + $pagedata["url"] = $media->href; + $content .= "\n".'[url='.$media->href.'][img]'.$pagedata["images"][0]["src"].'[/img][/url]'; + } else { + if ($pagedata["images"][0]["src"] != "") + $content .= "\n".'[img]'.$pagedata["images"][0]["src"].'[/img]'; // if just a link, it may be a wall photo - check - if(isset($post->link)) - $postarray["body"] .= fbpost_get_photo($media->href); + if (isset($post->link)) + $content .= fbpost_get_photo($media->href); } } } - if ($quote) - $postarray["body"] .= "\n[quote]".$quote."[/quote]"; + if ($pagedata["type"] != "") { + if ($pagedata["type"] == "link") + $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK; + + $postarray["body"] .= add_page_info_data($pagedata); + } else { + if ($content) + $postarray["body"] .= "\n".trim($content); + + if ($pagedata["text"]) + $postarray["body"] .= "\n[quote]".trim($pagedata["text"])."[/quote]"; - $postarray["body"] = trim($postarray["body"]); + $postarray["body"] = trim($postarray["body"]); + } 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)); @@ -227,6 +472,7 @@ 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); @@ -251,33 +497,64 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl return; $parent_uri = ""; + $parent_contact = 0; + $parent_nick = ""; // Fetch the parent uri (Checking if the parent exists) - $r = q("SELECT `uri` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", + $r = q("SELECT `uri`, `contact-id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' LIMIT 1", intval($uid), dbesc('fb::'.$comment->post_id) ); - if(count($r)) + if(count($r)) { $parent_uri = $r[0]["uri"]; + $parent_contact = $r[0]["contact-id"]; + } // check if it is a reply to an own post (separate posting for performance reasons) - $r = q("SELECT `uri` FROM `item` WHERE `uid` = %d AND `extid` = '%s' LIMIT 1", + $r = q("SELECT `uri`, `contact-id` FROM `item` WHERE `uid` = %d AND `extid` = '%s' LIMIT 1", intval($uid), dbesc('fb::'.$comment->post_id) ); - if(count($r)) + if(count($r)) { $parent_uri = $r[0]["uri"]; + $parent_contact = $r[0]["contact-id"]; + } // No parent? Then quit if ($parent_uri == "") return; + //logger("fbsync_createcomment: Checking if parent contact is blocked: ".$parent_contact." - ".$parent_uri, LOGGER_DEBUG); + + // Check if the contact id was blocked + if ($parent_contact > 0) { + $r = q("SELECT `blocked`, `readonly`, `nick` FROM `contact` WHERE `uid` = %d AND `id` = %d LIMIT 1", + intval($uid), intval($parent_contact)); + + // Should only happen if someone deleted the contact manually + if(!count($r)) { + logger("fbsync_createcomment: UID ".$uid." - Contact ".$parent_contact." doesn't seem to exist.", LOGGER_DEBUG); + return; + } + + // Is blocked? Then return + if ($r[0]["readonly"] || $r[0]["blocked"]) { + logger("fbsync_createcomment: UID ".$uid." - Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); + return; + } + + $parent_nick = $r[0]["nick"]; + logger("fbsync_createcomment: UID ".$uid." - Contact '".$r[0]["nick"]."' isn't blocked. ".print_r($r, true), LOGGER_DEBUG); + } + $postarray = array(); $postarray['gravity'] = 0; $postarray['uid'] = $uid; $postarray['wall'] = 0; $postarray['verb'] = ACTIVITY_POST; + $postarray['object-type'] = ACTIVITY_OBJ_COMMENT; + $postarray['network'] = dbesc(NETWORK_FACEBOOK); $postarray['uri'] = "fb::".$comment->id; $postarray['thr-parent'] = $parent_uri; @@ -286,8 +563,24 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl $contact_id = fbsync_fetch_contact($uid, $contacts[$comment->fromid], array(), false); - if ($contact_id <= 0) + $contact_nick = $contacts[$comment->fromid]->name; + + if ($contact_id == -1) { + logger('fbsync_createcomment: Contact was blocked. Comment not imported '.print_r($comment, true), LOGGER_DEBUG); + return; + } + + // If no contact was found, take it from the thread owner + if ($contact_id <= 0) { + $contact_id = $parent_contact; + $contact_nick = $parent_nick; + } + + // This case here should never happen + if ($contact_id <= 0) { $contact_id = $self[0]["id"]; + $contact_nick = $self[0]["nick"]; + } if ($comment->fromid != $self_id) { $postarray['contact-id'] = $contact_id; @@ -299,6 +592,7 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl $postarray['owner-name'] = $self[0]["name"]; $postarray['owner-link'] = $self[0]["url"]; $postarray['owner-avatar'] = $self[0]["photo"]; + $contact_nick = $self[0]["nick"]; } $postarray['author-name'] = $postarray['owner-name']; @@ -322,7 +616,9 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl return; $item = item_store($postarray); - logger('fbsync_createcomment: User '.$self[0]["nick"].' posted comment '.$item, LOGGER_DEBUG); + $postarray["id"] = $item; + + logger('fbsync_createcomment: UID '.$uid.' - CID '.$postarray['contact-id'].' - Nick '.$contact_nick.' posted comment '.$item, LOGGER_DEBUG); if ($item == 0) return; @@ -335,8 +631,8 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl if(count($myconv)) { $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname']; - $own_contact = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", - intval($uid), dbesc("facebook::".$self_id)); + $own_contact = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", + intval($uid), dbesc("facebook::".$self_id)); if (!count($own_contact)) return; @@ -344,7 +640,7 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl foreach($myconv as $conv) { // now if we find a match, it means we're in this conversation - if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$own_contact[0]["url"])) + if(!link_compare($conv['author-link'],$importer_url) && !link_compare($conv['author-link'],$own_contact[0]["url"])) continue; require_once('include/enotify.php'); @@ -359,7 +655,7 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl 'to_email' => $user[0]['email'], 'uid' => $user[0]['uid'], 'item' => $postarray, - 'link' => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $item, + 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item)), 'source_name' => $postarray['author-name'], 'source_link' => $postarray['author-link'], 'source_photo' => $postarray['author-avatar'], @@ -383,8 +679,8 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) { intval($uid) ); - if (count($r)) - $orig_post = $r[0]; + if (count($r)) + $orig_post = $r[0]; else return; @@ -410,15 +706,18 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) { $contact_id = $self[0]["id"]; $likedata = array(); - $likedata['parent'] = $orig_post['id']; - $likedata['verb'] = ACTIVITY_LIKE; - $likedata['gravity'] = 3; - $likedata['uid'] = $uid; - $likedata['wall'] = 0; - $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid); - $likedata['parent-uri'] = $orig_post["uri"]; - $likedata['app'] = "Facebook"; - $likedata['verb'] = ACTIVITY_LIKE; + $likedata['parent'] = $orig_post['id']; + + $likedata['verb'] = ACTIVITY_LIKE; + $likedata['object-type'] = ACTIVITY_OBJ_NOTE; + $likedate['network'] = dbesc(NETWORK_FACEBOOK); + + $likedata['gravity'] = 3; + $likedata['uid'] = $uid; + $likedata['wall'] = 0; + $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid); + $likedata['parent-uri'] = $orig_post["uri"]; + $likedata['app'] = "Facebook"; if ($like->user_id != $self_id) { $likedata['contact-id'] = $contact_id; @@ -438,12 +737,12 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) { $post_type = t('status'); $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'] = '' . ACTIVITY_OBJ_NOTE . '1' . - '' . $orig_post['uri'] . '' . xmlify('') . '' . $orig_post['title'] . '' . $orig_post['body'] . ''; + $likedata['object'] = '' . ACTIVITY_OBJ_NOTE . '1' . + '' . $orig_post['uri'] . '' . xmlify('') . '' . $orig_post['title'] . '' . $orig_post['body'] . ''; $r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `author-link` = '%s' AND `verb` = '%s' AND `uid` = %d LIMIT 1", @@ -453,7 +752,7 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) { intval($uid) ); - if (count($r)) + if (count($r)) return; $item = item_store($likedata); @@ -462,121 +761,144 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) { function fbsync_fetch_contact($uid, $contact, $create_user) { - $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", - intval($uid), dbesc("facebook::".$contact->id)); + if($contact->url == "") + return(0); + + // Check if the unique contact is existing + // To-Do: only update once a while + $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1", + dbesc(normalise_link($contact->url))); + + if (count($r) == 0) + q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')", + dbesc(normalise_link($contact->url)), + dbesc($contact->name), + dbesc($contact->username), + dbesc($contact->pic_square)); + else + q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'", + dbesc($contact->name), + dbesc($contact->username), + dbesc($contact->pic_square), + dbesc(normalise_link($contact->url))); + + $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1", + intval($uid), dbesc("facebook::".$contact->id)); - if(!count($r) AND !$create_user) - return(0); + if(!count($r) && !$create_user) + return(0); - 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); - return(-1); - } + if (count($r) && ($r[0]["readonly"] || $r[0]["blocked"])) { + logger("fbsync_fetch_contact: Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); + return(-1); + } $avatarpicture = $contact->pic_square; - 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"]); } function fbsync_get_self($uid) { @@ -638,13 +960,57 @@ 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)) { + if (($contact[0]["readonly"] || $contact[0]["blocked"])) { + logger("fbsync_fetchuser: Contact '".$contact[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); + $user["contact-id"] = -1; + } else + $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'); $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", @@ -679,7 +1045,6 @@ function fbsync_fetchfeed($a, $uid) { $url = "https://graph.facebook.com/fql?q=".urlencode(json_encode($fql))."&access_token=".$access_token; $feed = fetch_url($url); - $data = json_decode($feed); if (!is_array($data->data)) { @@ -763,11 +1128,6 @@ function fbsync_fetchfeed($a, $uid) { foreach ($post_data AS $post) { 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,10 +1139,8 @@ 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); - } ?>