]> git.mxchange.org Git - friendica-addons.git/blob - twitter/twitter.php
pumpio: Added README
[friendica-addons.git] / twitter / twitter.php
1 <?php
2 /**
3  * Name: Twitter Connector
4  * Description: Relay public postings to a connected Twitter account
5  * Version: 1.0.4
6  * Author: Tobias Diekershoff <http://diekershoff.homeunix.net/friendika/profile/tobias>
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9
10
11 /*   Twitter Plugin for Friendica
12  *
13  *   Author: Tobias Diekershoff
14  *           tobias.diekershoff@gmx.net
15  *
16  *   License:3-clause BSD license
17  *
18  *   Configuration:
19  *     To use this plugin you need a OAuth Consumer key pair (key & secret)
20  *     you can get it from Twitter at https://twitter.com/apps
21  *
22  *     Register your Friendica site as "Client" application with "Read & Write" access
23  *     we do not need "Twitter as login". When you've registered the app you get the
24  *     OAuth Consumer key and secret pair for your application/site.
25  *
26  *     Add this key pair to your global .htconfig.php or use the admin panel.
27  *
28  *     $a->config['twitter']['consumerkey'] = 'your consumer_key here';
29  *     $a->config['twitter']['consumersecret'] = 'your consumer_secret here';
30  *
31  *     To activate the plugin itself add it to the $a->config['system']['addon']
32  *     setting. After this, your user can configure their Twitter account settings
33  *     from "Settings -> Plugin Settings".
34  *
35  *     Requirements: PHP5, curl [Slinky library]
36  *
37  *     Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/Twitter_Plugin
38  */
39
40 define('TWITTER_DEFAULT_POLL_INTERVAL', 5); // given in minutes
41
42 function twitter_install() {
43         //  we need some hooks, for the configuration and for sending tweets
44         register_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
45         register_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
46         register_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
47         register_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
48         register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
49         register_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
50         logger("installed twitter");
51 }
52
53
54 function twitter_uninstall() {
55         unregister_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
56         unregister_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
57         unregister_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
58         unregister_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
59         unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
60         unregister_hook('cron', 'addon/twitter/twitter.php', 'twitter_cron');
61
62         // old setting - remove only
63         unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
64         unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
65         unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
66
67 }
68
69 function twitter_jot_nets(&$a,&$b) {
70         if(! local_user())
71                 return;
72
73         $tw_post = get_pconfig(local_user(),'twitter','post');
74         if(intval($tw_post) == 1) {
75                 $tw_defpost = get_pconfig(local_user(),'twitter','post_by_default');
76                 $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
77                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> ' 
78                         . t('Post to Twitter') . '</div>';
79         }
80 }
81
82 function twitter_settings_post ($a,$post) {
83         if(! local_user())
84                 return;
85         // don't check twitter settings if twitter submit button is not clicked 
86         if (!x($_POST,'twitter-submit')) return;
87         
88         if (isset($_POST['twitter-disconnect'])) {
89                 /***
90                  * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
91                  * from the user configuration
92                  */
93                 del_pconfig(local_user(), 'twitter', 'consumerkey');
94                 del_pconfig(local_user(), 'twitter', 'consumersecret');
95                 del_pconfig(local_user(), 'twitter', 'oauthtoken');
96                 del_pconfig(local_user(), 'twitter', 'oauthsecret');
97                 del_pconfig(local_user(), 'twitter', 'post');
98                 del_pconfig(local_user(), 'twitter', 'post_by_default');
99                 del_pconfig(local_user(), 'twitter', 'post_taglinks');
100                 del_pconfig(local_user(), 'twitter', 'lastid');
101                 del_pconfig(local_user(), 'twitter', 'mirror_posts');
102                 del_pconfig(local_user(), 'twitter', 'intelligent_shortening');
103         } else {
104         if (isset($_POST['twitter-pin'])) {
105                 //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
106                 logger('got a Twitter PIN');
107                 require_once('library/twitteroauth.php');
108                 $ckey    = get_config('twitter', 'consumerkey');
109                 $csecret = get_config('twitter', 'consumersecret');
110                 //  the token and secret for which the PIN was generated were hidden in the settings
111                 //  form as token and token2, we need a new connection to Twitter using these token
112                 //  and secret to request a Access Token with the PIN
113                 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
114                 $token   = $connection->getAccessToken( $_POST['twitter-pin'] );
115                 //  ok, now that we have the Access Token, save them in the user config
116                 set_pconfig(local_user(),'twitter', 'oauthtoken',  $token['oauth_token']);
117                 set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
118                 set_pconfig(local_user(),'twitter', 'post', 1);
119                 set_pconfig(local_user(),'twitter', 'post_taglinks', 1);
120                 //  reload the Addon Settings page, if we don't do it see Bug #42
121                 goaway($a->get_baseurl().'/settings/connectors');
122         } else {
123                 //  if no PIN is supplied in the POST variables, the user has changed the setting
124                 //  to post a tweet for every new __public__ posting to the wall
125                 set_pconfig(local_user(),'twitter','post',intval($_POST['twitter-enable']));
126                 set_pconfig(local_user(),'twitter','post_by_default',intval($_POST['twitter-default']));
127                 set_pconfig(local_user(),'twitter','post_taglinks',intval($_POST['twitter-sendtaglinks']));
128                 set_pconfig(local_user(), 'twitter', 'mirror_posts', intval($_POST['twitter-mirror']));
129                 set_pconfig(local_user(), 'twitter', 'intelligent_shortening', intval($_POST['twitter-shortening']));
130                 info( t('Twitter settings updated.') . EOL);
131         }}
132 }
133 function twitter_settings(&$a,&$s) {
134         if(! local_user())
135                 return;
136         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
137         /***
138          * 1) Check that we have global consumer key & secret
139          * 2) If no OAuthtoken & stuff is present, generate button to get some
140          * 3) Checkbox for "Send public notices (140 chars only)
141          */
142         $ckey    = get_config('twitter', 'consumerkey' );
143         $csecret = get_config('twitter', 'consumersecret' );
144         $otoken  = get_pconfig(local_user(), 'twitter', 'oauthtoken'  );
145         $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
146         $enabled = get_pconfig(local_user(), 'twitter', 'post');
147         $checked = (($enabled) ? ' checked="checked" ' : '');
148         $defenabled = get_pconfig(local_user(),'twitter','post_by_default');
149         $defchecked = (($defenabled) ? ' checked="checked" ' : '');
150         $linksenabled = get_pconfig(local_user(),'twitter','post_taglinks');
151         $linkschecked = (($linksenabled) ? ' checked="checked" ' : '');
152         $mirrorenabled = get_pconfig(local_user(),'twitter','mirror_posts');
153         $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
154         $shorteningenabled = get_pconfig(local_user(),'twitter','intelligent_shortening');
155         $shorteningchecked = (($shorteningenabled) ? ' checked="checked" ' : '');
156
157         $s .= '<div class="settings-block">';
158         $s .= '<h3>'. t('Twitter Posting Settings') .'</h3>';
159
160         if ( (!$ckey) && (!$csecret) ) {
161                 /***
162                  * no global consumer keys
163                  * display warning and skip personal config
164                  */
165                 $s .= '<p>'. t('No consumer key pair for Twitter found. Please contact your site administrator.') .'</p>';
166         } else {
167                 /***
168                  * ok we have a consumer key pair now look into the OAuth stuff
169                  */
170                 if ( (!$otoken) && (!$osecret) ) {
171                         /***
172                          * the user has not yet connected the account to twitter...
173                          * get a temporary OAuth key/secret pair and display a button with
174                          * which the user can request a PIN to connect the account to a
175                          * account at Twitter.
176                          */
177                         require_once('library/twitteroauth.php');
178                         $connection = new TwitterOAuth($ckey, $csecret);
179                         $request_token = $connection->getRequestToken();
180                         $token = $request_token['oauth_token'];
181                         /***
182                          *  make some nice form
183                          */
184                         $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>';
185                         $s .= '<a href="'.$connection->getAuthorizeURL($token).'" target="_twitter"><img src="addon/twitter/lighter.png" alt="'.t('Log in with Twitter').'"></a>';
186                         $s .= '<div id="twitter-pin-wrapper">';
187                         $s .= '<label id="twitter-pin-label" for="twitter-pin">'. t('Copy the PIN from Twitter here') .'</label>';
188                         $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
189                         $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="'.$token.'" />';
190                         $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="'.$request_token['oauth_token_secret'].'" />';
191             $s .= '</div><div class="clear"></div>';
192             $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
193                 } else {
194                         /***
195                          *  we have an OAuth key / secret pair for the user
196                          *  so let's give a chance to disable the postings to Twitter
197                          */
198                         require_once('library/twitteroauth.php');
199                         $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
200                         $details = $connection->get('account/verify_credentials');
201                         $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>';
202                         $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>';
203                         if ($a->user['hidewall']) {
204                             $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>';
205                         }
206                         $s .= '<div id="twitter-enable-wrapper">';
207                         $s .= '<label id="twitter-enable-label" for="twitter-checkbox">'. t('Allow posting to Twitter'). '</label>';
208                         $s .= '<input id="twitter-checkbox" type="checkbox" name="twitter-enable" value="1" ' . $checked . '/>';
209                         $s .= '<div class="clear"></div>';
210                         $s .= '<label id="twitter-default-label" for="twitter-default">'. t('Send public postings to Twitter by default') .'</label>';
211                         $s .= '<input id="twitter-default" type="checkbox" name="twitter-default" value="1" ' . $defchecked . '/>';
212                         $s .= '<div class="clear"></div>';
213
214                         $s .= '<label id="twitter-mirror-label" for="twitter-mirror">'.t('Mirror all posts from twitter that are no replies or retweets').'</label>';
215                         $s .= '<input id="twitter-mirror" type="checkbox" name="twitter-mirror" value="1" '. $mirrorchecked . '/>';
216                         $s .= '<div class="clear"></div>';
217
218                         $s .= '<label id="twitter-shortening-label" for="twitter-shortening">'.t('Shortening method that optimizes the tweet').'</label>';
219                         $s .= '<input id="twitter-shortening" type="checkbox" name="twitter-shortening" value="1" '. $shorteningchecked . '/>';
220                         $s .= '<div class="clear"></div>';
221
222                         $s .= '<label id="twitter-sendtaglinks-label" for="twitter-sendtaglinks">'.t('Send linked #-tags and @-names to Twitter').'</label>';
223                         $s .= '<input id="twitter-sendtaglinks" type="checkbox" name="twitter-sendtaglinks" value="1" '. $linkschecked . '/>';
224                         $s .= '</div><div class="clear"></div>';
225
226                         $s .= '<div id="twitter-disconnect-wrapper">';
227                         $s .= '<label id="twitter-disconnect-label" for="twitter-disconnect">'. t('Clear OAuth configuration') .'</label>';
228                         $s .= '<input id="twitter-disconnect" type="checkbox" name="twitter-disconnect" value="1" />';
229                         $s .= '</div><div class="clear"></div>';
230                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Submit') . '" /></div>'; 
231                 }
232         }
233         $s .= '</div><div class="clear"></div>';
234 }
235
236
237 function twitter_post_local(&$a,&$b) {
238
239         if($b['edit'])
240                 return;
241
242         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent']) ) {
243
244                 $twitter_post = intval(get_pconfig(local_user(),'twitter','post'));
245                 $twitter_enable = (($twitter_post && x($_REQUEST,'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
246
247                 // if API is used, default to the chosen settings
248                 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'twitter','post_by_default')))
249                         $twitter_enable = 1;
250
251         if(! $twitter_enable)
252             return;
253
254         if(strlen($b['postopts']))
255             $b['postopts'] .= ',';
256         $b['postopts'] .= 'twitter';
257         }
258 }
259
260 if (! function_exists('short_link')) {
261 function short_link ($url) {
262     require_once('library/slinky.php');
263     $slinky = new Slinky( $url );
264     $yourls_url = get_config('yourls','url1');
265     if ($yourls_url) {
266             $yourls_username = get_config('yourls','username1');
267             $yourls_password = get_config('yourls', 'password1');
268             $yourls_ssl = get_config('yourls', 'ssl1');
269             $yourls = new Slinky_YourLS();
270             $yourls->set( 'username', $yourls_username );
271             $yourls->set( 'password', $yourls_password );
272             $yourls->set( 'ssl', $yourls_ssl );
273             $yourls->set( 'yourls-url', $yourls_url );
274             $slinky->set_cascade( array( $yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
275     }
276     else {
277             // setup a cascade of shortening services
278             // try to get a short link from these services
279             // in the order ur1.ca, trim, id.gd, tinyurl
280             $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
281     }
282     return $slinky->short();
283 } };
284
285 function twitter_shortenmsg($b) {
286         require_once("include/bbcode.php");
287         require_once("include/html2plain.php");
288
289         $max_char = 140;
290
291         // Looking for the first image
292         $image = '';
293         if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
294                 $image = $matches[3];
295
296         if ($image == '')
297                 if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
298                         $image = $matches[1];
299
300         $multipleimages = (strpos($b['body'], "[img") != strrpos($b['body'], "[img"));
301
302         // When saved into the database the content is sent through htmlspecialchars
303         // That means that we have to decode all image-urls
304         $image = htmlspecialchars_decode($image);
305
306         $body = $b["body"];
307         if ($b["title"] != "")
308                 $body = $b["title"]."\n\n".$body;
309
310         if (strpos($body, "[bookmark") !== false) {
311                 // splitting the text in two parts:
312                 // before and after the bookmark
313                 $pos = strpos($body, "[bookmark");
314                 $body1 = substr($body, 0, $pos);
315                 $body2 = substr($body, $pos);
316
317                 // Removing all quotes after the bookmark
318                 // they are mostly only the content after the bookmark.
319                 $body2 = preg_replace("/\[quote\=([^\]]*)\](.*?)\[\/quote\]/ism",'',$body2);
320                 $body2 = preg_replace("/\[quote\](.*?)\[\/quote\]/ism",'',$body2);
321                 $body = $body1.$body2;
322         }
323
324         // Add some newlines so that the message could be cut better
325         $body = str_replace(array("[quote", "[bookmark", "[/bookmark]", "[/quote]"),
326                         array("\n[quote", "\n[bookmark", "[/bookmark]\n", "[/quote]\n"), $body);
327
328         // remove the recycle signs and the names since they aren't helpful on twitter
329         // recycle 1
330         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
331         $body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
332         // recycle 2 (Test)
333         $recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
334         $body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
335
336         // remove the share element
337         $body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
338
339         // At first convert the text to html
340         $html = bbcode($body, false, false);
341
342         // Then convert it to plain text
343         //$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
344         $msg = trim(html2plain($html, 0, true));
345         $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
346
347         // Removing multiple newlines
348         while (strpos($msg, "\n\n\n") !== false)
349                 $msg = str_replace("\n\n\n", "\n\n", $msg);
350
351         // Removing multiple spaces
352         while (strpos($msg, "  ") !== false)
353                 $msg = str_replace("  ", " ", $msg);
354
355         $origmsg = $msg;
356
357         // Removing URLs
358         $msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
359
360         $msg = trim($msg);
361
362         $link = '';
363         // look for bookmark-bbcode and handle it with priority
364         if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
365                 $link = $matches[1];
366
367         $multiplelinks = (strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark"));
368
369         // If there is no bookmark element then take the first link
370         if ($link == '') {
371                 $links = collecturls($html);
372                 if (sizeof($links) > 0) {
373                         reset($links);
374                         $link = current($links);
375                 }
376                 $multiplelinks = (sizeof($links) > 1);
377         }
378
379         $msglink = "";
380         if ($multiplelinks)
381                 $msglink = $b["plink"];
382         else if ($link != "")
383                 $msglink = $link;
384         else if ($multipleimages)
385                 $msglink = $b["plink"];
386         else if ($image != "")
387                 $msglink = $image;
388
389         if (($msglink == "") and strlen($msg) > $max_char)
390                 $msglink = $b["plink"];
391
392         // If the message is short enough then don't modify it.
393         if ((strlen(trim($origmsg)) <= $max_char) AND ($msglink == ""))
394                 return(trim($origmsg));
395
396         // If the message is short enough and the link exists in the original message don't modify it as well
397         // -3 because of the bad shortener of twitter
398         if ((strlen(trim($origmsg)) <= ($max_char - 3)) AND strpos($origmsg, $msglink))
399                 return(trim($origmsg));
400
401         if (strlen($msglink) > 20)
402                 $msglink = short_link($msglink);
403
404         if (strlen(trim($msg." ".$msglink)) > ($max_char - 3)) {
405                 $msg = substr($msg, 0, ($max_char - 3) - (strlen($msglink)));
406                 $lastchar = substr($msg, -1);
407                 $msg = substr($msg, 0, -1);
408                 $pos = strrpos($msg, "\n");
409                 if ($pos > 0)
410                         $msg = substr($msg, 0, $pos);
411                 else if ($lastchar != "\n")
412                         $msg = substr($msg, 0, -3)."...";
413         }
414         $msg = str_replace("\n", " ", $msg);
415
416         // Removing multiple spaces - again
417         while (strpos($msg, "  ") !== false)
418                 $msg = str_replace("  ", " ", $msg);
419
420         return(trim($msg." ".$msglink));
421 }
422
423 function twitter_post_hook(&$a,&$b) {
424
425         /**
426          * Post to Twitter
427          */
428
429         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
430         return;
431
432         if(! strstr($b['postopts'],'twitter'))
433                 return;
434
435         if($b['parent'] != $b['id'])
436                 return;
437
438         // if post comes from twitter don't send it back
439         if($b['app'] == "Twitter")
440                 return;
441
442         logger('twitter post invoked');
443
444
445         load_pconfig($b['uid'], 'twitter');
446
447         $ckey    = get_config('twitter', 'consumerkey');
448         $csecret = get_config('twitter', 'consumersecret');
449         $otoken  = get_pconfig($b['uid'], 'twitter', 'oauthtoken');
450         $osecret = get_pconfig($b['uid'], 'twitter', 'oauthsecret');
451         $intelligent_shortening = get_pconfig($b['uid'], 'twitter', 'intelligent_shortening');
452
453         // Global setting overrides this
454         if (get_config('twitter','intelligent_shortening'))
455                 $intelligent_shortening = get_config('twitter','intelligent_shortening');
456
457         if($ckey && $csecret && $otoken && $osecret) {
458                 logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG);
459
460                 require_once('library/twitteroauth.php');
461                 require_once('include/bbcode.php');
462                 $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
463                 // in theory max char is 140 but T. uses t.co to make links 
464                 // longer so we give them 10 characters extra
465                 if (!$intelligent_shortening) {
466                         $max_char = 130; // max. length for a tweet
467                         // we will only work with up to two times the length of the dent 
468                         // we can later send to Twitter. This way we can "gain" some 
469                         // information during shortening of potential links but do not 
470                         // shorten all the links in a 200000 character long essay.
471                         if (! $b['title']=='') {
472                             $tmp = $b['title'] . ' : '. $b['body'];
473         //                    $tmp = substr($tmp, 0, 4*$max_char);
474                         } else {
475                             $tmp = $b['body']; // substr($b['body'], 0, 3*$max_char);
476                         }
477                         // if [url=bla][img]blub.png[/img][/url] get blub.png
478                         $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\]\[img\](\\w+.*?)\\[\\/img\]\\[\\/url\]/i', '$2', $tmp);
479                         // preserve links to images, videos and audios
480                         $tmp = preg_replace( '/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism', '$3', $tmp);
481                         $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $tmp);
482                         $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp);
483                         $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp);
484                         $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp);
485                         $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp);
486                         $linksenabled = get_pconfig($b['uid'],'twitter','post_taglinks');
487                         // if a #tag is linked, don't send the [url] over to SN
488                         // that is, don't send if the option is not set in the
489                         // connector settings
490                         if ($linksenabled=='0') {
491                                 // #-tags
492                                 $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp);
493                                 // @-mentions
494                                 $tmp = preg_replace( '/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $tmp);
495                                 // recycle 1
496                                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
497                                 $tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
498                                 // recycle 2 (Test)
499                                 $recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
500                                 $tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
501                         }
502                         $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp);
503                         $tmp = preg_replace( '/\[bookmark\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/bookmark\]/i', '$2 $1', $tmp);
504                         // find all http or https links in the body of the entry and
505                         // apply the shortener if the link is longer then 20 characters
506                         if (( strlen($tmp)>$max_char ) && ( $max_char > 0 )) {
507                             preg_match_all ( '/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', $tmp, $allurls  );
508                             foreach ($allurls as $url) {
509                                 foreach ($url as $u) {
510                                     if (strlen($u)>20) {
511                                         $sl = short_link($u);
512                                         $tmp = str_replace( $u, $sl, $tmp );
513                                     }
514                                 }
515                             }
516                         }
517                         // ok, all the links we want to send out are save, now strip 
518                         // away the remaining bbcode
519                         //$msg = strip_tags(bbcode($tmp, false, false));
520                         $msg = bbcode($tmp, false, false, true);
521                         $msg = str_replace(array('<br>','<br />'),"\n",$msg);
522                         $msg = strip_tags($msg);
523
524                         // quotes not working - let's try this
525                         $msg = html_entity_decode($msg);
526                         if (( strlen($msg) > $max_char) && $max_char > 0) {
527                                 $shortlink = short_link( $b['plink'] );
528                                 // the new message will be shortened such that "... $shortlink"
529                                 // will fit into the character limit
530                                 $msg = nl2br(substr($msg, 0, $max_char-strlen($shortlink)-4));
531                                 $msg = str_replace(array('<br>','<br />'),' ',$msg);
532                                 $e = explode(' ', $msg);
533                                 //  remove the last word from the cut down message to 
534                                 //  avoid sending cut words to the MicroBlog
535                                 array_pop($e);
536                                 $msg = implode(' ', $e);
537                                 $msg .= '... ' . $shortlink;
538                         }
539
540                         $msg = trim($msg);
541                 } else
542                         $msg = twitter_shortenmsg($b);
543
544                 // and now tweet it :-)
545                 if(strlen($msg)) {
546                         $result = $tweet->post('statuses/update', array('status' => $msg));
547                         logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
548                         if ($result->error) {
549                                 logger('Send to Twitter failed: "' . $result->error . '"');
550                         }
551                 }
552         }
553 }
554
555 function twitter_plugin_admin_post(&$a){
556         $consumerkey    =       ((x($_POST,'consumerkey'))              ? notags(trim($_POST['consumerkey']))   : '');
557         $consumersecret =       ((x($_POST,'consumersecret'))   ? notags(trim($_POST['consumersecret'])): '');
558         $applicationname = ((x($_POST, 'applicationname')) ? notags(trim($_POST['applicationname'])):'');
559         set_config('twitter','consumerkey',$consumerkey);
560         set_config('twitter','consumersecret',$consumersecret);
561         set_config('twitter','application_name',$applicationname);
562         info( t('Settings updated.'). EOL );
563 }
564 function twitter_plugin_admin(&$a, &$o){
565         $t = get_markup_template( "admin.tpl", "addon/twitter/" );
566
567         $o = replace_macros($t, array(
568                 '$submit' => t('Submit'),
569                                                                 // name, label, value, help, [extra values]
570                 '$consumerkey' => array('consumerkey', t('Consumer key'),  get_config('twitter', 'consumerkey' ), ''),
571                 '$consumersecret' => array('consumersecret', t('Consumer secret'),  get_config('twitter', 'consumersecret' ), ''),
572                 '$applicationname' => array('applicationname', t('Name of the Twitter Application'), get_config('twitter','application_name'),t('set this to avoid mirroring postings from ~friendica back to ~friendica'))
573         ));
574 }
575
576 function twitter_cron($a,$b) {
577         $last = get_config('twitter','last_poll');
578
579         $poll_interval = intval(get_config('twitter','poll_interval'));
580         if(! $poll_interval)
581                 $poll_interval = TWITTER_DEFAULT_POLL_INTERVAL;
582
583         if($last) {
584                 $next = $last + ($poll_interval * 60);
585                 if($next > time()) {
586                         logger('twitter: poll intervall not reached');
587                         return;
588                 }
589         }
590         logger('twitter: cron_start');
591
592         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'twitter' AND `k` = 'mirror_posts' AND `v` = '1' ORDER BY RAND() ");
593         if(count($r)) {
594                 foreach($r as $rr) {
595                         logger('twitter: fetching for user '.$rr['uid']);
596                         twitter_fetchtimeline($a, $rr['uid']);
597                 }
598         }
599
600         logger('twitter: cron_end');
601
602         set_config('twitter','last_poll', time());
603 }
604
605 function twitter_fetchtimeline($a, $uid) {
606         $ckey    = get_config('twitter', 'consumerkey');
607         $csecret = get_config('twitter', 'consumersecret');
608         $otoken  = get_pconfig($uid, 'twitter', 'oauthtoken');
609         $osecret = get_pconfig($uid, 'twitter', 'oauthsecret');
610         $lastid  = get_pconfig($uid, 'twitter', 'lastid');
611
612         $application_name  = get_config('twitter', 'application_name');
613
614         if ($application_name == "")
615                 $application_name = $a->get_hostname();
616
617         require_once('library/twitteroauth.php');
618         $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
619
620         $parameters = array("exclude_replies" => true, "trim_user" => true, "contributor_details" => false, "include_rts" => false);
621
622         $first_time = ($lastid == "");
623
624         if ($lastid <> "")
625                 $parameters["since_id"] = $lastid;
626
627         $items = $connection->get('statuses/user_timeline', $parameters);
628
629         if (!is_array($items))
630                 return;
631
632         $posts = array_reverse($items);
633
634         if (count($posts)) {
635             foreach ($posts as $post) {
636                 if ($post->id_str > $lastid)
637                         $lastid = $post->id_str;
638
639                 if ($first_time)
640                         continue;
641
642                 if (!strpos($post->source, $application_name)) {
643                         $_SESSION["authenticated"] = true;
644                         $_SESSION["uid"] = $uid;
645
646                         $_REQUEST["type"] = "wall";
647                         $_REQUEST["api_source"] = true;
648                         $_REQUEST["profile_uid"] = $uid;
649                         $_REQUEST["source"] = "Twitter";
650
651                         //$_REQUEST["date"] = $post->created_at;
652
653                         $_REQUEST["body"] = $post->text;
654                         if (is_string($post->place->name))
655                                 $_REQUEST["location"] = $post->place->name;
656
657                         if (is_string($post->place->full_name))
658                                 $_REQUEST["location"] = $post->place->full_name;
659
660                         if (is_array($post->geo->coordinates))
661                                 $_REQUEST["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
662
663                         if (is_array($post->coordinates->coordinates))
664                                 $_REQUEST["coord"] = $post->coordinates->coordinates[1]." ".$post->coordinates->coordinates[0];
665
666                         //print_r($_REQUEST);
667                         logger('twitter: posting for user '.$uid);
668
669                         require_once('mod/item.php');
670                         item_post($a);
671
672                 }
673             }
674         }
675         set_pconfig($uid, 'twitter', 'lastid', $lastid);
676 }