]> git.mxchange.org Git - friendica-addons.git/blobdiff - fbpost/fbpost.php
Setting "editplain", "page" and "privacy_image_cache" as deprecated. Using new cache...
[friendica-addons.git] / fbpost / fbpost.php
index b5e320fc5d136c1a764fa7193a334743482b43ca..2ac558ea8def501329e32c1b58dafceffebadb4f 100644 (file)
@@ -34,6 +34,7 @@ function fbpost_install() {
        register_hook('enotify',          'addon/fbpost/fbpost.php', 'fbpost_enotify');
        register_hook('queue_predeliver', 'addon/fbpost/fbpost.php', 'fbpost_queue_hook');
        register_hook('cron',             'addon/fbpost/fbpost.php', 'fbpost_cron');
+       register_hook('prepare_body',     'addon/fbpost/fbpost.php', 'fbpost_prepare_body');
 }
 
 
@@ -45,6 +46,7 @@ function fbpost_uninstall() {
        unregister_hook('enotify',          'addon/fbpost/fbpost.php', 'fbpost_enotify');
        unregister_hook('queue_predeliver', 'addon/fbpost/fbpost.php', 'fbpost_queue_hook');
        unregister_hook('cron',             'addon/fbpost/fbpost.php', 'fbpost_cron');
+       unregister_hook('prepare_body',     'addon/fbpost/fbpost.php', 'fbpost_prepare_body');
 }
 
 
@@ -69,7 +71,7 @@ function fbpost_init(&$a) {
 
        if(strlen($nick))
                $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
-                               dbesc($nick)
+                       dbesc($nick)
                );
        if(!(isset($r) && count($r)))
                return;
