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.ini.php or use the admin panel.
54 * consumerkey = your consumer_key here
55 * consumersecret = your consumer_secret here
57 * To activate the addon itself add it to the [system] addon
58 * setting. After this, your user can configure their Twitter account settings
59 * from "Settings -> Addon Settings".
61 * Requirements: PHP5, curl
64 use Abraham\TwitterOAuth\TwitterOAuth;
65 use Abraham\TwitterOAuth\TwitterOAuthException;
67 use Friendica\Content\OEmbed;
68 use Friendica\Content\Text\BBCode;
69 use Friendica\Content\Text\Plaintext;
70 use Friendica\Core\Addon;
71 use Friendica\Core\Config;
72 use Friendica\Core\L10n;
73 use Friendica\Core\Logger;
74 use Friendica\Core\PConfig;
75 use Friendica\Core\Protocol;
76 use Friendica\Core\Renderer;
77 use Friendica\Core\Worker;
78 use Friendica\Database\DBA;
79 use Friendica\Model\Contact;
80 use Friendica\Model\Conversation;
81 use Friendica\Model\GContact;
82 use Friendica\Model\Group;
83 use Friendica\Model\Item;
84 use Friendica\Model\ItemContent;
85 use Friendica\Model\Queue;
86 use Friendica\Model\User;
87 use Friendica\Object\Image;
88 use Friendica\Util\DateTimeFormat;
89 use Friendica\Util\Network;
90 use Friendica\Util\Strings;
92 require_once 'boot.php';
93 require_once 'include/dba.php';
94 require_once 'include/enotify.php';
95 require_once 'include/text.php';
97 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
99 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
101 function twitter_install()
103 // we need some hooks, for the configuration and for sending tweets
104 Addon::registerHook('load_config' , __FILE__, 'twitter_load_config');
105 Addon::registerHook('connector_settings' , __FILE__, 'twitter_settings');
106 Addon::registerHook('connector_settings_post', __FILE__, 'twitter_settings_post');
107 Addon::registerHook('post_local' , __FILE__, 'twitter_post_local');
108 Addon::registerHook('notifier_normal' , __FILE__, 'twitter_post_hook');
109 Addon::registerHook('jot_networks' , __FILE__, 'twitter_jot_nets');
110 Addon::registerHook('cron' , __FILE__, 'twitter_cron');
111 Addon::registerHook('queue_predeliver' , __FILE__, 'twitter_queue_hook');
112 Addon::registerHook('follow' , __FILE__, 'twitter_follow');
113 Addon::registerHook('expire' , __FILE__, 'twitter_expire');
114 Addon::registerHook('prepare_body' , __FILE__, 'twitter_prepare_body');
115 Addon::registerHook('check_item_notification', __FILE__, 'twitter_check_item_notification');
116 Logger::log("installed twitter");
119 function twitter_uninstall()
121 Addon::unregisterHook('load_config' , __FILE__, 'twitter_load_config');
122 Addon::unregisterHook('connector_settings' , __FILE__, 'twitter_settings');
123 Addon::unregisterHook('connector_settings_post', __FILE__, 'twitter_settings_post');
124 Addon::unregisterHook('post_local' , __FILE__, 'twitter_post_local');
125 Addon::unregisterHook('notifier_normal' , __FILE__, 'twitter_post_hook');
126 Addon::unregisterHook('jot_networks' , __FILE__, 'twitter_jot_nets');
127 Addon::unregisterHook('cron' , __FILE__, 'twitter_cron');
128 Addon::unregisterHook('queue_predeliver' , __FILE__, 'twitter_queue_hook');
129 Addon::unregisterHook('follow' , __FILE__, 'twitter_follow');
130 Addon::unregisterHook('expire' , __FILE__, 'twitter_expire');
131 Addon::unregisterHook('prepare_body' , __FILE__, 'twitter_prepare_body');
132 Addon::unregisterHook('check_item_notification', __FILE__, 'twitter_check_item_notification');
134 // old setting - remove only
135 Addon::unregisterHook('post_local_end' , __FILE__, 'twitter_post_hook');
136 Addon::unregisterHook('addon_settings' , __FILE__, 'twitter_settings');
137 Addon::unregisterHook('addon_settings_post', __FILE__, 'twitter_settings_post');
140 function twitter_load_config(App $a)
142 $a->loadConfigFile(__DIR__ . '/config/twitter.ini.php');
145 function twitter_check_item_notification(App $a, array &$notification_data)
147 $own_id = PConfig::get($notification_data["uid"], 'twitter', 'own_id');
149 $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
150 intval($notification_data["uid"]),
151 DBA::escape("twitter::".$own_id)
155 $notification_data["profiles"][] = $own_user[0]["url"];
159 function twitter_follow(App $a, array &$contact)
161 Logger::log("twitter_follow: Check if contact is twitter contact. " . $contact["url"], Logger::DEBUG);
163 if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
167 // contact seems to be a twitter contact, so continue
168 $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
169 $nickname = str_replace("@twitter.com", "", $nickname);
171 $uid = $a->user["uid"];
173 $ckey = Config::get('twitter', 'consumerkey');
174 $csecret = Config::get('twitter', 'consumersecret');
175 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
176 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
178 // If the addon is not configured (general or for this user) quit here
179 if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
184 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
185 $connection->post('friendships/create', ['screen_name' => $nickname]);
187 twitter_fetchuser($a, $uid, $nickname);
189 $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey
190 FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
192 DBA::escape($nickname));
193 if (DBA::isResult($r)) {
194 $contact["contact"] = $r[0];
198 function twitter_jot_nets(App $a, &$b)
204 $tw_post = PConfig::get(local_user(), 'twitter', 'post');
205 if (intval($tw_post) == 1) {
206 $tw_defpost = PConfig::get(local_user(), 'twitter', 'post_by_default');
207 $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
208 $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> '
209 . L10n::t('Post to Twitter') . '</div>';
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_post_local(App $a, array &$b)
400 if (!local_user() || (local_user() != $b['uid'])) {
404 $twitter_post = intval(PConfig::get(local_user(), 'twitter', 'post'));
405 $twitter_enable = (($twitter_post && x($_REQUEST, 'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
407 // if API is used, default to the chosen settings
408 if ($b['api_source'] && intval(PConfig::get(local_user(), 'twitter', 'post_by_default'))) {
412 if (!$twitter_enable) {
416 if (strlen($b['postopts'])) {
417 $b['postopts'] .= ',';
420 $b['postopts'] .= 'twitter';
423 function twitter_action(App $a, $uid, $pid, $action)
425 $ckey = Config::get('twitter', 'consumerkey');
426 $csecret = Config::get('twitter', 'consumersecret');
427 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
428 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
430 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
432 $post = ['id' => $pid];
434 Logger::log("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), Logger::DATA);
438 // To-Do: $result = $connection->post('statuses/destroy', $post);
442 $result = $connection->post('favorites/create', $post);
445 $result = $connection->post('favorites/destroy', $post);
448 Logger::log('Unhandled action ' . $action, Logger::DEBUG);
451 Logger::log("twitter_action '" . $action . "' send, result: " . print_r($result, true), Logger::DEBUG);
454 function twitter_post_hook(App $a, array &$b)
457 if (!PConfig::get($b["uid"], 'twitter', 'import')
458 && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
462 if ($b['parent'] != $b['id']) {
463 Logger::log("twitter_post_hook: parameter " . print_r($b, true), Logger::DATA);
465 // Looking if its a reply to a twitter post
466 if ((substr($b["parent-uri"], 0, 9) != "twitter::")
467 && (substr($b["extid"], 0, 9) != "twitter::")
468 && (substr($b["thr-parent"], 0, 9) != "twitter::"))
470 Logger::log("twitter_post_hook: no twitter post " . $b["parent"]);
474 $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
475 $orig_post = Item::selectFirst([], $condition);
476 if (!DBA::isResult($orig_post)) {
477 Logger::log("twitter_post_hook: no parent found " . $b["thr-parent"]);
484 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
485 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
486 $nicknameplain = "@" . $nicknameplain;
488 Logger::log("twitter_post_hook: comparing " . $nickname . " and " . $nicknameplain . " with " . $b["body"], Logger::DEBUG);
489 if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
490 $b["body"] = $nickname . " " . $b["body"];
493 Logger::log("twitter_post_hook: parent found " . print_r($orig_post, true), Logger::DATA);
497 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
501 // Dont't post if the post doesn't belong to us.
502 // This is a check for forum postings
503 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
504 if ($b['contact-id'] != $self['id']) {
509 if (($b['verb'] == ACTIVITY_POST) && $b['deleted']) {
510 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
513 if ($b['verb'] == ACTIVITY_LIKE) {
514 Logger::log("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), Logger::DEBUG);
516 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
518 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
524 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
528 // if post comes from twitter don't send it back
529 if ($b['extid'] == Protocol::TWITTER) {
533 if ($b['app'] == "Twitter") {
537 Logger::log('twitter post invoked');
539 PConfig::load($b['uid'], 'twitter');
541 $ckey = Config::get('twitter', 'consumerkey');
542 $csecret = Config::get('twitter', 'consumersecret');
543 $otoken = PConfig::get($b['uid'], 'twitter', 'oauthtoken');
544 $osecret = PConfig::get($b['uid'], 'twitter', 'oauthsecret');
546 if ($ckey && $csecret && $otoken && $osecret) {
547 Logger::log('twitter: we have customer key and oauth stuff, going to send.', Logger::DEBUG);
549 // If it's a repeated message from twitter then do a native retweet and exit
550 if (twitter_is_retweet($a, $b['uid'], $b['body'])) {
554 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
556 // Set the timeout for upload to 30 seconds
557 $connection->setTimeouts(10, 30);
561 // Handling non-native reshares
562 $b['body'] = Friendica\Content\Text\BBCode::convertShare(
564 function (array $attributes, array $author_contact, $content, $is_quote_share) {
565 return twitter_convert_share($attributes, $author_contact, $content, $is_quote_share);
569 $b['body'] = twitter_update_mentions($b['body']);
571 $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
572 $msg = $msgarr["text"];
574 if (($msg == "") && isset($msgarr["title"])) {
575 $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
580 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
581 $msg .= "\n" . $msgarr["url"];
587 if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
588 $image = $msgarr["image"];
595 // and now tweet it :-)
598 if (!empty($image)) {
600 $img_str = Network::fetchUrl($image);
602 $tempfile = tempnam(get_temppath(), 'cache');
603 file_put_contents($tempfile, $img_str);
605 $media = $connection->upload('media/upload', ['media' => $tempfile]);
609 if (isset($media->media_id_string)) {
610 $post['media_ids'] = $media->media_id_string;
612 throw new Exception('Failed upload of ' . $image);
614 } catch (Exception $e) {
615 Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage());
617 // Workaround: Remove the picture link so that the post can be reposted without it
618 // When there is another url already added, a second url would be superfluous.
620 $msg .= "\n" . $image;
627 $post['status'] = $msg;
630 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
633 $url = 'statuses/update';
634 $result = $connection->post($url, $post);
635 Logger::log('twitter_post send, result: ' . print_r($result, true), Logger::DEBUG);
637 if (!empty($result->source)) {
638 Config::set("twitter", "application_name", strip_tags($result->source));
641 if (!empty($result->errors)) {
642 Logger::log('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
644 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
645 if (DBA::isResult($r)) {
646 $a->contact = $r[0]["id"];
649 $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
651 Queue::add($a->contact, Protocol::TWITTER, $s);
652 notice(L10n::t('Twitter post failed. Queued for retry.') . EOL);
653 } elseif ($iscomment) {
654 Logger::log('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
655 Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
660 function twitter_addon_admin_post(App $a)
662 $consumerkey = x($_POST, 'consumerkey') ? Strings::escapeTags(trim($_POST['consumerkey'])) : '';
663 $consumersecret = x($_POST, 'consumersecret') ? Strings::escapeTags(trim($_POST['consumersecret'])) : '';
664 Config::set('twitter', 'consumerkey', $consumerkey);
665 Config::set('twitter', 'consumersecret', $consumersecret);
666 info(L10n::t('Settings updated.') . EOL);
669 function twitter_addon_admin(App $a, &$o)
671 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/twitter/");
673 $o = Renderer::replaceMacros($t, [
674 '$submit' => L10n::t('Save Settings'),
675 // name, label, value, help, [extra values]
676 '$consumerkey' => ['consumerkey', L10n::t('Consumer key'), Config::get('twitter', 'consumerkey'), ''],
677 '$consumersecret' => ['consumersecret', L10n::t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''],
681 function twitter_cron(App $a)
683 $last = Config::get('twitter', 'last_poll');
685 $poll_interval = intval(Config::get('twitter', 'poll_interval'));
686 if (!$poll_interval) {
687 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
691 $next = $last + ($poll_interval * 60);
692 if ($next > time()) {
693 Logger::log('twitter: poll intervall not reached');
697 Logger::log('twitter: cron_start');
699 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
700 if (DBA::isResult($r)) {
701 foreach ($r as $rr) {
702 Logger::log('twitter: fetching for user ' . $rr['uid']);
703 Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
707 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
708 if ($abandon_days < 1) {
712 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
714 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
715 if (DBA::isResult($r)) {
716 foreach ($r as $rr) {
717 if ($abandon_days != 0) {
718 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
719 if (!DBA::isResult($user)) {
720 Logger::log('abandoned account: timeline from user ' . $rr['uid'] . ' will not be imported');
725 Logger::log('twitter: importing timeline from user ' . $rr['uid']);
726 Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
729 // check for new contacts once a day
730 $last_contact_check = PConfig::get($rr['uid'],'pumpio','contact_check');
731 if($last_contact_check)
732 $next_contact_check = $last_contact_check + 86400;
734 $next_contact_check = 0;
736 if($next_contact_check <= time()) {
737 pumpio_getallusers($a, $rr["uid"]);
738 PConfig::set($rr['uid'],'pumpio','contact_check',time());
744 Logger::log('twitter: cron_end');
746 Config::set('twitter', 'last_poll', time());
749 function twitter_expire(App $a)
751 $days = Config::get('twitter', 'expire');
757 $r = Item::select(['id'], ['deleted' => true, 'network' => Protocol::TWITTER]);
758 while ($row = DBA::fetch($r)) {
759 DBA::delete('item', ['id' => $row['id']]);
763 require_once "include/items.php";
765 Logger::log('twitter_expire: expire_start');
767 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
768 if (DBA::isResult($r)) {
769 foreach ($r as $rr) {
770 Logger::log('twitter_expire: user ' . $rr['uid']);
771 Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
775 Logger::log('twitter_expire: expire_end');
778 function twitter_prepare_body(App $a, array &$b)
780 if ($b["item"]["network"] != Protocol::TWITTER) {
787 $item["plink"] = $a->getBaseURL() . "/display/" . $a->user["nickname"] . "/" . $item["parent"];
789 $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
790 $orig_post = Item::selectFirst(['author-link'], $condition);
791 if (DBA::isResult($orig_post)) {
792 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
793 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
794 $nicknameplain = "@" . $nicknameplain;
796 if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) {
797 $item["body"] = $nickname . " " . $item["body"];
801 $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, 8);
802 $msg = $msgarr["text"];
804 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
805 $msg .= " " . $msgarr["url"];
808 if (isset($msgarr["image"])) {
809 $msg .= " " . $msgarr["image"];
812 $b['html'] = nl2br(htmlspecialchars($msg));
817 * @brief Build the item array for the mirrored post
819 * @param App $a Application class
820 * @param integer $uid User id
821 * @param object $post Twitter object with the post
823 * @return array item data to be posted
825 function twitter_do_mirrorpost(App $a, $uid, $post)
827 $datarray['api_source'] = true;
828 $datarray['profile_uid'] = $uid;
829 $datarray['extid'] = Protocol::TWITTER;
830 $datarray['message_id'] = Item::newURI($uid, Protocol::TWITTER . ':' . $post->id);
831 $datarray['protocol'] = Conversation::PARCEL_TWITTER;
832 $datarray['source'] = json_encode($post);
833 $datarray['title'] = '';
835 if (!empty($post->retweeted_status)) {
836 // We don't support nested shares, so we mustn't show quotes as shares on retweets
837 $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true);
839 if (empty($item['body'])) {
843 $datarray['body'] = "\n" . share_header(
844 $item['author-name'],
845 $item['author-link'],
846 $item['author-avatar'],
852 $datarray['body'] .= $item['body'] . '[/share]';
854 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
856 if (empty($item['body'])) {
860 $datarray['body'] = $item['body'];
863 $datarray['source'] = $item['app'];
864 $datarray['verb'] = $item['verb'];
866 if (isset($item['location'])) {
867 $datarray['location'] = $item['location'];
870 if (isset($item['coord'])) {
871 $datarray['coord'] = $item['coord'];
877 function twitter_fetchtimeline(App $a, $uid)
879 $ckey = Config::get('twitter', 'consumerkey');
880 $csecret = Config::get('twitter', 'consumersecret');
881 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
882 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
883 $lastid = PConfig::get($uid, 'twitter', 'lastid');
885 $application_name = Config::get('twitter', 'application_name');
887 if ($application_name == "") {
888 $application_name = $a->getHostName();
891 $has_picture = false;
893 require_once 'mod/item.php';
894 require_once 'include/items.php';
895 require_once 'mod/share.php';
897 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
899 $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
901 $first_time = ($lastid == "");
904 $parameters["since_id"] = $lastid;
908 $items = $connection->get('statuses/user_timeline', $parameters);
909 } catch (TwitterOAuthException $e) {
910 Logger::log('Error fetching timeline for user ' . $uid . ': ' . $e->getMessage());
914 if (!is_array($items)) {
915 Logger::log('No items for user ' . $uid, Logger::INFO);
919 $posts = array_reverse($items);
921 Logger::log('Starting from ID ' . $lastid . ' for user ' . $uid, Logger::DEBUG);
924 foreach ($posts as $post) {
925 if ($post->id_str > $lastid) {
926 $lastid = $post->id_str;
927 PConfig::set($uid, 'twitter', 'lastid', $lastid);
934 if (!stristr($post->source, $application_name)) {
935 $_SESSION["authenticated"] = true;
936 $_SESSION["uid"] = $uid;
938 Logger::log('Preparing Twitter ID ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
940 $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
942 if (empty($_REQUEST['body'])) {
946 Logger::log('Posting Twitter ID ' . $post->id_str . ' for user ' . $uid);
952 PConfig::set($uid, 'twitter', 'lastid', $lastid);
953 Logger::log('Last ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
956 function twitter_queue_hook(App $a)
958 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
959 DBA::escape(Protocol::TWITTER)
961 if (!DBA::isResult($qi)) {
965 foreach ($qi as $x) {
966 if ($x['network'] !== Protocol::TWITTER) {
970 Logger::log('twitter_queue: run');
972 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
973 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
976 if (!DBA::isResult($r)) {
982 $ckey = Config::get('twitter', 'consumerkey');
983 $csecret = Config::get('twitter', 'consumersecret');
984 $otoken = PConfig::get($user['uid'], 'twitter', 'oauthtoken');
985 $osecret = PConfig::get($user['uid'], 'twitter', 'oauthsecret');
989 if ($ckey && $csecret && $otoken && $osecret) {
990 Logger::log('twitter_queue: able to post');
992 $z = unserialize($x['content']);
994 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
995 $result = $connection->post($z['url'], $z['post']);
997 Logger::log('twitter_queue: post result: ' . print_r($result, true), Logger::DEBUG);
999 if ($result->errors) {
1000 Logger::log('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"');
1003 Queue::removeItem($x['id']);
1006 Logger::log("twitter_queue: Error getting tokens for user " . $user['uid']);
1010 Logger::log('twitter_queue: delayed');
1011 Queue::updateTime($x['id']);
1016 function twitter_fix_avatar($avatar)
1018 $new_avatar = str_replace("_normal.", ".", $avatar);
1020 $info = Image::getInfoFromURL($new_avatar);
1022 $new_avatar = $avatar;
1028 function twitter_fetch_contact($uid, $data, $create_user)
1030 if (empty($data->id_str)) {
1034 $avatar = twitter_fix_avatar($data->profile_image_url_https);
1035 $url = "https://twitter.com/" . $data->screen_name;
1036 $addr = $data->screen_name . "@twitter.com";
1038 GContact::update(["url" => $url, "network" => Protocol::TWITTER,
1039 "photo" => $avatar, "hide" => true,
1040 "name" => $data->name, "nick" => $data->screen_name,
1041 "location" => $data->location, "about" => $data->description,
1042 "addr" => $addr, "generation" => 2]);
1044 $fields = ['url' => $url, 'network' => Protocol::TWITTER,
1045 'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
1046 'location' => $data->location, 'about' => $data->description];
1048 $cid = Contact::getIdForURL($url, 0, true, $fields);
1050 DBA::update('contact', $fields, ['id' => $cid]);
1051 Contact::updateAvatar($avatar, 0, $cid);
1054 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
1055 if (!DBA::isResult($contact) && !$create_user) {
1059 if (!DBA::isResult($contact)) {
1060 // create contact record
1061 $fields['uid'] = $uid;
1062 $fields['created'] = DateTimeFormat::utcNow();
1063 $fields['nurl'] = Strings::normaliseLink($url);
1064 $fields['alias'] = 'twitter::' . $data->id_str;
1065 $fields['poll'] = 'twitter::' . $data->id_str;
1066 $fields['rel'] = Contact::FRIEND;
1067 $fields['priority'] = 1;
1068 $fields['writable'] = true;
1069 $fields['blocked'] = false;
1070 $fields['readonly'] = false;
1071 $fields['pending'] = false;
1073 if (!DBA::insert('contact', $fields)) {
1077 $contact_id = DBA::lastInsertId();
1079 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1081 Contact::updateAvatar($avatar, $uid, $contact_id);
1083 if ($contact["readonly"] || $contact["blocked"]) {
1084 Logger::log("twitter_fetch_contact: Contact '" . $contact["nick"] . "' is blocked or readonly.", Logger::DEBUG);
1088 $contact_id = $contact['id'];
1090 // update profile photos once every twelve hours as we have no notification of when they change.
1091 $update_photo = ($contact['avatar-date'] < DateTimeFormat::utc('now -12 hours'));
1093 // check that we have all the photos, this has been known to fail on occasion
1094 if (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']) || $update_photo) {
1095 Logger::log("twitter_fetch_contact: Updating contact " . $data->screen_name, Logger::DEBUG);
1097 Contact::updateAvatar($avatar, $uid, $contact['id']);
1099 $fields['name-date'] = DateTimeFormat::utcNow();
1100 $fields['uri-date'] = DateTimeFormat::utcNow();
1102 DBA::update('contact', $fields, ['id' => $contact['id']]);
1109 function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
1111 $ckey = Config::get('twitter', 'consumerkey');
1112 $csecret = Config::get('twitter', 'consumersecret');
1113 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1114 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1116 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1119 if (DBA::isResult($r)) {
1127 if ($screen_name != "") {
1128 $parameters["screen_name"] = $screen_name;
1131 if ($user_id != "") {
1132 $parameters["user_id"] = $user_id;
1135 // Fetching user data
1136 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1138 $user = $connection->get('users/show', $parameters);
1139 } catch (TwitterOAuthException $e) {
1140 Logger::log('twitter_fetchuser: Error fetching user ' . $uid . ': ' . $e->getMessage());
1144 if (!is_object($user)) {
1148 $contact_id = twitter_fetch_contact($uid, $user, true);
1153 function twitter_expand_entities(App $a, $body, $item, $picture)
1159 foreach ($item->entities->hashtags AS $hashtag) {
1160 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . rawurlencode($hashtag->text) . ']' . $hashtag->text . '[/url]';
1161 $tags_arr['#' . $hashtag->text] = $url;
1162 $body = str_replace('#' . $hashtag->text, $url, $body);
1165 foreach ($item->entities->user_mentions AS $mention) {
1166 $url = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
1167 $tags_arr['@' . $mention->screen_name] = $url;
1168 $body = str_replace('@' . $mention->screen_name, $url, $body);
1171 if (isset($item->entities->urls)) {
1177 foreach ($item->entities->urls as $url) {
1178 $plain = str_replace($url->url, '', $plain);
1180 if ($url->url && $url->expanded_url && $url->display_url) {
1181 // Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
1182 if (isset($item->quoted_status_id_str)
1183 && substr($url->expanded_url, -strlen($item->quoted_status_id_str)) == $item->quoted_status_id_str ) {
1184 $body = str_replace($url->url, '', $body);
1188 $expanded_url = Network::finalUrl($url->expanded_url);
1190 $oembed_data = OEmbed::fetchURL($expanded_url);
1192 if (empty($oembed_data) || empty($oembed_data->type)) {
1196 // Quickfix: Workaround for URL with '[' and ']' in it
1197 if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
1198 $expanded_url = $url->url;
1202 $type = $oembed_data->type;
1205 if ($oembed_data->type == 'video') {
1206 $type = $oembed_data->type;
1207 $footerurl = $expanded_url;
1208 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1210 $body = str_replace($url->url, $footerlink, $body);
1211 } elseif (($oembed_data->type == 'photo') && isset($oembed_data->url)) {
1212 $body = str_replace($url->url, '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]', $body);
1213 } elseif ($oembed_data->type != 'link') {
1214 $body = str_replace($url->url, '[url=' . $expanded_url . ']' . $url->display_url . '[/url]', $body);
1216 $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4);
1218 $tempfile = tempnam(get_temppath(), 'cache');
1219 file_put_contents($tempfile, $img_str);
1221 // See http://php.net/manual/en/function.exif-imagetype.php#79283
1222 if (filesize($tempfile) > 11) {
1223 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
1230 if (substr($mime, 0, 6) == 'image/') {
1232 $body = str_replace($url->url, '[img]' . $expanded_url . '[/img]', $body);
1234 $type = $oembed_data->type;
1235 $footerurl = $expanded_url;
1236 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1238 $body = str_replace($url->url, $footerlink, $body);
1244 // Footer will be taken care of with a share block in the case of a quote
1245 if (empty($item->quoted_status)) {
1246 if ($footerurl != '') {
1247 $footer = add_page_info($footerurl, false, $picture);
1250 if (($footerlink != '') && (trim($footer) != '')) {
1251 $removedlink = trim(str_replace($footerlink, '', $body));
1253 if (($removedlink == '') || strstr($body, $removedlink)) {
1254 $body = $removedlink;
1260 if ($footer == '' && $picture != '') {
1261 $body .= "\n\n[img]" . $picture . "[/img]\n";
1262 } elseif ($footer == '' && $picture == '') {
1263 $body = add_page_info_to_body($body);
1268 // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
1269 $tags = BBCode::getTags($body);
1272 foreach ($tags as $tag) {
1273 if (strstr(trim($tag), ' ')) {
1277 if (strpos($tag, '#') === 0) {
1278 if (strpos($tag, '[url=')) {
1282 // don't link tags that are already embedded in links
1283 if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
1286 if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
1290 $basetag = str_replace('_', ' ', substr($tag, 1));
1291 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1292 $body = str_replace($tag, $url, $body);
1293 $tags_arr['#' . $basetag] = $url;
1294 } elseif (strpos($tag, '@') === 0) {
1295 if (strpos($tag, '[url=')) {
1299 $basetag = substr($tag, 1);
1300 $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1301 $body = str_replace($tag, $url, $body);
1302 $tags_arr['@' . $basetag] = $url;
1307 $tags = implode($tags_arr, ',');
1309 return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
1313 * @brief Fetch media entities and add media links to the body
1315 * @param object $post Twitter object with the post
1316 * @param array $postarray Array of the item that is about to be posted
1318 * @return $picture string Image URL or empty string
1320 function twitter_media_entities($post, array &$postarray)
1322 // There are no media entities? So we quit.
1323 if (empty($post->extended_entities->media)) {
1327 // When the post links to an external page, we only take one picture.
1328 // We only do this when there is exactly one media.
1329 if ((count($post->entities->urls) > 0) && (count($post->extended_entities->media) == 1)) {
1330 $medium = $post->extended_entities->media[0];
1332 foreach ($post->entities->urls as $link) {
1333 // Let's make sure the external link url matches the media url
1334 if ($medium->url == $link->url && isset($medium->media_url_https)) {
1335 $picture = $medium->media_url_https;
1336 $postarray['body'] = str_replace($medium->url, '', $postarray['body']);
1342 // This is a pure media post, first search for all media urls
1344 foreach ($post->extended_entities->media AS $medium) {
1345 if (!isset($media[$medium->url])) {
1346 $media[$medium->url] = '';
1348 switch ($medium->type) {
1350 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1351 $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1354 case 'animated_gif':
1355 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1356 $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
1357 if (is_array($medium->video_info->variants)) {
1359 // We take the video with the highest bitrate
1360 foreach ($medium->video_info->variants AS $variant) {
1361 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1362 $media[$medium->url] = "\n[video]" . $variant->url . '[/video]';
1363 $bitrate = $variant->bitrate;
1368 // The following code will only be activated for test reasons
1370 // $postarray['body'] .= print_r($medium, true);
1374 // Now we replace the media urls.
1375 foreach ($media AS $key => $value) {
1376 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1382 function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $only_existing_contact, $noquote)
1385 $postarray['network'] = Protocol::TWITTER;
1386 $postarray['uid'] = $uid;
1387 $postarray['wall'] = 0;
1388 $postarray['uri'] = "twitter::" . $post->id_str;
1389 $postarray['protocol'] = Conversation::PARCEL_TWITTER;
1390 $postarray['source'] = json_encode($post);
1392 // Don't import our own comments
1393 if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
1394 Logger::log("Item with extid " . $postarray['uri'] . " found.", Logger::DEBUG);
1400 if ($post->in_reply_to_status_id_str != "") {
1401 $parent = "twitter::" . $post->in_reply_to_status_id_str;
1403 $fields = ['uri', 'parent-uri', 'parent'];
1404 $parent_item = Item::selectFirst($fields, ['uri' => $parent, 'uid' => $uid]);
1405 if (!DBA::isResult($parent_item)) {
1406 $parent_item = Item::selectFirst($fields, ['extid' => $parent, 'uid' => $uid]);
1409 if (DBA::isResult($parent_item)) {
1410 $postarray['thr-parent'] = $parent_item['uri'];
1411 $postarray['parent-uri'] = $parent_item['parent-uri'];
1412 $postarray['parent'] = $parent_item['parent'];
1413 $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1415 $postarray['thr-parent'] = $postarray['uri'];
1416 $postarray['parent-uri'] = $postarray['uri'];
1417 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1421 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1423 if ($post->user->id_str == $own_id) {
1424 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1427 if (DBA::isResult($r)) {
1428 $contactid = $r[0]["id"];
1430 $postarray['owner-name'] = $r[0]["name"];
1431 $postarray['owner-link'] = $r[0]["url"];
1432 $postarray['owner-avatar'] = $r[0]["photo"];
1434 Logger::log("No self contact for user " . $uid, Logger::DEBUG);
1438 // Don't create accounts of people who just comment something
1439 $create_user = false;
1441 $postarray['parent-uri'] = $postarray['uri'];
1442 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1445 if ($contactid == 0) {
1446 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1448 $postarray['owner-name'] = $post->user->name;
1449 $postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
1450 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1453 if (($contactid == 0) && !$only_existing_contact) {
1454 $contactid = $self['id'];
1455 } elseif ($contactid <= 0) {
1456 Logger::log("Contact ID is zero or less than zero.", Logger::DEBUG);
1460 $postarray['contact-id'] = $contactid;
1462 $postarray['verb'] = ACTIVITY_POST;
1463 $postarray['author-name'] = $postarray['owner-name'];
1464 $postarray['author-link'] = $postarray['owner-link'];
1465 $postarray['author-avatar'] = $postarray['owner-avatar'];
1466 $postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
1467 $postarray['app'] = strip_tags($post->source);
1469 if ($post->user->protected) {
1470 $postarray['private'] = 1;
1471 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1473 $postarray['private'] = 0;
1474 $postarray['allow_cid'] = '';
1477 if (!empty($post->full_text)) {
1478 $postarray['body'] = $post->full_text;
1480 $postarray['body'] = $post->text;
1483 // When the post contains links then use the correct object type
1484 if (count($post->entities->urls) > 0) {
1485 $postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
1488 // Search for media links
1489 $picture = twitter_media_entities($post, $postarray);
1491 $converted = twitter_expand_entities($a, $postarray['body'], $post, $picture);
1492 $postarray['body'] = $converted["body"];
1493 $postarray['tag'] = $converted["tags"];
1494 $postarray['created'] = DateTimeFormat::utc($post->created_at);
1495 $postarray['edited'] = DateTimeFormat::utc($post->created_at);
1497 $statustext = $converted["plain"];
1499 if (!empty($post->place->name)) {
1500 $postarray["location"] = $post->place->name;
1502 if (!empty($post->place->full_name)) {
1503 $postarray["location"] = $post->place->full_name;
1505 if (!empty($post->geo->coordinates)) {
1506 $postarray["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
1508 if (!empty($post->coordinates->coordinates)) {
1509 $postarray["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
1511 if (!empty($post->retweeted_status)) {
1512 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1514 if (empty($retweet['body'])) {
1518 $retweet['source'] = $postarray['source'];
1519 $retweet['private'] = $postarray['private'];
1520 $retweet['allow_cid'] = $postarray['allow_cid'];
1521 $retweet['contact-id'] = $postarray['contact-id'];
1522 $retweet['owner-name'] = $postarray['owner-name'];
1523 $retweet['owner-link'] = $postarray['owner-link'];
1524 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1526 $postarray = $retweet;
1529 if (!empty($post->quoted_status) && !$noquote) {
1530 $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
1532 if (empty($quoted['body'])) {
1536 $postarray['body'] .= "\n" . share_header(
1537 $quoted['author-name'],
1538 $quoted['author-link'],
1539 $quoted['author-avatar'],
1545 $postarray['body'] .= $quoted['body'] . '[/share]';
1551 function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection, array $self)
1553 Logger::log("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, Logger::DEBUG);
1557 while (!empty($post->in_reply_to_status_id_str)) {
1558 $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str];
1561 $post = $connection->get('statuses/show', $parameters);
1562 } catch (TwitterOAuthException $e) {
1563 Logger::log('twitter_fetchparentposts: Error fetching for user ' . $uid . ' and post ' . $post->id_str . ': ' . $e->getMessage());
1568 Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters->id, Logger::DEBUG);
1572 if (empty($post->id_str)) {
1573 Logger::log("twitter_fetchparentposts: This is not a post " . json_encode($post), Logger::DEBUG);
1577 if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1584 Logger::log("twitter_fetchparentposts: Fetching " . count($posts) . " parents", Logger::DEBUG);
1586 $posts = array_reverse($posts);
1588 if (!empty($posts)) {
1589 foreach ($posts as $post) {
1590 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1592 if (empty($postarray['body'])) {
1596 $item = Item::insert($postarray);
1598 $postarray["id"] = $item;
1600 Logger::log('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
1605 function twitter_fetchhometimeline(App $a, $uid)
1607 $ckey = Config::get('twitter', 'consumerkey');
1608 $csecret = Config::get('twitter', 'consumersecret');
1609 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1610 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1611 $create_user = PConfig::get($uid, 'twitter', 'create_user');
1612 $mirror_posts = PConfig::get($uid, 'twitter', 'mirror_posts');
1614 Logger::log("Fetching timeline for user " . $uid, Logger::DEBUG);
1616 $application_name = Config::get('twitter', 'application_name');
1618 if ($application_name == "") {
1619 $application_name = $a->getHostName();
1622 require_once 'include/items.php';
1624 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1627 $own_contact = twitter_fetch_own_contact($a, $uid);
1628 } catch (TwitterOAuthException $e) {
1629 Logger::log('Error fetching own contact for user ' . $uid . ': ' . $e->getMessage());
1633 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1634 intval($own_contact),
1637 if (DBA::isResult($r)) {
1638 $own_id = $r[0]["nick"];
1640 Logger::log("Own twitter contact not found for user " . $uid);
1644 $self = User::getOwnerDataById($uid);
1645 if ($self === false) {
1646 Logger::log("Own contact not found for user " . $uid);
1650 $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
1651 //$parameters["count"] = 200;
1652 // Fetching timeline
1653 $lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid');
1655 $first_time = ($lastid == "");
1657 if ($lastid != "") {
1658 $parameters["since_id"] = $lastid;
1662 $items = $connection->get('statuses/home_timeline', $parameters);
1663 } catch (TwitterOAuthException $e) {
1664 Logger::log('Error fetching home timeline for user ' . $uid . ': ' . $e->getMessage());
1668 if (!is_array($items)) {
1669 Logger::log('No array while fetching home timeline for user ' . $uid . ': ' . print_r($items, true));
1673 if (empty($items)) {
1674 Logger::log('No new timeline content for user ' . $uid, Logger::INFO);
1678 $posts = array_reverse($items);
1680 Logger::log('Fetching timeline from ID ' . $lastid . ' for user ' . $uid . ' ' . sizeof($posts) . ' items', Logger::DEBUG);
1682 if (count($posts)) {
1683 foreach ($posts as $post) {
1684 if ($post->id_str > $lastid) {
1685 $lastid = $post->id_str;
1686 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1693 if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1694 Logger::log("Skip previously sent post", Logger::DEBUG);
1698 if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1699 Logger::log("Skip post that will be mirrored", Logger::DEBUG);
1703 if ($post->in_reply_to_status_id_str != "") {
1704 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1707 Logger::log('Preparing post ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
1709 $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1711 if (empty($postarray['body']) || trim($postarray['body']) == "") {
1712 Logger::log('Empty body for post ' . $post->id_str . ' and user ' . $uid, Logger::DEBUG);
1718 if (($postarray['uri'] == $postarray['parent-uri']) && ($postarray['author-link'] == $postarray['owner-link'])) {
1719 $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
1720 if (DBA::isResult($contact)) {
1721 $notify = Item::isRemoteSelf($contact, $postarray);
1725 $item = Item::insert($postarray, false, $notify);
1726 $postarray["id"] = $item;
1728 Logger::log('User ' . $uid . ' posted home timeline item ' . $item);
1731 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1733 Logger::log('Last timeline ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1735 // Fetching mentions
1736 $lastid = PConfig::get($uid, 'twitter', 'lastmentionid');
1738 $first_time = ($lastid == "");
1740 if ($lastid != "") {
1741 $parameters["since_id"] = $lastid;
1745 $items = $connection->get('statuses/mentions_timeline', $parameters);
1746 } catch (TwitterOAuthException $e) {
1747 Logger::log('Error fetching mentions: ' . $e->getMessage());
1751 if (!is_array($items)) {
1752 Logger::log("Error fetching mentions: " . print_r($items, true), Logger::DEBUG);
1756 $posts = array_reverse($items);
1758 Logger::log("Fetching mentions for user " . $uid . " " . sizeof($posts) . " items", Logger::DEBUG);
1760 if (count($posts)) {
1761 foreach ($posts as $post) {
1762 if ($post->id_str > $lastid) {
1763 $lastid = $post->id_str;
1770 if ($post->in_reply_to_status_id_str != "") {
1771 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1774 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1776 if (empty($postarray['body'])) {
1780 $item = Item::insert($postarray);
1782 Logger::log('User ' . $uid . ' posted mention timeline item ' . $item);
1786 PConfig::set($uid, 'twitter', 'lastmentionid', $lastid);
1788 Logger::log('Last mentions ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1791 function twitter_fetch_own_contact(App $a, $uid)
1793 $ckey = Config::get('twitter', 'consumerkey');
1794 $csecret = Config::get('twitter', 'consumersecret');
1795 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1796 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1798 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1802 if ($own_id == "") {
1803 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1805 // Fetching user data
1806 // get() may throw TwitterOAuthException, but we will catch it later
1807 $user = $connection->get('account/verify_credentials');
1808 if (empty($user) || empty($user->id_str)) {
1812 PConfig::set($uid, 'twitter', 'own_id', $user->id_str);
1814 $contact_id = twitter_fetch_contact($uid, $user, true);
1816 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1818 DBA::escape("twitter::" . $own_id));
1819 if (DBA::isResult($r)) {
1820 $contact_id = $r[0]["id"];
1822 PConfig::delete($uid, 'twitter', 'own_id');
1829 function twitter_is_retweet(App $a, $uid, $body)
1831 $body = trim($body);
1833 // Skip if it isn't a pure repeated messages
1834 // Does it start with a share?
1835 if (strpos($body, "[share") > 0) {
1839 // Does it end with a share?
1840 if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
1844 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
1845 // Skip if there is no shared message in there
1846 if ($body == $attributes) {
1851 preg_match("/link='(.*?)'/ism", $attributes, $matches);
1852 if (!empty($matches[1])) {
1853 $link = $matches[1];
1856 preg_match('/link="(.*?)"/ism', $attributes, $matches);
1857 if (!empty($matches[1])) {
1858 $link = $matches[1];
1861 $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
1866 Logger::log('twitter_is_retweet: Retweeting id ' . $id . ' for user ' . $uid, Logger::DEBUG);
1868 $ckey = Config::get('twitter', 'consumerkey');
1869 $csecret = Config::get('twitter', 'consumersecret');
1870 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1871 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1873 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1874 $result = $connection->post('statuses/retweet/' . $id);
1876 Logger::log('twitter_is_retweet: result ' . print_r($result, true), Logger::DEBUG);
1878 return !isset($result->errors);
1881 function twitter_update_mentions($body)
1883 $URLSearchString = "^\[\]";
1884 $return = preg_replace_callback(
1885 "/@\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism",
1886 function ($matches) {
1887 if (strpos($matches[1], 'twitter.com')) {
1888 $return = '@' . substr($matches[1], strrpos($matches[1], '/') + 1);
1890 $return = $matches[2] . ' (' . $matches[1] . ')';
1901 function twitter_convert_share(array $attributes, array $author_contact, $content, $is_quote_share)
1903 if ($author_contact['network'] == Protocol::TWITTER) {
1904 $mention = '@' . $author_contact['nickname'];
1906 $mention = $author_contact['addr'];
1909 return ($is_quote_share ? "\n\n" : '' ) . 'RT ' . $mention . ': ' . $content . "\n\n" . $attributes['link'];