X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=fbsync%2Ffbsync.php;h=07f6dcf11254e98fa6be9dc2e25f54980ce9f5ef;hb=18784a484b04b5f74e6d4dca516b2dc20cffe953;hp=9abac7dc57cbdff8ff1190d783c2926a54149113;hpb=fb14c7094ca2ffe4afee583000dc93b6e9158a4d;p=friendica-addons.git diff --git a/fbsync/fbsync.php b/fbsync/fbsync.php index 9abac7dc..07f6dcf1 100644 --- a/fbsync/fbsync.php +++ b/fbsync/fbsync.php @@ -4,6 +4,7 @@ * Description: Synchronizes the Facebook Newsfeed * Version: 1.0 * Author: Michael Vogel + * Status: Unsupported */ /* To-Do @@ -41,7 +42,7 @@ 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")) + 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 @@ -172,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']); @@ -213,7 +229,11 @@ function fbsync_expire($a,$b) { 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", @@ -229,6 +249,7 @@ 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; @@ -246,7 +267,7 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $contact_id = 0; - if (($post->parent_post_id != "") AND ($post->actor_id == $post->source_id)) { + if (($post->parent_post_id != "") && ($post->actor_id == $post->source_id)) { $pos = strpos($post->parent_post_id, "_"); if ($pos != 0) { @@ -258,7 +279,7 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $postarray['contact-id'] = $contact_id; - if (array_key_exists("name", $userdata) AND ($userdata["name"] != "") AND !link_compare($userdata["link"], $postarray['author-link'])) { + 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"]; @@ -282,7 +303,7 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr // Testing if we know the source or the actor $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], false); - if (($contact_id == 0) and array_key_exists($post->actor_id, $contacts)) + 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 @@ -295,7 +316,7 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr if ($contact_id == -1) { logger('fbsync_createpost: Contact is blocked. Post not imported '.print_r($post, true), LOGGER_DEBUG); return; - } elseif (($contact_id <= 0) AND !$create_user) { + } 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) { @@ -314,73 +335,115 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr $postarray["body"] = $msgdata["body"]; $postarray["tag"] = $msgdata["tags"]; + // 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 = ""; - $type = ""; + $pagedata["type"] = ""; - if (isset($post->attachment->name) and isset($post->attachment->href)) { + 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); - $type = $oembed_data->type; - if ($type == "rich") - $type = "link"; + $pagedata["type"] = $oembed_data->type; + if ($pagedata["type"] == "rich") + $pagedata["type"] = "link"; + $pagedata["url"] = $post->attachment->href; + $pagedata["title"] = $post->attachment->name; $content = "[bookmark=".$post->attachment->href."]".$post->attachment->name."[/bookmark]"; - } elseif (isset($post->attachment->name) AND ($post->attachment->name != "")) + + // 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 (($removedlink == "") || strstr($postarray["body"], $removedlink)) + $postarray["body"] = $removedlink; + + } elseif (isset($post->attachment->name) && ($post->attachment->name != "")) $content = "[b]" . $post->attachment->name."[/b]"; - $quote = ""; - if(isset($post->attachment->description) and ($post->attachment->fb_object_type != "photo")) - $quote = $post->attachment->description; + $pagedata["text"] = ""; + if (isset($post->attachment->description) && ($post->attachment->fb_object_type != "photo")) + $pagedata["text"] = $post->attachment->description; - if(isset($post->attachment->caption) and ($post->attachment->fb_object_type == "photo")) - $quote = $post->attachment->caption; + if (isset($post->attachment->caption) && ($post->attachment->fb_object_type == "photo")) + $pagedata["text"] = $post->attachment->caption; - if ($quote.$post->attachment->href.$content.$postarray["body"] == "") + 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/")) - AND (($type == "") OR ($type == "link"))) { + 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->type)) - $type = $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); + } + } - if(isset($media->src) && isset($media->href) AND ($media->src != "") AND ($media->href != "")) - $content .= "\n".'[url='.$media->href.'][img]'.fpost_cleanpicture($media->src).'[/img][/url]'; - else { - if (isset($media->src) AND ($media->src != "")) - $content .= "\n".'[img]'.fpost_cleanpicture($media->src).'[/img]'; + $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)) + if (isset($post->link)) $content .= fbpost_get_photo($media->href); } } } - if ($content) - $postarray["body"] .= "\n"; - - if ($type) - $postarray["body"] .= "[class=type-".$type."]"; + if ($pagedata["type"] != "") { + if ($pagedata["type"] == "link") + $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK; - if ($content) - $postarray["body"] .= trim($content); - - if ($quote) - $postarray["body"] .= "\n[quote]".trim($quote)."[/quote]"; + $postarray["body"] .= add_page_info_data($pagedata); + } else { + if ($content) + $postarray["body"] .= "\n".trim($content); - if ($type) - $postarray["body"] .= "[/class]"; + 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; @@ -475,7 +538,7 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl } // Is blocked? Then return - if ($r[0]["readonly"] OR $r[0]["blocked"]) { + if ($r[0]["readonly"] || $r[0]["blocked"]) { logger("fbsync_createcomment: UID ".$uid." - Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); return; } @@ -490,6 +553,7 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl $postarray['wall'] = 0; $postarray['verb'] = ACTIVITY_POST; + $postarray['object-type'] = ACTIVITY_OBJ_COMMENT; $postarray['network'] = dbesc(NETWORK_FACEBOOK); $postarray['uri'] = "fb::".$comment->id; @@ -552,6 +616,8 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl return; $item = item_store($postarray); + $postarray["id"] = $item; + logger('fbsync_createcomment: UID '.$uid.' - CID '.$postarray['contact-id'].' - Nick '.$contact_nick.' posted comment '.$item, LOGGER_DEBUG); if ($item == 0) @@ -574,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'); @@ -589,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'], @@ -641,8 +707,11 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) { $likedata = array(); $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; @@ -716,10 +785,10 @@ 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(!count($r) AND !$create_user) + if(!count($r) && !$create_user) return(0); - if (count($r) AND ($r[0]["readonly"] OR $r[0]["blocked"])) { + 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); } @@ -901,7 +970,7 @@ function fbsync_fetchuser($a, $uid, $id) { intval($uid), dbesc("facebook::".$id)); if (count($contact)) { - if (($contact[0]["readonly"] OR $contact[0]["blocked"])) { + 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 @@ -956,7 +1025,7 @@ function fbsync_fetchfeed($a, $uid) { require_once('include/items.php'); - //if ($last_updated == "") + if ($last_updated == "") $last_updated = 0; logger("fbsync_fetchfeed: fetching content for user ".$self_id);