]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
Remove references to include/bbcode.php
[friendica-addons.git] / twitter / twitter.php
index 47b4fa0751064123690570b4419ee6945df68811..a460fd70f14b894f506805bcb45a7accec0ca4d1 100644 (file)
@@ -200,11 +200,11 @@ function twitter_settings_post(App $a, $post)
                return;
        }
        // don't check twitter settings if twitter submit button is not clicked
-       if (!x($_POST, 'twitter-submit')) {
+       if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
                return;
        }
 
-       if (isset($_POST['twitter-disconnect'])) {
+       if (!empty($_POST['twitter-disconnect'])) {
                /*               * *
                 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
                 * from the user configuration
@@ -229,12 +229,20 @@ function twitter_settings_post(App $a, $post)
                        //  the token and secret for which the PIN was generated were hidden in the settings
                        //  form as token and token2, we need a new connection to Twitter using these token
                        //  and secret to request a Access Token with the PIN
-                       $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
-                       $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]);
-                       //  ok, now that we have the Access Token, save them in the user config
-                       PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
-                       PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
-                       PConfig::set(local_user(), 'twitter', 'post', 1);
+                       try {
+                               if (empty($_POST['twitter-pin'])) {
+                                       throw new Exception(L10n::t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.'));
+                               }
+
+                               $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
+                               $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]);
+                               //  ok, now that we have the Access Token, save them in the user config
+                               PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
+                               PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
+                               PConfig::set(local_user(), 'twitter', 'post', 1);
+                       } catch(Exception $e) {
+                               info($e->getMessage());
+                       }
                        //  reload the Addon Settings page, if we don't do it see Bug #42
                        goaway('settings/connectors');
                } else {
@@ -288,34 +296,28 @@ function twitter_settings(App $a, &$s)
        $s .= '</span>';
 
        if ((!$ckey) && (!$csecret)) {
-               /*               * *
-                * no global consumer keys
+               /* no global consumer keys
                 * display warning and skip personal config
                 */
                $s .= '<p>' . L10n::t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
        } else {
-               /*               * *
-                * ok we have a consumer key pair now look into the OAuth stuff
-                */
+               // ok we have a consumer key pair now look into the OAuth stuff
                if ((!$otoken) && (!$osecret)) {
-                       /*                       * *
-                        * the user has not yet connected the account to twitter...
+                       /* the user has not yet connected the account to twitter...
                         * get a temporary OAuth key/secret pair and display a button with
                         * which the user can request a PIN to connect the account to a
                         * account at Twitter.
                         */
                        $connection = new TwitterOAuth($ckey, $csecret);
                        $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
-                       /*                       * *
-                        *  make some nice form
-                        */
+
                        $s .= '<p>' . L10n::t('At this Friendica instance the Twitter addon was enabled but you have not yet connected your account to your Twitter account. To do so click the button below to get a PIN from Twitter which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to Twitter.') . '</p>';
-                       $s .= '<a href="' . $connection->url('oauth/authorize', ['oauth_token' => $result->oauth_token]) . '" target="_twitter"><img src="addon/twitter/lighter.png" alt="' . L10n::t('Log in with Twitter') . '"></a>';
+                       $s .= '<a href="' . $connection->url('oauth/authorize', ['oauth_token' => $result['oauth_token']]) . '" target="_twitter"><img src="addon/twitter/lighter.png" alt="' . L10n::t('Log in with Twitter') . '"></a>';
                        $s .= '<div id="twitter-pin-wrapper">';
                        $s .= '<label id="twitter-pin-label" for="twitter-pin">' . L10n::t('Copy the PIN from Twitter here') . '</label>';
                        $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
-                       $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result->oauth_token . '" />';
-                       $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result->oauth_token_secret . '" />';
+                       $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result['oauth_token'] . '" />';
+                       $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result['oauth_token_secret'] . '" />';
                        $s .= '</div><div class="clear"></div>';
                        $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
                } else {
@@ -1147,12 +1149,24 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
        return $contact_id;
 }
 
-function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $picture)
+function twitter_expand_entities(App $a, $body, $item, $picture)
 {
-       $tags = "";
-
        $plain = $body;
 
+       $tags_arr = [];
+
+       foreach ($item->entities->hashtags AS $hashtag) {
+               $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]";
+               $tags_arr["#" . $hashtag->text] = $url;
+               $body = str_replace("#" . $hashtag->text, $url, $body);
+       }
+
+       foreach ($item->entities->user_mentions AS $mention) {
+               $url = "@[url=https://twitter.com/" . rawurlencode($mention->screen_name) . "]" . $mention->screen_name . "[/url]";
+               $tags_arr["@" . $mention->screen_name] = $url;
+               $body = str_replace("@" . $mention->screen_name, $url, $body);
+       }
+
        if (isset($item->entities->urls)) {
                $type = "";
                $footerurl = "";
@@ -1233,66 +1247,49 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur
                } elseif (($footer == "") && ($picture == "")) {
                        $body = add_page_info_to_body($body);
                }
+       }
 
