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\Logger;
13 use Friendica\Database\DBA;
15 use Friendica\Network\HTTPRequest;
17 function libertree_install()
19 Hook::register('hook_fork', 'addon/libertree/libertree.php', 'libertree_hook_fork');
20 Hook::register('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
21 Hook::register('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
22 Hook::register('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
23 Hook::register('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
24 Hook::register('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
27 function libertree_uninstall()
29 Hook::unregister('hook_fork', 'addon/libertree/libertree.php', 'libertree_hook_fork');
30 Hook::unregister('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
31 Hook::unregister('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
32 Hook::unregister('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
33 Hook::unregister('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
34 Hook::unregister('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
37 function libertree_jot_nets(App &$a, array &$jotnets_fields)
43 if (DI::pConfig()->get(local_user(), 'libertree', 'post')) {
48 DI::l10n()->t('Post to libertree'),
49 DI::pConfig()->get(local_user(), 'libertree', 'post_by_default')
56 function libertree_settings(&$a,&$s) {
61 /* Add our stylesheet to the page so we can make our settings look nice */
63 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/libertree/libertree.css' . '" media="all" />' . "\r\n";
65 /* Get the current state of our config variables */
67 $enabled = DI::pConfig()->get(local_user(),'libertree','post');
68 $checked = (($enabled) ? ' checked="checked" ' : '');
69 $css = (($enabled) ? '' : '-disabled');
71 $def_enabled = DI::pConfig()->get(local_user(),'libertree','post_by_default');
73 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
75 $ltree_api_token = DI::pConfig()->get(local_user(), 'libertree', 'libertree_api_token');
76 $ltree_url = DI::pConfig()->get(local_user(), 'libertree', 'libertree_url');
79 /* Add some HTML to the existing form */
81 $s .= '<span id="settings_libertree_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
82 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. DI::l10n()->t('libertree Export').'</h3>';
84 $s .= '<div id="settings_libertree_expanded" class="settings-block" style="display: none;">';
85 $s .= '<span class="fakelink" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
86 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. DI::l10n()->t('libertree Export').'</h3>';
89 $s .= '<div id="libertree-enable-wrapper">';
90 $s .= '<label id="libertree-enable-label" for="libertree-checkbox">' . DI::l10n()->t('Enable Libertree Post Addon') . '</label>';
91 $s .= '<input id="libertree-checkbox" type="checkbox" name="libertree" value="1" ' . $checked . '/>';
92 $s .= '</div><div class="clear"></div>';
94 $s .= '<div id="libertree-api_token-wrapper">';
95 $s .= '<label id="libertree-api_token-label" for="libertree-api_token">' . DI::l10n()->t('Libertree API token') . '</label>';
96 $s .= '<input id="libertree-api_token" type="text" name="libertree_api_token" value="' . $ltree_api_token . '" />';
97 $s .= '</div><div class="clear"></div>';
99 $s .= '<div id="libertree-url-wrapper">';
100 $s .= '<label id="libertree-url-label" for="libertree-url">' . DI::l10n()->t('Libertree site URL') . '</label>';
101 $s .= '<input id="libertree-url" type="text" name="libertree_url" value="' . $ltree_url . '" />';
102 $s .= '</div><div class="clear"></div>';
104 $s .= '<div id="libertree-bydefault-wrapper">';
105 $s .= '<label id="libertree-bydefault-label" for="libertree-bydefault">' . DI::l10n()->t('Post to Libertree by default') . '</label>';
106 $s .= '<input id="libertree-bydefault" type="checkbox" name="libertree_bydefault" value="1" ' . $def_checked . '/>';
107 $s .= '</div><div class="clear"></div>';
109 /* provide a submit button */
111 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="libertree-submit" name="libertree-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
116 function libertree_settings_post(&$a,&$b) {
118 if(!empty($_POST['libertree-submit'])) {
120 DI::pConfig()->set(local_user(),'libertree','post',intval($_POST['libertree']));
121 DI::pConfig()->set(local_user(),'libertree','post_by_default',intval($_POST['libertree_bydefault']));
122 DI::pConfig()->set(local_user(),'libertree','libertree_api_token',trim($_POST['libertree_api_token']));
123 DI::pConfig()->set(local_user(),'libertree','libertree_url',trim($_POST['libertree_url']));
129 function libertree_hook_fork(App &$a, array &$b)
131 if ($b['name'] != 'notifier_normal') {
137 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
138 !strstr($post['postopts'], 'libertree') || ($post['parent'] != $post['id'])) {
139 $b['execute'] = false;
144 function libertree_post_local(&$a,&$b) {
146 // This can probably be changed to allow editing by pointing to a different API endpoint
152 if ((! local_user()) || (local_user() != $b['uid'])) {
156 if ($b['private'] || $b['parent']) {
160 $ltree_post = intval(DI::pConfig()->get(local_user(),'libertree','post'));
162 $ltree_enable = (($ltree_post && !empty($_REQUEST['libertree_enable'])) ? intval($_REQUEST['libertree_enable']) : 0);
164 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'libertree','post_by_default'))) {
168 if (!$ltree_enable) {
172 if (strlen($b['postopts'])) {
173 $b['postopts'] .= ',';
176 $b['postopts'] .= 'libertree';
182 function libertree_send(&$a,&$b) {
184 Logger::log('libertree_send: invoked');
186 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
190 if (! strstr($b['postopts'],'libertree')) {
194 if ($b['parent'] != $b['id']) {
198 // Dont't post if the post doesn't belong to us.
199 // This is a check for forum postings
200 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
201 if ($b['contact-id'] != $self['id']) {
205 $ltree_api_token = DI::pConfig()->get($b['uid'],'libertree','libertree_api_token');
206 $ltree_url = DI::pConfig()->get($b['uid'],'libertree','libertree_url');
207 $ltree_blog = "$ltree_url/api/v1/posts/create/?token=$ltree_api_token";
208 $ltree_source = DI::baseUrl()->getHostname();
211 $ltree_source .= " (".$b['app'].")";
213 if($ltree_url && $ltree_api_token && $ltree_blog && $ltree_source) {
214 $title = $b['title'];
216 // Insert a newline before and after a quote
217 $body = str_ireplace("[quote", "\n\n[quote", $body);
218 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
220 // Removal of tags and mentions
222 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
224 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
226 // remove multiple newlines
229 $body = str_replace("\n\n\n", "\n\n", $body);
230 } while ($oldbody != $body);
232 // convert to markdown
233 $body = BBCode::toMarkdown($body, false);
237 $body = "## ".html_entity_decode($title)."\n\n".$body;
242 'source' => $ltree_source
243 // 'token' => $ltree_api_token
246 $result = HTTPRequest::post($ltree_blog, $params)->getBody();
247 Logger::log('libertree: ' . $result);