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\Object\Image;
88 use Friendica\Util\ConfigFileLoader;
89 use Friendica\Util\DateTimeFormat;
90 use Friendica\Util\Network;
91 use Friendica\Util\Strings;
93 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
95 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
97 function twitter_install()
99 // we need some hooks, for the configuration and for sending tweets
100 Hook::register('load_config' , __FILE__, 'twitter_load_config');
101 Hook::register('connector_settings' , __FILE__, 'twitter_settings');
102 Hook::register('connector_settings_post', __FILE__, 'twitter_settings_post');
103 Hook::register('hook_fork' , __FILE__, 'twitter_hook_fork');
104 Hook::register('post_local' , __FILE__, 'twitter_post_local');
105 Hook::register('notifier_normal' , __FILE__, 'twitter_post_hook');
106 Hook::register('jot_networks' , __FILE__, 'twitter_jot_nets');
107 Hook::register('cron' , __FILE__, 'twitter_cron');
108 Hook::register('follow' , __FILE__, 'twitter_follow');
109 Hook::register('expire' , __FILE__, 'twitter_expire');
110 Hook::register('prepare_body' , __FILE__, 'twitter_prepare_body');
111 Hook::register('check_item_notification', __FILE__, 'twitter_check_item_notification');
112 Logger::log("installed twitter");
115 function twitter_uninstall()
117 Hook::unregister('load_config' , __FILE__, 'twitter_load_config');
118 Hook::unregister('connector_settings' , __FILE__, 'twitter_settings');
119 Hook::unregister('connector_settings_post', __FILE__, 'twitter_settings_post');
120 Hook::unregister('hook_fork' , __FILE__, 'twitter_hook_fork');
121 Hook::unregister('post_local' , __FILE__, 'twitter_post_local');
122 Hook::unregister('notifier_normal' , __FILE__, 'twitter_post_hook');
123 Hook::unregister('jot_networks' , __FILE__, 'twitter_jot_nets');
124 Hook::unregister('cron' , __FILE__, 'twitter_cron');
125 Hook::unregister('follow' , __FILE__, 'twitter_follow');
126 Hook::unregister('expire' , __FILE__, 'twitter_expire');
127 Hook::unregister('prepare_body' , __FILE__, 'twitter_prepare_body');
128 Hook::unregister('check_item_notification', __FILE__, 'twitter_check_item_notification');
130 // old setting - remove only
131 Hook::unregister('post_local_end' , __FILE__, 'twitter_post_hook');
132 Hook::unregister('addon_settings' , __FILE__, 'twitter_settings');
133 Hook::unregister('addon_settings_post', __FILE__, 'twitter_settings_post');
136 function twitter_load_config(App $a, ConfigFileLoader $loader)
138 $a->getConfigCache()->load($loader->loadAddonConfig('twitter'));
141 function twitter_check_item_notification(App $a, array &$notification_data)
143 $own_id = PConfig::get($notification_data["uid"], 'twitter', 'own_id');
145 $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
146 intval($notification_data["uid"]),
147 DBA::escape("twitter::".$own_id)
151 $notification_data["profiles"][] = $own_user[0]["url"];
155 function twitter_follow(App $a, array &$contact)
157 Logger::log("twitter_follow: Check if contact is twitter contact. " . $contact["url"], Logger::DEBUG);
159 if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
163 // contact seems to be a twitter contact, so continue
164 $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
165 $nickname = str_replace("@twitter.com", "", $nickname);
167 $uid = $a->user["uid"];
169 $ckey = Config::get('twitter', 'consumerkey');
170 $csecret = Config::get('twitter', 'consumersecret');
171 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
172 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
174 // If the addon is not configured (general or for this user) quit here
175 if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
180 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
181 $connection->post('friendships/create', ['screen_name' => $nickname]);
183 twitter_fetchuser($a, $uid, $nickname);
185 $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey
186 FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
188 DBA::escape($nickname));
189 if (DBA::isResult($r)) {
190 $contact["contact"] = $r[0];
194 function twitter_jot_nets(App $a, array &$jotnets_fields)
200 if (PConfig::get(local_user(), 'twitter', 'post')) {
201 $jotnets_fields[] = [
202 'type' => 'checkbox',
205 L10n::t('Post to Twitter'),
206 PConfig::get(local_user(), 'twitter', 'post_by_default')
213 function twitter_settings_post(App $a)
218 // don't check twitter settings if twitter submit button is not clicked
219 if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
223 if (!empty($_POST['twitter-disconnect'])) {
225 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
226 * from the user configuration
228 PConfig::delete(local_user(), 'twitter', 'consumerkey');
229 PConfig::delete(local_user(), 'twitter', 'consumersecret');
230 PConfig::delete(local_user(), 'twitter', 'oauthtoken');
231 PConfig::delete(local_user(), 'twitter', 'oauthsecret');
232 PConfig::delete(local_user(), 'twitter', 'post');
233 PConfig::delete(local_user(), 'twitter', 'post_by_default');
234 PConfig::delete(local_user(), 'twitter', 'lastid');
235 PConfig::delete(local_user(), 'twitter', 'mirror_posts');
236 PConfig::delete(local_user(), 'twitter', 'import');
237 PConfig::delete(local_user(), 'twitter', 'create_user');
238 PConfig::delete(local_user(), 'twitter', 'own_id');
240 if (isset($_POST['twitter-pin'])) {
241 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
242 Logger::log('got a Twitter PIN');
243 $ckey = Config::get('twitter', 'consumerkey');
244 $csecret = Config::get('twitter', 'consumersecret');
245 // the token and secret for which the PIN was generated were hidden in the settings
246 // form as token and token2, we need a new connection to Twitter using these token
247 // and secret to request a Access Token with the PIN
249 if (empty($_POST['twitter-pin'])) {
250 throw new Exception(L10n::t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.'));
253 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
254 $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]);
255 // ok, now that we have the Access Token, save them in the user config
256 PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
257 PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
258 PConfig::set(local_user(), 'twitter', 'post', 1);
259 } catch(Exception $e) {
260 info($e->getMessage());
261 } catch(TwitterOAuthException $e) {
262 info($e->getMessage());
264 // reload the Addon Settings page, if we don't do it see Bug #42
265 $a->internalRedirect('settings/connectors');
267 // if no PIN is supplied in the POST variables, the user has changed the setting
268 // to post a tweet for every new __public__ posting to the wall
269 PConfig::set(local_user(), 'twitter', 'post', intval($_POST['twitter-enable']));
270 PConfig::set(local_user(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
271 PConfig::set(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
272 PConfig::set(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
273 PConfig::set(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
275 if (!intval($_POST['twitter-mirror'])) {
276 PConfig::delete(local_user(), 'twitter', 'lastid');
279 info(L10n::t('Twitter settings updated.') . EOL);
284 function twitter_settings(App $a, &$s)
289 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
291 * 1) Check that we have global consumer key & secret
292 * 2) If no OAuthtoken & stuff is present, generate button to get some
293 * 3) Checkbox for "Send public notices (280 chars only)
295 $ckey = Config::get('twitter', 'consumerkey');
296 $csecret = Config::get('twitter', 'consumersecret');
297 $otoken = PConfig::get(local_user(), 'twitter', 'oauthtoken');
298 $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret');
300 $enabled = intval(PConfig::get(local_user(), 'twitter', 'post'));
301 $defenabled = intval(PConfig::get(local_user(), 'twitter', 'post_by_default'));
302 $mirrorenabled = intval(PConfig::get(local_user(), 'twitter', 'mirror_posts'));
303 $importenabled = intval(PConfig::get(local_user(), 'twitter', 'import'));
304 $create_userenabled = intval(PConfig::get(local_user(), 'twitter', 'create_user'));
306 $css = (($enabled) ? '' : '-disabled');
308 $s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
309 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
311 $s .= '<div id="settings_twitter_expanded" class="settings-block" style="display: none;">';
312 $s .= '<span class="fakelink" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
313 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
316 if ((!$ckey) && (!$csecret)) {
317 /* no global consumer keys
318 * display warning and skip personal config
320 $s .= '<p>' . L10n::t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
322 // ok we have a consumer key pair now look into the OAuth stuff
323 if ((!$otoken) && (!$osecret)) {
324 /* the user has not yet connected the account to twitter...
325 * get a temporary OAuth key/secret pair and display a button with
326 * which the user can request a PIN to connect the account to a
327 * account at Twitter.
329 $connection = new TwitterOAuth($ckey, $csecret);
331 $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
332 $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>';
333 $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>';
334 $s .= '<div id="twitter-pin-wrapper">';
335 $s .= '<label id="twitter-pin-label" for="twitter-pin">' . L10n::t('Copy the PIN from Twitter here') . '</label>';
336 $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
337 $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result['oauth_token'] . '" />';
338 $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result['oauth_token_secret'] . '" />';
339 $s .= '</div><div class="clear"></div>';
340 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
341 } catch (TwitterOAuthException $e) {
342 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
346 * we have an OAuth key / secret pair for the user
347 * so let's give a chance to disable the postings to Twitter
349 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
351 $details = $connection->get('account/verify_credentials');
353 $field_checkbox = Renderer::getMarkupTemplate('field_checkbox.tpl');
355 $s .= '<div id="twitter-info" >
356 <p>' . L10n::t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
357 <button type="submit" name="twitter-disconnect" value="1">' . L10n::t('Disconnect') . '</button>
359 <p id="twitter-info-block">
360 <a href="https://twitter.com/' . $details->screen_name . '" target="_twitter"><img id="twitter-avatar" src="' . $details->profile_image_url . '" /></a>
361 <em>' . $details->description . '</em>
364 $s .= '<div class="clear"></div>';
366 $s .= Renderer::replaceMacros($field_checkbox, [
367 '$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.')]
369 if ($a->user['hidewall']) {
370 $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>';
372 $s .= Renderer::replaceMacros($field_checkbox, [
373 '$field' => ['twitter-default', L10n::t('Send public postings to Twitter by default'), $defenabled, '']
375 $s .= Renderer::replaceMacros($field_checkbox, [
376 '$field' => ['twitter-mirror', L10n::t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
378 $s .= Renderer::replaceMacros($field_checkbox, [
379 '$field' => ['twitter-import', L10n::t('Import the remote timeline'), $importenabled, '']
381 $s .= Renderer::replaceMacros($field_checkbox, [
382 '$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.')]
384 $s .= '<div class="clear"></div>';
385 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
386 } catch (TwitterOAuthException $e) {
387 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
391 $s .= '</div><div class="clear"></div>';
394 function twitter_hook_fork(App $a, array &$b)
396 if ($b['name'] != 'notifier_normal') {
402 // Deleting and editing is not supported by the addon (deleting could, but isn't by now)
403 if ($post['deleted'] || ($post['created'] !== $post['edited'])) {
404 $b['execute'] = false;
408 // if post comes from twitter don't send it back
409 if ($post['extid'] == Protocol::TWITTER) {
410 $b['execute'] = false;
414 if ($post['app'] == 'Twitter') {
415 $b['execute'] = false;
419 if (PConfig::get($post['uid'], 'twitter', 'import')) {
420 // Don't fork if it isn't a reply to a twitter post
421 if (($post['parent'] != $post['id']) && !Item::exists(['id' => $post['parent'], 'network' => Protocol::TWITTER])) {
422 Logger::log('No twitter parent found for item ' . $post['id']);
423 $b['execute'] = false;
427 // Comments are never exported when we don't import the twitter timeline
428 if (!strstr($post['postopts'], 'twitter') || ($post['parent'] != $post['id']) || $post['private']) {
429 $b['execute'] = false;
435 function twitter_post_local(App $a, array &$b)
441 if (!local_user() || (local_user() != $b['uid'])) {
445 $twitter_post = intval(PConfig::get(local_user(), 'twitter', 'post'));
446 $twitter_enable = (($twitter_post && !empty($_REQUEST['twitter_enable'])) ? intval($_REQUEST['twitter_enable']) : 0);
448 // if API is used, default to the chosen settings
449 if ($b['api_source'] && intval(PConfig::get(local_user(), 'twitter', 'post_by_default'))) {
453 if (!$twitter_enable) {
457 if (strlen($b['postopts'])) {
458 $b['postopts'] .= ',';
461 $b['postopts'] .= 'twitter';
464 function twitter_action(App $a, $uid, $pid, $action)
466 $ckey = Config::get('twitter', 'consumerkey');
467 $csecret = Config::get('twitter', 'consumersecret');
468 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
469 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
471 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
473 $post = ['id' => $pid];
475 Logger::log("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), Logger::DATA);
479 // To-Do: $result = $connection->post('statuses/destroy', $post);
483 $result = $connection->post('favorites/create', $post);
486 $result = $connection->post('favorites/destroy', $post);
489 Logger::log('Unhandled action ' . $action, Logger::DEBUG);
492 Logger::log("twitter_action '" . $action . "' send, result: " . print_r($result, true), Logger::DEBUG);
495 function twitter_post_hook(App $a, array &$b)
498 if (!PConfig::get($b["uid"], 'twitter', 'import')
499 && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
503 if ($b['parent'] != $b['id']) {
504 Logger::log("twitter_post_hook: parameter " . print_r($b, true), Logger::DATA);
506 // Looking if its a reply to a twitter post
507 if ((substr($b["parent-uri"], 0, 9) != "twitter::")
508 && (substr($b["extid"], 0, 9) != "twitter::")
509 && (substr($b["thr-parent"], 0, 9) != "twitter::"))
511 Logger::log("twitter_post_hook: no twitter post " . $b["parent"]);
515 $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
516 $orig_post = Item::selectFirst([], $condition);
517 if (!DBA::isResult($orig_post)) {
518 Logger::log("twitter_post_hook: no parent found " . $b["thr-parent"]);
525 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
526 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
527 $nicknameplain = "@" . $nicknameplain;
529 Logger::log("twitter_post_hook: comparing " . $nickname . " and " . $nicknameplain . " with " . $b["body"], Logger::DEBUG);
530 if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
531 $b["body"] = $nickname . " " . $b["body"];
534 Logger::log("twitter_post_hook: parent found " . print_r($orig_post, true), Logger::DATA);
538 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
542 // Dont't post if the post doesn't belong to us.
543 // This is a check for forum postings
544 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
545 if ($b['contact-id'] != $self['id']) {
550 if (($b['verb'] == ACTIVITY_POST) && $b['deleted']) {
551 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
554 if ($b['verb'] == ACTIVITY_LIKE) {
555 Logger::log("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), Logger::DEBUG);
557 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
559 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
565 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
569 // if post comes from twitter don't send it back
570 if ($b['extid'] == Protocol::TWITTER) {
574 if ($b['app'] == "Twitter") {
578 Logger::notice('twitter post invoked', ['id' => $b['id'], 'guid' => $b['guid']]);
580 PConfig::load($b['uid'], 'twitter');
582 $ckey = Config::get('twitter', 'consumerkey');
583 $csecret = Config::get('twitter', 'consumersecret');
584 $otoken = PConfig::get($b['uid'], 'twitter', 'oauthtoken');
585 $osecret = PConfig::get($b['uid'], 'twitter', 'oauthsecret');
587 if ($ckey && $csecret && $otoken && $osecret) {
588 Logger::log('twitter: we have customer key and oauth stuff, going to send.', Logger::DEBUG);
590 // If it's a repeated message from twitter then do a native retweet and exit
591 if (twitter_is_retweet($a, $b['uid'], $b['body'])) {
595 Codebird::setConsumerKey($ckey, $csecret);
596 $cb = Codebird::getInstance();
597 $cb->setToken($otoken, $osecret);
599 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
601 // Set the timeout for upload to 30 seconds
602 $connection->setTimeouts(10, 30);
606 // Handling non-native reshares
607 $b['body'] = Friendica\Content\Text\BBCode::convertShare(
609 function (array $attributes, array $author_contact, $content, $is_quote_share) {
610 return twitter_convert_share($attributes, $author_contact, $content, $is_quote_share);
614 $b['body'] = twitter_update_mentions($b['body']);
616 $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
617 Logger::info('Got plaintext', $msgarr);
618 $msg = $msgarr["text"];
620 if (($msg == "") && isset($msgarr["title"])) {
621 $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
626 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
627 $msg .= "\n" . $msgarr["url"];
633 if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
634 $image = $msgarr["image"];
641 // and now tweet it :-)
644 if (!empty($image)) {
646 $img_str = Network::fetchUrl($image);
648 $tempfile = tempnam(get_temppath(), 'cache');
649 file_put_contents($tempfile, $img_str);
651 $media = $connection->upload('media/upload', ['media' => $tempfile]);
655 if (isset($media->media_id_string)) {
656 $post['media_ids'] = $media->media_id_string;
657 //$details = $cb->account_verifyCredentials();
659 throw new Exception('Failed upload of ' . $image);
661 } catch (Exception $e) {
662 Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage());
664 // Workaround: Remove the picture link so that the post can be reposted without it
665 // When there is another url already added, a second url would be superfluous.
667 $msg .= "\n" . $image;
674 $post['status'] = $msg;
677 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
680 $url = 'statuses/update';
681 $result = $connection->post($url, $post);
682 Logger::log('twitter_post send, result: ' . print_r($result, true), Logger::DEBUG);
684 if (!empty($result->source)) {
685 Config::set("twitter", "application_name", strip_tags($result->source));
688 if (!empty($result->errors)) {
689 Logger::log('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
691 } elseif ($iscomment) {
692 Logger::log('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
693 Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
698 function twitter_addon_admin_post(App $a)
700 $consumerkey = !empty($_POST['consumerkey']) ? Strings::escapeTags(trim($_POST['consumerkey'])) : '';
701 $consumersecret = !empty($_POST['consumersecret']) ? Strings::escapeTags(trim($_POST['consumersecret'])) : '';
702 Config::set('twitter', 'consumerkey', $consumerkey);
703 Config::set('twitter', 'consumersecret', $consumersecret);
704 info(L10n::t('Settings updated.') . EOL);
707 function twitter_addon_admin(App $a, &$o)
709 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/twitter/");
711 $o = Renderer::replaceMacros($t, [
712 '$submit' => L10n::t('Save Settings'),
713 // name, label, value, help, [extra values]
714 '$consumerkey' => ['consumerkey', L10n::t('Consumer key'), Config::get('twitter', 'consumerkey'), ''],
715 '$consumersecret' => ['consumersecret', L10n::t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''],
719 function twitter_cron(App $a)
721 $last = Config::get('twitter', 'last_poll');
723 $poll_interval = intval(Config::get('twitter', 'poll_interval'));
724 if (!$poll_interval) {
725 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
729 $next = $last + ($poll_interval * 60);
730 if ($next > time()) {
731 Logger::log('twitter: poll intervall not reached');
735 Logger::log('twitter: cron_start');
737 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
738 if (DBA::isResult($r)) {
739 foreach ($r as $rr) {
740 Logger::log('twitter: fetching for user ' . $rr['uid']);
741 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
745 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
746 if ($abandon_days < 1) {
750 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
752 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
753 if (DBA::isResult($r)) {
754 foreach ($r as $rr) {
755 if ($abandon_days != 0) {
756 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
757 if (!DBA::isResult($user)) {
758 Logger::log('abandoned account: timeline from user ' . $rr['uid'] . ' will not be imported');
763 Logger::log('twitter: importing timeline from user ' . $rr['uid']);
764 Worker::add(['priority' => PRIORITY_MEDIUM, 'force_priority' => true], "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
767 // check for new contacts once a day
768 $last_contact_check = PConfig::get($rr['uid'],'pumpio','contact_check');
769 if($last_contact_check)
770 $next_contact_check = $last_contact_check + 86400;
772 $next_contact_check = 0;
774 if($next_contact_check <= time()) {
775 pumpio_getallusers($a, $rr["uid"]);
776 PConfig::set($rr['uid'],'pumpio','contact_check',time());
782 Logger::log('twitter: cron_end');
784 Config::set('twitter', 'last_poll', time());
787 function twitter_expire(App $a)
789 $days = Config::get('twitter', 'expire');
795 $r = Item::select(['id'], ['deleted' => true, 'network' => Protocol::TWITTER]);
796 while ($row = DBA::fetch($r)) {
797 DBA::delete('item', ['id' => $row['id']]);
801 Logger::log('twitter_expire: expire_start');
803 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
804 if (DBA::isResult($r)) {
805 foreach ($r as $rr) {
806 Logger::log('twitter_expire: user ' . $rr['uid']);
807 Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
811 Logger::log('twitter_expire: expire_end');
814 function twitter_prepare_body(App $a, array &$b)
816 if ($b["item"]["network"] != Protocol::TWITTER) {
823 $item["plink"] = $a->getBaseURL() . "/display/" . $item["guid"];
825 $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
826 $orig_post = Item::selectFirst(['author-link'], $condition);
827 if (DBA::isResult($orig_post)) {
828 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
829 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
830 $nicknameplain = "@" . $nicknameplain;
832 if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) {
833 $item["body"] = $nickname . " " . $item["body"];
837 $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, 8);
838 $msg = $msgarr["text"];
840 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
841 $msg .= " " . $msgarr["url"];
844 if (isset($msgarr["image"])) {
845 $msg .= " " . $msgarr["image"];
848 $b['html'] = nl2br(htmlspecialchars($msg));
853 * @brief Build the item array for the mirrored post
855 * @param App $a Application class
856 * @param integer $uid User id
857 * @param object $post Twitter object with the post
859 * @return array item data to be posted
861 function twitter_do_mirrorpost(App $a, $uid, $post)
863 $datarray['api_source'] = true;
864 $datarray['profile_uid'] = $uid;
865 $datarray['extid'] = Protocol::TWITTER;
866 $datarray['message_id'] = Item::newURI($uid, Protocol::TWITTER . ':' . $post->id);
867 $datarray['protocol'] = Conversation::PARCEL_TWITTER;
868 $datarray['source'] = json_encode($post);
869 $datarray['title'] = '';
871 if (!empty($post->retweeted_status)) {
872 // We don't support nested shares, so we mustn't show quotes as shares on retweets
873 $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true);
875 if (empty($item['body'])) {
879 $datarray['body'] = "\n" . share_header(
880 $item['author-name'],
881 $item['author-link'],
882 $item['author-avatar'],
888 $datarray['body'] .= $item['body'] . '[/share]';
890 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
892 if (empty($item['body'])) {
896 $datarray['body'] = $item['body'];
899 $datarray['source'] = $item['app'];
900 $datarray['verb'] = $item['verb'];
902 if (isset($item['location'])) {
903 $datarray['location'] = $item['location'];
906 if (isset($item['coord'])) {
907 $datarray['coord'] = $item['coord'];
913 function twitter_fetchtimeline(App $a, $uid)
915 $ckey = Config::get('twitter', 'consumerkey');
916 $csecret = Config::get('twitter', 'consumersecret');
917 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
918 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
919 $lastid = PConfig::get($uid, 'twitter', 'lastid');
921 $application_name = Config::get('twitter', 'application_name');
923 if ($application_name == "") {
924 $application_name = $a->getHostName();
927 $has_picture = false;
929 require_once 'mod/item.php';
930 require_once 'mod/share.php';
932 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
934 $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
936 $first_time = ($lastid == "");
939 $parameters["since_id"] = $lastid;
943 $items = $connection->get('statuses/user_timeline', $parameters);
944 } catch (TwitterOAuthException $e) {
945 Logger::log('Error fetching timeline for user ' . $uid . ': ' . $e->getMessage());
949 if (!is_array($items)) {
950 Logger::log('No items for user ' . $uid, Logger::INFO);
954 $posts = array_reverse($items);
956 Logger::log('Starting from ID ' . $lastid . ' for user ' . $uid, Logger::DEBUG);
959 foreach ($posts as $post) {
960 if ($post->id_str > $lastid) {
961 $lastid = $post->id_str;
962 PConfig::set($uid, 'twitter', 'lastid', $lastid);
969 if (!stristr($post->source, $application_name)) {
970 $_SESSION["authenticated"] = true;
971 $_SESSION["uid"] = $uid;
973 Logger::log('Preparing Twitter ID ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
975 $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
977 if (empty($_REQUEST['body'])) {
981 Logger::log('Posting Twitter ID ' . $post->id_str . ' for user ' . $uid);
987 PConfig::set($uid, 'twitter', 'lastid', $lastid);
988 Logger::log('Last ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
991 function twitter_fix_avatar($avatar)
993 $new_avatar = str_replace("_normal.", ".", $avatar);
995 $info = Image::getInfoFromURL($new_avatar);
997 $new_avatar = $avatar;
1003 function twitter_fetch_contact($uid, $data, $create_user)
1005 if (empty($data->id_str)) {
1009 $avatar = twitter_fix_avatar($data->profile_image_url_https);
1010 $url = "https://twitter.com/" . $data->screen_name;
1011 $addr = $data->screen_name . "@twitter.com";
1013 $fields = ['url' => $url, 'network' => Protocol::TWITTER,
1014 'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
1015 'location' => $data->location, 'about' => $data->description];
1017 $cid = Contact::getIdForURL($url, 0, true, $fields);
1019 DBA::update('contact', $fields, ['id' => $cid]);
1020 Contact::updateAvatar($avatar, 0, $cid);
1023 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
1024 if (!DBA::isResult($contact) && !$create_user) {
1028 if (!DBA::isResult($contact)) {
1029 // create contact record
1030 $fields['uid'] = $uid;
1031 $fields['created'] = DateTimeFormat::utcNow();
1032 $fields['nurl'] = Strings::normaliseLink($url);
1033 $fields['alias'] = 'twitter::' . $data->id_str;
1034 $fields['poll'] = 'twitter::' . $data->id_str;
1035 $fields['rel'] = Contact::FRIEND;
1036 $fields['priority'] = 1;
1037 $fields['writable'] = true;
1038 $fields['blocked'] = false;
1039 $fields['readonly'] = false;
1040 $fields['pending'] = false;
1042 if (!DBA::insert('contact', $fields)) {
1046 $contact_id = DBA::lastInsertId();
1048 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1050 Contact::updateAvatar($avatar, $uid, $contact_id);
1052 if ($contact["readonly"] || $contact["blocked"]) {
1053 Logger::log("twitter_fetch_contact: Contact '" . $contact["nick"] . "' is blocked or readonly.", Logger::DEBUG);
1057 $contact_id = $contact['id'];
1059 // update profile photos once every twelve hours as we have no notification of when they change.
1060 $update_photo = ($contact['avatar-date'] < DateTimeFormat::utc('now -12 hours'));
1062 // check that we have all the photos, this has been known to fail on occasion
1063 if (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']) || $update_photo) {
1064 Logger::log("twitter_fetch_contact: Updating contact " . $data->screen_name, Logger::DEBUG);
1066 Contact::updateAvatar($avatar, $uid, $contact['id']);
1068 $fields['name-date'] = DateTimeFormat::utcNow();
1069 $fields['uri-date'] = DateTimeFormat::utcNow();
1071 DBA::update('contact', $fields, ['id' => $contact['id']]);
1078 function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
1080 $ckey = Config::get('twitter', 'consumerkey');
1081 $csecret = Config::get('twitter', 'consumersecret');
1082 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1083 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1085 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1088 if (DBA::isResult($r)) {
1096 if ($screen_name != "") {
1097 $parameters["screen_name"] = $screen_name;
1100 if ($user_id != "") {
1101 $parameters["user_id"] = $user_id;
1104 // Fetching user data
1105 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1107 $user = $connection->get('users/show', $parameters);
1108 } catch (TwitterOAuthException $e) {
1109 Logger::log('twitter_fetchuser: Error fetching user ' . $uid . ': ' . $e->getMessage());
1113 if (!is_object($user)) {
1117 $contact_id = twitter_fetch_contact($uid, $user, true);
1122 function twitter_expand_entities(App $a, $body, $item, $picture)
1128 foreach ($item->entities->hashtags AS $hashtag) {
1129 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . $hashtag->text . ']' . $hashtag->text . '[/url]';
1130 $tags_arr['#' . $hashtag->text] = $url;
1131 $body = str_replace('#' . $hashtag->text, $url, $body);
1134 foreach ($item->entities->user_mentions AS $mention) {
1135 $url = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
1136 $tags_arr['@' . $mention->screen_name] = $url;
1137 $body = str_replace('@' . $mention->screen_name, $url, $body);
1140 if (isset($item->entities->urls)) {
1146 foreach ($item->entities->urls as $url) {
1147 $plain = str_replace($url->url, '', $plain);
1149 if ($url->url && $url->expanded_url && $url->display_url) {
1150 // Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
1151 if (isset($item->quoted_status_id_str)
1152 && substr($url->expanded_url, -strlen($item->quoted_status_id_str)) == $item->quoted_status_id_str ) {
1153 $body = str_replace($url->url, '', $body);
1157 $expanded_url = $url->expanded_url;
1159 $final_url = Network::finalUrl($url->expanded_url);
1161 $oembed_data = OEmbed::fetchURL($final_url);
1163 if (empty($oembed_data) || empty($oembed_data->type)) {
1167 // Quickfix: Workaround for URL with '[' and ']' in it
1168 if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
1169 $expanded_url = $url->url;
1173 $type = $oembed_data->type;
1176 if ($oembed_data->type == 'video') {
1177 $type = $oembed_data->type;
1178 $footerurl = $expanded_url;
1179 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1181 $body = str_replace($url->url, $footerlink, $body);
1182 } elseif (($oembed_data->type == 'photo') && isset($oembed_data->url)) {
1183 $body = str_replace($url->url, '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]', $body);
1184 } elseif ($oembed_data->type != 'link') {
1185 $body = str_replace($url->url, '[url=' . $expanded_url . ']' . $url->display_url . '[/url]', $body);
1187 $img_str = Network::fetchUrl($final_url, true, 4);
1189 $tempfile = tempnam(get_temppath(), 'cache');
1190 file_put_contents($tempfile, $img_str);
1192 // See http://php.net/manual/en/function.exif-imagetype.php#79283
1193 if (filesize($tempfile) > 11) {
1194 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
1201 if (substr($mime, 0, 6) == 'image/') {
1203 $body = str_replace($url->url, '[img]' . $final_url . '[/img]', $body);
1205 $type = $oembed_data->type;
1206 $footerurl = $expanded_url;
1207 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1209 $body = str_replace($url->url, $footerlink, $body);
1215 // Footer will be taken care of with a share block in the case of a quote
1216 if (empty($item->quoted_status)) {
1217 if ($footerurl != '') {
1218 $footer = add_page_info($footerurl, false, $picture);
1221 if (($footerlink != '') && (trim($footer) != '')) {
1222 $removedlink = trim(str_replace($footerlink, '', $body));
1224 if (($removedlink == '') || strstr($body, $removedlink)) {
1225 $body = $removedlink;
1231 if ($footer == '' && $picture != '') {
1232 $body .= "\n\n[img]" . $picture . "[/img]\n";
1233 } elseif ($footer == '' && $picture == '') {
1234 $body = add_page_info_to_body($body);
1239 // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
1240 $tags = BBCode::getTags($body);
1243 foreach ($tags as $tag) {
1244 if (strstr(trim($tag), ' ')) {
1248 if (strpos($tag, '#') === 0) {
1249 if (strpos($tag, '[url=')) {
1253 // don't link tags that are already embedded in links
1254 if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
1257 if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
1261 $basetag = str_replace('_', ' ', substr($tag, 1));
1262 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . $basetag . ']' . $basetag . '[/url]';
1263 $body = str_replace($tag, $url, $body);
1264 $tags_arr['#' . $basetag] = $url;
1265 } elseif (strpos($tag, '@') === 0) {
1266 if (strpos($tag, '[url=')) {
1270 $basetag = substr($tag, 1);
1271 $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1272 $body = str_replace($tag, $url, $body);
1273 $tags_arr['@' . $basetag] = $url;
1278 $tags = implode($tags_arr, ',');
1280 return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
1284 * @brief Fetch media entities and add media links to the body
1286 * @param object $post Twitter object with the post
1287 * @param array $postarray Array of the item that is about to be posted
1289 * @return $picture string Image URL or empty string
1291 function twitter_media_entities($post, array &$postarray)
1293 // There are no media entities? So we quit.
1294 if (empty($post->extended_entities->media)) {
1298 // When the post links to an external page, we only take one picture.
1299 // We only do this when there is exactly one media.
1300 if ((count($post->entities->urls) > 0) && (count($post->extended_entities->media) == 1)) {
1301 $medium = $post->extended_entities->media[0];
1303 foreach ($post->entities->urls as $link) {
1304 // Let's make sure the external link url matches the media url
1305 if ($medium->url == $link->url && isset($medium->media_url_https)) {
1306 $picture = $medium->media_url_https;
1307 $postarray['body'] = str_replace($medium->url, '', $postarray['body']);
1313 // This is a pure media post, first search for all media urls
1315 foreach ($post->extended_entities->media AS $medium) {
1316 if (!isset($media[$medium->url])) {
1317 $media[$medium->url] = '';
1319 switch ($medium->type) {
1321 if (!empty($medium->ext_alt_text)) {
1322 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1323 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1325 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1328 $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1331 case 'animated_gif':
1332 if (!empty($medium->ext_alt_text)) {
1333 Logger::info('Got text description', ['alt_text' => $medium->ext_alt_text]);
1334 $media[$medium->url] .= "\n[img=" . $medium->media_url_https .']' . $medium->ext_alt_text . '[/img]';
1336 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1339 $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
1340 if (is_array($medium->video_info->variants)) {
1342 // We take the video with the highest bitrate
1343 foreach ($medium->video_info->variants AS $variant) {
1344 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1345 $media[$medium->url] = "\n[video]" . $variant->url . '[/video]';
1346 $bitrate = $variant->bitrate;
1351 // The following code will only be activated for test reasons
1353 // $postarray['body'] .= print_r($medium, true);
1357 // Now we replace the media urls.
1358 foreach ($media AS $key => $value) {
1359 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1365 function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $only_existing_contact, $noquote)
1368 $postarray['network'] = Protocol::TWITTER;
1369 $postarray['uid'] = $uid;
1370 $postarray['wall'] = 0;
1371 $postarray['uri'] = "twitter::" . $post->id_str;
1372 $postarray['protocol'] = Conversation::PARCEL_TWITTER;
1373 $postarray['source'] = json_encode($post);
1375 // Don't import our own comments
1376 if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
1377 Logger::log("Item with extid " . $postarray['uri'] . " found.", Logger::DEBUG);
1383 if ($post->in_reply_to_status_id_str != "") {
1384 $parent = "twitter::" . $post->in_reply_to_status_id_str;
1386 $fields = ['uri', 'parent-uri', 'parent'];
1387 $parent_item = Item::selectFirst($fields, ['uri' => $parent, 'uid' => $uid]);
1388 if (!DBA::isResult($parent_item)) {
1389 $parent_item = Item::selectFirst($fields, ['extid' => $parent, 'uid' => $uid]);
1392 if (DBA::isResult($parent_item)) {
1393 $postarray['thr-parent'] = $parent_item['uri'];
1394 $postarray['parent-uri'] = $parent_item['parent-uri'];
1395 $postarray['parent'] = $parent_item['parent'];
1396 $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1398 $postarray['thr-parent'] = $postarray['uri'];
1399 $postarray['parent-uri'] = $postarray['uri'];
1400 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1404 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1406 if ($post->user->id_str == $own_id) {
1407 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1410 if (DBA::isResult($r)) {
1411 $contactid = $r[0]["id"];
1413 $postarray['owner-name'] = $r[0]["name"];
1414 $postarray['owner-link'] = $r[0]["url"];
1415 $postarray['owner-avatar'] = $r[0]["photo"];
1417 Logger::log("No self contact for user " . $uid, Logger::DEBUG);
1421 // Don't create accounts of people who just comment something
1422 $create_user = false;
1424 $postarray['parent-uri'] = $postarray['uri'];
1425 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1428 if ($contactid == 0) {
1429 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1431 $postarray['owner-name'] = $post->user->name;
1432 $postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
1433 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1436 if (($contactid == 0) && !$only_existing_contact) {
1437 $contactid = $self['id'];
1438 } elseif ($contactid <= 0) {
1439 Logger::log("Contact ID is zero or less than zero.", Logger::DEBUG);
1443 $postarray['contact-id'] = $contactid;
1445 $postarray['verb'] = ACTIVITY_POST;
1446 $postarray['author-name'] = $postarray['owner-name'];
1447 $postarray['author-link'] = $postarray['owner-link'];
1448 $postarray['author-avatar'] = $postarray['owner-avatar'];
1449 $postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
1450 $postarray['app'] = strip_tags($post->source);
1452 if ($post->user->protected) {
1453 $postarray['private'] = 1;
1454 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1456 $postarray['private'] = 0;
1457 $postarray['allow_cid'] = '';
1460 if (!empty($post->full_text)) {
1461 $postarray['body'] = $post->full_text;
1463 $postarray['body'] = $post->text;
1466 // When the post contains links then use the correct object type
1467 if (count($post->entities->urls) > 0) {
1468 $postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
1471 // Search for media links
1472 $picture = twitter_media_entities($post, $postarray);
1474 $converted = twitter_expand_entities($a, $postarray['body'], $post, $picture);
1475 $postarray['body'] = $converted["body"];
1476 $postarray['tag'] = $converted["tags"];
1477 $postarray['created'] = DateTimeFormat::utc($post->created_at);
1478 $postarray['edited'] = DateTimeFormat::utc($post->created_at);
1480 $statustext = $converted["plain"];
1482 if (!empty($post->place->name)) {
1483 $postarray["location"] = $post->place->name;
1485 if (!empty($post->place->full_name)) {
1486 $postarray["location"] = $post->place->full_name;
1488 if (!empty($post->geo->coordinates)) {
1489 $postarray["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
1491 if (!empty($post->coordinates->coordinates)) {
1492 $postarray["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
1494 if (!empty($post->retweeted_status)) {
1495 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1497 if (empty($retweet['body'])) {
1502 // Store the original tweet
1503 Item::insert($retweet);
1505 // CHange the other post into a reshare activity
1506 $postarray['verb'] = ACTIVITY2_ANNOUNCE;
1507 $postarray['gravity'] = GRAVITY_ACTIVITY;
1508 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1510 $postarray['thr-parent'] = $retweet['uri'];
1511 $postarray['parent-uri'] = $retweet['uri'];
1513 $retweet['source'] = $postarray['source'];
1514 $retweet['private'] = $postarray['private'];
1515 $retweet['allow_cid'] = $postarray['allow_cid'];
1516 $retweet['contact-id'] = $postarray['contact-id'];
1517 $retweet['owner-name'] = $postarray['owner-name'];
1518 $retweet['owner-link'] = $postarray['owner-link'];
1519 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1521 $postarray = $retweet;
1525 if (!empty($post->quoted_status) && !$noquote) {
1526 $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
1528 if (empty($quoted['body'])) {
1532 $postarray['body'] .= "\n" . share_header(
1533 $quoted['author-name'],
1534 $quoted['author-link'],
1535 $quoted['author-avatar'],
1541 $postarray['body'] .= $quoted['body'] . '[/share]';
1547 function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection, array $self)
1549 Logger::log("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, Logger::DEBUG);
1553 while (!empty($post->in_reply_to_status_id_str)) {
1554 $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str, "include_ext_alt_text" => true];
1557 $post = $connection->get('statuses/show', $parameters);
1558 } catch (TwitterOAuthException $e) {
1559 Logger::log('twitter_fetchparentposts: Error fetching for user ' . $uid . ' and post ' . $post->id_str . ': ' . $e->getMessage());
1564 Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters['id'], Logger::DEBUG);
1568 if (empty($post->id_str)) {
1569 Logger::log("twitter_fetchparentposts: This is not a post " . json_encode($post), Logger::DEBUG);
1573 if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1580 Logger::log("twitter_fetchparentposts: Fetching " . count($posts) . " parents", Logger::DEBUG);
1582 $posts = array_reverse($posts);
1584 if (!empty($posts)) {
1585 foreach ($posts as $post) {
1586 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1588 if (empty($postarray['body'])) {
1592 $item = Item::insert($postarray);
1594 $postarray["id"] = $item;
1596 Logger::log('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
1601 function twitter_fetchhometimeline(App $a, $uid)
1603 $ckey = Config::get('twitter', 'consumerkey');
1604 $csecret = Config::get('twitter', 'consumersecret');
1605 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1606 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1607 $create_user = PConfig::get($uid, 'twitter', 'create_user');
1608 $mirror_posts = PConfig::get($uid, 'twitter', 'mirror_posts');
1610 Logger::log("Fetching timeline for user " . $uid, Logger::DEBUG);
1612 $application_name = Config::get('twitter', 'application_name');
1614 if ($application_name == "") {
1615 $application_name = $a->getHostName();
1618 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1621 $own_contact = twitter_fetch_own_contact($a, $uid);
1622 } catch (TwitterOAuthException $e) {
1623 Logger::log('Error fetching own contact for user ' . $uid . ': ' . $e->getMessage());
1627 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1628 intval($own_contact),
1631 if (DBA::isResult($r)) {
1632 $own_id = $r[0]["nick"];
1634 Logger::log("Own twitter contact not found for user " . $uid);
1638 $self = User::getOwnerDataById($uid);
1639 if ($self === false) {
1640 Logger::log("Own contact not found for user " . $uid);
1644 $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended", "include_ext_alt_text" => true];
1645 //$parameters["count"] = 200;
1646 // Fetching timeline
1647 $lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid');
1649 $first_time = ($lastid == "");
1651 if ($lastid != "") {
1652 $parameters["since_id"] = $lastid;
1656 $items = $connection->get('statuses/home_timeline', $parameters);
1657 } catch (TwitterOAuthException $e) {
1658 Logger::log('Error fetching home timeline for user ' . $uid . ': ' . $e->getMessage());
1662 if (!is_array($items)) {
1663 Logger::log('No array while fetching home timeline for user ' . $uid . ': ' . print_r($items, true));
1667 if (empty($items)) {
1668 Logger::log('No new timeline content for user ' . $uid, Logger::INFO);
1672 $posts = array_reverse($items);
1674 Logger::log('Fetching timeline from ID ' . $lastid . ' for user ' . $uid . ' ' . sizeof($posts) . ' items', Logger::DEBUG);
1676 if (count($posts)) {
1677 foreach ($posts as $post) {
1678 if ($post->id_str > $lastid) {
1679 $lastid = $post->id_str;
1680 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1687 if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1688 Logger::log("Skip previously sent post", Logger::DEBUG);
1692 if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1693 Logger::log("Skip post that will be mirrored", Logger::DEBUG);
1697 if ($post->in_reply_to_status_id_str != "") {
1698 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1701 Logger::log('Preparing post ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
1703 $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1705 if (empty($postarray['body']) || trim($postarray['body']) == "") {
1706 Logger::log('Empty body for post ' . $post->id_str . ' and user ' . $uid, Logger::DEBUG);
1712 if (($postarray['uri'] == $postarray['parent-uri']) && ($postarray['author-link'] == $postarray['owner-link'])) {
1713 $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
1714 if (DBA::isResult($contact)) {
1715 $notify = Item::isRemoteSelf($contact, $postarray);
1719 $item = Item::insert($postarray, false, $notify);
1720 $postarray["id"] = $item;
1722 Logger::log('User ' . $uid . ' posted home timeline item ' . $item);
1725 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1727 Logger::log('Last timeline ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1729 // Fetching mentions
1730 $lastid = PConfig::get($uid, 'twitter', 'lastmentionid');
1732 $first_time = ($lastid == "");
1734 if ($lastid != "") {
1735 $parameters["since_id"] = $lastid;
1739 $items = $connection->get('statuses/mentions_timeline', $parameters);
1740 } catch (TwitterOAuthException $e) {
1741 Logger::log('Error fetching mentions: ' . $e->getMessage());
1745 if (!is_array($items)) {
1746 Logger::log("Error fetching mentions: " . print_r($items, true), Logger::DEBUG);
1750 $posts = array_reverse($items);
1752 Logger::log("Fetching mentions for user " . $uid . " " . sizeof($posts) . " items", Logger::DEBUG);
1754 if (count($posts)) {
1755 foreach ($posts as $post) {
1756 if ($post->id_str > $lastid) {
1757 $lastid = $post->id_str;
1764 if ($post->in_reply_to_status_id_str != "") {
1765 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1768 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1770 if (empty($postarray['body'])) {
1774 $item = Item::insert($postarray);
1776 Logger::log('User ' . $uid . ' posted mention timeline item ' . $item);
1780 PConfig::set($uid, 'twitter', 'lastmentionid', $lastid);
1782 Logger::log('Last mentions ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1785 function twitter_fetch_own_contact(App $a, $uid)
1787 $ckey = Config::get('twitter', 'consumerkey');
1788 $csecret = Config::get('twitter', 'consumersecret');
1789 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1790 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1792 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1796 if ($own_id == "") {
1797 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1799 // Fetching user data
1800 // get() may throw TwitterOAuthException, but we will catch it later
1801 $user = $connection->get('account/verify_credentials');
1802 if (empty($user) || empty($user->id_str)) {
1806 PConfig::set($uid, 'twitter', 'own_id', $user->id_str);
1808 $contact_id = twitter_fetch_contact($uid, $user, true);
1810 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1812 DBA::escape("twitter::" . $own_id));
1813 if (DBA::isResult($r)) {
1814 $contact_id = $r[0]["id"];
1816 PConfig::delete($uid, 'twitter', 'own_id');
1823 function twitter_is_retweet(App $a, $uid, $body)
1825 $body = trim($body);
1827 // Skip if it isn't a pure repeated messages
1828 // Does it start with a share?
1829 if (strpos($body, "[share") > 0) {
1833 // Does it end with a share?
1834 if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
1838 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
1839 // Skip if there is no shared message in there
1840 if ($body == $attributes) {
1845 preg_match("/link='(.*?)'/ism", $attributes, $matches);
1846 if (!empty($matches[1])) {
1847 $link = $matches[1];
1850 preg_match('/link="(.*?)"/ism', $attributes, $matches);
1851 if (!empty($matches[1])) {
1852 $link = $matches[1];
1855 $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
1860 Logger::log('twitter_is_retweet: Retweeting id ' . $id . ' for user ' . $uid, Logger::DEBUG);
1862 $ckey = Config::get('twitter', 'consumerkey');
1863 $csecret = Config::get('twitter', 'consumersecret');
1864 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1865 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1867 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1868 $result = $connection->post('statuses/retweet/' . $id);
1870 Logger::log('twitter_is_retweet: result ' . print_r($result, true), Logger::DEBUG);
1872 return !isset($result->errors);
1875 function twitter_update_mentions($body)
1877 $URLSearchString = "^\[\]";
1878 $return = preg_replace_callback(
1879 "/@\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
1880 function ($matches) {
1881 if (strpos($matches[1], 'twitter.com')) {
1882 $return = '@' . substr($matches[1], strrpos($matches[1], '/') + 1);
1884 $return = $matches[2] . ' (' . $matches[1] . ')';
1895 function twitter_convert_share(array $attributes, array $author_contact, $content, $is_quote_share)
1897 if ($author_contact['network'] == Protocol::TWITTER) {
1898 $mention = '@' . $author_contact['nickname'];
1900 $mention = $author_contact['addr'];
1903 return ($is_quote_share ? "\n\n" : '' ) . 'RT ' . $mention . ': ' . $content . "\n\n" . $attributes['link'];