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\Plaintext;
69 use Friendica\Core\Addon;
70 use Friendica\Core\Config;
71 use Friendica\Core\L10n;
72 use Friendica\Core\Logger;
73 use Friendica\Core\PConfig;
74 use Friendica\Core\Protocol;
75 use Friendica\Core\Worker;
76 use Friendica\Database\DBA;
77 use Friendica\Model\Contact;
78 use Friendica\Model\Conversation;
79 use Friendica\Model\GContact;
80 use Friendica\Model\Group;
81 use Friendica\Model\Item;
82 use Friendica\Model\ItemContent;
83 use Friendica\Model\Queue;
84 use Friendica\Model\User;
85 use Friendica\Object\Image;
86 use Friendica\Util\DateTimeFormat;
87 use Friendica\Util\Network;
89 require_once 'boot.php';
90 require_once 'include/dba.php';
91 require_once 'include/enotify.php';
92 require_once 'include/text.php';
94 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
96 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
98 function twitter_install()
100 // we need some hooks, for the configuration and for sending tweets
101 Addon::registerHook('load_config' , __FILE__, 'twitter_load_config');
102 Addon::registerHook('connector_settings' , __FILE__, 'twitter_settings');
103 Addon::registerHook('connector_settings_post', __FILE__, 'twitter_settings_post');
104 Addon::registerHook('post_local' , __FILE__, 'twitter_post_local');
105 Addon::registerHook('notifier_normal' , __FILE__, 'twitter_post_hook');
106 Addon::registerHook('jot_networks' , __FILE__, 'twitter_jot_nets');
107 Addon::registerHook('cron' , __FILE__, 'twitter_cron');
108 Addon::registerHook('queue_predeliver' , __FILE__, 'twitter_queue_hook');
109 Addon::registerHook('follow' , __FILE__, 'twitter_follow');
110 Addon::registerHook('expire' , __FILE__, 'twitter_expire');
111 Addon::registerHook('prepare_body' , __FILE__, 'twitter_prepare_body');
112 Addon::registerHook('check_item_notification', __FILE__, 'twitter_check_item_notification');
113 Logger::log("installed twitter");
116 function twitter_uninstall()
118 Addon::unregisterHook('load_config' , __FILE__, 'twitter_load_config');
119 Addon::unregisterHook('connector_settings' , __FILE__, 'twitter_settings');
120 Addon::unregisterHook('connector_settings_post', __FILE__, 'twitter_settings_post');
121 Addon::unregisterHook('post_local' , __FILE__, 'twitter_post_local');
122 Addon::unregisterHook('notifier_normal' , __FILE__, 'twitter_post_hook');
123 Addon::unregisterHook('jot_networks' , __FILE__, 'twitter_jot_nets');
124 Addon::unregisterHook('cron' , __FILE__, 'twitter_cron');
125 Addon::unregisterHook('queue_predeliver' , __FILE__, 'twitter_queue_hook');
126 Addon::unregisterHook('follow' , __FILE__, 'twitter_follow');
127 Addon::unregisterHook('expire' , __FILE__, 'twitter_expire');
128 Addon::unregisterHook('prepare_body' , __FILE__, 'twitter_prepare_body');
129 Addon::unregisterHook('check_item_notification', __FILE__, 'twitter_check_item_notification');
131 // old setting - remove only
132 Addon::unregisterHook('post_local_end' , __FILE__, 'twitter_post_hook');
133 Addon::unregisterHook('addon_settings' , __FILE__, 'twitter_settings');
134 Addon::unregisterHook('addon_settings_post', __FILE__, 'twitter_settings_post');
137 function twitter_load_config(App $a)
139 $a->loadConfigFile(__DIR__ . '/config/twitter.ini.php');
142 function twitter_check_item_notification(App $a, array &$notification_data)
144 $own_id = PConfig::get($notification_data["uid"], 'twitter', 'own_id');
146 $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
147 intval($notification_data["uid"]),
148 DBA::escape("twitter::".$own_id)
152 $notification_data["profiles"][] = $own_user[0]["url"];
156 function twitter_follow(App $a, array &$contact)
158 Logger::log("twitter_follow: Check if contact is twitter contact. " . $contact["url"], Logger::DEBUG);
160 if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
164 // contact seems to be a twitter contact, so continue
165 $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
166 $nickname = str_replace("@twitter.com", "", $nickname);
168 $uid = $a->user["uid"];
170 $ckey = Config::get('twitter', 'consumerkey');
171 $csecret = Config::get('twitter', 'consumersecret');
172 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
173 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
175 // If the addon is not configured (general or for this user) quit here
176 if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
181 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
182 $connection->post('friendships/create', ['screen_name' => $nickname]);
184 twitter_fetchuser($a, $uid, $nickname);
186 $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey
187 FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
189 DBA::escape($nickname));
190 if (DBA::isResult($r)) {
191 $contact["contact"] = $r[0];
195 function twitter_jot_nets(App $a, &$b)
201 $tw_post = PConfig::get(local_user(), 'twitter', 'post');
202 if (intval($tw_post) == 1) {
203 $tw_defpost = PConfig::get(local_user(), 'twitter', 'post_by_default');
204 $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
205 $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> '
206 . L10n::t('Post to Twitter') . '</div>';
210 function twitter_settings_post(App $a)
215 // don't check twitter settings if twitter submit button is not clicked
216 if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
220 if (!empty($_POST['twitter-disconnect'])) {
222 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
223 * from the user configuration
225 PConfig::delete(local_user(), 'twitter', 'consumerkey');
226 PConfig::delete(local_user(), 'twitter', 'consumersecret');
227 PConfig::delete(local_user(), 'twitter', 'oauthtoken');
228 PConfig::delete(local_user(), 'twitter', 'oauthsecret');
229 PConfig::delete(local_user(), 'twitter', 'post');
230 PConfig::delete(local_user(), 'twitter', 'post_by_default');
231 PConfig::delete(local_user(), 'twitter', 'lastid');
232 PConfig::delete(local_user(), 'twitter', 'mirror_posts');
233 PConfig::delete(local_user(), 'twitter', 'import');
234 PConfig::delete(local_user(), 'twitter', 'create_user');
235 PConfig::delete(local_user(), 'twitter', 'own_id');
237 if (isset($_POST['twitter-pin'])) {
238 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
239 Logger::log('got a Twitter PIN');
240 $ckey = Config::get('twitter', 'consumerkey');
241 $csecret = Config::get('twitter', 'consumersecret');
242 // the token and secret for which the PIN was generated were hidden in the settings
243 // form as token and token2, we need a new connection to Twitter using these token
244 // and secret to request a Access Token with the PIN
246 if (empty($_POST['twitter-pin'])) {
247 throw new Exception(L10n::t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.'));
250 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
251 $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]);
252 // ok, now that we have the Access Token, save them in the user config
253 PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
254 PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
255 PConfig::set(local_user(), 'twitter', 'post', 1);
256 } catch(Exception $e) {
257 info($e->getMessage());
258 } catch(TwitterOAuthException $e) {
259 info($e->getMessage());
261 // reload the Addon Settings page, if we don't do it see Bug #42
262 $a->internalRedirect('settings/connectors');
264 // if no PIN is supplied in the POST variables, the user has changed the setting
265 // to post a tweet for every new __public__ posting to the wall
266 PConfig::set(local_user(), 'twitter', 'post', intval($_POST['twitter-enable']));
267 PConfig::set(local_user(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
268 PConfig::set(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
269 PConfig::set(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
270 PConfig::set(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
272 if (!intval($_POST['twitter-mirror'])) {
273 PConfig::delete(local_user(), 'twitter', 'lastid');
276 info(L10n::t('Twitter settings updated.') . EOL);
281 function twitter_settings(App $a, &$s)
286 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
288 * 1) Check that we have global consumer key & secret
289 * 2) If no OAuthtoken & stuff is present, generate button to get some
290 * 3) Checkbox for "Send public notices (280 chars only)
292 $ckey = Config::get('twitter', 'consumerkey');
293 $csecret = Config::get('twitter', 'consumersecret');
294 $otoken = PConfig::get(local_user(), 'twitter', 'oauthtoken');
295 $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret');
297 $enabled = intval(PConfig::get(local_user(), 'twitter', 'post'));
298 $defenabled = intval(PConfig::get(local_user(), 'twitter', 'post_by_default'));
299 $mirrorenabled = intval(PConfig::get(local_user(), 'twitter', 'mirror_posts'));
300 $importenabled = intval(PConfig::get(local_user(), 'twitter', 'import'));
301 $create_userenabled = intval(PConfig::get(local_user(), 'twitter', 'create_user'));
303 $css = (($enabled) ? '' : '-disabled');
305 $s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
306 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
308 $s .= '<div id="settings_twitter_expanded" class="settings-block" style="display: none;">';
309 $s .= '<span class="fakelink" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
310 $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
313 if ((!$ckey) && (!$csecret)) {
314 /* no global consumer keys
315 * display warning and skip personal config
317 $s .= '<p>' . L10n::t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
319 // ok we have a consumer key pair now look into the OAuth stuff
320 if ((!$otoken) && (!$osecret)) {
321 /* the user has not yet connected the account to twitter...
322 * get a temporary OAuth key/secret pair and display a button with
323 * which the user can request a PIN to connect the account to a
324 * account at Twitter.
326 $connection = new TwitterOAuth($ckey, $csecret);
328 $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
329 $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>';
330 $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>';
331 $s .= '<div id="twitter-pin-wrapper">';
332 $s .= '<label id="twitter-pin-label" for="twitter-pin">' . L10n::t('Copy the PIN from Twitter here') . '</label>';
333 $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
334 $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result['oauth_token'] . '" />';
335 $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result['oauth_token_secret'] . '" />';
336 $s .= '</div><div class="clear"></div>';
337 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
338 } catch (TwitterOAuthException $e) {
339 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
343 * we have an OAuth key / secret pair for the user
344 * so let's give a chance to disable the postings to Twitter
346 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
348 $details = $connection->get('account/verify_credentials');
350 $field_checkbox = get_markup_template('field_checkbox.tpl');
352 $s .= '<div id="twitter-info" >
353 <p>' . L10n::t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
354 <button type="submit" name="twitter-disconnect" value="1">' . L10n::t('Disconnect') . '</button>
356 <p id="twitter-info-block">
357 <a href="https://twitter.com/' . $details->screen_name . '" target="_twitter"><img id="twitter-avatar" src="' . $details->profile_image_url . '" /></a>
358 <em>' . $details->description . '</em>
361 $s .= '<div class="clear"></div>';
363 $s .= replace_macros($field_checkbox, [
364 '$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.')]
366 if ($a->user['hidewall']) {
367 $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>';
369 $s .= replace_macros($field_checkbox, [
370 '$field' => ['twitter-default', L10n::t('Send public postings to Twitter by default'), $defenabled, '']
372 $s .= replace_macros($field_checkbox, [
373 '$field' => ['twitter-mirror', L10n::t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
375 $s .= replace_macros($field_checkbox, [
376 '$field' => ['twitter-import', L10n::t('Import the remote timeline'), $importenabled, '']
378 $s .= replace_macros($field_checkbox, [
379 '$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.')]
381 $s .= '<div class="clear"></div>';
382 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
383 } catch (TwitterOAuthException $e) {
384 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
388 $s .= '</div><div class="clear"></div>';
391 function twitter_post_local(App $a, array &$b)
397 if (!local_user() || (local_user() != $b['uid'])) {
401 $twitter_post = intval(PConfig::get(local_user(), 'twitter', 'post'));
402 $twitter_enable = (($twitter_post && x($_REQUEST, 'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
404 // if API is used, default to the chosen settings
405 if ($b['api_source'] && intval(PConfig::get(local_user(), 'twitter', 'post_by_default'))) {
409 if (!$twitter_enable) {
413 if (strlen($b['postopts'])) {
414 $b['postopts'] .= ',';
417 $b['postopts'] .= 'twitter';
420 function twitter_action(App $a, $uid, $pid, $action)
422 $ckey = Config::get('twitter', 'consumerkey');
423 $csecret = Config::get('twitter', 'consumersecret');
424 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
425 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
427 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
429 $post = ['id' => $pid];
431 Logger::log("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), Logger::DATA);
435 // To-Do: $result = $connection->post('statuses/destroy', $post);
439 $result = $connection->post('favorites/create', $post);
442 $result = $connection->post('favorites/destroy', $post);
445 Logger::log('Unhandled action ' . $action, Logger::DEBUG);
448 Logger::log("twitter_action '" . $action . "' send, result: " . print_r($result, true), Logger::DEBUG);
451 function twitter_post_hook(App $a, array &$b)
454 if (!PConfig::get($b["uid"], 'twitter', 'import')
455 && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
459 if ($b['parent'] != $b['id']) {
460 Logger::log("twitter_post_hook: parameter " . print_r($b, true), Logger::DATA);
462 // Looking if its a reply to a twitter post
463 if ((substr($b["parent-uri"], 0, 9) != "twitter::")
464 && (substr($b["extid"], 0, 9) != "twitter::")
465 && (substr($b["thr-parent"], 0, 9) != "twitter::"))
467 Logger::log("twitter_post_hook: no twitter post " . $b["parent"]);
471 $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
472 $orig_post = Item::selectFirst([], $condition);
473 if (!DBA::isResult($orig_post)) {
474 Logger::log("twitter_post_hook: no parent found " . $b["thr-parent"]);
481 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
482 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
483 $nicknameplain = "@" . $nicknameplain;
485 Logger::log("twitter_post_hook: comparing " . $nickname . " and " . $nicknameplain . " with " . $b["body"], Logger::DEBUG);
486 if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
487 $b["body"] = $nickname . " " . $b["body"];
490 Logger::log("twitter_post_hook: parent found " . print_r($orig_post, true), Logger::DATA);
494 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
498 // Dont't post if the post doesn't belong to us.
499 // This is a check for forum postings
500 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
501 if ($b['contact-id'] != $self['id']) {
506 if (($b['verb'] == ACTIVITY_POST) && $b['deleted']) {
507 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
510 if ($b['verb'] == ACTIVITY_LIKE) {
511 Logger::log("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), Logger::DEBUG);
513 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
515 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
521 if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
525 // if post comes from twitter don't send it back
526 if ($b['extid'] == Protocol::TWITTER) {
530 if ($b['app'] == "Twitter") {
534 Logger::log('twitter post invoked');
536 PConfig::load($b['uid'], 'twitter');
538 $ckey = Config::get('twitter', 'consumerkey');
539 $csecret = Config::get('twitter', 'consumersecret');
540 $otoken = PConfig::get($b['uid'], 'twitter', 'oauthtoken');
541 $osecret = PConfig::get($b['uid'], 'twitter', 'oauthsecret');
543 if ($ckey && $csecret && $otoken && $osecret) {
544 Logger::log('twitter: we have customer key and oauth stuff, going to send.', Logger::DEBUG);
546 // If it's a repeated message from twitter then do a native retweet and exit
547 if (twitter_is_retweet($a, $b['uid'], $b['body'])) {
551 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
553 // Set the timeout for upload to 30 seconds
554 $connection->setTimeouts(10, 30);
558 // Handling non-native reshares
559 $b['body'] = Friendica\Content\Text\BBCode::convertShare(
561 function (array $attributes, array $author_contact, $content, $is_quote_share) {
562 return twitter_convert_share($attributes, $author_contact, $content, $is_quote_share);
566 $b['body'] = twitter_update_mentions($b['body']);
568 $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
569 $msg = $msgarr["text"];
571 if (($msg == "") && isset($msgarr["title"])) {
572 $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
577 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
578 $msg .= "\n" . $msgarr["url"];
584 if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
585 $image = $msgarr["image"];
592 // and now tweet it :-)
595 if (!empty($image)) {
597 $img_str = Network::fetchUrl($image);
599 $tempfile = tempnam(get_temppath(), 'cache');
600 file_put_contents($tempfile, $img_str);
602 $media = $connection->upload('media/upload', ['media' => $tempfile]);
606 if (isset($media->media_id_string)) {
607 $post['media_ids'] = $media->media_id_string;
609 throw new Exception('Failed upload of ' . $image);
611 } catch (Exception $e) {
612 Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage());
614 // Workaround: Remove the picture link so that the post can be reposted without it
615 // When there is another url already added, a second url would be superfluous.
617 $msg .= "\n" . $image;
624 $post['status'] = $msg;
627 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
630 $url = 'statuses/update';
631 $result = $connection->post($url, $post);
632 Logger::log('twitter_post send, result: ' . print_r($result, true), Logger::DEBUG);
634 if (!empty($result->source)) {
635 Config::set("twitter", "application_name", strip_tags($result->source));
638 if (!empty($result->errors)) {
639 Logger::log('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
641 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
642 if (DBA::isResult($r)) {
643 $a->contact = $r[0]["id"];
646 $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
648 Queue::add($a->contact, Protocol::TWITTER, $s);
649 notice(L10n::t('Twitter post failed. Queued for retry.') . EOL);
650 } elseif ($iscomment) {
651 Logger::log('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
652 Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
657 function twitter_addon_admin_post(App $a)
659 $consumerkey = x($_POST, 'consumerkey') ? notags(trim($_POST['consumerkey'])) : '';
660 $consumersecret = x($_POST, 'consumersecret') ? notags(trim($_POST['consumersecret'])) : '';
661 Config::set('twitter', 'consumerkey', $consumerkey);
662 Config::set('twitter', 'consumersecret', $consumersecret);
663 info(L10n::t('Settings updated.') . EOL);
666 function twitter_addon_admin(App $a, &$o)
668 $t = get_markup_template("admin.tpl", "addon/twitter/");
670 $o = replace_macros($t, [
671 '$submit' => L10n::t('Save Settings'),
672 // name, label, value, help, [extra values]
673 '$consumerkey' => ['consumerkey', L10n::t('Consumer key'), Config::get('twitter', 'consumerkey'), ''],
674 '$consumersecret' => ['consumersecret', L10n::t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''],
678 function twitter_cron(App $a)
680 $last = Config::get('twitter', 'last_poll');
682 $poll_interval = intval(Config::get('twitter', 'poll_interval'));
683 if (!$poll_interval) {
684 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
688 $next = $last + ($poll_interval * 60);
689 if ($next > time()) {
690 Logger::log('twitter: poll intervall not reached');
694 Logger::log('twitter: cron_start');
696 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
697 if (DBA::isResult($r)) {
698 foreach ($r as $rr) {
699 Logger::log('twitter: fetching for user ' . $rr['uid']);
700 Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
704 $abandon_days = intval(Config::get('system', 'account_abandon_days'));
705 if ($abandon_days < 1) {
709 $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
711 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
712 if (DBA::isResult($r)) {
713 foreach ($r as $rr) {
714 if ($abandon_days != 0) {
715 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
716 if (!DBA::isResult($user)) {
717 Logger::log('abandoned account: timeline from user ' . $rr['uid'] . ' will not be imported');
722 Logger::log('twitter: importing timeline from user ' . $rr['uid']);
723 Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
726 // check for new contacts once a day
727 $last_contact_check = PConfig::get($rr['uid'],'pumpio','contact_check');
728 if($last_contact_check)
729 $next_contact_check = $last_contact_check + 86400;
731 $next_contact_check = 0;
733 if($next_contact_check <= time()) {
734 pumpio_getallusers($a, $rr["uid"]);
735 PConfig::set($rr['uid'],'pumpio','contact_check',time());
741 Logger::log('twitter: cron_end');
743 Config::set('twitter', 'last_poll', time());
746 function twitter_expire(App $a)
748 $days = Config::get('twitter', 'expire');
754 $r = Item::select(['id'], ['deleted' => true, 'network' => Protocol::TWITTER]);
755 while ($row = DBA::fetch($r)) {
756 DBA::delete('item', ['id' => $row['id']]);
760 require_once "include/items.php";
762 Logger::log('twitter_expire: expire_start');
764 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
765 if (DBA::isResult($r)) {
766 foreach ($r as $rr) {
767 Logger::log('twitter_expire: user ' . $rr['uid']);
768 Item::expire($rr['uid'], $days, Protocol::TWITTER, true);
772 Logger::log('twitter_expire: expire_end');
775 function twitter_prepare_body(App $a, array &$b)
777 if ($b["item"]["network"] != Protocol::TWITTER) {
784 $item["plink"] = $a->getBaseURL() . "/display/" . $a->user["nickname"] . "/" . $item["parent"];
786 $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
787 $orig_post = Item::selectFirst(['author-link'], $condition);
788 if (DBA::isResult($orig_post)) {
789 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
790 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
791 $nicknameplain = "@" . $nicknameplain;
793 if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) {
794 $item["body"] = $nickname . " " . $item["body"];
798 $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, 8);
799 $msg = $msgarr["text"];
801 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
802 $msg .= " " . $msgarr["url"];
805 if (isset($msgarr["image"])) {
806 $msg .= " " . $msgarr["image"];
809 $b['html'] = nl2br(htmlspecialchars($msg));
814 * @brief Build the item array for the mirrored post
816 * @param App $a Application class
817 * @param integer $uid User id
818 * @param object $post Twitter object with the post
820 * @return array item data to be posted
822 function twitter_do_mirrorpost(App $a, $uid, $post)
824 $datarray['api_source'] = true;
825 $datarray['profile_uid'] = $uid;
826 $datarray['extid'] = Protocol::TWITTER;
827 $datarray['message_id'] = Item::newURI($uid, Protocol::TWITTER . ':' . $post->id);
828 $datarray['protocol'] = Conversation::PARCEL_TWITTER;
829 $datarray['source'] = json_encode($post);
830 $datarray['title'] = '';
832 if (!empty($post->retweeted_status)) {
833 // We don't support nested shares, so we mustn't show quotes as shares on retweets
834 $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true);
836 if (empty($item['body'])) {
840 $datarray['body'] = "\n" . share_header(
841 $item['author-name'],
842 $item['author-link'],
843 $item['author-avatar'],
849 $datarray['body'] .= $item['body'] . '[/share]';
851 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
853 if (empty($item['body'])) {
857 $datarray['body'] = $item['body'];
860 $datarray['source'] = $item['app'];
861 $datarray['verb'] = $item['verb'];
863 if (isset($item['location'])) {
864 $datarray['location'] = $item['location'];
867 if (isset($item['coord'])) {
868 $datarray['coord'] = $item['coord'];
874 function twitter_fetchtimeline(App $a, $uid)
876 $ckey = Config::get('twitter', 'consumerkey');
877 $csecret = Config::get('twitter', 'consumersecret');
878 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
879 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
880 $lastid = PConfig::get($uid, 'twitter', 'lastid');
882 $application_name = Config::get('twitter', 'application_name');
884 if ($application_name == "") {
885 $application_name = $a->getHostName();
888 $has_picture = false;
890 require_once 'mod/item.php';
891 require_once 'include/items.php';
892 require_once 'mod/share.php';
894 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
896 $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
898 $first_time = ($lastid == "");
901 $parameters["since_id"] = $lastid;
905 $items = $connection->get('statuses/user_timeline', $parameters);
906 } catch (TwitterOAuthException $e) {
907 Logger::log('Error fetching timeline for user ' . $uid . ': ' . $e->getMessage());
911 if (!is_array($items)) {
912 Logger::log('No items for user ' . $uid, Logger::INFO);
916 $posts = array_reverse($items);
918 Logger::log('Starting from ID ' . $lastid . ' for user ' . $uid, Logger::DEBUG);
921 foreach ($posts as $post) {
922 if ($post->id_str > $lastid) {
923 $lastid = $post->id_str;
924 PConfig::set($uid, 'twitter', 'lastid', $lastid);
931 if (!stristr($post->source, $application_name)) {
932 $_SESSION["authenticated"] = true;
933 $_SESSION["uid"] = $uid;
935 Logger::log('Preparing Twitter ID ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
937 $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
939 if (empty($_REQUEST['body'])) {
943 Logger::log('Posting Twitter ID ' . $post->id_str . ' for user ' . $uid);
949 PConfig::set($uid, 'twitter', 'lastid', $lastid);
950 Logger::log('Last ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
953 function twitter_queue_hook(App $a)
955 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
956 DBA::escape(Protocol::TWITTER)
958 if (!DBA::isResult($qi)) {
962 foreach ($qi as $x) {
963 if ($x['network'] !== Protocol::TWITTER) {
967 Logger::log('twitter_queue: run');
969 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
970 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
973 if (!DBA::isResult($r)) {
979 $ckey = Config::get('twitter', 'consumerkey');
980 $csecret = Config::get('twitter', 'consumersecret');
981 $otoken = PConfig::get($user['uid'], 'twitter', 'oauthtoken');
982 $osecret = PConfig::get($user['uid'], 'twitter', 'oauthsecret');
986 if ($ckey && $csecret && $otoken && $osecret) {
987 Logger::log('twitter_queue: able to post');
989 $z = unserialize($x['content']);
991 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
992 $result = $connection->post($z['url'], $z['post']);
994 Logger::log('twitter_queue: post result: ' . print_r($result, true), Logger::DEBUG);
996 if ($result->errors) {
997 Logger::log('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"');
1000 Queue::removeItem($x['id']);
1003 Logger::log("twitter_queue: Error getting tokens for user " . $user['uid']);
1007 Logger::log('twitter_queue: delayed');
1008 Queue::updateTime($x['id']);
1013 function twitter_fix_avatar($avatar)
1015 $new_avatar = str_replace("_normal.", ".", $avatar);
1017 $info = Image::getInfoFromURL($new_avatar);
1019 $new_avatar = $avatar;
1025 function twitter_fetch_contact($uid, $data, $create_user)
1027 if (empty($data->id_str)) {
1031 $avatar = twitter_fix_avatar($data->profile_image_url_https);
1032 $url = "https://twitter.com/" . $data->screen_name;
1033 $addr = $data->screen_name . "@twitter.com";
1035 GContact::update(["url" => $url, "network" => Protocol::TWITTER,
1036 "photo" => $avatar, "hide" => true,
1037 "name" => $data->name, "nick" => $data->screen_name,
1038 "location" => $data->location, "about" => $data->description,
1039 "addr" => $addr, "generation" => 2]);
1041 $fields = ['url' => $url, 'network' => Protocol::TWITTER,
1042 'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
1043 'location' => $data->location, 'about' => $data->description];
1045 $cid = Contact::getIdForURL($url, 0, true, $fields);
1047 DBA::update('contact', $fields, ['id' => $cid]);
1048 Contact::updateAvatar($avatar, 0, $cid);
1051 $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
1052 if (!DBA::isResult($contact) && !$create_user) {
1056 if (!DBA::isResult($contact)) {
1057 // create contact record
1058 $fields['uid'] = $uid;
1059 $fields['created'] = DateTimeFormat::utcNow();
1060 $fields['nurl'] = normalise_link($url);
1061 $fields['alias'] = 'twitter::' . $data->id_str;
1062 $fields['poll'] = 'twitter::' . $data->id_str;
1063 $fields['rel'] = Contact::FRIEND;
1064 $fields['priority'] = 1;
1065 $fields['writable'] = true;
1066 $fields['blocked'] = false;
1067 $fields['readonly'] = false;
1068 $fields['pending'] = false;
1070 if (!DBA::insert('contact', $fields)) {
1074 $contact_id = DBA::lastInsertId();
1076 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1078 Contact::updateAvatar($avatar, $uid, $contact_id);
1080 if ($contact["readonly"] || $contact["blocked"]) {
1081 Logger::log("twitter_fetch_contact: Contact '" . $contact["nick"] . "' is blocked or readonly.", Logger::DEBUG);
1085 $contact_id = $contact['id'];
1087 // update profile photos once every twelve hours as we have no notification of when they change.
1088 $update_photo = ($contact['avatar-date'] < DateTimeFormat::utc('now -12 hours'));
1090 // check that we have all the photos, this has been known to fail on occasion
1091 if (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']) || $update_photo) {
1092 Logger::log("twitter_fetch_contact: Updating contact " . $data->screen_name, Logger::DEBUG);
1094 Contact::updateAvatar($avatar, $uid, $contact['id']);
1096 $fields['name-date'] = DateTimeFormat::utcNow();
1097 $fields['uri-date'] = DateTimeFormat::utcNow();
1099 DBA::update('contact', $fields, ['id' => $contact['id']]);
1106 function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
1108 $ckey = Config::get('twitter', 'consumerkey');
1109 $csecret = Config::get('twitter', 'consumersecret');
1110 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1111 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1113 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1116 if (DBA::isResult($r)) {
1124 if ($screen_name != "") {
1125 $parameters["screen_name"] = $screen_name;
1128 if ($user_id != "") {
1129 $parameters["user_id"] = $user_id;
1132 // Fetching user data
1133 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1135 $user = $connection->get('users/show', $parameters);
1136 } catch (TwitterOAuthException $e) {
1137 Logger::log('twitter_fetchuser: Error fetching user ' . $uid . ': ' . $e->getMessage());
1141 if (!is_object($user)) {
1145 $contact_id = twitter_fetch_contact($uid, $user, true);
1150 function twitter_expand_entities(App $a, $body, $item, $picture)
1156 foreach ($item->entities->hashtags AS $hashtag) {
1157 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . rawurlencode($hashtag->text) . ']' . $hashtag->text . '[/url]';
1158 $tags_arr['#' . $hashtag->text] = $url;
1159 $body = str_replace('#' . $hashtag->text, $url, $body);
1162 foreach ($item->entities->user_mentions AS $mention) {
1163 $url = '@[url=https://twitter.com/' . rawurlencode($mention->screen_name) . ']' . $mention->screen_name . '[/url]';
1164 $tags_arr['@' . $mention->screen_name] = $url;
1165 $body = str_replace('@' . $mention->screen_name, $url, $body);
1168 if (isset($item->entities->urls)) {
1174 foreach ($item->entities->urls as $url) {
1175 $plain = str_replace($url->url, '', $plain);
1177 if ($url->url && $url->expanded_url && $url->display_url) {
1178 // Quote tweet, we just remove the quoted tweet URL from the body, the share block will be added later.
1179 if (isset($item->quoted_status_id_str)
1180 && substr($url->expanded_url, -strlen($item->quoted_status_id_str)) == $item->quoted_status_id_str ) {
1181 $body = str_replace($url->url, '', $body);
1185 $expanded_url = Network::finalUrl($url->expanded_url);
1187 $oembed_data = OEmbed::fetchURL($expanded_url);
1189 if (empty($oembed_data) || empty($oembed_data->type)) {
1193 // Quickfix: Workaround for URL with '[' and ']' in it
1194 if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
1195 $expanded_url = $url->url;
1199 $type = $oembed_data->type;
1202 if ($oembed_data->type == 'video') {
1203 $type = $oembed_data->type;
1204 $footerurl = $expanded_url;
1205 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1207 $body = str_replace($url->url, $footerlink, $body);
1208 } elseif (($oembed_data->type == 'photo') && isset($oembed_data->url)) {
1209 $body = str_replace($url->url, '[url=' . $expanded_url . '][img]' . $oembed_data->url . '[/img][/url]', $body);
1210 } elseif ($oembed_data->type != 'link') {
1211 $body = str_replace($url->url, '[url=' . $expanded_url . ']' . $url->display_url . '[/url]', $body);
1213 $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4);
1215 $tempfile = tempnam(get_temppath(), 'cache');
1216 file_put_contents($tempfile, $img_str);
1218 // See http://php.net/manual/en/function.exif-imagetype.php#79283
1219 if (filesize($tempfile) > 11) {
1220 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
1227 if (substr($mime, 0, 6) == 'image/') {
1229 $body = str_replace($url->url, '[img]' . $expanded_url . '[/img]', $body);
1231 $type = $oembed_data->type;
1232 $footerurl = $expanded_url;
1233 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1235 $body = str_replace($url->url, $footerlink, $body);
1241 // Footer will be taken care of with a share block in the case of a quote
1242 if (empty($item->quoted_status)) {
1243 if ($footerurl != '') {
1244 $footer = add_page_info($footerurl, false, $picture);
1247 if (($footerlink != '') && (trim($footer) != '')) {
1248 $removedlink = trim(str_replace($footerlink, '', $body));
1250 if (($removedlink == '') || strstr($body, $removedlink)) {
1251 $body = $removedlink;
1257 if ($footer == '' && $picture != '') {
1258 $body .= "\n\n[img]" . $picture . "[/img]\n";
1259 } elseif ($footer == '' && $picture == '') {
1260 $body = add_page_info_to_body($body);
1265 // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
1266 $tags = get_tags($body);
1269 foreach ($tags as $tag) {
1270 if (strstr(trim($tag), ' ')) {
1274 if (strpos($tag, '#') === 0) {
1275 if (strpos($tag, '[url=')) {
1279 // don't link tags that are already embedded in links
1280 if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
1283 if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
1287 $basetag = str_replace('_', ' ', substr($tag, 1));
1288 $url = '#[url=' . $a->getBaseURL() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1289 $body = str_replace($tag, $url, $body);
1290 $tags_arr['#' . $basetag] = $url;
1291 } elseif (strpos($tag, '@') === 0) {
1292 if (strpos($tag, '[url=')) {
1296 $basetag = substr($tag, 1);
1297 $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1298 $body = str_replace($tag, $url, $body);
1299 $tags_arr['@' . $basetag] = $url;
1304 $tags = implode($tags_arr, ',');
1306 return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
1310 * @brief Fetch media entities and add media links to the body
1312 * @param object $post Twitter object with the post
1313 * @param array $postarray Array of the item that is about to be posted
1315 * @return $picture string Image URL or empty string
1317 function twitter_media_entities($post, array &$postarray)
1319 // There are no media entities? So we quit.
1320 if (empty($post->extended_entities->media)) {
1324 // When the post links to an external page, we only take one picture.
1325 // We only do this when there is exactly one media.
1326 if ((count($post->entities->urls) > 0) && (count($post->extended_entities->media) == 1)) {
1327 $medium = $post->extended_entities->media[0];
1329 foreach ($post->entities->urls as $link) {
1330 // Let's make sure the external link url matches the media url
1331 if ($medium->url == $link->url && isset($medium->media_url_https)) {
1332 $picture = $medium->media_url_https;
1333 $postarray['body'] = str_replace($medium->url, '', $postarray['body']);
1339 // This is a pure media post, first search for all media urls
1341 foreach ($post->extended_entities->media AS $medium) {
1342 if (!isset($media[$medium->url])) {
1343 $media[$medium->url] = '';
1345 switch ($medium->type) {
1347 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1348 $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1351 case 'animated_gif':
1352 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1353 $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
1354 if (is_array($medium->video_info->variants)) {
1356 // We take the video with the highest bitrate
1357 foreach ($medium->video_info->variants AS $variant) {
1358 if (($variant->content_type == 'video/mp4') && ($variant->bitrate >= $bitrate)) {
1359 $media[$medium->url] = "\n[video]" . $variant->url . '[/video]';
1360 $bitrate = $variant->bitrate;
1365 // The following code will only be activated for test reasons
1367 // $postarray['body'] .= print_r($medium, true);
1371 // Now we replace the media urls.
1372 foreach ($media AS $key => $value) {
1373 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1379 function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $only_existing_contact, $noquote)
1382 $postarray['network'] = Protocol::TWITTER;
1383 $postarray['uid'] = $uid;
1384 $postarray['wall'] = 0;
1385 $postarray['uri'] = "twitter::" . $post->id_str;
1386 $postarray['protocol'] = Conversation::PARCEL_TWITTER;
1387 $postarray['source'] = json_encode($post);
1389 // Don't import our own comments
1390 if (Item::exists(['extid' => $postarray['uri'], 'uid' => $uid])) {
1391 Logger::log("Item with extid " . $postarray['uri'] . " found.", Logger::DEBUG);
1397 if ($post->in_reply_to_status_id_str != "") {
1398 $parent = "twitter::" . $post->in_reply_to_status_id_str;
1400 $fields = ['uri', 'parent-uri', 'parent'];
1401 $parent_item = Item::selectFirst($fields, ['uri' => $parent, 'uid' => $uid]);
1402 if (!DBA::isResult($parent_item)) {
1403 $parent_item = Item::selectFirst($fields, ['extid' => $parent, 'uid' => $uid]);
1406 if (DBA::isResult($parent_item)) {
1407 $postarray['thr-parent'] = $parent_item['uri'];
1408 $postarray['parent-uri'] = $parent_item['parent-uri'];
1409 $postarray['parent'] = $parent_item['parent'];
1410 $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1412 $postarray['thr-parent'] = $postarray['uri'];
1413 $postarray['parent-uri'] = $postarray['uri'];
1414 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1418 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1420 if ($post->user->id_str == $own_id) {
1421 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1424 if (DBA::isResult($r)) {
1425 $contactid = $r[0]["id"];
1427 $postarray['owner-name'] = $r[0]["name"];
1428 $postarray['owner-link'] = $r[0]["url"];
1429 $postarray['owner-avatar'] = $r[0]["photo"];
1431 Logger::log("No self contact for user " . $uid, Logger::DEBUG);
1435 // Don't create accounts of people who just comment something
1436 $create_user = false;
1438 $postarray['parent-uri'] = $postarray['uri'];
1439 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1442 if ($contactid == 0) {
1443 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1445 $postarray['owner-name'] = $post->user->name;
1446 $postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
1447 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1450 if (($contactid == 0) && !$only_existing_contact) {
1451 $contactid = $self['id'];
1452 } elseif ($contactid <= 0) {
1453 Logger::log("Contact ID is zero or less than zero.", Logger::DEBUG);
1457 $postarray['contact-id'] = $contactid;
1459 $postarray['verb'] = ACTIVITY_POST;
1460 $postarray['author-name'] = $postarray['owner-name'];
1461 $postarray['author-link'] = $postarray['owner-link'];
1462 $postarray['author-avatar'] = $postarray['owner-avatar'];
1463 $postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
1464 $postarray['app'] = strip_tags($post->source);
1466 if ($post->user->protected) {
1467 $postarray['private'] = 1;
1468 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1470 $postarray['private'] = 0;
1471 $postarray['allow_cid'] = '';
1474 if (!empty($post->full_text)) {
1475 $postarray['body'] = $post->full_text;
1477 $postarray['body'] = $post->text;
1480 // When the post contains links then use the correct object type
1481 if (count($post->entities->urls) > 0) {
1482 $postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
1485 // Search for media links
1486 $picture = twitter_media_entities($post, $postarray);
1488 $converted = twitter_expand_entities($a, $postarray['body'], $post, $picture);
1489 $postarray['body'] = $converted["body"];
1490 $postarray['tag'] = $converted["tags"];
1491 $postarray['created'] = DateTimeFormat::utc($post->created_at);
1492 $postarray['edited'] = DateTimeFormat::utc($post->created_at);
1494 $statustext = $converted["plain"];
1496 if (!empty($post->place->name)) {
1497 $postarray["location"] = $post->place->name;
1499 if (!empty($post->place->full_name)) {
1500 $postarray["location"] = $post->place->full_name;
1502 if (!empty($post->geo->coordinates)) {
1503 $postarray["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
1505 if (!empty($post->coordinates->coordinates)) {
1506 $postarray["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
1508 if (!empty($post->retweeted_status)) {
1509 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1511 if (empty($retweet['body'])) {
1515 $retweet['source'] = $postarray['source'];
1516 $retweet['private'] = $postarray['private'];
1517 $retweet['allow_cid'] = $postarray['allow_cid'];
1518 $retweet['contact-id'] = $postarray['contact-id'];
1519 $retweet['owner-name'] = $postarray['owner-name'];
1520 $retweet['owner-link'] = $postarray['owner-link'];
1521 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1523 $postarray = $retweet;
1526 if (!empty($post->quoted_status) && !$noquote) {
1527 $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
1529 if (empty($quoted['body'])) {
1533 $postarray['body'] .= "\n" . share_header(
1534 $quoted['author-name'],
1535 $quoted['author-link'],
1536 $quoted['author-avatar'],
1542 $postarray['body'] .= $quoted['body'] . '[/share]';
1548 function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection, array $self)
1550 Logger::log("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, Logger::DEBUG);
1554 while (!empty($post->in_reply_to_status_id_str)) {
1555 $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str];
1558 $post = $connection->get('statuses/show', $parameters);
1559 } catch (TwitterOAuthException $e) {
1560 Logger::log('twitter_fetchparentposts: Error fetching for user ' . $uid . ' and post ' . $post->id_str . ': ' . $e->getMessage());
1565 Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters->id, Logger::DEBUG);
1569 if (empty($post->id_str)) {
1570 Logger::log("twitter_fetchparentposts: This is not a post " . json_encode($post), Logger::DEBUG);
1574 if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1581 Logger::log("twitter_fetchparentposts: Fetching " . count($posts) . " parents", Logger::DEBUG);
1583 $posts = array_reverse($posts);
1585 if (!empty($posts)) {
1586 foreach ($posts as $post) {
1587 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1589 if (empty($postarray['body'])) {
1593 $item = Item::insert($postarray);
1595 $postarray["id"] = $item;
1597 Logger::log('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
1602 function twitter_fetchhometimeline(App $a, $uid)
1604 $ckey = Config::get('twitter', 'consumerkey');
1605 $csecret = Config::get('twitter', 'consumersecret');
1606 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1607 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1608 $create_user = PConfig::get($uid, 'twitter', 'create_user');
1609 $mirror_posts = PConfig::get($uid, 'twitter', 'mirror_posts');
1611 Logger::log("Fetching timeline for user " . $uid, Logger::DEBUG);
1613 $application_name = Config::get('twitter', 'application_name');
1615 if ($application_name == "") {
1616 $application_name = $a->getHostName();
1619 require_once 'include/items.php';
1621 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1624 $own_contact = twitter_fetch_own_contact($a, $uid);
1625 } catch (TwitterOAuthException $e) {
1626 Logger::log('Error fetching own contact for user ' . $uid . ': ' . $e->getMessage());
1630 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1631 intval($own_contact),
1634 if (DBA::isResult($r)) {
1635 $own_id = $r[0]["nick"];
1637 Logger::log("Own twitter contact not found for user " . $uid);
1641 $self = User::getOwnerDataById($uid);
1642 if ($self === false) {
1643 Logger::log("Own contact not found for user " . $uid);
1647 $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
1648 //$parameters["count"] = 200;
1649 // Fetching timeline
1650 $lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid');
1652 $first_time = ($lastid == "");
1654 if ($lastid != "") {
1655 $parameters["since_id"] = $lastid;
1659 $items = $connection->get('statuses/home_timeline', $parameters);
1660 } catch (TwitterOAuthException $e) {
1661 Logger::log('Error fetching home timeline for user ' . $uid . ': ' . $e->getMessage());
1665 if (!is_array($items)) {
1666 Logger::log('No array while fetching home timeline for user ' . $uid . ': ' . print_r($items, true));
1670 if (empty($items)) {
1671 Logger::log('No new timeline content for user ' . $uid, Logger::INFO);
1675 $posts = array_reverse($items);
1677 Logger::log('Fetching timeline from ID ' . $lastid . ' for user ' . $uid . ' ' . sizeof($posts) . ' items', Logger::DEBUG);
1679 if (count($posts)) {
1680 foreach ($posts as $post) {
1681 if ($post->id_str > $lastid) {
1682 $lastid = $post->id_str;
1683 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1690 if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1691 Logger::log("Skip previously sent post", Logger::DEBUG);
1695 if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1696 Logger::log("Skip post that will be mirrored", Logger::DEBUG);
1700 if ($post->in_reply_to_status_id_str != "") {
1701 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1704 Logger::log('Preparing post ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
1706 $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1708 if (empty($postarray['body']) || trim($postarray['body']) == "") {
1709 Logger::log('Empty body for post ' . $post->id_str . ' and user ' . $uid, Logger::DEBUG);
1715 if (($postarray['uri'] == $postarray['parent-uri']) && ($postarray['author-link'] == $postarray['owner-link'])) {
1716 $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
1717 if (DBA::isResult($contact)) {
1718 $notify = Item::isRemoteSelf($contact, $postarray);
1722 $item = Item::insert($postarray, false, $notify);
1723 $postarray["id"] = $item;
1725 Logger::log('User ' . $uid . ' posted home timeline item ' . $item);
1728 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1730 Logger::log('Last timeline ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1732 // Fetching mentions
1733 $lastid = PConfig::get($uid, 'twitter', 'lastmentionid');
1735 $first_time = ($lastid == "");
1737 if ($lastid != "") {
1738 $parameters["since_id"] = $lastid;
1742 $items = $connection->get('statuses/mentions_timeline', $parameters);
1743 } catch (TwitterOAuthException $e) {
1744 Logger::log('Error fetching mentions: ' . $e->getMessage());
1748 if (!is_array($items)) {
1749 Logger::log("Error fetching mentions: " . print_r($items, true), Logger::DEBUG);
1753 $posts = array_reverse($items);
1755 Logger::log("Fetching mentions for user " . $uid . " " . sizeof($posts) . " items", Logger::DEBUG);
1757 if (count($posts)) {
1758 foreach ($posts as $post) {
1759 if ($post->id_str > $lastid) {
1760 $lastid = $post->id_str;
1767 if ($post->in_reply_to_status_id_str != "") {
1768 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1771 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1773 if (empty($postarray['body'])) {
1777 $item = Item::insert($postarray);
1779 Logger::log('User ' . $uid . ' posted mention timeline item ' . $item);
1783 PConfig::set($uid, 'twitter', 'lastmentionid', $lastid);
1785 Logger::log('Last mentions ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1788 function twitter_fetch_own_contact(App $a, $uid)
1790 $ckey = Config::get('twitter', 'consumerkey');
1791 $csecret = Config::get('twitter', 'consumersecret');
1792 $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1793 $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1795 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1799 if ($own_id == "") {
1800 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1802 // Fetching user data
1803 // get() may throw TwitterOAuthException, but we will catch it later
1804 $user = $connection->get('account/verify_credentials');
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'];