-               if ($no_tags) {
-                       return ["body" => $body, "tags" => "", "plain" => $plain];
-               }
-
-               $tags_arr = [];
-
-               foreach ($item->entities->hashtags AS $hashtag) {
-                       $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]";
-                       $tags_arr["#" . $hashtag->text] = $url;
-                       $body = str_replace("#" . $hashtag->text, $url, $body);
-               }
-
-               foreach ($item->entities->user_mentions AS $mention) {
-                       $url = "@[url=https://twitter.com/" . rawurlencode($mention->screen_name) . "]" . $mention->screen_name . "[/url]";
-                       $tags_arr["@" . $mention->screen_name] = $url;
-                       $body = str_replace("@" . $mention->screen_name, $url, $body);
-               }
+       // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
+       $tags = get_tags($body);
 
-               // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
-               $tags = get_tags($body);
+       if (count($tags)) {
+               foreach ($tags as $tag) {
+                       if (strstr(trim($tag), " ")) {
+                               continue;
+                       }
 
-               if (count($tags)) {
-                       foreach ($tags as $tag) {
-                               if (strstr(trim($tag), " ")) {
+                       if (strpos($tag, '#') === 0) {
+                               if (strpos($tag, '[url=')) {
                                        continue;
                                }
 
-                               if (strpos($tag, '#') === 0) {
-                                       if (strpos($tag, '[url=')) {
-                                               continue;
-                                       }
-
-                                       // don't link tags that are already embedded in links
-                                       if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
-                                               continue;
-                                       }
-                                       if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
-                                               continue;
-                                       }
-
-                                       $basetag = str_replace('_', ' ', substr($tag, 1));
-                                       $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
-                                       $body = str_replace($tag, $url, $body);
-                                       $tags_arr["#" . $basetag] = $url;
-                               } elseif (strpos($tag, '@') === 0) {
-                                       if (strpos($tag, '[url=')) {
-                                               continue;
-                                       }
+                               // don't link tags that are already embedded in links
+                               if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
+                                       continue;
+                               }
+                               if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
+                                       continue;
+                               }
 
-                                       $basetag = substr($tag, 1);
-                                       $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
-                                       $body = str_replace($tag, $url, $body);
-                                       $tags_arr["@" . $basetag] = $url;
+                               $basetag = str_replace('_', ' ', substr($tag, 1));
+                               $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
+                               $body = str_replace($tag, $url, $body);
+                               $tags_arr["#" . $basetag] = $url;
+                       } elseif (strpos($tag, '@') === 0) {
+                               if (strpos($tag, '[url=')) {
+                                       continue;
                                }
+
+                               $basetag = substr($tag, 1);
+                               $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
+                               $body = str_replace($tag, $url, $body);
+                               $tags_arr["@" . $basetag] = $url;
                        }
                }
-
-               $tags = implode($tags_arr, ",");
        }
+
+       $tags = implode($tags_arr, ",");
+
        return ["body" => $body, "tags" => $tags, "plain" => $plain];
 }
 
@@ -1302,7 +1299,7 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur
  * @param object $post Twitter object with the post
  * @param array $postarray Array of the item that is about to be posted
  *
- * @return $picture string Returns a a single picture string if it isn't a media post
+ * @return $picture string Image URL or empty string
  */
 function twitter_media_entities($post, &$postarray)
 {
@@ -1480,7 +1477,7 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
        // Search for media links
        $picture = twitter_media_entities($post, $postarray);
 
-       $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $picture);
+       $converted = twitter_expand_entities($a, $postarray['body'], $post, $picture);
        $postarray['body'] = $converted["body"];
        $postarray['tag'] = $converted["tags"];
        $postarray['created'] = DateTimeFormat::utc($post->created_at);