]> git.mxchange.org Git - friendica-addons.git/blobdiff - fbsync/fbsync.php
Merge pull request #384 from annando/1609-ignorelist-rendertime
[friendica-addons.git] / fbsync / fbsync.php
index 509426d9408b4bfae7a7fa2648a577de20d1e64e..5a429ad81137c309be06d0c09789ddc5be0930a6 100644 (file)
@@ -4,6 +4,7 @@
  * Description: Synchronizes the Facebook Newsfeed
  * Version: 1.0
  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
+ * Status: Unsupported
  */
 
 /* To-Do
@@ -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",
@@ -333,47 +353,75 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr
                                break;
                }
 
+       $pagedata = array();
        $content = "";
-       $type = "";
+       $pagedata["type"] = "";
 
        if (isset($post->attachment->name) and 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]";
+
+               // 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 == "") OR strstr($postarray["body"], $removedlink))
+                       $postarray["body"] = $removedlink;
+
        } elseif (isset($post->attachment->name) AND ($post->attachment->name != ""))
                $content = "[b]" . $post->attachment->name."[/b]";
 
-       $quote = "";
+       $pagedata["text"] = "";
        if (isset($post->attachment->description) and ($post->attachment->fb_object_type != "photo"))
-               $quote = $post->attachment->description;
+               $pagedata["text"] = $post->attachment->description;
 
        if (isset($post->attachment->caption) and ($post->attachment->fb_object_type == "photo"))
-               $quote = $post->attachment->caption;
+               $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 (($type == "") OR ($type == "link"))) {
+       if (isset($post->attachment->media) AND (($pagedata["type"] == "") OR ($pagedata["type"] == "link"))) {
                foreach ($post->attachment->media AS $media) {
 
                        if (isset($media->type))
-                               $type = $media->type;
+                               $pagedata["type"] = $media->type;
 
                        if (isset($media->src))
-                               $preview = $media->src;
+                               $pagedata["images"][0]["src"] = $media->src;
 
-                       if (isset($media->photo))
+                       if (isset($media->photo)) {
                                if (isset($media->photo->images) AND (count($media->photo->images) > 1))
-                                       $preview = $media->photo->images[1]->src;
+                                       $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) AND ($preview != "") AND ($media->href != ""))
-                               $content .= "\n".'[url='.$media->href.'][img]'.$preview.'[/img][/url]';
-                       else {
-                               if ($preview != "")
-                                       $content .= "\n".'[img]'.$preview.'[/img]';
+                       if (isset($media->href) AND ($pagedata["images"][0]["src"] != "") AND ($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))
@@ -382,25 +430,20 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr
                }
        }
 
-       if ($type == "link")
-               $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK;
-
-       if ($content)
-               $postarray["body"] .= "\n";
+       if ($pagedata["type"] != "") {
+               if ($pagedata["type"] == "link")
+                       $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK;
 
-       if ($type)
-               $postarray["body"] .= "[class=type-".$type."]";
-
-       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;
@@ -573,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)
@@ -610,8 +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/'.get_item_guid($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'],
@@ -981,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);
@@ -1001,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);
-       file_put_contents("fb.".$uid);
        $data = json_decode($feed);
 
        if (!is_array($data->data)) {