]> git.mxchange.org Git - friendica-addons.git/blob - twitter/twitter.php
Preparations for shared posts
[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  */
8
9
10 /*   Twitter Plugin for Friendica
11  *
12  *   Author: Tobias Diekershoff
13  *           tobias.diekershoff@gmx.net
14  *
15  *   License:3-clause BSD license
16  *
17  *   Configuration:
18  *     To use this plugin you need a OAuth Consumer key pair (key & secret)
19  *     you can get it from Twitter at https://twitter.com/apps
20  *
21  *     Register your Friendica site as "Client" application with "Read & Write" access
22  *     we do not need "Twitter as login". When you've registered the app you get the
23  *     OAuth Consumer key and secret pair for your application/site.
24  *
25  *     Add this key pair to your global .htconfig.php or use the admin panel.
26  *
27  *     $a->config['twitter']['consumerkey'] = 'your consumer_key here';
28  *     $a->config['twitter']['consumersecret'] = 'your consumer_secret here';
29  *
30  *     To activate the plugin itself add it to the $a->config['system']['addon']
31  *     setting. After this, your user can configure their Twitter account settings
32  *     from "Settings -> Plugin Settings".
33  *
34  *     Requirements: PHP5, curl [Slinky library]
35  *
36  *     Documentation: http://diekershoff.homeunix.net/redmine/wiki/friendikaplugin/Twitter_Plugin
37  */
38
39 function twitter_install() {
40         //  we need some hooks, for the configuration and for sending tweets
41         register_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
42         register_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
43         register_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
44         register_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
45         register_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
46         logger("installed twitter");
47 }
48
49
50 function twitter_uninstall() {
51         unregister_hook('connector_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
52         unregister_hook('connector_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
53         unregister_hook('post_local', 'addon/twitter/twitter.php', 'twitter_post_local');
54         unregister_hook('notifier_normal', 'addon/twitter/twitter.php', 'twitter_post_hook');
55         unregister_hook('jot_networks', 'addon/twitter/twitter.php', 'twitter_jot_nets');
56
57         // old setting - remove only
58         unregister_hook('post_local_end', 'addon/twitter/twitter.php', 'twitter_post_hook');
59         unregister_hook('plugin_settings', 'addon/twitter/twitter.php', 'twitter_settings'); 
60         unregister_hook('plugin_settings_post', 'addon/twitter/twitter.php', 'twitter_settings_post');
61
62 }
63
64 function twitter_jot_nets(&$a,&$b) {
65         if(! local_user())
66                 return;
67
68         $tw_post = get_pconfig(local_user(),'twitter','post');
69         if(intval($tw_post) == 1) {
70                 $tw_defpost = get_pconfig(local_user(),'twitter','post_by_default');
71                 $selected = ((intval($tw_defpost) == 1) ? ' checked="checked" ' : '');
72                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="twitter_enable"' . $selected . ' value="1" /> ' 
73                         . t('Post to Twitter') . '</div>';      
74         }
75
76
77 }
78
79 function twitter_settings_post ($a,$post) {
80         if(! local_user())
81                 return;
82         // don't check twitter settings if twitter submit button is not clicked 
83         if (!x($_POST,'twitter-submit')) return;
84         
85         if (isset($_POST['twitter-disconnect'])) {
86                 /***
87                  * if the twitter-disconnect checkbox is set, clear the OAuth key/secret pair
88                  * from the user configuration
89                  */
90                 del_pconfig( local_user(), 'twitter', 'consumerkey'  );
91                 del_pconfig( local_user(), 'twitter', 'consumersecret' );
92                 del_pconfig( local_user(), 'twitter', 'oauthtoken'  );  
93                 del_pconfig( local_user(), 'twitter', 'oauthsecret'  );  
94                 del_pconfig( local_user(), 'twitter', 'post' );
95                 del_pconfig( local_user(), 'twitter', 'post_by_default' );
96                 del_pconfig( local_user(), 'twitter', 'post_taglinks');
97         } else {
98         if (isset($_POST['twitter-pin'])) {
99                 //  if the user supplied us with a PIN from Twitter, let the magic of OAuth happen
100                 logger('got a Twitter PIN');
101                 require_once('library/twitteroauth.php');
102                 $ckey    = get_config('twitter', 'consumerkey'  );
103                 $csecret = get_config('twitter', 'consumersecret' );
104                 //  the token and secret for which the PIN was generated were hidden in the settings
105                 //  form as token and token2, we need a new connection to Twitter using these token
106                 //  and secret to request a Access Token with the PIN
107                 $connection = new TwitterOAuth($ckey, $csecret, $_POST['twitter-token'], $_POST['twitter-token2']);
108                 $token   = $connection->getAccessToken( $_POST['twitter-pin'] );
109                 //  ok, now that we have the Access Token, save them in the user config
110                 set_pconfig(local_user(),'twitter', 'oauthtoken',  $token['oauth_token']);
111                 set_pconfig(local_user(),'twitter', 'oauthsecret', $token['oauth_token_secret']);
112                 set_pconfig(local_user(),'twitter', 'post', 1);
113                 set_pconfig(local_user(),'twitter', 'post_taglinks', 1);
114                 //  reload the Addon Settings page, if we don't do it see Bug #42
115                 goaway($a->get_baseurl().'/settings/connectors');
116         } else {
117                 //  if no PIN is supplied in the POST variables, the user has changed the setting
118                 //  to post a tweet for every new __public__ posting to the wall
119                 set_pconfig(local_user(),'twitter','post',intval($_POST['twitter-enable']));
120                 set_pconfig(local_user(),'twitter','post_by_default',intval($_POST['twitter-default']));
121                 set_pconfig(local_user(),'twitter','post_taglinks',intval($_POST['twitter-sendtaglinks']));
122                 info( t('Twitter settings updated.') . EOL);
123         }}
124 }
125 function twitter_settings(&$a,&$s) {
126         if(! local_user())
127                 return;
128         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/twitter/twitter.css' . '" media="all" />' . "\r\n";
129         /***
130          * 1) Check that we have global consumer key & secret
131          * 2) If no OAuthtoken & stuff is present, generate button to get some
132          * 3) Checkbox for "Send public notices (140 chars only)
133          */
134         $ckey    = get_config('twitter', 'consumerkey' );
135         $csecret = get_config('twitter', 'consumersecret' );
136         $otoken  = get_pconfig(local_user(), 'twitter', 'oauthtoken'  );
137         $osecret = get_pconfig(local_user(), 'twitter', 'oauthsecret' );
138         $enabled = get_pconfig(local_user(), 'twitter', 'post');
139         $checked = (($enabled) ? ' checked="checked" ' : '');
140         $defenabled = get_pconfig(local_user(),'twitter','post_by_default');
141         $defchecked = (($defenabled) ? ' checked="checked" ' : '');
142         $linksenabled = get_pconfig(local_user(),'twitter','post_taglinks');
143         $linkschecked = (($linksenabled) ? ' checked="checked" ' : '');
144
145         $s .= '<div class="settings-block">';
146         $s .= '<h3>'. t('Twitter Posting Settings') .'</h3>';
147
148         if ( (!$ckey) && (!$csecret) ) {
149                 /***
150                  * no global consumer keys
151                  * display warning and skip personal config
152                  */
153                 $s .= '<p>'. t('No consumer key pair for Twitter found. Please contact your site administrator.') .'</p>';
154         } else {
155                 /***
156                  * ok we have a consumer key pair now look into the OAuth stuff
157                  */
158                 if ( (!$otoken) && (!$osecret) ) {
159                         /***
160                          * the user has not yet connected the account to twitter...
161                          * get a temporary OAuth key/secret pair and display a button with
162                          * which the user can request a PIN to connect the account to a
163                          * account at Twitter.
164                          */
165                         require_once('library/twitteroauth.php');
166                         $connection = new TwitterOAuth($ckey, $csecret);
167                         $request_token = $connection->getRequestToken();
168                         $token = $request_token['oauth_token'];
169                         /***
170                          *  make some nice form
171                          */
172                         $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>';
173                         $s .= '<a href="'.$connection->getAuthorizeURL($token).'" target="_twitter"><img src="addon/twitter/lighter.png" alt="'.t('Log in with Twitter').'"></a>';
174                         $s .= '<div id="twitter-pin-wrapper">';
175                         $s .= '<label id="twitter-pin-label" for="twitter-pin">'. t('Copy the PIN from Twitter here') .'</label>';
176                         $s .= '<input id="twitter-pin" type="text" name="twitter-pin" />';
177                         $s .= '<input id="twitter-token" type="hidden" name="twitter-token" value="'.$token.'" />';
178                         $s .= '<input id="twitter-token2" type="hidden" name="twitter-token2" value="'.$request_token['oauth_token_secret'].'" />';
179             $s .= '</div><div class="clear"></div>';
180             $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
181                 } else {
182                         /***
183                          *  we have an OAuth key / secret pair for the user
184                          *  so let's give a chance to disable the postings to Twitter
185                          */
186                         require_once('library/twitteroauth.php');
187                         $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
188                         $details = $connection->get('account/verify_credentials');
189                         $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>';
190                         $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>';
191                         if ($a->user['hidewall']) {
192                             $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>';
193                         }
194                         $s .= '<div id="twitter-enable-wrapper">';
195                         $s .= '<label id="twitter-enable-label" for="twitter-checkbox">'. t('Allow posting to Twitter'). '</label>';
196                         $s .= '<input id="twitter-checkbox" type="checkbox" name="twitter-enable" value="1" ' . $checked . '/>';
197                         $s .= '<div class="clear"></div>';
198                         $s .= '<label id="twitter-default-label" for="twitter-default">'. t('Send public postings to Twitter by default') .'</label>';
199                         $s .= '<input id="twitter-default" type="checkbox" name="twitter-default" value="1" ' . $defchecked . '/>';
200                         $s .= '<div class="clear"></div>';
201                         $s .= '<label id="twitter-sendtaglinks-label" for="twitter-sendtaglinks">'.t('Send linked #-tags and @-names to Twitter').'</label>';
202                         $s .= '<input id="twitter-sendtaglinks" type="checkbox" name="twitter-sendtaglinks" value="1" '. $linkschecked . '/>';
203                         $s .= '</div><div class="clear"></div>';
204
205                         $s .= '<div id="twitter-disconnect-wrapper">';
206                         $s .= '<label id="twitter-disconnect-label" for="twitter-disconnect">'. t('Clear OAuth configuration') .'</label>';
207                         $s .= '<input id="twitter-disconnect" type="checkbox" name="twitter-disconnect" value="1" />';
208                         $s .= '</div><div class="clear"></div>';
209                         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Submit') . '" /></div>'; 
210                 }
211         }
212         $s .= '</div><div class="clear"></div>';
213 }
214
215
216 function twitter_post_local(&$a,&$b) {
217
218         if($b['edit'])
219                 return;
220
221         if((local_user()) && (local_user() == $b['uid']) && (! $b['private']) && (! $b['parent']) ) {
222
223                 $twitter_post = intval(get_pconfig(local_user(),'twitter','post'));
224                 $twitter_enable = (($twitter_post && x($_REQUEST,'twitter_enable')) ? intval($_REQUEST['twitter_enable']) : 0);
225
226                 // if API is used, default to the chosen settings
227                 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'twitter','post_by_default')))
228                         $twitter_enable = 1;
229
230         if(! $twitter_enable)
231             return;
232
233         if(strlen($b['postopts']))
234             $b['postopts'] .= ',';
235         $b['postopts'] .= 'twitter';
236         }
237 }
238
239 if (! function_exists('short_link')) {
240 function short_link ($url) {
241     require_once('library/slinky.php');
242     $slinky = new Slinky( $url );
243     $yourls_url = get_config('yourls','url1');
244     if ($yourls_url) {
245             $yourls_username = get_config('yourls','username1');
246             $yourls_password = get_config('yourls', 'password1');
247             $yourls_ssl = get_config('yourls', 'ssl1');
248             $yourls = new Slinky_YourLS();
249             $yourls->set( 'username', $yourls_username );
250             $yourls->set( 'password', $yourls_password );
251             $yourls->set( 'ssl', $yourls_ssl );
252             $yourls->set( 'yourls-url', $yourls_url );
253             $slinky->set_cascade( array( $yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
254     }
255     else {
256             // setup a cascade of shortening services
257             // try to get a short link from these services
258             // in the order ur1.ca, trim, id.gd, tinyurl
259             $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
260     }
261     return $slinky->short();
262 } };
263
264 function twitter_shortenmsg($b) {
265         require_once("include/bbcode.php");
266         require_once("include/html2plain.php");
267
268         $max_char = 140;
269
270         // Looking for the first image
271         $image = '';
272         if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$b['body'],$matches))
273                 $image = $matches[3];
274
275         if ($image == '')
276                 if(preg_match("/\[img\](.*?)\[\/img\]/is",$b['body'],$matches))
277                         $image = $matches[1];
278
279         $multipleimages = (strpos($b['body'], "[img") != strrpos($b['body'], "[img"));
280
281         // When saved into the database the content is sent through htmlspecialchars
282         // That means that we have to decode all image-urls
283         $image = htmlspecialchars_decode($image);
284
285         $body = $b["body"];
286         if ($b["title"] != "")
287                 $body = $b["title"]."\n\n".$body;
288
289         // remove the recycle signs and the names since they aren't helpful on twitter
290         // recycle 1
291         $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
292         $body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
293         // recycle 2 (Test)
294         $recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
295         $body = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', "\n", $body);
296
297         // remove the share element
298         $body = preg_replace("/\[share(.*?)\](.*?)\[\/share\]/ism","\n\n$2\n\n",$body);
299
300         // At first convert the text to html
301         $html = bbcode($body, false, false);
302
303         // Then convert it to plain text
304         //$msg = trim($b['title']." \n\n".html2plain($html, 0, true));
305         $msg = trim(html2plain($html, 0, true));
306         $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
307
308         // Removing multiple newlines
309         while (strpos($msg, "\n\n\n") !== false)
310                 $msg = str_replace("\n\n\n", "\n\n", $msg);
311
312         // Removing multiple spaces
313         while (strpos($msg, "  ") !== false)
314                 $msg = str_replace("  ", " ", $msg);
315
316         // Removing URLs
317         $msg = preg_replace('/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', "", $msg);
318
319         $msg = trim($msg);
320
321         $link = '';
322         // look for bookmark-bbcode and handle it with priority
323         if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$b['body'],$matches))
324                 $link = $matches[1];
325
326         $multiplelinks = (strpos($b['body'], "[bookmark") != strrpos($b['body'], "[bookmark"));
327
328         // If there is no bookmark element then take the first link
329         if ($link == '') {
330                 $links = collecturls($html);
331                 if (sizeof($links) > 0) {
332                         reset($links);
333                         $link = current($links);
334                 }
335                 $multiplelinks = (sizeof($links) > 1);
336         }
337
338         $msglink = "";
339         if ($multiplelinks)
340                 $msglink = $b["plink"];
341         else if ($link != "")
342                 $msglink = $link;
343         else if ($multipleimages)
344                 $msglink = $b["plink"];
345         else if ($image != "")
346                 $msglink = $image;
347
348         if (($msglink == "") and strlen($msg) > $max_char)
349                 $msglink = $b["plink"];
350
351         if (strlen($msglink) > 20)
352                 $msglink = short_link($msglink);
353
354         if (strlen(trim($msg." ".$msglink)) > $max_char) {
355                 $msg = substr($msg, 0, $max_char - (strlen($msglink)));
356                 $lastchar = substr($msg, -1);
357                 $msg = substr($msg, 0, -1);
358                 $pos = strrpos($msg, "\n");
359                 if ($pos > 0)
360                         $msg = substr($msg, 0, $pos);
361                 else if ($lastchar != "\n")
362                         $msg = substr($msg, 0, -3)."...";
363         }
364         $msg = str_replace("\n", " ", $msg);
365
366         // Removing multiple spaces - again
367         while (strpos($msg, "  ") !== false)
368                 $msg = str_replace("  ", " ", $msg);
369
370         return(trim($msg." ".$msglink));
371 }
372
373 function twitter_post_hook(&$a,&$b) {
374
375         /**
376          * Post to Twitter
377          */
378
379         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
380         return;
381
382         if(! strstr($b['postopts'],'twitter'))
383                 return;
384
385         if($b['parent'] != $b['id'])
386                 return;
387
388         logger('twitter post invoked');
389
390
391         load_pconfig($b['uid'], 'twitter');
392
393         $ckey    = get_config('twitter', 'consumerkey'  );
394         $csecret = get_config('twitter', 'consumersecret' );
395         $otoken  = get_pconfig($b['uid'], 'twitter', 'oauthtoken'  );
396         $osecret = get_pconfig($b['uid'], 'twitter', 'oauthsecret' );
397
398         if($ckey && $csecret && $otoken && $osecret) {
399                 logger('twitter: we have customer key and oauth stuff, going to send.', LOGGER_DEBUG);
400
401                 require_once('library/twitteroauth.php');
402                 require_once('include/bbcode.php');
403                 $tweet = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
404                 // in theory max char is 140 but T. uses t.co to make links 
405                 // longer so we give them 10 characters extra
406
407                 $intelligent_shortening = get_config('twitter','intelligent_shortening');
408
409                 if (!$intelligent_shortening) {
410                         $max_char = 130; // max. length for a tweet
411                         // we will only work with up to two times the length of the dent 
412                         // we can later send to Twitter. This way we can "gain" some 
413                         // information during shortening of potential links but do not 
414                         // shorten all the links in a 200000 character long essay.
415                         if (! $b['title']=='') {
416                             $tmp = $b['title'] . ' : '. $b['body'];
417         //                    $tmp = substr($tmp, 0, 4*$max_char);
418                         } else {
419                             $tmp = $b['body']; // substr($b['body'], 0, 3*$max_char);
420                         }
421                         // if [url=bla][img]blub.png[/img][/url] get blub.png
422                         $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\]\[img\](\\w+.*?)\\[\\/img\]\\[\\/url\]/i', '$2', $tmp);
423                         // preserve links to images, videos and audios
424                         $tmp = preg_replace( '/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/ism', '$3', $tmp);
425                         $tmp = preg_replace( '/\[\\/?img(\\s+.*?\]|\])/i', '', $tmp);
426                         $tmp = preg_replace( '/\[\\/?video(\\s+.*?\]|\])/i', '', $tmp);
427                         $tmp = preg_replace( '/\[\\/?youtube(\\s+.*?\]|\])/i', '', $tmp);
428                         $tmp = preg_replace( '/\[\\/?vimeo(\\s+.*?\]|\])/i', '', $tmp);
429                         $tmp = preg_replace( '/\[\\/?audio(\\s+.*?\]|\])/i', '', $tmp);
430                         $linksenabled = get_pconfig($b['uid'],'twitter','post_taglinks');
431                         // if a #tag is linked, don't send the [url] over to SN
432                         // that is, don't send if the option is not set in the
433                         // connector settings
434                         if ($linksenabled=='0') {
435                                 // #-tags
436                                 $tmp = preg_replace( '/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $tmp);
437                                 // @-mentions
438                                 $tmp = preg_replace( '/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $tmp);
439                                 // recycle 1
440                                 $recycle = html_entity_decode("&#x2672; ", ENT_QUOTES, 'UTF-8');
441                                 $tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
442                                 // recycle 2 (Test)
443                                 $recycle = html_entity_decode("&#x25CC; ", ENT_QUOTES, 'UTF-8');
444                                 $tmp = preg_replace( '/'.$recycle.'\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', $recycle.'$2', $tmp);
445                         }
446                         $tmp = preg_replace( '/\[url\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/url\]/i', '$2 $1', $tmp);
447                         $tmp = preg_replace( '/\[bookmark\=(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)\](\w+.*?)\[\/bookmark\]/i', '$2 $1', $tmp);
448                         // find all http or https links in the body of the entry and
449                         // apply the shortener if the link is longer then 20 characters
450                         if (( strlen($tmp)>$max_char ) && ( $max_char > 0 )) {
451                             preg_match_all ( '/(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/i', $tmp, $allurls  );
452                             foreach ($allurls as $url) {
453                                 foreach ($url as $u) {
454                                     if (strlen($u)>20) {
455                                         $sl = short_link($u);
456                                         $tmp = str_replace( $u, $sl, $tmp );
457                                     }
458                                 }
459                             }
460                         }
461                         // ok, all the links we want to send out are save, now strip 
462                         // away the remaining bbcode
463                         //$msg = strip_tags(bbcode($tmp, false, false));
464                         $msg = bbcode($tmp, false, false);
465                         $msg = str_replace(array('<br>','<br />'),"\n",$msg);
466                         $msg = strip_tags($msg);
467
468                         // quotes not working - let's try this
469                         $msg = html_entity_decode($msg);
470                         if (( strlen($msg) > $max_char) && $max_char > 0) {
471                                 $shortlink = short_link( $b['plink'] );
472                                 // the new message will be shortened such that "... $shortlink"
473                                 // will fit into the character limit
474                                 $msg = nl2br(substr($msg, 0, $max_char-strlen($shortlink)-4));
475                                 $msg = str_replace(array('<br>','<br />'),' ',$msg);
476                                 $e = explode(' ', $msg);
477                                 //  remove the last word from the cut down message to 
478                                 //  avoid sending cut words to the MicroBlog
479                                 array_pop($e);
480                                 $msg = implode(' ', $e);
481                                 $msg .= '... ' . $shortlink;
482                         }
483
484                         $msg = trim($msg);
485                 } else
486                         $msg = twitter_shortenmsg($b);
487
488                 // and now tweet it :-)
489                 if(strlen($msg)) {
490                         $result = $tweet->post('statuses/update', array('status' => $msg));
491                         logger('twitter_post send, result: ' . print_r($result, true), LOGGER_DEBUG);
492                         if ($result->error) {
493                                 logger('Send to Twitter failed: "' . $result->error . '"');
494                         }
495                 }
496         }
497 }
498
499 function twitter_plugin_admin_post(&$a){
500         $consumerkey    =       ((x($_POST,'consumerkey'))              ? notags(trim($_POST['consumerkey']))   : '');
501         $consumersecret =       ((x($_POST,'consumersecret'))   ? notags(trim($_POST['consumersecret'])): '');
502         set_config('twitter','consumerkey',$consumerkey);
503         set_config('twitter','consumersecret',$consumersecret);
504         info( t('Settings updated.'). EOL );
505 }
506 function twitter_plugin_admin(&$a, &$o){
507         $t = file_get_contents( dirname(__file__). "/admin.tpl" );
508         $o = replace_macros($t, array(
509                 '$submit' => t('Submit'),
510                                                                 // name, label, value, help, [extra values]
511                 '$consumerkey' => array('consumerkey', t('Consumer key'),  get_config('twitter', 'consumerkey' ), ''),
512                 '$consumersecret' => array('consumersecret', t('Consumer secret'),  get_config('twitter', 'consumersecret' ), '')
513         ));
514 }