4 * Name: Libertree Post Connector
5 * Description: Post to libertree accounts
7 * Author: Tony Baldwin <https://free-haven.org/u/tony>
10 function libertree_install() {
11 register_hook('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
12 register_hook('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
13 register_hook('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
14 register_hook('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
15 register_hook('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
18 function libertree_uninstall() {
19 unregister_hook('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
20 unregister_hook('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
21 unregister_hook('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
22 unregister_hook('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
23 unregister_hook('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
27 function libertree_jot_nets(&$a,&$b) {
31 $ltree_post = get_pconfig(local_user(),'libertree','post');
32 if(intval($ltree_post) == 1) {
33 $ltree_defpost = get_pconfig(local_user(),'libertree','post_by_default');
34 $selected = ((intval($ltree_defpost) == 1) ? ' checked="checked" ' : '');
35 $b .= '<div class="profile-jot-net"><input type="checkbox" name="libertree_enable"' . $selected . ' value="1" /> '
36 . t('Post to libertree') . '</div>';
41 function libertree_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/libertree/libertree.css' . '" media="all" />' . "\r\n";
50 /* Get the current state of our config variables */
52 $enabled = get_pconfig(local_user(),'libertree','post');
53 $checked = (($enabled) ? ' checked="checked" ' : '');
54 $css = (($enabled) ? '' : '-disabled');
56 $def_enabled = get_pconfig(local_user(),'libertree','post_by_default');
58 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
60 $ltree_api_token = get_pconfig(local_user(), 'libertree', 'libertree_api_token');
61 $ltree_url = get_pconfig(local_user(), 'libertree', 'libertree_url');
64 /* Add some HTML to the existing form */
66 $s .= '<span id="settings_libertree_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
67 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. t('libertree Export').'</h3>';
69 $s .= '<div id="settings_libertree_expanded" class="settings-block" style="display: none;">';
70 $s .= '<span class="fakelink" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
71 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. t('libertree Export').'</h3>';
74 $s .= '<div id="libertree-enable-wrapper">';
75 $s .= '<label id="libertree-enable-label" for="libertree-checkbox">' . t('Enable Libertree Post Plugin') . '</label>';
76 $s .= '<input id="libertree-checkbox" type="checkbox" name="libertree" value="1" ' . $checked . '/>';
77 $s .= '</div><div class="clear"></div>';
79 $s .= '<div id="libertree-api_token-wrapper">';
80 $s .= '<label id="libertree-api_token-label" for="libertree-api_token">' . t('Libertree API token') . '</label>';
81 $s .= '<input id="libertree-api_token" type="text" name="libertree_api_token" value="' . $ltree_api_token . '" />';
82 $s .= '</div><div class="clear"></div>';
84 $s .= '<div id="libertree-url-wrapper">';
85 $s .= '<label id="libertree-url-label" for="libertree-url">' . t('Libertree site URL') . '</label>';
86 $s .= '<input id="libertree-url" type="text" name="libertree_url" value="' . $ltree_url . '" />';
87 $s .= '</div><div class="clear"></div>';
89 $s .= '<div id="libertree-bydefault-wrapper">';
90 $s .= '<label id="libertree-bydefault-label" for="libertree-bydefault">' . t('Post to Libertree by default') . '</label>';
91 $s .= '<input id="libertree-bydefault" type="checkbox" name="libertree_bydefault" value="1" ' . $def_checked . '/>';
92 $s .= '</div><div class="clear"></div>';
94 /* provide a submit button */
96 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="libertree-submit" name="libertree-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
101 function libertree_settings_post(&$a,&$b) {
103 if(x($_POST,'libertree-submit')) {
105 set_pconfig(local_user(),'libertree','post',intval($_POST['libertree']));
106 set_pconfig(local_user(),'libertree','post_by_default',intval($_POST['libertree_bydefault']));
107 set_pconfig(local_user(),'libertree','libertree_api_token',trim($_POST['libertree_api_token']));
108 set_pconfig(local_user(),'libertree','libertree_url',trim($_POST['libertree_url']));
114 function libertree_post_local(&$a,&$b) {
116 // This can probably be changed to allow editing by pointing to a different API endpoint
121 if((! local_user()) || (local_user() != $b['uid']))
124 if($b['private'] || $b['parent'])
127 $ltree_post = intval(get_pconfig(local_user(),'libertree','post'));
129 $ltree_enable = (($ltree_post && x($_REQUEST,'libertree_enable')) ? intval($_REQUEST['libertree_enable']) : 0);
131 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'libertree','post_by_default')))
137 if(strlen($b['postopts']))
138 $b['postopts'] .= ',';
139 $b['postopts'] .= 'libertree';
145 function libertree_send(&$a,&$b) {
147 logger('libertree_send: invoked');
149 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
152 if(! strstr($b['postopts'],'libertree'))
155 if($b['parent'] != $b['id'])
159 $ltree_api_token = get_pconfig($b['uid'],'libertree','libertree_api_token');
160 $ltree_url = get_pconfig($b['uid'],'libertree','libertree_url');
161 $ltree_blog = "$ltree_url/api/v1/posts/create/?token=$ltree_api_token";
162 if (isset($a->config['sitename']) AND ($a->config['sitename'] != ""))
163 $ltree_source = $a->config['sitename'];
165 $ltree_source = "Friendica";
167 if($ltree_url && $ltree_api_token && $ltree_blog && $ltree_source) {
169 require_once('include/bb2diaspora.php');
172 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
175 foreach($matches as $mtch) {
176 $tag_arr[] = $mtch[2];
180 $tags = implode(',',$tag_arr);
182 $title = $b['title'];
184 // Insert a newline before and after a quote
185 $body = str_ireplace("[quote", "\n\n[quote", $body);
186 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
188 // Removal of tags and mentions
190 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
192 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
194 // remove multiple newlines
197 $body = str_replace("\n\n\n", "\n\n", $body);
198 } while ($oldbody != $body);
200 // convert to markdown
201 $body = bb2diaspora($body, false, false);
205 $body = "## ".html_entity_decode($title)."\n\n".$body;
210 'source' => $ltree_source
211 // 'token' => $ltree_api_token
214 $result = post_url($ltree_blog,$params);
215 logger('libertree: ' . $result);