3 * Name: Libertree Post Connector
4 * Description: Post to libertree accounts
6 * Author: Tony Baldwin <https://free-haven.org/u/tony>
10 use Friendica\Content\Text\BBCode;
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Database\DBA;
17 use Friendica\Util\Network;
19 function libertree_install()
21 Hook::register('hook_fork', 'addon/libertree/libertree.php', 'libertree_hook_fork');
22 Hook::register('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
23 Hook::register('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
24 Hook::register('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
25 Hook::register('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
26 Hook::register('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
29 function libertree_uninstall()
31 Hook::unregister('hook_fork', 'addon/libertree/libertree.php', 'libertree_hook_fork');
32 Hook::unregister('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
33 Hook::unregister('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
34 Hook::unregister('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
35 Hook::unregister('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
36 Hook::unregister('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
39 function libertree_jot_nets(App &$a, array &$jotnets_fields)
45 if (DI::pConfig()->get(local_user(), 'libertree', 'post')) {
50 L10n::t('Post to libertree'),
51 DI::pConfig()->get(local_user(), 'libertree', 'post_by_default')
58 function libertree_settings(&$a,&$s) {
63 /* Add our stylesheet to the page so we can make our settings look nice */
65 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/libertree/libertree.css' . '" media="all" />' . "\r\n";
67 /* Get the current state of our config variables */
69 $enabled = DI::pConfig()->get(local_user(),'libertree','post');
70 $checked = (($enabled) ? ' checked="checked" ' : '');
71 $css = (($enabled) ? '' : '-disabled');
73 $def_enabled = DI::pConfig()->get(local_user(),'libertree','post_by_default');
75 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
77 $ltree_api_token = DI::pConfig()->get(local_user(), 'libertree', 'libertree_api_token');
78 $ltree_url = DI::pConfig()->get(local_user(), 'libertree', 'libertree_url');
81 /* Add some HTML to the existing form */
83 $s .= '<span id="settings_libertree_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
84 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. L10n::t('libertree Export').'</h3>';
86 $s .= '<div id="settings_libertree_expanded" class="settings-block" style="display: none;">';
87 $s .= '<span class="fakelink" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
88 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. L10n::t('libertree Export').'</h3>';
91 $s .= '<div id="libertree-enable-wrapper">';
92 $s .= '<label id="libertree-enable-label" for="libertree-checkbox">' . L10n::t('Enable Libertree Post Addon') . '</label>';
93 $s .= '<input id="libertree-checkbox" type="checkbox" name="libertree" value="1" ' . $checked . '/>';
94 $s .= '</div><div class="clear"></div>';
96 $s .= '<div id="libertree-api_token-wrapper">';
97 $s .= '<label id="libertree-api_token-label" for="libertree-api_token">' . L10n::t('Libertree API token') . '</label>';
98 $s .= '<input id="libertree-api_token" type="text" name="libertree_api_token" value="' . $ltree_api_token . '" />';
99 $s .= '</div><div class="clear"></div>';
101 $s .= '<div id="libertree-url-wrapper">';
102 $s .= '<label id="libertree-url-label" for="libertree-url">' . L10n::t('Libertree site URL') . '</label>';
103 $s .= '<input id="libertree-url" type="text" name="libertree_url" value="' . $ltree_url . '" />';
104 $s .= '</div><div class="clear"></div>';
106 $s .= '<div id="libertree-bydefault-wrapper">';
107 $s .= '<label id="libertree-bydefault-label" for="libertree-bydefault">' . L10n::t('Post to Libertree by default') . '</label>';
108 $s .= '<input id="libertree-bydefault" type="checkbox" name="libertree_bydefault" value="1" ' . $def_checked . '/>';
109 $s .= '</div><div class="clear"></div>';
111 /* provide a submit button */
113 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="libertree-submit" name="libertree-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
118 function libertree_settings_post(&$a,&$b) {
120 if(!empty($_POST['libertree-submit'])) {
122 DI::pConfig()->set(local_user(),'libertree','post',intval($_POST['libertree']));
123 DI::pConfig()->set(local_user(),'libertree','post_by_default',intval($_POST['libertree_bydefault']));
124 DI::pConfig()->set(local_user(),'libertree','libertree_api_token',trim($_POST['libertree_api_token']));
125 DI::pConfig()->set(local_user(),'libertree','libertree_url',trim($_POST['libertree_url']));
131 function libertree_hook_fork(App &$a, array &$b)
133 if ($b['name'] != 'notifier_normal') {
139 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
140 !strstr($post['postopts'], 'libertree') || ($post['parent'] != $post['id'])) {
141 $b['execute'] = false;
146 function libertree_post_local(&$a,&$b) {
148 // This can probably be changed to allow editing by pointing to a different API endpoint
154 if ((! local_user()) || (local_user() != $b['uid'])) {
158 if ($b['private'] || $b['parent']) {
162 $ltree_post = intval(DI::pConfig()->get(local_user(),'libertree','post'));
164 $ltree_enable = (($ltree_post && !empty($_REQUEST['libertree_enable'])) ? intval($_REQUEST['libertree_enable']) : 0);
166 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'libertree','post_by_default'))) {
170 if (!$ltree_enable) {
174 if (strlen($b['postopts'])) {
175 $b['postopts'] .= ',';
178 $b['postopts'] .= 'libertree';
184 function libertree_send(&$a,&$b) {
186 Logger::log('libertree_send: invoked');
188 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
192 if (! strstr($b['postopts'],'libertree')) {
196 if ($b['parent'] != $b['id']) {
200 // Dont't post if the post doesn't belong to us.
201 // This is a check for forum postings
202 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
203 if ($b['contact-id'] != $self['id']) {
207 $ltree_api_token = DI::pConfig()->get($b['uid'],'libertree','libertree_api_token');
208 $ltree_url = DI::pConfig()->get($b['uid'],'libertree','libertree_url');
209 $ltree_blog = "$ltree_url/api/v1/posts/create/?token=$ltree_api_token";
210 $ltree_source = DI::baseUrl()->getHostname();
213 $ltree_source .= " (".$b['app'].")";
215 if($ltree_url && $ltree_api_token && $ltree_blog && $ltree_source) {
218 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
221 foreach($matches as $mtch) {
222 $tag_arr[] = $mtch[2];
226 $tags = implode(',',$tag_arr);
228 $title = $b['title'];
230 // Insert a newline before and after a quote
231 $body = str_ireplace("[quote", "\n\n[quote", $body);
232 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
234 // Removal of tags and mentions
236 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
238 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
240 // remove multiple newlines
243 $body = str_replace("\n\n\n", "\n\n", $body);
244 } while ($oldbody != $body);
246 // convert to markdown
247 $body = BBCode::toMarkdown($body, false);
251 $body = "## ".html_entity_decode($title)."\n\n".$body;
256 'source' => $ltree_source
257 // 'token' => $ltree_api_token
260 $result = Network::post($ltree_blog, $params)->getBody();
261 Logger::log('libertree: ' . $result);