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