]> git.mxchange.org Git - friendica-addons.git/blobdiff - facebook/facebook.php
import images for wall photos (which facebook provides as an unnamed link and not...
[friendica-addons.git] / facebook / facebook.php
index 3c0a2a04fe75dd232b1359822ca87bdf70886447..57212fef9ac89a9fb0d937455bb503e6f1a61b3d 100644 (file)
@@ -6,19 +6,17 @@
  */
 
 /**
- * Installing the Friendika/Facebook connector
+ * Installing the Friendica/Facebook connector
  *
  * 1. register an API key for your site from developer.facebook.com
- *   a. We'd be very happy if you include "Friendika" in the application name
- *      to increase name recognition. The Friendika icons are also present
+ *   a. We'd be very happy if you include "Friendica" in the application name
+ *      to increase name recognition. The Friendica icons are also present
  *      in the images directory and may be uploaded as a Facebook app icon.
- *      Use images/friendika-16.jpg for the Icon and images/friendika-128.jpg for the Logo.
+ *      Use images/friendica-16.jpg for the Icon and images/friendica-128.jpg for the Logo.
  *   b. The url should be your site URL with a trailing slash.
- *      You may use http://portal.friendika.com/privacy as the privacy policy
- *      URL unless your site has different requirements, and 
- *      http://portal.friendika.com as the Terms of Service URL unless
- *      you have different requirements. (Friendika is a software application
- *      and does not require Terms of Service, though your installation of it might).
+ *      Friendica is a software application and does not require a Privacy Policy 
+ *      or Terms of Service, though your installation of it might. Facebook may require
+ *      that you provide a Privacy Policy, which we find ironic.  
  *   c. Set the following values in your .htconfig.php file
  *         $a->config['facebook']['appid'] = 'xxxxxxxxxxx';
  *         $a->config['facebook']['appsecret'] = 'xxxxxxxxxxxxxxx';
@@ -294,6 +292,8 @@ function facebook_post(&$a) {
                set_pconfig($uid,'facebook','private_wall',$private_wall);
        
 
+               set_pconfig($uid,'facebook','blocked_apps',escape_tags(trim($_POST['blocked_apps'])));
+
                $linkvalue = ((x($_POST,'facebook_linking')) ? intval($_POST['facebook_linking']) : 0);
                set_pconfig($uid,'facebook','no_linking', (($linkvalue) ? 0 : 1));
 
@@ -401,6 +401,12 @@ function facebook_content(&$a) {
 
                $o .= '<p>' . t('If you choose to link conversations and leave both of these boxes unchecked, your Facebook profile wall will be merged with your profile wall on this website and your privacy settings on this website will be used to determine who may see the conversations.') . '</p>';
 
+
+               $blocked_apps = get_pconfig(local_user(),'facebook','blocked_apps');
+
+               $o .= '<div><label id="blocked-apps-label" for="blocked-apps">' . t('Comma separated applications to ignore') . ' </label></div>';
+       $o .= '<div><textarea id="blocked-apps" name="blocked_apps" >' . htmlspecialchars($blocked_apps) . '</textarea></div>';
+
                $o .= '<input type="submit" name="submit" value="' . t('Submit') . '" /></form></div>';
        }
 
@@ -505,23 +511,37 @@ function facebook_post_hook(&$a,&$b) {
 
        $toplevel = (($b['id'] == $b['parent']) ? true : false);
 
-       if(strstr($b['postopts'],'facebook')) {
 
-               $linking = ((get_pconfig($b['uid'],'facebook','no_linking')) ? 0 : 1);
+       $linking = ((get_pconfig($b['uid'],'facebook','no_linking')) ? 0 : 1);
 
-               if((! $toplevel) && ($linking)) {
-                       $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
-                               intval($b['parent']),
-                               intval($b['uid'])
-                       );
-                       if(count($r) && substr($r[0]['uri'],0,4) === 'fb::')
-                               $reply = substr($r[0]['uri'],4);
-                       elseif(count($r) && substr($r[0]['extid'],0,4) === 'fb::')
-                               $reply = substr($r[0]['extid'],4);
-                       else
-                               return;
-                       logger('facebook reply id=' . $reply);
-               }
+       if((! $toplevel) && ($linking)) {
+               $r = q("SELECT * FROM `item` WHERE `id` = %d AND `uid` = %d LIMIT 1",
+                       intval($b['parent']),
+                       intval($b['uid'])
+               );
+               if(count($r) && substr($r[0]['uri'],0,4) === 'fb::')
+                       $reply = substr($r[0]['uri'],4);
+               elseif(count($r) && substr($r[0]['extid'],0,4) === 'fb::')
+                       $reply = substr($r[0]['extid'],4);
+               else
+                       return;
+
+               $u = q("SELECT * FROM user where uid = %d limit 1",
+                       intval($b['uid'])
+               );
+               if(! count($u))
+                       return;
+
+               // only accept comments from the item owner. Other contacts are unknown to FB.
+               if(! link_compare($b['author-link'], $a->get_baseurl() . '/profile/' . $u[0]['nickname']))
+                       return;
+               
+
+               logger('facebook reply id=' . $reply);
+       }
+
+       if(strstr($b['postopts'],'facebook') || ($b['private']) || ($reply)) {
 
                if($b['private'] && $reply === false) {
                        $allow_people = expand_acl($b['allow_cid']);
@@ -624,7 +644,7 @@ function facebook_post_hook(&$a,&$b) {
                                $msg = preg_replace("/\[img\](.*?)\[\/img\]/is", t('Image: ') . '$1', $msg);
 
                                if((strpos($link,z_root()) !== false) && (! $image))
-                                       $image = $a->get_baseurl() . '/images/friendika-64.jpg';
+                                       $image = $a->get_baseurl() . '/images/friendica-64.jpg';
 
                                $msg = trim(strip_tags(bbcode($msg)));
                                $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
@@ -681,7 +701,7 @@ function facebook_post_hook(&$a,&$b) {
                                                $postvars['name'] = $linkname;
                                }
 
-                               if(($b['private']) && (! $b['parent'])) {
+                               if(($b['private']) && ($toplevel)) {
                                        $postvars['privacy'] = '{"value": "CUSTOM", "friends": "SOME_FRIENDS"';
                                        if(count($allow_arr))
                                                $postvars['privacy'] .= ',"allow": "' . implode(',',$allow_arr) . '"';
@@ -697,7 +717,7 @@ function facebook_post_hook(&$a,&$b) {
                                else { 
                                        $url = 'https://graph.facebook.com/me/feed';
                                        if($b['plink'])
-                                               $postvars['actions'] = '{"name": "' . t('View on Friendika') . '", "link": "' .  $b['plink'] . '"}';
+                                               $postvars['actions'] = '{"name": "' . t('View on Friendica') . '", "link": "' .  $b['plink'] . '"}';
                                }
 
                                logger('facebook: post to ' . $url);
@@ -743,10 +763,10 @@ function facebook_post_local(&$a,&$b) {
        if((local_user()) && (local_user() == $b['uid'])) {
 
                $fb_post   = intval(get_pconfig(local_user(),'facebook','post'));
-               $fb_enable = (($fb_post && x($_POST,'facebook_enable')) ? intval($_POST['facebook_enable']) : 0);
+               $fb_enable = (($fb_post && x($_REQUEST,'facebook_enable')) ? intval($_REQUEST['facebook_enable']) : 0);
 
                // if API is used, default to the chosen settings
-               if($_POST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default')))
+               if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default')))
                        $fb_enable = 1;
 
                if(! $fb_enable)
@@ -843,6 +863,21 @@ function fb_consume_all($uid) {
 
 }
 
+function fb_get_photo($uid,$link) {
+       $access_token = get_pconfig($uid,'facebook','access_token');
+       if(! $access_token || (! stristr($link,'facebook.com/photo.php')))
+               return "\n" . '[url=' . $link . ']' . t('link') . '[/url]';
+       $ret = preg_match('/fbid=([0-9]*)/',$link,$match);
+       if($ret)
+               $photo_id = $match[1];
+       $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]' . $j->picture . '[/img][/url]';
+       else
+               return "\n" . '[url=' . $link . ']' . t('link') . '[/url]';
+}
+
 function fb_consume_stream($uid,$j,$wall = false) {
 
        $a = get_app();
@@ -864,6 +899,8 @@ function fb_consume_stream($uid,$j,$wall = false) {
                intval($uid)
        );
 
+       $blocked_apps = get_pconfig($uid,'facebook','blocked_apps');
+       $blocked_apps_arr = explode(',',$blocked_apps);
 
        $self_id = get_pconfig($uid,'facebook','self_id');
        if(! count($j->data) || (! strlen($self_id)))
@@ -922,16 +959,39 @@ function fb_consume_stream($uid,$j,$wall = false) {
                                $datarray['app'] = strip_tags($entry->application->name);
                        else
                                $datarray['app'] = 'facebook';
+
+                       $found_blocked = false;
+
+                       if(count($blocked_apps_arr)) {
+                               foreach($blocked_apps_arr as $bad_appl) {
+                                       if(strlen(trim($bad_appl)) && (stristr($datarray['app'],trim($bad_appl)))) {
+                                               $found_blocked = true;
+                                       }
+                               }
+                       }
+                               
+                       if($found_blocked) {
+                               logger('facebook: blocking application: ' . $datarray['app']);
+                               continue;
+                       }
+
                        $datarray['author-name'] = $from->name;
                        $datarray['author-link'] = 'http://facebook.com/profile.php?id=' . $from->id;
                        $datarray['author-avatar'] = 'https://graph.facebook.com/' . $from->id . '/picture';
                        $datarray['plink'] = $datarray['author-link'] . '&v=wall&story_fbid=' . substr($entry->id,strpos($entry->id,'_') + 1);
 
-                       $datarray['body'] = $entry->message;
-                       if($entry->picture)
-                               $datarray['body'] .= "\n\n" . '[img]' . $entry->picture . '[/img]';
-                       if($entry->link)
-                               $datarray['body'] .= "\n" . linkify($entry->link);
+                       $datarray['body'] = escape_tags($entry->message);
+
+                       if($entry->picture && $entry->link) {
+                               $datarray['body'] .= "\n\n" . '[url=' . $entry->link . '][img]' . $entry->picture . '[/img][/url]';
+                       }
+                       else {
+                               if($entry->picture)
+                                       $datarray['body'] .= "\n\n" . '[img]' . $entry->picture . '[/img]';
+                               // if just a link, it may be a wall photo - check
+                               if($entry->link)
+                                       $datarray['body'] .= fb_get_photo($uid,$entry->link);
+                       }
                        if($entry->name)
                                $datarray['body'] .= "\n" . $entry->name;
                        if($entry->caption)