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>
9 * Copyright (c) 2011-2013 Tobias Diekershoff, Michael Vogel
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above
17 * * copyright notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the distribution.
19 * * Neither the name of the <organization> nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 /* Twitter Plugin for Friendica
38 * Author: Tobias Diekershoff
39 * tobias.diekershoff@gmx.net
41 * License:3-clause BSD license
44 * To use this plugin 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 .htconfig.php or use the admin panel.
53 * $a->config['twitter']['consumerkey'] = 'your consumer_key here';
54 * $a->config['twitter']['consumersecret'] = 'your consumer_secret here';
56 * To activate the plugin itself add it to the $a->config['system']['addon']
57 * setting. After this, your user can configure their Twitter account settings
58 * from "Settings -> Plugin Settings".
60 * Requirements: PHP5, curl [Slinky library]
63 require_once('include/enotify.php');
64 require_once("include/socgraph.php");
66 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
68 function twitter_install() {
69 // we need some hooks, for the configuration and for sending tweets
70 register_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
71 register_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
72 register_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
73 register_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
74 register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
75 register_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
76 register_hook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook');
77 register_hook('follow', 'addon/twitter/twitter.php', 'twitter_follow');
78 register_hook('expire', 'addon/twitter/twitter.php', 'twitter_expire');
79 register_hook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body');
80 register_hook('check_item_notification','addon/twitter/twitter.php', 'twitter_check_item_notification');
81 logger("installed twitter");
85 function twitter_uninstall() {
86 unregister_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
87 unregister_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
88 unregister_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
89 unregister_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
90 unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
91 unregister_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
92 unregister_hook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook');
93 unregister_hook('follow', 'addon/twitter/twitter.php', 'twitter_follow');
94 unregister_hook('expire', 'addon/twitter/twitter.php', 'twitter_expire');
95 unregister_hook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body');
96 unregister_hook('check_item_notification','addon/twitter/twitter.php', 'twitter_check_item_notification');
98 // old setting - remove only
99 unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
100 unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings');
101 unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
105 function twitter_check_item_notification($a, &$notification_data) {
106 $own_id = get_pconfig($notification_data["uid"], 'twitter', 'own_id');
108 $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
109 intval($notification_data["uid"]),
110 dbesc("twitter::".$own_id)
114 $notification_data["profiles"][] = $own_user[0]["url"];
117 function twitter_follow($a, &$contact) {
119 logger("twitter_follow: Check if contact is twitter contact. ".$contact["url"], LOGGER_DEBUG);
121 if (!strstr($contact["url"], "://twitter.com") AND !strstr($contact["url"], "@twitter.com"))
124 // contact seems to be a twitter contact, so continue
125 $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
126 $nickname = str_replace("@twitter.com", "", $nickname);
128 $uid = $a->user["uid"];
130 $ckey = get_config('twitter', 'consumerkey');
131 $csecret = get_config('twitter', 'consumersecret');
132 $otoken = get_pconfig($uid, 'twitter', 'oauthtoken');
133 $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
135 require_once("addon/twitter/codebird.php");
137 $cb = \Codebird\Codebird::getInstance();
138 $cb->setConsumerKey($ckey, $csecret);
139 $cb->setToken($otoken, $osecret);
141 $parameters = array();
142 $parameters["screen_name"] = $nickname;
144 $user = $cb->friendships_create($parameters);
146 twitter_fetchuser($a, $uid, $nickname);
148 $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey
149 FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
153 $contact["contact"] = $r[0];
156 function twitter_jot_nets(&$a,&$b) {
160 $tw_post = get_pconfig(local_user(),'twitter','post');
161 if(intval($tw_post) == 1) {
162 $tw_defpost = get_pconfig(local_user(),'twitter','post_by_default');
163 $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
164 $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> '
165 . t('Post to Twitter') . '</div>';
169 function twitter_settings_post ($a,$post) {
172 // don't check twitter settings if twitter submit button is not clicked
173 if (!x($_POST,'twitter-submit'))
176 if (isset($_POST['twitter-disconnect'])) {
178 * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
179 * from the user configuration
181 del_pconfig(local_user(), 'twitter', 'consumerkey');
182 del_pconfig(local_user(), 'twitter', 'consumersecret');
183 del_pconfig(local_user(), 'twitter', 'oauthtoken');
184 del_pconfig(local_user(), 'twitter', 'oauthsecret');
185 del_pconfig(local_user(), 'twitter', 'post');
186 del_pconfig(local_user(), 'twitter', 'post_by_default');
187 del_pconfig(local_user(), 'twitter', 'lastid');
188 del_pconfig(local_user(), 'twitter', 'mirror_posts');
189 del_pconfig(local_user(), 'twitter', 'import');
190 del_pconfig(local_user(), 'twitter', 'create_user');
191 del_pconfig(local_user(), 'twitter', 'own_id');
193 if (isset($_POST['twitter-pin'])) {
194 // if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
195 logger('got a Twitter PIN');
196 require_once('library/twitteroauth.php');
197 $ckey = get_config('twitter', 'consumerkey');
198 $csecret = get_config('twitter', 'consumersecret');
199 // the token and secret for which the PIN was generated were hidden in the settings
200 // form as token and token2, we need a new connection to Twitter using these token
201 // and secret to request a Access Token with the PIN
202 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
203 $token = $connection->getAccessToken( $_POST['twitter-pin'] );
204 // ok, now that we have the Access Token, save them in the user config
205 set_pconfig(local_user(),'twitter', 'oauthtoken', $token['oauth_token']);
206 set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
207 set_pconfig(local_user(),'twitter', 'post', 1);
208 // reload the Addon Settings page, if we don't do it see Bug #42
209 goaway($a->get_baseurl().'/settings/connectors');
211 // if no PIN is supplied in the POST variables, the user has changed the setting
212 // to post a tweet for every new __public__ posting to the wall
213 set_pconfig(local_user(),'twitter','post',intval($_POST['twitter-enable']));
214 set_pconfig(local_user(),'twitter','post_by_default',intval($_POST['twitter-default']));
215 set_pconfig(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
216 set_pconfig(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
217 set_pconfig(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
219 if (!intval($_POST['twitter-mirror']))
220 del_pconfig(local_user(),'twitter','lastid');
222 info(t('Twitter settings updated.') . EOL);
225 function twitter_settings(&$a,&$s) {
228 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
230 * 1) Check that we have global consumer key & secret
231 * 2) If no OAuthtoken & stuff is present, generate button to get some
232 * 3) Checkbox for "Send public notices (140 chars only)
234 $ckey = get_config('twitter', 'consumerkey' );
235 $csecret = get_config('twitter', 'consumersecret' );
236 $otoken = get_pconfig(local_user(), 'twitter', 'oauthtoken' );
237 $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
238 $enabled = get_pconfig(local_user(), 'twitter', 'post');
239 $checked = (($enabled) ? ' checked="checked" ' : '');
240 $defenabled = get_pconfig(local_user(),'twitter','post_by_default');
241 $defchecked = (($defenabled) ? ' checked="checked" ' : '');
242 $mirrorenabled = get_pconfig(local_user(),'twitter','mirror_posts');
243 $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
244 $importenabled = get_pconfig(local_user(),'twitter','import');
245 $importchecked = (($importenabled) ? ' checked="checked" ' : '');
246 $create_userenabled = get_pconfig(local_user(),'twitter','create_user');
247 $create_userchecked = (($create_userenabled) ? ' checked="checked" ' : '');
249 $css = (($enabled) ? '' : '-disabled');
251 $s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
252 $s .= '<img class="connector'.$css.'" src="images/twitter.png" /><h3 class="connector">'. t('Twitter Import/Export/Mirror').'</h3>';
254 $s .= '<div id="settings_twitter_expanded" class="settings-block" style="display: none;">';
255 $s .= '<span class="fakelink" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
256 $s .= '<img class="connector'.$css.'" src="images/twitter.png" /><h3 class="connector">'. t('Twitter Import/Export/Mirror').'</h3>';
259 if ( (!$ckey) && (!$csecret) ) {
261 * no global consumer keys
262 * display warning and skip personal config
264 $s .= '<p>'. t('No consumer key pair for Twitter found. Please contact your site administrator.') .'</p>';
267 * ok we have a consumer key pair now look into the OAuth stuff
269 if ( (!$otoken) && (!$osecret) ) {
271 * the user has not yet connected the account to twitter...
272 * get a temporary OAuth key/secret pair and display a button with
273 * which the user can request a PIN to connect the account to a
274 * account at Twitter.
276 require_once('library/twitteroauth.php');
277 $connection = new TwitterOAuth($ckey, $csecret);
278 $request_token = $connection->getRequestToken();
279 $token = $request_token['oauth_token'];
281 * make some nice form
283 $s .= '<p>'. t('At this Friendica instance the Twitter plugin 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>';
284 $s .= '<a href="'.$connection->getAuthorizeURL($token).'" target="_twitter"><img src="addon/twitter/lighter.png" alt="'.t('Log in with Twitter').'"></a>';
285 $s .= '<div id="twitter-pin-wrapper">';
286 $s .= '<label id="twitter-pin-label" for="twitter-pin">'. t('Copy the PIN from Twitter here') .'</label>';
287 $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
288 $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="'.$token.'" />';
289 $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="'.$request_token['oauth_token_secret'].'" />';
290 $s .= '</div><div class="clear"></div>';
291 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
294 * we have an OAuth key / secret pair for the user
295 * so let's give a chance to disable the postings to Twitter
297 require_once('library/twitteroauth.php');
298 $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
299 $details = $connection->get('account/verify_credentials');
300 $s .= '<div id="twitter-info" ><img id="twitter-avatar" src="'.$details->profile_image_url.'" /><p id="twitter-info-block">'. t('Currently connected to: ') .'<a href="https://twitter.com/'.$details->screen_name.'" target="_twitter">'.$details->screen_name.'</a><br /><em>'.$details->description.'</em></p></div>';
301 $s .= '<p>'. 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.') .'</p>';
302 if ($a->user['hidewall']) {
303 $s .= '<p>'. t('<strong>Note</strong>: Due 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>';
305 $s .= '<div id="twitter-enable-wrapper">';
306 $s .= '<label id="twitter-enable-label" for="twitter-checkbox">'. t('Allow posting to Twitter'). '</label>';
307 $s .= '<input id="twitter-checkbox" type="checkbox" name="twitter-enable" value="1" ' . $checked . '/>';
308 $s .= '<div class="clear"></div>';
309 $s .= '<label id="twitter-default-label" for="twitter-default">'. t('Send public postings to Twitter by default') .'</label>';
310 $s .= '<input id="twitter-default" type="checkbox" name="twitter-default" value="1" ' . $defchecked . '/>';
311 $s .= '<div class="clear"></div>';
313 $s .= '<label id="twitter-mirror-label" for="twitter-mirror">'.t('Mirror all posts from twitter that are no replies').'</label>';
314 $s .= '<input id="twitter-mirror" type="checkbox" name="twitter-mirror" value="1" '. $mirrorchecked . '/>';
315 $s .= '<div class="clear"></div>';
318 $s .= '<label id="twitter-import-label" for="twitter-import">'.t('Import the remote timeline').'</label>';
319 $s .= '<input id="twitter-import" type="checkbox" name="twitter-import" value="1" '. $importchecked . '/>';
320 $s .= '<div class="clear"></div>';
322 $s .= '<label id="twitter-create_user-label" for="twitter-create_user">'.t('Automatically create contacts').'</label>';
323 $s .= '<input id="twitter-create_user" type="checkbox" name="twitter-create_user" value="1" '. $create_userchecked . '/>';
324 $s .= '<div class="clear"></div>';
326 $s .= '<div id="twitter-disconnect-wrapper">';
327 $s .= '<label id="twitter-disconnect-label" for="twitter-disconnect">'. t('Clear OAuth configuration') .'</label>';
328 $s .= '<input id="twitter-disconnect" type="checkbox" name="twitter-disconnect" value="1" />';
329 $s .= '</div><div class="clear"></div>';
330 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
333 $s .= '</div><div class="clear"></div>';
337 function twitter_post_local(&$a,&$b) {
342 if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent']) ) {
344 $twitter_post = intval(get_pconfig(local_user(),'twitter','post'));
345 $twitter_enable = (($twitter_post && x($_REQUEST,'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
347 // if API is used, default to the chosen settings
348 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'twitter','post_by_default')))
351 if(! $twitter_enable)
354 if(strlen($b['postopts']))
355 $b['postopts'] .= ',';
356 $b['postopts'] .= 'twitter';
360 function twitter_action($a, $uid, $pid, $action) {
362 $ckey = get_config('twitter', 'consumerkey');
363 $csecret = get_config('twitter', 'consumersecret');
364 $otoken = get_pconfig($uid, 'twitter', 'oauthtoken');
365 $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
367 require_once("addon/twitter/codebird.php");
369 $cb = \Codebird\Codebird::getInstance();
370 $cb->setConsumerKey($ckey, $csecret);
371 $cb->setToken($otoken, $osecret);
373 $post = array('id' => $pid);
375 logger("twitter_action '".$action."' ID: ".$pid." data: " . print_r($post, true), LOGGER_DATA);
379 // To-Do: $result = $cb->statuses_destroy($post);
382 $result = $cb->favorites_create($post);
385 $result = $cb->favorites_destroy($post);
388 logger("twitter_action '".$action."' send, result: " . print_r($result, true), LOGGER_DEBUG);
391 function twitter_post_hook(&$a,&$b) {
397 require_once("include/network.php");
399 if (!get_pconfig($b["uid"],'twitter','import')) {
400 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
404 if($b['parent'] != $b['id']) {
405 logger("twitter_post_hook: parameter ".print_r($b, true), LOGGER_DATA);
407 // Looking if its a reply to a twitter post
408 if ((substr($b["parent-uri"], 0, 9) != "twitter::") AND (substr($b["extid"], 0, 9) != "twitter::") AND (substr($b["thr-parent"], 0, 9) != "twitter::")) {
409 logger("twitter_post_hook: no twitter post ".$b["parent"]);
413 $r = q("SELECT * FROM item WHERE item.uri = '%s' AND item.uid = %d LIMIT 1",
414 dbesc($b["thr-parent"]),
418 logger("twitter_post_hook: no parent found ".$b["thr-parent"]);
426 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
427 $nickname = "@[url=".$orig_post["author-link"]."]".$nicknameplain."[/url]";
428 $nicknameplain = "@".$nicknameplain;
430 logger("twitter_post_hook: comparing ".$nickname." and ".$nicknameplain." with ".$b["body"], LOGGER_DEBUG);
431 if ((strpos($b["body"], $nickname) === false) AND (strpos($b["body"], $nicknameplain) === false))
432 $b["body"] = $nickname." ".$b["body"];
434 logger("twitter_post_hook: parent found ".print_r($orig_post, true), LOGGER_DATA);
438 if($b['private'] OR !strstr($b['postopts'],'twitter'))
442 if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
443 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
445 if($b['verb'] == ACTIVITY_LIKE) {
446 logger("twitter_post_hook: parameter 2 ".substr($b["thr-parent"], 9), LOGGER_DEBUG);
448 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
450 twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
454 if($b['deleted'] || ($b['created'] !== $b['edited']))
457 // if post comes from twitter don't send it back
458 if($b['extid'] == NETWORK_TWITTER)
461 if($b['app'] == "Twitter")
464 logger('twitter post invoked');
467 load_pconfig($b['uid'], 'twitter');
469 $ckey = get_config('twitter', 'consumerkey');
470 $csecret = get_config('twitter', 'consumersecret');
471 $otoken = get_pconfig($b['uid'], 'twitter', 'oauthtoken');
472 $osecret = get_pconfig($b['uid'], 'twitter', 'oauthsecret');
474 if($ckey && $csecret && $otoken && $osecret) {
475 logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG);
477 // If it's a repeated message from twitter then do a native retweet and exit
478 if (twitter_is_retweet($a, $b['uid'], $b['body']))
481 require_once('library/twitteroauth.php');
482 require_once('include/bbcode.php');
483 $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
486 require_once("include/plaintext.php");
487 $msgarr = plaintext($a, $b, $max_char, true, 8);
488 $msg = $msgarr["text"];
490 if (($msg == "") AND isset($msgarr["title"]))
491 $msg = shortenmsg($msgarr["title"], $max_char - 50);
495 if (isset($msgarr["url"]) AND ($msgarr["type"] != "photo"))
496 $msg .= "\n".$msgarr["url"];
497 elseif (isset($msgarr["image"]) AND ($msgarr["type"] != "video"))
498 $image = $msgarr["image"];
500 // and now tweet it :-)
501 if(strlen($msg) and ($image != "")) {
502 $img_str = fetch_url($image);
504 $tempfile = tempnam(get_temppath(), "cache");
505 file_put_contents($tempfile, $img_str);
507 // Twitter had changed something so that the old library doesn't work anymore
508 // so we are using a new library for twitter
510 // Switching completely to this library with all functions
511 require_once("addon/twitter/codebird.php");
513 $cb = \Codebird\Codebird::getInstance();
514 $cb->setConsumerKey($ckey, $csecret);
515 $cb->setToken($otoken, $osecret);
517 $post = array('status' => $msg, 'media[]' => $tempfile);
520 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
522 $result = $cb->statuses_updateWithMedia($post);
525 logger('twitter_post_with_media send, result: ' . print_r($result, true), LOGGER_DEBUG);
528 set_config("twitter", "application_name", strip_tags($result->source));
530 if ($result->errors OR $result->error) {
531 logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
533 // Workaround: Remove the picture link so that the post can be reposted without it
536 } elseif ($iscomment) {
537 logger('twitter_post: Update extid '.$result->id_str." for post id ".$b['id']);
538 q("UPDATE `item` SET `extid` = '%s', `body` = '%s' WHERE `id` = %d",
539 dbesc("twitter::".$result->id_str),
540 dbesc($result->text),
546 if(strlen($msg) and ($image == "")) {
547 $url = 'statuses/update';
548 $post = array('status' => $msg);
551 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
553 $result = $tweet->post($url, $post);
554 logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
557 set_config("twitter", "application_name", strip_tags($result->source));
559 if ($result->errors) {
560 logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
562 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
564 $a->contact = $r[0]["id"];
566 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $post));
567 require_once('include/queue_fn.php');
568 add_to_queue($a->contact,NETWORK_TWITTER,$s);
569 notice(t('Twitter post failed. Queued for retry.').EOL);
570 } elseif ($iscomment) {
571 logger('twitter_post: Update extid '.$result->id_str." for post id ".$b['id']);
572 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
573 dbesc("twitter::".$result->id_str),
576 //q("UPDATE `item` SET `extid` = '%s', `body` = '%s' WHERE `id` = %d",
577 // dbesc("twitter::".$result->id_str),
578 // dbesc($result->text),
586 function twitter_plugin_admin_post(&$a){
587 $consumerkey = ((x($_POST,'consumerkey')) ? notags(trim($_POST['consumerkey'])) : '');
588 $consumersecret = ((x($_POST,'consumersecret')) ? notags(trim($_POST['consumersecret'])): '');
589 $applicationname = ((x($_POST, 'applicationname')) ? notags(trim($_POST['applicationname'])):'');
590 set_config('twitter','consumerkey',$consumerkey);
591 set_config('twitter','consumersecret',$consumersecret);
592 //set_config('twitter','application_name',$applicationname);
593 info( t('Settings updated.'). EOL );
595 function twitter_plugin_admin(&$a, &$o){
596 $t = get_markup_template( "admin.tpl", "addon/twitter/" );
598 $o = replace_macros($t, array(
599 '$submit' => t('Save Settings'),
600 // name, label, value, help, [extra values]
601 '$consumerkey' => array('consumerkey', t('Consumer key'), get_config('twitter', 'consumerkey' ), ''),
602 '$consumersecret' => array('consumersecret', t('Consumer secret'), get_config('twitter', 'consumersecret' ), ''),
603 //'$applicationname' => array('applicationname', t('Name of the Twitter Application'), get_config('twitter','application_name'),t('Set this to the exact name you gave the app on twitter.com/apps to avoid mirroring postings from ~friendica back to ~friendica'))
607 function twitter_cron($a,$b) {
608 $last = get_config('twitter','last_poll');
610 $poll_interval = intval(get_config('twitter','poll_interval'));
612 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
615 $next = $last + ($poll_interval * 60);
617 logger('twitter: poll intervall not reached');
621 logger('twitter: cron_start');
623 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1' ORDER BY RAND()");
626 logger('twitter: fetching for user '.$rr['uid']);
627 twitter_fetchtimeline($a, $rr['uid']);
631 $abandon_days = intval(get_config('system','account_abandon_days'));
632 if ($abandon_days < 1)
635 $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400);
637 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
640 if ($abandon_days != 0) {
641 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
643 logger('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
648 logger('twitter: importing timeline from user '.$rr['uid']);
649 twitter_fetchhometimeline($a, $rr["uid"]);
653 // check for new contacts once a day
654 $last_contact_check = get_pconfig($rr['uid'],'pumpio','contact_check');
655 if($last_contact_check)
656 $next_contact_check = $last_contact_check + 86400;
658 $next_contact_check = 0;
660 if($next_contact_check <= time()) {
661 pumpio_getallusers($a, $rr["uid"]);
662 set_pconfig($rr['uid'],'pumpio','contact_check',time());
669 logger('twitter: cron_end');
671 set_config('twitter','last_poll', time());
674 function twitter_expire($a,$b) {
676 $days = get_config('twitter', 'expire');
681 $r = q("DELETE FROM `item` WHERE `deleted` AND `network` = '%s'", dbesc(NETWORK_TWITTER));
683 require_once("include/items.php");
685 logger('twitter_expire: expire_start');
687 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
690 logger('twitter_expire: user '.$rr['uid']);
691 item_expire($rr['uid'], $days, NETWORK_TWITTER, true);
695 logger('twitter_expire: expire_end');
698 function twitter_prepare_body(&$a,&$b) {
699 if ($b["item"]["network"] != NETWORK_TWITTER)
704 require_once("include/plaintext.php");
706 $item["plink"] = $a->get_baseurl()."/display/".$a->user["nickname"]."/".$item["parent"];
708 $r = q("SELECT `author-link` FROM item WHERE item.uri = '%s' AND item.uid = %d LIMIT 1",
709 dbesc($item["thr-parent"]),
710 intval(local_user()));
715 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
716 $nickname = "@[url=".$orig_post["author-link"]."]".$nicknameplain."[/url]";
717 $nicknameplain = "@".$nicknameplain;
719 if ((strpos($item["body"], $nickname) === false) AND (strpos($item["body"], $nicknameplain) === false))
720 $item["body"] = $nickname." ".$item["body"];
724 $msgarr = plaintext($a, $item, $max_char, true, 8);
725 $msg = $msgarr["text"];
727 if (isset($msgarr["url"]) AND ($msgarr["type"] != "photo"))
728 $msg .= " ".$msgarr["url"];
730 if (isset($msgarr["image"]))
731 $msg .= " ".$msgarr["image"];
733 $b['html'] = nl2br(htmlspecialchars($msg));
737 function twitter_do_mirrorpost($a, $uid, $post) {
738 $datarray["type"] = "wall";
739 $datarray["api_source"] = true;
740 $datarray["profile_uid"] = $uid;
741 $datarray["extid"] = NETWORK_TWITTER;
742 $datarray['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_TWITTER.":".$post->id);
743 $datarray["title"] = "";
745 if (is_object($post->retweeted_status)) {
746 $item = twitter_createpost($a, $uid, $post, array('id' => 0), false, false, true);
748 $datarray['body'] = "\n".share_header($item['author-name'], $item['author-link'], $item['author-avatar'], "",
749 $item['created'], $item['plink']);
751 $datarray['body'] .= $item['body'].'[/share]';
753 $item = twitter_createpost($a, $uid, $post, array('id' => 0), false, false, false);
755 $datarray['body'] = $item['body'];
758 $datarray["source"] = $item['app'];
759 $datarray["verb"] = $item['verb'];
761 if (isset($item["location"])) {
762 $datarray["location"] = $item["location"];
765 if (isset($item["coord"])) {
766 $datarray["coord"] = $item["coord"];
772 function twitter_fetchtimeline($a, $uid) {
773 $ckey = get_config('twitter', 'consumerkey');
774 $csecret = get_config('twitter', 'consumersecret');
775 $otoken = get_pconfig($uid, 'twitter', 'oauthtoken');
776 $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
777 $lastid = get_pconfig($uid, 'twitter', 'lastid');
779 $application_name = get_config('twitter', 'application_name');
781 if ($application_name == "")
782 $application_name = $a->get_hostname();
784 $has_picture = false;
786 require_once('mod/item.php');
787 require_once('include/items.php');
788 require_once('mod/share.php');
790 require_once('library/twitteroauth.php');
791 $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
793 //$parameters = array("exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true);
794 $parameters = array("exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended");
796 $first_time = ($lastid == "");
799 $parameters["since_id"] = $lastid;
801 $items = $connection->get('statuses/user_timeline', $parameters);
803 if (!is_array($items))
806 $posts = array_reverse($items);
809 foreach ($posts as $post) {
810 if ($post->id_str > $lastid) {
811 $lastid = $post->id_str;
812 set_pconfig($uid, 'twitter', 'lastid', $lastid);
818 if (!stristr($post->source, $application_name)) {
820 $_SESSION["authenticated"] = true;
821 $_SESSION["uid"] = $uid;
823 $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
827 $_REQUEST["type"] = "wall";
828 $_REQUEST["api_source"] = true;
829 $_REQUEST["profile_uid"] = $uid;
830 //$_REQUEST["source"] = "Twitter";
831 $_REQUEST["source"] = $post->source;
832 $_REQUEST["extid"] = NETWORK_TWITTER;
834 if (isset($post->id)) {
835 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_TWITTER.":".$post->id);
838 //$_REQUEST["date"] = $post->created_at;
840 $_REQUEST["title"] = "";
842 if (is_object($post->retweeted_status)) {
844 $_REQUEST['body'] = $post->retweeted_status->text;
849 if (is_array($post->retweeted_status->entities->media)) {
850 foreach($post->retweeted_status->entities->media AS $media) {
851 switch($media->type) {
853 //$_REQUEST['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $_REQUEST['body']);
854 //$has_picture = true;
855 $_REQUEST['body'] = str_replace($media->url, "", $_REQUEST['body']);
856 $picture = $media->media_url_https;
862 $converted = twitter_expand_entities($a, $_REQUEST['body'], $post->retweeted_status, true, $picture);
863 $_REQUEST['body'] = $converted["body"];
865 if (function_exists("share_header"))
866 $_REQUEST['body'] = share_header($post->retweeted_status->user->name, "https://twitter.com/".$post->retweeted_status->user->screen_name,
867 $post->retweeted_status->user->profile_image_url_https, "",
868 datetime_convert('UTC','UTC',$post->retweeted_status->created_at),
869 "https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str).
872 $_REQUEST['body'] = "[share author='".$post->retweeted_status->user->name.
873 "' profile='https://twitter.com/".$post->retweeted_status->user->screen_name.
874 "' avatar='".$post->retweeted_status->user->profile_image_url_https.
875 "' posted='".datetime_convert('UTC','UTC',$post->retweeted_status->created_at).
876 "' link='https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str."']".
879 $_REQUEST['body'] .= "[/share]";
881 $_REQUEST["body"] = $post->text;
885 if (is_array($post->entities->media)) {
886 foreach($post->entities->media AS $media) {
887 switch($media->type) {
889 //$_REQUEST['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $_REQUEST['body']);
890 //$has_picture = true;
891 $_REQUEST['body'] = str_replace($media->url, "", $_REQUEST['body']);
892 $picture = $media->media_url_https;
898 $converted = twitter_expand_entities($a, $_REQUEST["body"], $post, true, $picture);
899 $_REQUEST['body'] = $converted["body"];
902 if (is_string($post->place->name))
903 $_REQUEST["location"] = $post->place->name;
905 if (is_string($post->place->full_name))
906 $_REQUEST["location"] = $post->place->full_name;
908 if (is_array($post->geo->coordinates))
909 $_REQUEST["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
911 if (is_array($post->coordinates->coordinates))
912 $_REQUEST["coord"] = $post->coordinates->coordinates[1]." ".$post->coordinates->coordinates[0];
914 //print_r($_REQUEST);
915 logger('twitter: posting for user '.$uid);
917 // require_once('mod/item.php');
923 set_pconfig($uid, 'twitter', 'lastid', $lastid);
926 function twitter_queue_hook(&$a,&$b) {
928 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
929 dbesc(NETWORK_TWITTER)
934 require_once('include/queue_fn.php');
937 if($x['network'] !== NETWORK_TWITTER)
940 logger('twitter_queue: run');
942 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
943 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
951 $ckey = get_config('twitter', 'consumerkey');
952 $csecret = get_config('twitter', 'consumersecret');
953 $otoken = get_pconfig($user['uid'], 'twitter', 'oauthtoken');
954 $osecret = get_pconfig($user['uid'], 'twitter', 'oauthsecret');
958 if ($ckey AND $csecret AND $otoken AND $osecret) {
960 logger('twitter_queue: able to post');
962 $z = unserialize($x['content']);
964 require_once("addon/twitter/codebird.php");
966 $cb = \Codebird\Codebird::getInstance();
967 $cb->setConsumerKey($ckey, $csecret);
968 $cb->setToken($otoken, $osecret);
970 if ($z['url'] == "statuses/update")
971 $result = $cb->statuses_update($z['post']);
973 logger('twitter_queue: post result: ' . print_r($result, true), LOGGER_DEBUG);
976 logger('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"');
979 remove_queue_item($x['id']);
982 logger("twitter_queue: Error getting tokens for user ".$user['uid']);
985 logger('twitter_queue: delayed');
986 update_queue_time($x['id']);
991 function twitter_fix_avatar($avatar) {
992 require_once("include/Photo.php");
994 $new_avatar = str_replace("_normal.", ".", $avatar);
996 $info = get_photo_info($new_avatar);
998 $new_avatar = $avatar;
1003 function twitter_fetch_contact($uid, $contact, $create_user) {
1005 if ($contact->id_str == "")
1008 $avatar = twitter_fix_avatar($contact->profile_image_url_https);
1010 if (function_exists("update_gcontact"))
1011 update_gcontact(array("url" => "https://twitter.com/".$contact->screen_name,
1012 "network" => NETWORK_TWITTER, "photo" => $avatar, "hide" => true,
1013 "name" => $contact->name, "nick" => $contact->screen_name,
1014 "location" => $contact->location, "about" => $contact->description,
1015 "addr" => $contact->screen_name."@twitter.com", "generation" => 2));
1018 $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
1019 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
1022 q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
1023 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
1024 dbesc($contact->name),
1025 dbesc($contact->screen_name),
1028 q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
1029 dbesc($contact->name),
1030 dbesc($contact->screen_name),
1032 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
1034 if (DB_UPDATE_VERSION >= "1177")
1035 q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
1036 dbesc($contact->location),
1037 dbesc($contact->description),
1038 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
1041 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1042 intval($uid), dbesc("twitter::".$contact->id_str));
1044 if(!count($r) AND !$create_user)
1047 if (count($r) AND ($r[0]["readonly"] OR $r[0]["blocked"])) {
1048 logger("twitter_fetch_contact: Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG);
1053 // create contact record
1054 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
1055 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
1056 `writable`, `blocked`, `readonly`, `pending` )
1057 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0) ",
1059 dbesc(datetime_convert()),
1060 dbesc("https://twitter.com/".$contact->screen_name),
1061 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
1062 dbesc($contact->screen_name."@twitter.com"),
1063 dbesc("twitter::".$contact->id_str),
1065 dbesc("twitter::".$contact->id_str),
1066 dbesc($contact->name),
1067 dbesc($contact->screen_name),
1069 dbesc(NETWORK_TWITTER),
1070 intval(CONTACT_IS_FRIEND),
1075 $r = q("SELECT * FROM `contact` WHERE `alias` = '%s' AND `uid` = %d LIMIT 1",
1076 dbesc("twitter::".$contact->id_str),
1083 $contact_id = $r[0]['id'];
1085 $g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1",
1089 if($g && intval($g[0]['def_gid'])) {
1090 require_once('include/group.php');
1091 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
1094 require_once("Photo.php");
1096 $photos = import_profile_photo($avatar,$uid,$contact_id);
1098 q("UPDATE `contact` SET `photo` = '%s',
1103 `avatar-date` = '%s'
1108 dbesc(datetime_convert()),
1109 dbesc(datetime_convert()),
1110 dbesc(datetime_convert()),
1114 if (DB_UPDATE_VERSION >= "1177")
1115 q("UPDATE `contact` SET `location` = '%s',
1118 dbesc($contact->location),
1119 dbesc($contact->description),
1124 // update profile photos once every two weeks as we have no notification of when they change.
1126 //$update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -2 days')) ? true : false);
1127 $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
1129 // check that we have all the photos, this has been known to fail on occasion
1131 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
1133 logger("twitter_fetch_contact: Updating contact ".$contact->screen_name, LOGGER_DEBUG);
1135 require_once("Photo.php");
1137 $photos = import_profile_photo($avatar, $uid, $r[0]['id']);
1139 q("UPDATE `contact` SET `photo` = '%s',
1144 `avatar-date` = '%s',
1154 dbesc(datetime_convert()),
1155 dbesc(datetime_convert()),
1156 dbesc(datetime_convert()),
1157 dbesc("https://twitter.com/".$contact->screen_name),
1158 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
1159 dbesc($contact->screen_name."@twitter.com"),
1160 dbesc($contact->name),
1161 dbesc($contact->screen_name),
1165 if (DB_UPDATE_VERSION >= "1177")
1166 q("UPDATE `contact` SET `location` = '%s',
1169 dbesc($contact->location),
1170 dbesc($contact->description),
1176 return($r[0]["id"]);
1179 function twitter_fetchuser($a, $uid, $screen_name = "", $user_id = "") {
1180 $ckey = get_config('twitter', 'consumerkey');
1181 $csecret = get_config('twitter', 'consumersecret');
1182 $otoken = get_pconfig($uid, 'twitter', 'oauthtoken');
1183 $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1185 require_once("addon/twitter/codebird.php");
1187 $cb = \Codebird\Codebird::getInstance();
1188 $cb->setConsumerKey($ckey, $csecret);
1189 $cb->setToken($otoken, $osecret);
1191 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1199 $parameters = array();
1201 if ($screen_name != "")
1202 $parameters["screen_name"] = $screen_name;
1205 $parameters["user_id"] = $user_id;
1207 // Fetching user data
1208 $user = $cb->users_show($parameters);
1210 if (!is_object($user))
1213 $contact_id = twitter_fetch_contact($uid, $user, true);
1218 function twitter_expand_entities($a, $body, $item, $no_tags = false, $picture) {
1219 require_once("include/oembed.php");
1220 require_once("include/network.php");
1226 if (isset($item->entities->urls)) {
1232 foreach ($item->entities->urls AS $url) {
1234 $plain = str_replace($url->url, '', $plain);
1236 if ($url->url AND $url->expanded_url AND $url->display_url) {
1238 $expanded_url = original_url($url->expanded_url);
1240 $oembed_data = oembed_fetch_url($expanded_url);
1242 // Quickfix: Workaround for URL with "[" and "]" in it
1243 if (strpos($expanded_url, "[") OR strpos($expanded_url, "]"))
1244 $expanded_url = $url->url;
1247 $type = $oembed_data->type;
1249 if ($oembed_data->type == "video") {
1250 //$body = str_replace($url->url,
1251 // "[video]".$expanded_url."[/video]", $body);
1252 //$dontincludemedia = true;
1253 $type = $oembed_data->type;
1254 $footerurl = $expanded_url;
1255 $footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
1257 $body = str_replace($url->url, $footerlink, $body);
1258 //} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) {
1259 } elseif (($oembed_data->type == "photo") AND isset($oembed_data->url)) {
1260 $body = str_replace($url->url,
1261 "[url=".$expanded_url."][img]".$oembed_data->url."[/img][/url]",
1263 //$dontincludemedia = true;
1264 } elseif ($oembed_data->type != "link")
1265 $body = str_replace($url->url,
1266 "[url=".$expanded_url."]".$expanded_url."[/url]",
1269 $img_str = fetch_url($expanded_url, true, $redirects, 4);
1271 $tempfile = tempnam(get_temppath(), "cache");
1272 file_put_contents($tempfile, $img_str);
1273 $mime = image_type_to_mime_type(exif_imagetype($tempfile));
1276 if (substr($mime, 0, 6) == "image/") {
1278 $body = str_replace($url->url, "[img]".$expanded_url."[/img]", $body);
1279 //$dontincludemedia = true;
1281 $type = $oembed_data->type;
1282 $footerurl = $expanded_url;
1283 $footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
1285 $body = str_replace($url->url, $footerlink, $body);
1291 if ($footerurl != "")
1292 $footer = add_page_info($footerurl, false, $picture);
1294 if (($footerlink != "") AND (trim($footer) != "")) {
1295 $removedlink = trim(str_replace($footerlink, "", $body));
1297 if (($removedlink == "") OR strstr($body, $removedlink))
1298 $body = $removedlink;
1303 if (($footer == "") AND ($picture != ""))
1304 $body .= "\n\n[img]".$picture."[/img]\n";
1305 elseif (($footer == "") AND ($picture == ""))
1306 $body = add_page_info_to_body($body);
1309 return array("body" => $body, "tags" => "", "plain" => $plain);
1311 $tags_arr = array();
1313 foreach ($item->entities->hashtags AS $hashtag) {
1314 $url = "#[url=".$a->get_baseurl()."/search?tag=".rawurlencode($hashtag->text)."]".$hashtag->text."[/url]";
1315 $tags_arr["#".$hashtag->text] = $url;
1316 $body = str_replace("#".$hashtag->text, $url, $body);
1319 foreach ($item->entities->user_mentions AS $mention) {
1320 $url = "@[url=https://twitter.com/".rawurlencode($mention->screen_name)."]".$mention->screen_name."[/url]";
1321 $tags_arr["@".$mention->screen_name] = $url;
1322 $body = str_replace("@".$mention->screen_name, $url, $body);
1325 // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
1326 $tags = get_tags($body);
1329 foreach($tags as $tag) {
1330 if (strstr(trim($tag), " "))
1333 if(strpos($tag,'#') === 0) {
1334 if(strpos($tag,'[url='))
1337 // don't link tags that are already embedded in links
1339 if(preg_match('/\[(.*?)' . preg_quote($tag,'/') . '(.*?)\]/',$body))
1341 if(preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag,'/') . '(.*?)\)/',$body))
1344 $basetag = str_replace('_',' ',substr($tag,1));
1345 $url = '#[url='.$a->get_baseurl().'/search?tag='.rawurlencode($basetag).']'.$basetag.'[/url]';
1346 $body = str_replace($tag,$url,$body);
1347 $tags_arr["#".$basetag] = $url;
1349 } elseif(strpos($tag,'@') === 0) {
1350 if(strpos($tag,'[url='))
1353 $basetag = substr($tag,1);
1354 $url = '@[url=https://twitter.com/'.rawurlencode($basetag).']'.$basetag.'[/url]';
1355 $body = str_replace($tag,$url,$body);
1356 $tags_arr["@".$basetag] = $url;
1362 $tags = implode($tags_arr, ",");
1365 return array("body" => $body, "tags" => $tags, "plain" => $plain);
1369 * @brief Fetch media entities and add media links to the body
1371 * @param object $post Twitter object with the post
1372 * @param array $postarray Array of the item that is about to be posted
1374 * @return $picture string Returns a a single picture string if it isn't a media post
1376 function twitter_media_entities($post, &$postarray) {
1378 // There are no media entities? So we quit.
1379 if (!is_array($post->extended_entities->media)) {
1383 // When the post links to an external page, we only take one picture.
1384 // This could be improved in the future.
1385 if (count($post->entities->urls) > 0) {
1387 foreach($post->extended_entities->media AS $medium) {
1388 if (isset($medium->media_url_https)) {
1389 $picture = $medium->media_url_https;
1390 $postarray['body'] = str_replace($medium->url, "", $postarray['body']);
1396 // This is a pure media post, first search for all media urls
1398 foreach($post->extended_entities->media AS $medium) {
1399 switch($medium->type) {
1401 $media[$medium->url] .= "\n[img]".$medium->media_url_https."[/img]";
1402 $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1405 case 'animated_gif':
1406 $media[$medium->url] .= "\n[img]".$medium->media_url_https."[/img]";
1407 $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
1408 if (is_array($medium->video_info->variants)) {
1410 // We take the video with the highest bitrate
1411 foreach ($medium->video_info->variants AS $variant) {
1412 if (($variant->content_type == "video/mp4") AND ($variant->bitrate >= $bitrate)) {
1413 $media[$medium->url] = "\n[video]".$variant->url."[/video]";
1414 $bitrate = $variant->bitrate;
1419 // The following code will only be activated for test reasons
1421 // $postarray['body'] .= print_r($medium, true);
1425 // Now we replace the media urls.
1426 foreach ($media AS $key => $value) {
1427 $postarray['body'] = str_replace($key, "\n".$value."\n", $postarray['body']);
1432 function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing_contact, $noquote) {
1434 $postarray = array();
1435 $postarray['network'] = NETWORK_TWITTER;
1436 $postarray['gravity'] = 0;
1437 $postarray['uid'] = $uid;
1438 $postarray['wall'] = 0;
1439 $postarray['uri'] = "twitter::".$post->id_str;
1440 $postarray['object'] = json_encode($post);
1442 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1443 dbesc($postarray['uri']),
1453 if ($post->in_reply_to_status_id_str != "") {
1455 $parent = "twitter::".$post->in_reply_to_status_id_str;
1457 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1462 $postarray['thr-parent'] = $r[0]["uri"];
1463 $postarray['parent-uri'] = $r[0]["parent-uri"];
1464 $postarray['parent'] = $r[0]["parent"];
1465 $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1467 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1472 $postarray['thr-parent'] = $r[0]['uri'];
1473 $postarray['parent-uri'] = $r[0]['parent-uri'];
1474 $postarray['parent'] = $r[0]['parent'];
1475 $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1477 $postarray['thr-parent'] = $postarray['uri'];
1478 $postarray['parent-uri'] = $postarray['uri'];
1479 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1484 $own_id = get_pconfig($uid, 'twitter', 'own_id');
1486 if ($post->user->id_str == $own_id) {
1487 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1491 $contactid = $r[0]["id"];
1493 $postarray['owner-name'] = $r[0]["name"];
1494 $postarray['owner-link'] = $r[0]["url"];
1495 $postarray['owner-avatar'] = $r[0]["photo"];
1500 // Don't create accounts of people who just comment something
1501 $create_user = false;
1503 $postarray['parent-uri'] = $postarray['uri'];
1504 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1507 if ($contactid == 0) {
1508 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1510 $postarray['owner-name'] = $post->user->name;
1511 $postarray['owner-link'] = "https://twitter.com/".$post->user->screen_name;
1512 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1515 if(($contactid == 0) AND !$only_existing_contact) {
1516 $contactid = $self['id'];
1517 } elseif ($contactid <= 0) {
1521 $postarray['contact-id'] = $contactid;
1523 $postarray['verb'] = ACTIVITY_POST;
1524 $postarray['author-name'] = $postarray['owner-name'];
1525 $postarray['author-link'] = $postarray['owner-link'];
1526 $postarray['author-avatar'] = $postarray['owner-avatar'];
1527 $postarray['plink'] = "https://twitter.com/".$post->user->screen_name."/status/".$post->id_str;
1528 $postarray['app'] = strip_tags($post->source);
1530 if ($post->user->protected) {
1531 $postarray['private'] = 1;
1532 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1535 if (is_string($post->full_text)) {
1536 $postarray['body'] = $post->full_text;
1538 $postarray['body'] = $post->text;
1541 // When the post contains links then use the correct object type
1542 if (count($post->entities->urls) > 0) {
1543 $postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
1546 // Search for media links
1547 $picture = twitter_media_entities($post, $postarray);
1549 $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $picture);
1550 $postarray['body'] = $converted["body"];
1551 $postarray['tag'] = $converted["tags"];
1552 $postarray['created'] = datetime_convert('UTC','UTC',$post->created_at);
1553 $postarray['edited'] = datetime_convert('UTC','UTC',$post->created_at);
1555 $statustext = $converted["plain"];
1557 if (is_string($post->place->name)) {
1558 $postarray["location"] = $post->place->name;
1560 if (is_string($post->place->full_name)) {
1561 $postarray["location"] = $post->place->full_name;
1563 if (is_array($post->geo->coordinates)) {
1564 $postarray["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
1566 if (is_array($post->coordinates->coordinates)) {
1567 $postarray["coord"] = $post->coordinates->coordinates[1]." ".$post->coordinates->coordinates[0];
1569 if (is_object($post->retweeted_status)) {
1570 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1572 $retweet['object'] = $postarray['object'];
1573 $retweet['contact-id'] = $postarray['contact-id'];
1574 $retweet['owner-name'] = $postarray['owner-name'];
1575 $retweet['owner-link'] = $postarray['owner-link'];
1576 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1578 $postarray = $retweet;
1581 if (is_object($post->quoted_status) AND !$noquote) {
1582 $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
1584 $postarray['body'] = $statustext;
1586 $postarray['body'] .= "\n".share_header($quoted['author-name'], $quoted['author-link'], $quoted['author-avatar'], "",
1587 $quoted['created'], $quoted['plink']);
1589 $postarray['body'] .= $quoted['body'].'[/share]';
1595 function twitter_checknotification($a, $uid, $own_id, $top_item, $postarray) {
1597 // this whole function doesn't seem to work. Needs complete check
1599 $user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
1607 if (link_compare($user[0]["url"], $postarray['author-link']))
1610 $own_user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1612 dbesc("twitter::".$own_id)
1615 if(!count($own_user))
1618 // Is it me from twitter?
1619 if (link_compare($own_user[0]["url"], $postarray['author-link']))
1622 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
1623 dbesc($postarray['parent-uri']),
1627 if(count($myconv)) {
1629 foreach($myconv as $conv) {
1630 // now if we find a match, it means we're in this conversation
1632 if(!link_compare($conv['author-link'],$user[0]["url"]) AND !link_compare($conv['author-link'],$own_user[0]["url"]))
1635 require_once('include/enotify.php');
1637 $conv_parent = $conv['parent'];
1640 'type' => NOTIFY_COMMENT,
1641 'notify_flags' => $user[0]['notify-flags'],
1642 'language' => $user[0]['language'],
1643 'to_name' => $user[0]['username'],
1644 'to_email' => $user[0]['email'],
1645 'uid' => $user[0]['uid'],
1646 'item' => $postarray,
1647 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($top_item)),
1648 'source_name' => $postarray['author-name'],
1649 'source_link' => $postarray['author-link'],
1650 'source_photo' => $postarray['author-avatar'],
1651 'verb' => ACTIVITY_POST,
1653 'parent' => $conv_parent,
1656 // only send one notification
1662 function twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id) {
1663 logger("twitter_fetchparentposts: Fetching for user ".$uid." and post ".$post->id_str, LOGGER_DEBUG);
1667 while ($post->in_reply_to_status_id_str != "") {
1668 $parameters = array("trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str);
1670 $post = $connection->get('statuses/show', $parameters);
1672 if (!count($post)) {
1673 logger("twitter_fetchparentposts: Can't fetch post ".$parameters->id, LOGGER_DEBUG);
1677 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1678 dbesc("twitter::".$post->id_str),
1688 logger("twitter_fetchparentposts: Fetching ".count($posts)." parents", LOGGER_DEBUG);
1690 $posts = array_reverse($posts);
1692 if (count($posts)) {
1693 foreach ($posts as $post) {
1694 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1696 if (trim($postarray['body']) == "")
1699 $item = item_store($postarray);
1700 $postarray["id"] = $item;
1702 logger('twitter_fetchparentpost: User '.$self["nick"].' posted parent timeline item '.$item);
1704 if ($item AND !function_exists("check_item_notification"))
1705 twitter_checknotification($a, $uid, $own_id, $item, $postarray);
1710 function twitter_fetchhometimeline($a, $uid) {
1711 $ckey = get_config('twitter', 'consumerkey');
1712 $csecret = get_config('twitter', 'consumersecret');
1713 $otoken = get_pconfig($uid, 'twitter', 'oauthtoken');
1714 $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1715 $create_user = get_pconfig($uid, 'twitter', 'create_user');
1716 $mirror_posts = get_pconfig($uid, 'twitter', 'mirror_posts');
1718 logger("twitter_fetchhometimeline: Fetching for user ".$uid, LOGGER_DEBUG);
1720 $application_name = get_config('twitter', 'application_name');
1722 if ($application_name == "")
1723 $application_name = $a->get_hostname();
1725 require_once('library/twitteroauth.php');
1726 require_once('include/items.php');
1728 $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
1730 $own_contact = twitter_fetch_own_contact($a, $uid);
1732 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1733 intval($own_contact),
1737 $own_id = $r[0]["nick"];
1739 logger("twitter_fetchhometimeline: Own twitter contact not found for user ".$uid, LOGGER_DEBUG);
1743 $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1749 logger("twitter_fetchhometimeline: Own contact not found for user ".$uid, LOGGER_DEBUG);
1753 $u = q("SELECT * FROM user WHERE uid = %d LIMIT 1",
1756 logger("twitter_fetchhometimeline: Own user not found for user ".$uid, LOGGER_DEBUG);
1760 $parameters = array("exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended");
1761 //$parameters["count"] = 200;
1764 // Fetching timeline
1765 $lastid = get_pconfig($uid, 'twitter', 'lasthometimelineid');
1767 $first_time = ($lastid == "");
1770 $parameters["since_id"] = $lastid;
1772 $items = $connection->get('statuses/home_timeline', $parameters);
1774 if (!is_array($items)) {
1775 logger("twitter_fetchhometimeline: Error fetching home timeline: ".print_r($items, true), LOGGER_DEBUG);
1779 $posts = array_reverse($items);
1781 logger("twitter_fetchhometimeline: Fetching timeline for user ".$uid." ".sizeof($posts)." items", LOGGER_DEBUG);
1783 if (count($posts)) {
1784 foreach ($posts as $post) {
1785 if ($post->id_str > $lastid) {
1786 $lastid = $post->id_str;
1787 set_pconfig($uid, 'twitter', 'lasthometimelineid', $lastid);
1793 if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1794 logger("twitter_fetchhometimeline: Skip previously sended post", LOGGER_DEBUG);
1798 if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1799 logger("twitter_fetchhometimeline: Skip post that will be mirrored", LOGGER_DEBUG);
1803 if ($post->in_reply_to_status_id_str != "")
1804 twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id);
1806 $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1808 if (trim($postarray['body']) == "")
1811 $item = item_store($postarray);
1812 $postarray["id"] = $item;
1814 logger('twitter_fetchhometimeline: User '.$self["nick"].' posted home timeline item '.$item);
1816 if ($item AND !function_exists("check_item_notification"))
1817 twitter_checknotification($a, $uid, $own_id, $item, $postarray);
1821 set_pconfig($uid, 'twitter', 'lasthometimelineid', $lastid);
1823 // Fetching mentions
1824 $lastid = get_pconfig($uid, 'twitter', 'lastmentionid');
1826 $first_time = ($lastid == "");
1829 $parameters["since_id"] = $lastid;
1831 $items = $connection->get('statuses/mentions_timeline', $parameters);
1833 if (!is_array($items)) {
1834 logger("twitter_fetchhometimeline: Error fetching mentions: ".print_r($items, true), LOGGER_DEBUG);
1838 $posts = array_reverse($items);
1840 logger("twitter_fetchhometimeline: Fetching mentions for user ".$uid." ".sizeof($posts)." items", LOGGER_DEBUG);
1842 if (count($posts)) {
1843 foreach ($posts as $post) {
1844 if ($post->id_str > $lastid)
1845 $lastid = $post->id_str;
1850 if ($post->in_reply_to_status_id_str != "")
1851 twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id);
1853 $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1855 if (trim($postarray['body']) == "")
1858 $item = item_store($postarray);
1859 $postarray["id"] = $item;
1861 if ($item AND function_exists("check_item_notification"))
1862 check_item_notification($item, $uid, NOTIFY_TAGSELF);
1864 if (!isset($postarray["parent"]) OR ($postarray["parent"] == 0))
1865 $postarray["parent"] = $item;
1867 logger('twitter_fetchhometimeline: User '.$self["nick"].' posted mention timeline item '.$item);
1870 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1871 dbesc($postarray['uri']),
1875 $item = $r[0]['id'];
1876 $parent_id = $r[0]['parent'];
1879 $parent_id = $postarray['parent'];
1881 if (($item != 0) AND !function_exists("check_item_notification")) {
1882 require_once('include/enotify.php');
1884 'type' => NOTIFY_TAGSELF,
1885 'notify_flags' => $u[0]['notify-flags'],
1886 'language' => $u[0]['language'],
1887 'to_name' => $u[0]['username'],
1888 'to_email' => $u[0]['email'],
1889 'uid' => $u[0]['uid'],
1890 'item' => $postarray,
1891 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item)),
1892 'source_name' => $postarray['author-name'],
1893 'source_link' => $postarray['author-link'],
1894 'source_photo' => $postarray['author-avatar'],
1895 'verb' => ACTIVITY_TAG,
1897 'parent' => $parent_id
1903 set_pconfig($uid, 'twitter', 'lastmentionid', $lastid);
1906 function twitter_fetch_own_contact($a, $uid) {
1907 $ckey = get_config('twitter', 'consumerkey');
1908 $csecret = get_config('twitter', 'consumersecret');
1909 $otoken = get_pconfig($uid, 'twitter', 'oauthtoken');
1910 $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1912 $own_id = get_pconfig($uid, 'twitter', 'own_id');
1916 if ($own_id == "") {
1917 require_once('library/twitteroauth.php');
1919 $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
1921 // Fetching user data
1922 $user = $connection->get('account/verify_credentials');
1924 set_pconfig($uid, 'twitter', 'own_id', $user->id_str);
1926 $contact_id = twitter_fetch_contact($uid, $user, true);
1929 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1930 intval($uid), dbesc("twitter::".$own_id));
1932 $contact_id = $r[0]["id"];
1934 del_pconfig($uid, 'twitter', 'own_id');
1938 return($contact_id);
1941 function twitter_is_retweet($a, $uid, $body) {
1942 $body = trim($body);
1944 // Skip if it isn't a pure repeated messages
1945 // Does it start with a share?
1946 if (strpos($body, "[share") > 0)
1949 // Does it end with a share?
1950 if (strlen($body) > (strrpos($body, "[/share]") + 8))
1953 $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
1954 // Skip if there is no shared message in there
1955 if ($body == $attributes)
1959 preg_match("/link='(.*?)'/ism", $attributes, $matches);
1960 if ($matches[1] != "")
1961 $link = $matches[1];
1963 preg_match('/link="(.*?)"/ism', $attributes, $matches);
1964 if ($matches[1] != "")
1965 $link = $matches[1];
1967 $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
1971 logger('twitter_is_retweet: Retweeting id '.$id.' for user '.$uid, LOGGER_DEBUG);
1973 $ckey = get_config('twitter', 'consumerkey');
1974 $csecret = get_config('twitter', 'consumersecret');
1975 $otoken = get_pconfig($uid, 'twitter', 'oauthtoken');
1976 $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1978 require_once('library/twitteroauth.php');
1979 $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
1981 $result = $connection->post('statuses/retweet/'.$id);
1983 logger('twitter_is_retweet: result '.print_r($result, true), LOGGER_DEBUG);
1985 return(!isset($result->errors));