]> git.mxchange.org Git - friendica-addons.git/blobdiff - facebook/facebook.php
community home: update tgz
[friendica-addons.git] / facebook / facebook.php
index e7e2ab3e95d0bfcf727d790c9f707edfc03cbb44..f977bef90192f469097bc26d3cb729aab93447f9 100644 (file)
@@ -3,7 +3,8 @@
  * Name: Facebook Connector
  * Version: 1.3
  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
- *         Tobias Hößl <https://github.com/CatoTH/>
+ * Author: Tobias Hößl <https://github.com/CatoTH/>
+ *
  */
 
 /**
@@ -162,10 +163,11 @@ function facebook_init(&$a) {
                }
        }
 
-
        if($a->argc != 2)
                return;
+
        $nick = $a->argv[1];
+
        if(strlen($nick))
                $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
                                dbesc($nick)
@@ -270,9 +272,13 @@ function fb_get_friends_sync_parsecontact($uid, $contact) {
 
     if(count($r)) {
 
+               // update profile photos once every two weeks as we have no notification of when they change.
+
+               $update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
+
         // 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'])) {
+        if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
             require_once("Photo.php");
 
             $photos = import_profile_photo('https://graph.facebook.com/' . $contact->id . '/picture', $uid, $r[0]['id']);
@@ -312,7 +318,7 @@ function fb_get_friends_sync_parsecontact($uid, $contact) {
             dbesc($contact->id),
             dbesc('facebook ' . $contact->id),
             dbesc($contact->name),
-            dbesc(($contact->nickname) ? $contact->nickname : strtolower($contact->first_name)),
+            dbesc(($contact->nickname) ? $contact->nickname : mb_convert_case($contact->first_name, MB_CASE_LOWER, "UTF-8")),
             dbesc('https://graph.facebook.com/' . $contact->id . '/picture'),
             dbesc(NETWORK_FACEBOOK),
             intval(CONTACT_IS_FRIEND),
@@ -412,7 +418,7 @@ function fb_get_friends($uid, $fullsync = true) {
                return;
        $s = fetch_url('https://graph.facebook.com/me/friends?access_token=' . $access_token);
        if($s) {
-               logger('facebook: fb_get_friends: ' . $s, LOGGER_DATA);
+               logger('facebook: fb_gwet_friends: ' . $s, LOGGER_DATA);
                $j = json_decode($s);
                logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
                if(! $j->data)
@@ -505,6 +511,13 @@ function facebook_content(&$a) {
                return '';
        }
 
+
+       if(! service_class_allows(local_user(),'facebook_connect')) {
+               notice( t('Permission denied.') . EOL);
+               return upgrade_bool_message();
+       }
+
+
        if($a->argc > 1 && $a->argv[1] === 'remove') {
                del_pconfig(local_user(),'facebook','post');
                info( t('Facebook disabled') . EOL);
@@ -747,7 +760,7 @@ function facebook_plugin_admin(&$a, &$o){
                elseif (is_array($subs)) {
                        $o .= t('The given API Key seems to work correctly.') . '<br>';
                        $working_connection = true;
-               } else $o .= t('The correctness of the API Key could not be detected. Somthing strange\'s going on.') . '<br>';
+               } else $o .= t('The correctness of the API Key could not be detected. Something strange\'s going on.') . '<br>';
        }
        
        $o .= '<label for="fb_appid">' . t('App-ID / API-Key') . '</label><input id="fb_appid" name="appid" type="text" value="' . escape_tags($appid ? $appid : "") . '"><br style="clear: both;">';
@@ -937,7 +950,7 @@ function facebook_post_hook(&$a,&$b) {
                        if($fb_token && ($toplevel || $b['private'] || $reply)) {
                                logger('facebook: able to post');
                                require_once('library/facebook.php');
-                               require_once('include/bbcode.php');     
+                               require_once('include/bbcode.php');
 
                                $msg = $b['body'];
 
@@ -993,6 +1006,10 @@ function facebook_post_hook(&$a,&$b) {
                                        if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
                                                $image = $matches[1];
 
+                               // When saved into the database the content is sent through htmlspecialchars
+                               // That means that we have to decode all image-urls
+                               $image = htmlspecialchars_decode($image);
+
                                // Checking for a bookmark element
                                $body = $b['body'];
                                if (strpos($body, "[bookmark") !== false) {
@@ -1012,7 +1029,7 @@ function facebook_post_hook(&$a,&$b) {
                                }
 
                                // At first convert the text to html
-                               $html = bbcode($body);
+                               $html = bbcode($body, false, false);
 
                                // Then convert it to plain text
                                $msg = trim($b['title']." \n\n".html2plain($html, 0, true));
@@ -1076,13 +1093,13 @@ function facebook_post_hook(&$a,&$b) {
 
                                // Fallback - if message is empty
                                if(!strlen($msg))
-                                       $msg = $link;
+                                       $msg = $linkname;
 
                                if(!strlen($msg))
-                                       $msg = $image;
+                                       $msg = $link;
 
                                if(!strlen($msg))
-                                       $msg = $linkname;
+                                       $msg = $image;
 
                                // If there is nothing to post then exit
                                if(!strlen($msg))
@@ -1090,23 +1107,27 @@ function facebook_post_hook(&$a,&$b) {
 
                                logger('Facebook post: msg=' . $msg, LOGGER_DATA);
 
-                               if($likes) { 
+                               if($likes) {
                                        $postvars = array('access_token' => $fb_token);
                                }
                                else {
+                                       // message, picture, link, name, caption, description, source, place, tags
                                        $postvars = array(
-                                               'access_token' => $fb_token, 
+                                               'access_token' => $fb_token,
                                                'message' => $msg
                                        );
-                                       if(isset($image)) {
+                                       if(trim($image) != "") {
                                                $postvars['picture'] = $image;
-                                               //$postvars['type'] = "photo";
                                        }
-                                       if(isset($link)) {
+                                       if(trim($link) != "") {
                                                $postvars['link'] = $link;
-                                               //$postvars['type'] = "link";
+
+                                               // The following doesn't work - why?
+                                               if ((stristr($link,'youtube')) || (stristr($link,'youtu.be')) || (stristr($link,'vimeo'))) {
+                                                       $postvars['source'] = $link;
+                                               }
                                        }
-                                       if(isset($linkname))
+                                       if(trim($linkname) != "")
                                                $postvars['name'] = $linkname;
                                }
 
@@ -1122,15 +1143,15 @@ function facebook_post_hook(&$a,&$b) {
 
                                if($reply) {
                                        $url = 'https://graph.facebook.com/' . $reply . '/' . (($likes) ? 'likes' : 'comments');
-                               } else if (($link != "")  or ($image != "") or ($b['title'] == '') or (strlen($msg) < 500)) { 
+                               } else if (($link != "")  or ($image != "") or ($b['title'] == '') or (strlen($msg) < 500)) {
                                        $url = 'https://graph.facebook.com/me/feed';
                                        if($b['plink'])
                                                $postvars['actions'] = '{"name": "' . t('View on Friendica') . '", "link": "' .  $b['plink'] . '"}';
                                } 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, 
-                                               'message' => bbcode($b['body']),
+                                               'access_token' => $fb_token,
+                                               'message' => bbcode($b['body'], false, false),
                                                'subject' => $b['title'],
                                        );
                                        $url = 'https://graph.facebook.com/me/notes';
@@ -1221,8 +1242,12 @@ function facebook_post_local(&$a,&$b) {
                $fb_enable = (($fb_post && x($_REQUEST,'facebook_enable')) ? intval($_REQUEST['facebook_enable']) : 0);
 
                // if API is used, default to the chosen settings
-               if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default')))
-                       $fb_enable = 1;
+               // but allow a specific override
+
+               if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'facebook','post_by_default'))) {
+                       if(! x($_REQUEST,'facebook_enable'))
+                               $fb_enable = 1;
+               }
 
                if(! $fb_enable)
                        return;
@@ -1767,7 +1792,7 @@ function fb_consume_stream($uid,$j,$wall = false) {
                        // oembed display a picture of the video as well 
                        if ($entry->type != "video") {
                                if(isset($entry->picture) && isset($entry->link)) {
-                                       $datarray['body'] .= "\n" . '[url=' . $entry->link . '][img]'.$entry->picture.'[/img][/url]';   
+                                       $datarray['body'] .= "\n" . '[url=' . $entry->link . '][img]'.$entry->picture.'[/img][/url]';
                                }
                                else {
                                        if(isset($entry->picture))