Logger Levels
[friendica-addons.git/.git] / twitter / twitter.php
1 <?php
2 /**
3  * Name: Twitter Connector
4  * Description: Bidirectional (posting, relaying and reading) connector for Twitter.
5  * Version: 1.1.0
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>
9  *
10  * Copyright (c) 2011-2013 Tobias Diekershoff, Michael Vogel, Hypolite Petovan
11  * All rights reserved.
12  *
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.
23  *
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.
34  *
35  */
36 /*   Twitter Addon for Friendica
37  *
38  *   Author: Tobias Diekershoff
39  *           tobias.diekershoff@gmx.net
40  *
41  *   License:3-clause BSD license
42  *
43  *   Configuration:
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
46  *
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.
50  *
51  *     Add this key pair to your global config/addon.ini.php or use the admin panel.
52  *
53  *     [twitter]
54  *     consumerkey = your consumer_key here
55  *     consumersecret = your consumer_secret here
56  *
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".
60  *
61  *     Requirements: PHP5, curl
62  */
63
64 use Abraham\TwitterOAuth\TwitterOAuth;
65 use Abraham\TwitterOAuth\TwitterOAuthException;
66 use Friendica\App;
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;
88
89 require_once 'boot.php';
90 require_once 'include/dba.php';
91 require_once 'include/enotify.php';
92 require_once 'include/text.php';
93
94 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
95
96 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
97
98 function twitter_install()
99 {
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");
114 }
115
116 function twitter_uninstall()
117 {
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');
130
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');
135 }
136
137 function twitter_load_config(App $a)
138 {
139         $a->loadConfigFile(__DIR__ . '/config/twitter.ini.php');
140 }
141
142 function twitter_check_item_notification(App $a, array &$notification_data)
143 {
144         $own_id = PConfig::get($notification_data["uid"], 'twitter', 'own_id');
145
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)
149         );
150
151         if ($own_user) {
152                 $notification_data["profiles"][] = $own_user[0]["url"];
153         }
154 }
155
156 function twitter_follow(App $a, array &$contact)
157 {
158         Logger::log("twitter_follow: Check if contact is twitter contact. " . $contact["url"], Logger::DEBUG);
159
160         if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
161                 return;
162         }
163
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);
167
168         $uid = $a->user["uid"];
169
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');
174
175         // If the addon is not configured (general or for this user) quit here
176         if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
177                 $contact = false;
178                 return;
179         }
180
181         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
182         $connection->post('friendships/create', ['screen_name' => $nickname]);
183
184         twitter_fetchuser($a, $uid, $nickname);
185
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'",
188                                 intval($uid),
189                                 DBA::escape($nickname));
190         if (DBA::isResult($r)) {
191                 $contact["contact"] = $r[0];
192         }
193 }
194
195 function twitter_jot_nets(App $a, &$b)
196 {
197         if (!local_user()) {
198                 return;
199         }
200
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>';
207         }
208 }
209
210 function twitter_settings_post(App $a)
211 {
212         if (!local_user()) {
213                 return;
214         }
215         // don't check twitter settings if twitter submit button is not clicked
216         if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
217                 return;
218         }
219
220         if (!empty($_POST['twitter-disconnect'])) {
221                 /*               * *
222                  * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
223                  * from the user configuration
224                  */
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');
236         } else {
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
245                         try {
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.'));
248                                 }
249
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());
260                         }
261                         //  reload the Addon Settings page, if we don't do it see Bug #42
262                         $a->internalRedirect('settings/connectors');
263                 } else {
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']));
271
272                         if (!intval($_POST['twitter-mirror'])) {
273                                 PConfig::delete(local_user(), 'twitter', 'lastid');
274                         }
275
276                         info(L10n::t('Twitter settings updated.') . EOL);
277                 }
278         }
279 }
280
281 function twitter_settings(App $a, &$s)
282 {
283         if (!local_user()) {
284                 return;
285         }
286         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
287         /*       * *
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)
291          */
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');
296
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'));
302
303         $css = (($enabled) ? '' : '-disabled');
304
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>';
307         $s .= '</span>';
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>';
311         $s .= '</span>';
312
313         if ((!$ckey) && (!$csecret)) {
314                 /* no global consumer keys
315                  * display warning and skip personal config
316                  */
317                 $s .= '<p>' . L10n::t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
318         } else {
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.
325                          */
326                         $connection = new TwitterOAuth($ckey, $csecret);
327                         try {
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>';
340                         }
341                 } else {
342                         /*                       * *
343                          *  we have an OAuth key / secret pair for the user
344                          *  so let's give a chance to disable the postings to Twitter
345                          */
346                         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
347                         try {
348                                 $details = $connection->get('account/verify_credentials');
349
350                                 $field_checkbox = get_markup_template('field_checkbox.tpl');
351
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>
355                                         </p>
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>
359                                         </p>
360                                 </div>';
361                                 $s .= '<div class="clear"></div>';
362
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.')]
365                                 ]);
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>';
368                                 }
369                                 $s .= replace_macros($field_checkbox, [
370                                         '$field' => ['twitter-default', L10n::t('Send public postings to Twitter by default'), $defenabled, '']
371                                 ]);
372                                 $s .= replace_macros($field_checkbox, [
373                                         '$field' => ['twitter-mirror', L10n::t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
374                                 ]);
375                                 $s .= replace_macros($field_checkbox, [
376                                         '$field' => ['twitter-import', L10n::t('Import the remote timeline'), $importenabled, '']
377                                 ]);
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.')]
380                                 ]);
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>';
385                         }
386                 }
387         }
388         $s .= '</div><div class="clear"></div>';
389 }
390
391 function twitter_post_local(App $a, array &$b)
392 {
393         if ($b['edit']) {
394                 return;
395         }
396
397         if (!local_user() || (local_user() != $b['uid'])) {
398                 return;
399         }
400
401         $twitter_post = intval(PConfig::get(local_user(), 'twitter', 'post'));
402         $twitter_enable = (($twitter_post && x($_REQUEST, 'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
403
404         // if API is used, default to the chosen settings
405         if ($b['api_source'] && intval(PConfig::get(local_user(), 'twitter', 'post_by_default'))) {
406                 $twitter_enable = 1;
407         }
408
409         if (!$twitter_enable) {
410                 return;
411         }
412
413         if (strlen($b['postopts'])) {
414                 $b['postopts'] .= ',';
415         }
416
417         $b['postopts'] .= 'twitter';
418 }
419
420 function twitter_action(App $a, $uid, $pid, $action)
421 {
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');
426
427         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
428
429         $post = ['id' => $pid];
430
431         Logger::log("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), Logger::DATA);
432
433         switch ($action) {
434                 case "delete":
435                         // To-Do: $result = $connection->post('statuses/destroy', $post);
436                         $result = [];
437                         break;
438                 case "like":
439                         $result = $connection->post('favorites/create', $post);
440                         break;
441                 case "unlike":
442                         $result = $connection->post('favorites/destroy', $post);
443                         break;
444                 default:
445                         Logger::log('Unhandled action ' . $action, Logger::DEBUG);
446                         $result = [];
447         }
448         Logger::log("twitter_action '" . $action . "' send, result: " . print_r($result, true), Logger::DEBUG);
449 }
450
451 function twitter_post_hook(App $a, array &$b)
452 {
453         // Post to Twitter
454         if (!PConfig::get($b["uid"], 'twitter', 'import')
455                 && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
456                 return;
457         }
458
459         if ($b['parent'] != $b['id']) {
460                 Logger::log("twitter_post_hook: parameter " . print_r($b, true), Logger::DATA);
461
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::"))
466                 {
467                         Logger::log("twitter_post_hook: no twitter post " . $b["parent"]);
468                         return;
469                 }
470
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"]);
475                         return;
476                 } else {
477                         $iscomment = true;
478                 }
479
480
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;
484
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"];
488                 }
489
490                 Logger::log("twitter_post_hook: parent found " . print_r($orig_post, true), Logger::DATA);
491         } else {
492                 $iscomment = false;
493
494                 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
495                         return;
496                 }
497
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']) {
502                         return;
503                 }
504         }
505
506         if (($b['verb'] == ACTIVITY_POST) && $b['deleted']) {
507                 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
508         }
509
510         if ($b['verb'] == ACTIVITY_LIKE) {
511                 Logger::log("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), Logger::DEBUG);
512                 if ($b['deleted']) {
513                         twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
514                 } else {
515                         twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
516                 }
517
518                 return;
519         }
520
521         if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
522                 return;
523         }
524
525         // if post comes from twitter don't send it back
526         if ($b['extid'] == Protocol::TWITTER) {
527                 return;
528         }
529
530         if ($b['app'] == "Twitter") {
531                 return;
532         }
533
534         Logger::log('twitter post invoked');
535
536         PConfig::load($b['uid'], 'twitter');
537
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');
542
543         if ($ckey && $csecret && $otoken && $osecret) {
544                 Logger::log('twitter: we have customer key and oauth stuff, going to send.', Logger::DEBUG);
545
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'])) {
548                         return;
549                 }
550
551                 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
552
553                 // Set the timeout for upload to 30 seconds
554                 $connection->setTimeouts(10, 30);
555
556                 $max_char = 280;
557
558                 // Handling non-native reshares
559                 $b['body'] = Friendica\Content\Text\BBCode::convertShare(
560                         $b['body'],
561                         function (array $attributes, array $author_contact, $content, $is_quote_share) {
562                                 return twitter_convert_share($attributes, $author_contact, $content, $is_quote_share);
563                         }
564                 );
565
566                 $b['body'] = twitter_update_mentions($b['body']);
567
568                 $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
569                 $msg = $msgarr["text"];
570
571                 if (($msg == "") && isset($msgarr["title"])) {
572                         $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
573                 }
574
575                 $image = "";
576
577                 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
578                         $msg .= "\n" . $msgarr["url"];
579                         $url_added = true;
580                 } else {
581                         $url_added = false;
582                 }
583
584                 if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
585                         $image = $msgarr["image"];
586                 }
587
588                 if (empty($msg)) {
589                         return;
590                 }
591
592                 // and now tweet it :-)
593                 $post = [];
594
595                 if (!empty($image)) {
596                         try {
597                                 $img_str = Network::fetchUrl($image);
598
599                                 $tempfile = tempnam(get_temppath(), 'cache');
600                                 file_put_contents($tempfile, $img_str);
601
602                                 $media = $connection->upload('media/upload', ['media' => $tempfile]);
603
604                                 unlink($tempfile);
605
606                                 if (isset($media->media_id_string)) {
607                                         $post['media_ids'] = $media->media_id_string;
608                                 } else {
609                                         throw new Exception('Failed upload of ' . $image);
610                                 }
611                         } catch (Exception $e) {
612                                 Logger::log('Exception when trying to send to Twitter: ' . $e->getMessage());
613
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.
616                                 if (!$url_added) {
617                                         $msg .= "\n" . $image;
618                                 }
619
620                                 $image = "";
621                         }
622                 }
623
624                 $post['status'] = $msg;
625
626                 if ($iscomment) {
627                         $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
628                 }
629
630                 $url = 'statuses/update';
631                 $result = $connection->post($url, $post);
632                 Logger::log('twitter_post send, result: ' . print_r($result, true), Logger::DEBUG);
633
634                 if (!empty($result->source)) {
635                         Config::set("twitter", "application_name", strip_tags($result->source));
636                 }
637
638                 if (!empty($result->errors)) {
639                         Logger::log('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
640
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"];
644                         }
645
646                         $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
647
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']]);
653                 }
654         }
655 }
656
657 function twitter_addon_admin_post(App $a)
658 {
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);
664 }
665
666 function twitter_addon_admin(App $a, &$o)
667 {
668         $t = get_markup_template("admin.tpl", "addon/twitter/");
669
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'), ''],
675         ]);
676 }
677
678 function twitter_cron(App $a)
679 {
680         $last = Config::get('twitter', 'last_poll');
681
682         $poll_interval = intval(Config::get('twitter', 'poll_interval'));
683         if (!$poll_interval) {
684                 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
685         }
686
687         if ($last) {
688                 $next = $last + ($poll_interval * 60);
689                 if ($next > time()) {
690                         Logger::log('twitter: poll intervall not reached');
691                         return;
692                 }
693         }
694         Logger::log('twitter: cron_start');
695
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']);
701                 }
702         }
703
704         $abandon_days = intval(Config::get('system', 'account_abandon_days'));
705         if ($abandon_days < 1) {
706                 $abandon_days = 0;
707         }
708
709         $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
710
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');
718                                         continue;
719                                 }
720                         }
721
722                         Logger::log('twitter: importing timeline from user ' . $rr['uid']);
723                         Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
724                         /*
725                           // To-Do
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;
730                           else
731                           $next_contact_check = 0;
732
733                           if($next_contact_check <= time()) {
734                           pumpio_getallusers($a, $rr["uid"]);
735                           PConfig::set($rr['uid'],'pumpio','contact_check',time());
736                           }
737                          */
738                 }
739         }
740
741         Logger::log('twitter: cron_end');
742
743         Config::set('twitter', 'last_poll', time());
744 }
745
746 function twitter_expire(App $a)
747 {
748         $days = Config::get('twitter', 'expire');
749
750         if ($days == 0) {
751                 return;
752         }
753
754         $r = Item::select(['id'], ['deleted' => true, 'network' => Protocol::TWITTER]);
755         while ($row = DBA::fetch($r)) {
756                 DBA::delete('item', ['id' => $row['id']]);
757         }
758         DBA::close($r);
759
760         require_once "include/items.php";
761
762         Logger::log('twitter_expire: expire_start');
763
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);
769                 }
770         }
771
772         Logger::log('twitter_expire: expire_end');
773 }
774
775 function twitter_prepare_body(App $a, array &$b)
776 {
777         if ($b["item"]["network"] != Protocol::TWITTER) {
778                 return;
779         }
780
781         if ($b["preview"]) {
782                 $max_char = 280;
783                 $item = $b["item"];
784                 $item["plink"] = $a->getBaseURL() . "/display/" . $a->user["nickname"] . "/" . $item["parent"];
785
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;
792
793                         if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) {
794                                 $item["body"] = $nickname . " " . $item["body"];
795                         }
796                 }
797
798                 $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, 8);
799                 $msg = $msgarr["text"];
800
801                 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
802                         $msg .= " " . $msgarr["url"];
803                 }
804
805                 if (isset($msgarr["image"])) {
806                         $msg .= " " . $msgarr["image"];
807                 }
808
809                 $b['html'] = nl2br(htmlspecialchars($msg));
810         }
811 }
812
813 /**
814  * @brief Build the item array for the mirrored post
815  *
816  * @param App $a Application class
817  * @param integer $uid User id
818  * @param object $post Twitter object with the post
819  *
820  * @return array item data to be posted
821  */
822 function twitter_do_mirrorpost(App $a, $uid, $post)
823 {
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'] = '';
831
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);
835
836                 if (empty($item['body'])) {
837                         return [];
838                 }
839
840                 $datarray['body'] = "\n" . share_header(
841                         $item['author-name'],
842                         $item['author-link'],
843                         $item['author-avatar'],
844                         '',
845                         $item['created'],
846                         $item['plink']
847                 );
848
849                 $datarray['body'] .= $item['body'] . '[/share]';
850         } else {
851                 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
852
853                 if (empty($item['body'])) {
854                         return [];
855                 }
856
857                 $datarray['body'] = $item['body'];
858         }
859
860         $datarray['source'] = $item['app'];
861         $datarray['verb'] = $item['verb'];
862
863         if (isset($item['location'])) {
864                 $datarray['location'] = $item['location'];
865         }
866
867         if (isset($item['coord'])) {
868                 $datarray['coord'] = $item['coord'];
869         }
870
871         return $datarray;
872 }
873
874 function twitter_fetchtimeline(App $a, $uid)
875 {
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');
881
882         $application_name = Config::get('twitter', 'application_name');
883
884         if ($application_name == "") {
885                 $application_name = $a->getHostName();
886         }
887
888         $has_picture = false;
889
890         require_once 'mod/item.php';
891         require_once 'include/items.php';
892         require_once 'mod/share.php';
893
894         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
895
896         $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
897
898         $first_time = ($lastid == "");
899
900         if ($lastid != "") {
901                 $parameters["since_id"] = $lastid;
902         }
903
904         try {
905                 $items = $connection->get('statuses/user_timeline', $parameters);
906         } catch (TwitterOAuthException $e) {
907                 Logger::log('Error fetching timeline for user ' . $uid . ': ' . $e->getMessage());
908                 return;
909         }
910
911         if (!is_array($items)) {
912                 Logger::log('No items for user ' . $uid, Logger::INFO);
913                 return;
914         }
915
916         $posts = array_reverse($items);
917
918         Logger::log('Starting from ID ' . $lastid . ' for user ' . $uid, Logger::DEBUG);
919
920         if (count($posts)) {
921                 foreach ($posts as $post) {
922                         if ($post->id_str > $lastid) {
923                                 $lastid = $post->id_str;
924                                 PConfig::set($uid, 'twitter', 'lastid', $lastid);
925                         }
926
927                         if ($first_time) {
928                                 continue;
929                         }
930
931                         if (!stristr($post->source, $application_name)) {
932                                 $_SESSION["authenticated"] = true;
933                                 $_SESSION["uid"] = $uid;
934
935                                 Logger::log('Preparing Twitter ID ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
936
937                                 $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
938
939                                 if (empty($_REQUEST['body'])) {
940                                         continue;
941                                 }
942
943                                 Logger::log('Posting Twitter ID ' . $post->id_str . ' for user ' . $uid);
944
945                                 item_post($a);
946                         }
947                 }
948         }
949         PConfig::set($uid, 'twitter', 'lastid', $lastid);
950         Logger::log('Last ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
951 }
952
953 function twitter_queue_hook(App $a)
954 {
955         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
956                 DBA::escape(Protocol::TWITTER)
957         );
958         if (!DBA::isResult($qi)) {
959                 return;
960         }
961
962         foreach ($qi as $x) {
963                 if ($x['network'] !== Protocol::TWITTER) {
964                         continue;
965                 }
966
967                 Logger::log('twitter_queue: run');
968
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",
971                         intval($x['cid'])
972                 );
973                 if (!DBA::isResult($r)) {
974                         continue;
975                 }
976
977                 $user = $r[0];
978
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');
983
984                 $success = false;
985
986                 if ($ckey && $csecret && $otoken && $osecret) {
987                         Logger::log('twitter_queue: able to post');
988
989                         $z = unserialize($x['content']);
990
991                         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
992                         $result = $connection->post($z['url'], $z['post']);
993
994                         Logger::log('twitter_queue: post result: ' . print_r($result, true), Logger::DEBUG);
995
996                         if ($result->errors) {
997                                 Logger::log('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"');
998                         } else {
999                                 $success = true;
1000                                 Queue::removeItem($x['id']);
1001                         }
1002                 } else {
1003                         Logger::log("twitter_queue: Error getting tokens for user " . $user['uid']);
1004                 }
1005
1006                 if (!$success) {
1007                         Logger::log('twitter_queue: delayed');
1008                         Queue::updateTime($x['id']);
1009                 }
1010         }
1011 }
1012
1013 function twitter_fix_avatar($avatar)
1014 {
1015         $new_avatar = str_replace("_normal.", ".", $avatar);
1016
1017         $info = Image::getInfoFromURL($new_avatar);
1018         if (!$info) {
1019                 $new_avatar = $avatar;
1020         }
1021
1022         return $new_avatar;
1023 }
1024
1025 function twitter_fetch_contact($uid, $data, $create_user)
1026 {
1027         if (empty($data->id_str)) {
1028                 return -1;
1029         }
1030
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";
1034
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]);
1040
1041         $fields = ['url' => $url, 'network' => Protocol::TWITTER,
1042                 'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
1043                 'location' => $data->location, 'about' => $data->description];
1044
1045         $cid = Contact::getIdForURL($url, 0, true, $fields);
1046         if (!empty($cid)) {
1047                 DBA::update('contact', $fields, ['id' => $cid]);
1048                 Contact::updateAvatar($avatar, 0, $cid);
1049         }
1050
1051         $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
1052         if (!DBA::isResult($contact) && !$create_user) {
1053                 return 0;
1054         }
1055
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;
1069
1070                 if (!DBA::insert('contact', $fields)) {
1071                         return false;
1072                 }
1073
1074                 $contact_id = DBA::lastInsertId();
1075
1076                 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1077
1078                 Contact::updateAvatar($avatar, $uid, $contact_id);
1079         } else {
1080                 if ($contact["readonly"] || $contact["blocked"]) {
1081                         Logger::log("twitter_fetch_contact: Contact '" . $contact["nick"] . "' is blocked or readonly.", Logger::DEBUG);
1082                         return -1;
1083                 }
1084
1085                 $contact_id = $contact['id'];
1086
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'));
1089
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);
1093
1094                         Contact::updateAvatar($avatar, $uid, $contact['id']);
1095
1096                         $fields['name-date'] = DateTimeFormat::utcNow();
1097                         $fields['uri-date'] = DateTimeFormat::utcNow();
1098
1099                         DBA::update('contact', $fields, ['id' => $contact['id']]);
1100                 }
1101         }
1102
1103         return $contact_id;
1104 }
1105
1106 function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
1107 {
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');
1112
1113         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1114                 intval($uid));
1115
1116         if (DBA::isResult($r)) {
1117                 $self = $r[0];
1118         } else {
1119                 return;
1120         }
1121
1122         $parameters = [];
1123
1124         if ($screen_name != "") {
1125                 $parameters["screen_name"] = $screen_name;
1126         }
1127
1128         if ($user_id != "") {
1129                 $parameters["user_id"] = $user_id;
1130         }
1131
1132         // Fetching user data
1133         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1134         try {
1135                 $user = $connection->get('users/show', $parameters);
1136         } catch (TwitterOAuthException $e) {
1137                 Logger::log('twitter_fetchuser: Error fetching user ' . $uid . ': ' . $e->getMessage());
1138                 return;
1139         }
1140
1141         if (!is_object($user)) {
1142                 return;
1143         }
1144
1145         $contact_id = twitter_fetch_contact($uid, $user, true);
1146
1147         return $contact_id;
1148 }
1149
1150 function twitter_expand_entities(App $a, $body, $item, $picture)
1151 {
1152         $plain = $body;
1153
1154         $tags_arr = [];
1155
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);
1160         }
1161
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);
1166         }
1167
1168         if (isset($item->entities->urls)) {
1169                 $type = '';
1170                 $footerurl = '';
1171                 $footerlink = '';
1172                 $footer = '';
1173
1174                 foreach ($item->entities->urls as $url) {
1175                         $plain = str_replace($url->url, '', $plain);
1176
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);
1182                                         continue;
1183                                 }
1184
1185                                 $expanded_url = Network::finalUrl($url->expanded_url);
1186
1187                                 $oembed_data = OEmbed::fetchURL($expanded_url);
1188
1189                                 if (empty($oembed_data) || empty($oembed_data->type)) {
1190                                         continue;
1191                                 }
1192
1193                                 // Quickfix: Workaround for URL with '[' and ']' in it
1194                                 if (strpos($expanded_url, '[') || strpos($expanded_url, ']')) {
1195                                         $expanded_url = $url->url;
1196                                 }
1197
1198                                 if ($type == '') {
1199                                         $type = $oembed_data->type;
1200                                 }
1201
1202                                 if ($oembed_data->type == 'video') {
1203                                         $type = $oembed_data->type;
1204                                         $footerurl = $expanded_url;
1205                                         $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1206
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);
1212                                 } else {
1213                                         $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4);
1214
1215                                         $tempfile = tempnam(get_temppath(), 'cache');
1216                                         file_put_contents($tempfile, $img_str);
1217
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));
1221                                         } else {
1222                                                 $mime = false;
1223                                         }
1224
1225                                         unlink($tempfile);
1226
1227                                         if (substr($mime, 0, 6) == 'image/') {
1228                                                 $type = 'photo';
1229                                                 $body = str_replace($url->url, '[img]' . $expanded_url . '[/img]', $body);
1230                                         } else {
1231                                                 $type = $oembed_data->type;
1232                                                 $footerurl = $expanded_url;
1233                                                 $footerlink = '[url=' . $expanded_url . ']' . $url->display_url . '[/url]';
1234
1235                                                 $body = str_replace($url->url, $footerlink, $body);
1236                                         }
1237                                 }
1238                         }
1239                 }
1240
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);
1245                         }
1246
1247                         if (($footerlink != '') && (trim($footer) != '')) {
1248                                 $removedlink = trim(str_replace($footerlink, '', $body));
1249
1250                                 if (($removedlink == '') || strstr($body, $removedlink)) {
1251                                         $body = $removedlink;
1252                                 }
1253
1254                                 $body .= $footer;
1255                         }
1256
1257                         if ($footer == '' && $picture != '') {
1258                                 $body .= "\n\n[img]" . $picture . "[/img]\n";
1259                         } elseif ($footer == '' && $picture == '') {
1260                                 $body = add_page_info_to_body($body);
1261                         }
1262                 }
1263         }
1264
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);
1267
1268         if (count($tags)) {
1269                 foreach ($tags as $tag) {
1270                         if (strstr(trim($tag), ' ')) {
1271                                 continue;
1272                         }
1273
1274                         if (strpos($tag, '#') === 0) {
1275                                 if (strpos($tag, '[url=')) {
1276                                         continue;
1277                                 }
1278
1279                                 // don't link tags that are already embedded in links
1280                                 if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
1281                                         continue;
1282                                 }
1283                                 if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
1284                                         continue;
1285                                 }
1286
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=')) {
1293                                         continue;
1294                                 }
1295
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;
1300                         }
1301                 }
1302         }
1303
1304         $tags = implode($tags_arr, ',');
1305
1306         return ['body' => $body, 'tags' => $tags, 'plain' => $plain];
1307 }
1308
1309 /**
1310  * @brief Fetch media entities and add media links to the body
1311  *
1312  * @param object $post Twitter object with the post
1313  * @param array $postarray Array of the item that is about to be posted
1314  *
1315  * @return $picture string Image URL or empty string
1316  */
1317 function twitter_media_entities($post, array &$postarray)
1318 {
1319         // There are no media entities? So we quit.
1320         if (empty($post->extended_entities->media)) {
1321                 return '';
1322         }
1323
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];
1328                 $picture = '';
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']);
1334                                 return $picture;
1335                         }
1336                 }
1337         }
1338
1339         // This is a pure media post, first search for all media urls
1340         $media = [];
1341         foreach ($post->extended_entities->media AS $medium) {
1342                 if (!isset($media[$medium->url])) {
1343                         $media[$medium->url] = '';
1344                 }
1345                 switch ($medium->type) {
1346                         case 'photo':
1347                                 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . '[/img]';
1348                                 $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1349                                 break;
1350                         case 'video':
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)) {
1355                                         $bitrate = 0;
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;
1361                                                 }
1362                                         }
1363                                 }
1364                                 break;
1365                         // The following code will only be activated for test reasons
1366                         //default:
1367                         //      $postarray['body'] .= print_r($medium, true);
1368                 }
1369         }
1370
1371         // Now we replace the media urls.
1372         foreach ($media AS $key => $value) {
1373                 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1374         }
1375
1376         return '';
1377 }
1378
1379 function twitter_createpost(App $a, $uid, $post, array $self, $create_user, $only_existing_contact, $noquote)
1380 {
1381         $postarray = [];
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);
1388
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);
1392                 return [];
1393         }
1394
1395         $contactid = 0;
1396
1397         if ($post->in_reply_to_status_id_str != "") {
1398                 $parent = "twitter::" . $post->in_reply_to_status_id_str;
1399
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]);
1404                 }
1405
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;
1411                 } else {
1412                         $postarray['thr-parent'] = $postarray['uri'];
1413                         $postarray['parent-uri'] = $postarray['uri'];
1414                         $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1415                 }
1416
1417                 // Is it me?
1418                 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1419
1420                 if ($post->user->id_str == $own_id) {
1421                         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1422                                 intval($uid));
1423
1424                         if (DBA::isResult($r)) {
1425                                 $contactid = $r[0]["id"];
1426
1427                                 $postarray['owner-name']   = $r[0]["name"];
1428                                 $postarray['owner-link']   = $r[0]["url"];
1429                                 $postarray['owner-avatar'] = $r[0]["photo"];
1430                         } else {
1431                                 Logger::log("No self contact for user " . $uid, Logger::DEBUG);
1432                                 return [];
1433                         }
1434                 }
1435                 // Don't create accounts of people who just comment something
1436                 $create_user = false;
1437         } else {
1438                 $postarray['parent-uri'] = $postarray['uri'];
1439                 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1440         }
1441
1442         if ($contactid == 0) {
1443                 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1444
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);
1448         }
1449
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);
1454                 return [];
1455         }
1456
1457         $postarray['contact-id'] = $contactid;
1458
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);
1465
1466         if ($post->user->protected) {
1467                 $postarray['private'] = 1;
1468                 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1469         } else {
1470                 $postarray['private'] = 0;
1471                 $postarray['allow_cid'] = '';
1472         }
1473
1474         if (!empty($post->full_text)) {
1475                 $postarray['body'] = $post->full_text;
1476         } else {
1477                 $postarray['body'] = $post->text;
1478         }
1479
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;
1483         }
1484
1485         // Search for media links
1486         $picture = twitter_media_entities($post, $postarray);
1487
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);
1493
1494         $statustext = $converted["plain"];
1495
1496         if (!empty($post->place->name)) {
1497                 $postarray["location"] = $post->place->name;
1498         }
1499         if (!empty($post->place->full_name)) {
1500                 $postarray["location"] = $post->place->full_name;
1501         }
1502         if (!empty($post->geo->coordinates)) {
1503                 $postarray["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
1504         }
1505         if (!empty($post->coordinates->coordinates)) {
1506                 $postarray["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
1507         }
1508         if (!empty($post->retweeted_status)) {
1509                 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1510
1511                 if (empty($retweet['body'])) {
1512                         return [];
1513                 }
1514
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'];
1522
1523                 $postarray = $retweet;
1524         }
1525
1526         if (!empty($post->quoted_status) && !$noquote) {
1527                 $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
1528
1529                 if (empty($quoted['body'])) {
1530                         return [];
1531                 }
1532
1533                 $postarray['body'] .= "\n" . share_header(
1534                         $quoted['author-name'],
1535                         $quoted['author-link'],
1536                         $quoted['author-avatar'],
1537                         "",
1538                         $quoted['created'],
1539                         $quoted['plink']
1540                 );
1541
1542                 $postarray['body'] .= $quoted['body'] . '[/share]';
1543         }
1544
1545         return $postarray;
1546 }
1547
1548 function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection, array $self)
1549 {
1550         Logger::log("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, Logger::DEBUG);
1551
1552         $posts = [];
1553
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];
1556
1557                 try {
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());
1561                         break;
1562                 }
1563
1564                 if (empty($post)) {
1565                         Logger::log("twitter_fetchparentposts: Can't fetch post " . $parameters->id, Logger::DEBUG);
1566                         break;
1567                 }
1568
1569                 if (empty($post->id_str)) {
1570                         Logger::log("twitter_fetchparentposts: This is not a post " . json_encode($post), Logger::DEBUG);
1571                         break;
1572                 }
1573
1574                 if (Item::exists(['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1575                         break;
1576                 }
1577
1578                 $posts[] = $post;
1579         }
1580
1581         Logger::log("twitter_fetchparentposts: Fetching " . count($posts) . " parents", Logger::DEBUG);
1582
1583         $posts = array_reverse($posts);
1584
1585         if (!empty($posts)) {
1586                 foreach ($posts as $post) {
1587                         $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1588
1589                         if (empty($postarray['body'])) {
1590                                 continue;
1591                         }
1592
1593                         $item = Item::insert($postarray);
1594
1595                         $postarray["id"] = $item;
1596
1597                         Logger::log('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
1598                 }
1599         }
1600 }
1601
1602 function twitter_fetchhometimeline(App $a, $uid)
1603 {
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');
1610
1611         Logger::log("Fetching timeline for user " . $uid, Logger::DEBUG);
1612
1613         $application_name = Config::get('twitter', 'application_name');
1614
1615         if ($application_name == "") {
1616                 $application_name = $a->getHostName();
1617         }
1618
1619         require_once 'include/items.php';
1620
1621         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1622
1623         try {
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());
1627                 return;
1628         }
1629
1630         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1631                 intval($own_contact),
1632                 intval($uid));
1633
1634         if (DBA::isResult($r)) {
1635                 $own_id = $r[0]["nick"];
1636         } else {
1637                 Logger::log("Own twitter contact not found for user " . $uid);
1638                 return;
1639         }
1640
1641         $self = User::getOwnerDataById($uid);
1642         if ($self === false) {
1643                 Logger::log("Own contact not found for user " . $uid);
1644                 return;
1645         }
1646
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');
1651
1652         $first_time = ($lastid == "");
1653
1654         if ($lastid != "") {
1655                 $parameters["since_id"] = $lastid;
1656         }
1657
1658         try {
1659                 $items = $connection->get('statuses/home_timeline', $parameters);
1660         } catch (TwitterOAuthException $e) {
1661                 Logger::log('Error fetching home timeline for user ' . $uid . ': ' . $e->getMessage());
1662                 return;
1663         }
1664
1665         if (!is_array($items)) {
1666                 Logger::log('No array while fetching home timeline for user ' . $uid . ': ' . print_r($items, true));
1667                 return;
1668         }
1669
1670         if (empty($items)) {
1671                 Logger::log('No new timeline content for user ' . $uid, Logger::INFO);
1672                 return;
1673         }
1674
1675         $posts = array_reverse($items);
1676
1677         Logger::log('Fetching timeline from ID ' . $lastid . ' for user ' . $uid . ' ' . sizeof($posts) . ' items', Logger::DEBUG);
1678
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);
1684                         }
1685
1686                         if ($first_time) {
1687                                 continue;
1688                         }
1689
1690                         if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1691                                 Logger::log("Skip previously sent post", Logger::DEBUG);
1692                                 continue;
1693                         }
1694
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);
1697                                 continue;
1698                         }
1699
1700                         if ($post->in_reply_to_status_id_str != "") {
1701                                 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1702                         }
1703
1704                         Logger::log('Preparing post ' . $post->id_str . ' for user ' . $uid, Logger::DEBUG);
1705
1706                         $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1707
1708                         if (empty($postarray['body']) || trim($postarray['body']) == "") {
1709                                 Logger::log('Empty body for post ' . $post->id_str . ' and user ' . $uid, Logger::DEBUG);
1710                                 continue;
1711                         }
1712
1713                         $notify = false;
1714
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);
1719                                 }
1720                         }
1721
1722                         $item = Item::insert($postarray, false, $notify);
1723                         $postarray["id"] = $item;
1724
1725                         Logger::log('User ' . $uid . ' posted home timeline item ' . $item);
1726                 }
1727         }
1728         PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1729
1730         Logger::log('Last timeline ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1731
1732         // Fetching mentions
1733         $lastid = PConfig::get($uid, 'twitter', 'lastmentionid');
1734
1735         $first_time = ($lastid == "");
1736
1737         if ($lastid != "") {
1738                 $parameters["since_id"] = $lastid;
1739         }
1740
1741         try {
1742                 $items = $connection->get('statuses/mentions_timeline', $parameters);
1743         } catch (TwitterOAuthException $e) {
1744                 Logger::log('Error fetching mentions: ' . $e->getMessage());
1745                 return;
1746         }
1747
1748         if (!is_array($items)) {
1749                 Logger::log("Error fetching mentions: " . print_r($items, true), Logger::DEBUG);
1750                 return;
1751         }
1752
1753         $posts = array_reverse($items);
1754
1755         Logger::log("Fetching mentions for user " . $uid . " " . sizeof($posts) . " items", Logger::DEBUG);
1756
1757         if (count($posts)) {
1758                 foreach ($posts as $post) {
1759                         if ($post->id_str > $lastid) {
1760                                 $lastid = $post->id_str;
1761                         }
1762
1763                         if ($first_time) {
1764                                 continue;
1765                         }
1766
1767                         if ($post->in_reply_to_status_id_str != "") {
1768                                 twitter_fetchparentposts($a, $uid, $post, $connection, $self);
1769                         }
1770
1771                         $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1772
1773                         if (empty($postarray['body'])) {
1774                                 continue;
1775                         }
1776
1777                         $item = Item::insert($postarray);
1778
1779                         Logger::log('User ' . $uid . ' posted mention timeline item ' . $item);
1780                 }
1781         }
1782
1783         PConfig::set($uid, 'twitter', 'lastmentionid', $lastid);
1784
1785         Logger::log('Last mentions ID for user ' . $uid . ' is now ' . $lastid, Logger::DEBUG);
1786 }
1787
1788 function twitter_fetch_own_contact(App $a, $uid)
1789 {
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');
1794
1795         $own_id = PConfig::get($uid, 'twitter', 'own_id');
1796
1797         $contact_id = 0;
1798
1799         if ($own_id == "") {
1800                 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1801
1802                 // Fetching user data
1803                 // get() may throw TwitterOAuthException, but we will catch it later
1804                 $user = $connection->get('account/verify_credentials');
1805
1806                 PConfig::set($uid, 'twitter', 'own_id', $user->id_str);
1807
1808                 $contact_id = twitter_fetch_contact($uid, $user, true);
1809         } else {
1810                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1811                         intval($uid),
1812                         DBA::escape("twitter::" . $own_id));
1813                 if (DBA::isResult($r)) {
1814                         $contact_id = $r[0]["id"];
1815                 } else {
1816                         PConfig::delete($uid, 'twitter', 'own_id');
1817                 }
1818         }
1819
1820         return $contact_id;
1821 }
1822
1823 function twitter_is_retweet(App $a, $uid, $body)
1824 {
1825         $body = trim($body);
1826
1827         // Skip if it isn't a pure repeated messages
1828         // Does it start with a share?
1829         if (strpos($body, "[share") > 0) {
1830                 return false;
1831         }
1832
1833         // Does it end with a share?
1834         if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
1835                 return false;
1836         }
1837
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) {
1841                 return false;
1842         }
1843
1844         $link = "";
1845         preg_match("/link='(.*?)'/ism", $attributes, $matches);
1846         if (!empty($matches[1])) {
1847                 $link = $matches[1];
1848         }
1849
1850         preg_match('/link="(.*?)"/ism', $attributes, $matches);
1851         if (!empty($matches[1])) {
1852                 $link = $matches[1];
1853         }
1854
1855         $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
1856         if ($id == $link) {
1857                 return false;
1858         }
1859
1860         Logger::log('twitter_is_retweet: Retweeting id ' . $id . ' for user ' . $uid, Logger::DEBUG);
1861
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');
1866
1867         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1868         $result = $connection->post('statuses/retweet/' . $id);
1869
1870         Logger::log('twitter_is_retweet: result ' . print_r($result, true), Logger::DEBUG);
1871
1872         return !isset($result->errors);
1873 }
1874
1875 function twitter_update_mentions($body)
1876 {
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);
1883                         } else {
1884                                 $return = $matches[2] . ' (' . $matches[1] . ')';
1885                         }
1886
1887                         return $return;
1888                 },
1889                 $body
1890         );
1891
1892         return $return;
1893 }
1894
1895 function twitter_convert_share(array $attributes, array $author_contact, $content, $is_quote_share)
1896 {
1897         if ($author_contact['network'] == Protocol::TWITTER) {
1898                 $mention = '@' . $author_contact['nickname'];
1899         } else {
1900                 $mention = $author_contact['addr'];
1901         }
1902
1903         return ($is_quote_share ? "\n\n" : '' ) . 'RT ' . $mention . ': ' . $content . "\n\n" . $attributes['link'];
1904 }