3 * Name: Twitter Connector
4 * Description: Bidirectional (posting, relaying and reading) connector for Twitter.
6 * Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8 * Maintainer: Hypolite Petovan <https://friendica.mrpetovan.com/profile/hypolite>
10 * Copyright (c) 2011-2013 Tobias Diekershoff, Michael Vogel, Hypolite Petovan
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 * * Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * * Redistributions in binary form must reproduce the above
18 * * copyright notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the distribution.
20 * * Neither the name of the <organization> nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 /* Twitter Addon for Friendica
38 * Author: Tobias Diekershoff
39 * tobias.diekershoff@gmx.net
41 * License:3-clause BSD license
44 * To use this addon you need a OAuth Consumer key pair (key & secret)
45 * you can get it from Twitter at https://twitter.com/apps
47 * Register your Friendica site as "Client" application with "Read & Write" access
48 * we do not need "Twitter as login". When you've registered the app you get the
49 * OAuth Consumer key and secret pair for your application/site.
51 * Add this key pair to your global config/addon.config.php or use the admin panel.
54 * 'consumerkey' => '',
55 * 'consumersecret' => '',
58 * To activate the addon itself add it to the system.addon
59 * setting. After this, your user can configure their Twitter account settings
60 * from "Settings -> Addon Settings".
62 * Requirements: PHP5, curl
65 use Abraham\TwitterOAuth\TwitterOAuth;
66 use Abraham\TwitterOAuth\TwitterOAuthException;
67 use Codebird\Codebird;
69 use Friendica\Content\Text\BBCode;
70 use Friendica\Content\Text\Plaintext;
71 use Friendica\Core\Hook;
72 use Friendica\Core\Logger;
73 use Friendica\Core\Protocol;
74 use Friendica\Core\Renderer;
75 use Friendica\Core\Worker;
76 use Friendica\Database\DBA;
78 use Friendica\Model\Contact;
79 use Friendica\Model\Conversation;
80 use Friendica\Model\Group;
81 use Friendica\Model\Item;
82 use Friendica\Model\ItemURI;
83 use Friendica\Model\Post;
84 use Friendica\Model\Tag;
85 use Friendica\Model\User;
86 use Friendica\Protocol\Activity;
87 use Friendica\Core\Config\Util\ConfigFileLoader;
88 use Friendica\Core\System;
89 use Friendica\Util\DateTimeFormat;
90 use Friendica\Util\Images;
91 use Friendica\Util\Strings;
93 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
95 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
97 function twitter_install()
99 // we need some hooks, for the configuration and for sending tweets
100 Hook::register('load_config' , __FILE__, 'twitter_load_config');
101 Hook::register('connector_settings' , __FILE__, 'twitter_settings');
102 Hook::register('connector_settings_post', __FILE__, 'twitter_settings_post');
103 Hook::register('hook_fork' , __FILE__, 'twitter_hook_fork');
104 Hook::register('post_local' , __FILE__, 'twitter_post_local');
105 Hook::register('notifier_normal' , __FILE__, 'twitter_post_hook');
106 Hook::register('jot_networks' , __FILE__, 'twitter_jot_nets');
107 Hook::register('cron' , __FILE__, 'twitter_cron');
108 Hook::register('support_follow' , __FILE__, 'twitter_support_follow');
109 Hook::register('follow' , __FILE__, 'twitter_follow');
110 Hook::register('unfollow' , __FILE__, 'twitter_unfollow');
111 Hook::register('block' , __FILE__, 'twitter_block');
112 Hook::register('unblock' , __FILE__, 'twitter_unblock');
113 Hook::register('expire' , __FILE__, 'twitter_expire');
114 Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
115 Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
116 Hook::register('probe_detect' , __FILE__, 'twitter_probe_detect');
117 Hook::register('item_by_link' , __FILE__, 'twitter_item_by_link');
118 Hook::register('parse_link' , __FILE__, 'twitter_parse_link');
119 Logger::info('installed twitter');
124 function twitter_load_config(App $a, ConfigFileLoader $loader)
126 $a->getConfigCache()->load($loader->loadAddonConfig('twitter'));
129 function twitter_check_item_notification(App $a, array &$notification_data)
131 $own_id = DI::pConfig()->get($notification_data['uid'], 'twitter', 'own_id');
133 $own_user = Contact::selectFirst(['url'], ['uid' => $notification_data['uid'], 'alias' => 'twitter::'.$own_id]);
135 $notification_data['profiles'][] = $own_user['url'];
139 function twitter_support_follow(App $a, array &$data)
141 if ($data['protocol'] == Protocol::TWITTER) {
142 $data['result'] = true;
146 function twitter_follow(App $a, array &$contact)
148 Logger::info('Check if contact is twitter contact', ['url' => $contact['url']]);
150 if (!strstr($contact['url'], '://twitter.com') && !strstr($contact['url'], '@twitter.com')) {
154 // contact seems to be a twitter contact, so continue
155 $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact['url']);
156 $nickname = str_replace('@twitter.com', '', $nickname);
158 $uid = $a->getLoggedInUserId();
160 if (!twitter_api_contact('friendships/create', ['network' => Protocol::TWITTER, 'nick' => $nickname], $uid)) {
165 $user = twitter_fetchuser($nickname);
167 $contact_id = twitter_fetch_contact($uid, $user, true);
169 $contact = Contact::getById($contact_id, ['name', 'nick', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'photo', 'priority', 'network', 'alias', 'pubkey']);
171 if (DBA::isResult($contact)) {
172 $contact['contact'] = $contact;
176 function twitter_unfollow(App $a, array &$hook_data)
178 $hook_data['result'] = twitter_api_contact('friendships/destroy', $hook_data['contact'], $hook_data['uid']);
181 function twitter_block(App $a, array &$hook_data)
183 $hook_data['result'] = twitter_api_contact('blocks/create', $hook_data['contact'], $hook_data['uid']);
185 if ($hook_data['result'] === true) {
186 $cdata = Contact::getPublicAndUserContactID($hook_data['contact']['id'], $hook_data['uid']);
187 Contact::remove($cdata['user']);
191 function twitter_unblock(App $a, array &$hook_data)
193 $hook_data['result'] = twitter_api_contact('blocks/destroy', $hook_data['contact'], $hook_data['uid']);
196 function twitter_api_contact(string $apiPath, array $contact, int $uid): ?bool
198 if ($contact['network'] !== Protocol::TWITTER) {
202 return twitter_api_call($uid, $apiPath, ['screen_name' => $contact['nick']]);
205 function twitter_jot_nets(App $a, array &$jotnets_fields)
211 if (DI::pConfig()->get(local_user(), 'twitter', 'post')) {
212 $jotnets_fields[] = [
213 'type' => 'checkbox',
216 DI::l10n()->t('Post to Twitter'),
217 DI::pConfig()->get(local_user(), 'twitter', 'post_by_default')
224 function twitter_settings_post(App $a)
229 // don't check twitter settings if twitter submit button is not clicked
230 if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
234 if (!empty($_POST['twitter-disconnect'])) {
236 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
237 * from the user configuration
239 DI::pConfig()->delete(local_user(), 'twitter', 'consumerkey');
240 DI::pConfig()->delete(local_user(), 'twitter', 'consumersecret');
241 DI::pConfig()->delete(local_user(), 'twitter', 'oauthtoken');
242 DI::pConfig()->delete(local_user(), 'twitter', 'oauthsecret');
243 DI::pConfig()->delete(local_user(), 'twitter', 'post');
244 DI::pConfig()->delete(local_user(), 'twitter', 'post_by_default');
245 DI::pConfig()->delete(local_user(), 'twitter', 'lastid');
246 DI::pConfig()->delete(local_user(), 'twitter', 'mirror_posts');
247 DI::pConfig()->delete(local_user(), 'twitter', 'import');
248 DI::pConfig()->delete(local_user(), 'twitter', 'create_user');
249 DI::pConfig()->delete(local_user(), 'twitter', 'own_id');
251 if (isset($_POST['twitter-pin'])) {
252 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
253 Logger::notice('got a Twitter PIN');
254 $ckey = DI::config()->get('twitter', 'consumerkey');
255 $csecret = DI::config()->get('twitter', 'consumersecret');
256 // the token and secret for which the PIN was generated were hidden in the settings
257 // form as token and token2, we need a new connection to Twitter using these token
258 // and secret to request a Access Token with the PIN
260 if (empty($_POST['twitter-pin'])) {
261 throw new Exception(DI::l10n()->t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.'));
264 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
265 $token = $connection->oauth('oauth/access_token', ['oauth_verifier' => $_POST['twitter-pin']]);
266 // ok, now that we have the Access Token, save them in the user config
267 DI::pConfig()->set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
268 DI::pConfig()->set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
269 DI::pConfig()->set(local_user(), 'twitter', 'post', 1);
270 } catch(Exception $e) {
271 notice($e->getMessage());
272 } catch(TwitterOAuthException $e) {
273 notice($e->getMessage());
276 // if no PIN is supplied in the POST variables, the user has changed the setting
277 // to post a tweet for every new __public__ posting to the wall
278 DI::pConfig()->set(local_user(), 'twitter', 'post', intval($_POST['twitter-enable']));
279 DI::pConfig()->set(local_user(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
280 DI::pConfig()->set(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
281 DI::pConfig()->set(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
282 DI::pConfig()->set(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
284 if (!intval($_POST['twitter-mirror'])) {
285 DI::pConfig()->delete(local_user(), 'twitter', 'lastid');
291 function twitter_settings(App $a, array &$data)
297 $user = User::getById(local_user());
299 DI::page()->registerStylesheet(__DIR__ . '/twitter.css', 'all');
302 * 1) Check that we have global consumer key & secret
303 * 2) If no OAuthtoken & stuff is present, generate button to get some
304 * 3) Checkbox for "Send public notices (280 chars only)
306 $ckey = DI::config()->get('twitter', 'consumerkey');
307 $csecret = DI::config()->get('twitter', 'consumersecret');
308 $otoken = DI::pConfig()->get(local_user(), 'twitter', 'oauthtoken');
309 $osecret = DI::pConfig()->get(local_user(), 'twitter', 'oauthsecret');
311 $enabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'post'));
312 $defenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'post_by_default'));
313 $mirrorenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'mirror_posts'));
314 $importenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'import'));
315 $create_userenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'create_user'));
317 // Hide the submit button by default
320 if ((!$ckey) && (!$csecret)) {
321 /* no global consumer keys
322 * display warning and skip personal config
324 $html = '<p>' . DI::l10n()->t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
326 // ok we have a consumer key pair now look into the OAuth stuff
327 if ((!$otoken) && (!$osecret)) {
328 /* the user has not yet connected the account to twitter...
329 * get a temporary OAuth key/secret pair and display a button with
330 * which the user can request a PIN to connect the account to a
331 * account at Twitter.
333 $connection = new TwitterOAuth($ckey, $csecret);
335 $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
337 $html = '<p>' . DI::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>';
338 $html .= '<a href="' . $connection->url('oauth/authorize', ['oauth_token' => $result['oauth_token']]) . '" target="_twitter"><img src="addon/twitter/lighter.png" alt="' . DI::l10n()->t('Log in with Twitter') . '"></a>';
339 $html .= '<div id="twitter-pin-wrapper">';
340 $html .= '<label id="twitter-pin-label" for="twitter-pin">' . DI::l10n()->t('Copy the PIN from Twitter here') . '</label>';
341 $html .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
342 $html .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result['oauth_token'] . '" />';
343 $html .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result['oauth_token_secret'] . '" />';
347 } catch (TwitterOAuthException $e) {
348 $html = '<p>' . DI::l10n()->t('An error occured: ') . $e->getMessage() . '</p>';
352 * we have an OAuth key / secret pair for the user
353 * so let's give a chance to disable the postings to Twitter
355 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
357 $account = $connection->get('account/verify_credentials');
358 if (property_exists($account, 'screen_name') &&
359 property_exists($account, 'description') &&
360 property_exists($account, 'profile_image_url')
362 $connected = DI::l10n()->t('Currently connected to: <a href="https://twitter.com/%1$s" target="_twitter">%1$s</a>', $account->screen_name);
364 Logger::notice('Invalid twitter info (verify credentials).', ['auth' => TwitterOAuth::class]);
367 if ($user['hidewall']) {
368 $privacy_warning = DI::l10n()->t('<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to Twitter will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.');
371 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/twitter/');
372 $html = Renderer::replaceMacros($t, [
374 'connected' => $connected ?? '',
375 'invalid' => DI::l10n()->t('Invalid Twitter info'),
376 'disconnect' => DI::l10n()->t('Disconnect'),
377 'privacy_warning' => $privacy_warning ?? '',
380 '$account' => $account,
381 '$enable' => ['twitter-enable', DI::l10n()->t('Allow posting to Twitter'), $enabled, DI::l10n()->t('If enabled all your <strong>public</strong> postings can be posted to the associated Twitter account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.')],
382 '$default' => ['twitter-default', DI::l10n()->t('Send public postings to Twitter by default'), $defenabled],
383 '$mirror' => ['twitter-mirror', DI::l10n()->t('Mirror all posts from twitter that are no replies'), $mirrorenabled],
384 '$import' => ['twitter-import', DI::l10n()->t('Import the remote timeline'), $importenabled],
385 '$create_user' => ['twitter-create_user', DI::l10n()->t('Automatically create contacts'), $create_userenabled, DI::l10n()->t('This will automatically create a contact in Friendica as soon as you receive a message from an existing contact via the Twitter network. If you do not enable this, you need to manually add those Twitter contacts in Friendica from whom you would like to see posts here.')],
388 // Enable the default submit button
390 } catch (TwitterOAuthException $e) {
391 $html = '<p>' . DI::l10n()->t('An error occured: ') . $e->getMessage() . '</p>';
397 'connector' => 'twitter',
398 'title' => DI::l10n()->t('Twitter Import/Export/Mirror'),
399 'enabled' => $enabled,
400 'image' => 'images/twitter.png',
402 'submit' => $submit ?? null,
406 function twitter_hook_fork(App $a, array &$b)
408 DI::logger()->debug('twitter_hook_fork', $b);
410 if ($b['name'] != 'notifier_normal') {
416 // Deletion checks are done in twitter_delete_item()
417 if ($post['deleted']) {
421 // Editing is not supported by the addon
422 if ($post['created'] !== $post['edited']) {
423 DI::logger()->info('Editing is not supported by the addon');
424 $b['execute'] = false;
428 // if post comes from twitter don't send it back
429 if (($post['extid'] == Protocol::TWITTER) || twitter_get_id($post['extid'])) {
430 DI::logger()->info('If post comes from twitter don\'t send it back');
431 $b['execute'] = false;
435 if (substr($post['app'], 0, 7) == 'Twitter') {
436 DI::logger()->info('No Twitter app');
437 $b['execute'] = false;
441 if (DI::pConfig()->get($post['uid'], 'twitter', 'import')) {
442 // Don't fork if it isn't a reply to a twitter post
443 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
444 Logger::notice('No twitter parent found', ['item' => $post['id']]);
445 $b['execute'] = false;
449 // Comments are never exported when we don't import the twitter timeline
450 if (!strstr($post['postopts'], 'twitter') || ($post['parent'] != $post['id']) || $post['private']) {
451 DI::logger()->info('Comments are never exported when we don\'t import the twitter timeline');
452 $b['execute'] = false;
458 function twitter_post_local(App $a, array &$b)
464 if (!local_user() || (local_user() != $b['uid'])) {
468 $twitter_post = intval(DI::pConfig()->get(local_user(), 'twitter', 'post'));
469 $twitter_enable = (($twitter_post && !empty($_REQUEST['twitter_enable'])) ? intval($_REQUEST['twitter_enable']) : 0);
471 // if API is used, default to the chosen settings
472 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'twitter', 'post_by_default'))) {
476 if (!$twitter_enable) {
480 if (strlen($b['postopts'])) {
481 $b['postopts'] .= ',';
484 $b['postopts'] .= 'twitter';
487 function twitter_probe_detect(App $a, array &$hookData)
489 // Don't overwrite an existing result
490 if (isset($hookData['result'])) {
494 // Avoid a lookup for the wrong network
495 if (!in_array($hookData['network'], ['', Protocol::TWITTER])) {
499 if (preg_match('=([^@]+)@(?:mobile\.)?twitter\.com$=i', $hookData['uri'], $matches)) {
501 } elseif (preg_match('=^https?://(?:mobile\.)?twitter\.com/(.+)=i', $hookData['uri'], $matches)) {
502 if (strpos($matches[1], '/') !== false) {
503 // Status case: https://twitter.com/<nick>/status/<status id>
505 $hookData['result'] = false;
514 $user = twitter_fetchuser($nick);
517 $hookData['result'] = twitter_user_to_contact($user);
521 function twitter_item_by_link(App $a, array &$hookData)
523 // Don't overwrite an existing result
524 if (isset($hookData['item_id'])) {
529 if (!preg_match('#^https?://(?:mobile\.|www\.)?twitter.com/[^/]+/status/(\d+).*#', $hookData['uri'], $matches)) {
533 // From now on, any early return should abort the whole chain since we've established it was a Twitter URL
534 $hookData['item_id'] = false;
536 // Node-level configuration check
537 if (empty(DI::config()->get('twitter', 'consumerkey')) || empty(DI::config()->get('twitter', 'consumersecret'))) {
541 // No anonymous import
542 if (!$hookData['uid']) {
547 empty(DI::pConfig()->get($hookData['uid'], 'twitter', 'oauthtoken'))
548 || empty(DI::pConfig()->get($hookData['uid'], 'twitter', 'oauthsecret'))
550 notice(DI::l10n()->t('Please connect a Twitter account in your Social Network settings to import Twitter posts.'));
554 $status = twitter_statuses_show($matches[1]);
556 if (empty($status->id_str)) {
557 notice(DI::l10n()->t('Twitter post not found.'));
561 $item = twitter_createpost($a, $hookData['uid'], $status, [], true, false, false);
563 $hookData['item_id'] = Item::insert($item);
567 function twitter_api_post(string $apiPath, string $pid, int $uid): ?bool
573 return twitter_api_call($uid, $apiPath, ['id' => $pid]);
576 function twitter_api_call(int $uid, string $apiPath, array $parameters = []): ?bool
578 $ckey = DI::config()->get('twitter', 'consumerkey');
579 $csecret = DI::config()->get('twitter', 'consumersecret');
580 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
581 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
583 // If the addon is not configured (general or for this user) quit here
584 if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
589 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
590 $result = $connection->post($apiPath, $parameters);
592 if ($connection->getLastHttpCode() != 200) {
593 throw new Exception($result->errors[0]->message ?? json_encode($result), $connection->getLastHttpCode());
596 if (!empty($result->errors)) {
597 throw new Exception($result->errors[0]->message, $result->errors[0]->code);
600 Logger::info('[twitter] API call successful', ['apiPath' => $apiPath, 'parameters' => $parameters]);
601 Logger::debug('[twitter] API call result', ['apiPath' => $apiPath, 'parameters' => $parameters, 'result' => $result]);
604 } catch (TwitterOAuthException $twitterOAuthException) {
605 Logger::warning('Unable to communicate with twitter', ['apiPath' => $apiPath, 'parameters' => $parameters, 'code' => $twitterOAuthException->getCode(), 'exception' => $twitterOAuthException]);
607 } catch (Exception $e) {
608 Logger::notice('[twitter] API call failed', ['apiPath' => $apiPath, 'parameters' => $parameters, 'code' => $e->getCode(), 'message' => $e->getMessage()]);
613 function twitter_get_id(string $uri)
615 if ((substr($uri, 0, 9) != 'twitter::') || (strlen($uri) <= 9)) {
619 $id = substr($uri, 9);
620 if (!is_numeric($id)) {
627 function twitter_post_hook(App $a, array &$b)
629 DI::logger()->info('twitter_post_hook', $b);
632 twitter_delete_item($b);
637 if (!DI::pConfig()->get($b['uid'], 'twitter', 'import')
638 && ($b['private'] || ($b['created'] !== $b['edited']))) {
642 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
646 if ($b['parent'] != $b['id']) {
647 Logger::debug('Got comment', ['item' => $b]);
649 // Looking if its a reply to a twitter post
650 if (!twitter_get_id($b['parent-uri']) &&
651 !twitter_get_id($b['extid']) &&
652 !twitter_get_id($b['thr-parent'])) {
653 Logger::info('No twitter post', ['parent' => $b['parent']]);
657 $condition = ['uri' => $b['thr-parent'], 'uid' => $b['uid']];
658 $thr_parent = Post::selectFirst(['uri', 'extid', 'author-link', 'author-nick', 'author-network'], $condition);
659 if (!DBA::isResult($thr_parent)) {
660 Logger::warning('No parent found', ['thr-parent' => $b['thr-parent']]);
664 if ($thr_parent['author-network'] == Protocol::TWITTER) {
665 $nickname = '@[url=' . $thr_parent['author-link'] . ']' . $thr_parent['author-nick'] . '[/url]';
666 $nicknameplain = '@' . $thr_parent['author-nick'];
668 Logger::info('Comparing', ['nickname' => $nickname, 'nicknameplain' => $nicknameplain, 'body' => $b['body']]);
669 if ((strpos($b['body'], $nickname) === false) && (strpos($b['body'], $nicknameplain) === false)) {
670 $b['body'] = $nickname . ' ' . $b['body'];
674 Logger::debug('Parent found', ['parent' => $thr_parent]);
676 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
680 // Dont't post if the post doesn't belong to us.
681 // This is a check for forum postings
682 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
683 if ($b['contact-id'] != $self['id']) {
688 if ($b['verb'] == Activity::LIKE) {
689 Logger::info('Like', ['uid' => $b['uid'], 'id' => twitter_get_id($b['thr-parent'])]);
691 twitter_api_post('favorites/create', twitter_get_id($b['thr-parent']), $b['uid']);
696 if ($b['verb'] == Activity::ANNOUNCE) {
697 Logger::info('Retweet', ['uid' => $b['uid'], 'id' => twitter_get_id($b['thr-parent'])]);
698 twitter_retweet($b['uid'], twitter_get_id($b['thr-parent']));
702 if ($b['created'] !== $b['edited']) {
706 // if post comes from twitter don't send it back
707 if (($b['extid'] == Protocol::TWITTER) || twitter_get_id($b['extid'])) {
711 if ($b['app'] == 'Twitter') {
715 Logger::notice('twitter post invoked', ['id' => $b['id'], 'guid' => $b['guid']]);
717 DI::pConfig()->load($b['uid'], 'twitter');
719 $ckey = DI::config()->get('twitter', 'consumerkey');
720 $csecret = DI::config()->get('twitter', 'consumersecret');
721 $otoken = DI::pConfig()->get($b['uid'], 'twitter', 'oauthtoken');
722 $osecret = DI::pConfig()->get($b['uid'], 'twitter', 'oauthsecret');
724 if ($ckey && $csecret && $otoken && $osecret) {
725 Logger::info('We have customer key and oauth stuff, going to send.');
727 // If it's a repeated message from twitter then do a native retweet and exit
728 if (twitter_is_retweet($a, $b['uid'], $b['body'])) {
732 Codebird::setConsumerKey($ckey, $csecret);
733 $cb = Codebird::getInstance();
734 $cb->setToken($otoken, $osecret);
736 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
738 // Set the timeout for upload to 30 seconds
739 $connection->setTimeouts(10, 30);
743 // Handling non-native reshares
744 $b['body'] = Friendica\Content\Text\BBCode::convertShare(
746 function (array $attributes, array $author_contact, $content, $is_quote_share) {
747 return twitter_convert_share($attributes, $author_contact, $content, $is_quote_share);
751 $b['body'] = twitter_update_mentions($b['body']);
753 $msgarr = Plaintext::getPost($b, $max_char, true, BBCode::TWITTER);
754 Logger::info('Got plaintext', ['id' => $b['id'], 'message' => $msgarr]);
755 $msg = $msgarr['text'];
757 if (($msg == '') && isset($msgarr['title'])) {
758 $msg = Plaintext::shorten($msgarr['title'], $max_char - 50, $b['uid']);
761 // Add the link to the body if the type isn't a photo or there are more than 4 images in the post
762 if (!empty($msgarr['url']) && (strpos($msg, $msgarr['url']) === false) && (($msgarr['type'] != 'photo') || empty($msgarr['images']) || (count($msgarr['images']) > 4))) {
763 $msg .= "\n" . $msgarr['url'];
767 Logger::notice('Empty message', ['id' => $b['id']]);
771 // and now tweet it :-)
774 if (!empty($msgarr['images'])) {
775 Logger::info('Got images', ['id' => $b['id'], 'images' => $msgarr['images']]);
778 foreach ($msgarr['images'] as $image) {
779 if (count($media_ids) == 4) {
783 $img_str = DI::httpClient()->fetch($image['url']);
785 $tempfile = tempnam(System::getTempPath(), 'cache');
786 file_put_contents($tempfile, $img_str);
788 Logger::info('Uploading', ['id' => $b['id'], 'image' => $image['url']]);
789 $media = $connection->upload('media/upload', ['media' => $tempfile]);
793 if (isset($media->media_id_string)) {
794 $media_ids[] = $media->media_id_string;
796 if (!empty($image['description'])) {
797 $data = ['media_id' => $media->media_id_string,
798 'alt_text' => ['text' => substr($image['description'], 0, 420)]];
799 $ret = $cb->media_metadata_create($data);
800 Logger::info('Metadata create', ['id' => $b['id'], 'data' => $data, 'return' => $ret]);
803 Logger::error('Failed upload', ['id' => $b['id'], 'image' => $image['url'], 'return' => $media]);
804 throw new Exception('Failed upload of ' . $image['url']);
807 $post['media_ids'] = implode(',', $media_ids);
808 if (empty($post['media_ids'])) {
809 unset($post['media_ids']);
811 } catch (Exception $e) {
812 Logger::warning('Exception when trying to send to Twitter', ['id' => $b['id'], 'message' => $e->getMessage()]);
816 $post['status'] = $msg;
819 $post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
822 $result = $connection->post('statuses/update', $post);
823 Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
825 if (!empty($result->source)) {
826 DI::config()->set('twitter', 'application_name', strip_tags($result->source));
829 if (!empty($result->errors)) {
830 Logger::error('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
832 } elseif ($thr_parent) {
833 Logger::notice('Post send, updating extid', ['id' => $b['id'], 'extid' => $result->id_str]);
834 Item::update(['extid' => 'twitter::' . $result->id_str], ['id' => $b['id']]);
839 function twitter_delete_item(array $item)
841 if (!$item['deleted']) {
845 if ($item['parent'] != $item['id']) {
846 Logger::debug('Deleting comment/announce', ['item' => $item]);
848 // Looking if it's a reply to a twitter post
849 if (!twitter_get_id($item['parent-uri']) &&
850 !twitter_get_id($item['extid']) &&
851 !twitter_get_id($item['thr-parent'])) {
852 Logger::info('No twitter post', ['parent' => $item['parent']]);
856 $condition = ['uri' => $item['thr-parent'], 'uid' => $item['uid']];
857 $thr_parent = Post::selectFirst(['uri', 'extid', 'author-link', 'author-nick', 'author-network'], $condition);
858 if (!DBA::isResult($thr_parent)) {
859 Logger::warning('No parent found', ['thr-parent' => $item['thr-parent']]);
863 Logger::debug('Parent found', ['parent' => $thr_parent]);
865 if (!strstr($item['extid'], 'twitter')) {
866 DI::logger()->info('Not a Twitter post', ['extid' => $item['extid']]);
870 // Don't delete if the post doesn't belong to us.
871 // This is a check for forum postings
872 $self = DBA::selectFirst('contact', ['id'], ['uid' => $item['uid'], 'self' => true]);
873 if ($item['contact-id'] != $self['id']) {
874 DI::logger()->info('Don\'t delete if the post doesn\'t belong to the user', ['contact-id' => $item['contact-id'], 'self' => $self['id']]);
880 * @TODO Remaining caveat: Comments posted on Twitter and imported in Friendica do not trigger any Notifier task,
881 * possibly because they are private to the user and don't require any remote deletion notifications sent.
882 * Comments posted on Friendica and mirrored on Twitter trigger the Notifier task and the Twitter counter-part
883 * will be deleted accordingly.
885 if ($item['verb'] == Activity::POST) {
886 Logger::info('Delete post/comment', ['uid' => $item['uid'], 'id' => twitter_get_id($item['extid'])]);
887 twitter_api_post('statuses/destroy', twitter_get_id($item['extid']), $item['uid']);
891 if ($item['verb'] == Activity::LIKE) {
892 Logger::info('Unlike', ['uid' => $item['uid'], 'id' => twitter_get_id($item['thr-parent'])]);
893 twitter_api_post('favorites/destroy', twitter_get_id($item['thr-parent']), $item['uid']);
897 if ($item['verb'] == Activity::ANNOUNCE && !empty($thr_parent['uri'])) {
898 Logger::info('Unretweet', ['uid' => $item['uid'], 'extid' => $thr_parent['uri'], 'id' => twitter_get_id($thr_parent['uri'])]);
899 twitter_api_post('statuses/unretweet', twitter_get_id($thr_parent['uri']), $item['uid']);
904 function twitter_addon_admin_post(App $a)
906 DI::config()->set('twitter', 'consumerkey', trim($_POST['consumerkey'] ?? ''));
907 DI::config()->set('twitter', 'consumersecret', trim($_POST['consumersecret'] ?? ''));
910 function twitter_addon_admin(App $a, string &$o)
912 $t = Renderer::getMarkupTemplate('admin.tpl', 'addon/twitter/');
914 $o = Renderer::replaceMacros($t, [
915 '$submit' => DI::l10n()->t('Save Settings'),
916 // name, label, value, help, [extra values]
917 '$consumerkey' => ['consumerkey', DI::l10n()->t('Consumer key'), DI::config()->get('twitter', 'consumerkey'), ''],
918 '$consumersecret' => ['consumersecret', DI::l10n()->t('Consumer secret'), DI::config()->get('twitter', 'consumersecret'), ''],
922 function twitter_cron(App $a)
924 $last = DI::config()->get('twitter', 'last_poll');
926 $poll_interval = intval(DI::config()->get('twitter', 'poll_interval'));
927 if (!$poll_interval) {
928 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
932 $next = $last + ($poll_interval * 60);
933 if ($next > time()) {
934 Logger::notice('twitter: poll intervall not reached');
938 Logger::notice('twitter: cron_start');
940 $pconfigs = DBA::selectToArray('pconfig', [], ['cat' => 'twitter', 'k' => 'mirror_posts', 'v' => true]);
941 foreach ($pconfigs as $rr) {
942 Logger::notice('Fetching', ['user' => $rr['uid']]);
943 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], 'addon/twitter/twitter_sync.php', 1, (int) $rr['uid']);
946 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
947 if ($abandon_days < 1) {
951 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
953 $pconfigs = DBA::selectToArray('pconfig', [], ['cat' => 'twitter', 'k' => 'import', 'v' => true]);
954 foreach ($pconfigs as $rr) {
955 if ($abandon_days != 0) {
956 if (!DBA::exists('user', ["`uid` = ? AND `login_date` >= ?", $rr['uid'], $abandon_limit])) {
957 Logger::notice('abandoned account: timeline from user will not be imported', ['user' => $rr['uid']]);
962 Logger::notice('importing timeline', ['user' => $rr['uid']]);
963 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], 'addon/twitter/twitter_sync.php', 2, (int) $rr['uid']);
966 // check for new contacts once a day
967 $last_contact_check = DI::pConfig()->get($rr['uid'],'pumpio','contact_check');
968 if($last_contact_check)
969 $next_contact_check = $last_contact_check + 86400;
971 $next_contact_check = 0;
973 if($next_contact_check <= time()) {
974 pumpio_getallusers($a, $rr["uid"]);
975 DI::pConfig()->set($rr['uid'],'pumpio','contact_check',time());
980 Logger::notice('twitter: cron_end');
982 DI::config()->set('twitter', 'last_poll', time());
985 function twitter_expire(App $a)
987 $days = DI::config()->get('twitter', 'expire');
993 Logger::notice('Start deleting expired posts');
995 $r = Post::select(['id', 'guid'], ['deleted' => true, 'network' => Protocol::TWITTER]);
996 while ($row = Post::fetch($r)) {
997 Logger::info('[twitter] Delete expired item', ['id' => $row['id'], 'guid' => $row['guid'], 'callstack' => \Friendica\Core\System::callstack()]);
998 Item::markForDeletionById($row['id']);
1002 Logger::notice('End deleting expired posts');
1004 Logger::notice('Start expiry');
1006 $pconfigs = DBA::selectToArray('pconfig', [], ['cat' => 'twitter', 'k' => 'import', 'v' => true]);
1007 foreach ($pconfigs as $rr) {
1008 Logger::notice('twitter_expire', ['user' => $rr['uid']]);
1009 Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
1012 Logger::notice('End expiry');
1015 function twitter_prepare_body(App $a, array &$b)
1017 if ($b['item']['network'] != Protocol::TWITTER) {
1021 if ($b['preview']) {
1024 $item['plink'] = DI::baseUrl()->get() . '/display/' . $item['guid'];
1026 $condition = ['uri' => $item['thr-parent'], 'uid' => local_user()];
1027 $orig_post = Post::selectFirst(['author-link'], $condition);
1028 if (DBA::isResult($orig_post)) {
1029 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post['author-link']);
1030 $nickname = '@[url=' . $orig_post['author-link'] . ']' . $nicknameplain . '[/url]';
1031 $nicknameplain = '@' . $nicknameplain;
1033 if ((strpos($item['body'], $nickname) === false) && (strpos($item['body'], $nicknameplain) === false)) {
1034 $item['body'] = $nickname . ' ' . $item['body'];
1038 $msgarr = Plaintext::getPost($item, $max_char, true, BBCode::TWITTER);
1039 $msg = $msgarr['text'];
1041 if (isset($msgarr['url']) && ($msgarr['type'] != 'photo')) {
1042 $msg .= ' ' . $msgarr['url'];
1045 if (isset($msgarr['image'])) {
1046 $msg .= ' ' . $msgarr['image'];
1049 $b['html'] = nl2br(htmlspecialchars($msg));
1053 function twitter_statuses_show(string $id, TwitterOAuth $twitterOAuth = null)
1055 if ($twitterOAuth === null) {
1056 $ckey = DI::config()->get('twitter', 'consumerkey');
1057 $csecret = DI::config()->get('twitter', 'consumersecret');
1059 if (empty($ckey) || empty($csecret)) {
1060 return new stdClass();
1063 $twitterOAuth = new TwitterOAuth($ckey, $csecret);
1066 $parameters = ['trim_user' => false, 'tweet_mode' => 'extended', 'id' => $id, 'include_ext_alt_text' => true];
1068 return $twitterOAuth->get('statuses/show', $parameters);
1072 * Parse Twitter status URLs since Twitter removed OEmbed
1075 * @param array $b Expected format:
1077 * 'url' => [URL to parse],
1078 * 'format' => 'json'|'',
1079 * 'text' => Output parameter
1081 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1083 function twitter_parse_link(App $a, array &$b)
1085 // Only handle Twitter status URLs
1086 if (!preg_match('#^https?://(?:mobile\.|www\.)?twitter.com/[^/]+/status/(\d+).*#', $b['url'], $matches)) {
1090 $status = twitter_statuses_show($matches[1]);
1092 if (empty($status->id)) {
1096 $item = twitter_createpost($a, 0, $status, [], true, false, true);
1101 if ($b['format'] == 'json') {
1103 foreach ($status->extended_entities->media ?? [] as $media) {
1104 if (!empty($media->media_url_https)) {
1106 'src' => $media->media_url_https,
1107 'width' => $media->sizes->thumb->w,
1108 'height' => $media->sizes->thumb->h,
1116 'url' => $item['plink'],
1117 'title' => DI::l10n()->t('%s on Twitter', $status->user->name),
1118 'text' => BBCode::toPlaintext($item['body'], false),
1119 'images' => $images,
1121 'contentType' => 'attachment',
1125 $b['text'] = BBCode::getShareOpeningTag(
1126 $item['author-name'],
1127 $item['author-link'],
1128 $item['author-avatar'],
1132 $b['text'] .= $item['body'] . '[/share]';
1137 /*********************
1141 *********************/
1145 * @brief Build the item array for the mirrored post
1147 * @param App $a Application class
1148 * @param integer $uid User id
1149 * @param object $post Twitter object with the post
1151 * @return array item data to be posted
1153 function twitter_do_mirrorpost(App $a, int $uid, $post)
1155 $datarray['uid'] = $uid;
1156 $datarray['extid'] = 'twitter::' . $post->id;
1157 $datarray['title'] = '';
1159 if (!empty($post->retweeted_status)) {
1160 // We don't support nested shares, so we mustn't show quotes as shares on retweets
1161 $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true, -1);
1167 $datarray['body'] = "\n" . BBCode::getShareOpeningTag(
1168 $item['author-name'],
1169 $item['author-link'],
1170 $item['author-avatar'],
1175 $datarray['body'] .= $item['body'] . '[/share]';
1177 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false, -1);
1183 $datarray['body'] = $item['body'];
1186 $datarray['app'] = $item['app'];
1187 $datarray['verb'] = $item['verb'];
1189 if (isset($item['location'])) {
1190 $datarray['location'] = $item['location'];
1193 if (isset($item['coord'])) {
1194 $datarray['coord'] = $item['coord'];
1201 * Fetches the Twitter user's own posts
1208 function twitter_fetchtimeline(App $a, int $uid): void
1210 $ckey = DI::config()->get('twitter', 'consumerkey');
1211 $csecret = DI::config()->get('twitter', 'consumersecret');
1212 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
1213 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
1214 $lastid = DI::pConfig()->get($uid, 'twitter', 'lastid');
1216 $application_name = DI::config()->get('twitter', 'application_name');
1218 if ($application_name == '') {
1219 $application_name = DI::baseUrl()->getHostname();
1222 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1224 // Ensure to have the own contact
1226 twitter_fetch_own_contact($a, $uid);
1227 } catch (TwitterOAuthException $e) {
1228 Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
1233 'exclude_replies' => true,
1234 'trim_user' => false,
1235 'contributor_details' => true,
1236 'include_rts' => true,
1237 'tweet_mode' => 'extended',
1238 'include_ext_alt_text' => true,
1241 $first_time = ($lastid == '');
1243 if ($lastid != '') {
1244 $parameters['since_id'] = $lastid;
1248 $items = $connection->get('statuses/user_timeline', $parameters);
1249 } catch (TwitterOAuthException $e) {
1250 Logger::warning('Error fetching timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
1254 if (!is_array($items)) {
1255 Logger::notice('No items', ['user' => $uid]);
1259 $posts = array_reverse($items);
1261 Logger::notice('Start processing posts', ['from' => $lastid, 'user' => $uid, 'count' => count($posts)]);
1263 if (count($posts)) {
1264 foreach ($posts as $post) {
1265 if ($post->id_str > $lastid) {
1266 $lastid = $post->id_str;
1267 DI::pConfig()->set($uid, 'twitter', 'lastid', $lastid);
1271 Logger::warning('First time, continue');
1275 if (stristr($post->source, $application_name)) {
1276 Logger::warning('Source is application name', ['source' => $post->source, 'application_name' => $application_name]);
1279 Logger::info('Preparing mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
1281 $mirrorpost = twitter_do_mirrorpost($a, $uid, $post);
1283 if (empty($mirrorpost['body'])) {
1284 Logger::warning('Body is empty', ['post' => $post, 'mirrorpost' => $mirrorpost]);
1288 Logger::info('Posting mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
1290 Post\Delayed::add($mirrorpost['extid'], $mirrorpost, PRIORITY_MEDIUM, Post\Delayed::UNPREPARED);
1293 DI::pConfig()->set($uid, 'twitter', 'lastid', $lastid);
1294 Logger::info('Last ID for user ' . $uid . ' is now ' . $lastid);
1297 function twitter_fix_avatar($avatar)
1299 $new_avatar = str_replace('_normal.', '_400x400.', $avatar);
1301 $info = Images::getInfoFromURLCached($new_avatar);
1303 $new_avatar = $avatar;
1309 function twitter_get_relation($uid, $target, $contact = [])
1311 if (isset($contact['rel'])) {
1312 $relation = $contact['rel'];
1317 $ckey = DI::config()->get('twitter', 'consumerkey');
1318 $csecret = DI::config()->get('twitter', 'consumersecret');
1319 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
1320 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
1321 $own_id = DI::pConfig()->get($uid, 'twitter', 'own_id');
1323 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1324 $parameters = ['source_id' => $own_id, 'target_screen_name' => $target];
1327 $status = $connection->get('friendships/show', $parameters);
1328 if ($connection->getLastHttpCode() !== 200) {
1329 throw new Exception($status->errors[0]->message ?? 'HTTP response code ' . $connection->getLastHttpCode(), $status->errors[0]->code ?? $connection->getLastHttpCode());
1332 $following = $status->relationship->source->following;
1333 $followed = $status->relationship->source->followed_by;
1335 if ($following && !$followed) {
1336 $relation = Contact::SHARING;
1337 } elseif (!$following && $followed) {
1338 $relation = Contact::FOLLOWER;
1339 } elseif ($following && $followed) {
1340 $relation = Contact::FRIEND;
1341 } elseif (!$following && !$followed) {
1345 Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]);
1346 } catch (Throwable $e) {
1347 Logger::warning('Error fetching friendship status', ['uid' => $uid, 'target' => $target, 'message' => $e->getMessage()]);
1357 function twitter_user_to_contact($data)
1359 if (empty($data->id_str)) {
1363 $baseurl = 'https://twitter.com';
1364 $url = $baseurl . '/' . $data->screen_name;
1365 $addr = $data->screen_name . '@twitter.com';
1369 'nurl' => Strings::normaliseLink($url),
1370 'uri-id' => ItemURI::getIdByURI($url),
1371 'network' => Protocol::TWITTER,
1372 'alias' => 'twitter::' . $data->id_str,
1373 'baseurl' => $baseurl,
1374 'name' => $data->name,
1375 'nick' => $data->screen_name,
1377 'location' => $data->location,
1378 'about' => $data->description,
1379 'photo' => twitter_fix_avatar($data->profile_image_url_https),
1380 'header' => $data->profile_banner_url ?? $data->profile_background_image_url_https,
1386 function twitter_get_contact($data, int $uid = 0)
1388 $contact = DBA::selectFirst('contact', ['id'], ['uid' => $uid, 'alias' => 'twitter::' . $data->id_str]);
1389 if (DBA::isResult($contact)) {
1390 return $contact['id'];
1392 return twitter_fetch_contact($uid, $data, false);
1396 function twitter_fetch_contact($uid, $data, $create_user)
1398 $fields = twitter_user_to_contact($data);
1400 if (empty($fields)) {
1404 // photo comes from twitter_user_to_contact but shouldn't be saved directly in the contact row
1405 $avatar = $fields['photo'];
1406 unset($fields['photo']);
1408 // Update the public contact
1409 $pcontact = DBA::selectFirst('contact', ['id'], ['uid' => 0, 'alias' => 'twitter::' . $data->id_str]);
1410 if (DBA::isResult($pcontact)) {
1411 $cid = $pcontact['id'];
1413 $cid = Contact::getIdForURL($fields['url'], 0, false, $fields);
1417 Contact::update($fields, ['id' => $cid]);
1418 Contact::updateAvatar($cid, $avatar);
1420 Logger::warning('No contact found', ['fields' => $fields]);
1423 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => 'twitter::' . $data->id_str]);
1424 if (!DBA::isResult($contact) && empty($cid)) {
1425 Logger::warning('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
1427 } elseif (!$create_user) {
1431 if (!DBA::isResult($contact)) {
1432 $relation = twitter_get_relation($uid, $data->screen_name);
1434 // create contact record
1435 $fields['uid'] = $uid;
1436 $fields['created'] = DateTimeFormat::utcNow();
1437 $fields['poll'] = 'twitter::' . $data->id_str;
1438 $fields['rel'] = $relation;
1439 $fields['priority'] = 1;
1440 $fields['writable'] = true;
1441 $fields['blocked'] = false;
1442 $fields['readonly'] = false;
1443 $fields['pending'] = false;
1445 if (!Contact::insert($fields)) {
1449 $contact_id = DBA::lastInsertId();
1451 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1453 if ($contact['readonly'] || $contact['blocked']) {
1454 Logger::notice('Contact is blocked or readonly.', ['nickname' => $contact['nick']]);
1458 $contact_id = $contact['id'];
1461 // Update the contact relation once per day
1462 if ($contact['updated'] < DateTimeFormat::utc('now -24 hours')) {
1463 $fields['rel'] = twitter_get_relation($uid, $data->screen_name, $contact);
1467 if ($contact['name'] != $data->name) {
1468 $fields['name-date'] = $fields['uri-date'] = DateTimeFormat::utcNow();
1472 if ($contact['nick'] != $data->screen_name) {
1473 $fields['uri-date'] = DateTimeFormat::utcNow();
1477 if (($contact['location'] != $data->location) || ($contact['about'] != $data->description)) {
1482 $fields['updated'] = DateTimeFormat::utcNow();
1483 Contact::update($fields, ['id' => $contact['id']]);
1484 Logger::info('Updated contact', ['id' => $contact['id'], 'nick' => $data->screen_name]);
1488 Contact::updateAvatar($contact_id, $avatar);
1494 * @param string $screen_name
1495 * @return stdClass|null
1498 function twitter_fetchuser($screen_name)
1500 $ckey = DI::config()->get('twitter', 'consumerkey');
1501 $csecret = DI::config()->get('twitter', 'consumersecret');
1504 // Fetching user data
1505 $connection = new TwitterOAuth($ckey, $csecret);
1506 $parameters = ['screen_name' => $screen_name];
1507 $user = $connection->get('users/show', $parameters);
1508 } catch (TwitterOAuthException $e) {
1509 Logger::warning('Error fetching user', ['user' => $screen_name, 'message' => $e->getMessage()]);
1513 if (!is_object($user)) {
1521 * Replaces Twitter entities with Friendica-friendly links.
1523 * The Twitter API gives indices for each entity, which allows for fine-grained replacement.
1525 * First, we need to collect everything that needs to be replaced, what we will replace it with, and the start index.
1526 * Then we sort the indices decreasingly, and we replace from the end of the body to the start in order for the next
1527 * index to be correct even after the last replacement.
1529 * @param string $body
1530 * @param stdClass $status
1532 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1534 function twitter_expand_entities($body, stdClass $status)
1537 $contains_urls = false;
1541 $replacementList = [];
1543 foreach ($status->entities->hashtags AS $hashtag) {
1544 $replace = '#[url=' . DI::baseUrl()->get() . '/search?tag=' . $hashtag->text . ']' . $hashtag->text . '[/url]';
1545 $taglist['#' . $hashtag->text] = ['#', $hashtag->text, ''];
1547 $replacementList[$hashtag->indices[0]] = [
1548 'replace' => $replace,
1549 'length' => $hashtag->indices[1] - $hashtag->indices[0],
1553 foreach ($status->entities->user_mentions AS $mention) {
1554 $replace = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
1555 $taglist['@' . $mention->screen_name] = ['@', $mention->screen_name, 'https://twitter.com/' . rawurlencode($mention->screen_name)];
1557 $replacementList[$mention->indices[0]] = [
1558 'replace' => $replace,
1559 'length' => $mention->indices[1] - $mention->indices[0],
1563 foreach ($status->entities->urls ?? [] as $url) {
1564 $plain = str_replace($url->url, '', $plain);
1566 if ($url->url && $url->expanded_url && $url->display_url) {
1567 // Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
1568 if (!empty($status->quoted_status) && isset($status->quoted_status_id_str)
1569 && substr($url->expanded_url, -strlen($status->quoted_status_id_str)) == $status->quoted_status_id_str
1571 $replacementList[$url->indices[0]] = [
1573 'length' => $url->indices[1] - $url->indices[0],
1578 $contains_urls = true;
1580 $expanded_url = $url->expanded_url;
1582 // Quickfix: Workaround for URL with '[' and ']' in it
1583 if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
1584 $expanded_url = $url->url;
1587 $replacementList[$url->indices[0]] = [
1588 'replace' => '[url=' . $expanded_url . ']' . $url->display_url . '[/url]',
1589 'length' => $url->indices[1] - $url->indices[0],
1594 krsort($replacementList);
1596 foreach ($replacementList as $startIndex => $parameters) {
1597 $body = Strings::substringReplace($body, $parameters['replace'], $startIndex, $parameters['length']);
1600 $body = trim($body);
1602 return ['body' => trim($body), 'plain' => trim($plain), 'taglist' => $taglist, 'urls' => $contains_urls];
1606 * Store entity attachments
1608 * @param integer $uriId
1609 * @param object $post Twitter object with the post
1611 function twitter_store_attachments(int $uriId, $post)
1613 if (!empty($post->extended_entities->media)) {
1614 foreach ($post->extended_entities->media AS $medium) {
1615 switch ($medium->type) {
1617 $attachment = ['uri-id' => $uriId, 'type' => Post\Media::IMAGE];
1619 $attachment['url'] = $medium->media_url_https . '?name=large';
1620 $attachment['width'] = $medium->sizes->large->w;
1621 $attachment['height'] = $medium->sizes->large->h;
1623 if ($medium->sizes->small->w != $attachment['width']) {
1624 $attachment['preview'] = $medium->media_url_https . '?name=small';
1625 $attachment['preview-width'] = $medium->sizes->small->w;
1626 $attachment['preview-height'] = $medium->sizes->small->h;
1629 $attachment['name'] = $medium->display_url ?? null;
1630 $attachment['description'] = $medium->ext_alt_text ?? null;
1631 Logger::debug('Photo attachment', ['attachment' => $attachment]);
1632 Post\Media::insert($attachment);
1635 case 'animated_gif':
1636 $attachment = ['uri-id' => $uriId, 'type' => Post\Media::VIDEO];
1637 if (is_array($medium->video_info->variants)) {
1639 // We take the video with the highest bitrate
1640 foreach ($medium->video_info->variants AS $variant) {
1641 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1642 $attachment['url'] = $variant->url;
1643 $bitrate = $variant->bitrate;
1648 $attachment['name'] = $medium->display_url ?? null;
1649 $attachment['preview'] = $medium->media_url_https . ':small';
1650 $attachment['preview-width'] = $medium->sizes->small->w;
1651 $attachment['preview-height'] = $medium->sizes->small->h;
1652 $attachment['description'] = $medium->ext_alt_text ?? null;
1653 Logger::debug('Video attachment', ['attachment' => $attachment]);
1654 Post\Media::insert($attachment);
1657 Logger::notice('Unknown media type', ['medium' => $medium]);
1662 if (!empty($post->entities->urls)) {
1663 foreach ($post->entities->urls as $url) {
1664 $attachment = ['uri-id' => $uriId, 'type' => Post\Media::UNKNOWN, 'url' => $url->expanded_url, 'name' => $url->display_url];
1665 Logger::debug('Attached link', ['attachment' => $attachment]);
1666 Post\Media::insert($attachment);
1672 * @brief Fetch media entities and add media links to the body
1674 * @param object $post Twitter object with the post
1675 * @param array $postarray Array of the item that is about to be posted
1676 * @param integer $uriId URI Id used to store tags. -1 = don't store tags for this post.
1678 function twitter_media_entities($post, array &$postarray, int $uriId = -1)
1680 // There are no media entities? So we quit.
1681 if (empty($post->extended_entities->media)) {
1685 // This is a pure media post, first search for all media urls
1687 foreach ($post->extended_entities->media AS $medium) {
1688 if (!isset($media[$medium->url])) {
1689 $media[$medium->url] = '';
1691 switch ($medium->type) {
1693 if (!empty($medium->ext_alt_text)) {
1694 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1695 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1697 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1700 $postarray['object-type'] = Activity\ObjectType::IMAGE;
1701 $postarray['post-type'] = Item::PT_IMAGE;
1704 // Currently deactivated, since this causes the video to be display before the content
1705 // We have to figure out a better way for declaring the post type and the display style.
1706 //$postarray['post-type'] = Item::PT_VIDEO;
1707 case 'animated_gif':
1708 if (!empty($medium->ext_alt_text)) {
1709 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1710 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1712 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1715 $postarray['object-type'] = Activity\ObjectType::VIDEO;
1716 if (is_array($medium->video_info->variants)) {
1718 // We take the video with the highest bitrate
1719 foreach ($medium->video_info->variants AS $variant) {
1720 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1721 $media[$medium->url] = "\n[video]" . $variant->url . '[/video]';
1722 $bitrate = $variant->bitrate;
1731 foreach ($media AS $key => $value) {
1732 $postarray['body'] = str_replace($key, '', $postarray['body']);
1737 // Now we replace the media urls.
1738 foreach ($media AS $key => $value) {
1739 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1744 * Undocumented function
1747 * @param integer $uid User ID
1748 * @param object $post Incoming Twitter post
1749 * @param array $self
1750 * @param bool $create_user Should users be created?
1751 * @param bool $only_existing_contact Only import existing contacts if set to "true"
1752 * @param bool $noquote
1753 * @param integer $uriId URI Id used to store tags. 0 = create a new one; -1 = don't store tags for this post.
1754 * @return array item array
1756 function twitter_createpost(App $a, int $uid, $post, array $self, $create_user, bool $only_existing_contact, bool $noquote, int $uriId = 0): array
1759 $postarray['network'] = Protocol::TWITTER;
1760 $postarray['uid'] = $uid;
1761 $postarray['wall'] = 0;
1762 $postarray['uri'] = 'twitter::' . $post->id_str;
1763 $postarray['protocol'] = Conversation::PARCEL_TWITTER;
1764 $postarray['source'] = json_encode($post);
1765 $postarray['direction'] = Conversation::PULL;
1767 if (empty($uriId)) {
1768 $uriId = $postarray['uri-id'] = ItemURI::insert(['uri' => $postarray['uri']]);
1771 // Don't import our own comments
1772 if (Post::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
1773 Logger::info('Item found', ['extid' => $postarray['uri']]);
1779 if ($post->in_reply_to_status_id_str != '') {
1780 $thr_parent = 'twitter::' . $post->in_reply_to_status_id_str;
1782 $item = Post::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
1783 if (!DBA::isResult($item)) {
1784 $item = Post::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]);
1787 if (DBA::isResult($item)) {
1788 $postarray['thr-parent'] = $item['uri'];
1789 $postarray['object-type'] = Activity\ObjectType::COMMENT;
1791 $postarray['object-type'] = Activity\ObjectType::NOTE;
1795 $own_id = DI::pConfig()->get($uid, 'twitter', 'own_id');
1797 if ($post->user->id_str == $own_id) {
1798 $self = Contact::selectFirst(['id', 'name', 'url', 'photo'], ['self' => true, 'uid' => $uid]);
1799 if (DBA::isResult($self)) {
1800 $contactid = $self['id'];
1802 $postarray['owner-name'] = $self['name'];
1803 $postarray['owner-link'] = $self['url'];
1804 $postarray['owner-avatar'] = $self['photo'];
1806 Logger::error('No self contact found', ['uid' => $uid]);
1810 // Don't create accounts of people who just comment something
1811 $create_user = false;
1813 $postarray['object-type'] = Activity\ObjectType::NOTE;
1816 if ($contactid == 0) {
1817 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1819 $postarray['owner-id'] = twitter_get_contact($post->user);
1820 $postarray['owner-name'] = $post->user->name;
1821 $postarray['owner-link'] = 'https://twitter.com/' . $post->user->screen_name;
1822 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1825 if (($contactid == 0) && !$only_existing_contact) {
1826 $contactid = $self['id'];
1827 } elseif ($contactid <= 0) {
1828 Logger::info('Contact ID is zero or less than zero.');
1832 $postarray['contact-id'] = $contactid;
1833 $postarray['verb'] = Activity::POST;
1834 $postarray['author-id'] = $postarray['owner-id'];
1835 $postarray['author-name'] = $postarray['owner-name'];
1836 $postarray['author-link'] = $postarray['owner-link'];
1837 $postarray['author-avatar'] = $postarray['owner-avatar'];
1838 $postarray['plink'] = 'https://twitter.com/' . $post->user->screen_name . '/status/' . $post->id_str;
1839 $postarray['app'] = strip_tags($post->source);
1841 if ($post->user->protected) {
1842 $postarray['private'] = Item::PRIVATE;
1843 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1845 $postarray['private'] = Item::UNLISTED;
1846 $postarray['allow_cid'] = '';
1849 if (!empty($post->full_text)) {
1850 $postarray['body'] = $post->full_text;
1852 $postarray['body'] = $post->text;
1855 // When the post contains links then use the correct object type
1856 if (count($post->entities->urls) > 0) {
1857 $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
1860 // Search for media links
1861 twitter_media_entities($post, $postarray, $uriId);
1863 $converted = twitter_expand_entities($postarray['body'], $post);
1865 // When the post contains external links then images or videos are just "decorations".
1866 if (!empty($converted['urls'])) {
1867 $postarray['post-type'] = Item::PT_NOTE;
1870 $postarray['body'] = $converted['body'];
1871 $postarray['created'] = DateTimeFormat::utc($post->created_at);
1872 $postarray['edited'] = DateTimeFormat::utc($post->created_at);
1875 twitter_store_tags($uriId, $converted['taglist']);
1876 twitter_store_attachments($uriId, $post);
1879 if (!empty($post->place->name)) {
1880 $postarray['location'] = $post->place->name;
1882 if (!empty($post->place->full_name)) {
1883 $postarray['location'] = $post->place->full_name;
1885 if (!empty($post->geo->coordinates)) {
1886 $postarray['coord'] = $post->geo->coordinates[0] . ' ' . $post->geo->coordinates[1];
1888 if (!empty($post->coordinates->coordinates)) {
1889 $postarray['coord'] = $post->coordinates->coordinates[1] . ' ' . $post->coordinates->coordinates[0];
1891 if (!empty($post->retweeted_status)) {
1892 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1894 if (empty($retweet)) {
1899 // Store the original tweet
1900 Item::insert($retweet);
1902 // CHange the other post into a reshare activity
1903 $postarray['verb'] = Activity::ANNOUNCE;
1904 $postarray['gravity'] = GRAVITY_ACTIVITY;
1905 $postarray['object-type'] = Activity\ObjectType::NOTE;
1907 $postarray['thr-parent'] = $retweet['uri'];
1909 $retweet['source'] = $postarray['source'];
1910 $retweet['direction'] = $postarray['direction'];
1911 $retweet['private'] = $postarray['private'];
1912 $retweet['allow_cid'] = $postarray['allow_cid'];
1913 $retweet['contact-id'] = $postarray['contact-id'];
1914 $retweet['owner-id'] = $postarray['owner-id'];
1915 $retweet['owner-name'] = $postarray['owner-name'];
1916 $retweet['owner-link'] = $postarray['owner-link'];
1917 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1919 $postarray = $retweet;
1923 if (!empty($post->quoted_status)) {
1925 // To avoid recursive share blocks we just provide the link to avoid removing quote context.
1926 $postarray['body'] .= "\n\nhttps://twitter.com/" . $post->quoted_status->user->screen_name . "/status/" . $post->quoted_status->id_str;
1928 $quoted = twitter_createpost($a, 0, $post->quoted_status, $self, false, false, true);
1929 if (!empty($quoted)) {
1930 Item::insert($quoted);
1931 $post = Post::selectFirst(['guid', 'uri-id'], ['uri' => $quoted['uri'], 'uid' => 0]);
1932 Logger::info('Stored quoted post', ['uid' => $uid, 'uri-id' => $uriId, 'post' => $post]);
1934 $postarray['body'] .= "\n" . BBCode::getShareOpeningTag(
1935 $quoted['author-name'],
1936 $quoted['author-link'],
1937 $quoted['author-avatar'],
1943 $postarray['body'] .= $quoted['body'] . '[/share]';
1945 // Quoted post author is blocked/ignored, so we just provide the link to avoid removing quote context.
1946 $postarray['body'] .= "\n\nhttps://twitter.com/" . $post->quoted_status->user->screen_name . '/status/' . $post->quoted_status->id_str;
1955 * Store tags and mentions
1957 * @param integer $uriId
1958 * @param array $taglist
1961 function twitter_store_tags(int $uriId, array $taglist)
1963 foreach ($taglist as $tag) {
1964 Tag::storeByHash($uriId, $tag[0], $tag[1], $tag[2]);
1968 function twitter_fetchparentposts(App $a, int $uid, $post, TwitterOAuth $connection, array $self)
1970 Logger::info('Fetching parent posts', ['user' => $uid, 'post' => $post->id_str]);
1974 while (!empty($post->in_reply_to_status_id_str)) {
1976 $post = twitter_statuses_show($post->in_reply_to_status_id_str, $connection);
1977 } catch (TwitterOAuthException $e) {
1978 Logger::warning('Error fetching parent post', ['uid' => $uid, 'post' => $post->id_str, 'message' => $e->getMessage()]);
1983 Logger::info("twitter_fetchparentposts: Can't fetch post");
1987 if (empty($post->id_str)) {
1988 Logger::info('twitter_fetchparentposts: This is not a post', ['post' => $post]);
1992 if (Post::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1999 Logger::info('twitter_fetchparentposts: Fetching ' . count($posts) . ' parents');
2001 $posts = array_reverse($posts);
2003 if (!empty($posts)) {
2004 foreach ($posts as $post) {
2005 $postarray = twitter_createpost($a, $uid, $post, $self, false, !DI::pConfig()->get($uid, 'twitter', 'create_user'), false);
2007 if (empty($postarray)) {
2011 $item = Item::insert($postarray);
2013 $postarray['id'] = $item;
2015 Logger::notice('twitter_fetchparentpost: User ' . $self['nick'] . ' posted parent timeline item ' . $item);
2021 * Fetches the posts received by the Twitter user
2028 function twitter_fetchhometimeline(App $a, int $uid): void
2030 $ckey = DI::config()->get('twitter', 'consumerkey');
2031 $csecret = DI::config()->get('twitter', 'consumersecret');
2032 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
2033 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
2034 $create_user = DI::pConfig()->get($uid, 'twitter', 'create_user');
2035 $mirror_posts = DI::pConfig()->get($uid, 'twitter', 'mirror_posts');
2037 Logger::info('Fetching timeline', ['uid' => $uid]);
2039 $application_name = DI::config()->get('twitter', 'application_name');
2041 if ($application_name == '') {
2042 $application_name = DI::baseUrl()->getHostname();
2045 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
2048 $own_contact = twitter_fetch_own_contact($a, $uid);
2049 } catch (TwitterOAuthException $e) {
2050 Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
2054 $contact = Contact::selectFirst(['nick'], ['id' => $own_contact, 'uid' => $uid]);
2055 if (DBA::isResult($contact)) {
2056 $own_id = $contact['nick'];
2058 Logger::warning('Own twitter contact not found', ['uid' => $uid]);
2062 $self = User::getOwnerDataById($uid);
2063 if ($self === false) {
2064 Logger::warning('Own contact not found', ['uid' => $uid]);
2069 'exclude_replies' => false,
2070 'trim_user' => false,
2071 'contributor_details' => true,
2072 'include_rts' => true,
2073 'tweet_mode' => 'extended',
2074 'include_ext_alt_text' => true,
2078 // Fetching timeline
2079 $lastid = DI::pConfig()->get($uid, 'twitter', 'lasthometimelineid');
2081 $first_time = ($lastid == '');
2083 if ($lastid != '') {
2084 $parameters['since_id'] = $lastid;
2088 $items = $connection->get('statuses/home_timeline', $parameters);
2089 } catch (TwitterOAuthException $e) {
2090 Logger::warning('Error fetching home timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
2094 if (!is_array($items)) {
2095 Logger::warning('home timeline is no array', ['items' => $items]);
2099 if (empty($items)) {
2100 Logger::notice('No new timeline content', ['uid' => $uid]);
2104 $posts = array_reverse($items);
2106 Logger::notice('Processing timeline', ['lastid' => $lastid, 'uid' => $uid, 'count' => count($posts)]);
2108 if (count($posts)) {
2109 foreach ($posts as $post) {
2110 if ($post->id_str > $lastid) {
2111 $lastid = $post->id_str;
2112 DI::pConfig()->set($uid, 'twitter', 'lasthometimelineid', $lastid);
2119 if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
2120 Logger::info('Skip previously sent post');
2124 if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == '') {
2125 Logger::info('Skip post that will be mirrored');
2129 if ($post->in_reply_to_status_id_str != '') {
2130 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
2133 Logger::info('Preparing post ' . $post->id_str . ' for user ' . $uid);
2135 $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
2137 if (empty($postarray)) {
2138 Logger::info('Empty post ' . $post->id_str . ' and user ' . $uid);
2144 if (empty($postarray['thr-parent'])) {
2145 $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
2146 if (DBA::isResult($contact) && Item::isRemoteSelf($contact, $postarray)) {
2147 $notify = PRIORITY_MEDIUM;
2151 $item = Item::insert($postarray, $notify);
2152 $postarray['id'] = $item;
2154 Logger::notice('User ' . $uid . ' posted home timeline item ' . $item);
2157 DI::pConfig()->set($uid, 'twitter', 'lasthometimelineid', $lastid);
2159 Logger::info('Last timeline ID for user ' . $uid . ' is now ' . $lastid);
2161 // Fetching mentions
2162 $lastid = DI::pConfig()->get($uid, 'twitter', 'lastmentionid');
2164 $first_time = ($lastid == '');
2166 if ($lastid != '') {
2167 $parameters['since_id'] = $lastid;
2171 $items = $connection->get('statuses/mentions_timeline', $parameters);
2172 } catch (TwitterOAuthException $e) {
2173 Logger::warning('Error fetching mentions', ['uid' => $uid, 'message' => $e->getMessage()]);
2177 if (!is_array($items)) {
2178 Logger::warning('mentions are no arrays', ['items' => $items]);
2182 $posts = array_reverse($items);
2184 Logger::info('Fetching mentions for user ' . $uid . ' ' . sizeof($posts) . ' items');
2186 if (count($posts)) {
2187 foreach ($posts as $post) {
2188 if ($post->id_str > $lastid) {
2189 $lastid = $post->id_str;
2196 if ($post->in_reply_to_status_id_str != '') {
2197 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
2200 $postarray = twitter_createpost($a, $uid, $post, $self, false, !$create_user, false);
2202 if (empty($postarray)) {
2206 $item = Item::insert($postarray);
2208 Logger::notice('User ' . $uid . ' posted mention timeline item ' . $item);
2212 DI::pConfig()->set($uid, 'twitter', 'lastmentionid', $lastid);
2214 Logger::info('Last mentions ID for user ' . $uid . ' is now ' . $lastid);
2217 function twitter_fetch_own_contact(App $a, int $uid)
2219 $ckey = DI::config()->get('twitter', 'consumerkey');
2220 $csecret = DI::config()->get('twitter', 'consumersecret');
2221 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
2222 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
2224 $own_id = DI::pConfig()->get($uid, 'twitter', 'own_id');
2228 if ($own_id == '') {
2229 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
2231 // Fetching user data
2232 // get() may throw TwitterOAuthException, but we will catch it later
2233 $user = $connection->get('account/verify_credentials');
2234 if (empty($user->id_str)) {
2238 DI::pConfig()->set($uid, 'twitter', 'own_id', $user->id_str);
2240 $contact_id = twitter_fetch_contact($uid, $user, true);
2242 $contact = Contact::selectFirst(['id'], ['uid' => $uid, 'alias' => 'twitter::' . $own_id]);
2243 if (DBA::isResult($contact)) {
2244 $contact_id = $contact['id'];
2246 DI::pConfig()->delete($uid, 'twitter', 'own_id');
2253 function twitter_is_retweet(App $a, int $uid, string $body): bool
2255 $body = trim($body);
2257 // Skip if it isn't a pure repeated messages
2258 // Does it start with a share?
2259 if (strpos($body, '[share') > 0) {
2263 // Does it end with a share?
2264 if (strlen($body) > (strrpos($body, '[/share]') + 8)) {
2268 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
2269 // Skip if there is no shared message in there
2270 if ($body == $attributes) {
2275 preg_match("/link='(.*?)'/ism", $attributes, $matches);
2276 if (!empty($matches[1])) {
2277 $link = $matches[1];
2280 preg_match('/link="(.*?)"/ism', $attributes, $matches);
2281 if (!empty($matches[1])) {
2282 $link = $matches[1];
2285 $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
2289 return twitter_retweet($uid, $id);
2292 function twitter_retweet(int $uid, int $id, int $item_id = 0): bool
2294 Logger::info('Retweeting', ['user' => $uid, 'id' => $id]);
2296 $result = twitter_api_post('statuses/retweet', $id, $uid);
2298 Logger::info('Retweeted', ['user' => $uid, 'id' => $id, 'result' => $result]);
2300 if (!empty($item_id) && !empty($result->id_str)) {
2301 Logger::notice('Update extid', ['id' => $item_id, 'extid' => $result->id_str]);
2302 Item::update(['extid' => 'twitter::' . $result->id_str], ['id' => $item_id]);
2305 return !isset($result->errors);
2308 function twitter_update_mentions(string $body): string
2310 $URLSearchString = '^\[\]';
2311 $return = preg_replace_callback(
2312 "/@\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
2313 function ($matches) {
2314 if (strpos($matches[1], 'twitter.com')) {
2315 $return = '@' . substr($matches[1], strrpos($matches[1], '/') + 1);
2317 $return = $matches[2] . ' (' . $matches[1] . ')';
2328 function twitter_convert_share(array $attributes, array $author_contact, string $content, bool $is_quote_share): string
2330 if (empty($author_contact)) {
2331 return $content . "\n\n" . $attributes['link'];
2334 if (!empty($author_contact['network']) && ($author_contact['network'] == Protocol::TWITTER)) {
2335 $mention = '@' . $author_contact['nick'];
2337 $mention = $author_contact['addr'];
2340 return ($is_quote_share ? "\n\n" : '' ) . 'RT ' . $mention . ': ' . $content . "\n\n" . $attributes['link'];