The source name now includes the application name where the post comes from.
[friendica-addons.git] / libertree / libertree.php
1 <?php
2
3 /**
4  * Name: Libertree Post Connector
5  * Description: Post to libertree accounts
6  * Version: 1.0
7  * Author: Tony Baldwin <https://free-haven.org/u/tony>
8  */
9
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');
16
17 }
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');
24 }
25
26
27 function libertree_jot_nets(&$a,&$b) {
28     if(! local_user())
29         return;
30
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>';
37     }
38 }
39
40
41 function libertree_settings(&$a,&$s) {
42
43     if(! local_user())
44         return;
45
46     /* Add our stylesheet to the page so we can make our settings look nice */
47
48     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/libertree/libertree.css' . '" media="all" />' . "\r\n";
49
50     /* Get the current state of our config variables */
51
52     $enabled = get_pconfig(local_user(),'libertree','post');
53     $checked = (($enabled) ? ' checked="checked" ' : '');
54     $css = (($enabled) ? '' : '-disabled');
55
56     $def_enabled = get_pconfig(local_user(),'libertree','post_by_default');
57
58     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
59
60     $ltree_api_token = get_pconfig(local_user(), 'libertree', 'libertree_api_token');
61     $ltree_url = get_pconfig(local_user(), 'libertree', 'libertree_url');
62
63
64     /* Add some HTML to the existing form */
65
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>';
68     $s .= '</span>';
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>';
72     $s .= '</span>';
73
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>';
78
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>';
83
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>';
88
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>';
93
94     /* provide a submit button */
95
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>';
97
98 }
99
100
101 function libertree_settings_post(&$a,&$b) {
102
103         if(x($_POST,'libertree-submit')) {
104
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']));
109
110         }
111
112 }
113
114 function libertree_post_local(&$a,&$b) {
115
116         // This can probably be changed to allow editing by pointing to a different API endpoint
117
118         if($b['edit'])
119                 return;
120
121         if((! local_user()) || (local_user() != $b['uid']))
122                 return;
123
124         if($b['private'] || $b['parent'])
125                 return;
126
127         $ltree_post   = intval(get_pconfig(local_user(),'libertree','post'));
128
129         $ltree_enable = (($ltree_post && x($_REQUEST,'libertree_enable')) ? intval($_REQUEST['libertree_enable']) : 0);
130
131         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'libertree','post_by_default')))
132                 $ltree_enable = 1;
133
134     if(! $ltree_enable)
135        return;
136
137     if(strlen($b['postopts']))
138        $b['postopts'] .= ',';
139      $b['postopts'] .= 'libertree';
140 }
141
142
143
144
145 function libertree_send(&$a,&$b) {
146
147         logger('libertree_send: invoked');
148
149     if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
150         return;
151
152     if(! strstr($b['postopts'],'libertree'))
153         return;
154
155     if($b['parent'] != $b['id'])
156         return;
157
158
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         $ltree_source = $a->get_hostname();
163
164         if ($b['app'] != "")
165                 $ltree_source .= " (".$b['app'].")";
166
167         if($ltree_url && $ltree_api_token && $ltree_blog && $ltree_source) {
168
169                 require_once('include/bb2diaspora.php');
170                 $tag_arr = array();
171                 $tags = '';
172                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
173
174                 if($x) {
175                         foreach($matches as $mtch) {
176                                 $tag_arr[] = $mtch[2];
177                         }
178                 }
179                 if(count($tag_arr))
180                         $tags = implode(',',$tag_arr);
181
182                 $title = $b['title'];
183                 $body = $b['body'];
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);
187
188                 // Removal of tags and mentions
189                 // #-tags
190                 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
191                 // @-mentions
192                 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
193
194                 // remove multiple newlines
195                 do {
196                         $oldbody = $body;
197                         $body = str_replace("\n\n\n", "\n\n", $body);
198                 } while ($oldbody != $body);
199
200                 // convert to markdown
201                 $body = bb2diaspora($body, false, false);
202
203                 // Adding the title
204                 if(strlen($title))
205                         $body = "## ".html_entity_decode($title)."\n\n".$body;
206
207
208                 $params = array(
209                         'text' => $body,
210                         'source' => $ltree_source
211                 //      'token' => $ltree_api_token
212                 );
213
214                 $result = post_url($ltree_blog,$params);
215                 logger('libertree: ' . $result);
216
217         }
218 }
219