]> git.mxchange.org Git - friendica-addons.git/blob - twitter/twitter.php
Merge pull request #363 from rabuzarus/1308-notifyall
[friendica-addons.git] / twitter / twitter.php
1 <?php
2 /**
3  * Name: Twitter Connector
4  * Description: Bidirectional (posting, relaying and reading) connector for Twitter.
5  * Version: 1.0.4
6  * Author: Tobias Diekershoff <https://f.diekershoff.de/profile/tobias>
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  *
9  * Copyright (c) 2011-2013 Tobias Diekershoff, Michael Vogel
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *    * Redistributions of source code must retain the above copyright notice,
15  *     this list of conditions and the following disclaimer.
16  *    * Redistributions in binary form must reproduce the above
17  *    * copyright notice, this list of conditions and the following disclaimer in
18  *      the documentation and/or other materials provided with the distribution.
19  *    * Neither the name of the <organization> nor the names of its contributors
20  *      may be used to endorse or promote products derived from this software
21  *      without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35  
36 /*   Twitter Plugin 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 plugin 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 .htconfig.php or use the admin panel.
52  *
53  *     $a->config['twitter']['consumerkey'] = 'your consumer_key here';
54  *     $a->config['twitter']['consumersecret'] = 'your consumer_secret here';
55  *
56  *     To activate the plugin itself add it to the $a->config['system']['addon']
57  *     setting. After this, your user can configure their Twitter account settings
58  *     from "Settings -> Plugin Settings".
59  *
60  *     Requirements: PHP5, curl [Slinky library]
61  */
62
63 require_once('include/enotify.php');
64 require_once("include/socgraph.php");
65
66 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
67
68 function twitter_install() {
69         //  we need some hooks, for the configuration and for sending tweets
70         register_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
71         register_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
72         register_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
73         register_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
74         register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
75         register_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
76         register_hook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook');
77         register_hook('follow', 'addon/twitter/twitter.php', 'twitter_follow');
78         register_hook('expire', 'addon/twitter/twitter.php', 'twitter_expire');
79         register_hook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body');
80         register_hook('check_item_notification','addon/twitter/twitter.php', 'twitter_check_item_notification');
81         logger("installed twitter");
82 }
83
84
85 function twitter_uninstall() {
86         unregister_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings');
87         unregister_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
88         unregister_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
89         unregister_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
90         unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
91         unregister_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
92         unregister_hook('queue_predeliver', 'addon/twitter/twitter.php', 'twitter_queue_hook');
93         unregister_hook('follow', 'addon/twitter/twitter.php', 'twitter_follow');
94         unregister_hook('expire', 'addon/twitter/twitter.php', 'twitter_expire');
95         unregister_hook('prepare_body', 'addon/twitter/twitter.php', 'twitter_prepare_body');
96         unregister_hook('check_item_notification','addon/twitter/twitter.php', 'twitter_check_item_notification');
97
98         // old setting - remove only
99         unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
100         unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings');
101         unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
102
103 }
104
105 function twitter_check_item_notification($a, &$notification_data) {
106         $own_id = get_pconfig($notification_data["uid"], 'twitter', 'own_id');
107
108         $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
109                         intval($notification_data["uid"]),
110                         dbesc("twitter::".$own_id)
111                 );
112
113         if ($own_user)
114                 $notification_data["profiles"][] = $own_user[0]["url"];
115 }
116
117 function twitter_follow($a, &$contact) {
118
119         logger("twitter_follow: Check if contact is twitter contact. ".$contact["url"], LOGGER_DEBUG);
120
121         if (!strstr($contact["url"], "://twitter.com") AND !strstr($contact["url"], "@twitter.com"))
122                 return;
123
124         // contact seems to be a twitter contact, so continue
125         $nickname = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $contact["url"]);
126         $nickname = str_replace("@twitter.com", "", $nickname);
127
128         $uid = $a->user["uid"];
129
130         $ckey    = get_config('twitter', 'consumerkey');
131         $csecret = get_config('twitter', 'consumersecret');
132         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
133         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
134
135         require_once("addon/twitter/codebird.php");
136
137         $cb = \Codebird\Codebird::getInstance();
138         $cb->setConsumerKey($ckey, $csecret);
139         $cb->setToken($otoken, $osecret);
140
141         $parameters = array();
142         $parameters["screen_name"] = $nickname;
143
144         $user = $cb->friendships_create($parameters);
145
146         twitter_fetchuser($a, $uid, $nickname);
147
148         $r = q("SELECT name,nick,url,addr,batch,notify,poll,request,confirm,poco,photo,priority,network,alias,pubkey
149                 FROM `contact` WHERE `uid` = %d AND `nick` = '%s'",
150                                 intval($uid),
151                                 dbesc($nickname));
152         if (count($r))
153                 $contact["contact"] = $r[0];
154 }
155
156 function twitter_jot_nets(&$a,&$b) {
157         if(! local_user())
158                 return;
159
160         $tw_post = get_pconfig(local_user(),'twitter','post');
161         if(intval($tw_post) == 1) {
162                 $tw_defpost = get_pconfig(local_user(),'twitter','post_by_default');
163                 $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
164                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> ' 
165                         . t('Post to Twitter') . '</div>';
166         }
167 }
168
169 function twitter_settings_post ($a,$post) {
170         if(! local_user())
171                 return;
172         // don't check twitter settings if twitter submit button is not clicked
173         if (!x($_POST,'twitter-submit'))
174                 return;
175
176         if (isset($_POST['twitter-disconnect'])) {
177                 /***
178                  * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
179                  * from the user configuration
180                  */
181                 del_pconfig(local_user(), 'twitter', 'consumerkey');
182                 del_pconfig(local_user(), 'twitter', 'consumersecret');
183                 del_pconfig(local_user(), 'twitter', 'oauthtoken');
184                 del_pconfig(local_user(), 'twitter', 'oauthsecret');
185                 del_pconfig(local_user(), 'twitter', 'post');
186                 del_pconfig(local_user(), 'twitter', 'post_by_default');
187                 del_pconfig(local_user(), 'twitter', 'lastid');
188                 del_pconfig(local_user(), 'twitter', 'mirror_posts');
189                 del_pconfig(local_user(), 'twitter', 'import');
190                 del_pconfig(local_user(), 'twitter', 'create_user');
191                 del_pconfig(local_user(), 'twitter', 'own_id');
192         } else {
193         if (isset($_POST['twitter-pin'])) {
194                 //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
195                 logger('got a Twitter PIN');
196                 require_once('library/twitteroauth.php');
197                 $ckey    = get_config('twitter', 'consumerkey');
198                 $csecret = get_config('twitter', 'consumersecret');
199                 //  the token and secret for which the PIN was generated were hidden in the settings
200                 //  form as token and token2, we need a new connection to Twitter using these token
201                 //  and secret to request a Access Token with the PIN
202                 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
203                 $token   = $connection->getAccessToken( $_POST['twitter-pin'] );
204                 //  ok, now that we have the Access Token, save them in the user config
205                 set_pconfig(local_user(),'twitter', 'oauthtoken',  $token['oauth_token']);
206                 set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
207                 set_pconfig(local_user(),'twitter', 'post', 1);
208                 //  reload the Addon Settings page, if we don't do it see Bug #42
209                 goaway($a->get_baseurl().'/settings/connectors');
210         } else {
211                 //  if no PIN is supplied in the POST variables, the user has changed the setting
212                 //  to post a tweet for every new __public__ posting to the wall
213                 set_pconfig(local_user(),'twitter','post',intval($_POST['twitter-enable']));
214                 set_pconfig(local_user(),'twitter','post_by_default',intval($_POST['twitter-default']));
215                 set_pconfig(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
216                 set_pconfig(local_user(), 'twitter', 'import', intval($_POST['twitter-import']));
217                 set_pconfig(local_user(), 'twitter', 'create_user', intval($_POST['twitter-create_user']));
218
219                 if (!intval($_POST['twitter-mirror']))
220                         del_pconfig(local_user(),'twitter','lastid');
221
222                 info(t('Twitter settings updated.') . EOL);
223         }}
224 }
225 function twitter_settings(&$a,&$s) {
226         if(! local_user())
227                 return;
228         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
229         /***
230          * 1) Check that we have global consumer key & secret
231          * 2) If no OAuthtoken & stuff is present, generate button to get some
232          * 3) Checkbox for "Send public notices (140 chars only)
233          */
234         $ckey    = get_config('twitter', 'consumerkey' );
235         $csecret = get_config('twitter', 'consumersecret' );
236         $otoken  = get_pconfig(local_user(), 'twitter', 'oauthtoken'  );
237         $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
238         $enabled = get_pconfig(local_user(), 'twitter', 'post');
239         $checked = (($enabled) ? ' checked="checked" ' : '');
240         $defenabled = get_pconfig(local_user(),'twitter','post_by_default');
241         $defchecked = (($defenabled) ? ' checked="checked" ' : '');
242         $mirrorenabled = get_pconfig(local_user(),'twitter','mirror_posts');
243         $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
244         $importenabled = get_pconfig(local_user(),'twitter','import');
245         $importchecked = (($importenabled) ? ' checked="checked" ' : '');
246         $create_userenabled = get_pconfig(local_user(),'twitter','create_user');
247         $create_userchecked = (($create_userenabled) ? ' checked="checked" ' : '');
248
249         $css = (($enabled) ? '' : '-disabled');
250
251         $s .= '<span id="settings_twitter_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
252         $s .= '<img class="connector'.$css.'" src="images/twitter.png" /><h3 class="connector">'. t('Twitter Import/Export/Mirror').'</h3>';
253         $s .= '</span>';
254         $s .= '<div id="settings_twitter_expanded" class="settings-block" style="display: none;">';
255         $s .= '<span class="fakelink" onclick="openClose(\'settings_twitter_expanded\'); openClose(\'settings_twitter_inflated\');">';
256         $s .= '<img class="connector'.$css.'" src="images/twitter.png" /><h3 class="connector">'. t('Twitter Import/Export/Mirror').'</h3>';
257         $s .= '</span>';
258
259         if ( (!$ckey) && (!$csecret) ) {
260                 /***
261                  * no global consumer keys
262                  * display warning and skip personal config
263                  */
264                 $s .= '<p>'. t('No consumer key pair for Twitter found. Please contact your site administrator.') .'</p>';
265         } else {
266                 /***
267                  * ok we have a consumer key pair now look into the OAuth stuff
268                  */
269                 if ( (!$otoken) && (!$osecret) ) {
270                         /***
271                          * the user has not yet connected the account to twitter...
272                          * get a temporary OAuth key/secret pair and display a button with
273                          * which the user can request a PIN to connect the account to a
274                          * account at Twitter.
275                          */
276                         require_once('library/twitteroauth.php');
277                         $connection = new TwitterOAuth($ckey, $csecret);
278                         $request_token = $connection->getRequestToken();
279                         $token = $request_token['oauth_token'];
280                         /***
281                          *  make some nice form
282                          */
283                         $s .= '<p>'. t('At this Friendica instance the Twitter plugin was enabled but you have not yet connected your account to your Twitter account. To do so click the button below to get a PIN from Twitter which you have to copy into the input box below and submit the form. Only your <strong>public</strong> posts will be posted to Twitter.') .'</p>';
284                         $s .= '<a href="'.$connection->getAuthorizeURL($token).'" target="_twitter"><img src="addon/twitter/lighter.png" alt="'.t('Log in with Twitter').'"></a>';
285                         $s .= '<div id="twitter-pin-wrapper">';
286                         $s .= '<label id="twitter-pin-label" for="twitter-pin">'. t('Copy the PIN from Twitter here') .'</label>';
287                         $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
288                         $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="'.$token.'" />';
289                         $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="'.$request_token['oauth_token_secret'].'" />';
290             $s .= '</div><div class="clear"></div>';
291             $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
292                 } else {
293                         /***
294                          *  we have an OAuth key / secret pair for the user
295                          *  so let's give a chance to disable the postings to Twitter
296                          */
297                         require_once('library/twitteroauth.php');
298                         $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
299                         $details = $connection->get('account/verify_credentials');
300                         $s .= '<div id="twitter-info" ><img id="twitter-avatar" src="'.$details->profile_image_url.'" /><p id="twitter-info-block">'. t('Currently connected to: ') .'<a href="https://twitter.com/'.$details->screen_name.'" target="_twitter">'.$details->screen_name.'</a><br /><em>'.$details->description.'</em></p></div>';
301                         $s .= '<p>'. t('If enabled all your <strong>public</strong> postings can be posted to the associated Twitter account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.') .'</p>';
302                         if ($a->user['hidewall']) {
303                             $s .= '<p>'. t('<strong>Note</strong>: Due your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to Twitter will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') .'</p>';
304                         }
305                         $s .= '<div id="twitter-enable-wrapper">';
306                         $s .= '<label id="twitter-enable-label" for="twitter-checkbox">'. t('Allow posting to Twitter'). '</label>';
307                         $s .= '<input id="twitter-checkbox" type="checkbox" name="twitter-enable" value="1" ' . $checked . '/>';
308                         $s .= '<div class="clear"></div>';
309                         $s .= '<label id="twitter-default-label" for="twitter-default">'. t('Send public postings to Twitter by default') .'</label>';
310                         $s .= '<input id="twitter-default" type="checkbox" name="twitter-default" value="1" ' . $defchecked . '/>';
311                         $s .= '<div class="clear"></div>';
312
313                         $s .= '<label id="twitter-mirror-label" for="twitter-mirror">'.t('Mirror all posts from twitter that are no replies').'</label>';
314                         $s .= '<input id="twitter-mirror" type="checkbox" name="twitter-mirror" value="1" '. $mirrorchecked . '/>';
315                         $s .= '<div class="clear"></div>';
316                         $s .= '</div>';
317
318                         $s .= '<label id="twitter-import-label" for="twitter-import">'.t('Import the remote timeline').'</label>';
319                         $s .= '<input id="twitter-import" type="checkbox" name="twitter-import" value="1" '. $importchecked . '/>';
320                         $s .= '<div class="clear"></div>';
321
322                         $s .= '<label id="twitter-create_user-label" for="twitter-create_user">'.t('Automatically create contacts').'</label>';
323                         $s .= '<input id="twitter-create_user" type="checkbox" name="twitter-create_user" value="1" '. $create_userchecked . '/>';
324                         $s .= '<div class="clear"></div>';
325
326                         $s .= '<div id="twitter-disconnect-wrapper">';
327                         $s .= '<label id="twitter-disconnect-label" for="twitter-disconnect">'. t('Clear OAuth configuration') .'</label>';
328                         $s .= '<input id="twitter-disconnect" type="checkbox" name="twitter-disconnect" value="1" />';
329                         $s .= '</div><div class="clear"></div>';
330                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>'; 
331                 }
332         }
333         $s .= '</div><div class="clear"></div>';
334 }
335
336
337 function twitter_post_local(&$a,&$b) {
338
339         if($b['edit'])
340                 return;
341
342         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent']) ) {
343
344                 $twitter_post = intval(get_pconfig(local_user(),'twitter','post'));
345                 $twitter_enable = (($twitter_post && x($_REQUEST,'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
346
347                 // if API is used, default to the chosen settings
348                 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'twitter','post_by_default')))
349                         $twitter_enable = 1;
350
351         if(! $twitter_enable)
352                 return;
353
354         if(strlen($b['postopts']))
355                 $b['postopts'] .= ',';
356                 $b['postopts'] .= 'twitter';
357         }
358 }
359
360 function twitter_action($a, $uid, $pid, $action) {
361
362         $ckey    = get_config('twitter', 'consumerkey');
363         $csecret = get_config('twitter', 'consumersecret');
364         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
365         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
366
367         require_once("addon/twitter/codebird.php");
368
369         $cb = \Codebird\Codebird::getInstance();
370         $cb->setConsumerKey($ckey, $csecret);
371         $cb->setToken($otoken, $osecret);
372
373         $post = array('id' => $pid);
374
375         logger("twitter_action '".$action."' ID: ".$pid." data: " . print_r($post, true), LOGGER_DATA);
376
377         switch ($action) {
378                 case "delete":
379                         // To-Do: $result = $cb->statuses_destroy($post);
380                         break;
381                 case "like":
382                         $result = $cb->favorites_create($post);
383                         break;
384                 case "unlike":
385                         $result = $cb->favorites_destroy($post);
386                         break;
387         }
388         logger("twitter_action '".$action."' send, result: " . print_r($result, true), LOGGER_DEBUG);
389 }
390
391 function twitter_post_hook(&$a,&$b) {
392
393         /**
394          * Post to Twitter
395          */
396
397         require_once("include/network.php");
398
399         if (!get_pconfig($b["uid"],'twitter','import')) {
400                 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
401                         return;
402         }
403
404         if($b['parent'] != $b['id']) {
405                 logger("twitter_post_hook: parameter ".print_r($b, true), LOGGER_DATA);
406
407                 // Looking if its a reply to a twitter post
408                 if ((substr($b["parent-uri"], 0, 9) != "twitter::") AND (substr($b["extid"], 0, 9) != "twitter::") AND (substr($b["thr-parent"], 0, 9) != "twitter::")) {
409                         logger("twitter_post_hook: no twitter post ".$b["parent"]);
410                         return;
411                 }
412
413                 $r = q("SELECT * FROM item WHERE item.uri = '%s' AND item.uid = %d LIMIT 1",
414                         dbesc($b["thr-parent"]),
415                         intval($b["uid"]));
416
417                 if(!count($r)) {
418                         logger("twitter_post_hook: no parent found ".$b["thr-parent"]);
419                         return;
420                 } else {
421                         $iscomment = true;
422                         $orig_post = $r[0];
423                 }
424
425
426                 $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
427                 $nickname = "@[url=".$orig_post["author-link"]."]".$nicknameplain."[/url]";
428                 $nicknameplain = "@".$nicknameplain;
429
430                 logger("twitter_post_hook: comparing ".$nickname." and ".$nicknameplain." with ".$b["body"], LOGGER_DEBUG);
431                 if ((strpos($b["body"], $nickname) === false) AND (strpos($b["body"], $nicknameplain) === false))
432                         $b["body"] = $nickname." ".$b["body"];
433
434                 logger("twitter_post_hook: parent found ".print_r($orig_post, true), LOGGER_DATA);
435         } else {
436                 $iscomment = false;
437
438                 if($b['private'] OR !strstr($b['postopts'],'twitter'))
439                         return;
440         }
441
442         if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
443                 twitter_action($a, $b["uid"], substr($orig_post["uri"], 9), "delete");
444
445         if($b['verb'] == ACTIVITY_LIKE) {
446                 logger("twitter_post_hook: parameter 2 ".substr($b["thr-parent"], 9), LOGGER_DEBUG);
447                 if ($b['deleted'])
448                         twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "unlike");
449                 else
450                         twitter_action($a, $b["uid"], substr($b["thr-parent"], 9), "like");
451                 return;
452         }
453
454         if($b['deleted'] || ($b['created'] !== $b['edited']))
455                 return;
456
457         // if post comes from twitter don't send it back
458         if($b['extid'] == NETWORK_TWITTER)
459                 return;
460
461         if($b['app'] == "Twitter")
462                 return;
463
464         logger('twitter post invoked');
465
466
467         load_pconfig($b['uid'], 'twitter');
468
469         $ckey    = get_config('twitter', 'consumerkey');
470         $csecret = get_config('twitter', 'consumersecret');
471         $otoken  = get_pconfig($b['uid'], 'twitter', 'oauthtoken');
472         $osecret = get_pconfig($b['uid'], 'twitter', 'oauthsecret');
473
474         if($ckey && $csecret && $otoken && $osecret) {
475                 logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG);
476
477                 // If it's a repeated message from twitter then do a native retweet and exit
478                 if (twitter_is_retweet($a, $b['uid'], $b['body']))
479                         return;
480
481                 require_once('library/twitteroauth.php');
482                 require_once('include/bbcode.php');
483                 $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
484
485                 $max_char = 140;
486                 require_once("include/plaintext.php");
487                 $msgarr = plaintext($a, $b, $max_char, true, 8);
488                 $msg = $msgarr["text"];
489
490                 if (($msg == "") AND isset($msgarr["title"]))
491                         $msg = shortenmsg($msgarr["title"], $max_char - 50);
492
493                 $image = "";
494
495                 if (isset($msgarr["url"]) AND ($msgarr["type"] != "photo"))
496                         $msg .= "\n".$msgarr["url"];
497                 elseif (isset($msgarr["image"]) AND ($msgarr["type"] != "video"))
498                         $image = $msgarr["image"];
499
500                 // and now tweet it :-)
501                 if(strlen($msg) and ($image != "")) {
502                         $img_str = fetch_url($image);
503
504                         $tempfile = tempnam(get_temppath(), "cache");
505                         file_put_contents($tempfile, $img_str);
506
507                         // Twitter had changed something so that the old library doesn't work anymore
508                         // so we are using a new library for twitter
509                         // To-Do:
510                         // Switching completely to this library with all functions
511                         require_once("addon/twitter/codebird.php");
512
513                         $cb = \Codebird\Codebird::getInstance();
514                         $cb->setConsumerKey($ckey, $csecret);
515                         $cb->setToken($otoken, $osecret);
516
517                         $post = array('status' => $msg, 'media[]' => $tempfile);
518
519                         if ($iscomment)
520                                 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
521
522                         $result = $cb->statuses_updateWithMedia($post);
523                         unlink($tempfile);
524
525                         logger('twitter_post_with_media send, result: ' . print_r($result, true), LOGGER_DEBUG);
526
527                         if ($result->source)
528                                 set_config("twitter", "application_name", strip_tags($result->source));
529
530                         if ($result->errors OR $result->error) {
531                                 logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
532
533                                 // Workaround: Remove the picture link so that the post can be reposted without it
534                                 $msg .= " ".$image;
535                                 $image = "";
536                         } elseif ($iscomment) {
537                                 logger('twitter_post: Update extid '.$result->id_str." for post id ".$b['id']);
538                                 q("UPDATE `item` SET `extid` = '%s', `body` = '%s' WHERE `id` = %d",
539                                         dbesc("twitter::".$result->id_str),
540                                         dbesc($result->text),
541                                         intval($b['id'])
542                                 );
543                         }
544                 }
545
546                 if(strlen($msg) and ($image == "")) {
547                         $url = 'statuses/update';
548                         $post = array('status' => $msg);
549
550                         if ($iscomment)
551                                 $post["in_reply_to_status_id"] = substr($orig_post["uri"], 9);
552
553                         $result = $tweet->post($url, $post);
554                         logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
555
556                         if ($result->source)
557                                 set_config("twitter", "application_name", strip_tags($result->source));
558
559                         if ($result->errors) {
560                                 logger('Send to Twitter failed: "' . print_r($result->errors, true) . '"');
561
562                                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", intval($b['uid']));
563                                 if (count($r))
564                                         $a->contact = $r[0]["id"];
565
566                                 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $post));
567                                 require_once('include/queue_fn.php');
568                                 add_to_queue($a->contact,NETWORK_TWITTER,$s);
569                                 notice(t('Twitter post failed. Queued for retry.').EOL);
570                         } elseif ($iscomment) {
571                                 logger('twitter_post: Update extid '.$result->id_str." for post id ".$b['id']);
572                                 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
573                                         dbesc("twitter::".$result->id_str),
574                                         intval($b['id'])
575                                 );
576                                 //q("UPDATE `item` SET `extid` = '%s', `body` = '%s' WHERE `id` = %d",
577                                 //      dbesc("twitter::".$result->id_str),
578                                 //      dbesc($result->text),
579                                 //      intval($b['id'])
580                                 //);
581                         }
582                 }
583         }
584 }
585
586 function twitter_plugin_admin_post(&$a){
587         $consumerkey    =       ((x($_POST,'consumerkey'))              ? notags(trim($_POST['consumerkey']))   : '');
588         $consumersecret =       ((x($_POST,'consumersecret'))   ? notags(trim($_POST['consumersecret'])): '');
589         $applicationname = ((x($_POST, 'applicationname')) ? notags(trim($_POST['applicationname'])):'');
590         set_config('twitter','consumerkey',$consumerkey);
591         set_config('twitter','consumersecret',$consumersecret);
592         //set_config('twitter','application_name',$applicationname);
593         info( t('Settings updated.'). EOL );
594 }
595 function twitter_plugin_admin(&$a, &$o){
596         $t = get_markup_template( "admin.tpl", "addon/twitter/" );
597
598         $o = replace_macros($t, array(
599                 '$submit' => t('Save Settings'),
600                                                                 // name, label, value, help, [extra values]
601                 '$consumerkey' => array('consumerkey', t('Consumer key'),  get_config('twitter', 'consumerkey' ), ''),
602                 '$consumersecret' => array('consumersecret', t('Consumer secret'),  get_config('twitter', 'consumersecret' ), ''),
603                 //'$applicationname' => array('applicationname', t('Name of the Twitter Application'), get_config('twitter','application_name'),t('Set this to the exact name you gave the app on twitter.com/apps to avoid mirroring postings from ~friendica back to ~friendica'))
604         ));
605 }
606
607 function twitter_cron($a,$b) {
608         $last = get_config('twitter','last_poll');
609
610         $poll_interval = intval(get_config('twitter','poll_interval'));
611         if(! $poll_interval)
612                 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
613
614         if($last) {
615                 $next = $last + ($poll_interval * 60);
616                 if($next > time()) {
617                         logger('twitter: poll intervall not reached');
618                         return;
619                 }
620         }
621         logger('twitter: cron_start');
622
623         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1' ORDER BY RAND()");
624         if(count($r)) {
625                 foreach($r as $rr) {
626                         logger('twitter: fetching for user '.$rr['uid']);
627                         twitter_fetchtimeline($a, $rr['uid']);
628                 }
629         }
630
631         $abandon_days = intval(get_config('system','account_abandon_days'));
632         if ($abandon_days < 1)
633                 $abandon_days = 0;
634
635         $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400);
636
637         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
638         if(count($r)) {
639                 foreach($r as $rr) {
640                         if ($abandon_days != 0) {
641                                 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
642                                 if (!count($user)) {
643                                         logger('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
644                                         continue;
645                                 }
646                         }
647
648                         logger('twitter: importing timeline from user '.$rr['uid']);
649                         twitter_fetchhometimeline($a, $rr["uid"]);
650
651 /*
652                         // To-Do
653                         // check for new contacts once a day
654                         $last_contact_check = get_pconfig($rr['uid'],'pumpio','contact_check');
655                         if($last_contact_check)
656                                 $next_contact_check = $last_contact_check + 86400;
657                         else
658                                 $next_contact_check = 0;
659
660                         if($next_contact_check <= time()) {
661                                 pumpio_getallusers($a, $rr["uid"]);
662                                 set_pconfig($rr['uid'],'pumpio','contact_check',time());
663                         }
664 */
665
666                 }
667         }
668
669         logger('twitter: cron_end');
670
671         set_config('twitter','last_poll', time());
672 }
673
674 function twitter_expire($a,$b) {
675
676         $days = get_config('twitter', 'expire');
677
678         if ($days == 0)
679                 return;
680
681         $r = q("DELETE FROM `item` WHERE `deleted` AND `network` = '%s'", dbesc(NETWORK_TWITTER));
682
683         require_once("include/items.php");
684
685         logger('twitter_expire: expire_start');
686
687         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
688         if(count($r)) {
689                 foreach($r as $rr) {
690                         logger('twitter_expire: user '.$rr['uid']);
691                         item_expire($rr['uid'], $days, NETWORK_TWITTER, true);
692                 }
693         }
694
695         logger('twitter_expire: expire_end');
696 }
697
698 function twitter_prepare_body(&$a,&$b) {
699         if ($b["item"]["network"] != NETWORK_TWITTER)
700                 return;
701
702         if ($b["preview"]) {
703                 $max_char = 140;
704                 require_once("include/plaintext.php");
705                 $item = $b["item"];
706                 $item["plink"] = $a->get_baseurl()."/display/".$a->user["nickname"]."/".$item["parent"];
707
708                 $r = q("SELECT `author-link` FROM item WHERE item.uri = '%s' AND item.uid = %d LIMIT 1",
709                         dbesc($item["thr-parent"]),
710                         intval(local_user()));
711
712                 if(count($r)) {
713                         $orig_post = $r[0];
714
715                         $nicknameplain = preg_replace("=https?://twitter.com/(.*)=ism", "$1", $orig_post["author-link"]);
716                         $nickname = "@[url=".$orig_post["author-link"]."]".$nicknameplain."[/url]";
717                         $nicknameplain = "@".$nicknameplain;
718
719                         if ((strpos($item["body"], $nickname) === false) AND (strpos($item["body"], $nicknameplain) === false))
720                                 $item["body"] = $nickname." ".$item["body"];
721                 }
722
723
724                 $msgarr = plaintext($a, $item, $max_char, true, 8);
725                 $msg = $msgarr["text"];
726
727                 if (isset($msgarr["url"]) AND ($msgarr["type"] != "photo"))
728                         $msg .= " ".$msgarr["url"];
729
730                 if (isset($msgarr["image"]))
731                         $msg .= " ".$msgarr["image"];
732
733                 $b['html'] = nl2br(htmlspecialchars($msg));
734         }
735 }
736
737 function twitter_fetchtimeline($a, $uid) {
738         $ckey    = get_config('twitter', 'consumerkey');
739         $csecret = get_config('twitter', 'consumersecret');
740         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
741         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
742         $lastid  = get_pconfig($uid, 'twitter', 'lastid');
743
744         $application_name  = get_config('twitter', 'application_name');
745
746         if ($application_name == "")
747                 $application_name = $a->get_hostname();
748
749         $has_picture = false;
750
751         require_once('mod/item.php');
752         require_once('include/items.php');
753         require_once('mod/share.php');
754
755         require_once('library/twitteroauth.php');
756         $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
757
758         $parameters = array("exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true);
759
760         $first_time = ($lastid == "");
761
762         if ($lastid <> "")
763                 $parameters["since_id"] = $lastid;
764
765         $items = $connection->get('statuses/user_timeline', $parameters);
766
767         if (!is_array($items))
768                 return;
769
770         $posts = array_reverse($items);
771
772         if (count($posts)) {
773             foreach ($posts as $post) {
774                 if ($post->id_str > $lastid) {
775                         $lastid = $post->id_str;
776                         set_pconfig($uid, 'twitter', 'lastid', $lastid);
777                 }
778
779                 if ($first_time)
780                         continue;
781
782                 if (!stristr($post->source, $application_name)) {
783                         $_SESSION["authenticated"] = true;
784                         $_SESSION["uid"] = $uid;
785
786                         unset($_REQUEST);
787                         $_REQUEST["type"] = "wall";
788                         $_REQUEST["api_source"] = true;
789                         $_REQUEST["profile_uid"] = $uid;
790                         //$_REQUEST["source"] = "Twitter";
791                         $_REQUEST["source"] = $post->source;
792                         $_REQUEST["extid"] = NETWORK_TWITTER;
793
794                         //$_REQUEST["date"] = $post->created_at;
795
796                         $_REQUEST["title"] = "";
797
798                         if (is_object($post->retweeted_status)) {
799
800                                 $_REQUEST['body'] = $post->retweeted_status->text;
801
802                                 $picture = "";
803
804                                 // media
805                                 if (is_array($post->retweeted_status->entities->media)) {
806                                         foreach($post->retweeted_status->entities->media AS $media) {
807                                                 switch($media->type) {
808                                                         case 'photo':
809                                                                 //$_REQUEST['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $_REQUEST['body']);
810                                                                 //$has_picture = true;
811                                                                 $_REQUEST['body'] = str_replace($media->url, "", $_REQUEST['body']);
812                                                                 $picture = $media->media_url_https;
813                                                                 break;
814                                                 }
815                                         }
816                                 }
817
818                                 $converted = twitter_expand_entities($a, $_REQUEST['body'], $post->retweeted_status, true, $picture);
819                                 $_REQUEST['body'] = $converted["body"];
820
821                                 if (function_exists("share_header"))
822                                         $_REQUEST['body'] = share_header($post->retweeted_status->user->name, "https://twitter.com/".$post->retweeted_status->user->screen_name,
823                                                                         $post->retweeted_status->user->profile_image_url_https, "",
824                                                                         datetime_convert('UTC','UTC',$post->retweeted_status->created_at),
825                                                                         "https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str).
826                                                                 $_REQUEST['body'];
827                                 else
828                                         $_REQUEST['body'] = "[share author='".$post->retweeted_status->user->name.
829                                                 "' profile='https://twitter.com/".$post->retweeted_status->user->screen_name.
830                                                 "' avatar='".$post->retweeted_status->user->profile_image_url_https.
831                                                 "' posted='".datetime_convert('UTC','UTC',$post->retweeted_status->created_at).
832                                                 "' link='https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str."']".
833                                                 $_REQUEST['body'];
834
835                                 $_REQUEST['body'] .= "[/share]";
836                         } else {
837                                 $_REQUEST["body"] = $post->text;
838
839                                 $picture = "";
840
841                                 if (is_array($post->entities->media)) {
842                                         foreach($post->entities->media AS $media) {
843                                                 switch($media->type) {
844                                                         case 'photo':
845                                                                 //$_REQUEST['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $_REQUEST['body']);
846                                                                 //$has_picture = true;
847                                                                 $_REQUEST['body'] = str_replace($media->url, "", $_REQUEST['body']);
848                                                                 $picture = $media->media_url_https;
849                                                                 break;
850                                                 }
851                                         }
852                                 }
853
854                                 $converted = twitter_expand_entities($a, $_REQUEST["body"], $post, true, $picture);
855                                 $_REQUEST['body'] = $converted["body"];
856                         }
857
858                         if (is_string($post->place->name))
859                                 $_REQUEST["location"] = $post->place->name;
860
861                         if (is_string($post->place->full_name))
862                                 $_REQUEST["location"] = $post->place->full_name;
863
864                         if (is_array($post->geo->coordinates))
865                                 $_REQUEST["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
866
867                         if (is_array($post->coordinates->coordinates))
868                                 $_REQUEST["coord"] = $post->coordinates->coordinates[1]." ".$post->coordinates->coordinates[0];
869
870                         //print_r($_REQUEST);
871                         logger('twitter: posting for user '.$uid);
872
873 //                      require_once('mod/item.php');
874
875                         item_post($a);
876                 }
877             }
878         }
879         set_pconfig($uid, 'twitter', 'lastid', $lastid);
880 }
881
882 function twitter_queue_hook(&$a,&$b) {
883
884         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
885                 dbesc(NETWORK_TWITTER)
886                 );
887         if(! count($qi))
888                 return;
889
890         require_once('include/queue_fn.php');
891
892         foreach($qi as $x) {
893                 if($x['network'] !== NETWORK_TWITTER)
894                         continue;
895
896                 logger('twitter_queue: run');
897
898                 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid` 
899                         WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
900                         intval($x['cid'])
901                 );
902                 if(! count($r))
903                         continue;
904
905                 $user = $r[0];
906
907                 $ckey    = get_config('twitter', 'consumerkey');
908                 $csecret = get_config('twitter', 'consumersecret');
909                 $otoken  = get_pconfig($user['uid'], 'twitter', 'oauthtoken');
910                 $osecret = get_pconfig($user['uid'], 'twitter', 'oauthsecret');
911
912                 $success = false;
913
914                 if ($ckey AND $csecret AND $otoken AND $osecret) {
915
916                         logger('twitter_queue: able to post');
917
918                         $z = unserialize($x['content']);
919
920                         require_once("addon/twitter/codebird.php");
921
922                         $cb = \Codebird\Codebird::getInstance();
923                         $cb->setConsumerKey($ckey, $csecret);
924                         $cb->setToken($otoken, $osecret);
925
926                         if ($z['url'] == "statuses/update")
927                                 $result = $cb->statuses_update($z['post']);
928
929                         logger('twitter_queue: post result: ' . print_r($result, true), LOGGER_DEBUG);
930
931                         if ($result->errors)
932                                 logger('twitter_queue: Send to Twitter failed: "' . print_r($result->errors, true) . '"');
933                         else {
934                                 $success = true;
935                                 remove_queue_item($x['id']);
936                         }
937                 } else
938                         logger("twitter_queue: Error getting tokens for user ".$user['uid']);
939
940                 if (!$success) {
941                         logger('twitter_queue: delayed');
942                         update_queue_time($x['id']);
943                 }
944         }
945 }
946
947 function twitter_fix_avatar($avatar) {
948         require_once("include/Photo.php");
949
950         $new_avatar = str_replace("_normal.", ".", $avatar);
951
952         $info = get_photo_info($new_avatar);
953         if (!$info)
954                 $new_avatar = $avatar;
955
956         return $new_avatar;
957 }
958
959 function twitter_fetch_contact($uid, $contact, $create_user) {
960
961         if ($contact->id_str == "")
962                 return(-1);
963
964         $avatar = twitter_fix_avatar($contact->profile_image_url_https);
965
966         if (function_exists("update_gcontact"))
967                 update_gcontact(array("url" => "https://twitter.com/".$contact->screen_name,
968                                 "network" => NETWORK_TWITTER, "photo" => $avatar,  "hide" => true,
969                                 "name" => $contact->name, "nick" => $contact->screen_name,
970                                 "location" => $contact->location, "about" => $contact->description,
971                                 "addr" => $contact->screen_name."@twitter.com", "generation" => 2));
972         else {
973                 // Old Code
974                 $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
975                                 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
976
977                 if (count($r) == 0)
978                         q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
979                                 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
980                                 dbesc($contact->name),
981                                 dbesc($contact->screen_name),
982                                 dbesc($avatar));
983                 else
984                         q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
985                                 dbesc($contact->name),
986                                 dbesc($contact->screen_name),
987                                 dbesc($avatar),
988                                 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
989
990                 if (DB_UPDATE_VERSION >= "1177")
991                         q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
992                                 dbesc($contact->location),
993                                 dbesc($contact->description),
994                                 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
995         }
996
997         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
998                 intval($uid), dbesc("twitter::".$contact->id_str));
999
1000         if(!count($r) AND !$create_user)
1001                 return(0);
1002
1003         if (count($r) AND ($r[0]["readonly"] OR $r[0]["blocked"])) {
1004                 logger("twitter_fetch_contact: Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG);
1005                 return(-1);
1006         }
1007
1008         if(!count($r)) {
1009                 // create contact record
1010                 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
1011                                         `name`, `nick`, `photo`, `network`, `rel`, `priority`,
1012                                         `writable`, `blocked`, `readonly`, `pending` )
1013                                         VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0) ",
1014                         intval($uid),
1015                         dbesc(datetime_convert()),
1016                         dbesc("https://twitter.com/".$contact->screen_name),
1017                         dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
1018                         dbesc($contact->screen_name."@twitter.com"),
1019                         dbesc("twitter::".$contact->id_str),
1020                         dbesc(''),
1021                         dbesc("twitter::".$contact->id_str),
1022                         dbesc($contact->name),
1023                         dbesc($contact->screen_name),
1024                         dbesc($avatar),
1025                         dbesc(NETWORK_TWITTER),
1026                         intval(CONTACT_IS_FRIEND),
1027                         intval(1),
1028                         intval(1)
1029                 );
1030
1031                 $r = q("SELECT * FROM `contact` WHERE `alias` = '%s' AND `uid` = %d LIMIT 1",
1032                         dbesc("twitter::".$contact->id_str),
1033                         intval($uid)
1034                         );
1035
1036                 if(! count($r))
1037                         return(false);
1038
1039                 $contact_id  = $r[0]['id'];
1040
1041                 $g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1",
1042                         intval($uid)
1043                 );
1044
1045                 if($g && intval($g[0]['def_gid'])) {
1046                         require_once('include/group.php');
1047                         group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
1048                 }
1049
1050                 require_once("Photo.php");
1051
1052                 $photos = import_profile_photo($avatar,$uid,$contact_id);
1053
1054                 q("UPDATE `contact` SET `photo` = '%s',
1055                                         `thumb` = '%s',
1056                                         `micro` = '%s',
1057                                         `name-date` = '%s',
1058                                         `uri-date` = '%s',
1059                                         `avatar-date` = '%s'
1060                                 WHERE `id` = %d",
1061                         dbesc($photos[0]),
1062                         dbesc($photos[1]),
1063                         dbesc($photos[2]),
1064                         dbesc(datetime_convert()),
1065                         dbesc(datetime_convert()),
1066                         dbesc(datetime_convert()),
1067                         intval($contact_id)
1068                 );
1069
1070                 if (DB_UPDATE_VERSION >= "1177")
1071                         q("UPDATE `contact` SET `location` = '%s',
1072                                                 `about` = '%s'
1073                                         WHERE `id` = %d",
1074                                 dbesc($contact->location),
1075                                 dbesc($contact->description),
1076                                 intval($contact_id)
1077                         );
1078
1079         } else {
1080                 // update profile photos once every two weeks as we have no notification of when they change.
1081
1082                 //$update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -2 days')) ? true : false);
1083                 $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
1084
1085                 // check that we have all the photos, this has been known to fail on occasion
1086
1087                 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
1088
1089                         logger("twitter_fetch_contact: Updating contact ".$contact->screen_name, LOGGER_DEBUG);
1090
1091                         require_once("Photo.php");
1092
1093                         $photos = import_profile_photo($avatar, $uid, $r[0]['id']);
1094
1095                         q("UPDATE `contact` SET `photo` = '%s',
1096                                                 `thumb` = '%s',
1097                                                 `micro` = '%s',
1098                                                 `name-date` = '%s',
1099                                                 `uri-date` = '%s',
1100                                                 `avatar-date` = '%s',
1101                                                 `url` = '%s',
1102                                                 `nurl` = '%s',
1103                                                 `addr` = '%s',
1104                                                 `name` = '%s',
1105                                                 `nick` = '%s'
1106                                         WHERE `id` = %d",
1107                                 dbesc($photos[0]),
1108                                 dbesc($photos[1]),
1109                                 dbesc($photos[2]),
1110                                 dbesc(datetime_convert()),
1111                                 dbesc(datetime_convert()),
1112                                 dbesc(datetime_convert()),
1113                                 dbesc("https://twitter.com/".$contact->screen_name),
1114                                 dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
1115                                 dbesc($contact->screen_name."@twitter.com"),
1116                                 dbesc($contact->name),
1117                                 dbesc($contact->screen_name),
1118                                 intval($r[0]['id'])
1119                         );
1120
1121                         if (DB_UPDATE_VERSION >= "1177")
1122                                 q("UPDATE `contact` SET `location` = '%s',
1123                                                         `about` = '%s'
1124                                                 WHERE `id` = %d",
1125                                         dbesc($contact->location),
1126                                         dbesc($contact->description),
1127                                         intval($r[0]['id'])
1128                                 );
1129                 }
1130         }
1131
1132         return($r[0]["id"]);
1133 }
1134
1135 function twitter_fetchuser($a, $uid, $screen_name = "", $user_id = "") {
1136         $ckey    = get_config('twitter', 'consumerkey');
1137         $csecret = get_config('twitter', 'consumersecret');
1138         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
1139         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1140
1141         require_once("addon/twitter/codebird.php");
1142
1143         $cb = \Codebird\Codebird::getInstance();
1144         $cb->setConsumerKey($ckey, $csecret);
1145         $cb->setToken($otoken, $osecret);
1146
1147         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1148                 intval($uid));
1149
1150         if(count($r)) {
1151                 $self = $r[0];
1152         } else
1153                 return;
1154
1155         $parameters = array();
1156
1157         if ($screen_name != "")
1158                 $parameters["screen_name"] = $screen_name;
1159
1160         if ($user_id != "")
1161                 $parameters["user_id"] = $user_id;
1162
1163         // Fetching user data
1164         $user = $cb->users_show($parameters);
1165
1166         if (!is_object($user))
1167                 return;
1168
1169         $contact_id = twitter_fetch_contact($uid, $user, true);
1170
1171         return $contact_id;
1172 }
1173
1174 function twitter_expand_entities($a, $body, $item, $no_tags = false, $picture) {
1175         require_once("include/oembed.php");
1176         require_once("include/network.php");
1177
1178         $tags = "";
1179
1180         if (isset($item->entities->urls)) {
1181                 $type = "";
1182                 $footerurl = "";
1183                 $footerlink = "";
1184                 $footer = "";
1185
1186                 foreach ($item->entities->urls AS $url) {
1187                         if ($url->url AND $url->expanded_url AND $url->display_url) {
1188
1189                                 $expanded_url = original_url($url->expanded_url);
1190
1191                                 $oembed_data = oembed_fetch_url($expanded_url);
1192
1193                                 // Quickfix: Workaround for URL with "[" and "]" in it
1194                                 if (strpos($expanded_url, "[") OR strpos($expanded_url, "]"))
1195                                         $expanded_url = $url->url;
1196
1197                                 if ($type == "")
1198                                         $type = $oembed_data->type;
1199
1200                                 if ($oembed_data->type == "video") {
1201                                         //$body = str_replace($url->url,
1202                                         //              "[video]".$expanded_url."[/video]", $body);
1203                                         //$dontincludemedia = true;
1204                                         $type = $oembed_data->type;
1205                                         $footerurl = $expanded_url;
1206                                         $footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
1207
1208                                         $body = str_replace($url->url, $footerlink, $body);
1209                                 //} elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) {
1210                                 } elseif (($oembed_data->type == "photo") AND isset($oembed_data->url)) {
1211                                         $body = str_replace($url->url,
1212                                                         "[url=".$expanded_url."][img]".$oembed_data->url."[/img][/url]",
1213                                                         $body);
1214                                         //$dontincludemedia = true;
1215                                 } elseif ($oembed_data->type != "link")
1216                                         $body = str_replace($url->url,
1217                                                         "[url=".$expanded_url."]".$expanded_url."[/url]",
1218                                                         $body);
1219                                 else {
1220                                         $img_str = fetch_url($expanded_url, true, $redirects, 4);
1221
1222                                         $tempfile = tempnam(get_temppath(), "cache");
1223                                         file_put_contents($tempfile, $img_str);
1224                                         $mime = image_type_to_mime_type(exif_imagetype($tempfile));
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                                                 //$dontincludemedia = true;
1231                                         } else {
1232                                                 $type = $oembed_data->type;
1233                                                 $footerurl = $expanded_url;
1234                                                 $footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]";
1235
1236                                                 $body = str_replace($url->url, $footerlink, $body);
1237                                         }
1238                                 }
1239                         }
1240                 }
1241
1242                 if ($footerurl != "")
1243                         $footer = add_page_info($footerurl, false, $picture);
1244
1245                 if (($footerlink != "") AND (trim($footer) != "")) {
1246                         $removedlink = trim(str_replace($footerlink, "", $body));
1247
1248                         if (($removedlink == "") OR strstr($body, $removedlink))
1249                                 $body = $removedlink;
1250
1251                         $body .= $footer;
1252                 }
1253
1254                 if (($footer == "") AND ($picture != ""))
1255                         $body .= "\n\n[img]".$picture."[/img]\n";
1256                 elseif (($footer == "") AND ($picture == ""))
1257                         $body = add_page_info_to_body($body);
1258
1259                 if ($no_tags)
1260                         return(array("body" => $body, "tags" => ""));
1261
1262                 $tags_arr = array();
1263
1264                 foreach ($item->entities->hashtags AS $hashtag) {
1265                         $url = "#[url=".$a->get_baseurl()."/search?tag=".rawurlencode($hashtag->text)."]".$hashtag->text."[/url]";
1266                         $tags_arr["#".$hashtag->text] = $url;
1267                         $body = str_replace("#".$hashtag->text, $url, $body);
1268                 }
1269
1270                 foreach ($item->entities->user_mentions AS $mention) {
1271                         $url = "@[url=https://twitter.com/".rawurlencode($mention->screen_name)."]".$mention->screen_name."[/url]";
1272                         $tags_arr["@".$mention->screen_name] = $url;
1273                         $body = str_replace("@".$mention->screen_name, $url, $body);
1274                 }
1275
1276                 // it seems as if the entities aren't always covering all mentions. So the rest will be checked here
1277                 $tags = get_tags($body);
1278
1279                 if(count($tags)) {
1280                         foreach($tags as $tag) {
1281                                 if (strstr(trim($tag), " "))
1282                                         continue;
1283
1284                                 if(strpos($tag,'#') === 0) {
1285                                         if(strpos($tag,'[url='))
1286                                                 continue;
1287
1288                                         // don't link tags that are already embedded in links
1289
1290                                         if(preg_match('/\[(.*?)' . preg_quote($tag,'/') . '(.*?)\]/',$body))
1291                                                 continue;
1292                                         if(preg_match('/\[(.*?)\]\((.*?)' . preg_quote($tag,'/') . '(.*?)\)/',$body))
1293                                                 continue;
1294
1295                                         $basetag = str_replace('_',' ',substr($tag,1));
1296                                         $url = '#[url='.$a->get_baseurl().'/search?tag='.rawurlencode($basetag).']'.$basetag.'[/url]';
1297                                         $body = str_replace($tag,$url,$body);
1298                                         $tags_arr["#".$basetag] = $url;
1299                                         continue;
1300                                 } elseif(strpos($tag,'@') === 0) {
1301                                         if(strpos($tag,'[url='))
1302                                                 continue;
1303
1304                                         $basetag = substr($tag,1);
1305                                         $url = '@[url=https://twitter.com/'.rawurlencode($basetag).']'.$basetag.'[/url]';
1306                                         $body = str_replace($tag,$url,$body);
1307                                         $tags_arr["@".$basetag] = $url;
1308                                 }
1309                         }
1310                 }
1311
1312
1313                 $tags = implode($tags_arr, ",");
1314
1315         }
1316         return(array("body" => $body, "tags" => $tags));
1317 }
1318
1319 function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing_contact) {
1320
1321         $has_picture = false;
1322
1323         $postarray = array();
1324         $postarray['network'] = NETWORK_TWITTER;
1325         $postarray['gravity'] = 0;
1326         $postarray['uid'] = $uid;
1327         $postarray['wall'] = 0;
1328         $postarray['uri'] = "twitter::".$post->id_str;
1329
1330         $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1331                         dbesc($postarray['uri']),
1332                         intval($uid)
1333                 );
1334
1335         if (count($r))
1336                 return(array());
1337
1338         $contactid = 0;
1339
1340         if ($post->in_reply_to_status_id_str != "") {
1341
1342                 $parent = "twitter::".$post->in_reply_to_status_id_str;
1343
1344                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1345                                 dbesc($parent),
1346                                 intval($uid)
1347                         );
1348                 if (count($r)) {
1349                         $postarray['thr-parent'] = $r[0]["uri"];
1350                         $postarray['parent-uri'] = $r[0]["parent-uri"];
1351                         $postarray['parent'] = $r[0]["parent"];
1352                         $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1353                 } else {
1354                         $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1355                                         dbesc($parent),
1356                                         intval($uid)
1357                                 );
1358                         if (count($r)) {
1359                                 $postarray['thr-parent'] = $r[0]['uri'];
1360                                 $postarray['parent-uri'] = $r[0]['parent-uri'];
1361                                 $postarray['parent'] = $r[0]['parent'];
1362                                 $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
1363                         } else {
1364                                 $postarray['thr-parent'] = $postarray['uri'];
1365                                 $postarray['parent-uri'] = $postarray['uri'];
1366                                 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1367                         }
1368                 }
1369
1370                 // Is it me?
1371                 $own_id = get_pconfig($uid, 'twitter', 'own_id');
1372
1373                 if ($post->user->id_str == $own_id) {
1374                         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1375                                 intval($uid));
1376
1377                         if(count($r)) {
1378                                 $contactid = $r[0]["id"];
1379
1380                                 $postarray['owner-name'] =  $r[0]["name"];
1381                                 $postarray['owner-link'] = $r[0]["url"];
1382                                 $postarray['owner-avatar'] =  $r[0]["photo"];
1383                         } else
1384                                 return(array());
1385                 }
1386                 // Don't create accounts of people who just comment something
1387                 $create_user = false;
1388         } else {
1389                 $postarray['parent-uri'] = $postarray['uri'];
1390                 $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
1391         }
1392
1393         if ($contactid == 0) {
1394                 $contactid = twitter_fetch_contact($uid, $post->user, $create_user);
1395
1396                 $postarray['owner-name'] = $post->user->name;
1397                 $postarray['owner-link'] = "https://twitter.com/".$post->user->screen_name;
1398                 $postarray['owner-avatar'] = twitter_fix_avatar($post->user->profile_image_url_https);
1399         }
1400
1401         if(($contactid == 0) AND !$only_existing_contact)
1402                 $contactid = $self['id'];
1403         elseif ($contactid <= 0)
1404                 return(array());
1405
1406         $postarray['contact-id'] = $contactid;
1407
1408         $postarray['verb'] = ACTIVITY_POST;
1409         $postarray['author-name'] = $postarray['owner-name'];
1410         $postarray['author-link'] = $postarray['owner-link'];
1411         $postarray['author-avatar'] = $postarray['owner-avatar'];
1412         $postarray['plink'] = "https://twitter.com/".$post->user->screen_name."/status/".$post->id_str;
1413         $postarray['app'] = strip_tags($post->source);
1414
1415         if ($post->user->protected) {
1416                 $postarray['private'] = 1;
1417                 $postarray['allow_cid'] = '<' . $self['id'] . '>';
1418         }
1419
1420         $postarray['body'] = $post->text;
1421
1422         $picture = "";
1423
1424         // media
1425         if (is_array($post->entities->media)) {
1426                 foreach($post->entities->media AS $media) {
1427                         switch($media->type) {
1428                                 case 'photo':
1429                                         //$postarray['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $postarray['body']);
1430                                         //$has_picture = true;
1431                                         $postarray['body'] = str_replace($media->url, "", $postarray['body']);
1432                                         $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1433                                         $picture = $media->media_url_https;
1434                                         break;
1435                                 default:
1436                                         $postarray['body'] .= print_r($media, true);
1437                         }
1438                 }
1439         }
1440
1441         $converted = twitter_expand_entities($a, $postarray['body'], $post, false, $picture);
1442         $postarray['body'] = $converted["body"];
1443         $postarray['tag'] = $converted["tags"];
1444
1445         $postarray['created'] = datetime_convert('UTC','UTC',$post->created_at);
1446         $postarray['edited'] = datetime_convert('UTC','UTC',$post->created_at);
1447
1448         if (is_string($post->place->name))
1449                 $postarray["location"] = $post->place->name;
1450
1451         if (is_string($post->place->full_name))
1452                 $postarray["location"] = $post->place->full_name;
1453
1454         if (is_array($post->geo->coordinates))
1455                 $postarray["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
1456
1457         if (is_array($post->coordinates->coordinates))
1458                 $postarray["coord"] = $post->coordinates->coordinates[1]." ".$post->coordinates->coordinates[0];
1459
1460         if (is_object($post->retweeted_status)) {
1461
1462                 $postarray['body'] = $post->retweeted_status->text;
1463
1464                 $picture = "";
1465
1466                 // media
1467                 if (is_array($post->retweeted_status->entities->media)) {
1468                         foreach($post->retweeted_status->entities->media AS $media) {
1469                                 switch($media->type) {
1470                                         case 'photo':
1471                                                 //$postarray['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $postarray['body']);
1472                                                 //$has_picture = true;
1473                                                 $postarray['body'] = str_replace($media->url, "", $postarray['body']);
1474                                                 $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
1475                                                 $picture = $media->media_url_https;
1476                                                 break;
1477                                         default:
1478                                                 $postarray['body'] .= print_r($media, true);
1479                                 }
1480                         }
1481                 }
1482
1483                 $converted = twitter_expand_entities($a, $postarray['body'], $post->retweeted_status, false, $picture);
1484                 $postarray['body'] = $converted["body"];
1485                 $postarray['tag'] = $converted["tags"];
1486
1487                 twitter_fetch_contact($uid, $post->retweeted_status->user, false);
1488
1489                 // Deactivated at the moment, since there are problems with answers to retweets
1490                 if (false AND !intval(get_config('system','wall-to-wall_share'))) {
1491                         $postarray['body'] = "[share author='".$post->retweeted_status->user->name.
1492                                 "' profile='https://twitter.com/".$post->retweeted_status->user->screen_name.
1493                                 "' avatar='".$post->retweeted_status->user->profile_image_url_https.
1494                                 "' posted='".datetime_convert('UTC','UTC',$post->retweeted_status->created_at).
1495                                 "' link='https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str."']".
1496                                 $postarray['body'];
1497                         $postarray['body'] .= "[/share]";
1498                 } else {
1499                         // Let retweets look like wall-to-wall posts
1500                         $postarray['author-name'] = $post->retweeted_status->user->name;
1501                         $postarray['author-link'] = "https://twitter.com/".$post->retweeted_status->user->screen_name;
1502                         $postarray['author-avatar'] = twitter_fix_avatar($post->retweeted_status->user->profile_image_url_https);
1503                         //if (($post->retweeted_status->user->screen_name != "") AND ($post->retweeted_status->id_str != "")) {
1504                         //      $postarray['plink'] = "https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str;
1505                         //      $postarray['uri'] = "twitter::".$post->retweeted_status->id_str;
1506                         //}
1507                 }
1508
1509         }
1510         return($postarray);
1511 }
1512
1513 function twitter_checknotification($a, $uid, $own_id, $top_item, $postarray) {
1514
1515         // this whole function doesn't seem to work. Needs complete check
1516
1517         $user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` LIMIT 1",
1518                         intval($uid)
1519                 );
1520
1521         if(!count($user))
1522                 return;
1523
1524         // Is it me?
1525         if (link_compare($user[0]["url"], $postarray['author-link']))
1526                 return;
1527
1528         $own_user = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1529                         intval($uid),
1530                         dbesc("twitter::".$own_id)
1531                 );
1532
1533         if(!count($own_user))
1534                 return;
1535
1536         // Is it me from twitter?
1537         if (link_compare($own_user[0]["url"], $postarray['author-link']))
1538                 return;
1539
1540         $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
1541                         dbesc($postarray['parent-uri']),
1542                         intval($uid)
1543                         );
1544
1545         if(count($myconv)) {
1546
1547                 foreach($myconv as $conv) {
1548                         // now if we find a match, it means we're in this conversation
1549
1550                         if(!link_compare($conv['author-link'],$user[0]["url"]) AND !link_compare($conv['author-link'],$own_user[0]["url"]))
1551                                 continue;
1552
1553                         require_once('include/enotify.php');
1554
1555                         $conv_parent = $conv['parent'];
1556
1557                         notification(array(
1558                                 'type'         => NOTIFY_COMMENT,
1559                                 'notify_flags' => $user[0]['notify-flags'],
1560                                 'language'     => $user[0]['language'],
1561                                 'to_name'      => $user[0]['username'],
1562                                 'to_email'     => $user[0]['email'],
1563                                 'uid'          => $user[0]['uid'],
1564                                 'item'         => $postarray,
1565                                 'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($top_item)),
1566                                 'source_name'  => $postarray['author-name'],
1567                                 'source_link'  => $postarray['author-link'],
1568                                 'source_photo' => $postarray['author-avatar'],
1569                                 'verb'         => ACTIVITY_POST,
1570                                 'otype'        => 'item',
1571                                 'parent'       => $conv_parent,
1572                         ));
1573
1574                         // only send one notification
1575                         break;
1576                 }
1577         }
1578 }
1579
1580 function twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id) {
1581         logger("twitter_fetchparentposts: Fetching for user ".$uid." and post ".$post->id_str, LOGGER_DEBUG);
1582
1583         $posts = array();
1584
1585         while ($post->in_reply_to_status_id_str != "") {
1586                 $parameters = array("trim_user" => false, "id" => $post->in_reply_to_status_id_str);
1587
1588                 $post = $connection->get('statuses/show', $parameters);
1589
1590                 if (!count($post)) {
1591                         logger("twitter_fetchparentposts: Can't fetch post ".$parameters->id, LOGGER_DEBUG);
1592                         break;
1593                 }
1594
1595                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1596                                 dbesc("twitter::".$post->id_str),
1597                                 intval($uid)
1598                         );
1599
1600                 if (count($r))
1601                         break;
1602
1603                 $posts[] = $post;
1604         }
1605
1606         logger("twitter_fetchparentposts: Fetching ".count($posts)." parents", LOGGER_DEBUG);
1607
1608         $posts = array_reverse($posts);
1609
1610         if (count($posts)) {
1611                 foreach ($posts as $post) {
1612                         $postarray = twitter_createpost($a, $uid, $post, $self, false, false);
1613
1614                         if (trim($postarray['body']) == "")
1615                                 continue;
1616
1617                         $item = item_store($postarray);
1618                         $postarray["id"] = $item;
1619
1620                         logger('twitter_fetchparentpost: User '.$self["nick"].' posted parent timeline item '.$item);
1621
1622                         if ($item AND !function_exists("check_item_notification"))
1623                                 twitter_checknotification($a, $uid, $own_id, $item, $postarray);
1624                 }
1625         }
1626 }
1627
1628 function twitter_fetchhometimeline($a, $uid) {
1629         $ckey    = get_config('twitter', 'consumerkey');
1630         $csecret = get_config('twitter', 'consumersecret');
1631         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
1632         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1633         $create_user = get_pconfig($uid, 'twitter', 'create_user');
1634         $mirror_posts = get_pconfig($uid, 'twitter', 'mirror_posts');
1635
1636         logger("twitter_fetchhometimeline: Fetching for user ".$uid, LOGGER_DEBUG);
1637
1638         $application_name  = get_config('twitter', 'application_name');
1639
1640         if ($application_name == "")
1641                 $application_name = $a->get_hostname();
1642
1643         require_once('library/twitteroauth.php');
1644         require_once('include/items.php');
1645
1646         $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
1647
1648         $own_contact = twitter_fetch_own_contact($a, $uid);
1649
1650         $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
1651                 intval($own_contact),
1652                 intval($uid));
1653
1654         if(count($r)) {
1655                 $own_id = $r[0]["nick"];
1656         } else {
1657                 logger("twitter_fetchhometimeline: Own twitter contact not found for user ".$uid, LOGGER_DEBUG);
1658                 return;
1659         }
1660
1661         $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1662                 intval($uid));
1663
1664         if(count($r)) {
1665                 $self = $r[0];
1666         } else {
1667                 logger("twitter_fetchhometimeline: Own contact not found for user ".$uid, LOGGER_DEBUG);
1668                 return;
1669         }
1670
1671         $u = q("SELECT * FROM user WHERE uid = %d LIMIT 1",
1672                 intval($uid));
1673         if(!count($u)) {
1674                 logger("twitter_fetchhometimeline: Own user not found for user ".$uid, LOGGER_DEBUG);
1675                 return;
1676         }
1677
1678         $parameters = array("exclude_replies" => false, "trim_user" => false, "contributor_details" => true, "include_rts" => true);
1679         //$parameters["count"] = 200;
1680
1681
1682         // Fetching timeline
1683         $lastid  = get_pconfig($uid, 'twitter', 'lasthometimelineid');
1684
1685         $first_time = ($lastid == "");
1686
1687         if ($lastid <> "")
1688                 $parameters["since_id"] = $lastid;
1689
1690         $items = $connection->get('statuses/home_timeline', $parameters);
1691
1692         if (!is_array($items)) {
1693                 logger("twitter_fetchhometimeline: Error fetching home timeline: ".print_r($items, true), LOGGER_DEBUG);
1694                 return;
1695         }
1696
1697         $posts = array_reverse($items);
1698
1699         logger("twitter_fetchhometimeline: Fetching timeline for user ".$uid." ".sizeof($posts)." items", LOGGER_DEBUG);
1700
1701         if (count($posts)) {
1702                 foreach ($posts as $post) {
1703                         if ($post->id_str > $lastid) {
1704                                 $lastid = $post->id_str;
1705                                 set_pconfig($uid, 'twitter', 'lasthometimelineid', $lastid);
1706                         }
1707
1708                         if ($first_time)
1709                                 continue;
1710
1711                         if (stristr($post->source, $application_name) && $post->user->screen_name == $own_id) {
1712                                 logger("twitter_fetchhometimeline: Skip previously sended post", LOGGER_DEBUG);
1713                                 continue;
1714                         }
1715
1716                         if ($mirror_posts && $post->user->screen_name == $own_id && $post->in_reply_to_status_id_str == "") {
1717                                 logger("twitter_fetchhometimeline: Skip post that will be mirrored", LOGGER_DEBUG);
1718                                 continue;
1719                         }
1720
1721                         if ($post->in_reply_to_status_id_str != "")
1722                                 twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id);
1723
1724                         $postarray = twitter_createpost($a, $uid, $post, $self, $create_user, true);
1725
1726                         if (trim($postarray['body']) == "")
1727                                 continue;
1728
1729                         $item = item_store($postarray);
1730                         $postarray["id"] = $item;
1731
1732                         logger('twitter_fetchhometimeline: User '.$self["nick"].' posted home timeline item '.$item);
1733
1734                         if ($item AND !function_exists("check_item_notification"))
1735                                 twitter_checknotification($a, $uid, $own_id, $item, $postarray);
1736
1737                 }
1738         }
1739         set_pconfig($uid, 'twitter', 'lasthometimelineid', $lastid);
1740
1741         // Fetching mentions
1742         $lastid  = get_pconfig($uid, 'twitter', 'lastmentionid');
1743
1744         $first_time = ($lastid == "");
1745
1746         if ($lastid <> "")
1747                 $parameters["since_id"] = $lastid;
1748
1749         $items = $connection->get('statuses/mentions_timeline', $parameters);
1750
1751         if (!is_array($items)) {
1752                 logger("twitter_fetchhometimeline: Error fetching mentions: ".print_r($items, true), LOGGER_DEBUG);
1753                 return;
1754         }
1755
1756         $posts = array_reverse($items);
1757
1758         logger("twitter_fetchhometimeline: Fetching mentions for user ".$uid." ".sizeof($posts)." items", LOGGER_DEBUG);
1759
1760         if (count($posts)) {
1761                 foreach ($posts as $post) {
1762                         if ($post->id_str > $lastid)
1763                                 $lastid = $post->id_str;
1764
1765                         if ($first_time)
1766                                 continue;
1767
1768                         if ($post->in_reply_to_status_id_str != "")
1769                                 twitter_fetchparentposts($a, $uid, $post, $connection, $self, $own_id);
1770
1771                         $postarray = twitter_createpost($a, $uid, $post, $self, false, false);
1772
1773                         if (trim($postarray['body']) == "")
1774                                 continue;
1775
1776                         $item = item_store($postarray);
1777                         $postarray["id"] = $item;
1778
1779                         if ($item AND function_exists("check_item_notification"))
1780                                 check_item_notification($item, $uid, NOTIFY_TAGSELF);
1781
1782                         if (!isset($postarray["parent"]) OR ($postarray["parent"] == 0))
1783                                 $postarray["parent"] = $item;
1784
1785                         logger('twitter_fetchhometimeline: User '.$self["nick"].' posted mention timeline item '.$item);
1786
1787                         if ($item == 0) {
1788                                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1789                                         dbesc($postarray['uri']),
1790                                         intval($uid)
1791                                 );
1792                                 if (count($r)) {
1793                                         $item = $r[0]['id'];
1794                                         $parent_id = $r[0]['parent'];
1795                                 }
1796                         } else
1797                                 $parent_id = $postarray['parent'];
1798
1799                         if (($item != 0) AND !function_exists("check_item_notification")) {
1800                                 require_once('include/enotify.php');
1801                                 notification(array(
1802                                         'type'         => NOTIFY_TAGSELF,
1803                                         'notify_flags' => $u[0]['notify-flags'],
1804                                         'language'     => $u[0]['language'],
1805                                         'to_name'      => $u[0]['username'],
1806                                         'to_email'     => $u[0]['email'],
1807                                         'uid'          => $u[0]['uid'],
1808                                         'item'         => $postarray,
1809                                         'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item)),
1810                                         'source_name'  => $postarray['author-name'],
1811                                         'source_link'  => $postarray['author-link'],
1812                                         'source_photo' => $postarray['author-avatar'],
1813                                         'verb'         => ACTIVITY_TAG,
1814                                         'otype'        => 'item',
1815                                         'parent'       => $parent_id
1816                                 ));
1817                         }
1818                 }
1819         }
1820
1821         set_pconfig($uid, 'twitter', 'lastmentionid', $lastid);
1822 }
1823
1824 function twitter_fetch_own_contact($a, $uid) {
1825         $ckey    = get_config('twitter', 'consumerkey');
1826         $csecret = get_config('twitter', 'consumersecret');
1827         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
1828         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1829
1830         $own_id = get_pconfig($uid, 'twitter', 'own_id');
1831
1832         $contact_id = 0;
1833
1834         if ($own_id == "") {
1835                 require_once('library/twitteroauth.php');
1836
1837                 $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
1838
1839                 // Fetching user data
1840                 $user = $connection->get('account/verify_credentials');
1841
1842                 set_pconfig($uid, 'twitter', 'own_id', $user->id_str);
1843
1844                 $contact_id = twitter_fetch_contact($uid, $user, true);
1845
1846         } else {
1847                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
1848                         intval($uid), dbesc("twitter::".$own_id));
1849                 if(count($r))
1850                         $contact_id = $r[0]["id"];
1851                 else
1852                         del_pconfig($uid, 'twitter', 'own_id');
1853
1854         }
1855
1856         return($contact_id);
1857 }
1858
1859 function twitter_is_retweet($a, $uid, $body) {
1860         $body = trim($body);
1861
1862         // Skip if it isn't a pure repeated messages
1863         // Does it start with a share?
1864         if (strpos($body, "[share") > 0)
1865                 return(false);
1866
1867         // Does it end with a share?
1868         if (strlen($body) > (strrpos($body, "[/share]") + 8))
1869                 return(false);
1870
1871         $attributes = preg_replace("/\[share(.*?)\]\s?(.*?)\s?\[\/share\]\s?/ism","$1",$body);
1872         // Skip if there is no shared message in there
1873         if ($body == $attributes)
1874                 return(false);
1875
1876         $link = "";
1877         preg_match("/link='(.*?)'/ism", $attributes, $matches);
1878         if ($matches[1] != "")
1879                 $link = $matches[1];
1880
1881         preg_match('/link="(.*?)"/ism', $attributes, $matches);
1882         if ($matches[1] != "")
1883                 $link = $matches[1];
1884
1885         $id = preg_replace("=https?://twitter.com/(.*)/status/(.*)=ism", "$2", $link);
1886         if ($id == $link)
1887                 return(false);
1888
1889         logger('twitter_is_retweet: Retweeting id '.$id.' for user '.$uid, LOGGER_DEBUG);
1890
1891         $ckey    = get_config('twitter', 'consumerkey');
1892         $csecret = get_config('twitter', 'consumersecret');
1893         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
1894         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
1895
1896         require_once('library/twitteroauth.php');
1897         $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
1898
1899         $result = $connection->post('statuses/retweet/'.$id);
1900
1901         logger('twitter_is_retweet: result '.print_r($result, true), LOGGER_DEBUG);
1902
1903         return(!isset($result->errors));
1904 }
1905 ?>