]> git.mxchange.org Git - friendica-addons.git/blob - appnetpost/appnetpost.php
The connector settings are now with logo.
[friendica-addons.git] / appnetpost / appnetpost.php
1 <?php
2
3 /**
4  * Name: App.net Post
5  * Description: Posts to app.net with the help of ifttt.com
6  * Version: 0.1
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9
10 function appnetpost_install() {
11         register_hook('post_local',           'addon/appnetpost/appnetpost.php', 'appnetpost_post_local');
12         register_hook('notifier_normal',      'addon/appnetpost/appnetpost.php', 'appnetpost_send');
13         register_hook('jot_networks',         'addon/appnetpost/appnetpost.php', 'appnetpost_jot_nets');
14         register_hook('connector_settings',      'addon/appnetpost/appnetpost.php', 'appnetpost_settings');
15         register_hook('connector_settings_post', 'addon/appnetpost/appnetpost.php', 'appnetpost_settings_post');
16 }
17
18
19 function appnetpost_uninstall() {
20         unregister_hook('post_local',       'addon/appnetpost/appnetpost.php', 'appnetpost_post_local');
21         unregister_hook('notifier_normal',  'addon/appnetpost/appnetpost.php', 'appnetpost_send');
22         unregister_hook('jot_networks',     'addon/appnetpost/appnetpost.php', 'appnetpost_jot_nets');
23         unregister_hook('connector_settings',      'addon/appnetpost/appnetpost.php', 'appnetpost_settings');
24         unregister_hook('connector_settings_post', 'addon/appnetpost/appnetpost.php', 'appnetpost_settings_post');
25 }
26
27 function appnetpost_jot_nets(&$a,&$b) {
28         if(! local_user())
29                 return;
30
31         $post = get_pconfig(local_user(),'appnetpost','post');
32         if(intval($post) == 1) {
33                 $defpost = get_pconfig(local_user(),'appnetpost','post_by_default');
34                 $selected = ((intval($defpost) == 1) ? ' checked="checked" ' : '');
35                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="appnetpost_enable"' . $selected . ' value="1" /> '
36                         . t('Post to app.net') . '</div>';
37     }
38 }
39
40 function appnetpost_settings(&$a,&$s) {
41
42         if(! local_user())
43                 return;
44
45         /* Add our stylesheet to the page so we can make our settings look nice */
46
47         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/appnetpost/appnetpost.css' . '" media="all" />' . "\r\n";
48
49         $enabled = get_pconfig(local_user(),'appnetpost','post');
50         $checked = (($enabled) ? ' checked="checked" ' : '');
51
52         $def_enabled = get_pconfig(local_user(),'appnetpost','post_by_default');
53         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
54
55         $s .= '<span id="settings_appnetpost_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_appnetpost_expanded\'); openClose(\'settings_appnetpost_inflated\');">';
56         $s .= '<img class="connector" src="images/appnet.png" /><h3 class="connector">'. t('App.net Export').'</h3>';
57         $s .= '</span>';
58         $s .= '<div id="settings_appnetpost_expanded" class="settings-block" style="display: none;">';
59         $s .= '<span class="fakelink" onclick="openClose(\'settings_appnetpost_expanded\'); openClose(\'settings_appnetpost_inflated\');">';
60         $s .= '<img class="connector" src="images/appnet.png" /><h3 class="connector">'. t('App.net Export').'</h3>';
61         $s .= '</span>';
62
63         $s .= '<div id="appnetpost-enable-wrapper">';
64         $s .= '<label id="appnetpost-enable-label" for="appnetpost-checkbox">' . t('Enable App.net Post Plugin') . '</label>';
65         $s .= '<input id="appnetpost-checkbox" type="checkbox" name="appnetpost" value="1" ' . $checked . '/>';
66         $s .= '</div><div class="clear"></div>';
67
68         $s .= '<div id="appnetpost-bydefault-wrapper">';
69         $s .= '<label id="appnetpost-bydefault-label" for="appnetpost-bydefault">' . t('Post to App.net by default') . '</label>';
70         $s .= '<input id="appnetpost-bydefault" type="checkbox" name="appnetpost_bydefault" value="1" ' . $def_checked . '/>';
71         $s .= '</div><div class="clear"></div>';
72
73         /* provide a submit button */
74
75         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="appnetpost-submit" name="appnetpost-submit" class="settings-submit" value="' . t('Submit') . '" /></div>';
76         $s .= '<p>Register an account at <a href="https://ifttt.com">IFTTT</a> and create a recipe with the following values:';
77         $s .= '<ul><li>If: New feed item (via RSS)</li>';
78         $s .= '<li>Then: Post an update (via app.net)</li>';
79         $s .= '<li>Feed URL: '.$a->get_baseurl().'/appnetpost/'.urlencode($a->user["nickname"]).'</li>';
80         $s .= '<li>Message: {{EntryContent}}</li>';
81         $s .= '<li>Original URL: {{EntryUrl}}</li></ul></div>';
82 }
83
84 function appnetpost_settings_post(&$a,&$b) {
85
86         if(x($_POST,'appnetpost-submit')) {
87                 set_pconfig(local_user(),'appnetpost','post',intval($_POST['appnetpost']));
88                 set_pconfig(local_user(),'appnetpost','post_by_default',intval($_POST['appnetpost_bydefault']));
89         }
90 }
91
92 function appnetpost_post_local(&$a,&$b) {
93
94         if($b['edit'])
95                 return;
96
97         if((! local_user()) || (local_user() != $b['uid']))
98                 return;
99
100         if($b['private'] || $b['parent'])
101                 return;
102
103         $post   = intval(get_pconfig(local_user(),'appnetpost','post'));
104
105         $enable = (($post && x($_REQUEST,'appnetpost_enable')) ? intval($_REQUEST['appnetpost_enable']) : 0);
106
107         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'appnetpost','post_by_default')))
108                 $enable = 1;
109
110         if(!$enable)
111                 return;
112
113         if(strlen($b['postopts']))
114                 $b['postopts'] .= ',';
115
116         $b['postopts'] .= 'gplus';
117 }
118
119 function appnetpost_send(&$a,&$b) {
120
121         logger('appnetpost_send: invoked for post '.$b['id']." ".$b['app']);
122
123         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
124                 return;
125
126         if(! strstr($b['postopts'],'gplus'))
127                 return;
128
129         if($b['parent'] != $b['id'])
130                 return;
131
132         $itemlist = get_pconfig($b["uid"],'appnetpost','itemlist');
133         $items = explode(",", $itemlist);
134
135         $i = 0;
136         $newitems = array($b['id']);
137         foreach ($items AS $item)
138                 if ($i++ < 9)
139                         $newitems[] = $item;
140
141         $itemlist = implode(",", $newitems);
142
143         logger('appnetpost_send: new itemlist: '.$itemlist." for uid ".$b["uid"]);
144
145         set_pconfig($b["uid"],'appnetpost','itemlist', $itemlist);
146 }
147
148 function appnetpost_module() {}
149
150 function appnetpost_init() {
151         global $a, $_SERVER;
152
153         $uid = 0;
154
155         if (isset($a->argv[1])) {
156                 $uid = (int)$a->argv[1];
157                 if ($uid == 0) {
158                         $contacts = q("SELECT `username`, `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($a->argv[1]));
159                         if ($contacts) {
160                                 $uid = $contacts[0]["uid"];
161                                 $nick = $a->argv[1];
162                         }
163                 } else {
164                         $contacts = q("SELECT `username` FROM `user` WHERE `uid`=%d LIMIT 1", intval($uid));
165                         $nick = $uid;
166                 }
167         }
168
169         header("content-type: application/atom+xml");
170         echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
171         echo '<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'."\n";
172         echo "\t".'<title type="html"><![CDATA['.$a->config['sitename'].']]></title>'."\n";
173         if ($uid != 0) {
174                 echo "\t".'<subtitle type="html"><![CDATA['.$contacts[0]["username"]."]]></subtitle>\n";
175                 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/appnetpost/'.$nick.'"/>'."\n";
176         } else
177                 echo "\t".'<link rel="self" href="'.$a->get_baseurl().'/appnetpost"/>'."\n";
178         echo "\t<id>".$a->get_baseurl()."/</id>\n";
179         echo "\t".'<link rel="alternate" type="text/html" href="'.$a->get_baseurl().'"/>'."\n";
180         echo "\t<updated>".date("c")."</updated>\n"; // To-Do
181         // <rights>Copyright ... </rights>
182         echo "\t".'<generator uri="'.$a->get_baseurl().'">'.$a->config['sitename'].'</generator>'."\n";
183
184         if ($uid != 0) {
185                 $itemlist = get_pconfig($uid,'appnetpost','itemlist');
186                 $items = explode(",", $itemlist);
187
188                 foreach ($items AS $item)
189                         appnetpost_feeditem($item, $uid);
190         } else {
191                 $items = q("SELECT `id` FROM `item` FORCE INDEX (`received`) WHERE `item`.`visible` = 1 AND `item`.`deleted` = 0 and `item`.`moderated` = 0 AND `item`.`allow_cid` = ''  AND `item`.`allow_gid` = '' AND `item`.`deny_cid`  = '' AND `item`.`deny_gid`  = '' AND `item`.`private` = 0 AND `item`.`wall` = 1 AND `item`.`id` = `item`.`parent` ORDER BY `received` DESC LIMIT 10");
192                 foreach ($items AS $item)
193                         appnetpost_feeditem($item["id"], $uid);
194         }
195         echo "</feed>\n";
196         killme();
197 }
198
199 if (! function_exists( 'short_link' )) {
200 function short_link($url) {
201         require_once('library/slinky.php');
202         $slinky = new Slinky( $url );
203         $yourls_url = get_config('yourls','url1');
204         if ($yourls_url) {
205                 $yourls_username = get_config('yourls','username1');
206                 $yourls_password = get_config('yourls', 'password1');
207                 $yourls_ssl = get_config('yourls', 'ssl1');
208                 $yourls = new Slinky_YourLS();
209                 $yourls->set( 'username', $yourls_username );
210                 $yourls->set( 'password', $yourls_password );
211                 $yourls->set( 'ssl', $yourls_ssl );
212                 $yourls->set( 'yourls-url', $yourls_url );
213                 $slinky->set_cascade( array( $yourls, new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
214         } else {
215                 // setup a cascade of shortening services
216                 // try to get a short link from these services
217                 // in the order ur1.ca, trim, id.gd, tinyurl
218                 $slinky->set_cascade( array( new Slinky_UR1ca(), new Slinky_Trim(), new Slinky_IsGd(), new Slinky_TinyURL() ) );
219         }
220         return $slinky->short();
221 } };
222
223 function appnetpost_feeditem($pid, $uid) {
224         global $a;
225
226         require_once('include/bbcode.php');
227         require_once("include/html2plain.php");
228
229         $items = q("SELECT `uri`, `plink`, `author-link`, `author-name`, `created`, `edited`, `id`, `title`, `body` from `item` WHERE id=%d", intval($pid));
230         foreach ($items AS $item) {
231
232                 $item['body'] = bb_CleanPictureLinks($item['body']);
233
234                 // Looking for the first image
235                 $image = '';
236                 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$item['body'],$matches))
237                         $image = $matches[3];
238
239                 if ($image == '')
240                         if(preg_match("/\[img\](.*?)\[\/img\]/is",$item['body'],$matches))
241                                 $image = $matches[1];
242
243                 $multipleimages = (strpos($item['body'], "[img") != strrpos($item['body'], "[img"));
244
245                 // When saved into the database the content is sent through htmlspecialchars
246                 // That means that we have to decode all image-urls
247                 $image = htmlspecialchars_decode($image);
248
249                 $link = '';
250                 // look for bookmark-bbcode and handle it with priority
251                 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$item['body'],$matches))
252                         $link = $matches[1];
253
254                 $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
255
256                 $body = $item['body'];
257
258                 // At first convert the text to html
259                 $html = bbcode($body, false, false, 2);
260
261                 // Then convert it to plain text
262                 $msg = trim(html2plain($html, 0, true));
263                 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
264
265                 // If there is no bookmark element then take the first link
266                 if ($link == '') {
267                         $links = collecturls($html);
268                         if (sizeof($links) > 0) {
269                                 reset($links);
270                                 $link = current($links);
271                         }
272                         $multiplelinks = (sizeof($links) > 1);
273
274                         if ($multiplelinks) {
275                                 $html2 = bbcode($msg, false, false);
276                                 $links2 = collecturls($html2);
277                                 if (sizeof($links2) > 0) {
278                                         reset($links2);
279                                         $link = current($links2);
280                                         $multiplelinks = (sizeof($links2) > 1);
281                                 }
282                         }
283                 }
284
285                 $msglink = "";
286                 if ($multiplelinks)
287                         $msglink = $item["plink"];
288                 else if ($link != "")
289                         $msglink = $link;
290                 else if ($multipleimages)
291                         $msglink = $item["plink"];
292                 else if ($image != "")
293                         $msglink = $image;
294
295                 // Fetching the title and add all lines
296                 if ($item["title"] != "")
297                         $title = $item["title"];
298
299                 $lines = explode("\n", $msg);
300                 foreach ($lines AS $line)
301                         $title .= "\n".$line;
302
303                 $max_char = 256;
304
305                 $origlink = $msglink;
306
307                 if (strlen($msglink) > 20)
308                         $msglink = short_link($msglink);
309
310                 $title = trim(str_replace($origlink, $msglink, $title));
311
312                 if (strlen(trim($title." ".$msglink)) > $max_char) {
313                         $title = substr($title, 0, $max_char - (strlen($msglink)));
314                         $lastchar = substr($title, -1);
315                         $title = substr($title, 0, -1);
316                         $pos = strrpos($title, "\n");
317                         if ($pos > 0)
318                                 $title = substr($title, 0, $pos);
319                         else if ($lastchar != "\n")
320                         $title = substr($title, 0, -3)."...";
321                 }
322
323                 if (($msglink != "") AND !strstr($title, $msglink))
324                         $title = trim($title." ".$msglink);
325                 else
326                         $title = trim($title);
327
328                 if ($title == "")
329                         continue;
330
331                 //$origlink = original_url($origlink);
332
333                 $html = nl2br($title);
334
335                 $origlink = $item["plink"];
336                 $origlink = htmlspecialchars(html_entity_decode($origlink));
337
338                 $title = str_replace("&", "&amp;", $title);
339                 //$html = str_replace("&", "&amp;", $html);
340
341                 echo "\t".'<entry xmlns="http://www.w3.org/2005/Atom">'."\n";
342                 echo "\t\t".'<title type="html" xml:space="preserve"><![CDATA['.$title."]]></title>\n";
343                 echo "\t\t".'<link rel="alternate" type="text/html" href="'.$origlink.'" />'."\n";
344                 // <link rel="enclosure" type="audio/mpeg" length="1337" href="http://example.org/audio/ph34r_my_podcast.mp3"/>
345                 echo "\t\t<id>".$item["uri"]."</id>\n";
346                 echo "\t\t<updated>".date("c", strtotime($item["edited"]))."</updated>\n";
347                 echo "\t\t<published>".date("c", strtotime($item["created"]))."</published>\n";
348                 echo "\t\t<author>\n\t\t\t<name><![CDATA[".$item["author-name"]."]]></name>\n";
349                 echo "\t\t\t<uri>".$item["author-link"]."</uri>\n\t\t</author>\n";
350                 //echo '<content type="image/png" src="http://media.example.org/the_beach.png"/>';
351                 echo "\t\t".'<content type="html" xml:space="preserve" xml:base="'.$item["plink"].'"><![CDATA['.$html."]]></content>\n";
352                 echo "\t</entry>\n";
353         }
354 }