removed code
[friendica-addons.git/.git] / pumpio / pumpio.php
index fb96d5b..9960ec5 100644 (file)
@@ -13,6 +13,7 @@ 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\Model\Contact;
@@ -137,9 +138,6 @@ function pumpio_registerclient(App $a, $host)
 
 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(App $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(App $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");
@@ -360,19 +358,17 @@ function pumpio_settings_post(App $a, array &$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', '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'         , $_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', '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");
                }
        }
 }
@@ -417,7 +413,7 @@ function pumpio_send(App $a, array &$b)
 
        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 (!DBA::isResult($orig_post)) {
@@ -578,7 +574,7 @@ function pumpio_send(App $a, array &$b)
 
                        $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);
                }
        }
@@ -655,7 +651,7 @@ function pumpio_action(App $a, $uid, $uri, $action, $content = "")
 
                $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);
        }
 }
@@ -778,6 +774,7 @@ function pumpio_fetchtimeline(App $a, $uid)
                $success = $client->CallAPI($url, 'GET', [], ['FailOnAccessError'=>true], $user);
        } else {
                $success = false;
+               $user = [];
        }
 
        if (!$success) {
@@ -830,7 +827,7 @@ function pumpio_fetchtimeline(App $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 != "") {
@@ -952,7 +949,7 @@ function pumpio_dolike(App $a, $uid, $self, $post, $own_id, $threadcompletion =
        }
 
        $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(App $a, $uid, $self, $post, $own_id, $threadcompletion =
        $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(App $a, $uid, $self, $post, $own_id, $threadcompletion =
 
 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)];
@@ -1034,8 +1033,8 @@ function pumpio_get_contact($uid, $contact, $no_insert = false)
                        DBA::escape($contact->displayName),
                        DBA::escape($contact->preferredUsername),
                        DBA::escape($contact->image->url),
-                       DBA::escape(NETWORK_PUMPIO),
-                       intval(CONTACT_IS_FRIEND),
+                       DBA::escape(Protocol::PUMPIO),
+                       intval(Contact::FRIEND),
                        intval(1),
                        DBA::escape($contact->location->displayName),
                        DBA::escape($contact->summary),
@@ -1079,13 +1078,13 @@ 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;
        }
@@ -1110,10 +1109,10 @@ function pumpio_dopost(App $a, $client, $uid, $self, $post, $own_id, $threadcomp
 
        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(App $a, $client, $uid, $self, $post, $own_id, $threadcomp
        }
 
        $postarray = [];
-       $postarray['network'] = NETWORK_PUMPIO;
+       $postarray['network'] = Protocol::PUMPIO;
        $postarray['uid'] = $uid;
        $postarray['wall'] = 0;
        $postarray['uri'] = $post->object->id;
@@ -1198,7 +1197,10 @@ function pumpio_dopost(App $a, $client, $uid, $self, $post, $own_id, $threadcomp
                        $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(App $a, $client, $uid, $self, $post, $own_id, $threadcomp
                $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(App $a, $client, $uid, $self, $post, $own_id, $threadcomp
        $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(App $a, $client, $uid, $self, $post, $own_id, $threadcomp
                        $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]";
        }
 
@@ -1304,7 +1319,7 @@ function pumpio_fetchinbox(App $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",
-                               DBA::escape(NETWORK_PUMPIO),
+                               DBA::escape(Protocol::PUMPIO),
                                intval($uid)
                        );
 
@@ -1381,6 +1396,10 @@ function pumpio_getallusers(App &$a, $uid)
                $success = false;
        }
 
+       if (empty($users)) {
+               return;
+       }
+
        if ($users->totalItems > count($users->items)) {
                $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
 
@@ -1391,7 +1410,7 @@ function pumpio_getallusers(App &$a, $uid)
                }
        }
 
-       if (is_array($users->items)) {
+       if (!empty($users->items)) {
                foreach ($users->items as $user) {
                        pumpio_get_contact($uid, $user);
                }
@@ -1401,7 +1420,7 @@ function pumpio_getallusers(App &$a, $uid)
 function pumpio_queue_hook(App $a, array &$b)
 {
        $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
-               DBA::escape(NETWORK_PUMPIO)
+               DBA::escape(Protocol::PUMPIO)
        );
 
        if (!DBA::isResult($qi)) {
@@ -1409,7 +1428,7 @@ function pumpio_queue_hook(App $a, array &$b)
        }
 
        foreach ($qi as $x) {
-               if ($x['network'] !== NETWORK_PUMPIO) {
+               if ($x['network'] !== Protocol::PUMPIO) {
                        continue;
                }
 
@@ -1469,7 +1488,7 @@ function pumpio_queue_hook(App $a, array &$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']);
@@ -1508,7 +1527,7 @@ function pumpio_getreceiver(App $a, array $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"]),
-                               DBA::escape(NETWORK_PUMPIO)
+                               DBA::escape(Protocol::PUMPIO)
                                );
 
                        if (DBA::isResult($r)) {
@@ -1526,7 +1545,7 @@ function pumpio_getreceiver(App $a, array $b)
                                "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d ".
                                "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
                                        intval($gid),
-                                       DBA::escape(NETWORK_PUMPIO)
+                                       DBA::escape(Protocol::PUMPIO)
                                );
 
                        foreach ($r AS $row)
@@ -1551,7 +1570,7 @@ function pumpio_getreceiver(App $a, array $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"]),
-                               DBA::escape(NETWORK_PUMPIO)
+                               DBA::escape(Protocol::PUMPIO)
                                );
 
                        if (DBA::isResult($r)) {
@@ -1620,7 +1639,9 @@ function pumpio_fetchallcomments(App $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;
@@ -1640,11 +1661,11 @@ function pumpio_fetchallcomments(App $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;
                }