@@ -154,7 +156,7 @@ function fbpost_post(&$a) {
                $result = q("SELECT `installed` FROM `addon` WHERE `name` = 'fbsync' AND `installed`");
                if (count($result) > 0) {
                        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']));
                }
 
                info( t('Settings updated.') . EOL);
@@ -248,7 +250,7 @@ function fbpost_content(&$a) {
                //read_stream,publish_stream,manage_pages,photo_upload,user_groups,offline_access
 
                $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri=' 
-                       . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=export_stream,read_stream,publish_stream,manage_pages,photo_upload,user_groups,publish_actions,user_friends,create_note,share_item,video_upload,status_update">' . t('Install Facebook Post connector for this account.') . '</a>';
+                       . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=export_stream,read_stream,publish_stream,manage_pages,photo_upload,user_groups,publish_actions,user_friends,share_item,video_upload,status_update">' . t('Install Facebook Post connector for this account.') . '</a>';
                $o .= '</div>';
        }
 
@@ -260,7 +262,7 @@ function fbpost_content(&$a) {
                $o .= '<div id="fbpost-enable-wrapper">';
 
                $o .= '<a href="https://www.facebook.com/dialog/oauth?client_id=' . $appid . '&redirect_uri=' 
-                       . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=export_stream,read_stream,publish_stream,manage_pages,photo_upload,user_groups,publish_actions,user_friends,create_note,share_item,video_upload,status_update">' . t('Re-authenticate [This is necessary whenever your Facebook password is changed.]') . '</a>';
+                       . $a->get_baseurl() . '/fbpost/' . $a->user['nickname'] . '&scope=export_stream,read_stream,publish_stream,manage_pages,photo_upload,user_groups,publish_actions,user_friends,share_item,video_upload,status_update">' . t('Re-authenticate [This is necessary whenever your Facebook password is changed.]') . '</a>';
                $o .= '</div>';
 
                $o .= '<div id="fbpost-post-default-form">';
@@ -316,11 +318,11 @@ function fbpost_content(&$a) {
 
                        $o .= '<div class="clear"></div>';
 
-                       $sync_enabled = get_pconfig(local_user(),'fbsync','sync');
+                       $sync_enabled = get_pconfig(local_user(),'fbsync','sync');
                        $checked = (($sync_enabled) ? ' checked="checked" ' : '');
                        $o .= '<input type="checkbox" name="fbsync" value="1"' . $checked . '/>' . ' ' . t('Import Facebook newsfeed.') . EOL;
 
-                       $create_user = get_pconfig(local_user(),'fbsync','create_user');
+                       $create_user = get_pconfig(local_user(),'fbsync','create_user');
                        $checked = (($create_user) ? ' checked="checked" ' : '');
                        $o .= '<input type="checkbox" name="create_user" value="1"' . $checked . '/>' . ' ' . t('Automatically create contacts.') . EOL;
 
@@ -562,12 +564,14 @@ function fbpost_post_hook(&$a,&$b) {
 
                                if ($toplevel) {
                                        require_once("include/plaintext.php");
-                                       $msgarr = plaintext($a, $b, 0, false);
+                                       $msgarr = plaintext($a, $b, 0, false, 9);
                                        $msg = $msgarr["text"];
                                        $link = $msgarr["url"];
-                                       $image = $msgarr["image"];
                                        $linkname = $msgarr["title"];
 
+                                       if ($msgarr["type"] != "video")
+                                               $image = $msgarr["image"];
+
                                        // Fallback - if message is empty
                                        if(!strlen($msg))
                                                $msg = $linkname;
@@ -666,11 +670,13 @@ function fbpost_post_hook(&$a,&$b) {
                                                $postvars['message'] = $msg;
 
                                        $url = 'https://graph.facebook.com/'.$target.'/photos';
-                               } else if (($link != "") or ($image != "") or ($b['title'] == '') or (strlen($msg) < 500)) {
+                               //} else if (($link != "") or ($image != "") or ($b['title'] == '') or (strlen($msg) < 500)) {
+                               } else {
                                        $url = 'https://graph.facebook.com/'.$target.'/feed';
                                        if (!get_pconfig($b['uid'],'facebook','suppress_view_on_friendica') and $b['plink'])
                                                $postvars['actions'] = '{"name": "' . t('View on Friendica') . '", "link": "' .  $b['plink'] . '"}';
-                               } else {
+                               }
+/*                             } else {
                                        // if its only a message and a subject and the message is larger than 500 characters then post it as note
                                        $postvars = array(
                                                'access_token' => $fb_token,
@@ -678,7 +684,7 @@ function fbpost_post_hook(&$a,&$b) {
                                                'subject' => $b['title'],
                                        );
                                        $url = 'https://graph.facebook.com/'.$target.'/notes';
-                               }
+                               } */
 
                                // Post to page?
                                if (!$reply and ($target != "me") and $page_access_token)
@@ -915,6 +921,23 @@ function fbpost_get_app_access_token() {
        }
 }
 
+function fbpost_prepare_body(&$a,&$b) {
+        if ($b["item"]["network"] != NETWORK_FACEBOOK)
+                return;
+
+        if ($b["preview"]) {
+                $msg = $b["item"]["body"];
+
+               require_once("include/bbcode.php");
+               require_once("include/html2plain.php");
+               $msg = bb_CleanPictureLinks($msg);
+               $msg = bbcode($msg, false, false, 2, true);
+               $msg = trim(html2plain($msg, 0));
+
+                $b['html'] = nl2br(htmlspecialchars($msg));
+        }
+}
+
 function fbpost_cron($a,$b) {
        $last = get_config('facebook','last_poll');
 
@@ -946,6 +969,7 @@ function fbpost_cron($a,$b) {
 
 function fbpost_fetchwall($a, $uid) {
        require_once("include/oembed.php");
+       require_once('mod/item.php');
 
        $access_token = get_pconfig($uid,'facebook','access_token');
        $post_to_page = get_pconfig($uid,'facebook','post_to_page');
@@ -1008,6 +1032,13 @@ function fbpost_fetchwall($a, $uid) {
                        $oembed_data = oembed_fetch_url($item->link);
                        $type = $oembed_data->type;
                        $content = "[bookmark=".$item->link."]".$item->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($item->link, "", $_REQUEST["body"]));
+
+                       if (($removedlink == "") OR strstr($_REQUEST["body"], $removedlink))
+                               $_REQUEST["body"] = $removedlink;
+
                } elseif (isset($item->name))
                        $content .= "[b]".$item->name."[/b]";
 
@@ -1026,11 +1057,25 @@ function fbpost_fetchwall($a, $uid) {
 
                        $type = $item->type;
 
-                       if(isset($item->picture) && isset($item->link))
-                               $content .= "\n".'[url='.$item->link.'][img]'.fpost_cleanpicture($item->picture).'[/img][/url]';
+                       if (isset($item->picture))
+                               $picture = $item->picture;
+
+                       if (($type == "photo") AND isset($item->object_id)) {
+                                logger('fbpost_fetchwall: fetching fbid '.$item->object_id, LOGGER_DEBUG);
+                               $url = "https://graph.facebook.com/".$item->object_id."?access_token=".$access_token;
+                               $feed = fetch_url($url);
+                               $data = json_decode($feed);
+                               if (isset($data->images)) {
+                                       $picture = $data->images[0]->source;
+                                       logger('fbpost_fetchwall: got fbid image '.$preview, LOGGER_DEBUG);
+                               }
+                       }
+
+                       if(($picture != "") && isset($item->link))
+                               $content .= "\n".'[url='.$item->link.'][img]'.$picture.'[/img][/url]';
                        else {
-                               if (isset($item->picture))
-                                       $content .= "\n".'[img]'.fpost_cleanpicture($item->picture).'[/img]';
+                               if ($picture != "")
+                                       $content .= "\n".'[img]'.$picture.'[/img]';
                                // if just a link, it may be a wall photo - check
                                if(isset($item->link))
                                        $content .= fbpost_get_photo($uid,$item->link);
@@ -1081,8 +1126,6 @@ function fbpost_fetchwall($a, $uid) {
 
                //print_r($_REQUEST);
                logger('facebook: posting for user '.$uid);
-
-               require_once('mod/item.php');
                item_post($a);
        }
 
@@ -1103,7 +1146,7 @@ function fbpost_get_photo($uid,$link) {
        $x = fetch_url('https://graph.facebook.com/'.$photo_id.'?access_token='.$access_token);
        $j = json_decode($x);
        if($j->picture)
-               return "\n\n".'[url='.$link.'][img]'.fpost_cleanpicture($j->picture).'[/img][/url]';
+               return "\n\n".'[url='.$link.'][img]'.$j->picture.'[/img][/url]';
 
        return "";
 }