]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
[twitter] Fix TwitterOauth library usage
[friendica-addons.git] / twitter / twitter.php
index 8b483c9ea0ef91f8a6a29024e9fe4653c3bc396d..8265181271e9ba5af0b12eadd0f73df7f7ebd32b 100644 (file)
@@ -77,6 +77,7 @@ use Friendica\Model\Photo;
 use Friendica\Model\Queue;
 use Friendica\Model\User;
 use Friendica\Object\Image;
+use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
 
 require_once 'boot.php';
@@ -158,6 +159,12 @@ function twitter_follow(App $a, &$contact)
        $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
        $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
 
+       // If the addon is not configured (general or for this user) quit here
+       if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
+               $contact = false;
+               return;
+       }
+
        $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
        $connection->post('friendships/create', ['screen_name' => $nickname]);
 
@@ -281,34 +288,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);
-                       $request_token = $connection->url('oauth/request_token', ['oauth_callback' => 'oob']);
-                       /*                       * *
-                        *  make some nice form
-                        */
+                       $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
+
                        $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' => $request_token['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="' . $request_token['oauth_token'] . '" />';
-                       $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $request_token['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 {
@@ -564,11 +565,7 @@ function twitter_post_hook(App $a, &$b)
                                $image = "";
                        } elseif ($iscomment) {
                                logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
-                               q("UPDATE `item` SET `extid` = '%s', `body` = '%s' WHERE `id` = %d",
-                                       dbesc("twitter::" . $result->id_str),
-                                       dbesc($result->text),
-                                       intval($b['id'])
-                               );
+                               Item::update(['extid' => "twitter::" . $result->id_str, 'body' => $result->text], ['id' => $b['id']]);
                        }
                }
 
@@ -614,10 +611,7 @@ function twitter_post_hook(App $a, &$b)
                                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']);
-                               q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
-                                       dbesc("twitter::" . $result->id_str),
-                                       intval($b['id'])
-                               );
+                               Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
                        }
                }
        }
@@ -675,7 +669,7 @@ function twitter_cron(App $a, $b)
                $abandon_days = 0;
        }
 
-       $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400);
+       $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
 
        $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
        if (count($r)) {
@@ -1010,7 +1004,7 @@ function twitter_fetch_contact($uid, $contact, $create_user)
                                        `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(datetime_convert()),
+                       dbesc(DateTimeFormat::utcNow()),
                        dbesc("https://twitter.com/" . $contact->screen_name),
                        dbesc(normalise_link("https://twitter.com/" . $contact->screen_name)),
                        dbesc($contact->screen_name."@twitter.com"),
@@ -1054,16 +1048,16 @@ function twitter_fetch_contact($uid, $contact, $create_user)
                                dbesc($photos[0]),
                                dbesc($photos[1]),
                                dbesc($photos[2]),
-                               dbesc(datetime_convert()),
-                               dbesc(datetime_convert()),
-                               dbesc(datetime_convert()),
+                               dbesc(DateTimeFormat::utcNow()),
+                               dbesc(DateTimeFormat::utcNow()),
+                               dbesc(DateTimeFormat::utcNow()),
                                intval($contact_id)
                        );
                }
        } else {
                // update profile photos once every two weeks as we have no notification of when they change.
-               //$update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -2 days')) ? true : false);
-               $update_photo = ($r[0]['avatar-date'] < datetime_convert('', '', 'now -12 hours'));
+               //$update_photo = (($r[0]['avatar-date'] < DateTimeFormat::convert('now -2 days', '', '', )) ? true : false);
+               $update_photo = ($r[0]['avatar-date'] < DateTimeFormat::utc('now -12 hours'));
 
                // check that we have all the photos, this has been known to fail on occasion
                if ((!$r[0]['photo']) || (!$r[0]['thumb']) || (!$r[0]['micro']) || ($update_photo)) {
@@ -1089,9 +1083,9 @@ function twitter_fetch_contact($uid, $contact, $create_user)
                                        dbesc($photos[0]),
                                        dbesc($photos[1]),
                                        dbesc($photos[2]),
-                                       dbesc(datetime_convert()),
-                                       dbesc(datetime_convert()),
-                                       dbesc(datetime_convert()),
+                                       dbesc(DateTimeFormat::utcNow()),
+                                       dbesc(DateTimeFormat::utcNow()),
+                                       dbesc(DateTimeFormat::utcNow()),
                                        dbesc("https://twitter.com/".$contact->screen_name),
                                        dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
                                        dbesc($contact->screen_name."@twitter.com"),
@@ -1483,8 +1477,8 @@ function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_exis
        $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $picture);
        $postarray['body'] = $converted["body"];
        $postarray['tag'] = $converted["tags"];
-       $postarray['created'] = datetime_convert('UTC', 'UTC', $post->created_at);
-       $postarray['edited'] = datetime_convert('UTC', 'UTC', $post->created_at);
+       $postarray['created'] = DateTimeFormat::utc($post->created_at);
+       $postarray['edited'] = DateTimeFormat::utc($post->created_at);
 
        $statustext = $converted["plain"];