]> git.mxchange.org Git - friendica-addons.git/blob - diaspora/diaspora.php
25c6777caa40603b908e20e0be6d53e949008031
[friendica-addons.git] / diaspora / diaspora.php
1 <?php
2
3 /**
4  * Name: Diaspora Post Connector
5  * Description: Post to Diaspora
6  * Version: 0.2
7  * Author: Michael Vogel <heluecht@pirati.ca>
8  */
9
10 require_once 'addon/diaspora/Diaspora_Connection.php';
11
12 use Friendica\App;
13 use Friendica\Content\Text\BBCode;
14 use Friendica\Core\Hook;
15 use Friendica\Core\L10n;
16 use Friendica\Core\Logger;
17 use Friendica\Database\DBA;
18 use Friendica\Core\Worker;
19 use Friendica\DI;
20
21 function diaspora_install()
22 {
23         Hook::register('hook_fork',               'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
24         Hook::register('post_local',              'addon/diaspora/diaspora.php', 'diaspora_post_local');
25         Hook::register('notifier_normal',         'addon/diaspora/diaspora.php', 'diaspora_send');
26         Hook::register('jot_networks',            'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
27         Hook::register('connector_settings',      'addon/diaspora/diaspora.php', 'diaspora_settings');
28         Hook::register('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
29 }
30
31 function diaspora_uninstall()
32 {
33         Hook::unregister('hook_fork',               'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
34         Hook::unregister('post_local',              'addon/diaspora/diaspora.php', 'diaspora_post_local');
35         Hook::unregister('notifier_normal',         'addon/diaspora/diaspora.php', 'diaspora_send');
36         Hook::unregister('jot_networks',            'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
37         Hook::unregister('connector_settings',      'addon/diaspora/diaspora.php', 'diaspora_settings');
38         Hook::unregister('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
39 }
40
41 function diaspora_jot_nets(App $a, array &$jotnets_fields)
42 {
43         if (!local_user()) {
44                 return;
45         }
46
47         if (DI::pConfig()->get(local_user(), 'diaspora', 'post')) {
48                 $jotnets_fields[] = [
49                         'type' => 'checkbox',
50                         'field' => [
51                                 'diaspora_enable',
52                                 DI::l10n()->t('Post to Diaspora'),
53                                 DI::pConfig()->get(local_user(), 'diaspora', 'post_by_default')
54                         ]
55                 ];
56         }
57 }
58
59 function diaspora_settings(App $a, &$s)
60 {
61         if (! local_user()) {
62                 return;
63         }
64
65         /* Add our stylesheet to the page so we can make our settings look nice */
66
67         DI::page()['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . DI::baseUrl()->get() . '/addon/diaspora/diaspora.css' . '" media="all" />' . "\r\n";
68
69         /* Get the current state of our config variables */
70
71         $enabled = DI::pConfig()->get(local_user(),'diaspora','post');
72         $checked = (($enabled) ? ' checked="checked" ' : '');
73         $css = (($enabled) ? '' : '-disabled');
74
75         $def_enabled = DI::pConfig()->get(local_user(),'diaspora','post_by_default');
76
77         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
78
79         $handle = DI::pConfig()->get(local_user(), 'diaspora', 'handle');
80         $password = DI::pConfig()->get(local_user(), 'diaspora', 'password');
81         $aspect = DI::pConfig()->get(local_user(),'diaspora','aspect');
82
83         $status = "";
84
85         $r = q("SELECT `addr` FROM `contact` WHERE `self` AND `uid` = %d", intval(local_user()));
86
87         if (DBA::isResult($r)) {
88                 $status = DI::l10n()->t("Please remember: You can always be reached from Diaspora with your Friendica handle %s. ", $r[0]['addr']);
89                 $status .= DI::l10n()->t('This connector is only meant if you still want to use your old Diaspora account for some time. ');
90                 $status .= DI::l10n()->t('However, it is preferred that you tell your Diaspora contacts the new handle %s instead.', $r[0]['addr']);
91         }
92
93         $aspects = false;
94
95         if ($handle && $password) {
96                 $conn = new Diaspora_Connection($handle, $password);
97                 $conn->logIn();
98                 $aspects = $conn->getAspects();
99
100                 if (!$aspects) {
101                         $status = DI::l10n()->t("Can't login to your Diaspora account. Please check handle (in the format user@domain.tld) and password.");
102                 }
103         }
104
105         /* Add some HTML to the existing form */
106
107         $s .= '<span id="settings_diaspora_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
108         $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. DI::l10n()->t('Diaspora Export').'</h3>';
109         $s .= '</span>';
110         $s .= '<div id="settings_diaspora_expanded" class="settings-block" style="display: none;">';
111         $s .= '<span class="fakelink" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
112         $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. DI::l10n()->t('Diaspora Export').'</h3>';
113         $s .= '</span>';
114
115         if ($status) {
116                 $s .= '<div id="diaspora-status-wrapper"><strong>';
117                 $s .= $status;
118                 $s .= '</strong></div><div class="clear"></div>';
119         }
120
121         $s .= '<div id="diaspora-enable-wrapper">';
122         $s .= '<label id="diaspora-enable-label" for="diaspora-checkbox">' . DI::l10n()->t('Enable Diaspora Post Addon') . '</label>';
123         $s .= '<input id="diaspora-checkbox" type="checkbox" name="diaspora" value="1" ' . $checked . '/>';
124         $s .= '</div><div class="clear"></div>';
125
126         $s .= '<div id="diaspora-username-wrapper">';
127         $s .= '<label id="diaspora-username-label" for="diaspora-username">' . DI::l10n()->t('Diaspora handle') . '</label>';
128         $s .= '<input id="diaspora-username" type="text" name="handle" value="' . $handle . '" />';
129         $s .= '</div><div class="clear"></div>';
130
131         $s .= '<div id="diaspora-password-wrapper">';
132         $s .= '<label id="diaspora-password-label" for="diaspora-password">' . DI::l10n()->t('Diaspora password') . '</label>';
133         $s .= '<input id="diaspora-password" type="password" name="password" value="' . $password . '" />';
134         $s .= '</div><div class="clear"></div>';
135
136         if ($aspects) {
137                 $single_aspect =  new stdClass();
138                 $single_aspect->id = 'all_aspects';
139                 $single_aspect->name = DI::l10n()->t('All aspects');
140                 $aspects[] = $single_aspect;
141
142                 $single_aspect =  new stdClass();
143                 $single_aspect->id = 'public';
144                 $single_aspect->name = DI::l10n()->t('Public');
145                 $aspects[] = $single_aspect;
146
147                 $s .= '<label id="diaspora-aspect-label" for="diaspora-aspect">' . DI::l10n()->t('Post to aspect:') . '</label>';
148                 $s .= '<select name="aspect" id="diaspora-aspect">';
149                 foreach($aspects as $single_aspect) {
150                         if ($single_aspect->id == $aspect)
151                                 $s .= "<option value='".$single_aspect->id."' selected>".$single_aspect->name."</option>";
152                         else
153                                 $s .= "<option value='".$single_aspect->id."'>".$single_aspect->name."</option>";
154                 }
155
156                 $s .= "</select>";
157                 $s .= '<div class="clear"></div>';
158         }
159
160         $s .= '<div id="diaspora-bydefault-wrapper">';
161         $s .= '<label id="diaspora-bydefault-label" for="diaspora-bydefault">' . DI::l10n()->t('Post to Diaspora by default') . '</label>';
162         $s .= '<input id="diaspora-bydefault" type="checkbox" name="diaspora_bydefault" value="1" ' . $def_checked . '/>';
163         $s .= '</div><div class="clear"></div>';
164
165         /* provide a submit button */
166
167         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="diaspora-submit" name="diaspora-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
168
169 }
170
171
172 function diaspora_settings_post(App $a, &$b)
173 {
174         if (!empty($_POST['diaspora-submit'])) {
175                 DI::pConfig()->set(local_user(),'diaspora', 'post'           , intval($_POST['diaspora']));
176                 DI::pConfig()->set(local_user(),'diaspora', 'post_by_default', intval($_POST['diaspora_bydefault']));
177                 DI::pConfig()->set(local_user(),'diaspora', 'handle'         , trim($_POST['handle']));
178                 DI::pConfig()->set(local_user(),'diaspora', 'password'       , trim($_POST['password']));
179                 DI::pConfig()->set(local_user(),'diaspora', 'aspect'         , trim($_POST['aspect']));
180         }
181 }
182
183 function diaspora_hook_fork(&$a, &$b)
184 {
185         if ($b['name'] != 'notifier_normal') {
186                 return;
187         }
188
189         $post = $b['data'];
190
191         if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
192                 !strstr($post['postopts'], 'diaspora') || ($post['parent'] != $post['id'])) {
193                 $b['execute'] = false;
194                 return;
195         }
196 }
197
198 function diaspora_post_local(App $a, array &$b)
199 {
200         if ($b['edit']) {
201                 return;
202         }
203
204         if (!local_user() || (local_user() != $b['uid'])) {
205                 return;
206         }
207
208         if ($b['private'] || $b['parent']) {
209                 return;
210         }
211
212         $diaspora_post   = intval(DI::pConfig()->get(local_user(),'diaspora','post'));
213
214         $diaspora_enable = (($diaspora_post && !empty($_REQUEST['diaspora_enable'])) ? intval($_REQUEST['diaspora_enable']) : 0);
215
216         if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'diaspora','post_by_default'))) {
217                 $diaspora_enable = 1;
218         }
219
220         if (!$diaspora_enable) {
221                 return;
222         }
223
224         if (strlen($b['postopts'])) {
225                 $b['postopts'] .= ',';
226         }
227
228         $b['postopts'] .= 'diaspora';
229 }
230
231 function diaspora_send(App $a, array &$b)
232 {
233         $hostname = DI::baseUrl()->getHostname();
234
235         Logger::log('diaspora_send: invoked');
236
237         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
238                 return;
239         }
240
241         if (!strstr($b['postopts'],'diaspora')) {
242                 return;
243         }
244
245         if ($b['parent'] != $b['id']) {
246                 return;
247         }
248
249         // Dont't post if the post doesn't belong to us.
250         // This is a check for forum postings
251         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
252
253         if ($b['contact-id'] != $self['id']) {
254                 return;
255         }
256
257         Logger::log('diaspora_send: prepare posting', Logger::DEBUG);
258
259         $handle = DI::pConfig()->get($b['uid'],'diaspora','handle');
260         $password = DI::pConfig()->get($b['uid'],'diaspora','password');
261         $aspect = DI::pConfig()->get($b['uid'],'diaspora','aspect');
262
263         if ($handle && $password) {
264                 Logger::log('diaspora_send: all values seem to be okay', Logger::DEBUG);
265
266                 $tag_arr = [];
267                 $tags = '';
268                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
269
270                 if ($x) {
271                         foreach ($matches as $mtch) {
272                                 $tag_arr[] = $mtch[2];
273                         }
274                 }
275
276                 if (count($tag_arr)) {
277                         $tags = implode(',',$tag_arr);
278                 }
279
280                 $title = $b['title'];
281                 $body = $b['body'];
282                 // Insert a newline before and after a quote
283                 $body = str_ireplace("[quote", "\n\n[quote", $body);
284                 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
285
286                 // Removal of tags and mentions
287                 // #-tags
288                 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
289                 // @-mentions
290                 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
291
292                 // remove multiple newlines
293                 do {
294                         $oldbody = $body;
295                         $body = str_replace("\n\n\n", "\n\n", $body);
296                 } while ($oldbody != $body);
297
298                 // convert to markdown
299                 $body = BBCode::toMarkdown($body);
300
301                 // Adding the title
302                 if (strlen($title)) {
303                         $body = "## ".html_entity_decode($title)."\n\n".$body;
304                 }
305
306                 require_once "addon/diaspora/diasphp.php";
307
308                 try {
309                         Logger::log('diaspora_send: prepare', Logger::DEBUG);
310                         $conn = new Diaspora_Connection($handle, $password);
311                         Logger::log('diaspora_send: try to log in '.$handle, Logger::DEBUG);
312                         $conn->logIn();
313                         Logger::log('diaspora_send: try to send '.$body, Logger::DEBUG);
314
315                         $conn->provider = $hostname;
316                         $conn->postStatusMessage($body, $aspect);
317
318                         Logger::log('diaspora_send: success');
319                 } catch (Exception $e) {
320                         Logger::log("diaspora_send: Error submitting the post: " . $e->getMessage());
321
322                         Logger::log('diaspora_send: requeueing '.$b['uid'], Logger::DEBUG);
323
324                         Worker::defer();
325                 }
326         }
327 }