And one notice removed for twitter
[friendica-addons.git/.git] / twitter / twitter.php
index ab7697b..8b71664 100644 (file)
@@ -70,6 +70,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;
@@ -521,7 +522,7 @@ function twitter_post_hook(App $a, array &$b)
        }
 
        // if post comes from twitter don't send it back
-       if ($b['extid'] == NETWORK_TWITTER) {
+       if ($b['extid'] == Protocol::TWITTER) {
                return;
        }
 
@@ -628,7 +629,7 @@ function twitter_post_hook(App $a, array &$b)
 
                        $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
 
-                       Queue::add($a->contact, NETWORK_TWITTER, $s);
+                       Queue::add($a->contact, Protocol::TWITTER, $s);
                        notice(L10n::t('Twitter post failed. Queued for retry.') . EOL);
                } elseif ($iscomment) {
                        logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
@@ -734,15 +735,9 @@ function twitter_expire(App $a)
                return;
        }
 
-       $r = DBA::select('item', ['id', 'iaid', 'icid'], ['deleted' => true, 'network' => NETWORK_TWITTER]);
+       $r = Item::select(['id'], ['deleted' => true, 'network' => Protocol::TWITTER]);
        while ($row = DBA::fetch($r)) {
                DBA::delete('item', ['id' => $row['id']]);
-               if (!empty($row['iaid']) && !DBA::exists('item', ['iaid' => $row['iaid']])) {
-                       DBA::delete('item-activity', ['id' => $row['iaid']]);
-               }
-               if (!empty($row['icid']) && !DBA::exists('item', ['icid' => $row['icid']])) {
-                       DBA::delete('item-content', ['id' => $row['icid']]);
-               }
        }
        DBA::close($r);
 
@@ -754,7 +749,7 @@ function twitter_expire(App $a)
        if (DBA::isResult($r)) {
                foreach ($r as $rr) {
                        logger('twitter_expire: user ' . $rr['uid']);
-                       Item::expire($rr['uid'], $days, NETWORK_TWITTER, true);
+                       Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
                }
        }
 
