5 * Description: Posts to app.net with the help of ifttt.com
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
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');
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');
28 function appnetpost_jot_nets(&$a,&$b) {
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>';
41 function appnetpost_settings(&$a,&$s) {
46 /* Add our stylesheet to the page so we can make our settings look nice */
48 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/appnetpost/appnetpost.css' . '" media="all" />' . "\r\n";
50 $enabled = get_pconfig(local_user(),'appnetpost','post');
51 $checked = (($enabled) ? ' checked="checked" ' : '');
53 $css = (($enabled) ? '' : '-disabled');
55 $def_enabled = get_pconfig(local_user(),'appnetpost','post_by_default');
56 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
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>';
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>';
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>';
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>';
76 /* provide a submit button */
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>';
87 function appnetpost_settings_post(&$a,&$b) {
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']));
95 function appnetpost_post_local(&$a,&$b) {
100 if((! local_user()) || (local_user() != $b['uid']))
103 if($b['private'] || $b['parent'])
106 $post = intval(get_pconfig(local_user(),'appnetpost','post'));
108 $enable = (($post && x($_REQUEST,'appnetpost_enable')) ? intval($_REQUEST['appnetpost_enable']) : 0);
110 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'appnetpost','post_by_default')))
116 if(strlen($b['postopts']))
117 $b['postopts'] .= ',';
119 $b['postopts'] .= 'gplus';
122 function appnetpost_send(&$a,&$b) {
124 logger('appnetpost_send: invoked for post '.$b['id']." ".$b['app']);
126 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
129 if(! strstr($b['postopts'],'gplus'))
132 if($b['parent'] != $b['id'])
135 $itemlist = get_pconfig($b["uid"],'appnetpost','itemlist');
136 $items = explode(",", $itemlist);
139 $newitems = array($b['id']);
140 foreach ($items AS $item)
144 $itemlist = implode(",", $newitems);
146 logger('appnetpost_send: new itemlist: '.$itemlist." for uid ".$b["uid"]);
148 set_pconfig($b["uid"],'appnetpost','itemlist', $itemlist);
151 function appnetpost_module() {}
153 function appnetpost_init() {
158 if (isset($a->argv[1])) {
159 $uid = (int)$a->argv[1];
161 $contacts = q("SELECT `username`, `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($a->argv[1]));
163 $uid = $contacts[0]["uid"];
167 $contacts = q("SELECT `username` FROM `user` WHERE `uid`=%d LIMIT 1", intval($uid));
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";
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";
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";
188 $itemlist = get_pconfig($uid,'appnetpost','itemlist');
189 $items = explode(",", $itemlist);
191 foreach ($items AS $item)
192 appnetpost_feeditem($item, $uid);
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);
202 function appnetpost_feeditem($pid, $uid) {
205 require_once('include/bbcode.php');
206 require_once("include/html2plain.php");
207 require_once("include/network.php");
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) {
212 $item['body'] = bb_CleanPictureLinks($item['body']);
214 // Looking for the first image
216 if(preg_match("/\[img\=([0-9]*)x([0-9]*)\](.*?)\[\/img\]/is",$item['body'],$matches))
217 $image = $matches[3];
220 if(preg_match("/\[img\](.*?)\[\/img\]/is",$item['body'],$matches))
221 $image = $matches[1];
223 $multipleimages = (strpos($item['body'], "[img") != strrpos($item['body'], "[img"));
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);
230 // look for bookmark-bbcode and handle it with priority
231 if(preg_match("/\[bookmark\=([^\]]*)\](.*?)\[\/bookmark\]/is",$item['body'],$matches))
234 $multiplelinks = (strpos($item['body'], "[bookmark") != strrpos($item['body'], "[bookmark"));
236 $body = $item['body'];
238 // At first convert the text to html
239 $html = bbcode($body, false, false, 2);
241 // Then convert it to plain text
242 $msg = trim(html2plain($html, 0, true));
243 $msg = html_entity_decode($msg,ENT_QUOTES,'UTF-8');
245 // If there is no bookmark element then take the first link
247 $links = collecturls($html);
248 if (sizeof($links) > 0) {
250 $link = current($links);
252 $multiplelinks = (sizeof($links) > 1);
254 if ($multiplelinks) {
255 $html2 = bbcode($msg, false, false);
256 $links2 = collecturls($html2);
257 if (sizeof($links2) > 0) {
259 $link = current($links2);
260 $multiplelinks = (sizeof($links2) > 1);
267 $msglink = $item["plink"];
268 else if ($link != "")
270 else if ($multipleimages)
271 $msglink = $item["plink"];
272 else if ($image != "")
275 // Fetching the title and add all lines
276 if ($item["title"] != "")
277 $title = $item["title"];
279 $lines = explode("\n", $msg);
280 foreach ($lines AS $line)
281 $title .= "\n".$line;
285 $origlink = $msglink;
287 if (strlen($msglink) > 20)
288 $msglink = short_link($msglink);
290 $title = trim(str_replace($origlink, $msglink, $title));
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");
298 $title = substr($title, 0, $pos);
299 else if ($lastchar != "\n")
300 $title = substr($title, 0, -3)."...";
303 if (($msglink != "") AND !strstr($title, $msglink))
304 $title = trim($title." ".$msglink);
306 $title = trim($title);
311 //$origlink = original_url($origlink);
313 $html = nl2br($title);
315 $origlink = $item["plink"];
316 $origlink = htmlspecialchars(html_entity_decode($origlink));
318 $title = str_replace("&", "&", $title);
319 //$html = str_replace("&", "&", $html);
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";