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