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\Util\ConfigFileLoader;
88 use Friendica\Util\DateTimeFormat;
89 use Friendica\Util\Images;
90 use Friendica\Util\Strings;
92 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
94 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
96 function twitter_install()
98 // we need some hooks, for the configuration and for sending tweets
99 Hook::register('load_config' , __FILE__, 'twitter_load_config');
100 Hook::register('connector_settings' , __FILE__, 'twitter_settings');
101 Hook::register('connector_settings_post', __FILE__, 'twitter_settings_post');
102 Hook::register('hook_fork' , __FILE__, 'twitter_hook_fork');
103 Hook::register('post_local' , __FILE__, 'twitter_post_local');
104 Hook::register('notifier_normal' , __FILE__, 'twitter_post_hook');
105 Hook::register('jot_networks' , __FILE__, 'twitter_jot_nets');
106 Hook::register('cron' , __FILE__, 'twitter_cron');
107 Hook::register('follow' , __FILE__, 'twitter_follow');
108 Hook::register('expire' , __FILE__, 'twitter_expire');
109 Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
110 Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
111 Hook::register('probe_detect' , __FILE__, 'twitter_probe_detect');
112 Hook::register('parse_link' , __FILE__, 'twitter_parse_link');
113 Logger::info("installed twitter");
118 function twitter_load_config(App $a, ConfigFileLoader $loader)
120 $a->getConfigCache()->load($loader->loadAddonConfig('twitter'));
123 function twitter_check_item_notification(App $a, array &$notification_data)
125 $own_id = DI::pConfig()->get($notification_data["uid"], 'twitter', 'own_id');
127 $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
128 intval($notification_data["uid"]),
129 DBA::escape("twitter::".$own_id)
133 $notification_data["profiles"][] = $own_user[0]["url"];
137 function twitter_follow(App $a, array &$contact)
139 Logger::info('Check if contact is twitter contact', ['url' => $contact["url"]]);
141 if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
145 // contact seems to be a twitter contact, so continue
146 $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
147 $nickname = str_replace("@twitter.com", "", $nickname);
149 $uid = $a->user["uid"];
151 $ckey = DI::config()->get('twitter', 'consumerkey');
152 $csecret = DI::config()->get('twitter', 'consumersecret');
153 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
154 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
156 // If the addon is not configured (general or for this user) quit here
157 if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
162 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
163 $connection->post('friendships/create', ['screen_name' => $nickname]);
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_jot_nets(App $a, array &$jotnets_fields)
182 if (DI::pConfig()->get(local_user(), 'twitter', 'post')) {
183 $jotnets_fields[] = [
184 'type' => 'checkbox',
187 DI::l10n()->t('Post to Twitter'),
188 DI::pConfig()->get(local_user(), 'twitter', 'post_by_default')
195 function twitter_settings_post(App $a)
200 // don't check twitter settings if twitter submit button is not clicked
201 if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
205 if (!empty($_POST['twitter-disconnect'])) {
207 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
208 * from the user configuration
210 DI::pConfig()->delete(local_user(), 'twitter', 'consumerkey');
211 DI::pConfig()->delete(local_user(), 'twitter', 'consumersecret');
212 DI::pConfig()->delete(local_user(), 'twitter', 'oauthtoken');
213 DI::pConfig()->delete(local_user(), 'twitter', 'oauthsecret');
214 DI::pConfig()->delete(local_user(), 'twitter', 'post');
215 DI::pConfig()->delete(local_user(), 'twitter', 'post_by_default');
216 DI::pConfig()->delete(local_user(), 'twitter', 'lastid');
217 DI::pConfig()->delete(local_user(), 'twitter', 'mirror_posts');
218 DI::pConfig()->delete(local_user(), 'twitter', 'import');
219 DI::pConfig()->delete(local_user(), 'twitter', 'create_user');
220 DI::pConfig()->delete(local_user(), 'twitter', 'own_id');
222 if (isset($_POST['twitter-pin'])) {
223 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
224 Logger::notice('got a Twitter PIN');
225 $ckey = DI::config()->get('twitter', 'consumerkey');
226 $csecret = DI::config()->get('twitter', 'consumersecret');
227 // the token and secret for which the PIN was generated were hidden in the settings
228 // form as token and token2, we need a new connection to Twitter using these token
229 // and secret to request a Access Token with the PIN
231 if (empty($_POST['twitter-pin'])) {
232 throw new Exception(DI::l10n()->t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.'));
235 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
236 $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]);
237 // ok, now that we have the Access Token, save them in the user config
238 DI::pConfig()->set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
239 DI::pConfig()->set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
240 DI::pConfig()->set(local_user(), 'twitter', 'post', 1);
241 } catch(Exception $e) {
242 notice($e->getMessage());
243 } catch(TwitterOAuthException $e) {
244 notice($e->getMessage());
246 // reload the Addon Settings page, if we don't do it see Bug #42
247 DI::baseUrl()->redirect('settings/connectors');
249 // if no PIN is supplied in the POST variables, the user has changed the setting
250 // to post a tweet for every new __public__ posting to the wall
251 DI::pConfig()->set(local_user(), 'twitter', 'post', intval($_POST['twitter-enable']));
252 DI::pConfig()->set(local_user(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
253 DI::pConfig()->set(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
254 DI::pConfig()->set(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
255 DI::pConfig()->set(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
257 if (!intval($_POST['twitter-mirror'])) {
258 DI::pConfig()->delete(local_user(), 'twitter', 'lastid');
264 function twitter_settings(App $a, &$s)
269 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
271 * 1) Check that we have global consumer key & secret
272 * 2) If no OAuthtoken & stuff is present, generate button to get some
273 * 3) Checkbox for "Send public notices (280 chars only)
275 $ckey = DI::config()->get('twitter', 'consumerkey');
276 $csecret = DI::config()->get('twitter', 'consumersecret');
277 $otoken = DI::pConfig()->get(local_user(), 'twitter', 'oauthtoken');
278 $osecret = DI::pConfig()->get(local_user(), 'twitter', 'oauthsecret');
280 $enabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'post'));
281 $defenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'post_by_default'));
282 $mirrorenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'mirror_posts'));
283 $importenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'import'));
284 $create_userenabled = intval(DI::pConfig()->get(local_user(), 'twitter', 'create_user'));
286 $css = (($enabled) ? '' : '-disabled');
288 $s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
289 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . DI::l10n()->t('Twitter Import/Export/Mirror') . '</h3>';
291 $s .= '<div id="settings_twitter_expanded" class="settings-block" style="display: none;">';
292 $s .= '<span class="fakelink" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
293 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . DI::l10n()->t('Twitter Import/Export/Mirror') . '</h3>';
296 if ((!$ckey) && (!$csecret)) {
297 /* no global consumer keys
298 * display warning and skip personal config
300 $s .= '<p>' . DI::l10n()->t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
302 // ok we have a consumer key pair now look into the OAuth stuff
303 if ((!$otoken) && (!$osecret)) {
304 /* the user has not yet connected the account to twitter...
305 * get a temporary OAuth key/secret pair and display a button with
306 * which the user can request a PIN to connect the account to a
307 * account at Twitter.
309 $connection = new TwitterOAuth($ckey, $csecret);
311 $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
312 $s .= '<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>';
313 $s .= '<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>';
314 $s .= '<div id="twitter-pin-wrapper">';
315 $s .= '<label id="twitter-pin-label" for="twitter-pin">' . DI::l10n()->t('Copy the PIN from Twitter here') . '</label>';
316 $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
317 $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result['oauth_token'] . '" />';
318 $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result['oauth_token_secret'] . '" />';
319 $s .= '</div><div class="clear"></div>';
320 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div>';
321 } catch (TwitterOAuthException $e) {
322 $s .= '<p>' . DI::l10n()->t('An error occured: ') . $e->getMessage() . '</p>';
326 * we have an OAuth key / secret pair for the user
327 * so let's give a chance to disable the postings to Twitter
329 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
331 $details = $connection->get('account/verify_credentials');
333 $field_checkbox = Renderer::getMarkupTemplate('field_checkbox.tpl');
335 if (property_exists($details, 'screen_name') &&
336 property_exists($details, 'description') &&
337 property_exists($details, 'profile_image_url')) {
338 $s .= '<div id="twitter-info" >
339 <p>' . DI::l10n()->t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
340 <button type="submit" name="twitter-disconnect" value="1">' . DI::l10n()->t('Disconnect') . '</button>
342 <p id="twitter-info-block">
343 <a href="https://twitter.com/' . $details->screen_name . '" target="_twitter"><img id="twitter-avatar" src="' . $details->profile_image_url . '" /></a>
344 <em>' . $details->description . '</em>
348 $s .= '<div id="twitter-info" >
349 <p>Invalid Twitter info</p>
350 <button type="submit" name="twitter-disconnect" value="1">' . DI::l10n()->t('Disconnect') . '</button>
352 Logger::notice('Invalid twitter info (verify credentials).', ['auth' => TwitterOAuth::class]);
354 $s .= '<div class="clear"></div>';
356 $s .= Renderer::replaceMacros($field_checkbox, [
357 '$field' => ['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.')]
359 if ($a->user['hidewall']) {
360 $s .= '<p>' . 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.') . '</p>';
362 $s .= Renderer::replaceMacros($field_checkbox, [
363 '$field' => ['twitter-default', DI::l10n()->t('Send public postings to Twitter by default'), $defenabled, '']
365 $s .= Renderer::replaceMacros($field_checkbox, [
366 '$field' => ['twitter-mirror', DI::l10n()->t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
368 $s .= Renderer::replaceMacros($field_checkbox, [
369 '$field' => ['twitter-import', DI::l10n()->t('Import the remote timeline'), $importenabled, '']
371 $s .= Renderer::replaceMacros($field_checkbox, [
372 '$field' => ['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. However if enabled, you cannot merely remove a twitter contact from the Friendica contact list, as it will recreate this contact when they post again.')]
374 $s .= '<div class="clear"></div>';
375 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div>';
376 } catch (TwitterOAuthException $e) {
377 $s .= '<p>' . DI::l10n()->t('An error occured: ') . $e->getMessage() . '</p>';
381 $s .= '</div><div class="clear"></div>';
384 function twitter_hook_fork(App $a, array &$b)
386 if ($b['name'] != 'notifier_normal') {
392 // Deleting and editing is not supported by the addon (deleting could, but isn't by now)
393 if ($post['deleted'] || ($post['created'] !== $post['edited'])) {
394 $b['execute'] = false;
398 // if post comes from twitter don't send it back
399 if (($post['extid'] == Protocol::TWITTER) || twitter_get_id($post['extid'])) {
400 $b['execute'] = false;
404 if (substr($post['app'], 0, 7) == 'Twitter') {
405 $b['execute'] = false;
409 if (DI::pConfig()->get($post['uid'], 'twitter', 'import')) {
410 // Don't fork if it isn't a reply to a twitter post
411 if (($post['parent'] != $post['id']) && !Post::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
412 Logger::notice('No twitter parent found', ['item' => $post['id']]);
413 $b['execute'] = false;
417 // Comments are never exported when we don't import the twitter timeline
418 if (!strstr($post['postopts'], 'twitter') || ($post['parent'] != $post['id']) || $post['private']) {
419 $b['execute'] = false;
425 function twitter_post_local(App $a, array &$b)
431 if (!local_user() || (local_user() != $b['uid'])) {
435 $twitter_post = intval(DI::pConfig()->get(local_user(), 'twitter', 'post'));
436 $twitter_enable = (($twitter_post && !empty($_REQUEST['twitter_enable'])) ? intval($_REQUEST['twitter_enable']) : 0);
438 // if API is used, default to the chosen settings
439 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(), 'twitter', 'post_by_default'))) {
443 if (!$twitter_enable) {
447 if (strlen($b['postopts'])) {
448 $b['postopts'] .= ',';
451 $b['postopts'] .= 'twitter';
454 function twitter_probe_detect(App $a, array &$hookData)
456 // Don't overwrite an existing result
457 if ($hookData['result']) {
461 // Avoid a lookup for the wrong network
462 if (!in_array($hookData['network'], ['', Protocol::TWITTER])) {
466 if (preg_match('=([^@]+)@(?:mobile\.)?twitter\.com$=i', $hookData['uri'], $matches)) {
468 } elseif (preg_match('=^https?://(?:mobile\.)?twitter\.com/(.+)=i', $hookData['uri'], $matches)) {
474 $user = twitter_fetchuser($nick);
477 $hookData['result'] = twitter_user_to_contact($user);
481 function twitter_action(App $a, $uid, $pid, $action)
487 $ckey = DI::config()->get('twitter', 'consumerkey');
488 $csecret = DI::config()->get('twitter', 'consumersecret');
489 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
490 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
492 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
494 $post = ['id' => $pid];
496 Logger::debug('before action', ['action' => $action, 'pid' => $pid, 'data' => $post]);
500 // To-Do: $result = $connection->post('statuses/destroy', $post);
504 $result = $connection->post('favorites/create', $post);
505 if ($connection->getLastHttpCode() != 200) {
506 Logger::warning('Unable to create favorite', ['result' => $result]);
510 $result = $connection->post('favorites/destroy', $post);
511 if ($connection->getLastHttpCode() != 200) {
512 Logger::warning('Unable to destroy favorite', ['result' => $result]);
516 Logger::warning('Unhandled action', ['action' => $action]);
520 Logger::info('after action', ['action' => $action, 'result' => $result]);
523 function twitter_get_id(string $uri)
525 if ((substr($uri, 0, 9) != 'twitter::') || (strlen($uri) <= 9)) {
529 $id = substr($uri, 9);
530 if (!is_numeric($id)) {
537 function twitter_post_hook(App $a, array &$b)
540 if (!DI::pConfig()->get($b["uid"], 'twitter', 'import')
541 && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
545 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
549 if ($b['parent'] != $b['id']) {
550 Logger::debug('Got comment', ['item' => $b]);
552 // Looking if its a reply to a twitter post
553 if (!twitter_get_id($b["parent-uri"]) &&
554 !twitter_get_id($b["extid"]) &&
555 !twitter_get_id($b["thr-parent"])) {
556 Logger::info('No twitter post', ['parent' => $b["parent"]]);
560 $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
561 $thr_parent = Post::selectFirst(['uri', 'extid', 'author-link', 'author-nick', 'author-network'], $condition);
562 if (!DBA::isResult($thr_parent)) {
563 Logger::warning('No parent found', ['thr-parent' => $b["thr-parent"]]);
567 if ($thr_parent['author-network'] == Protocol::TWITTER) {
568 $nickname = '@[url=' . $thr_parent['author-link'] . ']' . $thr_parent['author-nick'] . '[/url]';
569 $nicknameplain = '@' . $thr_parent['author-nick'];
571 Logger::info('Comparing', ['nickname' => $nickname, 'nicknameplain' => $nicknameplain, 'body' => $b["body"]]);
572 if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
573 $b["body"] = $nickname . " " . $b["body"];
577 Logger::debug('Parent found', ['parent' => $thr_parent]);
579 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
583 // Dont't post if the post doesn't belong to us.
584 // This is a check for forum postings
585 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
586 if ($b['contact-id'] != $self['id']) {
591 if (($b['verb'] == Activity::POST) && $b['deleted']) {
592 twitter_action($a, $b['uid'], twitter_get_id($thr_parent['uri']), 'delete');
595 if ($b['verb'] == Activity::LIKE) {
596 Logger::info('Like', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]);
598 twitter_action($a, $b["uid"], twitter_get_id($b["thr-parent"]), "unlike");
600 twitter_action($a, $b["uid"], twitter_get_id($b["thr-parent"]), "like");
606 if ($b['verb'] == Activity::ANNOUNCE) {
607 Logger::info('Retweet', ['uid' => $b['uid'], 'id' => twitter_get_id($b["thr-parent"])]);
609 twitter_action($a, $b['uid'], twitter_get_id($thr_parent['extid']), 'delete');
611 twitter_retweet($b["uid"], twitter_get_id($b["thr-parent"]));
617 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
621 // if post comes from twitter don't send it back
622 if (($b['extid'] == Protocol::TWITTER) || twitter_get_id($b['extid'])) {
626 if ($b['app'] == "Twitter") {
630 Logger::notice('twitter post invoked', ['id' => $b['id'], 'guid' => $b['guid']]);
632 DI::pConfig()->load($b['uid'], 'twitter');
634 $ckey = DI::config()->get('twitter', 'consumerkey');
635 $csecret = DI::config()->get('twitter', 'consumersecret');
636 $otoken = DI::pConfig()->get($b['uid'], 'twitter', 'oauthtoken');
637 $osecret = DI::pConfig()->get($b['uid'], 'twitter', 'oauthsecret');
639 if ($ckey && $csecret && $otoken && $osecret) {
640 Logger::info('We have customer key and oauth stuff, going to send.');
642 // If it's a repeated message from twitter then do a native retweet and exit
643 if (twitter_is_retweet($a, $b['uid'], $b['body'])) {
647 Codebird::setConsumerKey($ckey, $csecret);
648 $cb = Codebird::getInstance();
649 $cb->setToken($otoken, $osecret);
651 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
653 // Set the timeout for upload to 30 seconds
654 $connection->setTimeouts(10, 30);
658 // Handling non-native reshares
659 $b['body'] = Friendica\Content\Text\BBCode::convertShare(
661 function (array $attributes, array $author_contact, $content, $is_quote_share) {
662 return twitter_convert_share($attributes, $author_contact, $content, $is_quote_share);
666 $b['body'] = twitter_update_mentions($b['body']);
668 $msgarr = Plaintext::getPost($b, $max_char, true, BBCode::TWITTER);
669 Logger::info('Got plaintext', ['id' => $b['id'], 'message' => $msgarr]);
670 $msg = $msgarr["text"];
672 if (($msg == "") && isset($msgarr["title"])) {
673 $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
676 // Add the link to the body if the type isn't a photo or there are more than 4 images in the post
677 if (!empty($msgarr['url']) && (($msgarr['type'] != 'photo') || empty($msgarr['images']) || (count($msgarr['images']) > 4))) {
678 $msg .= "\n" . $msgarr['url'];
682 Logger::notice('Empty message', ['id' => $b['id']]);
686 // and now tweet it :-)
689 if (!empty($msgarr['images'])) {
690 Logger::info('Got images', ['id' => $b['id'], 'images' => $msgarr['images']]);
693 foreach ($msgarr['images'] as $image) {
694 if (count($media_ids) == 4) {
698 $img_str = DI::httpRequest()->fetch($image['url']);
700 $tempfile = tempnam(get_temppath(), 'cache');
701 file_put_contents($tempfile, $img_str);
703 Logger::info('Uploading', ['id' => $b['id'], 'image' => $image['url']]);
704 $media = $connection->upload('media/upload', ['media' => $tempfile]);
708 if (isset($media->media_id_string)) {
709 $media_ids[] = $media->media_id_string;
711 if (!empty($image['description'])) {
712 $data = ['media_id' => $media->media_id_string,
713 'alt_text' => ['text' => substr($image['description'], 0, 420)]];
714 $ret = $cb->media_metadata_create($data);
715 Logger::info('Metadata create', ['id' => $b['id'], 'data' => $data, 'return' => json_encode($ret)]);
718 Logger::error('Failed upload', ['id' => $b['id'], 'image' => $image['url']]);
719 throw new Exception('Failed upload of ' . $image['url']);
722 $post['media_ids'] = implode(',', $media_ids);
723 if (empty($post['media_ids'])) {
724 unset($post['media_ids']);
726 } catch (Exception $e) {
727 Logger::warning('Exception when trying to send to Twitter', ['id' => $b['id'], 'message' => $e->getMessage()]);
731 $post['status'] = $msg;
734 $post['in_reply_to_status_id'] = twitter_get_id($thr_parent['uri']);
737 $url = 'statuses/update';
738 $result = $connection->post($url, $post);
739 Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
741 if (!empty($result->source)) {
742 DI::config()->set("twitter", "application_name", strip_tags($result->source));
745 if (!empty($result->errors)) {
746 Logger::error('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
748 } elseif ($thr_parent) {
749 Logger::notice('Post send, updating extid', ['id' => $b['id'], 'extid' => $result->id_str]);
750 Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
755 function twitter_addon_admin_post(App $a)
757 $consumerkey = !empty($_POST['consumerkey']) ? Strings::escapeTags(trim($_POST['consumerkey'])) : '';
758 $consumersecret = !empty($_POST['consumersecret']) ? Strings::escapeTags(trim($_POST['consumersecret'])) : '';
759 DI::config()->set('twitter', 'consumerkey', $consumerkey);
760 DI::config()->set('twitter', 'consumersecret', $consumersecret);
763 function twitter_addon_admin(App $a, &$o)
765 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/twitter/");
767 $o = Renderer::replaceMacros($t, [
768 '$submit' => DI::l10n()->t('Save Settings'),
769 // name, label, value, help, [extra values]
770 '$consumerkey' => ['consumerkey', DI::l10n()->t('Consumer key'), DI::config()->get('twitter', 'consumerkey'), ''],
771 '$consumersecret' => ['consumersecret', DI::l10n()->t('Consumer secret'), DI::config()->get('twitter', 'consumersecret'), ''],
775 function twitter_cron(App $a)
777 $last = DI::config()->get('twitter', 'last_poll');
779 $poll_interval = intval(DI::config()->get('twitter', 'poll_interval'));
780 if (!$poll_interval) {
781 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
785 $next = $last + ($poll_interval * 60);
786 if ($next > time()) {
787 Logger::notice('twitter: poll intervall not reached');
791 Logger::notice('twitter: cron_start');
793 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
794 if (DBA::isResult($r)) {
795 foreach ($r as $rr) {
796 Logger::notice('Fetching', ['user' => $rr['uid']]);
797 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
801 $abandon_days = intval(DI::config()->get('system', 'account_abandon_days'));
802 if ($abandon_days < 1) {
806 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
808 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
809 if (DBA::isResult($r)) {
810 foreach ($r as $rr) {
811 if ($abandon_days != 0) {
812 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
813 if (!DBA::isResult($user)) {
814 Logger::notice('abandoned account: timeline from user will not be imported', ['user' => $rr['uid']]);
819 Logger::notice('importing timeline', ['user' => $rr['uid']]);
820 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
823 // check for new contacts once a day
824 $last_contact_check = DI::pConfig()->get($rr['uid'],'pumpio','contact_check');
825 if($last_contact_check)
826 $next_contact_check = $last_contact_check + 86400;
828 $next_contact_check = 0;
830 if($next_contact_check <= time()) {
831 pumpio_getallusers($a, $rr["uid"]);
832 DI::pConfig()->set($rr['uid'],'pumpio','contact_check',time());
838 Logger::notice('twitter: cron_end');
840 DI::config()->set('twitter', 'last_poll', time());
843 function twitter_expire(App $a)
845 $days = DI::config()->get('twitter', 'expire');
851 Logger::notice('Start deleting expired posts');
853 $r = Post::select(['id', 'guid'], ['deleted' => true, 'network' => Protocol::TWITTER]);
854 while ($row = Post::fetch($r)) {
855 Logger::info('[twitter] Delete expired item', ['id' => $row['id'], 'guid' => $row['guid'], 'callstack' => \Friendica\Core\System::callstack()]);
856 Item::markForDeletionById($row['id']);
860 Logger::notice('End deleting expired posts');
862 Logger::notice('Start expiry');
864 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
865 if (DBA::isResult($r)) {
866 foreach ($r as $rr) {
867 Logger::notice('twitter_expire', ['user' => $rr['uid']]);
868 Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
872 Logger::notice('End expiry');
875 function twitter_prepare_body(App $a, array &$b)
877 if ($b["item"]["network"] != Protocol::TWITTER) {
884 $item["plink"] = DI::baseUrl()->get() . "/display/" . $item["guid"];
886 $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
887 $orig_post = Post::selectFirst(['author-link'], $condition);
888 if (DBA::isResult($orig_post)) {
889 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
890 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
891 $nicknameplain = "@" . $nicknameplain;
893 if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) {
894 $item["body"] = $nickname . " " . $item["body"];
898 $msgarr = Plaintext::getPost($item, $max_char, true, BBCode::TWITTER);
899 $msg = $msgarr["text"];
901 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
902 $msg .= " " . $msgarr["url"];
905 if (isset($msgarr["image"])) {
906 $msg .= " " . $msgarr["image"];
909 $b['html'] = nl2br(htmlspecialchars($msg));
913 function twitter_statuses_show(string $id, TwitterOAuth $twitterOAuth = null)
915 if ($twitterOAuth === null) {
916 $ckey = DI::config()->get('twitter', 'consumerkey');
917 $csecret = DI::config()->get('twitter', 'consumersecret');
919 if (empty($ckey) || empty($csecret)) {
920 return new stdClass();
923 $twitterOAuth = new TwitterOAuth($ckey, $csecret);
926 $parameters = ['trim_user' => false, 'tweet_mode' => 'extended', 'id' => $id, 'include_ext_alt_text' => true];
928 return $twitterOAuth->get('statuses/show', $parameters);
932 * Parse Twitter status URLs since Twitter removed OEmbed
935 * @param array $b Expected format:
937 * 'url' => [URL to parse],
938 * 'format' => 'json'|'',
939 * 'text' => Output parameter
941 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
943 function twitter_parse_link(App $a, array &$b)
945 // Only handle Twitter status URLs
946 if (!preg_match('#^https?://(?:mobile\.|www\.)?twitter.com/[^/]+/status/(\d+).*#', $b['url'], $matches)) {
950 $status = twitter_statuses_show($matches[1]);
952 if (empty($status->id)) {
956 $item = twitter_createpost($a, 0, $status, [], true, false, true);
958 if ($b['format'] == 'json') {
960 foreach ($status->extended_entities->media ?? [] as $media) {
961 if (!empty($media->media_url_https)) {
963 'src' => $media->media_url_https,
964 'width' => $media->sizes->thumb->w,
965 'height' => $media->sizes->thumb->h,
973 'url' => $item['plink'],
974 'title' => DI::l10n()->t('%s on Twitter', $status->user->name),
975 'text' => BBCode::toPlaintext($item['body'], false),
978 'contentType' => 'attachment',
982 $b['text'] = BBCode::getShareOpeningTag(
983 $item['author-name'],
984 $item['author-link'],
985 $item['author-avatar'],
989 $b['text'] .= $item['body'] . '[/share]';
994 /*********************
998 *********************/
1002 * @brief Build the item array for the mirrored post
1004 * @param App $a Application class
1005 * @param integer $uid User id
1006 * @param object $post Twitter object with the post
1008 * @return array item data to be posted
1010 function twitter_do_mirrorpost(App $a, $uid, $post)
1012 $datarray['uid'] = $uid;
1013 $datarray['extid'] = 'twitter::' . $post->id;
1014 $datarray['title'] = '';
1016 if (!empty($post->retweeted_status)) {
1017 // We don't support nested shares, so we mustn't show quotes as shares on retweets
1018 $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true, -1);
1020 if (empty($item['body'])) {
1024 $datarray['body'] = "\n" . BBCode::getShareOpeningTag(
1025 $item['author-name'],
1026 $item['author-link'],
1027 $item['author-avatar'],
1032 $datarray['body'] .= $item['body'] . '[/share]';
1034 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false, -1);
1036 if (empty($item['body'])) {
1040 $datarray['body'] = $item['body'];
1043 $datarray['app'] = $item['app'];
1044 $datarray['verb'] = $item['verb'];
1046 if (isset($item['location'])) {
1047 $datarray['location'] = $item['location'];
1050 if (isset($item['coord'])) {
1051 $datarray['coord'] = $item['coord'];
1057 function twitter_fetchtimeline(App $a, $uid)
1059 $ckey = DI::config()->get('twitter', 'consumerkey');
1060 $csecret = DI::config()->get('twitter', 'consumersecret');
1061 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
1062 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
1063 $lastid = DI::pConfig()->get($uid, 'twitter', 'lastid');
1065 $application_name = DI::config()->get('twitter', 'application_name');
1067 if ($application_name == "") {
1068 $application_name = DI::baseUrl()->getHostname();
1071 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1073 // Ensure to have the own contact
1075 twitter_fetch_own_contact($a, $uid);
1076 } catch (TwitterOAuthException $e) {
1077 Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
1081 $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
1083 $first_time = ($lastid == "");
1085 if ($lastid != "") {
1086 $parameters["since_id"] = $lastid;
1090 $items = $connection->get('statuses/user_timeline', $parameters);
1091 } catch (TwitterOAuthException $e) {
1092 Logger::warning('Error fetching timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
1096 if (!is_array($items)) {
1097 Logger::notice('No items', ['user' => $uid]);
1101 $posts = array_reverse($items);
1103 Logger::notice('Start processing posts', ['from' => $lastid, 'user' => $uid, 'count' => count($posts)]);
1105 if (count($posts)) {
1106 foreach ($posts as $post) {
1107 if ($post->id_str > $lastid) {
1108 $lastid = $post->id_str;
1109 DI::pConfig()->set($uid, 'twitter', 'lastid', $lastid);
1116 if (!stristr($post->source, $application_name)) {
1117 Logger::info('Preparing mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
1119 $mirrorpost = twitter_do_mirrorpost($a, $uid, $post);
1121 if (empty($mirrorpost['body'])) {
1125 Logger::info('Posting mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]);
1127 Post\Delayed::add($mirrorpost['extid'], $mirrorpost, PRIORITY_MEDIUM, true);
1131 DI::pConfig()->set($uid, 'twitter', 'lastid', $lastid);
1132 Logger::log('Last ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1135 function twitter_fix_avatar($avatar)
1137 $new_avatar = str_replace("_normal.", ".", $avatar);
1139 $info = Images::getInfoFromURLCached($new_avatar);
1141 $new_avatar = $avatar;
1147 function twitter_get_relation($uid, $target, $contact = [])
1149 if (isset($contact['rel'])) {
1150 $relation = $contact['rel'];
1155 $ckey = DI::config()->get('twitter', 'consumerkey');
1156 $csecret = DI::config()->get('twitter', 'consumersecret');
1157 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
1158 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
1159 $own_id = DI::pConfig()->get($uid, 'twitter', 'own_id');
1161 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1162 $parameters = ['source_id' => $own_id, 'target_screen_name' => $target];
1165 $status = $connection->get('friendships/show', $parameters);
1166 if ($connection->getLastHttpCode() !== 200) {
1167 throw new Exception($status->errors[0]->message ?? 'HTTP response code ' . $connection->getLastHttpCode(), $status->errors[0]->code ?? $connection->getLastHttpCode());
1170 $following = $status->relationship->source->following;
1171 $followed = $status->relationship->source->followed_by;
1173 if ($following && !$followed) {
1174 $relation = Contact::SHARING;
1175 } elseif (!$following && $followed) {
1176 $relation = Contact::FOLLOWER;
1177 } elseif ($following && $followed) {
1178 $relation = Contact::FRIEND;
1179 } elseif (!$following && !$followed) {
1183 Logger::info('Fetched friendship relation', ['user' => $uid, 'target' => $target, 'relation' => $relation]);
1184 } catch (Throwable $e) {
1185 Logger::warning('Error fetching friendship status', ['uid' => $uid, 'target' => $target, 'message' => $e->getMessage()]);
1195 function twitter_user_to_contact($data)
1197 if (empty($data->id_str)) {
1201 $baseurl = 'https://twitter.com';
1202 $url = $baseurl . '/' . $data->screen_name;
1203 $addr = $data->screen_name . '@twitter.com';
1207 'network' => Protocol::TWITTER,
1208 'alias' => 'twitter::' . $data->id_str,
1209 'baseurl' => $baseurl,
1210 'name' => $data->name,
1211 'nick' => $data->screen_name,
1213 'location' => $data->location,
1214 'about' => $data->description,
1215 'photo' => twitter_fix_avatar($data->profile_image_url_https),
1221 function twitter_fetch_contact($uid, $data, $create_user)
1223 $fields = twitter_user_to_contact($data);
1225 if (empty($fields)) {
1229 // photo comes from twitter_user_to_contact but shouldn't be saved directly in the contact row
1230 $avatar = $fields['photo'];
1231 unset($fields['photo']);
1233 // Update the public contact
1234 $pcontact = DBA::selectFirst('contact', ['id'], ['uid' => 0, 'alias' => "twitter::" . $data->id_str]);
1235 if (DBA::isResult($pcontact)) {
1236 $cid = $pcontact['id'];
1238 $cid = Contact::getIdForURL($fields['url'], 0, false, $fields);
1242 DBA::update('contact', $fields, ['id' => $cid]);
1243 Contact::updateAvatar($cid, $avatar);
1245 Logger::warning('No contact found', ['fields' => $fields]);
1248 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
1249 if (!DBA::isResult($contact) && empty($cid)) {
1250 Logger::warning('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
1252 } elseif (!$create_user) {
1256 if (!DBA::isResult($contact)) {
1257 $relation = twitter_get_relation($uid, $data->screen_name);
1259 // create contact record
1260 $fields['uid'] = $uid;
1261 $fields['created'] = DateTimeFormat::utcNow();
1262 $fields['nurl'] = Strings::normaliseLink($fields['url']);
1263 $fields['poll'] = 'twitter::' . $data->id_str;
1264 $fields['rel'] = $relation;
1265 $fields['priority'] = 1;
1266 $fields['writable'] = true;
1267 $fields['blocked'] = false;
1268 $fields['readonly'] = false;
1269 $fields['pending'] = false;
1271 if (!DBA::insert('contact', $fields)) {
1275 $contact_id = DBA::lastInsertId();
1277 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1279 Contact::updateAvatar($contact_id, $avatar);
1281 if ($contact["readonly"] || $contact["blocked"]) {
1282 Logger::notice('Contact is blocked or readonly.', ['nickname' => $contact["nick"]]);
1286 $contact_id = $contact['id'];
1289 // Update the contact relation once per day
1290 if ($contact['updated'] < DateTimeFormat::utc('now -24 hours')) {
1291 $fields['rel'] = twitter_get_relation($uid, $data->screen_name, $contact);
1295 Contact::updateAvatar($contact['id'], $avatar);
1297 if ($contact['name'] != $data->name) {
1298 $fields['name-date'] = $fields['uri-date'] = DateTimeFormat::utcNow();
1302 if ($contact['nick'] != $data->screen_name) {
1303 $fields['uri-date'] = DateTimeFormat::utcNow();
1307 if (($contact['location'] != $data->location) || ($contact['about'] != $data->description)) {
1312 $fields['updated'] = DateTimeFormat::utcNow();
1313 DBA::update('contact', $fields, ['id' => $contact['id']]);
1314 Logger::info('Updated contact', ['id' => $contact['id'], 'nick' => $data->screen_name]);
1322 * @param string $screen_name
1323 * @return stdClass|null
1326 function twitter_fetchuser($screen_name)
1328 $ckey = DI::config()->get('twitter', 'consumerkey');
1329 $csecret = DI::config()->get('twitter', 'consumersecret');
1332 // Fetching user data
1333 $connection = new TwitterOAuth($ckey, $csecret);
1334 $parameters = ['screen_name' => $screen_name];
1335 $user = $connection->get('users/show', $parameters);
1336 } catch (TwitterOAuthException $e) {
1337 Logger::warning('Error fetching user', ['user' => $screen_name, 'message' => $e->getMessage()]);
1341 if (!is_object($user)) {
1349 * Replaces Twitter entities with Friendica-friendly links.
1351 * The Twitter API gives indices for each entity, which allows for fine-grained replacement.
1353 * First, we need to collect everything that needs to be replaced, what we will replace it with, and the start index.
1354 * Then we sort the indices decreasingly, and we replace from the end of the body to the start in order for the next
1355 * index to be correct even after the last replacement.
1357 * @param string $body
1358 * @param stdClass $status
1360 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
1362 function twitter_expand_entities($body, stdClass $status)
1365 $contains_urls = false;
1369 $replacementList = [];
1371 foreach ($status->entities->hashtags AS $hashtag) {
1372 $replace = '#[url=' . DI::baseUrl()->get() . '/search?tag=' . $hashtag->text . ']' . $hashtag->text . '[/url]';
1373 $taglist['#' . $hashtag->text] = ['#', $hashtag->text, ''];
1375 $replacementList[$hashtag->indices[0]] = [
1376 'replace' => $replace,
1377 'length' => $hashtag->indices[1] - $hashtag->indices[0],
1381 foreach ($status->entities->user_mentions AS $mention) {
1382 $replace = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
1383 $taglist['@' . $mention->screen_name] = ['@', $mention->screen_name, 'https://twitter.com/' . rawurlencode($mention->screen_name)];
1385 $replacementList[$mention->indices[0]] = [
1386 'replace' => $replace,
1387 'length' => $mention->indices[1] - $mention->indices[0],
1391 foreach ($status->entities->urls ?? [] as $url) {
1392 $plain = str_replace($url->url, '', $plain);
1394 if ($url->url && $url->expanded_url && $url->display_url) {
1395 // Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
1396 if (!empty($status->quoted_status) && isset($status->quoted_status_id_str)
1397 && substr($url->expanded_url, -strlen($status->quoted_status_id_str)) == $status->quoted_status_id_str
1399 $replacementList[$url->indices[0]] = [
1401 'length' => $url->indices[1] - $url->indices[0],
1406 $contains_urls = true;
1408 $expanded_url = $url->expanded_url;
1410 // Quickfix: Workaround for URL with '[' and ']' in it
1411 if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
1412 $expanded_url = $url->url;
1415 $replacementList[$url->indices[0]] = [
1416 'replace' => '[url=' . $expanded_url . ']' . $url->display_url . '[/url]',
1417 'length' => $url->indices[1] - $url->indices[0],
1422 krsort($replacementList);
1424 foreach ($replacementList as $startIndex => $parameters) {
1425 $body = Strings::substringReplace($body, $parameters['replace'], $startIndex, $parameters['length']);
1428 $body = trim($body);
1430 return ['body' => trim($body), 'plain' => trim($plain), 'taglist' => $taglist, 'urls' => $contains_urls];
1434 * Store entity attachments
1436 * @param integer $uriid
1437 * @param object $post Twitter object with the post
1439 function twitter_store_attachments(int $uriid, $post)
1441 if (!empty($post->extended_entities->media)) {
1442 foreach ($post->extended_entities->media AS $medium) {
1443 switch ($medium->type) {
1445 $attachment = ['uri-id' => $uriid, 'type' => Post\Media::IMAGE];
1447 $attachment['url'] = $medium->media_url_https . '?name=large';
1448 $attachment['width'] = $medium->sizes->large->w;
1449 $attachment['height'] = $medium->sizes->large->h;
1451 if ($medium->sizes->small->w != $attachment['width']) {
1452 $attachment['preview'] = $medium->media_url_https . '?name=small';
1453 $attachment['preview-width'] = $medium->sizes->small->w;
1454 $attachment['preview-height'] = $medium->sizes->small->h;
1457 $attachment['name'] = $medium->display_url ?? null;
1458 $attachment['description'] = $medium->ext_alt_text ?? null;
1459 Logger::debug('Photo attachment', ['attachment' => $attachment]);
1460 Post\Media::insert($attachment);
1463 case 'animated_gif':
1464 $attachment = ['uri-id' => $uriid, 'type' => Post\Media::VIDEO];
1465 if (is_array($medium->video_info->variants)) {
1467 // We take the video with the highest bitrate
1468 foreach ($medium->video_info->variants AS $variant) {
1469 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1470 $attachment['url'] = $variant->url;
1471 $bitrate = $variant->bitrate;
1476 $attachment['name'] = $medium->display_url ?? null;
1477 $attachment['preview'] = $medium->media_url_https . ':small';
1478 $attachment['preview-width'] = $medium->sizes->small->w;
1479 $attachment['preview-height'] = $medium->sizes->small->h;
1480 $attachment['description'] = $medium->ext_alt_text ?? null;
1481 Logger::debug('Video attachment', ['attachment' => $attachment]);
1482 Post\Media::insert($attachment);
1485 Logger::notice('Unknown media type', ['medium' => $medium]);
1490 if (!empty($post->entities->urls)) {
1491 foreach ($post->entities->urls as $url) {
1492 $attachment = ['uri-id' => $uriid, 'type' => Post\Media::UNKNOWN, 'url' => $url->expanded_url, 'name' => $url->display_url];
1493 Logger::debug('Attached link', ['attachment' => $attachment]);
1494 Post\Media::insert($attachment);
1500 * @brief Fetch media entities and add media links to the body
1502 * @param object $post Twitter object with the post
1503 * @param array $postarray Array of the item that is about to be posted
1504 * @param integer $uriid URI Id used to store tags. -1 = don't store tags for this post.
1506 function twitter_media_entities($post, array &$postarray, int $uriid = -1)
1508 // There are no media entities? So we quit.
1509 if (empty($post->extended_entities->media)) {
1513 // This is a pure media post, first search for all media urls
1515 foreach ($post->extended_entities->media AS $medium) {
1516 if (!isset($media[$medium->url])) {
1517 $media[$medium->url] = '';
1519 switch ($medium->type) {
1521 if (!empty($medium->ext_alt_text)) {
1522 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1523 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1525 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1528 $postarray['object-type'] = Activity\ObjectType::IMAGE;
1529 $postarray['post-type'] = Item::PT_IMAGE;
1532 // Currently deactivated, since this causes the video to be display before the content
1533 // We have to figure out a better way for declaring the post type and the display style.
1534 //$postarray['post-type'] = Item::PT_VIDEO;
1535 case 'animated_gif':
1536 if (!empty($medium->ext_alt_text)) {
1537 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1538 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1540 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1543 $postarray['object-type'] = Activity\ObjectType::VIDEO;
1544 if (is_array($medium->video_info->variants)) {
1546 // We take the video with the highest bitrate
1547 foreach ($medium->video_info->variants AS $variant) {
1548 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1549 $media[$medium->url] = "\n[video]" . $variant->url . '[/video]';
1550 $bitrate = $variant->bitrate;
1559 foreach ($media AS $key => $value) {
1560 $postarray['body'] = str_replace($key, '', $postarray['body']);
1565 // Now we replace the media urls.
1566 foreach ($media AS $key => $value) {
1567 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1572 * Undocumented function
1575 * @param integer $uid User ID
1576 * @param object $post Incoming Twitter post
1577 * @param array $self
1578 * @param bool $create_user Should users be created?
1579 * @param bool $only_existing_contact Only import existing contacts if set to "true"
1580 * @param bool $noquote
1581 * @param integer $uriid URI Id used to store tags. 0 = create a new one; -1 = don't store tags for this post.
1582 * @return array item array
1584 function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $only_existing_contact, $noquote, int $uriid = 0)
1587 $postarray['network'] = Protocol::TWITTER;
1588 $postarray['uid'] = $uid;
1589 $postarray['wall'] = 0;
1590 $postarray['uri'] = "twitter::" . $post->id_str;
1591 $postarray['protocol'] = Conversation::PARCEL_TWITTER;
1592 $postarray['source'] = json_encode($post);
1593 $postarray['direction'] = Conversation::PULL;
1595 if (empty($uriid)) {
1596 $uriid = $postarray['uri-id'] = ItemURI::insert(['uri' => $postarray['uri']]);
1599 // Don't import our own comments
1600 if (Post::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
1601 Logger::info('Item found', ['extid' => $postarray['uri']]);
1607 if ($post->in_reply_to_status_id_str != "") {
1608 $thr_parent = "twitter::" . $post->in_reply_to_status_id_str;
1610 $item = Post::selectFirst(['uri'], ['uri' => $thr_parent, 'uid' => $uid]);
1611 if (!DBA::isResult($item)) {
1612 $item = Post::selectFirst(['uri'], ['extid' => $thr_parent, 'uid' => $uid]);
1615 if (DBA::isResult($item)) {
1616 $postarray['thr-parent'] = $item['uri'];
1617 $postarray['object-type'] = Activity\ObjectType::COMMENT;
1619 $postarray['object-type'] = Activity\ObjectType::NOTE;
1623 $own_id = DI::pConfig()->get($uid, 'twitter', 'own_id');
1625 if ($post->user->id_str == $own_id) {
1626 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1629 if (DBA::isResult($r)) {
1630 $contactid = $r[0]["id"];
1632 $postarray['owner-name'] = $r[0]["name"];
1633 $postarray['owner-link'] = $r[0]["url"];
1634 $postarray['owner-avatar'] = $r[0]["photo"];
1636 Logger::error('No self contact found', ['uid' => $uid]);
1640 // Don't create accounts of people who just comment something
1641 $create_user = false;
1643 $postarray['object-type'] = Activity\ObjectType::NOTE;
1646 if ($contactid == 0) {
1647 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1649 $postarray['owner-name'] = $post->user->name;
1650 $postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
1651 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1654 if (($contactid == 0) && !$only_existing_contact) {
1655 $contactid = $self['id'];
1656 } elseif ($contactid <= 0) {
1657 Logger::info('Contact ID is zero or less than zero.');
1661 $postarray['contact-id'] = $contactid;
1663 $postarray['verb'] = Activity::POST;
1664 $postarray['author-name'] = $postarray['owner-name'];
1665 $postarray['author-link'] = $postarray['owner-link'];
1666 $postarray['author-avatar'] = $postarray['owner-avatar'];
1667 $postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
1668 $postarray['app'] = strip_tags($post->source);
1670 if ($post->user->protected) {
1671 $postarray['private'] = Item::PRIVATE;
1672 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1674 $postarray['private'] = Item::UNLISTED;
1675 $postarray['allow_cid'] = '';
1678 if (!empty($post->full_text)) {
1679 $postarray['body'] = $post->full_text;
1681 $postarray['body'] = $post->text;
1684 // When the post contains links then use the correct object type
1685 if (count($post->entities->urls) > 0) {
1686 $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
1689 // Search for media links
1690 twitter_media_entities($post, $postarray, $uriid);
1692 $converted = twitter_expand_entities($postarray['body'], $post);
1694 // When the post contains external links then images or videos are just "decorations".
1695 if (!empty($converted['urls'])) {
1696 $postarray['post-type'] = Item::PT_NOTE;
1699 $postarray['body'] = $converted['body'];
1700 $postarray['created'] = DateTimeFormat::utc($post->created_at);
1701 $postarray['edited'] = DateTimeFormat::utc($post->created_at);
1704 twitter_store_tags($uriid, $converted['taglist']);
1705 twitter_store_attachments($uriid, $post);
1708 if (!empty($post->place->name)) {
1709 $postarray["location"] = $post->place->name;
1711 if (!empty($post->place->full_name)) {
1712 $postarray["location"] = $post->place->full_name;
1714 if (!empty($post->geo->coordinates)) {
1715 $postarray["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
1717 if (!empty($post->coordinates->coordinates)) {
1718 $postarray["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
1720 if (!empty($post->retweeted_status)) {
1721 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1723 if (empty($retweet['body'])) {
1728 // Store the original tweet
1729 Item::insert($retweet);
1731 // CHange the other post into a reshare activity
1732 $postarray['verb'] = Activity::ANNOUNCE;
1733 $postarray['gravity'] = GRAVITY_ACTIVITY;
1734 $postarray['object-type'] = Activity\ObjectType::NOTE;
1736 $postarray['thr-parent'] = $retweet['uri'];
1738 $retweet['source'] = $postarray['source'];
1739 $retweet['direction'] = $postarray['direction'];
1740 $retweet['private'] = $postarray['private'];
1741 $retweet['allow_cid'] = $postarray['allow_cid'];
1742 $retweet['contact-id'] = $postarray['contact-id'];
1743 $retweet['owner-name'] = $postarray['owner-name'];
1744 $retweet['owner-link'] = $postarray['owner-link'];
1745 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1747 $postarray = $retweet;
1751 if (!empty($post->quoted_status)) {
1753 // To avoid recursive share blocks we just provide the link to avoid removing quote context.
1754 $postarray['body'] .= "\n\nhttps://twitter.com/" . $post->quoted_status->user->screen_name . "/status/" . $post->quoted_status->id_str;
1756 $quoted = twitter_createpost($a, 0, $post->quoted_status, $self, false, false, true);
1757 if (!empty($quoted['body'])) {
1758 Item::insert($quoted);
1759 $post = Post::selectFirst(['guid', 'uri-id'], ['uri' => $quoted['uri'], 'uid' => 0]);
1760 Logger::info('Stored quoted post', ['uid' => $uid, 'uri-id' => $uriid, 'post' => $post]);
1762 $postarray['body'] .= "\n" . BBCode::getShareOpeningTag(
1763 $quoted['author-name'],
1764 $quoted['author-link'],
1765 $quoted['author-avatar'],
1771 $postarray['body'] .= $quoted['body'] . '[/share]';
1773 // Quoted post author is blocked/ignored, so we just provide the link to avoid removing quote context.
1774 $postarray['body'] .= "\n\nhttps://twitter.com/" . $post->quoted_status->user->screen_name . "/status/" . $post->quoted_status->id_str;
1783 * Store tags and mentions
1785 * @param integer $uriid
1786 * @param array $taglist
1788 function twitter_store_tags(int $uriid, array $taglist)
1790 foreach ($taglist as $tag) {
1791 Tag::storeByHash($uriid, $tag[0], $tag[1], $tag[2]);
1795 function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection, array $self)
1797 Logger::info('Fetching parent posts', ['user' => $uid, 'post' => $post->id_str]);
1801 while (!empty($post->in_reply_to_status_id_str)) {
1803 $post = twitter_statuses_show($post->in_reply_to_status_id_str, $connection);
1804 } catch (TwitterOAuthException $e) {
1805 Logger::warning('Error fetching parent post', ['uid' => $uid, 'post' => $post->id_str, 'message' => $e->getMessage()]);
1810 Logger::info("twitter_fetchparentposts: Can't fetch post");
1814 if (empty($post->id_str)) {
1815 Logger::info("twitter_fetchparentposts: This is not a post", ['post' => $post]);
1819 if (Post::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1826 Logger::log("twitter_fetchparentposts: Fetching " . count($posts) . " parents", Logger::DEBUG);
1828 $posts = array_reverse($posts);
1830 if (!empty($posts)) {
1831 foreach ($posts as $post) {
1832 $postarray = twitter_createpost($a, $uid, $post, $self, false, !DI::pConfig()->get($uid, 'twitter', 'create_user'), false);
1834 if (empty($postarray['body'])) {
1838 $item = Item::insert($postarray);
1840 $postarray["id"] = $item;
1842 Logger::log('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
1847 function twitter_fetchhometimeline(App $a, $uid)
1849 $ckey = DI::config()->get('twitter', 'consumerkey');
1850 $csecret = DI::config()->get('twitter', 'consumersecret');
1851 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
1852 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
1853 $create_user = DI::pConfig()->get($uid, 'twitter', 'create_user');
1854 $mirror_posts = DI::pConfig()->get($uid, 'twitter', 'mirror_posts');
1856 Logger::info('Fetching timeline', ['uid' => $uid]);
1858 $application_name = DI::config()->get('twitter', 'application_name');
1860 if ($application_name == "") {
1861 $application_name = DI::baseUrl()->getHostname();
1864 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1867 $own_contact = twitter_fetch_own_contact($a, $uid);
1868 } catch (TwitterOAuthException $e) {
1869 Logger::warning('Error fetching own contact', ['uid' => $uid, 'message' => $e->getMessage()]);
1873 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1874 intval($own_contact),
1877 if (DBA::isResult($r)) {
1878 $own_id = $r[0]["nick"];
1880 Logger::warning('Own twitter contact not found', ['uid' => $uid]);
1884 $self = User::getOwnerDataById($uid);
1885 if ($self === false) {
1886 Logger::warning('Own contact not found', ['uid' => $uid]);
1890 $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
1891 //$parameters["count"] = 200;
1892 // Fetching timeline
1893 $lastid = DI::pConfig()->get($uid, 'twitter', 'lasthometimelineid');
1895 $first_time = ($lastid == "");
1897 if ($lastid != "") {
1898 $parameters["since_id"] = $lastid;
1902 $items = $connection->get('statuses/home_timeline', $parameters);
1903 } catch (TwitterOAuthException $e) {
1904 Logger::warning('Error fetching home timeline', ['uid' => $uid, 'message' => $e->getMessage()]);
1908 if (!is_array($items)) {
1909 Logger::warning('home timeline is no array', ['items' => $items]);
1913 if (empty($items)) {
1914 Logger::notice('No new timeline content', ['uid' => $uid]);
1918 $posts = array_reverse($items);
1920 Logger::notice('Processing timeline', ['lastid' => $lastid, 'uid' => $uid, 'count' => count($posts)]);
1922 if (count($posts)) {
1923 foreach ($posts as $post) {
1924 if ($post->id_str > $lastid) {
1925 $lastid = $post->id_str;
1926 DI::pConfig()->set($uid, 'twitter', 'lasthometimelineid', $lastid);
1933 if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1934 Logger::info("Skip previously sent post");
1938 if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1939 Logger::info("Skip post that will be mirrored");
1943 if ($post->in_reply_to_status_id_str != "") {
1944 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1947 Logger::log('Preparing post ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
1949 $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1951 if (empty($postarray['body']) || trim($postarray['body']) == "") {
1952 Logger::log('Empty body for post ' . $post->id_str . ' and user ' . $uid, Logger::DEBUG);
1958 if (empty($postarray['thr-parent'])) {
1959 $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
1960 if (DBA::isResult($contact) && Item::isRemoteSelf($contact, $postarray)) {
1961 $notify = PRIORITY_MEDIUM;
1965 $item = Item::insert($postarray, $notify);
1966 $postarray["id"] = $item;
1968 Logger::log('User ' . $uid . ' posted home timeline item ' . $item);
1971 DI::pConfig()->set($uid, 'twitter', 'lasthometimelineid', $lastid);
1973 Logger::log('Last timeline ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1975 // Fetching mentions
1976 $lastid = DI::pConfig()->get($uid, 'twitter', 'lastmentionid');
1978 $first_time = ($lastid == "");
1980 if ($lastid != "") {
1981 $parameters["since_id"] = $lastid;
1985 $items = $connection->get('statuses/mentions_timeline', $parameters);
1986 } catch (TwitterOAuthException $e) {
1987 Logger::warning('Error fetching mentions', ['uid' => $uid, 'message' => $e->getMessage()]);
1991 if (!is_array($items)) {
1992 Logger::warning("mentions are no arrays", ['items' => $items]);
1996 $posts = array_reverse($items);
1998 Logger::log("Fetching mentions for user " . $uid . " " . sizeof($posts) . " items", Logger::DEBUG);
2000 if (count($posts)) {
2001 foreach ($posts as $post) {
2002 if ($post->id_str > $lastid) {
2003 $lastid = $post->id_str;
2010 if ($post->in_reply_to_status_id_str != "") {
2011 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
2014 $postarray = twitter_createpost($a, $uid, $post, $self, false, !$create_user, false);
2016 if (empty($postarray['body'])) {
2020 $item = Item::insert($postarray);
2022 Logger::log('User ' . $uid . ' posted mention timeline item ' . $item);
2026 DI::pConfig()->set($uid, 'twitter', 'lastmentionid', $lastid);
2028 Logger::log('Last mentions ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
2031 function twitter_fetch_own_contact(App $a, $uid)
2033 $ckey = DI::config()->get('twitter', 'consumerkey');
2034 $csecret = DI::config()->get('twitter', 'consumersecret');
2035 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
2036 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
2038 $own_id = DI::pConfig()->get($uid, 'twitter', 'own_id');
2042 if ($own_id == "") {
2043 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
2045 // Fetching user data
2046 // get() may throw TwitterOAuthException, but we will catch it later
2047 $user = $connection->get('account/verify_credentials');
2048 if (empty($user->id_str)) {
2052 DI::pConfig()->set($uid, 'twitter', 'own_id', $user->id_str);
2054 $contact_id = twitter_fetch_contact($uid, $user, true);
2056 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
2058 DBA::escape("twitter::" . $own_id));
2059 if (DBA::isResult($r)) {
2060 $contact_id = $r[0]["id"];
2062 DI::pConfig()->delete($uid, 'twitter', 'own_id');
2069 function twitter_is_retweet(App $a, $uid, $body)
2071 $body = trim($body);
2073 // Skip if it isn't a pure repeated messages
2074 // Does it start with a share?
2075 if (strpos($body, "[share") > 0) {
2079 // Does it end with a share?
2080 if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
2084 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
2085 // Skip if there is no shared message in there
2086 if ($body == $attributes) {
2091 preg_match("/link='(.*?)'/ism", $attributes, $matches);
2092 if (!empty($matches[1])) {
2093 $link = $matches[1];
2096 preg_match('/link="(.*?)"/ism', $attributes, $matches);
2097 if (!empty($matches[1])) {
2098 $link = $matches[1];
2101 $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
2105 return twitter_retweet($uid, $id);
2108 function twitter_retweet(int $uid, int $id, int $item_id = 0)
2110 Logger::info('Retweeting', ['user' => $uid, 'id' => $id]);
2112 $ckey = DI::config()->get('twitter', 'consumerkey');
2113 $csecret = DI::config()->get('twitter', 'consumersecret');
2114 $otoken = DI::pConfig()->get($uid, 'twitter', 'oauthtoken');
2115 $osecret = DI::pConfig()->get($uid, 'twitter', 'oauthsecret');
2117 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
2118 $result = $connection->post('statuses/retweet/' . $id);
2120 Logger::info('Retweeted', ['user' => $uid, 'id' => $id, 'result' => $result]);
2122 if (!empty($item_id) && !empty($result->id_str)) {
2123 Logger::notice('Update extid', ['id' => $item_id, 'extid' => $result->id_str]);
2124 Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $item_id]);
2127 return !isset($result->errors);
2130 function twitter_update_mentions($body)
2132 $URLSearchString = "^\[\]";
2133 $return = preg_replace_callback(
2134 "/@\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
2135 function ($matches) {
2136 if (strpos($matches[1], 'twitter.com')) {
2137 $return = '@' . substr($matches[1], strrpos($matches[1], '/') + 1);
2139 $return = $matches[2] . ' (' . $matches[1] . ')';
2150 function twitter_convert_share(array $attributes, array $author_contact, $content, $is_quote_share)
2152 if (empty($author_contact)) {
2153 return $content . "\n\n" . $attributes['link'];
2156 if (!empty($author_contact['network']) && ($author_contact['network'] == Protocol::TWITTER)) {
2157 $mention = '@' . $author_contact['nick'];
2159 $mention = $author_contact['addr'];
2162 return ($is_quote_share ? "\n\n" : '' ) . 'RT ' . $mention . ': ' . $content . "\n\n" . $attributes['link'];