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