removed code
[friendica-addons.git/.git] / pumpio / pumpio.php
index a2f21ce..9960ec5 100644 (file)
@@ -6,15 +6,16 @@
  * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
  */
 
+use Friendica\App;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Core\PConfig;
+use Friendica\Core\Protocol;
 use Friendica\Core\Worker;
-use Friendica\Database\dba;
-use Friendica\Database\DBM;
+use Friendica\Database\DBA;
 use Friendica\Model\Contact;
 use Friendica\Model\GContact;
 use Friendica\Model\Group;
@@ -59,7 +60,7 @@ function pumpio_uninstall()
 
 function pumpio_module() {}
 
-function pumpio_content(&$a)
+function pumpio_content(App $a)
 {
        if (!local_user()) {
                notice(L10n::t('Permission denied.') . EOL);
@@ -92,7 +93,7 @@ function pumpio_check_item_notification($a, &$notification_data)
        $notification_data["profiles"][] = "https://".$hostname."/".$username;
 }
 
-function pumpio_registerclient(&$a, $host)
+function pumpio_registerclient(App $a, $host)
 {
        $url = "https://".$host."/api/client/register";
 
@@ -135,11 +136,8 @@ function pumpio_registerclient(&$a, $host)
 
 }
 
-function pumpio_connect(&$a)
+function pumpio_connect(App $a)
 {
-       // Start a session.  This is necessary to hold on to  a few keys the callback script will also need
-       session_start();
-
        // Define the needed keys
        $consumer_key = PConfig::get(local_user(), 'pumpio', 'consumer_key');
        $consumer_secret = PConfig::get(local_user(), 'pumpio', 'consumer_secret');
@@ -173,7 +171,7 @@ function pumpio_connect(&$a)
        // Create a new instance of the oauth_client_class library.  For this step, all we need to give the library is our
        // Consumer Key and Consumer Secret
        $client = new oauth_client_class;
-       $client->debug = 1;
+       $client->debug = 0;
        $client->server = '';
        $client->oauth_version = '1.0a';
        $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
@@ -201,7 +199,7 @@ function pumpio_connect(&$a)
 
        if ($success) {
                logger("pumpio_connect: authenticated");
-               $o .= L10n::t("You are now authenticated to pumpio.");
+               $o = L10n::t("You are now authenticated to pumpio.");
                $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.L10n::t("return to the connector page").'</a>';
        } else {
                logger("pumpio_connect: could not connect");
@@ -211,13 +209,14 @@ function pumpio_connect(&$a)
        return $o;
 }
 
-function pumpio_jot_nets(&$a, &$b)
+function pumpio_jot_nets(App $a, &$b)
 {
-       if (!local_user()) {
+       if (! local_user()) {
                return;
        }
 
        $pumpio_post = PConfig::get(local_user(), 'pumpio', 'post');
+
        if (intval($pumpio_post) == 1) {
                $pumpio_defpost = PConfig::get(local_user(), 'pumpio', 'post_by_default');
                $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
@@ -226,7 +225,7 @@ function pumpio_jot_nets(&$a, &$b)
        }
 }
 
-function pumpio_settings(&$a, &$s)
+function pumpio_settings(App $a, &$s)
 {
        if (!local_user()) {
                return;
@@ -326,28 +325,29 @@ function pumpio_settings(&$a, &$s)
        $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
 }
 
-function pumpio_settings_post(&$a, &$b)
+function pumpio_settings_post(App $a, array &$b)
 {
-       if (x($_POST, 'pumpio-submit')) {
-               if (x($_POST, 'pumpio_delete')) {
-                       PConfig::set(local_user(), 'pumpio', 'consumer_key', '');
-                       PConfig::set(local_user(), 'pumpio', 'consumer_secret', '');
-                       PConfig::set(local_user(), 'pumpio', 'oauth_token', '');
+       if (!empty($_POST['pumpio-submit'])) {
+               if (!empty($_POST['pumpio_delete'])) {
+                       PConfig::set(local_user(), 'pumpio', 'consumer_key'      , '');
+                       PConfig::set(local_user(), 'pumpio', 'consumer_secret'   , '');
+                       PConfig::set(local_user(), 'pumpio', 'oauth_token'       , '');
                        PConfig::set(local_user(), 'pumpio', 'oauth_token_secret', '');
-                       PConfig::set(local_user(), 'pumpio', 'post', false);
-                       PConfig::set(local_user(), 'pumpio', 'import', false);
-                       PConfig::set(local_user(), 'pumpio', 'host', '');
-                       PConfig::set(local_user(), 'pumpio', 'user', '');
-                       PConfig::set(local_user(), 'pumpio', 'public', false);
-                       PConfig::set(local_user(), 'pumpio', 'mirror', false);
-                       PConfig::set(local_user(), 'pumpio', 'post_by_default', false);
-                       PConfig::set(local_user(), 'pumpio', 'lastdate', 0);
-                       PConfig::set(local_user(), 'pumpio', 'last_id', '');
+                       PConfig::set(local_user(), 'pumpio', 'post'              , false);
+                       PConfig::set(local_user(), 'pumpio', 'import'            , false);
+                       PConfig::set(local_user(), 'pumpio', 'host'              , '');
+                       PConfig::set(local_user(), 'pumpio', 'user'              , '');
+                       PConfig::set(local_user(), 'pumpio', 'public'            , false);
+                       PConfig::set(local_user(), 'pumpio', 'mirror'            , false);
+                       PConfig::set(local_user(), 'pumpio', 'post_by_default'   , false);
+                       PConfig::set(local_user(), 'pumpio', 'lastdate'          , 0);
+                       PConfig::set(local_user(), 'pumpio', 'last_id'           , '');
                } else {
                        // filtering the username if it is filled wrong
                        $user = $_POST['pumpio_user'];
                        if (strstr($user, "@")) {
                                $pos = strpos($user, "@");
+
                                if ($pos > 0) {
                                        $user = substr($user, 0, $pos);
                                }
@@ -358,29 +358,27 @@ function pumpio_settings_post(&$a, &$b)
                        $host = trim($host);
                        $host = str_replace(["https://", "http://"], ["", ""], $host);
 
-                       PConfig::set(local_user(), 'pumpio', 'post',intval($_POST['pumpio']));
-                       PConfig::set(local_user(), 'pumpio', 'import', $_POST['pumpio_import']);
-                       PConfig::set(local_user(), 'pumpio', 'host', $host);
-                       PConfig::set(local_user(), 'pumpio', 'user', $user);
-                       PConfig::set(local_user(), 'pumpio', 'public', $_POST['pumpio_public']);
-                       PConfig::set(local_user(), 'pumpio', 'mirror', $_POST['pumpio_mirror']);
-                       PConfig::set(local_user(), 'pumpio', 'post_by_default',intval($_POST['pumpio_bydefault']));
+                       PConfig::set(local_user(), 'pumpio', 'post'           , defaults($_POST, 'pumpio', false));
+                       PConfig::set(local_user(), 'pumpio', 'import'         , defaults($_POST, 'pumpio_import', false));
+                       PConfig::set(local_user(), 'pumpio', 'host'           , $host);
+                       PConfig::set(local_user(), 'pumpio', 'user'           , $user);
+                       PConfig::set(local_user(), 'pumpio', 'public'         , defaults($_POST, 'pumpio_public', false));
+                       PConfig::set(local_user(), 'pumpio', 'mirror'         , defaults($_POST, 'pumpio_mirror', false));
+                       PConfig::set(local_user(), 'pumpio', 'post_by_default', defaults($_POST, 'pumpio_bydefault', false));
 
-                       if (!$_POST['pumpio_mirror']) {
+                       if (!empty($_POST['pumpio_mirror'])) {
                                PConfig::delete(local_user(), 'pumpio', 'lastdate');
                        }
-
-                       //header("Location: ".$a->get_baseurl()."/pumpio/connect");
                }
        }
 }
 
-function pumpio_load_config(\Friendica\App $a)
+function pumpio_load_config(App $a)
 {
        $a->loadConfigFile(__DIR__. '/config/pumpio.ini.php');
 }
 
-function pumpio_post_local(&$a, &$b)
+function pumpio_post_local(App $a, array &$b)
 {
        if (!local_user() || (local_user() != $b['uid'])) {
                return;
@@ -388,7 +386,7 @@ function pumpio_post_local(&$a, &$b)
 
        $pumpio_post   = intval(PConfig::get(local_user(), 'pumpio', 'post'));
 
-       $pumpio_enable = (($pumpio_post && x($_REQUEST, 'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
+       $pumpio_enable = (($pumpio_post && !empty($_REQUEST['pumpio_enable'])) ? intval($_REQUEST['pumpio_enable']) : 0);
 
        if ($b['api_source'] && intval(PConfig::get(local_user(), 'pumpio', 'post_by_default'))) {
                $pumpio_enable = 1;
@@ -405,22 +403,20 @@ function pumpio_post_local(&$a, &$b)
        $b['postopts'] .= 'pumpio';
 }
 
-function pumpio_send(&$a, &$b)
+function pumpio_send(App $a, array &$b)
 {
-       if (!PConfig::get($b["uid"], 'pumpio', 'import')) {
-               if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
-                       return;
-               }
+       if (!PConfig::get($b["uid"], 'pumpio', 'import') && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
+               return;
        }
 
        logger("pumpio_send: parameter ".print_r($b, true), LOGGER_DATA);
 
        if ($b['parent'] != $b['id']) {
                // Looking if its a reply to a pumpio post
-               $condition = ['id' => $b['parent'], 'network' => NETWORK_PUMPIO];
+               $condition = ['id' => $b['parent'], 'network' => Protocol::PUMPIO];
                $orig_post = Item::selectFirst([], $condition);
 
-               if (!DBM::is_result($orig_post)) {
+               if (!DBA::isResult($orig_post)) {
                        logger("pumpio_send: no pumpio post ".$b["parent"]);
                        return;
                } else {
@@ -439,7 +435,7 @@ function pumpio_send(&$a, &$b)
 
                // Dont't post if the post doesn't belong to us.
                // This is a check for forum postings
-               $self = dba::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
+               $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
                if ($b['contact-id'] != $self['id']) {
                        return;
                }
@@ -572,19 +568,19 @@ function pumpio_send(&$a, &$b)
                        logger('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user, true));
 
                        $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
-                       if (DBM::is_result($r)) {
+                       if (DBA::isResult($r)) {
                                $a->contact = $r[0]["id"];
                        }
 
                        $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $params]);
 
-                       Queue::add($a->contact, NETWORK_PUMPIO, $s);
+                       Queue::add($a->contact, Protocol::PUMPIO, $s);
                        notice(L10n::t('Pump.io post failed. Queued for retry.').EOL);
                }
        }
 }
 
-function pumpio_action(&$a, $uid, $uri, $action, $content = "")
+function pumpio_action(App $a, $uid, $uri, $action, $content = "")
 {
        // Don't do likes and other stuff if you don't import the timeline
        if (!PConfig::get($uid, 'pumpio', 'import')) {
@@ -600,7 +596,7 @@ function pumpio_action(&$a, $uid, $uri, $action, $content = "")
 
        $orig_post = Item::selectFirst([], ['uri' => $uri, 'uid' => $uid]);
 
-       if (!DBM::is_result($orig_post)) {
+       if (!DBA::isResult($orig_post)) {
                return;
        }
 
@@ -649,22 +645,22 @@ function pumpio_action(&$a, $uid, $uri, $action, $content = "")
                logger('pumpio_action '.$username.' '.$action.': general error: '.$uri.' '.print_r($user, true));
 
                $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
-               if (DBM::is_result($r)) {
+               if (DBA::isResult($r)) {
                        $a->contact = $r[0]["id"];
                }
 
                $s = serialize(['url' => $url, 'item' => $orig_post["id"], 'post' => $params]);
 
-               Queue::add($a->contact, NETWORK_PUMPIO, $s);
+               Queue::add($a->contact, Protocol::PUMPIO, $s);
                notice(L10n::t('Pump.io like failed. Queued for retry.').EOL);
        }
 }
 
-function pumpio_sync(&$a)
+function pumpio_sync(App $a)
 {
        $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = 'pumpio'");
 
-       if (!DBM::is_result($r)) {
+       if (!DBA::isResult($r)) {
                return;
        }
 
@@ -682,7 +678,7 @@ function pumpio_sync(&$a)
        logger('pumpio: cron_start');
 
        $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
-       if (DBM::is_result($r)) {
+       if (DBA::isResult($r)) {
                foreach ($r as $rr) {
                        logger('pumpio: mirroring user '.$rr['uid']);
                        pumpio_fetchtimeline($a, $rr['uid']);
@@ -697,11 +693,11 @@ function pumpio_sync(&$a)
        $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
 
        $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
-       if (DBM::is_result($r)) {
+       if (DBA::isResult($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 (!DBM::is_result($user)) {
+                               if (!DBA::isResult($user)) {
                                        logger('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
                                        continue;
                                }
@@ -730,12 +726,12 @@ function pumpio_sync(&$a)
        Config::set('pumpio', 'last_poll', time());
 }
 
-function pumpio_cron(&$a, $b)
+function pumpio_cron(App $a, $b)
 {
        Worker::add(PRIORITY_MEDIUM,"addon/pumpio/pumpio_sync.php");
 }
 
-function pumpio_fetchtimeline(&$a, $uid)
+function pumpio_fetchtimeline(App $a, $uid)
 {
        $ckey    = PConfig::get($uid, 'pumpio', 'consumer_key');
        $csecret = PConfig::get($uid, 'pumpio', 'consumer_secret');
@@ -778,6 +774,7 @@ function pumpio_fetchtimeline(&$a, $uid)
                $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError'=>true], $user);
        } else {
                $success = false;
+               $user = [];
        }
 
        if (!$success) {
@@ -830,7 +827,7 @@ function pumpio_fetchtimeline(&$a, $uid)
                                $_REQUEST["source"] = "pump.io";
 
                                if (isset($post->object->id)) {
-                                       $_REQUEST['message_id'] = NETWORK_PUMPIO.":".$post->object->id;
+                                       $_REQUEST['message_id'] = Protocol::PUMPIO.":".$post->object->id;
                                }
 
                                if ($post->object->displayName != "") {
@@ -865,14 +862,14 @@ function pumpio_fetchtimeline(&$a, $uid)
        }
 }
 
-function pumpio_dounlike(&$a, $uid, $self, $post, $own_id)
+function pumpio_dounlike(App $a, $uid, $self, $post, $own_id)
 {
        // Searching for the unliked post
        // Two queries for speed issues
        $orig_post = Item::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
-       if (!DBM::is_result($orig_post)) {
+       if (!DBA::isResult($orig_post)) {
                $orig_post = Item::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
-               if (!DBM::is_result($orig_post)) {
+               if (!DBA::isResult($orig_post)) {
                        return;
                }
        }
@@ -883,11 +880,11 @@ function pumpio_dounlike(&$a, $uid, $self, $post, $own_id)
                $contactid = $self[0]['id'];
        } else {
                $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
-                       dbesc(normalise_link($post->actor->url)),
+                       DBA::escape(normalise_link($post->actor->url)),
                        intval($uid)
                );
 
-               if (DBM::is_result($r)) {
+               if (DBA::isResult($r)) {
                        $contactid = $r[0]['id'];
                }
 
@@ -898,14 +895,14 @@ function pumpio_dounlike(&$a, $uid, $self, $post, $own_id)
 
        Item::delete(['verb' => ACTIVITY_LIKE, 'uid' => $uid, 'contact-id' => $contactid, 'thr-parent' => $orig_post['uri']]);
 
-       if (DBM::is_result($r)) {
+       if (DBA::isResult($r)) {
                logger("pumpio_dounlike: unliked existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
        } else {
                logger("pumpio_dounlike: not found. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
        }
 }
 
-function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = true)
+function pumpio_dolike(App $a, $uid, $self, $post, $own_id, $threadcompletion = true)
 {
        require_once('include/items.php');
 
@@ -917,9 +914,9 @@ function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = tru
        // Searching for the liked post
        // Two queries for speed issues
        $orig_post = Item::selectFirst([], ['uri' => $post->object->id, 'uid' => $uid]);
-       if (!DBM::is_result($orig_post)) {
+       if (!DBA::isResult($orig_post)) {
                $orig_post = Item::selectFirst([], ['extid' => $post->object->id, 'uid' => $uid]);
-               if (!DBM::is_result($orig_post)) {
+               if (!DBA::isResult($orig_post)) {
                        return;
                }
        }
@@ -938,11 +935,11 @@ function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = tru
                $post->actor->image->url = $self[0]['photo'];
        } else {
                $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
-                       dbesc(normalise_link($post->actor->url)),
+                       DBA::escape(normalise_link($post->actor->url)),
                        intval($uid)
                );
 
-               if (DBM::is_result($r)) {
+               if (DBA::isResult($r)) {
                        $contactid = $r[0]['id'];
                }
 
@@ -952,7 +949,7 @@ function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = tru
        }
 
        $condition = ['verb' => ACTIVITY_LIKE, 'uid' => $uid, 'contact-id' => $contactid, 'thr-parent' => $orig_post['uri']];
-       if (dba::exists('item', $condition)) {
+       if (Item::exists($condition)) {
                logger("pumpio_dolike: found existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
                return;
        }
@@ -963,14 +960,16 @@ function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = tru
        $likedata['gravity'] = GRAVITY_ACTIVITY;
        $likedata['uid'] = $uid;
        $likedata['wall'] = 0;
-       $likedata['network'] = NETWORK_PUMPIO;
+       $likedata['network'] = Protocol::PUMPIO;
        $likedata['uri'] = Item::newURI($uid);
        $likedata['parent-uri'] = $orig_post["uri"];
        $likedata['contact-id'] = $contactid;
        $likedata['app'] = $post->generator->displayName;
        $likedata['author-name'] = $post->actor->displayName;
        $likedata['author-link'] = $post->actor->url;
-       $likedata['author-avatar'] = $post->actor->image->url;
+       if (!empty($post->actor->image)) {
+               $likedata['author-avatar'] = $post->actor->image->url;
+       }
 
        $author  = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
        $objauthor =  '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
@@ -990,7 +989,7 @@ function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = tru
 
 function pumpio_get_contact($uid, $contact, $no_insert = false)
 {
-       $gcontact = ["url" => $contact->url, "network" => NETWORK_PUMPIO, "generation" => 2,
+       $gcontact = ["url" => $contact->url, "network" => Protocol::PUMPIO, "generation" => 2,
                "name" => $contact->displayName,  "hide" => true,
                "nick" => $contact->preferredUsername,
                "addr" => str_replace("acct:", "", $contact->id)];
@@ -1015,39 +1014,39 @@ function pumpio_get_contact($uid, $contact, $no_insert = false)
        }
 
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' LIMIT 1",
-               intval($uid), dbesc(normalise_link($contact->url)));
+               intval($uid), DBA::escape(normalise_link($contact->url)));
 
-       if (!DBM::is_result($r)) {
+       if (!DBA::isResult($r)) {
                // create contact record
                q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
                                        `name`, `nick`, `photo`, `network`, `rel`, `priority`,
                                        `location`, `about`, `writable`, `blocked`, `readonly`, `pending` )
                                VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d, 0, 0, 0)",
                        intval($uid),
-                       dbesc(DateTimeFormat::utcNow()),
-                       dbesc($contact->url),
-                       dbesc(normalise_link($contact->url)),
-                       dbesc(str_replace("acct:", "", $contact->id)),
-                       dbesc(''),
-                       dbesc($contact->id), // What is it for?
-                       dbesc('pump.io ' . $contact->id), // What is it for?
-                       dbesc($contact->displayName),
-                       dbesc($contact->preferredUsername),
-                       dbesc($contact->image->url),
-                       dbesc(NETWORK_PUMPIO),
-                       intval(CONTACT_IS_FRIEND),
+                       DBA::escape(DateTimeFormat::utcNow()),
+                       DBA::escape($contact->url),
+                       DBA::escape(normalise_link($contact->url)),
+                       DBA::escape(str_replace("acct:", "", $contact->id)),
+                       DBA::escape(''),
+                       DBA::escape($contact->id), // What is it for?
+                       DBA::escape('pump.io ' . $contact->id), // What is it for?
+                       DBA::escape($contact->displayName),
+                       DBA::escape($contact->preferredUsername),
+                       DBA::escape($contact->image->url),
+                       DBA::escape(Protocol::PUMPIO),
+                       intval(Contact::FRIEND),
                        intval(1),
-                       dbesc($contact->location->displayName),
-                       dbesc($contact->summary),
+                       DBA::escape($contact->location->displayName),
+                       DBA::escape($contact->summary),
                        intval(1)
                );
 
                $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d LIMIT 1",
-                       dbesc(normalise_link($contact->url)),
+                       DBA::escape(normalise_link($contact->url)),
                        intval($uid)
                        );
 
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        return false;
                }
 
@@ -1075,24 +1074,24 @@ function pumpio_get_contact($uid, $contact, $no_insert = false)
        return $contact_id;
 }
 
-function pumpio_dodelete(&$a, $uid, $self, $post, $own_id)
+function pumpio_dodelete(App $a, $uid, $self, $post, $own_id)
 {
        // Two queries for speed issues
        $condition = ['uri' => $post->object->id, 'uid' => $uid];
-       if (dba::exists('item', $condition)) {
+       if (Item::exists($condition)) {
                Item::delete($condition);
                return true;
        }
 
        $condition = ['extid' => $post->object->id, 'uid' => $uid];
-       if (dba::exists('item', $condition)) {
+       if (Item::exists($condition)) {
                Item::delete($condition);
                return true;
        }
        return false;
 }
 
-function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcompletion = true)
+function pumpio_dopost(App $a, $client, $uid, $self, $post, $own_id, $threadcompletion = true)
 {
        require_once('include/items.php');
 
@@ -1110,10 +1109,10 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
 
        if ($post->verb != "update") {
                // Two queries for speed issues
-               if (dba::exists('item', ['uri' => $post->object->id, 'uid' => $uid])) {
+               if (Item::exists(['uri' => $post->object->id, 'uid' => $uid])) {
                        return false;
                }
-               if (dba::exists('item', ['extid' => $post->object->id, 'uid' => $uid])) {
+               if (Item::exists(['extid' => $post->object->id, 'uid' => $uid])) {
                        return false;
                }
        }
@@ -1141,7 +1140,7 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
        }
 
        $postarray = [];
-       $postarray['network'] = NETWORK_PUMPIO;
+       $postarray['network'] = Protocol::PUMPIO;
        $postarray['uid'] = $uid;
        $postarray['wall'] = 0;
        $postarray['uri'] = $post->object->id;
@@ -1171,19 +1170,19 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
                } elseif ($contact_id == 0) {
                        // Take an existing contact, the contact of the note or - as a fallback - the id of the user
                        $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
-                               dbesc(normalise_link($post->actor->url)),
+                               DBA::escape(normalise_link($post->actor->url)),
                                intval($uid)
                        );
 
-                       if (DBM::is_result($r)) {
+                       if (DBA::isResult($r)) {
                                $contact_id = $r[0]['id'];
                        } else {
                                $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
-                                       dbesc(normalise_link($post->actor->url)),
+                                       DBA::escape(normalise_link($post->actor->url)),
                                        intval($uid)
                                );
 
-                               if (DBM::is_result($r)) {
+                               if (DBA::isResult($r)) {
                                        $contact_id = $r[0]['id'];
                                } else {
                                        $contact_id = $self[0]['id'];
@@ -1198,7 +1197,10 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
                        $reply->cc = $post->cc;
                }
 
-               $reply->to = $post->to;
+               if (isset($post->to)) {
+                       $reply->to = $post->to;
+               }
+
                $reply->object = new stdClass;
                $reply->object->objectType = $post->object->inReplyTo->objectType;
                $reply->object->content = $post->object->inReplyTo->content;
@@ -1215,6 +1217,11 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
                $postarray['parent-uri'] = $post->object->inReplyTo->id;
        }
 
+       // When there is no content there is no need to continue
+       if (empty($post->object->content)) {
+               return false;
+       }
+
        if (!empty($post->object->pump_io->proxyURL)) {
                $postarray['extid'] = $post->object->pump_io->proxyURL;
        }
@@ -1223,12 +1230,15 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
        $postarray['verb'] = ACTIVITY_POST;
        $postarray['owner-name'] = $post->actor->displayName;
        $postarray['owner-link'] = $post->actor->url;
-       $postarray['owner-avatar'] = $post->actor->image->url;
-       $postarray['author-name'] = $post->actor->displayName;
-       $postarray['author-link'] = $post->actor->url;
-       $postarray['author-avatar'] = $post->actor->image->url;
+       $postarray['author-name'] = $postarray['owner-name'];
+       $postarray['author-link'] = $postarray['owner-link'];
+       if (!empty($post->actor->image)) {
+               $postarray['owner-avatar'] = $post->actor->image->url;
+               $postarray['author-avatar'] = $postarray['owner-avatar'];
+       }
        $postarray['plink'] = $post->object->url;
        $postarray['app'] = $post->generator->displayName;
+       $postarray['title'] = '';
        $postarray['body'] = HTML::toBBCode($post->object->content);
        $postarray['object'] = json_encode($post);
 
@@ -1258,10 +1268,15 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
                        $share_author = $post->object->author->url;
                }
 
+               if (isset($post->object->created)) {
+                       $created = DateTimeFormat::utc($post->object->created);
+               } else {
+                       $created = '';
+               }
+
                $postarray['body'] = share_header($share_author, $post->object->author->url,
                                                $post->object->author->image->url, "",
-                                               DateTimeFormat::utc($post->object->created),
-                                               $post->links->self->href).
+                                               $created, $post->links->self->href).
                                        $postarray['body']."[/share]";
        }
 
@@ -1285,12 +1300,12 @@ function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcomplet
        return $top_item;
 }
 
-function pumpio_fetchinbox(&$a, $uid)
+function pumpio_fetchinbox(App $a, $uid)
 {
-       $ckey    = PConfig::get($uid, 'pumpio', 'consumer_key');
-       $csecret = PConfig::get($uid, 'pumpio', 'consumer_secret');
-       $otoken  = PConfig::get($uid, 'pumpio', 'oauth_token');
-       $osecret = PConfig::get($uid, 'pumpio', 'oauth_token_secret');
+       $ckey     = PConfig::get($uid, 'pumpio', 'consumer_key');
+       $csecret  = PConfig::get($uid, 'pumpio', 'consumer_secret');
+       $otoken   = PConfig::get($uid, 'pumpio', 'oauth_token');
+       $osecret  = PConfig::get($uid, 'pumpio', 'oauth_token_secret');
        $lastdate = PConfig::get($uid, 'pumpio', 'lastdate');
        $hostname = PConfig::get($uid, 'pumpio', 'host');
        $username = PConfig::get($uid, "pumpio", "user");
@@ -1304,7 +1319,7 @@ function pumpio_fetchinbox(&$a, $uid)
                        INNER JOIN `item` ON `item`.`id` = `thread`.`iid`
                        WHERE `thread`.`network` = '%s' AND `thread`.`uid` = %d AND `item`.`extid` != ''
                        ORDER BY `thread`.`commented` DESC LIMIT 10",
-                               dbesc(NETWORK_PUMPIO),
+                               DBA::escape(Protocol::PUMPIO),
                                intval($uid)
                        );
 
@@ -1347,19 +1362,19 @@ function pumpio_fetchinbox(&$a, $uid)
                }
        }
 
-       foreach ($lastitems AS $item) {
+       foreach ($lastitems as $item) {
                pumpio_fetchallcomments($a, $uid, $item["uri"]);
        }
 
        PConfig::set($uid, 'pumpio', 'last_id', $last_id);
 }
 
-function pumpio_getallusers(&$a, $uid)
+function pumpio_getallusers(App &$a, $uid)
 {
-       $ckey    = PConfig::get($uid, 'pumpio', 'consumer_key');
-       $csecret = PConfig::get($uid, 'pumpio', 'consumer_secret');
-       $otoken  = PConfig::get($uid, 'pumpio', 'oauth_token');
-       $osecret = PConfig::get($uid, 'pumpio', 'oauth_token_secret');
+       $ckey     = PConfig::get($uid, 'pumpio', 'consumer_key');
+       $csecret  = PConfig::get($uid, 'pumpio', 'consumer_secret');
+       $otoken   = PConfig::get($uid, 'pumpio', 'oauth_token');
+       $osecret  = PConfig::get($uid, 'pumpio', 'oauth_token_secret');
        $hostname = PConfig::get($uid, 'pumpio', 'host');
        $username = PConfig::get($uid, "pumpio", "user");
 
@@ -1376,39 +1391,44 @@ function pumpio_getallusers(&$a, $uid)
        $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
 
        if (pumpio_reachable($url)) {
-               $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError'=>true], $users);
+               $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
        } else {
                $success = false;
        }
 
+       if (empty($users)) {
+               return;
+       }
+
        if ($users->totalItems > count($users->items)) {
                $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
 
                if (pumpio_reachable($url)) {
-                       $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError'=>true], $users);
+                       $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError' => true], $users);
                } else {
                        $success = false;
                }
        }
 
-       if (is_array($users->items)) {
-               foreach ($users->items AS $user) {
+       if (!empty($users->items)) {
+               foreach ($users->items as $user) {
                        pumpio_get_contact($uid, $user);
                }
        }
 }
 
-function pumpio_queue_hook(&$a, &$b)
+function pumpio_queue_hook(App $a, array &$b)
 {
        $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
-               dbesc(NETWORK_PUMPIO)
+               DBA::escape(Protocol::PUMPIO)
        );
-       if (!DBM::is_result($qi)) {
+
+       if (!DBA::isResult($qi)) {
                return;
        }
 
        foreach ($qi as $x) {
-               if ($x['network'] !== NETWORK_PUMPIO) {
+               if ($x['network'] !== Protocol::PUMPIO) {
                        continue;
                }
 
@@ -1418,7 +1438,7 @@ function pumpio_queue_hook(&$a, &$b)
                        WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
                        intval($x['cid'])
                );
-               if (!DBM::is_result($r)) {
+               if (!DBA::isResult($r)) {
                        continue;
                }
 
@@ -1426,10 +1446,10 @@ function pumpio_queue_hook(&$a, &$b)
 
                //logger('pumpio_queue: fetching userdata '.print_r($userdata, true));
 
-               $oauth_token = PConfig::get($userdata['uid'], "pumpio", "oauth_token");
+               $oauth_token        = PConfig::get($userdata['uid'], "pumpio", "oauth_token");
                $oauth_token_secret = PConfig::get($userdata['uid'], "pumpio", "oauth_token_secret");
-               $consumer_key = PConfig::get($userdata['uid'], "pumpio","consumer_key");
-               $consumer_secret = PConfig::get($userdata['uid'], "pumpio","consumer_secret");
+               $consumer_key       = PConfig::get($userdata['uid'], "pumpio", "consumer_key");
+               $consumer_secret    = PConfig::get($userdata['uid'], "pumpio", "consumer_secret");
 
                $host = PConfig::get($userdata['uid'], "pumpio", "host");
                $user = PConfig::get($userdata['uid'], "pumpio", "user");
@@ -1468,7 +1488,7 @@ function pumpio_queue_hook(&$a, &$b)
                                }
                                Queue::removeItem($x['id']);
                        } else {
-                               logger('pumpio_queue: send '.$username.': '.$url.' general error: ' . print_r($user, true));
+                               logger('pumpio_queue: send '.$username.': '.$z['url'].' general error: ' . print_r($user, true));
                        }
                } else {
                        logger("pumpio_queue: Error getting tokens for user ".$userdata['uid']);
@@ -1481,7 +1501,7 @@ function pumpio_queue_hook(&$a, &$b)
        }
 }
 
-function pumpio_getreceiver(&$a, $b)
+function pumpio_getreceiver(App $a, array $b)
 {
        $receiver = [];
 
@@ -1507,10 +1527,10 @@ function pumpio_getreceiver(&$a, $b)
                        $r = q("SELECT `name`, `nick`, `url` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `network` = '%s' AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
                                intval($cid),
                                intval($b["uid"]),
-                               dbesc(NETWORK_PUMPIO)
+                               DBA::escape(Protocol::PUMPIO)
                                );
 
-                       if (DBM::is_result($r)) {
+                       if (DBA::isResult($r)) {
                                $receiver["bcc"][] = [
                                                        "displayName" => $r[0]["name"],
                                                        "objectType" => "person",
@@ -1525,7 +1545,7 @@ function pumpio_getreceiver(&$a, $b)
                                "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d ".
                                "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
                                        intval($gid),
-                                       dbesc(NETWORK_PUMPIO)
+                                       DBA::escape(Protocol::PUMPIO)
                                );
 
                        foreach ($r AS $row)
@@ -1550,10 +1570,10 @@ function pumpio_getreceiver(&$a, $b)
                        $r = q("SELECT `name`, `nick`, `url` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `network` = '%s' AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
                                intval($cid),
                                intval($b["uid"]),
-                               dbesc(NETWORK_PUMPIO)
+                               DBA::escape(Protocol::PUMPIO)
                                );
 
-                       if (DBM::is_result($r)) {
+                       if (DBA::isResult($r)) {
                                $receiver["to"][] = [
                                        "displayName" => $r[0]["name"],
                                        "objectType" => "person",
@@ -1566,12 +1586,12 @@ function pumpio_getreceiver(&$a, $b)
        return $receiver;
 }
 
-function pumpio_fetchallcomments(&$a, $uid, $id)
+function pumpio_fetchallcomments(App $a, $uid, $id)
 {
-       $ckey    = PConfig::get($uid, 'pumpio', 'consumer_key');
-       $csecret = PConfig::get($uid, 'pumpio', 'consumer_secret');
-       $otoken  = PConfig::get($uid, 'pumpio', 'oauth_token');
-       $osecret = PConfig::get($uid, 'pumpio', 'oauth_token_secret');
+       $ckey     = PConfig::get($uid, 'pumpio', 'consumer_key');
+       $csecret  = PConfig::get($uid, 'pumpio', 'consumer_secret');
+       $otoken   = PConfig::get($uid, 'pumpio', 'oauth_token');
+       $osecret  = PConfig::get($uid, 'pumpio', 'oauth_token_secret');
        $hostname = PConfig::get($uid, 'pumpio', 'host');
        $username = PConfig::get($uid, "pumpio", "user");
 
@@ -1585,7 +1605,7 @@ function pumpio_fetchallcomments(&$a, $uid, $id)
        // Fetching the original post
        $condition = ["`uri` = ? AND `uid` = ? AND `extid` != ''", $id, $uid];
        $item = Item::selectFirst(['extid'], $condition);
-       if (!DBM::is_result($item)) {
+       if (!DBA::isResult($item)) {
                return false;
        }
 
@@ -1619,7 +1639,9 @@ function pumpio_fetchallcomments(&$a, $uid, $id)
                        $like->object = new stdClass;
                        $like->object->id = $item->id;
                        $like->actor = new stdClass;
-                       $like->actor->displayName = $item->displayName;
+                       if (!empty($item->displayName)) {
+                               $like->actor->displayName = $item->displayName;
+                       }
                        //$like->actor->preferredUsername = $item->preferredUsername;
                        //$like->actor->image = $item->image;
                        $like->actor->url = $item->url;
@@ -1639,11 +1661,11 @@ function pumpio_fetchallcomments(&$a, $uid, $id)
                }
 
                // Checking if the comment already exists - Two queries for speed issues
-               if (dba::exists('item', ['uri' => $item->id, 'uid' => $uid])) {
+               if (Item::exists(['uri' => $item->id, 'uid' => $uid])) {
                        continue;
                }
 
-               if (dba::exists('item', ['extid' => $item->id, 'uid' => $uid])) {
+               if (Item::exists(['extid' => $item->id, 'uid' => $uid])) {
                        continue;
                }