]> git.mxchange.org Git - friendica-addons.git/blobdiff - facebook/facebook.php
Merge pull request #48 from Leberwurscht/master
[friendica-addons.git] / facebook / facebook.php
old mode 100755 (executable)
new mode 100644 (file)
index cb1fdee..4f534a0
@@ -1,7 +1,7 @@
 <?php
 /**
  * Name: Facebook Connector
- * Version: 1.1
+ * Version: 1.2
  * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
  *         Tobias Hößl <https://github.com/CatoTH/>
  */
@@ -9,33 +9,8 @@
 /**
  * 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 "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/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.
- *      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';
- *      Replace with the settings Facebook gives you.
- *   d. Navigate to Set Web->Site URL & Domain -> Website Settings.  Set 
- *      Site URL to yoursubdomain.yourdomain.com. Set Site Domain to your 
- *      yourdomain.com.
- * 2. Visit the Facebook Settings section of the "Settings->Plugin Settings" page.
- *    and click 'Install Facebook Connector'.
- * 3. Visit the Facebook Settings section of the "Settings->Plugin Settings" page.
- *    and click 'Install Facebook Connector'.
- * 4. This will ask you to login to Facebook and grant permission to the 
- *    plugin to do its stuff. Allow it to do so. 
- * 5. Optional step: If you want to use Facebook Real Time Updates (so new messages
- *    and new contacts are added ~1min after they are postet / added on FB), go to
- *    Settings -> plugins -> facebook and press the "Activate Real-Time Updates"-button.
- * 6. You're done. To turn it off visit the Plugin Settings page again and
- *    'Remove Facebook posting'.
+ * Detailed instructions how to use this plugin can be found at
+ * https://github.com/friendica/friendica/wiki/How-to:-Friendica%E2%80%99s-Facebook-connector
  *
  * Vidoes and embeds will not be posted if there is no other content. Links 
  * and images will be converted to a format suitable for the Facebook API and 
@@ -96,8 +71,11 @@ function facebook_module() {}
 // If a->argv[1] is a nickname, this is a callback from Facebook oauth requests.
 // If $_REQUEST["realtime_cb"] is set, this is a callback from the Real-Time Updates API
 
+/**
+ * @param App $a
+ */
 function facebook_init(&$a) {
-       
+
        if (x($_REQUEST, "realtime_cb") && x($_REQUEST, "realtime_cb")) {
                logger("facebook_init: Facebook Real-Time callback called", LOGGER_DEBUG);
                
@@ -191,7 +169,7 @@ function facebook_init(&$a) {
                $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1",
                                dbesc($nick)
                );
-       if(! count($r))
+       if(!(isset($r) && count($r)))
                return;
 
        $uid           = $r[0]['uid'];
@@ -233,6 +211,9 @@ function facebook_init(&$a) {
 }
 
 
+/**
+ * @param int $uid
+ */
 function fb_get_self($uid) {
        $access_token = get_pconfig($uid,'facebook','access_token');
        if(! $access_token)
@@ -244,133 +225,165 @@ function fb_get_self($uid) {
        }
 }
 
-function fb_get_friends_sync_new($uid, $access_token, $person) {
-       $link = 'http://facebook.com/profile.php?id=' . $person->id;
-       
-       $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
-               intval($uid),
-               dbesc($link)
-       );
-       
-       if (count($r) == 0) {
-               logger('fb_get_friends: new contact found: ' . $link, LOGGER_DEBUG);
-               
-               fb_get_friends_sync_full($uid, $access_token, $person);
-       }
-}
-
-function fb_get_friends_sync_full($uid, $access_token, $person) {
-       $s = fetch_url('https://graph.facebook.com/' . $person->id . '?access_token=' . $access_token);
-       if($s) {
-               $jp = json_decode($s);
-               logger('fb_get_friends: info: ' . print_r($jp,true), LOGGER_DATA);
-
-               // always use numeric link for consistency
-
-               $jp->link = 'http://facebook.com/profile.php?id=' . $person->id;
-
-               // If its a page then set the first name from the username
-               if (!$jp->first_name and $jp->username)
-                       $jp->first_name = $jp->username;
-
-               // check if we already have a contact
+/**
+ * @param int $uid
+ * @param string $access_token
+ * @param array $persons
+ */
+function fb_get_friends_sync_new($uid, $access_token, $persons) {
+    $persons_todo = array();
+    foreach ($persons as $person) {
+        $link = 'http://facebook.com/profile.php?id=' . $person->id;
+
+        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
+            intval($uid),
+            dbesc($link)
+        );
+
+        if (count($r) == 0) {
+            logger('fb_get_friends: new contact found: ' . $link, LOGGER_DEBUG);
+            $persons_todo[] = $person;
+        }
 
-               $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
-                       intval($uid),
-                       dbesc($jp->link)
-               );                      
+        if (count($persons_todo) > 0) fb_get_friends_sync_full($uid, $access_token, $persons_todo);
+    }
+}
 
-               if(count($r)) {
+/**
+ * @param int $uid
+ * @param object $contact
+ */
+function fb_get_friends_sync_parsecontact($uid, $contact) {
+    $contact->link = 'http://facebook.com/profile.php?id=' . $contact->id;
+
+    // If its a page then set the first name from the username
+    if (!$contact->first_name and $contact->username)
+        $contact->first_name = $contact->username;
+
+    // check if we already have a contact
+
+    $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
+        intval($uid),
+        dbesc($contact->link)
+    );
+
+    if(count($r)) {
+
+        // 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'])) {
+            require_once("Photo.php");
+
+            $photos = import_profile_photo('https://graph.facebook.com/' . $contact->id . '/picture', $uid, $r[0]['id']);
+
+            q("UPDATE `contact` SET `photo` = '%s',
+                                        `thumb` = '%s',
+                                        `micro` = '%s',
+                                        `name-date` = '%s',
+                                        `uri-date` = '%s',
+                                        `avatar-date` = '%s'
+                                        WHERE `id` = %d LIMIT 1
+                                ",
+                dbesc($photos[0]),
+                dbesc($photos[1]),
+                dbesc($photos[2]),
+                dbesc(datetime_convert()),
+                dbesc(datetime_convert()),
+                dbesc(datetime_convert()),
+                intval($r[0]['id'])
+            );
+        }
+        return;
+    }
+    else {
+
+        // create contact record
+        q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
+                                `name`, `nick`, `photo`, `network`, `rel`, `priority`,
+                                `writable`, `blocked`, `readonly`, `pending` )
+                                VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
+            intval($uid),
+            dbesc(datetime_convert()),
+            dbesc($contact->link),
+            dbesc(normalise_link($contact->link)),
+            dbesc(''),
+            dbesc(''),
+            dbesc($contact->id),
+            dbesc('facebook ' . $contact->id),
+            dbesc($contact->name),
+            dbesc(($contact->nickname) ? $contact->nickname : strtolower($contact->first_name)),
+            dbesc('https://graph.facebook.com/' . $contact->id . '/picture'),
+            dbesc(NETWORK_FACEBOOK),
+            intval(CONTACT_IS_FRIEND),
+            intval(1),
+            intval(1)
+        );
+    }
 
