]> git.mxchange.org Git - friendica-addons.git/blobdiff - statusnet/statusnet.php
Update .drone.yml
[friendica-addons.git] / statusnet / statusnet.php
index a89f9cef7ede966325ed38e5df43e99695c1f93b..bb51e24ba6287bc2822a032ea0d4f8076cd44b43 100644 (file)
@@ -40,9 +40,9 @@ require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . '
 use CodebirdSN\CodebirdSN;
 use Friendica\App;
 use Friendica\Content\OEmbed;
+use Friendica\Content\PageInfo;
 use Friendica\Content\Text\HTML;
 use Friendica\Content\Text\Plaintext;
-use Friendica\Core\Config;
 use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
@@ -52,12 +52,11 @@ use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
-use Friendica\Model\ItemContent;
 use Friendica\Model\Photo;
+use Friendica\Model\Post;
 use Friendica\Model\User;
 use Friendica\Protocol\Activity;
 use Friendica\Util\DateTimeFormat;
-use Friendica\Util\Network;
 use Friendica\Util\Strings;
 
 function statusnet_install()
@@ -75,24 +74,6 @@ function statusnet_install()
        Logger::log("installed GNU Social");
 }
 
-function statusnet_uninstall()
-{
-       Hook::unregister('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
-       Hook::unregister('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
-       Hook::unregister('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
-       Hook::unregister('hook_fork', 'addon/statusnet/statusnet.php', 'statusnet_hook_fork');
-       Hook::unregister('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local');
-       Hook::unregister('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets');
-       Hook::unregister('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron');
-       Hook::unregister('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body');
-       Hook::unregister('check_item_notification', 'addon/statusnet/statusnet.php', 'statusnet_check_item_notification');
-
-       // old setting - remove only
-       Hook::unregister('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook');
-       Hook::unregister('addon_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings');
-       Hook::unregister('addon_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post');
-}
-
 function statusnet_check_item_notification(App $a, &$notification_data)
 {
        if (DI::pConfig()->get($notification_data["uid"], 'statusnet', 'post')) {
@@ -151,11 +132,11 @@ function statusnet_settings_post(App $a, $post)
                         * use them. All the data are available in the global config.
                         * Check the API Url never the less and blame the admin if it's not working ^^
                         */
-                       $globalsn = Config::get('statusnet', 'sites');
+                       $globalsn = DI::config()->get('statusnet', 'sites');
                        foreach ($globalsn as $asn) {
                                if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl']) {
                                        $apibase = $asn['apiurl'];
-                                       $c = Network::fetchUrl($apibase . 'statusnet/version.xml');
+                                       $c = DI::httpRequest()->fetch($apibase . 'statusnet/version.xml');
                                        if (strlen($c) > 0) {
                                                DI::pConfig()->set(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey']);
                                                DI::pConfig()->set(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret']);
@@ -173,7 +154,7 @@ function statusnet_settings_post(App $a, $post)
                                //  we'll check the API Version for that, if we don't get one we'll try to fix the path but will
                                //  resign quickly after this one try to fix the path ;-)
                                $apibase = $_POST['statusnet-baseapi'];
-                               $c = Network::fetchUrl($apibase . 'statusnet/version.xml');
+                               $c = DI::httpRequest()->fetch($apibase . 'statusnet/version.xml');
                                if (strlen($c) > 0) {
                                        //  ok the API path is correct, let's save the settings
                                        DI::pConfig()->set(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
@@ -183,7 +164,7 @@ function statusnet_settings_post(App $a, $post)
                                } else {
                                        //  the API path is not correct, maybe missing trailing / ?
                                        $apibase = $apibase . '/';
-                                       $c = Network::fetchUrl($apibase . 'statusnet/version.xml');
+                                       $c = DI::httpRequest()->fetch($apibase . 'statusnet/version.xml');
                                        if (strlen($c) > 0) {
                                                //  ok the API path is now correct, let's save the settings
                                                DI::pConfig()->set(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']);
@@ -224,8 +205,6 @@ function statusnet_settings_post(App $a, $post)
 
                                        if (!intval($_POST['statusnet-mirror']))
                                                DI::pConfig()->delete(local_user(), 'statusnet', 'lastid');
-
-                                       info(DI::l10n()->t('GNU Social settings updated.') . EOL);
                                }
                        }
                }
@@ -277,7 +256,7 @@ function statusnet_settings(App $a, &$s)
                /*               * *
                 * no consumer keys
                 */
-               $globalsn = Config::get('statusnet', 'sites');
+               $globalsn = DI::config()->get('statusnet', 'sites');
                /*               * *
                 * lets check if we have one or more globally configured GNU Social
                 * server OAuth credentials in the configuration. If so offer them
@@ -423,7 +402,7 @@ function statusnet_hook_fork(App $a, array &$b)
 
        if (DI::pConfig()->get($post['uid'], 'statusnet', 'import')) {
                // Don't fork if it isn't a reply to a GNU Social post
-               if (($post['parent'] != $post['id']) && !Item::exists(['id' => $post['parent'], 'network' => Protocol::STATUSNET])) {
+               if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::STATUSNET])) {
                        Logger::log('No GNU Social parent found for item ' . $post['id']);
                        $b['execute'] = false;
                        return;
@@ -502,6 +481,8 @@ function statusnet_post_hook(App $a, &$b)
                        return;
        }
 
+       $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
+
        $api = DI::pConfig()->get($b["uid"], 'statusnet', 'baseapi');
        $hostname = preg_replace("=https?://([\w\.]*)/.*=ism", "$1", $api);
 
@@ -516,7 +497,7 @@ function statusnet_post_hook(App $a, &$b)
                }
 
                $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
-               $orig_post = Item::selectFirst(['author-link', 'uri'], $condition);
+               $orig_post = Post::selectFirst(['author-link', 'uri'], $condition);
                if (!DBA::isResult($orig_post)) {
                        Logger::log("statusnet_post_hook: no parent found " . $b["thr-parent"]);
                        return;
@@ -598,7 +579,7 @@ function statusnet_post_hook(App $a, &$b)
                DI::pConfig()->set($b['uid'], 'statusnet', 'max_char', $max_char);
 
                $tempfile = "";
-               $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 7);
+               $msgarr = Plaintext::getPost($b, $max_char, true, 7);
                $msg = $msgarr["text"];
 
                if (($msg == "") && isset($msgarr["title"]))
@@ -613,7 +594,7 @@ function statusnet_post_hook(App $a, &$b)
                }
 
                if ($image != "") {
-                       $img_str = Network::fetchUrl($image);
+                       $img_str = DI::httpRequest()->fetch($image);
                        $tempfile = tempnam(get_temppath(), "cache");
                        file_put_contents($tempfile, $img_str);
                        $postdata = ["status" => $msg, "media[]" => $tempfile];
@@ -685,12 +666,12 @@ function statusnet_addon_admin_post(App $a)
                }
        }
 
-       $sites = Config::set('statusnet', 'sites', $sites);
+       $sites = DI::config()->set('statusnet', 'sites', $sites);
 }
 
 function statusnet_addon_admin(App $a, &$o)
 {
-       $sites = Config::get('statusnet', 'sites');
+       $sites = DI::config()->get('statusnet', 'sites');
        $sitesform = [];
        if (is_array($sites)) {
                foreach ($sites as $id => $s) {
@@ -737,7 +718,7 @@ function statusnet_prepare_body(App $a, &$b)
                $item["plink"] = DI::baseUrl()->get() . "/display/" . $item["guid"];
 
                $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
-               $orig_post = Item::selectFirst(['author-link', 'uri'], $condition);
+               $orig_post = Post::selectFirst(['author-link', 'uri'], $condition);
                if (DBA::isResult($orig_post)) {
                        $nick = preg_replace("=https?://(.*)/(.*)=ism", "$2", $orig_post["author-link"]);
 
@@ -749,7 +730,7 @@ function statusnet_prepare_body(App $a, &$b)
                        }
                }
 
-               $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, 7);
+               $msgarr = Plaintext::getPost($item, $max_char, true, 7);
                $msg = $msgarr["text"];
 
                if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
@@ -766,9 +747,9 @@ function statusnet_prepare_body(App $a, &$b)
 
 function statusnet_cron(App $a, $b)
 {
-       $last = Config::get('statusnet', 'last_poll');
+       $last = DI::config()->get('statusnet', 'last_poll');
 
-       $poll_interval = intval(Config::get('statusnet', 'poll_interval'));
+       $poll_interval = intval(DI::config()->get('statusnet', 'poll_interval'));
        if (!$poll_interval) {
                $poll_interval = STATUSNET_DEFAULT_POLL_INTERVAL;
        }
@@ -790,7 +771,7 @@ function statusnet_cron(App $a, $b)
                }
        }
 
-       $abandon_days = intval(Config::get('system', 'account_abandon_days'));
+       $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
        if ($abandon_days < 1) {
                $abandon_days = 0;
        }
@@ -815,7 +796,7 @@ function statusnet_cron(App $a, $b)
 
        Logger::log('statusnet: cron_end');
 
-       Config::set('statusnet', 'last_poll', time());
+       DI::config()->set('statusnet', 'last_poll', time());
 }
 
 function statusnet_fetchtimeline(App $a, $uid)
@@ -833,7 +814,7 @@ function statusnet_fetchtimeline(App $a, $uid)
        //  hostname of the node if neither one is set.
        $application_name = DI::pConfig()->get($uid, 'statusnet', 'application_name');
        if ($application_name == "") {
-               $application_name = Config::get('statusnet', 'application_name');
+               $application_name = DI::config()->get('statusnet', 'application_name');
        }
        if ($application_name == "") {
                $application_name = DI::baseUrl()->getHostname();
@@ -897,7 +878,7 @@ function statusnet_fetchtimeline(App $a, $uid)
 
                                $_REQUEST["title"] = "";
 
-                               $_REQUEST["body"] = add_page_info_to_body($post->text, true);
+                               $_REQUEST["body"] = $post->text;
                                if (is_string($post->place->name)) {
                                        $_REQUEST["location"] = $post->place->name;
                                }
@@ -1118,31 +1099,24 @@ function statusnet_createpost(App $a, $uid, $post, $self, $create_user, $only_ex
 
        $postarray['uri'] = $hostname . "::" . $content->id;
 
-       if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
+       if (Post::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
                return [];
        }
 
        $contactid = 0;
 
        if (!empty($content->in_reply_to_status_id)) {
+               $thr_parent = $hostname . "::" . $content->in_reply_to_status_id;
 
-               $parent = $hostname . "::" . $content->in_reply_to_status_id;
-
-               $fields = ['uri', 'parent-uri', 'parent'];
-               $item = Item::selectFirst($fields, ['uri' => $parent, 'uid' => $uid]);
-
+               $item = Post::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
                if (!DBA::isResult($item)) {
-                       $item = Item::selectFirst($fields, ['extid' => $parent, 'uid' => $uid]);
+                       $item = Post::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid]);
                }
 
                if (DBA::isResult($item)) {
                        $postarray['thr-parent'] = $item['uri'];
-                       $postarray['parent-uri'] = $item['parent-uri'];
-                       $postarray['parent'] = $item['parent'];
                        $postarray['object-type'] = Activity\ObjectType::COMMENT;
                } else {
-                       $postarray['thr-parent'] = $postarray['uri'];
-                       $postarray['parent-uri'] = $postarray['uri'];
                        $postarray['object-type'] = Activity\ObjectType::NOTE;
                }
 
@@ -1166,7 +1140,6 @@ function statusnet_createpost(App $a, $uid, $post, $self, $create_user, $only_ex
                // Don't create accounts of people who just comment something
                $create_user = false;
        } else {
-               $postarray['parent-uri'] = $postarray['uri'];
                $postarray['object-type'] = Activity\ObjectType::NOTE;
        }
 
@@ -1203,9 +1176,7 @@ function statusnet_createpost(App $a, $uid, $post, $self, $create_user, $only_ex
 
        $postarray['body'] = HTML::toBBCode($content->statusnet_html);
 
-       $converted = statusnet_convertmsg($a, $postarray['body'], false);
-       $postarray['body'] = $converted["body"];
-       $postarray['tag'] = $converted["tags"];
+       $postarray['body'] = statusnet_convertmsg($a, $postarray['body']);
 
        $postarray['created'] = DateTimeFormat::utc($content->created_at);
        $postarray['edited'] = DateTimeFormat::utc($content->created_at);
@@ -1434,7 +1405,7 @@ function statusnet_complete_conversation(App $a, $uid, $self, $create_user, $nic
        }
 }
 
-function statusnet_convertmsg(App $a, $body, $no_tags = false)
+function statusnet_convertmsg(App $a, $body)
 {
        $body = preg_replace("=\[url\=https?://([0-9]*).([0-9]*).([0-9]*).([0-9]*)/([0-9]*)\](.*?)\[\/url\]=ism", "$1.$2.$3.$4/$5", $body);
 
@@ -1452,7 +1423,7 @@ function statusnet_convertmsg(App $a, $body, $no_tags = false)
 
                        Logger::log("statusnet_convertmsg: expanding url " . $match[1], Logger::DEBUG);
 
-                       $expanded_url = Network::finalUrl($match[1]);
+                       $expanded_url = DI::httpRequest()->finalUrl($match[1]);
 
                        Logger::log("statusnet_convertmsg: fetching data for " . $expanded_url, Logger::DEBUG);
 
@@ -1476,7 +1447,7 @@ function statusnet_convertmsg(App $a, $body, $no_tags = false)
                        } elseif ($oembed_data->type != "link") {
                                $body = str_replace($search, "[url=" . $expanded_url . "]" . $expanded_url . "[/url]", $body);
                        } else {
-                               $img_str = Network::fetchUrl($expanded_url, true, 4);
+                               $img_str = DI::httpRequest()->fetch($expanded_url, 4);
 
                                $tempfile = tempnam(get_temppath(), "cache");
                                file_put_contents($tempfile, $img_str);
@@ -1497,7 +1468,7 @@ function statusnet_convertmsg(App $a, $body, $no_tags = false)
                }
 
                if ($footerurl != "") {
-                       $footer = add_page_info($footerurl);
+                       $footer = "\n" . PageInfo::getFooterFromUrl($footerurl);
                }
 
                if (($footerlink != "") && (trim($footer) != "")) {
@@ -1511,36 +1482,7 @@ function statusnet_convertmsg(App $a, $body, $no_tags = false)
                }
        }
 
-       if ($no_tags) {
-               return ["body" => $body, "tags" => ""];
-       }
-
-       $str_tags = '';
-
-       $cnt = preg_match_all("/([!#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", $body, $matches, PREG_SET_ORDER);
-       if ($cnt) {
-               foreach ($matches as $mtch) {
-                       if (strlen($str_tags)) {
-                               $str_tags .= ',';
-                       }
-
-                       if ($mtch[1] == "#") {
-                               // Replacing the hash tags that are directed to the GNU Social server with internal links
-                               $snhash = "#[url=" . $mtch[2] . "]" . $mtch[3] . "[/url]";
-                               $frdchash = '#[url=' . DI::baseUrl()->get() . '/search?tag=' . $mtch[3] . ']' . $mtch[3] . '[/url]';
-                               $body = str_replace($snhash, $frdchash, $body);
-
-                               $str_tags .= $frdchash;
-                       } else {
-                               $str_tags .= "@[url=" . $mtch[2] . "]" . $mtch[3] . "[/url]";
-                       }
-                       // To-Do:
-                       // There is a problem with links with to GNU Social groups, so these links are stored with "@" like friendica groups
-                       //$str_tags .= $mtch[1]."[url=".$mtch[2]."]".$mtch[3]."[/url]";
-               }
-       }
-
-       return ["body" => $body, "tags" => $str_tags];
+       return $body;
 }
 
 function statusnet_fetch_own_contact(App $a, $uid)