@@ -763,7 +758,7 @@ function twitter_expire(App $a)
 
 function twitter_prepare_body(App $a, array &$b)
 {
-       if ($b["item"]["network"] != NETWORK_TWITTER) {
+       if ($b["item"]["network"] != Protocol::TWITTER) {
                return;
        }
 
@@ -812,8 +807,8 @@ function twitter_do_mirrorpost(App $a, $uid, $post)
 {
        $datarray['api_source'] = true;
        $datarray['profile_uid'] = $uid;
-       $datarray['extid'] = NETWORK_TWITTER;
-       $datarray['message_id'] = Item::newURI($uid, NETWORK_TWITTER . ':' . $post->id);
+       $datarray['extid'] = Protocol::TWITTER;
+       $datarray['message_id'] = Item::newURI($uid, Protocol::TWITTER . ':' . $post->id);
        $datarray['protocol'] = Conversation::PARCEL_TWITTER;
        $datarray['source'] = json_encode($post);
        $datarray['title'] = '';
@@ -822,6 +817,10 @@ function twitter_do_mirrorpost(App $a, $uid, $post)
                // We don't support nested shares, so we mustn't show quotes as shares on retweets
                $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true);
 
+               if (empty($item['body'])) {
+                       return [];
+               }
+
                $datarray['body'] = "\n" . share_header(
                        $item['author-name'],
                        $item['author-link'],
@@ -835,6 +834,10 @@ function twitter_do_mirrorpost(App $a, $uid, $post)
        } else {
                $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
 
+               if (empty($item['body'])) {
+                       return [];
+               }
+
                $datarray['body'] = $item['body'];
        }
 
@@ -912,6 +915,10 @@ function twitter_fetchtimeline(App $a, $uid)
 
                                $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
 
+                               if (empty($_REQUEST['body'])) {
+                                       continue;
+                               }
+
                                logger('twitter: posting for user ' . $uid);
 
                                item_post($a);
@@ -924,14 +931,14 @@ function twitter_fetchtimeline(App $a, $uid)
 function twitter_queue_hook(App $a)
 {
        $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
-               DBA::escape(NETWORK_TWITTER)
+               DBA::escape(Protocol::TWITTER)
        );
        if (!DBA::isResult($qi)) {
                return;
        }
 
        foreach ($qi as $x) {
-               if ($x['network'] !== NETWORK_TWITTER) {
+               if ($x['network'] !== Protocol::TWITTER) {
                        continue;
                }
 
@@ -995,7 +1002,7 @@ function twitter_fix_avatar($avatar)
 
 function twitter_fetch_contact($uid, $data, $create_user)
 {
-       if ($data->id_str == "") {
+       if (empty($data->id_str)) {
                return -1;
        }
 
@@ -1003,13 +1010,13 @@ function twitter_fetch_contact($uid, $data, $create_user)
        $url = "https://twitter.com/" . $data->screen_name;
        $addr = $data->screen_name . "@twitter.com";
 
-       GContact::update(["url" => $url, "network" => NETWORK_TWITTER,
+       GContact::update(["url" => $url, "network" => Protocol::TWITTER,
                "photo" => $avatar, "hide" => true,
                "name" => $data->name, "nick" => $data->screen_name,
                "location" => $data->location, "about" => $data->description,
                "addr" => $addr, "generation" => 2]);
 
-       $fields = ['url' => $url, 'network' => NETWORK_TWITTER,
+       $fields = ['url' => $url, 'network' => Protocol::TWITTER,
                'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
                 'location' => $data->location, 'about' => $data->description];
 
@@ -1169,7 +1176,7 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
                                        //$dontincludemedia = true;
                                        $type = $oembed_data->type;
                                        $footerurl = $expanded_url;
-                                       $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]";
+                                       $footerlink = "[url=" . $expanded_url . "]" . $url->display_url . "[/url]";
 
                                        $body = str_replace($url->url, $footerlink, $body);
                                        //} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) {
@@ -1177,13 +1184,20 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
                                        $body = str_replace($url->url, "[url=" . $expanded_url . "][img]" . $oembed_data->url . "[/img][/url]", $body);
                                        //$dontincludemedia = true;
                                } elseif ($oembed_data->type != "link") {
-                                       $body = str_replace($url->url, "[url=" . $expanded_url . "]" . $expanded_url . "[/url]", $body);
+                                       $body = str_replace($url->url, "[url=" . $expanded_url . "]" . $url->display_url . "[/url]", $body);
                                } else {
                                        $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4);
 
                                        $tempfile = tempnam(get_temppath(), "cache");
                                        file_put_contents($tempfile, $img_str);
-                                       $mime = image_type_to_mime_type(exif_imagetype($tempfile));
+
+                                       // See http://php.net/manual/en/function.exif-imagetype.php#79283
+                                       if (filesize($tempfile) > 11) {
+                                               $mime = image_type_to_mime_type(exif_imagetype($tempfile));
+                                       } else {
+                                               $mime = false;
+                                       }
+
                                        unlink($tempfile);
 
                                        if (substr($mime, 0, 6) == "image/") {
@@ -1193,7 +1207,7 @@ function twitter_expand_entities(App $a, $body, $item, $picture)
                                        } else {
                                                $type = $oembed_data->type;
                                                $footerurl = $expanded_url;
-                                               $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]";
+                                               $footerlink = "[url=" . $expanded_url . "]" . $url->display_url . "[/url]";
 
                                                $body = str_replace($url->url, $footerlink, $body);
                                        }
@@ -1336,7 +1350,7 @@ function twitter_media_entities($post, array &$postarray)
 function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $only_existing_contact, $noquote)
 {
        $postarray = [];
-       $postarray['network'] = NETWORK_TWITTER;
+       $postarray['network'] = Protocol::TWITTER;
        $postarray['uid'] = $uid;
        $postarray['wall'] = 0;
        $postarray['uri'] = "twitter::" . $post->id_str;
@@ -1344,7 +1358,7 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
        $postarray['source'] = json_encode($post);
 
        // Don't import our own comments
-       if (DBA::exists('item', ['extid' => $postarray['uri'], 'uid' => $uid])) {
+       if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
                logger("Item with extid " . $postarray['uri'] . " found.", LOGGER_DEBUG);
                return [];
        }
@@ -1465,6 +1479,10 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
        if (!empty($post->retweeted_status)) {
                $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
 
+               if (empty($retweet['body'])) {
+                       return [];
+               }
+
                $retweet['source'] = $postarray['source'];
                $retweet['private'] = $postarray['private'];
                $retweet['allow_cid'] = $postarray['allow_cid'];
@@ -1479,6 +1497,10 @@ function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $onl
        if (!empty($post->quoted_status) && !$noquote) {
                $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
 
+               if (empty($quoted['body'])) {
+                       return [];
+               }
+
                $postarray['body'] = $statustext;
 
                $postarray['body'] .= "\n" . share_header(
@@ -1502,7 +1524,7 @@ function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection,
 
        $posts = [];
 
-       while ($post->in_reply_to_status_id_str != "") {
+       while (!empty($post->in_reply_to_status_id_str)) {
                $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str];
 
                try {
@@ -1522,7 +1544,7 @@ function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection,
                        break;
                }
 
-               if (DBA::exists('item', ['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
+               if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
                        break;
                }
 
@@ -1666,7 +1688,7 @@ function twitter_fetchhometimeline(App $a, $uid)
 
                        $notify = false;
 
-                       if ($postarray['uri'] == $postarray['parent-uri']) {
+                       if (($postarray['uri'] == $postarray['parent-uri']) && ($postarray['author-link'] == $postarray['owner-link'])) {
                                $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
                                if (DBA::isResult($contact)) {
                                        $notify = Item::isRemoteSelf($contact, $postarray);
@@ -1722,7 +1744,7 @@ function twitter_fetchhometimeline(App $a, $uid)
 
                        $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
 
-                       if (trim($postarray['body']) == "") {
+                       if (empty($postarray['body'])) {
                                continue;
                        }