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\OEmbed;
70 use Friendica\Content\Text\BBCode;
71 use Friendica\Content\Text\Plaintext;
72 use Friendica\Core\Config;
73 use Friendica\Core\Hook;
74 use Friendica\Core\L10n;
75 use Friendica\Core\Logger;
76 use Friendica\Core\PConfig;
77 use Friendica\Core\Protocol;
78 use Friendica\Core\Renderer;
79 use Friendica\Core\Worker;
80 use Friendica\Database\DBA;
81 use Friendica\Model\Contact;
82 use Friendica\Model\Conversation;
83 use Friendica\Model\Group;
84 use Friendica\Model\Item;
85 use Friendica\Model\ItemContent;
86 use Friendica\Model\User;
87 use Friendica\Protocol\Activity;
88 use Friendica\Util\ConfigFileLoader;
89 use Friendica\Util\DateTimeFormat;
90 use Friendica\Util\Images;
91 use Friendica\Util\Network;
92 use Friendica\Util\Strings;
94 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
96 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
98 function twitter_install()
100 // we need some hooks, for the configuration and for sending tweets
101 Hook::register('load_config' , __FILE__, 'twitter_load_config');
102 Hook::register('connector_settings' , __FILE__, 'twitter_settings');
103 Hook::register('connector_settings_post', __FILE__, 'twitter_settings_post');
104 Hook::register('hook_fork' , __FILE__, 'twitter_hook_fork');
105 Hook::register('post_local' , __FILE__, 'twitter_post_local');
106 Hook::register('notifier_normal' , __FILE__, 'twitter_post_hook');
107 Hook::register('jot_networks' , __FILE__, 'twitter_jot_nets');
108 Hook::register('cron' , __FILE__, 'twitter_cron');
109 Hook::register('follow' , __FILE__, 'twitter_follow');
110 Hook::register('expire' , __FILE__, 'twitter_expire');
111 Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
112 Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
113 Logger::info("installed twitter");
116 function twitter_uninstall()
118 Hook::unregister('load_config' , __FILE__, 'twitter_load_config');
119 Hook::unregister('connector_settings' , __FILE__, 'twitter_settings');
120 Hook::unregister('connector_settings_post', __FILE__, 'twitter_settings_post');
121 Hook::unregister('hook_fork' , __FILE__, 'twitter_hook_fork');
122 Hook::unregister('post_local' , __FILE__, 'twitter_post_local');
123 Hook::unregister('notifier_normal' , __FILE__, 'twitter_post_hook');
124 Hook::unregister('jot_networks' , __FILE__, 'twitter_jot_nets');
125 Hook::unregister('cron' , __FILE__, 'twitter_cron');
126 Hook::unregister('follow' , __FILE__, 'twitter_follow');
127 Hook::unregister('expire' , __FILE__, 'twitter_expire');
128 Hook::unregister('prepare_body' , __FILE__, 'twitter_prepare_body');
129 Hook::unregister('check_item_notification', __FILE__, 'twitter_check_item_notification');
131 // old setting - remove only
132 Hook::unregister('post_local_end' , __FILE__, 'twitter_post_hook');
133 Hook::unregister('addon_settings' , __FILE__, 'twitter_settings');
134 Hook::unregister('addon_settings_post', __FILE__, 'twitter_settings_post');
137 function twitter_load_config(App $a, ConfigFileLoader $loader)
139 $a->getConfigCache()->load($loader->loadAddonConfig('twitter'));
142 function twitter_check_item_notification(App $a, array &$notification_data)
144 $own_id = PConfig::get($notification_data["uid"], 'twitter', 'own_id');
146 $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
147 intval($notification_data["uid"]),
148 DBA::escape("twitter::".$own_id)
152 $notification_data["profiles"][] = $own_user[0]["url"];
156 function twitter_follow(App $a, array &$contact)
158 Logger::info('Check if contact is twitter contact', ['url' => $contact["url"]]);
160 if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
164 // contact seems to be a twitter contact, so continue
165 $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
166 $nickname = str_replace("@twitter.com", "", $nickname);
168 $uid = $a->user["uid"];
170 $ckey = Config::get('twitter', 'consumerkey');
171 $csecret = Config::get('twitter', 'consumersecret');
172 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
173 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
175 // If the addon is not configured (general or for this user) quit here
176 if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
181 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
182 $connection->post('friendships/create', ['screen_name' => $nickname]);
184 twitter_fetchuser($a, $uid, $nickname);
186 $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey
187 FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
189 DBA::escape($nickname));
190 if (DBA::isResult($r)) {
191 $contact["contact"] = $r[0];
195 function twitter_jot_nets(App $a, array &$jotnets_fields)
201 if (PConfig::get(local_user(), 'twitter', 'post')) {
202 $jotnets_fields[] = [
203 'type' => 'checkbox',
206 L10n::t('Post to Twitter'),
207 PConfig::get(local_user(), 'twitter', 'post_by_default')
214 function twitter_settings_post(App $a)
219 // don't check twitter settings if twitter submit button is not clicked
220 if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
224 if (!empty($_POST['twitter-disconnect'])) {
226 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
227 * from the user configuration
229 PConfig::delete(local_user(), 'twitter', 'consumerkey');
230 PConfig::delete(local_user(), 'twitter', 'consumersecret');
231 PConfig::delete(local_user(), 'twitter', 'oauthtoken');
232 PConfig::delete(local_user(), 'twitter', 'oauthsecret');
233 PConfig::delete(local_user(), 'twitter', 'post');
234 PConfig::delete(local_user(), 'twitter', 'post_by_default');
235 PConfig::delete(local_user(), 'twitter', 'lastid');
236 PConfig::delete(local_user(), 'twitter', 'mirror_posts');
237 PConfig::delete(local_user(), 'twitter', 'import');
238 PConfig::delete(local_user(), 'twitter', 'create_user');
239 PConfig::delete(local_user(), 'twitter', 'own_id');
241 if (isset($_POST['twitter-pin'])) {
242 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
243 Logger::notice('got a Twitter PIN');
244 $ckey = Config::get('twitter', 'consumerkey');
245 $csecret = Config::get('twitter', 'consumersecret');
246 // the token and secret for which the PIN was generated were hidden in the settings
247 // form as token and token2, we need a new connection to Twitter using these token
248 // and secret to request a Access Token with the PIN
250 if (empty($_POST['twitter-pin'])) {
251 throw new Exception(L10n::t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.'));
254 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
255 $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]);
256 // ok, now that we have the Access Token, save them in the user config
257 PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
258 PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
259 PConfig::set(local_user(), 'twitter', 'post', 1);
260 } catch(Exception $e) {
261 info($e->getMessage());
262 } catch(TwitterOAuthException $e) {
263 info($e->getMessage());
265 // reload the Addon Settings page, if we don't do it see Bug #42
266 $a->internalRedirect('settings/connectors');
268 // if no PIN is supplied in the POST variables, the user has changed the setting
269 // to post a tweet for every new __public__ posting to the wall
270 PConfig::set(local_user(), 'twitter', 'post', intval($_POST['twitter-enable']));
271 PConfig::set(local_user(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
272 PConfig::set(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
273 PConfig::set(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
274 PConfig::set(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
276 if (!intval($_POST['twitter-mirror'])) {
277 PConfig::delete(local_user(), 'twitter', 'lastid');
280 info(L10n::t('Twitter settings updated.') . EOL);
285 function twitter_settings(App $a, &$s)
290 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
292 * 1) Check that we have global consumer key & secret
293 * 2) If no OAuthtoken & stuff is present, generate button to get some
294 * 3) Checkbox for "Send public notices (280 chars only)
296 $ckey = Config::get('twitter', 'consumerkey');
297 $csecret = Config::get('twitter', 'consumersecret');
298 $otoken = PConfig::get(local_user(), 'twitter', 'oauthtoken');
299 $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret');
301 $enabled = intval(PConfig::get(local_user(), 'twitter', 'post'));
302 $defenabled = intval(PConfig::get(local_user(), 'twitter', 'post_by_default'));
303 $mirrorenabled = intval(PConfig::get(local_user(), 'twitter', 'mirror_posts'));
304 $importenabled = intval(PConfig::get(local_user(), 'twitter', 'import'));
305 $create_userenabled = intval(PConfig::get(local_user(), 'twitter', 'create_user'));
307 $css = (($enabled) ? '' : '-disabled');
309 $s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
310 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
312 $s .= '<div id="settings_twitter_expanded" class="settings-block" style="display: none;">';
313 $s .= '<span class="fakelink" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
314 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
317 if ((!$ckey) && (!$csecret)) {
318 /* no global consumer keys
319 * display warning and skip personal config
321 $s .= '<p>' . L10n::t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
323 // ok we have a consumer key pair now look into the OAuth stuff
324 if ((!$otoken) && (!$osecret)) {
325 /* the user has not yet connected the account to twitter...
326 * get a temporary OAuth key/secret pair and display a button with
327 * which the user can request a PIN to connect the account to a
328 * account at Twitter.
330 $connection = new TwitterOAuth($ckey, $csecret);
332 $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
333 $s .= '<p>' . L10n::t('At this Friendica instance the Twitter addon was enabled but you have not yet connected your account to your Twitter account. To do so click the button below to get a PIN from Twitter which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to Twitter.') . '</p>';
334 $s .= '<a href="' . $connection->url('oauth/authorize', ['oauth_token' => $result['oauth_token']]) . '" target="_twitter"><img src="addon/twitter/lighter.png" alt="' . L10n::t('Log in with Twitter') . '"></a>';
335 $s .= '<div id="twitter-pin-wrapper">';
336 $s .= '<label id="twitter-pin-label" for="twitter-pin">' . L10n::t('Copy the PIN from Twitter here') . '</label>';
337 $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
338 $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result['oauth_token'] . '" />';
339 $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result['oauth_token_secret'] . '" />';
340 $s .= '</div><div class="clear"></div>';
341 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
342 } catch (TwitterOAuthException $e) {
343 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
347 * we have an OAuth key / secret pair for the user
348 * so let's give a chance to disable the postings to Twitter
350 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
352 $details = $connection->get('account/verify_credentials');
354 $field_checkbox = Renderer::getMarkupTemplate('field_checkbox.tpl');
356 if (property_exists($details, 'screen_name') &&
357 property_exists($details, 'description') &&
358 property_exists($details, 'profile_image_url')) {
359 $s .= '<div id="twitter-info" >
360 <p>' . L10n::t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
361 <button type="submit" name="twitter-disconnect" value="1">' . L10n::t('Disconnect') . '</button>
363 <p id="twitter-info-block">
364 <a href="https://twitter.com/' . $details->screen_name . '" target="_twitter"><img id="twitter-avatar" src="' . $details->profile_image_url . '" /></a>
365 <em>' . $details->description . '</em>
369 $s .= '<div id="twitter-info" >
370 <p>Invalid Twitter info</p>
372 Logger::info('Invalid twitter info (verify credentials).', ['auth' => TwitterOAuth::class]);
374 $s .= '<div class="clear"></div>';
376 $s .= Renderer::replaceMacros($field_checkbox, [
377 '$field' => ['twitter-enable', L10n::t('Allow posting to Twitter'), $enabled, 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.')]
379 if ($a->user['hidewall']) {
380 $s .= '<p>' . 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>';
382 $s .= Renderer::replaceMacros($field_checkbox, [
383 '$field' => ['twitter-default', L10n::t('Send public postings to Twitter by default'), $defenabled, '']
385 $s .= Renderer::replaceMacros($field_checkbox, [
386 '$field' => ['twitter-mirror', L10n::t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
388 $s .= Renderer::replaceMacros($field_checkbox, [
389 '$field' => ['twitter-import', L10n::t('Import the remote timeline'), $importenabled, '']
391 $s .= Renderer::replaceMacros($field_checkbox, [
392 '$field' => ['twitter-create_user', L10n::t('Automatically create contacts'), $create_userenabled, 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.')]
394 $s .= '<div class="clear"></div>';
395 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
396 } catch (TwitterOAuthException $e) {
397 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
401 $s .= '</div><div class="clear"></div>';
404 function twitter_hook_fork(App $a, array &$b)
406 if ($b['name'] != 'notifier_normal') {
412 // Deleting and editing is not supported by the addon (deleting could, but isn't by now)
413 if ($post['deleted'] || ($post['created'] !== $post['edited'])) {
414 $b['execute'] = false;
418 // if post comes from twitter don't send it back
419 if ($post['extid'] == Protocol::TWITTER) {
420 $b['execute'] = false;
424 if ($post['app'] == 'Twitter') {
425 $b['execute'] = false;
429 if (PConfig::get($post['uid'], 'twitter', 'import')) {
430 // Don't fork if it isn't a reply to a twitter post
431 if (($post['parent'] != $post['id']) && !Item::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
432 Logger::notice('No twitter parent found', ['item' => $post['id']]);
433 $b['execute'] = false;
437 // Comments are never exported when we don't import the twitter timeline
438 if (!strstr($post['postopts'], 'twitter') || ($post['parent'] != $post['id']) || $post['private']) {
439 $b['execute'] = false;
445 function twitter_post_local(App $a, array &$b)
451 if (!local_user() || (local_user() != $b['uid'])) {
455 $twitter_post = intval(PConfig::get(local_user(), 'twitter', 'post'));
456 $twitter_enable = (($twitter_post && !empty($_REQUEST['twitter_enable'])) ? intval($_REQUEST['twitter_enable']) : 0);
458 // if API is used, default to the chosen settings
459 if ($b['api_source'] && intval(PConfig::get(local_user(), 'twitter', 'post_by_default'))) {
463 if (!$twitter_enable) {
467 if (strlen($b['postopts'])) {
468 $b['postopts'] .= ',';
471 $b['postopts'] .= 'twitter';
474 function twitter_action(App $a, $uid, $pid, $action)
476 $ckey = Config::get('twitter', 'consumerkey');
477 $csecret = Config::get('twitter', 'consumersecret');
478 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
479 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
481 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
483 $post = ['id' => $pid];
485 Logger::log("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), Logger::DATA);
489 // To-Do: $result = $connection->post('statuses/destroy', $post);
493 $result = $connection->post('favorites/create', $post);
496 $result = $connection->post('favorites/destroy', $post);
499 Logger::log('Unhandled action ' . $action, Logger::DEBUG);
502 Logger::log("twitter_action '" . $action . "' send, result: " . print_r($result, true), Logger::DEBUG);
505 function twitter_post_hook(App $a, array &$b)
508 if (!PConfig::get($b["uid"], 'twitter', 'import')
509 && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
513 if ($b['parent'] != $b['id']) {
514 Logger::log("twitter_post_hook: parameter " . print_r($b, true), Logger::DATA);
516 // Looking if its a reply to a twitter post
517 if ((substr($b["parent-uri"], 0, 9) != "twitter::")
518 && (substr($b["extid"], 0, 9) != "twitter::")
519 && (substr($b["thr-parent"], 0, 9) != "twitter::"))
521 Logger::log("twitter_post_hook: no twitter post " . $b["parent"]);
525 $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
526 $orig_post = Item::selectFirst([], $condition);
527 if (!DBA::isResult($orig_post)) {
528 Logger::log("twitter_post_hook: no parent found " . $b["thr-parent"]);
535 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
536 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
537 $nicknameplain = "@" . $nicknameplain;
539 Logger::log("twitter_post_hook: comparing " . $nickname . " and " . $nicknameplain . " with " . $b["body"], Logger::DEBUG);
540 if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
541 $b["body"] = $nickname . " " . $b["body"];
544 Logger::log("twitter_post_hook: parent found " . print_r($orig_post, true), Logger::DATA);
548 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
552 // Dont't post if the post doesn't belong to us.
553 // This is a check for forum postings
554 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
555 if ($b['contact-id'] != $self['id']) {
560 if (($b['verb'] == Activity::POST) && $b['deleted']) {
561 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
564 if ($b['verb'] == Activity::LIKE) {
565 Logger::log("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), Logger::DEBUG);
567 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
569 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
575 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
579 // if post comes from twitter don't send it back
580 if ($b['extid'] == Protocol::TWITTER) {
584 if ($b['app'] == "Twitter") {
588 Logger::notice('twitter post invoked', ['id' => $b['id'], 'guid' => $b['guid']]);
590 PConfig::load($b['uid'], 'twitter');
592 $ckey = Config::get('twitter', 'consumerkey');
593 $csecret = Config::get('twitter', 'consumersecret');
594 $otoken = PConfig::get($b['uid'], 'twitter', 'oauthtoken');
595 $osecret = PConfig::get($b['uid'], 'twitter', 'oauthsecret');
597 if ($ckey && $csecret && $otoken && $osecret) {
598 Logger::log('twitter: we have customer key and oauth stuff, going to send.', Logger::DEBUG);
600 // If it's a repeated message from twitter then do a native retweet and exit
601 if (twitter_is_retweet($a, $b['uid'], $b['body'])) {
605 Codebird::setConsumerKey($ckey, $csecret);
606 $cb = Codebird::getInstance();
607 $cb->setToken($otoken, $osecret);
609 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
611 // Set the timeout for upload to 30 seconds
612 $connection->setTimeouts(10, 30);
616 // Handling non-native reshares
617 $b['body'] = Friendica\Content\Text\BBCode::convertShare(
619 function (array $attributes, array $author_contact, $content, $is_quote_share) {
620 return twitter_convert_share($attributes, $author_contact, $content, $is_quote_share);
624 $b['body'] = twitter_update_mentions($b['body']);
626 $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
627 Logger::info('Got plaintext', ['id' => $b['id'], 'message' => $msgarr]);
628 $msg = $msgarr["text"];
630 if (($msg == "") && isset($msgarr["title"])) {
631 $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
634 if (!empty($msgarr['url']) && ($msgarr['url'] == $b['plink']) && !empty($msgarr['images']) && (count($msgarr['images']) <= 4)) {
636 } elseif (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
637 $msg .= "\n" . $msgarr["url"];
644 Logger::info('Empty message', ['id' => $b['id']]);
648 // and now tweet it :-)
651 if (!empty($msgarr['images'])) {
652 Logger::info('Got images', ['id' => $b['id'], 'images' => $msgarr['images']]);
655 foreach ($msgarr['images'] as $image) {
656 if (count($media_ids) == 4) {
660 $img_str = Network::fetchUrl($image['url']);
662 $tempfile = tempnam(get_temppath(), 'cache');
663 file_put_contents($tempfile, $img_str);
665 Logger::info('Uploading', ['id' => $b['id'], 'image' => $image['url']]);
666 $media = $connection->upload('media/upload', ['media' => $tempfile]);
670 if (isset($media->media_id_string)) {
671 $media_ids[] = $media->media_id_string;
673 if (!empty($image['description'])) {
674 $data = ['media_id' => $media->media_id_string,
675 'alt_text' => ['text' => substr($image['description'], 0, 420)]];
676 $ret = $cb->media_metadata_create($data);
677 Logger::info('Metadata create', ['id' => $b['id'], 'data' => $data, 'return' => json_encode($ret)]);
680 throw new Exception('Failed upload', ['id' => $b['id'], 'image' => $image['url']]);
683 $post['media_ids'] = implode(',', $media_ids);
684 if (empty($post['media_ids'])) {
685 unset($post['media_ids']);
687 } catch (Exception $e) {
688 Logger::info('Exception when trying to send to Twitter', ['id' => $b['id'], 'message' => $e->getMessage()]);
692 $post['status'] = $msg;
695 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
698 $url = 'statuses/update';
699 $result = $connection->post($url, $post);
700 Logger::info('twitter_post send', ['id' => $b['id'], 'result' => $result]);
702 if (!empty($result->source)) {
703 Config::set("twitter", "application_name", strip_tags($result->source));
706 if (!empty($result->errors)) {
707 Logger::info('Send to Twitter failed', ['id' => $b['id'], 'error' => $result->errors]);
709 } elseif ($iscomment) {
710 Logger::info('Update extid', ['id' => $b['id'], 'extid' => $result->id_str]);
711 Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
716 function twitter_addon_admin_post(App $a)
718 $consumerkey = !empty($_POST['consumerkey']) ? Strings::escapeTags(trim($_POST['consumerkey'])) : '';
719 $consumersecret = !empty($_POST['consumersecret']) ? Strings::escapeTags(trim($_POST['consumersecret'])) : '';
720 Config::set('twitter', 'consumerkey', $consumerkey);
721 Config::set('twitter', 'consumersecret', $consumersecret);
722 info(L10n::t('Settings updated.') . EOL);
725 function twitter_addon_admin(App $a, &$o)
727 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/twitter/");
729 $o = Renderer::replaceMacros($t, [
730 '$submit' => L10n::t('Save Settings'),
731 // name, label, value, help, [extra values]
732 '$consumerkey' => ['consumerkey', L10n::t('Consumer key'), Config::get('twitter', 'consumerkey'), ''],
733 '$consumersecret' => ['consumersecret', L10n::t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''],
737 function twitter_cron(App $a)
739 $last = Config::get('twitter', 'last_poll');
741 $poll_interval = intval(Config::get('twitter', 'poll_interval'));
742 if (!$poll_interval) {
743 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
747 $next = $last + ($poll_interval * 60);
748 if ($next > time()) {
749 Logger::notice('twitter: poll intervall not reached');
753 Logger::notice('twitter: cron_start');
755 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
756 if (DBA::isResult($r)) {
757 foreach ($r as $rr) {
758 Logger::notice('Fetching', ['user' => $rr['uid']]);
759 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
763 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
764 if ($abandon_days < 1) {
768 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
770 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
771 if (DBA::isResult($r)) {
772 foreach ($r as $rr) {
773 if ($abandon_days != 0) {
774 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
775 if (!DBA::isResult($user)) {
776 Logger::notice('abandoned account: timeline from user will not be imported', ['user' => $rr['uid']]);
781 Logger::notice('importing timeline', ['user' => $rr['uid']]);
782 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
785 // check for new contacts once a day
786 $last_contact_check = PConfig::get($rr['uid'],'pumpio','contact_check');
787 if($last_contact_check)
788 $next_contact_check = $last_contact_check + 86400;
790 $next_contact_check = 0;
792 if($next_contact_check <= time()) {
793 pumpio_getallusers($a, $rr["uid"]);
794 PConfig::set($rr['uid'],'pumpio','contact_check',time());
800 Logger::notice('twitter: cron_end');
802 Config::set('twitter', 'last_poll', time());
805 function twitter_expire(App $a)
807 $days = Config::get('twitter', 'expire');
813 $r = Item::select(['id'], ['deleted' => true, 'network' => Protocol::TWITTER]);
814 while ($row = DBA::fetch($r)) {
815 DBA::delete('item', ['id' => $row['id']]);
819 Logger::notice('twitter_expire: expire_start');
821 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
822 if (DBA::isResult($r)) {
823 foreach ($r as $rr) {
824 Logger::notice('twitter_expire', ['user' => $rr['uid']]);
825 Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
829 Logger::notice('twitter_expire: expire_end');
832 function twitter_prepare_body(App $a, array &$b)
834 if ($b["item"]["network"] != Protocol::TWITTER) {
841 $item["plink"] = $a->getBaseURL() . "/display/" . $item["guid"];
843 $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
844 $orig_post = Item::selectFirst(['author-link'], $condition);
845 if (DBA::isResult($orig_post)) {
846 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
847 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
848 $nicknameplain = "@" . $nicknameplain;
850 if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) {
851 $item["body"] = $nickname . " " . $item["body"];
855 $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, 8);
856 $msg = $msgarr["text"];
858 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
859 $msg .= " " . $msgarr["url"];
862 if (isset($msgarr["image"])) {
863 $msg .= " " . $msgarr["image"];
866 $b['html'] = nl2br(htmlspecialchars($msg));
871 * @brief Build the item array for the mirrored post
873 * @param App $a Application class
874 * @param integer $uid User id
875 * @param object $post Twitter object with the post
877 * @return array item data to be posted
879 function twitter_do_mirrorpost(App $a, $uid, $post)
881 $datarray['api_source'] = true;
882 $datarray['profile_uid'] = $uid;
883 $datarray['extid'] = Protocol::TWITTER;
884 $datarray['message_id'] = Item::newURI($uid, Protocol::TWITTER . ':' . $post->id);
885 $datarray['protocol'] = Conversation::PARCEL_TWITTER;
886 $datarray['source'] = json_encode($post);
887 $datarray['title'] = '';
889 if (!empty($post->retweeted_status)) {
890 // We don't support nested shares, so we mustn't show quotes as shares on retweets
891 $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true);
893 if (empty($item['body'])) {
897 $datarray['body'] = "\n" . share_header(
898 $item['author-name'],
899 $item['author-link'],
900 $item['author-avatar'],
906 $datarray['body'] .= $item['body'] . '[/share]';
908 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
910 if (empty($item['body'])) {
914 $datarray['body'] = $item['body'];
917 $datarray['source'] = $item['app'];
918 $datarray['verb'] = $item['verb'];
920 if (isset($item['location'])) {
921 $datarray['location'] = $item['location'];
924 if (isset($item['coord'])) {
925 $datarray['coord'] = $item['coord'];
931 function twitter_fetchtimeline(App $a, $uid)
933 $ckey = Config::get('twitter', 'consumerkey');
934 $csecret = Config::get('twitter', 'consumersecret');
935 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
936 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
937 $lastid = PConfig::get($uid, 'twitter', 'lastid');
939 $application_name = Config::get('twitter', 'application_name');
941 if ($application_name == "") {
942 $application_name = $a->getHostName();
945 $has_picture = false;
947 require_once 'mod/item.php';
948 require_once 'mod/share.php';
950 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
952 $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
954 $first_time = ($lastid == "");
957 $parameters["since_id"] = $lastid;
961 $items = $connection->get('statuses/user_timeline', $parameters);
962 } catch (TwitterOAuthException $e) {
963 Logger::notice('Error fetching timeline', ['user' => $uid, 'message' => $e->getMessage()]);
967 if (!is_array($items)) {
968 Logger::notice('No items', ['user' => $uid]);
972 $posts = array_reverse($items);
974 Logger::log('Starting from ID ' . $lastid . ' for user ' . $uid, Logger::DEBUG);
977 foreach ($posts as $post) {
978 if ($post->id_str > $lastid) {
979 $lastid = $post->id_str;
980 PConfig::set($uid, 'twitter', 'lastid', $lastid);
987 if (!stristr($post->source, $application_name)) {
988 $_SESSION["authenticated"] = true;
989 $_SESSION["uid"] = $uid;
991 Logger::log('Preparing Twitter ID ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
993 $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
995 if (empty($_REQUEST['body'])) {
999 Logger::log('Posting Twitter ID ' . $post->id_str . ' for user ' . $uid);
1005 PConfig::set($uid, 'twitter', 'lastid', $lastid);
1006 Logger::log('Last ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1009 function twitter_fix_avatar($avatar)
1011 $new_avatar = str_replace("_normal.", ".", $avatar);
1013 $info = Images::getInfoFromURLCached($new_avatar);
1015 $new_avatar = $avatar;
1021 function twitter_fetch_contact($uid, $data, $create_user)
1023 if (empty($data->id_str)) {
1027 $avatar = twitter_fix_avatar($data->profile_image_url_https);
1028 $url = "https://twitter.com/" . $data->screen_name;
1029 $addr = $data->screen_name . "@twitter.com";
1031 $fields = ['url' => $url, 'network' => Protocol::TWITTER,
1032 'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
1033 'location' => $data->location, 'about' => $data->description];
1035 $cid = Contact::getIdForURL($url, 0, true, $fields);
1037 DBA::update('contact', $fields, ['id' => $cid]);
1038 Contact::updateAvatar($avatar, 0, $cid);
1041 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
1042 if (!DBA::isResult($contact) && !$create_user) {
1043 Logger::info('User contact not found', ['uid' => $uid, 'twitter-id' => $data->id_str]);
1047 if (!DBA::isResult($contact)) {
1048 // create contact record
1049 $fields['uid'] = $uid;
1050 $fields['created'] = DateTimeFormat::utcNow();
1051 $fields['nurl'] = Strings::normaliseLink($url);
1052 $fields['alias'] = 'twitter::' . $data->id_str;
1053 $fields['poll'] = 'twitter::' . $data->id_str;
1054 $fields['rel'] = Contact::FRIEND;
1055 $fields['priority'] = 1;
1056 $fields['writable'] = true;
1057 $fields['blocked'] = false;
1058 $fields['readonly'] = false;
1059 $fields['pending'] = false;
1061 if (!DBA::insert('contact', $fields)) {
1065 $contact_id = DBA::lastInsertId();
1067 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1069 Contact::updateAvatar($avatar, $uid, $contact_id);
1071 if ($contact["readonly"] || $contact["blocked"]) {
1072 Logger::log("twitter_fetch_contact: Contact '" . $contact["nick"] . "' is blocked or readonly.", Logger::DEBUG);
1076 $contact_id = $contact['id'];
1078 // update profile photos once every twelve hours as we have no notification of when they change.
1079 $update_photo = ($contact['avatar-date'] < DateTimeFormat::utc('now -12 hours'));
1081 // check that we have all the photos, this has been known to fail on occasion
1082 if (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']) || $update_photo) {
1083 Logger::log("twitter_fetch_contact: Updating contact " . $data->screen_name, Logger::DEBUG);
1085 Contact::updateAvatar($avatar, $uid, $contact['id']);
1087 $fields['name-date'] = DateTimeFormat::utcNow();
1088 $fields['uri-date'] = DateTimeFormat::utcNow();
1090 DBA::update('contact', $fields, ['id' => $contact['id']]);
1097 function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
1099 $ckey = Config::get('twitter', 'consumerkey');
1100 $csecret = Config::get('twitter', 'consumersecret');
1101 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1102 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1104 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1107 if (DBA::isResult($r)) {
1115 if ($screen_name != "") {
1116 $parameters["screen_name"] = $screen_name;
1119 if ($user_id != "") {
1120 $parameters["user_id"] = $user_id;
1123 // Fetching user data
1124 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1126 $user = $connection->get('users/show', $parameters);
1127 } catch (TwitterOAuthException $e) {
1128 Logger::log('twitter_fetchuser: Error fetching user ' . $uid . ': ' . $e->getMessage());
1132 if (!is_object($user)) {
1136 $contact_id = twitter_fetch_contact($uid, $user, true);
1141 function twitter_expand_entities(App $a, $body, $item, $picture)
1147 foreach ($item->entities->hashtags AS $hashtag) {
1148 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . $hashtag->text . ']' . $hashtag->text . '[/url]';
1149 $tags_arr['#' . $hashtag->text] = $url;
1150 $body = str_replace('#' . $hashtag->text, $url, $body);
1153 foreach ($item->entities->user_mentions AS $mention) {
1154 $url = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
1155 $tags_arr['@' . $mention->screen_name] = $url;
1156 $body = str_replace('@' . $mention->screen_name, $url, $body);
1159 if (isset($item->entities->urls)) {
1165 foreach ($item->entities->urls as $url) {
1166 $plain = str_replace($url->url, '', $plain);
1168 if ($url->url && $url->expanded_url && $url->display_url) {
1169 // Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
1170 if (!empty($item->quoted_status) && isset($item->quoted_status_id_str)
1171 && substr($url->expanded_url, -strlen($item->quoted_status_id_str)) == $item->quoted_status_id_str ) {
1172 $body = str_replace($url->url, '', $body);
1176 $expanded_url = $url->expanded_url;
1178 $final_url = Network::finalUrl($url->expanded_url);
1180 $oembed_data = OEmbed::fetchURL($final_url);
1182 if (empty($oembed_data) || empty($oembed_data->type)) {
1186 // Quickfix: Workaround for URL with '[' and ']' in it
1187 if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
1188 $expanded_url = $url->url;
1192 $type = $oembed_data->type;
1195 if ($oembed_data->type == 'video') {
1196 $type = $oembed_data->type;
1197 $footerurl = $expanded_url;
1198 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1200 $body = str_replace($url->url, $footerlink, $body);
1201 } elseif (($oembed_data->type == 'photo') && isset($oembed_data->url)) {
1202 $body = str_replace($url->url, '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]', $body);
1203 } elseif ($oembed_data->type != 'link') {
1204 $body = str_replace($url->url, '[url=' . $expanded_url . ']' . $url->display_url . '[/url]', $body);
1206 $img_str = Network::fetchUrl($final_url, true, 4);
1208 $tempfile = tempnam(get_temppath(), 'cache');
1209 file_put_contents($tempfile, $img_str);
1211 // See http://php.net/manual/en/function.exif-imagetype.php#79283
1212 if (filesize($tempfile) > 11) {
1213 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
1220 if (substr($mime, 0, 6) == 'image/') {
1222 $body = str_replace($url->url, '[img]' . $final_url . '[/img]', $body);
1224 $type = $oembed_data->type;
1225 $footerurl = $expanded_url;
1226 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1228 $body = str_replace($url->url, $footerlink, $body);
1234 // Footer will be taken care of with a share block in the case of a quote
1235 if (empty($item->quoted_status)) {
1236 if ($footerurl != '') {
1237 $footer = add_page_info($footerurl, false, $picture);
1240 if (($footerlink != '') && (trim($footer) != '')) {
1241 $removedlink = trim(str_replace($footerlink, '', $body));
1243 if (($removedlink == '') || strstr($body, $removedlink)) {
1244 $body = $removedlink;
1250 if ($footer == '' && $picture != '') {
1251 $body .= "\n\n[img]" . $picture . "[/img]\n";
1252 } elseif ($footer == '' && $picture == '') {
1253 $body = add_page_info_to_body($body);
1258 // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
1259 $tags = BBCode::getTags($body);
1262 foreach ($tags as $tag) {
1263 if (strstr(trim($tag), ' ')) {
1267 if (strpos($tag, '#') === 0) {
1268 if (strpos($tag, '[url=')) {
1272 // don't link tags that are already embedded in links
1273 if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
1276 if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
1280 $basetag = str_replace('_', ' ', substr($tag, 1));
1281 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]';
1282 $body = str_replace($tag, $url, $body);
1283 $tags_arr['#' . $basetag] = $url;
1284 } elseif (strpos($tag, '@') === 0) {
1285 if (strpos($tag, '[url=')) {
1289 $basetag = substr($tag, 1);
1290 $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1291 $body = str_replace($tag, $url, $body);
1292 $tags_arr['@' . $basetag] = $url;
1297 $tags = implode($tags_arr, ',');
1299 return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
1303 * @brief Fetch media entities and add media links to the body
1305 * @param object $post Twitter object with the post
1306 * @param array $postarray Array of the item that is about to be posted
1308 * @return $picture string Image URL or empty string
1310 function twitter_media_entities($post, array &$postarray)
1312 // There are no media entities? So we quit.
1313 if (empty($post->extended_entities->media)) {
1317 // When the post links to an external page, we only take one picture.
1318 // We only do this when there is exactly one media.
1319 if ((count($post->entities->urls) > 0) && (count($post->extended_entities->media) == 1)) {
1320 $medium = $post->extended_entities->media[0];
1322 foreach ($post->entities->urls as $link) {
1323 // Let's make sure the external link url matches the media url
1324 if ($medium->url == $link->url && isset($medium->media_url_https)) {
1325 $picture = $medium->media_url_https;
1326 $postarray['body'] = str_replace($medium->url, '', $postarray['body']);
1332 // This is a pure media post, first search for all media urls
1334 foreach ($post->extended_entities->media AS $medium) {
1335 if (!isset($media[$medium->url])) {
1336 $media[$medium->url] = '';
1338 switch ($medium->type) {
1340 if (!empty($medium->ext_alt_text)) {
1341 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1342 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1344 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1347 $postarray['object-type'] = Activity\ObjectType::IMAGE;
1350 case 'animated_gif':
1351 if (!empty($medium->ext_alt_text)) {
1352 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1353 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1355 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1358 $postarray['object-type'] = Activity\ObjectType::VIDEO;
1359 if (is_array($medium->video_info->variants)) {
1361 // We take the video with the highest bitrate
1362 foreach ($medium->video_info->variants AS $variant) {
1363 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1364 $media[$medium->url] = "\n[video]" . $variant->url . '[/video]';
1365 $bitrate = $variant->bitrate;
1370 // The following code will only be activated for test reasons
1372 // $postarray['body'] .= print_r($medium, true);
1376 // Now we replace the media urls.
1377 foreach ($media AS $key => $value) {
1378 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1384 function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $only_existing_contact, $noquote)
1387 $postarray['network'] = Protocol::TWITTER;
1388 $postarray['uid'] = $uid;
1389 $postarray['wall'] = 0;
1390 $postarray['uri'] = "twitter::" . $post->id_str;
1391 $postarray['protocol'] = Conversation::PARCEL_TWITTER;
1392 $postarray['source'] = json_encode($post);
1394 // Don't import our own comments
1395 if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
1396 Logger::log("Item with extid " . $postarray['uri'] . " found.", Logger::DEBUG);
1402 if ($post->in_reply_to_status_id_str != "") {
1403 $parent = "twitter::" . $post->in_reply_to_status_id_str;
1405 $fields = ['uri', 'parent-uri', 'parent'];
1406 $parent_item = Item::selectFirst($fields, ['uri' => $parent, 'uid' => $uid]);
1407 if (!DBA::isResult($parent_item)) {
1408 $parent_item = Item::selectFirst($fields, ['extid' => $parent, 'uid' => $uid]);
1411 if (DBA::isResult($parent_item)) {
1412 $postarray['thr-parent'] = $parent_item['uri'];
1413 $postarray['parent-uri'] = $parent_item['parent-uri'];
1414 $postarray['parent'] = $parent_item['parent'];
1415 $postarray['object-type'] = Activity\ObjectType::COMMENT;
1417 $postarray['thr-parent'] = $postarray['uri'];
1418 $postarray['parent-uri'] = $postarray['uri'];
1419 $postarray['object-type'] = Activity\ObjectType::NOTE;
1423 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1425 if ($post->user->id_str == $own_id) {
1426 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1429 if (DBA::isResult($r)) {
1430 $contactid = $r[0]["id"];
1432 $postarray['owner-name'] = $r[0]["name"];
1433 $postarray['owner-link'] = $r[0]["url"];
1434 $postarray['owner-avatar'] = $r[0]["photo"];
1436 Logger::log("No self contact for user " . $uid, Logger::DEBUG);
1440 // Don't create accounts of people who just comment something
1441 $create_user = false;
1443 $postarray['parent-uri'] = $postarray['uri'];
1444 $postarray['object-type'] = Activity\ObjectType::NOTE;
1447 if ($contactid == 0) {
1448 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1450 $postarray['owner-name'] = $post->user->name;
1451 $postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
1452 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1455 if (($contactid == 0) && !$only_existing_contact) {
1456 $contactid = $self['id'];
1457 } elseif ($contactid <= 0) {
1458 Logger::log("Contact ID is zero or less than zero.", Logger::DEBUG);
1462 $postarray['contact-id'] = $contactid;
1464 $postarray['verb'] = Activity::POST;
1465 $postarray['author-name'] = $postarray['owner-name'];
1466 $postarray['author-link'] = $postarray['owner-link'];
1467 $postarray['author-avatar'] = $postarray['owner-avatar'];
1468 $postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
1469 $postarray['app'] = strip_tags($post->source);
1471 if ($post->user->protected) {
1472 $postarray['private'] = 1;
1473 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1475 $postarray['private'] = 0;
1476 $postarray['allow_cid'] = '';
1479 if (!empty($post->full_text)) {
1480 $postarray['body'] = $post->full_text;
1482 $postarray['body'] = $post->text;
1485 // When the post contains links then use the correct object type
1486 if (count($post->entities->urls) > 0) {
1487 $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
1490 // Search for media links
1491 $picture = twitter_media_entities($post, $postarray);
1493 $converted = twitter_expand_entities($a, $postarray['body'], $post, $picture);
1494 $postarray['body'] = $converted["body"];
1495 $postarray['tag'] = $converted["tags"];
1496 $postarray['created'] = DateTimeFormat::utc($post->created_at);
1497 $postarray['edited'] = DateTimeFormat::utc($post->created_at);
1499 $statustext = $converted["plain"];
1501 if (!empty($post->place->name)) {
1502 $postarray["location"] = $post->place->name;
1504 if (!empty($post->place->full_name)) {
1505 $postarray["location"] = $post->place->full_name;
1507 if (!empty($post->geo->coordinates)) {
1508 $postarray["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
1510 if (!empty($post->coordinates->coordinates)) {
1511 $postarray["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
1513 if (!empty($post->retweeted_status)) {
1514 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1516 if (empty($retweet['body'])) {
1521 // Store the original tweet
1522 Item::insert($retweet);
1524 // CHange the other post into a reshare activity
1525 $postarray['verb'] = Activity::ANNOUNCE;
1526 $postarray['gravity'] = GRAVITY_ACTIVITY;
1527 $postarray['object-type'] = Activity\ObjectType::NOTE;
1529 $postarray['thr-parent'] = $retweet['uri'];
1530 $postarray['parent-uri'] = $retweet['uri'];
1532 $retweet['source'] = $postarray['source'];
1533 $retweet['private'] = $postarray['private'];
1534 $retweet['allow_cid'] = $postarray['allow_cid'];
1535 $retweet['contact-id'] = $postarray['contact-id'];
1536 $retweet['owner-name'] = $postarray['owner-name'];
1537 $retweet['owner-link'] = $postarray['owner-link'];
1538 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1540 $postarray = $retweet;
1544 if (!empty($post->quoted_status) && !$noquote) {
1545 $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
1547 if (!empty($quoted['body'])) {
1548 $postarray['body'] .= "\n" . share_header(
1549 $quoted['author-name'],
1550 $quoted['author-link'],
1551 $quoted['author-avatar'],
1557 $postarray['body'] .= $quoted['body'] . '[/share]';
1559 // Quoted post author is blocked/ignored, so we just provide the link to avoid removing quote context.
1560 $postarray['body'] .= "\n\nhttps://twitter.com/" . $post->quoted_status->user->screen_name . "/status/" . $post->quoted_status->id_str;
1567 function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection, array $self)
1569 Logger::log("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, Logger::DEBUG);
1573 while (!empty($post->in_reply_to_status_id_str)) {
1574 $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str, "include_ext_alt_text" => true];
1577 $post = $connection->get('statuses/show', $parameters);
1578 } catch (TwitterOAuthException $e) {
1579 Logger::log('twitter_fetchparentposts: Error fetching for user ' . $uid . ' and post ' . $post->id_str . ': ' . $e->getMessage());
1584 Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters['id'], Logger::DEBUG);
1588 if (empty($post->id_str)) {
1589 Logger::log("twitter_fetchparentposts: This is not a post " . json_encode($post), Logger::DEBUG);
1593 if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1600 Logger::log("twitter_fetchparentposts: Fetching " . count($posts) . " parents", Logger::DEBUG);
1602 $posts = array_reverse($posts);
1604 if (!empty($posts)) {
1605 foreach ($posts as $post) {
1606 $postarray = twitter_createpost($a, $uid, $post, $self, false, !PConfig::get($uid, 'twitter', 'create_user'), false);
1608 if (empty($postarray['body'])) {
1612 $item = Item::insert($postarray);
1614 $postarray["id"] = $item;
1616 Logger::log('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
1621 function twitter_fetchhometimeline(App $a, $uid)
1623 $ckey = Config::get('twitter', 'consumerkey');
1624 $csecret = Config::get('twitter', 'consumersecret');
1625 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1626 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1627 $create_user = PConfig::get($uid, 'twitter', 'create_user');
1628 $mirror_posts = PConfig::get($uid, 'twitter', 'mirror_posts');
1630 Logger::log("Fetching timeline for user " . $uid, Logger::DEBUG);
1632 $application_name = Config::get('twitter', 'application_name');
1634 if ($application_name == "") {
1635 $application_name = $a->getHostName();
1638 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1641 $own_contact = twitter_fetch_own_contact($a, $uid);
1642 } catch (TwitterOAuthException $e) {
1643 Logger::log('Error fetching own contact for user ' . $uid . ': ' . $e->getMessage());
1647 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1648 intval($own_contact),
1651 if (DBA::isResult($r)) {
1652 $own_id = $r[0]["nick"];
1654 Logger::log("Own twitter contact not found for user " . $uid);
1658 $self = User::getOwnerDataById($uid);
1659 if ($self === false) {
1660 Logger::log("Own contact not found for user " . $uid);
1664 $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
1665 //$parameters["count"] = 200;
1666 // Fetching timeline
1667 $lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid');
1669 $first_time = ($lastid == "");
1671 if ($lastid != "") {
1672 $parameters["since_id"] = $lastid;
1676 $items = $connection->get('statuses/home_timeline', $parameters);
1677 } catch (TwitterOAuthException $e) {
1678 Logger::log('Error fetching home timeline for user ' . $uid . ': ' . $e->getMessage());
1682 if (!is_array($items)) {
1683 Logger::log('No array while fetching home timeline for user ' . $uid . ': ' . print_r($items, true));
1687 if (empty($items)) {
1688 Logger::log('No new timeline content for user ' . $uid, Logger::INFO);
1692 $posts = array_reverse($items);
1694 Logger::log('Fetching timeline from ID ' . $lastid . ' for user ' . $uid . ' ' . sizeof($posts) . ' items', Logger::DEBUG);
1696 if (count($posts)) {
1697 foreach ($posts as $post) {
1698 if ($post->id_str > $lastid) {
1699 $lastid = $post->id_str;
1700 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1707 if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1708 Logger::log("Skip previously sent post", Logger::DEBUG);
1712 if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1713 Logger::log("Skip post that will be mirrored", Logger::DEBUG);
1717 if ($post->in_reply_to_status_id_str != "") {
1718 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1721 Logger::log('Preparing post ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
1723 $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1725 if (empty($postarray['body']) || trim($postarray['body']) == "") {
1726 Logger::log('Empty body for post ' . $post->id_str . ' and user ' . $uid, Logger::DEBUG);
1732 if (($postarray['uri'] == $postarray['parent-uri']) && ($postarray['author-link'] == $postarray['owner-link'])) {
1733 $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
1734 if (DBA::isResult($contact)) {
1735 $notify = Item::isRemoteSelf($contact, $postarray);
1739 $item = Item::insert($postarray, false, $notify);
1740 $postarray["id"] = $item;
1742 Logger::log('User ' . $uid . ' posted home timeline item ' . $item);
1745 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1747 Logger::log('Last timeline ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1749 // Fetching mentions
1750 $lastid = PConfig::get($uid, 'twitter', 'lastmentionid');
1752 $first_time = ($lastid == "");
1754 if ($lastid != "") {
1755 $parameters["since_id"] = $lastid;
1759 $items = $connection->get('statuses/mentions_timeline', $parameters);
1760 } catch (TwitterOAuthException $e) {
1761 Logger::log('Error fetching mentions: ' . $e->getMessage());
1765 if (!is_array($items)) {
1766 Logger::log("Error fetching mentions: " . print_r($items, true), Logger::DEBUG);
1770 $posts = array_reverse($items);
1772 Logger::log("Fetching mentions for user " . $uid . " " . sizeof($posts) . " items", Logger::DEBUG);
1774 if (count($posts)) {
1775 foreach ($posts as $post) {
1776 if ($post->id_str > $lastid) {
1777 $lastid = $post->id_str;
1784 if ($post->in_reply_to_status_id_str != "") {
1785 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1788 $postarray = twitter_createpost($a, $uid, $post, $self, false, !$create_user, false);
1790 if (empty($postarray['body'])) {
1794 $item = Item::insert($postarray);
1796 Logger::log('User ' . $uid . ' posted mention timeline item ' . $item);
1800 PConfig::set($uid, 'twitter', 'lastmentionid', $lastid);
1802 Logger::log('Last mentions ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1805 function twitter_fetch_own_contact(App $a, $uid)
1807 $ckey = Config::get('twitter', 'consumerkey');
1808 $csecret = Config::get('twitter', 'consumersecret');
1809 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1810 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1812 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1816 if ($own_id == "") {
1817 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1819 // Fetching user data
1820 // get() may throw TwitterOAuthException, but we will catch it later
1821 $user = $connection->get('account/verify_credentials');
1822 if (empty($user) || empty($user->id_str)) {
1826 PConfig::set($uid, 'twitter', 'own_id', $user->id_str);
1828 $contact_id = twitter_fetch_contact($uid, $user, true);
1830 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1832 DBA::escape("twitter::" . $own_id));
1833 if (DBA::isResult($r)) {
1834 $contact_id = $r[0]["id"];
1836 PConfig::delete($uid, 'twitter', 'own_id');
1843 function twitter_is_retweet(App $a, $uid, $body)
1845 $body = trim($body);
1847 // Skip if it isn't a pure repeated messages
1848 // Does it start with a share?
1849 if (strpos($body, "[share") > 0) {
1853 // Does it end with a share?
1854 if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
1858 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
1859 // Skip if there is no shared message in there
1860 if ($body == $attributes) {
1865 preg_match("/link='(.*?)'/ism", $attributes, $matches);
1866 if (!empty($matches[1])) {
1867 $link = $matches[1];
1870 preg_match('/link="(.*?)"/ism', $attributes, $matches);
1871 if (!empty($matches[1])) {
1872 $link = $matches[1];
1875 $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
1880 Logger::log('twitter_is_retweet: Retweeting id ' . $id . ' for user ' . $uid, Logger::DEBUG);
1882 $ckey = Config::get('twitter', 'consumerkey');
1883 $csecret = Config::get('twitter', 'consumersecret');
1884 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1885 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1887 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1888 $result = $connection->post('statuses/retweet/' . $id);
1890 Logger::log('twitter_is_retweet: result ' . print_r($result, true), Logger::DEBUG);
1892 return !isset($result->errors);
1895 function twitter_update_mentions($body)
1897 $URLSearchString = "^\[\]";
1898 $return = preg_replace_callback(
1899 "/@\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
1900 function ($matches) {
1901 if (strpos($matches[1], 'twitter.com')) {
1902 $return = '@' . substr($matches[1], strrpos($matches[1], '/') + 1);
1904 $return = $matches[2] . ' (' . $matches[1] . ')';
1915 function twitter_convert_share(array $attributes, array $author_contact, $content, $is_quote_share)
1917 if ($author_contact['network'] == Protocol::TWITTER) {
1918 $mention = '@' . $author_contact['nick'];
1920 $mention = $author_contact['addr'];
1923 return ($is_quote_share ? "\n\n" : '' ) . 'RT ' . $mention . ': ' . $content . "\n\n" . $attributes['link'];