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\Addon;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Database\DBA;
16 use Friendica\Util\Network;
18 function libertree_install()
20 Addon::registerHook('hook_fork', 'addon/libertree/libertree.php', 'libertree_hook_fork');
21 Addon::registerHook('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
22 Addon::registerHook('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
23 Addon::registerHook('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
24 Addon::registerHook('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
25 Addon::registerHook('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
28 function libertree_uninstall()
30 Addon::unregisterHook('hook_fork', 'addon/libertree/libertree.php', 'libertree_hook_fork');
31 Addon::unregisterHook('post_local', 'addon/libertree/libertree.php', 'libertree_post_local');
32 Addon::unregisterHook('notifier_normal', 'addon/libertree/libertree.php', 'libertree_send');
33 Addon::unregisterHook('jot_networks', 'addon/libertree/libertree.php', 'libertree_jot_nets');
34 Addon::unregisterHook('connector_settings', 'addon/libertree/libertree.php', 'libertree_settings');
35 Addon::unregisterHook('connector_settings_post', 'addon/libertree/libertree.php', 'libertree_settings_post');
38 function libertree_jot_nets(&$a,&$b) {
42 $ltree_post = PConfig::get(local_user(),'libertree','post');
43 if(intval($ltree_post) == 1) {
44 $ltree_defpost = PConfig::get(local_user(),'libertree','post_by_default');
45 $selected = ((intval($ltree_defpost) == 1) ? ' checked="checked" ' : '');
46 $b .= '<div class="profile-jot-net"><input type="checkbox" name="libertree_enable"' . $selected . ' value="1" /> '
47 . L10n::t('Post to libertree') . '</div>';
52 function libertree_settings(&$a,&$s) {
57 /* Add our stylesheet to the page so we can make our settings look nice */
59 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/libertree/libertree.css' . '" media="all" />' . "\r\n";
61 /* Get the current state of our config variables */
63 $enabled = PConfig::get(local_user(),'libertree','post');
64 $checked = (($enabled) ? ' checked="checked" ' : '');
65 $css = (($enabled) ? '' : '-disabled');
67 $def_enabled = PConfig::get(local_user(),'libertree','post_by_default');
69 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
71 $ltree_api_token = PConfig::get(local_user(), 'libertree', 'libertree_api_token');
72 $ltree_url = PConfig::get(local_user(), 'libertree', 'libertree_url');
75 /* Add some HTML to the existing form */
77 $s .= '<span id="settings_libertree_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
78 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. L10n::t('libertree Export').'</h3>';
80 $s .= '<div id="settings_libertree_expanded" class="settings-block" style="display: none;">';
81 $s .= '<span class="fakelink" onclick="openClose(\'settings_libertree_expanded\'); openClose(\'settings_libertree_inflated\');">';
82 $s .= '<img class="connector'.$css.'" src="images/libertree.png" /><h3 class="connector">'. L10n::t('libertree Export').'</h3>';
85 $s .= '<div id="libertree-enable-wrapper">';
86 $s .= '<label id="libertree-enable-label" for="libertree-checkbox">' . L10n::t('Enable Libertree Post Addon') . '</label>';
87 $s .= '<input id="libertree-checkbox" type="checkbox" name="libertree" value="1" ' . $checked . '/>';
88 $s .= '</div><div class="clear"></div>';
90 $s .= '<div id="libertree-api_token-wrapper">';
91 $s .= '<label id="libertree-api_token-label" for="libertree-api_token">' . L10n::t('Libertree API token') . '</label>';
92 $s .= '<input id="libertree-api_token" type="text" name="libertree_api_token" value="' . $ltree_api_token . '" />';
93 $s .= '</div><div class="clear"></div>';
95 $s .= '<div id="libertree-url-wrapper">';
96 $s .= '<label id="libertree-url-label" for="libertree-url">' . L10n::t('Libertree site URL') . '</label>';
97 $s .= '<input id="libertree-url" type="text" name="libertree_url" value="' . $ltree_url . '" />';
98 $s .= '</div><div class="clear"></div>';
100 $s .= '<div id="libertree-bydefault-wrapper">';
101 $s .= '<label id="libertree-bydefault-label" for="libertree-bydefault">' . L10n::t('Post to Libertree by default') . '</label>';
102 $s .= '<input id="libertree-bydefault" type="checkbox" name="libertree_bydefault" value="1" ' . $def_checked . '/>';
103 $s .= '</div><div class="clear"></div>';
105 /* provide a submit button */
107 $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>';
112 function libertree_settings_post(&$a,&$b) {
114 if(x($_POST,'libertree-submit')) {
116 PConfig::set(local_user(),'libertree','post',intval($_POST['libertree']));
117 PConfig::set(local_user(),'libertree','post_by_default',intval($_POST['libertree_bydefault']));
118 PConfig::set(local_user(),'libertree','libertree_api_token',trim($_POST['libertree_api_token']));
119 PConfig::set(local_user(),'libertree','libertree_url',trim($_POST['libertree_url']));
125 function libertree_hook_fork(App &$a, array &$b)
127 if ($b['name'] != 'notifier_normal') {
133 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
134 !strstr($post['postopts'], 'libertree') || ($post['parent'] != $post['id'])) {
135 $b['execute'] = false;
140 function libertree_post_local(&$a,&$b) {
142 // This can probably be changed to allow editing by pointing to a different API endpoint
148 if ((! local_user()) || (local_user() != $b['uid'])) {
152 if ($b['private'] || $b['parent']) {
156 $ltree_post = intval(PConfig::get(local_user(),'libertree','post'));
158 $ltree_enable = (($ltree_post && x($_REQUEST,'libertree_enable')) ? intval($_REQUEST['libertree_enable']) : 0);
160 if ($b['api_source'] && intval(PConfig::get(local_user(),'libertree','post_by_default'))) {
164 if (!$ltree_enable) {
168 if (strlen($b['postopts'])) {
169 $b['postopts'] .= ',';
172 $b['postopts'] .= 'libertree';
178 function libertree_send(&$a,&$b) {
180 Logger::log('libertree_send: invoked');
182 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
186 if (! strstr($b['postopts'],'libertree')) {
190 if ($b['parent'] != $b['id']) {
194 // Dont't post if the post doesn't belong to us.
195 // This is a check for forum postings
196 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
197 if ($b['contact-id'] != $self['id']) {
201 $ltree_api_token = PConfig::get($b['uid'],'libertree','libertree_api_token');
202 $ltree_url = PConfig::get($b['uid'],'libertree','libertree_url');
203 $ltree_blog = "$ltree_url/api/v1/posts/create/?token=$ltree_api_token";
204 $ltree_source = $a->getHostName();
207 $ltree_source .= " (".$b['app'].")";
209 if($ltree_url && $ltree_api_token && $ltree_blog && $ltree_source) {
212 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
215 foreach($matches as $mtch) {
216 $tag_arr[] = $mtch[2];
220 $tags = implode(',',$tag_arr);
222 $title = $b['title'];
224 // Insert a newline before and after a quote
225 $body = str_ireplace("[quote", "\n\n[quote", $body);
226 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
228 // Removal of tags and mentions
230 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
232 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
234 // remove multiple newlines
237 $body = str_replace("\n\n\n", "\n\n", $body);
238 } while ($oldbody != $body);
240 // convert to markdown
241 $body = BBCode::toMarkdown($body, false);
245 $body = "## ".html_entity_decode($title)."\n\n".$body;
250 'source' => $ltree_source
251 // 'token' => $ltree_api_token
254 $result = Network::post($ltree_blog, $params)->getBody();
255 Logger::log('libertree: ' . $result);