Merge pull request #652 from MrPetovan/task/5410-rename-dba-to-DBA
[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\PConfig;
73 use Friendica\Core\Worker;
74 use Friendica\Database\DBA;
75 use Friendica\Database\DBM;
76 use Friendica\Model\Contact;
77 use Friendica\Model\GContact;
78 use Friendica\Model\Group;
79 use Friendica\Model\Item;
80 use Friendica\Model\ItemContent;
81 use Friendica\Model\Queue;
82 use Friendica\Model\User;
83 use Friendica\Object\Image;
84 use Friendica\Util\DateTimeFormat;
85 use Friendica\Util\Network;
86
87 require_once 'boot.php';
88 require_once 'include/dba.php';
89 require_once 'include/enotify.php';
90 require_once 'include/text.php';
91
92 require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
93
94 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
95
96 function twitter_install()
97 {
98         //  we need some hooks, for the configuration and for sending tweets
99         Addon::registerHook('load_config', 'addon/twitter/twitter.php', 'twitter_load_config');
100         Addon::registerHook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
101         Addon::registerHook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
102         Addon::registerHook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
103         Addon::registerHook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
104         Addon::registerHook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
105         Addon::registerHook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
106         Addon::registerHook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook');
107         Addon::registerHook('follow', 'addon/twitter/twitter.php', 'twitter_follow');
108         Addon::registerHook('expire', 'addon/twitter/twitter.php', 'twitter_expire');
109         Addon::registerHook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body');
110         Addon::registerHook('check_item_notification', 'addon/twitter/twitter.php', 'twitter_check_item_notification');
111         logger("installed twitter");
112 }
113
114 function twitter_uninstall()
115 {
116         Addon::unregisterHook('load_config', 'addon/twitter/twitter.php', 'twitter_load_config');
117         Addon::unregisterHook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
118         Addon::unregisterHook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
119         Addon::unregisterHook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
120         Addon::unregisterHook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
121         Addon::unregisterHook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
122         Addon::unregisterHook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
123         Addon::unregisterHook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook');
124         Addon::unregisterHook('follow', 'addon/twitter/twitter.php', 'twitter_follow');
125         Addon::unregisterHook('expire', 'addon/twitter/twitter.php', 'twitter_expire');
126         Addon::unregisterHook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body');
127         Addon::unregisterHook('check_item_notification', 'addon/twitter/twitter.php', 'twitter_check_item_notification');
128
129         // old setting - remove only
130         Addon::unregisterHook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
131         Addon::unregisterHook('addon_settings', 'addon/twitter/twitter.php', 'twitter_settings');
132         Addon::unregisterHook('addon_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
133 }
134
135 function twitter_load_config(App $a)
136 {
137         $a->loadConfigFile(__DIR__. '/config/twitter.ini.php');
138 }
139
140 function twitter_check_item_notification(App $a, &$notification_data)
141 {
142         $own_id = PConfig::get($notification_data["uid"], 'twitter', 'own_id');
143
144         $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
145                         intval($notification_data["uid"]),
146                         dbesc("twitter::".$own_id)
147         );
148
149         if ($own_user) {
150                 $notification_data["profiles"][] = $own_user[0]["url"];
151         }
152 }
153
154 function twitter_follow(App $a, &$contact)
155 {
156         logger("twitter_follow: Check if contact is twitter contact. " . $contact["url"], LOGGER_DEBUG);
157
158         if (!strstr($contact["url"], "://twitter.com") && !strstr($contact["url"], "@twitter.com")) {
159                 return;
160         }
161
162         // contact seems to be a twitter contact, so continue
163         $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
164         $nickname = str_replace("@twitter.com", "", $nickname);
165
166         $uid = $a->user["uid"];
167
168         $ckey = Config::get('twitter', 'consumerkey');
169         $csecret = Config::get('twitter', 'consumersecret');
170         $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
171         $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
172
173         // If the addon is not configured (general or for this user) quit here
174         if (empty($ckey) || empty($csecret) || empty($otoken) || empty($osecret)) {
175                 $contact = false;
176                 return;
177         }
178
179         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
180         $connection->post('friendships/create', ['screen_name' => $nickname]);
181
182         twitter_fetchuser($a, $uid, $nickname);
183
184         $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey
185                 FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
186                                 intval($uid),
187                                 dbesc($nickname));
188         if (DBM::is_result($r)) {
189                 $contact["contact"] = $r[0];
190         }
191 }
192
193 function twitter_jot_nets(App $a, &$b)
194 {
195         if (!local_user()) {
196                 return;
197         }
198
199         $tw_post = PConfig::get(local_user(), 'twitter', 'post');
200         if (intval($tw_post) == 1) {
201                 $tw_defpost = PConfig::get(local_user(), 'twitter', 'post_by_default');
202                 $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
203                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> '
204                         . L10n::t('Post to Twitter') . '</div>';
205         }
206 }
207
208 function twitter_settings_post(App $a, $post)
209 {
210         if (!local_user()) {
211                 return;
212         }
213         // don't check twitter settings if twitter submit button is not clicked
214         if (empty($_POST['twitter-disconnect']) && empty($_POST['twitter-submit'])) {
215                 return;
216         }
217
218         if (!empty($_POST['twitter-disconnect'])) {
219                 /*               * *
220                  * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
221                  * from the user configuration
222                  */
223                 PConfig::delete(local_user(), 'twitter', 'consumerkey');
224                 PConfig::delete(local_user(), 'twitter', 'consumersecret');
225                 PConfig::delete(local_user(), 'twitter', 'oauthtoken');
226                 PConfig::delete(local_user(), 'twitter', 'oauthsecret');
227                 PConfig::delete(local_user(), 'twitter', 'post');
228                 PConfig::delete(local_user(), 'twitter', 'post_by_default');
229                 PConfig::delete(local_user(), 'twitter', 'lastid');
230                 PConfig::delete(local_user(), 'twitter', 'mirror_posts');
231                 PConfig::delete(local_user(), 'twitter', 'import');
232                 PConfig::delete(local_user(), 'twitter', 'create_user');
233                 PConfig::delete(local_user(), 'twitter', 'own_id');
234         } else {
235                 if (isset($_POST['twitter-pin'])) {
236                         //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
237                         logger('got a Twitter PIN');
238                         $ckey    = Config::get('twitter', 'consumerkey');
239                         $csecret = Config::get('twitter', 'consumersecret');
240                         //  the token and secret for which the PIN was generated were hidden in the settings
241                         //  form as token and token2, we need a new connection to Twitter using these token
242                         //  and secret to request a Access Token with the PIN
243                         try {
244                                 if (empty($_POST['twitter-pin'])) {
245                                         throw new Exception(L10n::t('You submitted an empty PIN, please Sign In with Twitter again to get a new one.'));
246                                 }
247
248                                 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
249                                 $token = $connection->oauth("oauth/access_token", ["oauth_verifier" => $_POST['twitter-pin']]);
250                                 //  ok, now that we have the Access Token, save them in the user config
251                                 PConfig::set(local_user(), 'twitter', 'oauthtoken', $token['oauth_token']);
252                                 PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
253                                 PConfig::set(local_user(), 'twitter', 'post', 1);
254                         } catch(Exception $e) {
255                                 info($e->getMessage());
256                         } catch(TwitterOAuthException $e) {
257                                 info($e->getMessage());
258                         }
259                         //  reload the Addon Settings page, if we don't do it see Bug #42
260                         goaway('settings/connectors');
261                 } else {
262                         //  if no PIN is supplied in the POST variables, the user has changed the setting
263                         //  to post a tweet for every new __public__ posting to the wall
264                         PConfig::set(local_user(), 'twitter', 'post', intval($_POST['twitter-enable']));
265                         PConfig::set(local_user(), 'twitter', 'post_by_default', intval($_POST['twitter-default']));
266                         PConfig::set(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
267                         PConfig::set(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
268                         PConfig::set(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
269
270                         if (!intval($_POST['twitter-mirror'])) {
271                                 PConfig::delete(local_user(), 'twitter', 'lastid');
272                         }
273
274                         info(L10n::t('Twitter settings updated.') . EOL);
275                 }
276         }
277 }
278
279 function twitter_settings(App $a, &$s)
280 {
281         if (!local_user()) {
282                 return;
283         }
284         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
285         /*       * *
286          * 1) Check that we have global consumer key & secret
287          * 2) If no OAuthtoken & stuff is present, generate button to get some
288          * 3) Checkbox for "Send public notices (280 chars only)
289          */
290         $ckey    = Config::get('twitter', 'consumerkey');
291         $csecret = Config::get('twitter', 'consumersecret');
292         $otoken  = PConfig::get(local_user(), 'twitter', 'oauthtoken');
293         $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret');
294
295         $enabled            = intval(PConfig::get(local_user(), 'twitter', 'post'));
296         $defenabled         = intval(PConfig::get(local_user(), 'twitter', 'post_by_default'));
297         $mirrorenabled      = intval(PConfig::get(local_user(), 'twitter', 'mirror_posts'));
298         $importenabled      = intval(PConfig::get(local_user(), 'twitter', 'import'));
299         $create_userenabled = intval(PConfig::get(local_user(), 'twitter', 'create_user'));
300
301         $css = (($enabled) ? '' : '-disabled');
302
303         $s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
304         $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
305         $s .= '</span>';
306         $s .= '<div id="settings_twitter_expanded" class="settings-block" style="display: none;">';
307         $s .= '<span class="fakelink" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
308         $s .= '<img class="connector' . $css . '" src="images/twitter.png" /><h3 class="connector">' . L10n::t('Twitter Import/Export/Mirror') . '</h3>';
309         $s .= '</span>';
310
311         if ((!$ckey) && (!$csecret)) {
312                 /* no global consumer keys
313                  * display warning and skip personal config
314                  */
315                 $s .= '<p>' . L10n::t('No consumer key pair for Twitter found. Please contact your site administrator.') . '</p>';
316         } else {
317                 // ok we have a consumer key pair now look into the OAuth stuff
318                 if ((!$otoken) && (!$osecret)) {
319                         /* the user has not yet connected the account to twitter...
320                          * get a temporary OAuth key/secret pair and display a button with
321                          * which the user can request a PIN to connect the account to a
322                          * account at Twitter.
323                          */
324                         $connection = new TwitterOAuth($ckey, $csecret);
325                         try {
326                                 $result = $connection->oauth('oauth/request_token', ['oauth_callback' => 'oob']);
327                                 $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>';
328                                 $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>';
329                                 $s .= '<div id="twitter-pin-wrapper">';
330                                 $s .= '<label id="twitter-pin-label" for="twitter-pin">' . L10n::t('Copy the PIN from Twitter here') . '</label>';
331                                 $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
332                                 $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="' . $result['oauth_token'] . '" />';
333                                 $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="' . $result['oauth_token_secret'] . '" />';
334                                 $s .= '</div><div class="clear"></div>';
335                                 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
336                         } catch (TwitterOAuthException $e) {
337                                 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
338                         }
339                 } else {
340                         /*                       * *
341                          *  we have an OAuth key / secret pair for the user
342                          *  so let's give a chance to disable the postings to Twitter
343                          */
344                         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
345                         try {
346                                 $details = $connection->get('account/verify_credentials');
347
348                                 $field_checkbox = get_markup_template('field_checkbox.tpl');
349
350                                 $s .= '<div id="twitter-info" >
351                                         <p>' . L10n::t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
352                                                 <button type="submit" name="twitter-disconnect" value="1">' . L10n::t('Disconnect') . '</button>
353                                         </p>
354                                         <p id="twitter-info-block">
355                                                 <a href="https://twitter.com/' . $details->screen_name . '" target="_twitter"><img id="twitter-avatar" src="' . $details->profile_image_url . '" /></a>
356                                                 <em>' . $details->description . '</em>
357                                         </p>
358                                 </div>';
359                                 $s .= '<div class="clear"></div>';
360
361                                 $s .= replace_macros($field_checkbox, [
362                                         '$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.')]
363                                 ]);
364                                 if ($a->user['hidewall']) {
365                                         $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>';
366                                 }
367                                 $s .= replace_macros($field_checkbox, [
368                                         '$field' => ['twitter-default', L10n::t('Send public postings to Twitter by default'), $defenabled, '']
369                                 ]);
370                                 $s .= replace_macros($field_checkbox, [
371                                         '$field' => ['twitter-mirror', L10n::t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '']
372                                 ]);
373                                 $s .= replace_macros($field_checkbox, [
374                                         '$field' => ['twitter-import', L10n::t('Import the remote timeline'), $importenabled, '']
375                                 ]);
376                                 $s .= replace_macros($field_checkbox, [
377                                         '$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.')]
378                                 ]);
379                                 $s .= '<div class="clear"></div>';
380                                 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
381                         } catch (TwitterOAuthException $e) {
382                                 $s .= '<p>' . L10n::t('An error occured: ') . $e->getMessage() . '</p>';
383                         }
384                 }
385         }
386         $s .= '</div><div class="clear"></div>';
387 }
388
389 function twitter_post_local(App $a, &$b)
390 {
391         if ($b['edit']) {
392                 return;
393         }
394
395         if (!local_user() || (local_user() != $b['uid'])) {
396                 return;
397         }
398
399         $twitter_post = intval(PConfig::get(local_user(), 'twitter', 'post'));
400         $twitter_enable = (($twitter_post && x($_REQUEST, 'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
401
402         // if API is used, default to the chosen settings
403         if ($b['api_source'] && intval(PConfig::get(local_user(), 'twitter', 'post_by_default'))) {
404                 $twitter_enable = 1;
405         }
406
407         if (!$twitter_enable) {
408                 return;
409         }
410
411         if (strlen($b['postopts'])) {
412                 $b['postopts'] .= ',';
413         }
414
415         $b['postopts'] .= 'twitter';
416 }
417
418 function twitter_action(App $a, $uid, $pid, $action)
419 {
420         $ckey = Config::get('twitter', 'consumerkey');
421         $csecret = Config::get('twitter', 'consumersecret');
422         $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
423         $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
424
425         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
426
427         $post = ['id' => $pid];
428
429         logger("twitter_action '" . $action . "' ID: " . $pid . " data: " . print_r($post, true), LOGGER_DATA);
430
431         switch ($action) {
432                 case "delete":
433                         // To-Do: $result = $connection->post('statuses/destroy', $post);
434                         $result = [];
435                         break;
436                 case "like":
437                         $result = $connection->post('favorites/create', $post);
438                         break;
439                 case "unlike":
440                         $result = $connection->post('favorites/destroy', $post);
441                         break;
442                 default:
443                         logger('Unhandled action ' . $action, LOGGER_DEBUG);
444                         $result = [];
445         }
446         logger("twitter_action '" . $action . "' send, result: " . print_r($result, true), LOGGER_DEBUG);
447 }
448
449 function twitter_post_hook(App $a, &$b)
450 {
451         // Post to Twitter
452         if (!PConfig::get($b["uid"], 'twitter', 'import')
453                 && ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))) {
454                 return;
455         }
456
457         if ($b['parent'] != $b['id']) {
458                 logger("twitter_post_hook: parameter " . print_r($b, true), LOGGER_DATA);
459
460                 // Looking if its a reply to a twitter post
461                 if ((substr($b["parent-uri"], 0, 9) != "twitter::")
462                         && (substr($b["extid"], 0, 9) != "twitter::")
463                         && (substr($b["thr-parent"], 0, 9) != "twitter::"))
464                 {
465                         logger("twitter_post_hook: no twitter post " . $b["parent"]);
466                         return;
467                 }
468
469                 $condition = ['uri' => $b["thr-parent"], 'uid' => $b["uid"]];
470                 $orig_post = Item::selectFirst([], $condition);
471                 if (!DBM::is_result($orig_post)) {
472                         logger("twitter_post_hook: no parent found " . $b["thr-parent"]);
473                         return;
474                 } else {
475                         $iscomment = true;
476                 }
477
478
479                 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
480                 $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
481                 $nicknameplain = "@" . $nicknameplain;
482
483                 logger("twitter_post_hook: comparing " . $nickname . " and " . $nicknameplain . " with " . $b["body"], LOGGER_DEBUG);
484                 if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) {
485                         $b["body"] = $nickname . " " . $b["body"];
486                 }
487
488                 logger("twitter_post_hook: parent found " . print_r($orig_post, true), LOGGER_DATA);
489         } else {
490                 $iscomment = false;
491
492                 if ($b['private'] || !strstr($b['postopts'], 'twitter')) {
493                         return;
494                 }
495
496                 // Dont't post if the post doesn't belong to us.
497                 // This is a check for forum postings
498                 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
499                 if ($b['contact-id'] != $self['id']) {
500                         return;
501                 }
502         }
503
504         if (($b['verb'] == ACTIVITY_POST) && $b['deleted']) {
505                 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
506         }
507
508         if ($b['verb'] == ACTIVITY_LIKE) {
509                 logger("twitter_post_hook: parameter 2 " . substr($b["thr-parent"], 9), LOGGER_DEBUG);
510                 if ($b['deleted']) {
511                         twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
512                 } else {
513                         twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
514                 }
515
516                 return;
517         }
518
519         if ($b['deleted'] || ($b['created'] !== $b['edited'])) {
520                 return;
521         }
522
523         // if post comes from twitter don't send it back
524         if ($b['extid'] == NETWORK_TWITTER) {
525                 return;
526         }
527
528         if ($b['app'] == "Twitter") {
529                 return;
530         }
531
532         logger('twitter post invoked');
533
534         PConfig::load($b['uid'], 'twitter');
535
536         $ckey    = Config::get('twitter', 'consumerkey');
537         $csecret = Config::get('twitter', 'consumersecret');
538         $otoken  = PConfig::get($b['uid'], 'twitter', 'oauthtoken');
539         $osecret = PConfig::get($b['uid'], 'twitter', 'oauthsecret');
540
541         if ($ckey && $csecret && $otoken && $osecret) {
542                 logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG);
543
544                 // If it's a repeated message from twitter then do a native retweet and exit
545                 if (twitter_is_retweet($a, $b['uid'], $b['body'])) {
546                         return;
547                 }
548
549                 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
550
551                 // Set the timeout for upload to 30 seconds
552                 $connection->setTimeouts(10, 30);
553
554                 $max_char = 280;
555                 $msgarr = ItemContent::getPlaintextPost($b, $max_char, true, 8);
556                 $msg = $msgarr["text"];
557
558                 if (($msg == "") && isset($msgarr["title"])) {
559                         $msg = Plaintext::shorten($msgarr["title"], $max_char - 50);
560                 }
561
562                 $image = "";
563
564                 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
565                         $msg .= "\n" . $msgarr["url"];
566                         $url_added = true;
567                 } else {
568                         $url_added = false;
569                 }
570
571                 if (isset($msgarr["image"]) && ($msgarr["type"] != "video")) {
572                         $image = $msgarr["image"];
573                 }
574
575                 if (empty($msg)) {
576                         return;
577                 }
578
579                 // and now tweet it :-)
580                 $post = [];
581
582                 if (!empty($image)) {
583                         try {
584                                 $img_str = Network::fetchUrl($image);
585
586                                 $tempfile = tempnam(get_temppath(), 'cache');
587                                 file_put_contents($tempfile, $img_str);
588
589                                 $media = $connection->upload('media/upload', ['media' => $tempfile]);
590
591                                 unlink($tempfile);
592
593                                 $post['media_ids'] = $media->media_id_string;
594                         } catch (Exception $e) {
595                                 logger('Exception when trying to send to Twitter: ' . $e->getMessage());
596
597                                 // Workaround: Remove the picture link so that the post can be reposted without it
598                                 // When there is another url already added, a second url would be superfluous.
599                                 if (!$url_added) {
600                                         $msg .= "\n" . $image;
601                                 }
602
603                                 $image = "";
604                         }
605                 }
606
607                 $post['status'] = $msg;
608
609                 if ($iscomment) {
610                         $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
611                 }
612
613                 $url = 'statuses/update';
614                 $result = $connection->post($url, $post);
615                 logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
616
617                 if (!empty($result->source)) {
618                         Config::set("twitter", "application_name", strip_tags($result->source));
619                 }
620
621                 if (!empty($result->errors)) {
622                         logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
623
624                         $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
625                         if (DBM::is_result($r)) {
626                                 $a->contact = $r[0]["id"];
627                         }
628
629                         $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $post]);
630
631                         Queue::add($a->contact, NETWORK_TWITTER, $s);
632                         notice(L10n::t('Twitter post failed. Queued for retry.') . EOL);
633                 } elseif ($iscomment) {
634                         logger('twitter_post: Update extid ' . $result->id_str . " for post id " . $b['id']);
635                         Item::update(['extid' => "twitter::" . $result->id_str], ['id' => $b['id']]);
636                 }
637         }
638 }
639
640 function twitter_addon_admin_post(App $a)
641 {
642         $consumerkey    = x($_POST, 'consumerkey')    ? notags(trim($_POST['consumerkey']))    : '';
643         $consumersecret = x($_POST, 'consumersecret') ? notags(trim($_POST['consumersecret'])) : '';
644         Config::set('twitter', 'consumerkey', $consumerkey);
645         Config::set('twitter', 'consumersecret', $consumersecret);
646         info(L10n::t('Settings updated.') . EOL);
647 }
648
649 function twitter_addon_admin(App $a, &$o)
650 {
651         $t = get_markup_template("admin.tpl", "addon/twitter/");
652
653         $o = replace_macros($t, [
654                 '$submit' => L10n::t('Save Settings'),
655                 // name, label, value, help, [extra values]
656                 '$consumerkey' => ['consumerkey', L10n::t('Consumer key'), Config::get('twitter', 'consumerkey'), ''],
657                 '$consumersecret' => ['consumersecret', L10n::t('Consumer secret'), Config::get('twitter', 'consumersecret'), ''],
658         ]);
659 }
660
661 function twitter_cron(App $a, $b)
662 {
663         $last = Config::get('twitter', 'last_poll');
664
665         $poll_interval = intval(Config::get('twitter', 'poll_interval'));
666         if (!$poll_interval) {
667                 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
668         }
669
670         if ($last) {
671                 $next = $last + ($poll_interval * 60);
672                 if ($next > time()) {
673                         logger('twitter: poll intervall not reached');
674                         return;
675                 }
676         }
677         logger('twitter: cron_start');
678
679         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1'");
680         if (DBM::is_result($r)) {
681                 foreach ($r as $rr) {
682                         logger('twitter: fetching for user ' . $rr['uid']);
683                         Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 1, (int) $rr['uid']);
684                 }
685         }
686
687         $abandon_days = intval(Config::get('system', 'account_abandon_days'));
688         if ($abandon_days < 1) {
689                 $abandon_days = 0;
690         }
691
692         $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400);
693
694         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1'");
695         if (DBM::is_result($r)) {
696                 foreach ($r as $rr) {
697                         if ($abandon_days != 0) {
698                                 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
699                                 if (!DBM::is_result($user)) {
700                                         logger('abandoned account: timeline from user ' . $rr['uid'] . ' will not be imported');
701                                         continue;
702                                 }
703                         }
704
705                         logger('twitter: importing timeline from user ' . $rr['uid']);
706                         Worker::add(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 2, (int) $rr['uid']);
707                         /*
708                           // To-Do
709                           // check for new contacts once a day
710                           $last_contact_check = PConfig::get($rr['uid'],'pumpio','contact_check');
711                           if($last_contact_check)
712                           $next_contact_check = $last_contact_check + 86400;
713                           else
714                           $next_contact_check = 0;
715
716                           if($next_contact_check <= time()) {
717                           pumpio_getallusers($a, $rr["uid"]);
718                           PConfig::set($rr['uid'],'pumpio','contact_check',time());
719                           }
720                          */
721                 }
722         }
723
724         logger('twitter: cron_end');
725
726         Config::set('twitter', 'last_poll', time());
727 }
728
729 function twitter_expire(App $a, $b)
730 {
731         $days = Config::get('twitter', 'expire');
732
733         if ($days == 0) {
734                 return;
735         }
736
737         $r = DBA::select('item', ['id', 'iaid', 'icid'], ['deleted' => true, 'network' => NETWORK_TWITTER]);
738         while ($row = DBA::fetch($r)) {
739                 DBA::delete('item', ['id' => $row['id']]);
740                 if (!empty($row['iaid']) && !DBA::exists('item', ['iaid' => $row['iaid']])) {
741                         DBA::delete('item-activity', ['id' => $row['iaid']]);
742                 }
743                 if (!empty($row['icid']) && !DBA::exists('item', ['icid' => $row['icid']])) {
744                         DBA::delete('item-content', ['id' => $row['icid']]);
745                 }
746         }
747         DBA::close($r);
748
749         require_once "include/items.php";
750
751         logger('twitter_expire: expire_start');
752
753         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
754         if (DBM::is_result($r)) {
755                 foreach ($r as $rr) {
756                         logger('twitter_expire: user ' . $rr['uid']);
757                         Item::expire($rr['uid'], $days, NETWORK_TWITTER, true);
758                 }
759         }
760
761         logger('twitter_expire: expire_end');
762 }
763
764 function twitter_prepare_body(App $a, &$b)
765 {
766         if ($b["item"]["network"] != NETWORK_TWITTER) {
767                 return;
768         }
769
770         if ($b["preview"]) {
771                 $max_char = 280;
772                 $item = $b["item"];
773                 $item["plink"] = $a->get_baseurl() . "/display/" . $a->user["nickname"] . "/" . $item["parent"];
774
775                 $condition = ['uri' => $item["thr-parent"], 'uid' => local_user()];
776                 $orig_post = Item::selectFirst(['author-link'], $condition);
777                 if (DBM::is_result($orig_post)) {
778                         $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
779                         $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
780                         $nicknameplain = "@" . $nicknameplain;
781
782                         if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) {
783                                 $item["body"] = $nickname . " " . $item["body"];
784                         }
785                 }
786
787                 $msgarr = ItemContent::getPlaintextPost($item, $max_char, true, 8);
788                 $msg = $msgarr["text"];
789
790                 if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
791                         $msg .= " " . $msgarr["url"];
792                 }
793
794                 if (isset($msgarr["image"])) {
795                         $msg .= " " . $msgarr["image"];
796                 }
797
798                 $b['html'] = nl2br(htmlspecialchars($msg));
799         }
800 }
801
802 /**
803  * @brief Build the item array for the mirrored post
804  *
805  * @param App $a Application class
806  * @param integer $uid User id
807  * @param object $post Twitter object with the post
808  *
809  * @return array item data to be posted
810  */
811 function twitter_do_mirrorpost(App $a, $uid, $post)
812 {
813         $datarray["api_source"] = true;
814         $datarray["profile_uid"] = $uid;
815         $datarray["extid"] = NETWORK_TWITTER;
816         $datarray['message_id'] = Item::newURI($uid, NETWORK_TWITTER . ":" . $post->id);
817         // $datarray['object'] = json_encode($post); // Activate for debugging
818         $datarray["title"] = "";
819
820         if (!empty($post->retweeted_status)) {
821                 // We don't support nested shares, so we mustn't show quotes as shares on retweets
822                 $item = twitter_createpost($a, $uid, $post->retweeted_status, ['id' => 0], false, false, true);
823
824                 $datarray['body'] = "\n" . share_header(
825                         $item['author-name'],
826                         $item['author-link'],
827                         $item['author-avatar'],
828                         "",
829                         $item['created'],
830                         $item['plink']
831                 );
832
833                 $datarray['body'] .= $item['body'] . '[/share]';
834         } else {
835                 $item = twitter_createpost($a, $uid, $post, ['id' => 0], false, false, false);
836
837                 $datarray['body'] = $item['body'];
838         }
839
840         $datarray["source"] = $item['app'];
841         $datarray["verb"] = $item['verb'];
842
843         if (isset($item["location"])) {
844                 $datarray["location"] = $item["location"];
845         }
846
847         if (isset($item["coord"])) {
848                 $datarray["coord"] = $item["coord"];
849         }
850
851         return $datarray;
852 }
853
854 function twitter_fetchtimeline(App $a, $uid)
855 {
856         $ckey    = Config::get('twitter', 'consumerkey');
857         $csecret = Config::get('twitter', 'consumersecret');
858         $otoken  = PConfig::get($uid, 'twitter', 'oauthtoken');
859         $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
860         $lastid  = PConfig::get($uid, 'twitter', 'lastid');
861
862         $application_name = Config::get('twitter', 'application_name');
863
864         if ($application_name == "") {
865                 $application_name = $a->get_hostname();
866         }
867
868         $has_picture = false;
869
870         require_once 'mod/item.php';
871         require_once 'include/items.php';
872         require_once 'mod/share.php';
873
874         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
875
876         $parameters = ["exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
877
878         $first_time = ($lastid == "");
879
880         if ($lastid != "") {
881                 $parameters["since_id"] = $lastid;
882         }
883
884         try {
885                 $items = $connection->get('statuses/user_timeline', $parameters);
886         } catch (TwitterOAuthException $e) {
887                 logger('twitter_fetchtimeline: Error fetching timeline for user ' . $uid . ': ' . $e->getMessage());
888                 return;
889         }
890
891         if (!is_array($items)) {
892                 return;
893         }
894
895         $posts = array_reverse($items);
896
897         if (count($posts)) {
898                 foreach ($posts as $post) {
899                         if ($post->id_str > $lastid) {
900                                 $lastid = $post->id_str;
901                                 PConfig::set($uid, 'twitter', 'lastid', $lastid);
902                         }
903
904                         if ($first_time) {
905                                 continue;
906                         }
907
908                         if (!stristr($post->source, $application_name)) {
909                                 $_SESSION["authenticated"] = true;
910                                 $_SESSION["uid"] = $uid;
911
912                                 $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
913
914                                 logger('twitter: posting for user ' . $uid);
915
916                                 item_post($a);
917                         }
918                 }
919         }
920         PConfig::set($uid, 'twitter', 'lastid', $lastid);
921 }
922
923 function twitter_queue_hook(App $a, &$b)
924 {
925         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
926                 dbesc(NETWORK_TWITTER)
927         );
928         if (!DBM::is_result($qi)) {
929                 return;
930         }
931
932         foreach ($qi as $x) {
933                 if ($x['network'] !== NETWORK_TWITTER) {
934                         continue;
935                 }
936
937                 logger('twitter_queue: run');
938
939                 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
940                         WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
941                         intval($x['cid'])
942                 );
943                 if (!DBM::is_result($r)) {
944                         continue;
945                 }
946
947                 $user = $r[0];
948
949                 $ckey    = Config::get('twitter', 'consumerkey');
950                 $csecret = Config::get('twitter', 'consumersecret');
951                 $otoken  = PConfig::get($user['uid'], 'twitter', 'oauthtoken');
952                 $osecret = PConfig::get($user['uid'], 'twitter', 'oauthsecret');
953
954                 $success = false;
955
956                 if ($ckey && $csecret && $otoken && $osecret) {
957                         logger('twitter_queue: able to post');
958
959                         $z = unserialize($x['content']);
960
961                         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
962                         $result = $connection->post($z['url'], $z['post']);
963
964                         logger('twitter_queue: post result: ' . print_r($result, true), LOGGER_DEBUG);
965
966                         if ($result->errors) {
967                                 logger('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"');
968                         } else {
969                                 $success = true;
970                                 Queue::removeItem($x['id']);
971                         }
972                 } else {
973                         logger("twitter_queue: Error getting tokens for user " . $user['uid']);
974                 }
975
976                 if (!$success) {
977                         logger('twitter_queue: delayed');
978                         Queue::updateTime($x['id']);
979                 }
980         }
981 }
982
983 function twitter_fix_avatar($avatar)
984 {
985         $new_avatar = str_replace("_normal.", ".", $avatar);
986
987         $info = Image::getInfoFromURL($new_avatar);
988         if (!$info) {
989                 $new_avatar = $avatar;
990         }
991
992         return $new_avatar;
993 }
994
995 function twitter_fetch_contact($uid, $data, $create_user)
996 {
997         if ($data->id_str == "") {
998                 return -1;
999         }
1000
1001         $avatar = twitter_fix_avatar($data->profile_image_url_https);
1002         $url = "https://twitter.com/" . $data->screen_name;
1003         $addr = $data->screen_name . "@twitter.com";
1004
1005         GContact::update(["url" => $url, "network" => NETWORK_TWITTER,
1006                 "photo" => $avatar, "hide" => true,
1007                 "name" => $data->name, "nick" => $data->screen_name,
1008                 "location" => $data->location, "about" => $data->description,
1009                 "addr" => $addr, "generation" => 2]);
1010
1011         $fields = ['url' => $url, 'network' => NETWORK_TWITTER,
1012                 'name' => $data->name, 'nick' => $data->screen_name, 'addr' => $addr,
1013                 'location' => $data->location, 'about' => $data->description];
1014
1015         $cid = Contact::getIdForURL($url, 0, true, $fields);
1016         if (!empty($cid)) {
1017                 DBA::update('contact', $fields, ['id' => $cid]);
1018                 Contact::updateAvatar($avatar, 0, $cid);
1019         }
1020
1021         $contact = DBA::selectFirst('contact', [], ['uid' => $uid, 'alias' => "twitter::" . $data->id_str]);
1022         if (!DBM::is_result($contact) && !$create_user) {
1023                 return 0;
1024         }
1025
1026         if (!DBM::is_result($contact)) {
1027                 // create contact record
1028                 $fields['uid'] = $uid;
1029                 $fields['created'] = DateTimeFormat::utcNow();
1030                 $fields['nurl'] = normalise_link($url);
1031                 $fields['alias'] = 'twitter::' . $data->id_str;
1032                 $fields['poll'] = 'twitter::' . $data->id_str;
1033                 $fields['rel'] = CONTACT_IS_FRIEND;
1034                 $fields['priority'] = 1;
1035                 $fields['writable'] = true;
1036                 $fields['blocked'] = false;
1037                 $fields['readonly'] = false;
1038                 $fields['pending'] = false;
1039
1040                 if (!DBA::insert('contact', $fields)) {
1041                         return false;
1042                 }
1043
1044                 $contact_id = DBA::lastInsertId();
1045
1046                 Group::addMember(User::getDefaultGroup($uid), $contact_id);
1047
1048                 Contact::updateAvatar($avatar, $uid, $contact_id);
1049         } else {
1050                 if ($contact["readonly"] || $contact["blocked"]) {
1051                         logger("twitter_fetch_contact: Contact '" . $contact["nick"] . "' is blocked or readonly.", LOGGER_DEBUG);
1052                         return -1;
1053                 }
1054
1055                 $contact_id = $contact['id'];
1056
1057                 // update profile photos once every twelve hours as we have no notification of when they change.
1058                 $update_photo = ($contact['avatar-date'] < DateTimeFormat::utc('now -12 hours'));
1059
1060                 // check that we have all the photos, this has been known to fail on occasion
1061                 if (empty($contact['photo']) || empty($contact['thumb']) || empty($contact['micro']) || $update_photo) {
1062                         logger("twitter_fetch_contact: Updating contact " . $data->screen_name, LOGGER_DEBUG);
1063
1064                         Contact::updateAvatar($avatar, $uid, $contact['id']);
1065
1066                         $fields['name-date'] = DateTimeFormat::utcNow();
1067                         $fields['uri-date'] = DateTimeFormat::utcNow();
1068
1069                         DBA::update('contact', $fields, ['id' => $contact['id']]);
1070                 }
1071         }
1072
1073         return $contact_id;
1074 }
1075
1076 function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
1077 {
1078         $ckey = Config::get('twitter', 'consumerkey');
1079         $csecret = Config::get('twitter', 'consumersecret');
1080         $otoken = PConfig::get($uid, 'twitter', 'oauthtoken');
1081         $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1082
1083         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1084                 intval($uid));
1085
1086         if (DBM::is_result($r)) {
1087                 $self = $r[0];
1088         } else {
1089                 return;
1090         }
1091
1092         $parameters = [];
1093
1094         if ($screen_name != "") {
1095                 $parameters["screen_name"] = $screen_name;
1096         }
1097
1098         if ($user_id != "") {
1099                 $parameters["user_id"] = $user_id;
1100         }
1101
1102         // Fetching user data
1103         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1104         try {
1105                 $user = $connection->get('users/show', $parameters);
1106         } catch (TwitterOAuthException $e) {
1107                 logger('twitter_fetchuser: Error fetching user ' . $uid . ': ' . $e->getMessage());
1108                 return;
1109         }
1110
1111         if (!is_object($user)) {
1112                 return;
1113         }
1114
1115         $contact_id = twitter_fetch_contact($uid, $user, true);
1116
1117         return $contact_id;
1118 }
1119
1120 function twitter_expand_entities(App $a, $body, $item, $picture)
1121 {
1122         $plain = $body;
1123
1124         $tags_arr = [];
1125
1126         foreach ($item->entities->hashtags AS $hashtag) {
1127                 $url = "#[url=" . $a->get_baseurl() . "/search?tag=" . rawurlencode($hashtag->text) . "]" . $hashtag->text . "[/url]";
1128                 $tags_arr["#" . $hashtag->text] = $url;
1129                 $body = str_replace("#" . $hashtag->text, $url, $body);
1130         }
1131
1132         foreach ($item->entities->user_mentions AS $mention) {
1133                 $url = "@[url=https://twitter.com/" . rawurlencode($mention->screen_name) . "]" . $mention->screen_name . "[/url]";
1134                 $tags_arr["@" . $mention->screen_name] = $url;
1135                 $body = str_replace("@" . $mention->screen_name, $url, $body);
1136         }
1137
1138         if (isset($item->entities->urls)) {
1139                 $type = "";
1140                 $footerurl = "";
1141                 $footerlink = "";
1142                 $footer = "";
1143
1144                 foreach ($item->entities->urls as $url) {
1145                         $plain = str_replace($url->url, '', $plain);
1146
1147                         if ($url->url && $url->expanded_url && $url->display_url) {
1148                                 $expanded_url = Network::finalUrl($url->expanded_url);
1149
1150                                 $oembed_data = OEmbed::fetchURL($expanded_url);
1151
1152                                 if (empty($oembed_data) || empty($oembed_data->type)) {
1153                                         continue;
1154                                 }
1155
1156                                 // Quickfix: Workaround for URL with "[" and "]" in it
1157                                 if (strpos($expanded_url, "[") || strpos($expanded_url, "]")) {
1158                                         $expanded_url = $url->url;
1159                                 }
1160
1161                                 if ($type == "") {
1162                                         $type = $oembed_data->type;
1163                                 }
1164
1165                                 if ($oembed_data->type == "video") {
1166                                         //$body = str_replace($url->url,
1167                                         //              "[video]".$expanded_url."[/video]", $body);
1168                                         //$dontincludemedia = true;
1169                                         $type = $oembed_data->type;
1170                                         $footerurl = $expanded_url;
1171                                         $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]";
1172
1173                                         $body = str_replace($url->url, $footerlink, $body);
1174                                         //} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) {
1175                                 } elseif (($oembed_data->type == "photo") && isset($oembed_data->url)) {
1176                                         $body = str_replace($url->url, "[url=" . $expanded_url . "][img]" . $oembed_data->url . "[/img][/url]", $body);
1177                                         //$dontincludemedia = true;
1178                                 } elseif ($oembed_data->type != "link") {
1179                                         $body = str_replace($url->url, "[url=" . $expanded_url . "]" . $expanded_url . "[/url]", $body);
1180                                 } else {
1181                                         $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4);
1182
1183                                         $tempfile = tempnam(get_temppath(), "cache");
1184                                         file_put_contents($tempfile, $img_str);
1185                                         $mime = image_type_to_mime_type(exif_imagetype($tempfile));
1186                                         unlink($tempfile);
1187
1188                                         if (substr($mime, 0, 6) == "image/") {
1189                                                 $type = "photo";
1190                                                 $body = str_replace($url->url, "[img]" . $expanded_url . "[/img]", $body);
1191                                                 //$dontincludemedia = true;
1192                                         } else {
1193                                                 $type = $oembed_data->type;
1194                                                 $footerurl = $expanded_url;
1195                                                 $footerlink = "[url=" . $expanded_url . "]" . $expanded_url . "[/url]";
1196
1197                                                 $body = str_replace($url->url, $footerlink, $body);
1198                                         }
1199                                 }
1200                         }
1201                 }
1202
1203                 if ($footerurl != "") {
1204                         $footer = add_page_info($footerurl, false, $picture);
1205                 }
1206
1207                 if (($footerlink != "") && (trim($footer) != "")) {
1208                         $removedlink = trim(str_replace($footerlink, "", $body));
1209
1210                         if (($removedlink == "") || strstr($body, $removedlink)) {
1211                                 $body = $removedlink;
1212                         }
1213
1214                         $body .= $footer;
1215                 }
1216
1217                 if (($footer == "") && ($picture != "")) {
1218                         $body .= "\n\n[img]" . $picture . "[/img]\n";
1219                 } elseif (($footer == "") && ($picture == "")) {
1220                         $body = add_page_info_to_body($body);
1221                 }
1222         }
1223
1224         // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
1225         $tags = get_tags($body);
1226
1227         if (count($tags)) {
1228                 foreach ($tags as $tag) {
1229                         if (strstr(trim($tag), " ")) {
1230                                 continue;
1231                         }
1232
1233                         if (strpos($tag, '#') === 0) {
1234                                 if (strpos($tag, '[url=')) {
1235                                         continue;
1236                                 }
1237
1238                                 // don't link tags that are already embedded in links
1239                                 if (preg_match('/\[(.*?)' . preg_quote($tag, '/') . '(.*?)\]/', $body)) {
1240                                         continue;
1241                                 }
1242                                 if (preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag, '/') . '(.*?)\)/', $body)) {
1243                                         continue;
1244                                 }
1245
1246                                 $basetag = str_replace('_', ' ', substr($tag, 1));
1247                                 $url = '#[url=' . $a->get_baseurl() . '/search?tag=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1248                                 $body = str_replace($tag, $url, $body);
1249                                 $tags_arr["#" . $basetag] = $url;
1250                         } elseif (strpos($tag, '@') === 0) {
1251                                 if (strpos($tag, '[url=')) {
1252                                         continue;
1253                                 }
1254
1255                                 $basetag = substr($tag, 1);
1256                                 $url = '@[url=https://twitter.com/' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
1257                                 $body = str_replace($tag, $url, $body);
1258                                 $tags_arr["@" . $basetag] = $url;
1259                         }
1260                 }
1261         }
1262
1263         $tags = implode($tags_arr, ",");
1264
1265         return ["body" => $body, "tags" => $tags, "plain" => $plain];
1266 }
1267
1268 /**
1269  * @brief Fetch media entities and add media links to the body
1270  *
1271  * @param object $post Twitter object with the post
1272  * @param array $postarray Array of the item that is about to be posted
1273  *
1274  * @return $picture string Image URL or empty string
1275  */
1276 function twitter_media_entities($post, &$postarray)
1277 {
1278         // There are no media entities? So we quit.
1279         if (empty($post->extended_entities->media)) {
1280                 return "";
1281         }
1282
1283         // When the post links to an external page, we only take one picture.
1284         // We only do this when there is exactly one media.
1285         if ((count($post->entities->urls) > 0) && (count($post->extended_entities->media) == 1)) {
1286                 $picture = "";
1287                 foreach ($post->extended_entities->media AS $medium) {
1288                         if (isset($medium->media_url_https)) {
1289                                 $picture = $medium->media_url_https;
1290                                 $postarray['body'] = str_replace($medium->url, "", $postarray['body']);
1291                         }
1292                 }
1293                 return $picture;
1294         }
1295
1296         // This is a pure media post, first search for all media urls
1297         $media = [];
1298         foreach ($post->extended_entities->media AS $medium) {
1299                 if (!isset($media[$medium->url])) {
1300                         $media[$medium->url] = '';
1301                 }
1302                 switch ($medium->type) {
1303                         case 'photo':
1304                                 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . "[/img]";
1305                                 $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1306                                 break;
1307                         case 'video':
1308                         case 'animated_gif':
1309                                 $media[$medium->url] .= "\n[img]" . $medium->media_url_https . "[/img]";
1310                                 $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
1311                                 if (is_array($medium->video_info->variants)) {
1312                                         $bitrate = 0;
1313                                         // We take the video with the highest bitrate
1314                                         foreach ($medium->video_info->variants AS $variant) {
1315                                                 if (($variant->content_type == "video/mp4") && ($variant->bitrate >= $bitrate)) {
1316                                                         $media[$medium->url] = "\n[video]" . $variant->url . "[/video]";
1317                                                         $bitrate = $variant->bitrate;
1318                                                 }
1319                                         }
1320                                 }
1321                                 break;
1322                         // The following code will only be activated for test reasons
1323                         //default:
1324                         //      $postarray['body'] .= print_r($medium, true);
1325                 }
1326         }
1327
1328         // Now we replace the media urls.
1329         foreach ($media AS $key => $value) {
1330                 $postarray['body'] = str_replace($key, "\n" . $value . "\n", $postarray['body']);
1331         }
1332         return "";
1333 }
1334
1335 function twitter_createpost(App $a, $uid, $post, $self, $create_user, $only_existing_contact, $noquote)
1336 {
1337         $postarray = [];
1338         $postarray['network'] = NETWORK_TWITTER;
1339         $postarray['uid'] = $uid;
1340         $postarray['wall'] = 0;
1341         $postarray['uri'] = "twitter::" . $post->id_str;
1342         // $postarray['object'] = json_encode($post); // Activate for debugging
1343
1344         // Don't import our own comments
1345         if (DBA::exists('item', ['extid' => $postarray['uri'], 'uid' => $uid])) {
1346                 logger("Item with extid " . $postarray['uri'] . " found.", LOGGER_DEBUG);
1347                 return [];
1348         }
1349
1350         $contactid = 0;
1351
1352         if ($post->in_reply_to_status_id_str != "") {
1353                 $parent = "twitter::" . $post->in_reply_to_status_id_str;
1354
1355                 $fields = ['uri', 'parent-uri', 'parent'];
1356                 $parent_item = Item::selectFirst($fields, ['uri' => $parent, 'uid' => $uid]);
1357                 if (!DBM::is_result($parent_item)) {
1358                         $parent_item = Item::selectFirst($fields, ['extid' => $parent, 'uid' => $uid]);
1359                 }
1360
1361                 if (DBM::is_result($parent_item)) {
1362                         $postarray['thr-parent'] = $parent_item['uri'];
1363                         $postarray['parent-uri'] = $parent_item['parent-uri'];
1364                         $postarray['parent'] = $parent_item['parent'];
1365                         $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1366                 } else {
1367                         $postarray['thr-parent'] = $postarray['uri'];
1368                         $postarray['parent-uri'] = $postarray['uri'];
1369                         $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1370                 }
1371
1372                 // Is it me?
1373                 $own_id = PConfig::get($uid, 'twitter', 'own_id');
1374
1375                 if ($post->user->id_str == $own_id) {
1376                         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1377                                 intval($uid));
1378
1379                         if (DBM::is_result($r)) {
1380                                 $contactid = $r[0]["id"];
1381
1382                                 $postarray['owner-name']   = $r[0]["name"];
1383                                 $postarray['owner-link']   = $r[0]["url"];
1384                                 $postarray['owner-avatar'] = $r[0]["photo"];
1385                         } else {
1386                                 logger("No self contact for user " . $uid, LOGGER_DEBUG);
1387                                 return [];
1388                         }
1389                 }
1390                 // Don't create accounts of people who just comment something
1391                 $create_user = false;
1392         } else {
1393                 $postarray['parent-uri'] = $postarray['uri'];
1394                 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1395         }
1396
1397         if ($contactid == 0) {
1398                 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1399
1400                 $postarray['owner-name'] = $post->user->name;
1401                 $postarray['owner-link'] = "https://twitter.com/" . $post->user->screen_name;
1402                 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1403         }
1404
1405         if (($contactid == 0) && !$only_existing_contact) {
1406                 $contactid = $self['id'];
1407         } elseif ($contactid <= 0) {
1408                 logger("Contact ID is zero or less than zero.", LOGGER_DEBUG);
1409                 return [];
1410         }
1411
1412         $postarray['contact-id'] = $contactid;
1413
1414         $postarray['verb'] = ACTIVITY_POST;
1415         $postarray['author-name'] = $postarray['owner-name'];
1416         $postarray['author-link'] = $postarray['owner-link'];
1417         $postarray['author-avatar'] = $postarray['owner-avatar'];
1418         $postarray['plink'] = "https://twitter.com/" . $post->user->screen_name . "/status/" . $post->id_str;
1419         $postarray['app'] = strip_tags($post->source);
1420
1421         if ($post->user->protected) {
1422                 $postarray['private'] = 1;
1423                 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1424         } else {
1425                 $postarray['private'] = 0;
1426                 $postarray['allow_cid'] = '';
1427         }
1428
1429         if (is_string($post->full_text)) {
1430                 $postarray['body'] = $post->full_text;
1431         } else {
1432                 $postarray['body'] = $post->text;
1433         }
1434
1435         // When the post contains links then use the correct object type
1436         if (count($post->entities->urls) > 0) {
1437                 $postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
1438         }
1439
1440         // Search for media links
1441         $picture = twitter_media_entities($post, $postarray);
1442
1443         $converted = twitter_expand_entities($a, $postarray['body'], $post, $picture);
1444         $postarray['body'] = $converted["body"];
1445         $postarray['tag'] = $converted["tags"];
1446         $postarray['created'] = DateTimeFormat::utc($post->created_at);
1447         $postarray['edited'] = DateTimeFormat::utc($post->created_at);
1448
1449         $statustext = $converted["plain"];
1450
1451         if (!empty($post->place->name)) {
1452                 $postarray["location"] = $post->place->name;
1453         }
1454         if (!empty($post->place->full_name)) {
1455                 $postarray["location"] = $post->place->full_name;
1456         }
1457         if (!empty($post->geo->coordinates)) {
1458                 $postarray["coord"] = $post->geo->coordinates[0] . " " . $post->geo->coordinates[1];
1459         }
1460         if (!empty($post->coordinates->coordinates)) {
1461                 $postarray["coord"] = $post->coordinates->coordinates[1] . " " . $post->coordinates->coordinates[0];
1462         }
1463         if (!empty($post->retweeted_status)) {
1464                 $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
1465
1466                 //$retweet['object'] = $postarray['object']; // Activate for debugging
1467                 $retweet['private'] = $postarray['private'];
1468                 $retweet['allow_cid'] = $postarray['allow_cid'];
1469                 $retweet['contact-id'] = $postarray['contact-id'];
1470                 $retweet['owner-name'] = $postarray['owner-name'];
1471                 $retweet['owner-link'] = $postarray['owner-link'];
1472                 $retweet['owner-avatar'] = $postarray['owner-avatar'];
1473
1474                 $postarray = $retweet;
1475         }
1476
1477         if (!empty($post->quoted_status) && !$noquote) {
1478                 $quoted = twitter_createpost($a, $uid, $post->quoted_status, $self, false, false, true);
1479
1480                 $postarray['body'] = $statustext;
1481
1482                 $postarray['body'] .= "\n" . share_header(
1483                         $quoted['author-name'],
1484                         $quoted['author-link'],
1485                         $quoted['author-avatar'],
1486                         "",
1487                         $quoted['created'],
1488                         $quoted['plink']
1489                 );
1490
1491                 $postarray['body'] .= $quoted['body'] . '[/share]';
1492         }
1493
1494         return $postarray;
1495 }
1496
1497 function twitter_fetchparentposts(App $a, $uid, $post, TwitterOAuth $connection, $self, $own_id)
1498 {
1499         logger("twitter_fetchparentposts: Fetching for user " . $uid . " and post " . $post->id_str, LOGGER_DEBUG);
1500
1501         $posts = [];
1502
1503         while ($post->in_reply_to_status_id_str != "") {
1504                 $parameters = ["trim_user" => false, "tweet_mode" => "extended", "id" => $post->in_reply_to_status_id_str];
1505
1506                 try {
1507                         $post = $connection->get('statuses/show', $parameters);
1508                 } catch (TwitterOAuthException $e) {
1509                         logger('twitter_fetchparentposts: Error fetching for user ' . $uid . ' and post ' . $post->id_str . ': ' . $e->getMessage());
1510                         break;
1511                 }
1512
1513                 if (empty($post)) {
1514                         logger("twitter_fetchparentposts: Can't fetch post " . $parameters->id, LOGGER_DEBUG);
1515                         break;
1516                 }
1517
1518                 if (DBA::exists('item', ['uri' => 'twitter::' . $post->id_str, 'uid' => $uid])) {
1519                         break;
1520                 }
1521
1522                 $posts[] = $post;
1523         }
1524
1525         logger("twitter_fetchparentposts: Fetching " . count($posts) . " parents", LOGGER_DEBUG);
1526
1527         $posts = array_reverse($posts);
1528
1529         if (!empty($posts)) {
1530                 foreach ($posts as $post) {
1531                         $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1532
1533                         if (empty($postarray['body'])) {
1534                                 continue;
1535                         }
1536
1537                         $item = Item::insert($postarray);
1538
1539                         $postarray["id"] = $item;
1540
1541                         logger('twitter_fetchparentpost: User ' . $self["nick"] . ' posted parent timeline item ' . $item);
1542                 }
1543         }
1544 }
1545
1546 function twitter_fetchhometimeline(App $a, $uid)
1547 {
1548         $ckey    = Config::get('twitter', 'consumerkey');
1549         $csecret = Config::get('twitter', 'consumersecret');
1550         $otoken  = PConfig::get($uid, 'twitter', 'oauthtoken');
1551         $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1552         $create_user = PConfig::get($uid, 'twitter', 'create_user');
1553         $mirror_posts = PConfig::get($uid, 'twitter', 'mirror_posts');
1554
1555         logger("twitter_fetchhometimeline: Fetching for user " . $uid, LOGGER_DEBUG);
1556
1557         $application_name = Config::get('twitter', 'application_name');
1558
1559         if ($application_name == "") {
1560                 $application_name = $a->get_hostname();
1561         }
1562
1563         require_once 'include/items.php';
1564
1565         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1566
1567         try {
1568                 $own_contact = twitter_fetch_own_contact($a, $uid);
1569         } catch (TwitterOAuthException $e) {
1570                 logger('twitter_fetchhometimeline: Error fetching own contact for user ' . $uid . ': ' . $e->getMessage());
1571                 return;
1572         }
1573
1574         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1575                 intval($own_contact),
1576                 intval($uid));
1577
1578         if (DBM::is_result($r)) {
1579                 $own_id = $r[0]["nick"];
1580         } else {
1581                 logger("twitter_fetchhometimeline: Own twitter contact not found for user " . $uid, LOGGER_DEBUG);
1582                 return;
1583         }
1584
1585         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1586                 intval($uid));
1587
1588         if (DBM::is_result($r)) {
1589                 $self = $r[0];
1590         } else {
1591                 logger("twitter_fetchhometimeline: Own contact not found for user " . $uid, LOGGER_DEBUG);
1592                 return;
1593         }
1594
1595         $u = q("SELECT * FROM user WHERE uid = %d LIMIT 1",
1596                 intval($uid));
1597         if (!DBM::is_result($u)) {
1598                 logger("twitter_fetchhometimeline: Own user not found for user " . $uid, LOGGER_DEBUG);
1599                 return;
1600         }
1601
1602         $parameters = ["exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended"];
1603         //$parameters["count"] = 200;
1604         // Fetching timeline
1605         $lastid = PConfig::get($uid, 'twitter', 'lasthometimelineid');
1606
1607         $first_time = ($lastid == "");
1608
1609         if ($lastid != "") {
1610                 $parameters["since_id"] = $lastid;
1611         }
1612
1613         try {
1614                 $items = $connection->get('statuses/home_timeline', $parameters);
1615         } catch (TwitterOAuthException $e) {
1616                 logger('twitter_fetchhometimeline: Error fetching home timeline: ' . $e->getMessage());
1617                 return;
1618         }
1619
1620         if (!is_array($items)) {
1621                 logger("twitter_fetchhometimeline: Error fetching home timeline: " . print_r($items, true), LOGGER_DEBUG);
1622                 return;
1623         }
1624
1625         $posts = array_reverse($items);
1626
1627         logger("twitter_fetchhometimeline: Fetching timeline for user " . $uid . " " . sizeof($posts) . " items", LOGGER_DEBUG);
1628
1629         if (count($posts)) {
1630                 foreach ($posts as $post) {
1631                         if ($post->id_str > $lastid) {
1632                                 $lastid = $post->id_str;
1633                                 PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1634                         }
1635
1636                         if ($first_time) {
1637                                 continue;
1638                         }
1639
1640                         if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1641                                 logger("twitter_fetchhometimeline: Skip previously sended post", LOGGER_DEBUG);
1642                                 continue;
1643                         }
1644
1645                         if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1646                                 logger("twitter_fetchhometimeline: Skip post that will be mirrored", LOGGER_DEBUG);
1647                                 continue;
1648                         }
1649
1650                         if ($post->in_reply_to_status_id_str != "") {
1651                                 twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id);
1652                         }
1653
1654                         $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true, false);
1655
1656                         if (empty($postarray['body']) || trim($postarray['body']) == "") {
1657                                 continue;
1658                         }
1659
1660                         $notify = false;
1661
1662                         if ($postarray['uri'] == $postarray['parent-uri']) {
1663                                 $contact = DBA::selectFirst('contact', [], ['id' => $postarray['contact-id'], 'self' => false]);
1664                                 if (DBM::is_result($contact)) {
1665                                         $notify = Item::isRemoteSelf($contact, $postarray);
1666                                 }
1667                         }
1668
1669                         $item = Item::insert($postarray, false, $notify);
1670                         $postarray["id"] = $item;
1671
1672                         logger('twitter_fetchhometimeline: User ' . $self["nick"] . ' posted home timeline item ' . $item);
1673                 }
1674         }
1675         PConfig::set($uid, 'twitter', 'lasthometimelineid', $lastid);
1676
1677         // Fetching mentions
1678         $lastid = PConfig::get($uid, 'twitter', 'lastmentionid');
1679
1680         $first_time = ($lastid == "");
1681
1682         if ($lastid != "") {
1683                 $parameters["since_id"] = $lastid;
1684         }
1685
1686         try {
1687                 $items = $connection->get('statuses/mentions_timeline', $parameters);
1688         } catch (TwitterOAuthException $e) {
1689                 logger('twitter_fetchhometimeline: Error fetching mentions: ' . $e->getMessage());
1690                 return;
1691         }
1692
1693         if (!is_array($items)) {
1694                 logger("twitter_fetchhometimeline: Error fetching mentions: " . print_r($items, true), LOGGER_DEBUG);
1695                 return;
1696         }
1697
1698         $posts = array_reverse($items);
1699
1700         logger("twitter_fetchhometimeline: Fetching mentions for user " . $uid . " " . sizeof($posts) . " items", LOGGER_DEBUG);
1701
1702         if (count($posts)) {
1703                 foreach ($posts as $post) {
1704                         if ($post->id_str > $lastid) {
1705                                 $lastid = $post->id_str;
1706                         }
1707
1708                         if ($first_time) {
1709                                 continue;
1710                         }
1711
1712                         if ($post->in_reply_to_status_id_str != "") {
1713                                 twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id);
1714                         }
1715
1716                         $postarray = twitter_createpost($a, $uid, $post, $self, false, false, false);
1717
1718                         if (trim($postarray['body']) == "") {
1719                                 continue;
1720                         }
1721
1722                         $item = Item::insert($postarray);
1723
1724                         logger('twitter_fetchhometimeline: User ' . $self["nick"] . ' posted mention timeline item ' . $item);
1725                 }
1726         }
1727
1728         PConfig::set($uid, 'twitter', 'lastmentionid', $lastid);
1729 }
1730
1731 function twitter_fetch_own_contact(App $a, $uid)
1732 {
1733         $ckey    = Config::get('twitter', 'consumerkey');
1734         $csecret = Config::get('twitter', 'consumersecret');
1735         $otoken  = PConfig::get($uid, 'twitter', 'oauthtoken');
1736         $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1737
1738         $own_id = PConfig::get($uid, 'twitter', 'own_id');
1739
1740         $contact_id = 0;
1741
1742         if ($own_id == "") {
1743                 $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1744
1745                 // Fetching user data
1746                 // get() may throw TwitterOAuthException, but we will catch it later
1747                 $user = $connection->get('account/verify_credentials');
1748
1749                 PConfig::set($uid, 'twitter', 'own_id', $user->id_str);
1750
1751                 $contact_id = twitter_fetch_contact($uid, $user, true);
1752         } else {
1753                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1754                         intval($uid),
1755                         dbesc("twitter::" . $own_id));
1756                 if (DBM::is_result($r)) {
1757                         $contact_id = $r[0]["id"];
1758                 } else {
1759                         PConfig::delete($uid, 'twitter', 'own_id');
1760                 }
1761         }
1762
1763         return $contact_id;
1764 }
1765
1766 function twitter_is_retweet(App $a, $uid, $body)
1767 {
1768         $body = trim($body);
1769
1770         // Skip if it isn't a pure repeated messages
1771         // Does it start with a share?
1772         if (strpos($body, "[share") > 0) {
1773                 return false;
1774         }
1775
1776         // Does it end with a share?
1777         if (strlen($body) > (strrpos($body, "[/share]") + 8)) {
1778                 return false;
1779         }
1780
1781         $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism", "$1", $body);
1782         // Skip if there is no shared message in there
1783         if ($body == $attributes) {
1784                 return false;
1785         }
1786
1787         $link = "";
1788         preg_match("/link='(.*?)'/ism", $attributes, $matches);
1789         if (!empty($matches[1])) {
1790                 $link = $matches[1];
1791         }
1792
1793         preg_match('/link="(.*?)"/ism', $attributes, $matches);
1794         if (!empty($matches[1])) {
1795                 $link = $matches[1];
1796         }
1797
1798         $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
1799         if ($id == $link) {
1800                 return false;
1801         }
1802
1803         logger('twitter_is_retweet: Retweeting id ' . $id . ' for user ' . $uid, LOGGER_DEBUG);
1804
1805         $ckey    = Config::get('twitter', 'consumerkey');
1806         $csecret = Config::get('twitter', 'consumersecret');
1807         $otoken  = PConfig::get($uid, 'twitter', 'oauthtoken');
1808         $osecret = PConfig::get($uid, 'twitter', 'oauthsecret');
1809
1810         $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
1811         $result = $connection->post('statuses/retweet/' . $id);
1812
1813         logger('twitter_is_retweet: result ' . print_r($result, true), LOGGER_DEBUG);
1814
1815         return !isset($result->errors);
1816 }