-                       // 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'])) {  
-                               require_once("Photo.php");
-
-                               $photos = import_profile_photo('https://graph.facebook.com/' . $jp->id . '/picture', $uid, $r[0]['id']);
-
-                               $r = q("UPDATE `contact` SET `photo` = '%s', 
-                                       `thumb` = '%s',
-                                       `micro` = '%s', 
-                                       `name-date` = '%s', 
-                                       `uri-date` = '%s', 
-                                       `avatar-date` = '%s'
-                                       WHERE `id` = %d LIMIT 1
-                               ",
-                                       dbesc($photos[0]),
-                                       dbesc($photos[1]),
-                                       dbesc($photos[2]),
-                                       dbesc(datetime_convert()),
-                                       dbesc(datetime_convert()),
-                                       dbesc(datetime_convert()),
-                                       intval($r[0]['id'])
-                               );                      
-                       }       
-                       return;
-               }
-               else {
+    $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
+        dbesc($contact->link),
+        intval($uid)
+    );
 
-                       // create contact record 
-                       $r = q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`, 
-                               `name`, `nick`, `photo`, `network`, `rel`, `priority`,
-                               `writable`, `blocked`, `readonly`, `pending` )
-                               VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
-                               intval($uid),
-                               dbesc(datetime_convert()),
-                               dbesc($jp->link),
-                               dbesc(normalise_link($jp->link)),
-                               dbesc(''),
-                               dbesc(''),
-                               dbesc($jp->id),
-                               dbesc('facebook ' . $jp->id),
-                               dbesc($jp->name),
-                               dbesc(($jp->nickname) ? $jp->nickname : strtolower($jp->first_name)),
-                               dbesc('https://graph.facebook.com/' . $jp->id . '/picture'),
-                               dbesc(NETWORK_FACEBOOK),
-                               intval(CONTACT_IS_FRIEND),
-                               intval(1),
-                               intval(1)
-                       );
-               }
+    if(! count($r)) {
+        return;
+    }
 
-               $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc($jp->link),
-                       intval($uid)
-               );
+    $contact_id  = $r[0]['id'];
+
+    require_once("Photo.php");
+
+    $photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
+
+    q("UPDATE `contact` SET `photo` = '%s',
+                        `thumb` = '%s',
+                        `micro` = '%s',
+                        `name-date` = '%s',
+                        `uri-date` = '%s',
+                        `avatar-date` = '%s'
+                        WHERE `id` = %d LIMIT 1
+                ",
+        dbesc($photos[0]),
+        dbesc($photos[1]),
+        dbesc($photos[2]),
+        dbesc(datetime_convert()),
+        dbesc(datetime_convert()),
+        dbesc(datetime_convert()),
+        intval($contact_id)
+    );
+}
 
-               if(! count($r)) {
-                       return;
-               }
+/**
+ * @param int $uid
+ * @param string $access_token
+ * @param array $persons
+ */
+function fb_get_friends_sync_full($uid, $access_token, $persons) {
+    if (count($persons) == 0) return;
+    $nums = Ceil(count($persons) / 50);
+    for ($i = 0; $i < $nums; $i++) {
+        $batch_request = array();
+        for ($j = $i * 50; $j < ($i+1) * 50 && $j < count($persons); $j++) $batch_request[] = array('method'=>'GET', 'relative_url'=>$persons[$j]->id);
+        $s = post_url('https://graph.facebook.com/', array('access_token' => $access_token, 'batch' => json_encode($batch_request)));
+        if($s) {
+            $results = json_decode($s);
+            logger('fb_get_friends: info: ' . print_r($results,true), LOGGER_DATA);
+            foreach ($results as $contact) {
+                if ($contact->code != 200) logger('fb_get_friends: not found: ' . print_r($contact,true), LOGGER_DEBUG);
+                else fb_get_friends_sync_parsecontact($uid, json_decode($contact->body));
+            }
+        }
+    }
+}
 
-               $contact = $r[0];
-               $contact_id  = $r[0]['id'];
-
-               require_once("Photo.php");
-
-               $photos = import_profile_photo($r[0]['photo'],$uid,$contact_id);
-
-               $r = q("UPDATE `contact` SET `photo` = '%s', 
-                       `thumb` = '%s',
-                       `micro` = '%s', 
-                       `name-date` = '%s', 
-                       `uri-date` = '%s', 
-                       `avatar-date` = '%s'
-                       WHERE `id` = %d LIMIT 1
-               ",
-                       dbesc($photos[0]),
-                       dbesc($photos[1]),
-                       dbesc($photos[2]),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       intval($contact_id)
-               );                      
 
-       }
-}
 
 // if $fullsync is true, only new contacts are searched for
 
+/**
+ * @param int $uid
+ * @param bool $fullsync
+ */
 function fb_get_friends($uid, $fullsync = true) {
 
        $r = q("SELECT `uid` FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
@@ -394,17 +407,23 @@ function fb_get_friends($uid, $fullsync = true) {
                logger('facebook: fb_get_friends: json: ' . print_r($j,true), LOGGER_DATA);
                if(! $j->data)
                        return;
-               foreach($j->data as $person)
-                       if ($fullsync)
-                               fb_get_friends_sync_full($uid, $access_token, $person);
-                       else
-                               fb_get_friends_sync_new($uid, $access_token, $person);
+
+           $persons_todo = array();
+        foreach($j->data as $person) $persons_todo[] = $person;
+
+        if ($fullsync)
+            fb_get_friends_sync_full($uid, $access_token, $persons_todo);
+        else
+            fb_get_friends_sync_new($uid, $access_token, $persons_todo);
        }
 }
 
 // This is the POST method to the facebook settings page
 // Content is posted to Facebook in the function facebook_post_hook() 
 
+/**
+ * @param App $a
+ */
 function facebook_post(&$a) {
 
        $uid = local_user();
@@ -455,6 +474,10 @@ function facebook_post(&$a) {
 
 // Facebook settings form
 
+/**
+ * @param App $a
+ * @return string
+ */
 function facebook_content(&$a) {
 
        if(! local_user()) {
@@ -478,7 +501,6 @@ function facebook_content(&$a) {
        if (get_pconfig(local_user(),'facebook','post')) {
                $access_token = get_pconfig(local_user(),'facebook','access_token');
                if ($access_token) {
-                       $private_wall = intval(get_pconfig($uid,'facebook','private_wall'));
                        $s = fetch_url('https://graph.facebook.com/me/feed?access_token=' . $access_token);
                        if($s) {
                                $j = json_decode($s);
@@ -556,7 +578,11 @@ function facebook_content(&$a) {
 }
 
 
-
+/**
+ * @param App $a
+ * @param null|object $b
+ * @return mixed
+ */
 function facebook_cron($a,$b) {
 
        $last = get_config('facebook','last_poll');
@@ -597,6 +623,8 @@ function facebook_cron($a,$b) {
                        $last_friend_check = get_pconfig($rr['uid'],'facebook','friend_check');
                        if($last_friend_check) 
                                $next_friend_check = $last_friend_check + 86400;
+                       else
+                           $next_friend_check = 0;
                        if($next_friend_check <= time()) {
                                fb_get_friends($rr['uid'], true);
                                set_pconfig($rr['uid'],'facebook','friend_check',time());
@@ -617,7 +645,7 @@ function facebook_cron($a,$b) {
                                logger('facebook_cron: Failed', LOGGER_NORMAL);
                                
                                if(strlen($a->config['admin_email']) && !get_config('facebook', 'realtime_err_mailsent')) {
-                                       $res = mail($a->config['admin_email'], t('Problems with Facebook Real-Time Updates'), 
+                                       mail($a->config['admin_email'], t('Problems with Facebook Real-Time Updates'),
                                                "Hi!\n\nThere's a problem with the Facebook Real-Time Updates that cannot be solved automatically. Maybe a permission issue?\n\nPlease try to re-activate it on " . $a->config["system"]["url"] . "/admin/plugins/facebook\n\nThis e-mail will only be sent once.",
                                                'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
                                                . 'Content-type: text/plain; charset=UTF-8' . "\n"
@@ -637,7 +665,10 @@ function facebook_cron($a,$b) {
 }
 
 
-
+/**
+ * @param App $a
+ * @param null|object $b
+ */
 function facebook_plugin_settings(&$a,&$b) {
 
        $b .= '<div class="settings-block">';
@@ -648,6 +679,10 @@ function facebook_plugin_settings(&$a,&$b) {
 }
 
 
+/**
+ * @param App $a
+ * @param null|object $o
+ */
 function facebook_plugin_admin(&$a, &$o){
 
 
@@ -692,6 +727,10 @@ function facebook_plugin_admin(&$a, &$o){
        }
 }
 
+/**
+ * @param App $a
+ * @param null|object $o
+ */
 function facebook_plugin_admin_post(&$a, &$o){
        check_form_security_token_redirectOnErr('/admin/plugins/facebook', 'fbsave');
        
@@ -711,6 +750,11 @@ function facebook_plugin_admin_post(&$a, &$o){
        }
 }
 
+/**
+ * @param App $a
+ * @param object $b
+ * @return mixed
+ */
 function facebook_jot_nets(&$a,&$b) {
        if(! local_user())
                return;
@@ -725,6 +769,11 @@ function facebook_jot_nets(&$a,&$b) {
 }
 
 
+/**
+ * @param App $a
+ * @param object $b
+ * @return mixed
+ */
 function facebook_post_hook(&$a,&$b) {
 
 
@@ -743,6 +792,9 @@ function facebook_post_hook(&$a,&$b) {
        $reply = false;
        $likes = false;
 
+       $deny_arr = array();
+       $allow_arr = array();
+
        $toplevel = (($b['id'] == $b['parent']) ? true : false);
 
 
@@ -789,8 +841,7 @@ function facebook_post_hook(&$a,&$b) {
                        $allow_str = dbesc(implode(', ',$recipients));
                        if($allow_str) {
                                $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $allow_str ) AND `network` = 'face'"); 
-                               $allow_arr = array();
-                               if(count($r)) 
+                               if(count($r))
                                        foreach($r as $rr)
                                                $allow_arr[] = $rr['notify'];
                        }
@@ -798,8 +849,7 @@ function facebook_post_hook(&$a,&$b) {
                        $deny_str = dbesc(implode(', ',$deny));
                        if($deny_str) {
                                $r = q("SELECT `notify` FROM `contact` WHERE `id` IN ( $deny_str ) AND `network` = 'face'"); 
-                               $deny_arr = array();
-                               if(count($r)) 
+                               if(count($r))
                                        foreach($r as $rr)
                                                $deny_arr[] = $rr['notify'];
                        }
@@ -854,8 +904,8 @@ function facebook_post_hook(&$a,&$b) {
 
                                // unless it's a dislike - just send the text as a comment
 
-                               if($b['verb'] == ACTIVITY_DISLIKE)
-                                       $msg = trim(strip_tags(bbcode($msg)));
+                               // if($b['verb'] == ACTIVITY_DISLIKE)
+                               //      $msg = trim(strip_tags(bbcode($msg)));
 
                                // Old code
                                /*$search_str = $a->get_baseurl() . '/search';
@@ -896,7 +946,7 @@ function facebook_post_hook(&$a,&$b) {
                                if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
                                        $image = $matches[3];
 
-                               if ($image != '')
+                               if ($image == '')
                                        if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
                                                $image = $matches[1];
 
@@ -965,7 +1015,6 @@ function facebook_post_hook(&$a,&$b) {
 
                                // Since facebook increased the maxpostlen massively this never should happen again :)
                                if (strlen($msg) > FACEBOOK_MAXPOSTLEN) {
-                                       $shortlink = "";
                                        require_once('library/slinky.php');
 
                                        $display_url = $b['plink'];
@@ -1006,10 +1055,14 @@ function facebook_post_hook(&$a,&$b) {
                                                'access_token' => $fb_token, 
                                                'message' => $msg
                                        );
-                                       if(isset($image))
+                                       if(isset($image)) {
                                                $postvars['picture'] = $image;
-                                       if(isset($link))
+                                               //$postvars['type'] = "photo";
+                                       }
+                                       if(isset($link)) {
                                                $postvars['link'] = $link;
+                                               //$postvars['type'] = "link";
+                                       }
                                        if(isset($linkname))
                                                $postvars['name'] = $linkname;
                                }
@@ -1026,11 +1079,18 @@ function facebook_post_hook(&$a,&$b) {
 
                                if($reply) {
                                        $url = 'https://graph.facebook.com/' . $reply . '/' . (($likes) ? 'likes' : 'comments');
-                               }
-                               else { 
+                               } 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']),
+                                               'subject' => $b['title'],
+                                       );
+                                       $url = 'https://graph.facebook.com/me/notes';
                                }
 
                                logger('facebook: post to ' . $url);
@@ -1088,6 +1148,10 @@ function facebook_post_hook(&$a,&$b) {
        }
 }
 
+/**
+ * @param App $app
+ * @param object $data
+ */
 function facebook_enotify(&$app, &$data) {
        if (x($data, 'params') && $data['params']['type'] == NOTIFY_SYSTEM && x($data['params'], 'system_type') && $data['params']['system_type'] == 'facebook_connection_invalid') {
                $data['itemlink'] = '/facebook';
@@ -1097,6 +1161,10 @@ function facebook_enotify(&$app, &$data) {
        }
 }
 
+/**
+ * @param App $a
+ * @param object $b
+ */
 function facebook_post_local(&$a,&$b) {
 
        // Figure out if Facebook posting is enabled for this post and file it in 'postopts'
@@ -1123,6 +1191,10 @@ function facebook_post_local(&$a,&$b) {
 }
 
 
+/**
+ * @param App $a
+ * @param object $b
+ */
 function fb_queue_hook(&$a,&$b) {
 
        $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
@@ -1181,8 +1253,14 @@ function fb_queue_hook(&$a,&$b) {
        }
 }
 
+/**
+ * @param string $access_token
+ * @param int $since
+ * @return object
+ */
 function fb_get_timeline($access_token, &$since) {
 
+    $entries = new stdClass();
        $entries->data = array();
        $newest = 0;
 
@@ -1211,7 +1289,7 @@ function fb_get_timeline($access_token, &$since) {
                else
                        break;
 
-               $url = $j->paging->next;
+               $url = (isset($j->paging) && isset($j->paging->next) ? $j->paging->next : '');
 
        } while (($oldestdate > $since) and ($since != 0) and ($url != ''));
 
@@ -1221,6 +1299,9 @@ function fb_get_timeline($access_token, &$since) {
        return($entries);
 }
 
+/**
+ * @param int $uid
+ */
 function fb_consume_all($uid) {
 
        require_once('include/items.php');
@@ -1245,7 +1326,7 @@ function fb_consume_all($uid) {
        // Get the last date
        $lastdate = get_pconfig($uid,'facebook','lastdate');
        // fetch all items since the last date
-       $j = fb_get_timeline($access_token, &$lastdate);
+       $j = fb_get_timeline($access_token, $lastdate);
        if (isset($j->data)) {
                logger('fb_consume_stream: feed: ' . print_r($j,true), LOGGER_DATA);
                fb_consume_stream($uid,$j,false);
@@ -1256,6 +1337,11 @@ function fb_consume_all($uid) {
                logger('fb_consume_stream: feed: got no data from Facebook: ' . print_r($j,true), LOGGER_NORMAL);
 }
 
+/**
+ * @param int $uid
+ * @param string $link
+ * @return string
+ */
 function fb_get_photo($uid,$link) {
        $access_token = get_pconfig($uid,'facebook','access_token');
        if(! $access_token || (! stristr($link,'facebook.com/photo.php')))
@@ -1264,14 +1350,22 @@ function fb_get_photo($uid,$link) {
        $ret = preg_match('/fbid=([0-9]*)/',$link,$match);
        if($ret)
                $photo_id = $match[1];
+       else
+           return "";
        $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]';
+       return "";
 }
 
+/**
+ * @param int $uid
+ * @param object $j
+ * @param bool $wall
+ */
 function fb_consume_stream($uid,$j,$wall = false) {
 
        $a = get_app();
@@ -1283,7 +1377,7 @@ function fb_consume_stream($uid,$j,$wall = false) {
        if(! count($user))
                return;
 
-       $my_local_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
+       // $my_local_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
 
        $no_linking = get_pconfig($uid,'facebook','no_linking');
        if($no_linking)
@@ -1300,7 +1394,9 @@ function fb_consume_stream($uid,$j,$wall = false) {
        if(! count($j->data) || (! strlen($self_id)))
                return;
 
-       foreach($j->data as $entry) {
+    $top_item = 0;
+
+    foreach($j->data as $entry) {
                logger('fb_consume: entry: ' . print_r($entry,true), LOGGER_DATA);
                $datarray = array();
 
@@ -1310,12 +1406,10 @@ function fb_consume_stream($uid,$j,$wall = false) {
                                intval($uid)
                );
                if(count($r)) {
-                       $post_exists = true;
                        $orig_post = $r[0];
                        $top_item = $r[0]['id'];
                }
                else {
-                       $post_exists = false;
                        $orig_post = null;
                }
 
@@ -1330,7 +1424,7 @@ function fb_consume_stream($uid,$j,$wall = false) {
                        else {
                                // Looking if user is known - if not he is added
                                $access_token = get_pconfig($uid, 'facebook', 'access_token');
-                               fb_get_friends_sync_new($uid, $access_token, $from);
+                               fb_get_friends_sync_new($uid, $access_token, array($from));
 
                                $r = q("SELECT * FROM `contact` WHERE `notify` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
                                        dbesc($from->id),
@@ -1379,32 +1473,32 @@ function fb_consume_stream($uid,$j,$wall = false) {
 
                        logger('facebook: post '.$entry->id.' from '.$from->name);
 
-                       $datarray['body'] = escape_tags($entry->message);
+                       $datarray['body'] = (isset($entry->message) ? escape_tags($entry->message) : '');
 
-                       if($entry->name and $entry->link)
+                       if(isset($entry->name) and isset($entry->link))
                                $datarray['body'] .= "\n\n[bookmark=".$entry->link."]".$entry->name."[/bookmark]";
-                       elseif ($entry->name)
+                       elseif (isset($entry->name))
                                $datarray['body'] .= "\n\n[b]" . $entry->name."[/b]";
 
-                       if($entry->caption) {
-                               if(!$entry->name and $entry->link)
+                       if(isset($entry->caption)) {
+                               if(!isset($entry->name) and isset($entry->link))
                                        $datarray['body'] .= "\n\n[bookmark=".$entry->link."]".$entry->caption."[/bookmark]";
                                else
                                        $datarray['body'] .= "[i]" . $entry->caption."[/i]\n";
                        }
 
-                       if(!$entry->caption and !$entry->name) {
-                               if ($entry->link)
+                       if(!isset($entry->caption) and !isset($entry->name)) {
+                               if (isset($entry->link))
                                        $datarray['body'] .= "\n[url]".$entry->link."[/url]\n";
                                else
                                        $datarray['body'] .= "\n";
                        }
 
                        $quote = "";
-                       if($entry->description)
+                       if(isset($entry->description))
                                $quote = $entry->description;
 
-                       if ($entry->properties)
+                       if (isset($entry->properties))
                                foreach ($entry->properties as $property)
                                        $quote .= "\n".$property->name.": [url=".$property->href."]".$property->text."[/url]";
 
@@ -1414,22 +1508,31 @@ function fb_consume_stream($uid,$j,$wall = false) {
                        // Only import the picture when the message is no video
                        // oembed display a picture of the video as well 
                        if ($entry->type != "video") {
-                               if($entry->picture && $entry->link) {
+                               if(isset($entry->picture) && isset($entry->link)) {
                                        $datarray['body'] .= "\n" . '[url=' . $entry->link . '][img]'.$entry->picture.'[/img][/url]';   
                                }
                                else {
-                                       if($entry->picture)
+                                       if(isset($entry->picture))
                                                $datarray['body'] .= "\n" . '[img]' . $entry->picture . '[/img]';
                                        // if just a link, it may be a wall photo - check
-                                       if($entry->link)
+                                       if(isset($entry->link))
                                                $datarray['body'] .= fb_get_photo($uid,$entry->link);
                                }
                        }
 
+                       if (($datarray['app'] == "Events") and isset($entry->actions))
+                               foreach ($entry->actions as $action)
+                                       if ($action->name == "View")
+                                               $datarray['body'] .= " [url=".$action->link."]".$entry->story."[/url]";
+
                        // Just as a test - to see if these are the missing entries
                        //if(trim($datarray['body']) == '')
                        //      $datarray['body'] = $entry->story;
 
+                       // Adding the "story" text to see if there are useful data in it (testing)
+                       //if (($datarray['app'] != "Events") and $entry->story)
+                       //      $datarray['body'] .= "\n".$entry->story;
+
                        if(trim($datarray['body']) == '') {
                                logger('facebook: empty body '.$entry->id.' '.print_r($entry, true));
                                continue;
@@ -1437,10 +1540,10 @@ function fb_consume_stream($uid,$j,$wall = false) {
 
                        $datarray['body'] .= "\n";
 
-                       if ($entry->icon)
+                       if (isset($entry->icon))
                                $datarray['body'] .= "[img]".$entry->icon."[/img] &nbsp; ";
 
-                       if ($entry->actions)
+                       if (isset($entry->actions))
                                foreach ($entry->actions as $action)
                                        if (($action->name != "Comment") and ($action->name != "Like"))
                                                $datarray['body'] .= "[url=".$action->link."]".$action->name."[/url] &nbsp; ";
@@ -1450,26 +1553,29 @@ function fb_consume_stream($uid,$j,$wall = false) {
                        //if(($datarray['body'] != '') and ($uid == 1))
                        //      $datarray['body'] .= "[noparse]".print_r($entry, true)."[/noparse]";
 
-                       if ($entry->place->name)
-                               $datarray['coord'] = $entry->place->name;
-                       else if ($entry->place->location->street or $entry->place->location->city or $entry->place->location->Denmark) {
-                               if ($entry->place->location->street)
-                                       $datarray['coord'] = $entry->place->location->street;
-                               if ($entry->place->location->city)
-                                       $datarray['coord'] .= " ".$entry->place->location->city;
-                               if ($entry->place->location->country)
-                                       $datarray['coord'] .= " ".$entry->place->location->country;
-                       } else if ($entry->place->location->latitude and $entry->place->location->longitude)
-                               $datarray['coord'] = substr($entry->place->location->latitude, 0, 8)
+            if (isset($entry->place)) {
+                           if ($entry->place->name or $entry->place->location->street or
+                                   $entry->place->location->city or $entry->place->location->Denmark) {
+                                   $datarray['coord'] = '';
+                                   if ($entry->place->name)
+                                           $datarray['coord'] .= $entry->place->name;
+                                   if ($entry->place->location->street)
+                                           $datarray['coord'] .= $entry->place->location->street;
+                                   if ($entry->place->location->city)
+                                           $datarray['coord'] .= " ".$entry->place->location->city;
+                                   if ($entry->place->location->country)
+                                           $datarray['coord'] .= " ".$entry->place->location->country;
+                           } else if ($entry->place->location->latitude and $entry->place->location->longitude)
+                                   $datarray['coord'] = substr($entry->place->location->latitude, 0, 8)
                                                        .' '.substr($entry->place->location->longitude, 0, 8);
-
+            }
                        $datarray['created'] = datetime_convert('UTC','UTC',$entry->created_time);
                        $datarray['edited'] = datetime_convert('UTC','UTC',$entry->updated_time);
 
                        // If the entry has a privacy policy, we cannot assume who can or cannot see it,
                        // as the identities are from a foreign system. Mark it as private to the owner.
 
-                       if($entry->privacy && $entry->privacy->value !== 'EVERYONE') {
+                       if(isset($entry->privacy) && $entry->privacy->value !== 'EVERYONE') {
                                $datarray['private'] = 1;
                                $datarray['allow_cid'] = '<' . $self[0]['id'] . '>';
                        }
@@ -1554,7 +1660,7 @@ function fb_consume_stream($uid,$j,$wall = false) {
                                $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' . 
                                        '<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . xmlify($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';  
 
-                               $item = item_store($likedata);                  
+                               item_store($likedata);
                        }
                }
                if(is_array($comments)) {
@@ -1633,7 +1739,7 @@ function fb_consume_stream($uid,$j,$wall = false) {
                                                        'to_email'     => $user[0]['email'],
                                                        'uid'          => $user[0]['uid'],
                                                        'item'         => $cmntdata,
-                                                       'link'             => $a->get_baseurl() . '/display/' . $importer['nickname'] . '/' . $item,
+                                                       'link'             => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $item,
                                                        'source_name'  => $cmntdata['author-name'],
                                                        'source_link'  => $cmntdata['author-link'],
                                                        'source_photo' => $cmntdata['author-avatar'],
@@ -1652,6 +1758,9 @@ function fb_consume_stream($uid,$j,$wall = false) {
 }
 
 
+/**
+ * @return bool|string
+ */
 function fb_get_app_access_token() {
        
        $acc_token = get_config('facebook','app_access_token');
@@ -1697,6 +1806,9 @@ function facebook_subscription_del_users() {
        if (!facebook_check_realtime_active()) del_config('facebook', 'realtime_active');
 }
 
+/**
+ * @param bool $second_try
+ */
 function facebook_subscription_add_users($second_try = false) {
        $a = get_app();
        $access_token = fb_get_app_access_token();
@@ -1733,6 +1845,9 @@ function facebook_subscription_add_users($second_try = false) {
        };
 }
 
+/**
+ * @return null|array
+ */
 function facebook_subscriptions_get() {
        
        $access_token = fb_get_app_access_token();
@@ -1749,6 +1864,9 @@ function facebook_subscriptions_get() {
 }
 
 
+/**
+ * @return bool
+ */
 function facebook_check_realtime_active() {
        $ret = facebook_subscriptions_get();
        if (is_null($ret)) return false;
@@ -1762,7 +1880,14 @@ function facebook_check_realtime_active() {
 // DELETE-request to $url
 
 if(! function_exists('facebook_delete_url')) {
-function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0) {
+    /**
+     * @param string $url
+     * @param null|array $headers
+     * @param int $redirects
+     * @param int $timeout
+     * @return bool|string
+     */
+    function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0) {
        $a = get_app();
        $ch = curl_init($url);
        if(($redirects > 8) || (! $ch)) 
@@ -1833,7 +1958,7 @@ function facebook_delete_url($url,$headers = null, &$redirects = 0, $timeout = 0
         $url_parsed = @parse_url($url);
         if (isset($url_parsed)) {
             $redirects++;
-            return delete_url($url,$headers,$redirects,$timeout);
+            return facebook_delete_url($url,$headers,$redirects,$timeout);
         }
     }
        $a->set_curl_code($http_code);