Merge pull request #779 from MrPetovan/task/remove-x
[friendica-addons.git/.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\Addon;
15 use Friendica\Core\L10n;
16 use Friendica\Core\Logger;
17 use Friendica\Core\PConfig;
18 use Friendica\Core\Protocol;
19 use Friendica\Database\DBA;
20 use Friendica\Model\Queue;
21
22 function diaspora_install()
23 {
24         Addon::registerHook('hook_fork',               'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
25         Addon::registerHook('post_local',              'addon/diaspora/diaspora.php', 'diaspora_post_local');
26         Addon::registerHook('notifier_normal',         'addon/diaspora/diaspora.php', 'diaspora_send');
27         Addon::registerHook('jot_networks',            'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
28         Addon::registerHook('connector_settings',      'addon/diaspora/diaspora.php', 'diaspora_settings');
29         Addon::registerHook('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
30         Addon::registerHook('queue_predeliver',        'addon/diaspora/diaspora.php', 'diaspora_queue_hook');
31 }
32
33 function diaspora_uninstall()
34 {
35         Addon::unregisterHook('hook_fork',               'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
36         Addon::unregisterHook('post_local',              'addon/diaspora/diaspora.php', 'diaspora_post_local');
37         Addon::unregisterHook('notifier_normal',         'addon/diaspora/diaspora.php', 'diaspora_send');
38         Addon::unregisterHook('jot_networks',            'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
39         Addon::unregisterHook('connector_settings',      'addon/diaspora/diaspora.php', 'diaspora_settings');
40         Addon::unregisterHook('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
41         Addon::unregisterHook('queue_predeliver',        'addon/diaspora/diaspora.php', 'diaspora_queue_hook');
42 }
43
44 function diaspora_jot_nets(App $a, &$b)
45 {
46         if (!local_user()) {
47                 return;
48         }
49
50         $diaspora_post = PConfig::get(local_user(), 'diaspora', 'post');
51
52         if (intval($diaspora_post) == 1) {
53                 $diaspora_defpost = PConfig::get(local_user(), 'diaspora', 'post_by_default');
54
55                 $selected = ((intval($diaspora_defpost) == 1) ? ' checked="checked" ' : '');
56
57                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="diaspora_enable"' . $selected . ' value="1" /> '
58                 . L10n::t('Post to Diaspora') . '</div>';
59         }
60 }
61
62 function diaspora_queue_hook(App $a, &$b) {
63         $hostname = $a->getHostName();
64
65         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
66                 DBA::escape(Protocol::DIASPORA2)
67         );
68
69         if (!DBA::isResult($qi)) {
70                 return;
71         }
72
73         foreach ($qi as $x) {
74                 if ($x['network'] !== Protocol::DIASPORA2) {
75                         continue;
76                 }
77
78                 Logger::log('diaspora_queue: run');
79
80                 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
81                         WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
82                         intval($x['cid'])
83                 );
84
85                 if (!DBA::isResult($r)) {
86                         continue;
87                 }
88
89                 $userdata = $r[0];
90
91                 $handle   = PConfig::get($userdata['uid'], 'diaspora', 'handle');
92                 $password = PConfig::get($userdata['uid'], 'diaspora', 'password');
93                 $aspect   = PConfig::get($userdata['uid'], 'diaspora', 'aspect');
94
95                 $success = false;
96
97                 if ($handle && $password) {
98                         Logger::log('diaspora_queue: able to post for user '.$handle);
99
100                         $z = unserialize($x['content']);
101
102                         $post = $z['post'];
103
104                         Logger::log('diaspora_queue: post: '.$post, Logger::DATA);
105
106                         try {
107                                 Logger::log('diaspora_queue: prepare', Logger::DEBUG);
108                                 $conn = new Diaspora_Connection($handle, $password);
109                                 Logger::log('diaspora_queue: try to log in '.$handle, Logger::DEBUG);
110                                 $conn->logIn();
111                                 Logger::log('diaspora_queue: try to send '.$body, Logger::DEBUG);
112                                 $conn->provider = $hostname;
113                                 $conn->postStatusMessage($post, $aspect);
114
115                                 Logger::log('diaspora_queue: send '.$userdata['uid'].' success', Logger::DEBUG);
116
117                                 $success = true;
118
119                                 Queue::removeItem($x['id']);
120                         } catch (Exception $e) {
121                                 Logger::log("diaspora_queue: Send ".$userdata['uid']." failed: ".$e->getMessage(), Logger::DEBUG);
122                         }
123                 } else {
124                         Logger::log('diaspora_queue: send '.$userdata['uid'].' missing username or password', Logger::DEBUG);
125                 }
126
127                 if (!$success) {
128                         Logger::log('diaspora_queue: delayed');
129                         Queue::updateTime($x['id']);
130                 }
131         }
132 }
133
134 function diaspora_settings(App $a, &$s)
135 {
136         if (! local_user()) {
137                 return;
138         }
139
140         /* Add our stylesheet to the page so we can make our settings look nice */
141
142         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->getBaseURL() . '/addon/diaspora/diaspora.css' . '" media="all" />' . "\r\n";
143
144         /* Get the current state of our config variables */
145
146         $enabled = PConfig::get(local_user(),'diaspora','post');
147         $checked = (($enabled) ? ' checked="checked" ' : '');
148         $css = (($enabled) ? '' : '-disabled');
149
150         $def_enabled = PConfig::get(local_user(),'diaspora','post_by_default');
151
152         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
153
154         $handle = PConfig::get(local_user(), 'diaspora', 'handle');
155         $password = PConfig::get(local_user(), 'diaspora', 'password');
156         $aspect = PConfig::get(local_user(),'diaspora','aspect');
157
158         $status = "";
159
160         $r = q("SELECT `addr` FROM `contact` WHERE `self` AND `uid` = %d", intval(local_user()));
161
162         if (DBA::isResult($r)) {
163                 $status = L10n::t("Please remember: You can always be reached from Diaspora with your Friendica handle %s. ", $r[0]['addr']);
164                 $status .= L10n::t('This connector is only meant if you still want to use your old Diaspora account for some time. ');
165                 $status .= L10n::t('However, it is preferred that you tell your Diaspora contacts the new handle %s instead.', $r[0]['addr']);
166         }
167
168         $aspects = false;
169
170         if ($handle && $password) {
171                 $conn = new Diaspora_Connection($handle, $password);
172                 $conn->logIn();
173                 $aspects = $conn->getAspects();
174
175                 if (!$aspects) {
176                         $status = L10n::t("Can't login to your Diaspora account. Please check handle (in the format user@domain.tld) and password.");
177                 }
178         }
179
180         /* Add some HTML to the existing form */
181
182         $s .= '<span id="settings_diaspora_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
183         $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. L10n::t('Diaspora Export').'</h3>';
184         $s .= '</span>';
185         $s .= '<div id="settings_diaspora_expanded" class="settings-block" style="display: none;">';
186         $s .= '<span class="fakelink" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
187         $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. L10n::t('Diaspora Export').'</h3>';
188         $s .= '</span>';
189
190         if ($status) {
191                 $s .= '<div id="diaspora-status-wrapper"><strong>';
192                 $s .= $status;
193                 $s .= '</strong></div><div class="clear"></div>';
194         }
195
196         $s .= '<div id="diaspora-enable-wrapper">';
197         $s .= '<label id="diaspora-enable-label" for="diaspora-checkbox">' . L10n::t('Enable Diaspora Post Addon') . '</label>';
198         $s .= '<input id="diaspora-checkbox" type="checkbox" name="diaspora" value="1" ' . $checked . '/>';
199         $s .= '</div><div class="clear"></div>';
200
201         $s .= '<div id="diaspora-username-wrapper">';
202         $s .= '<label id="diaspora-username-label" for="diaspora-username">' . L10n::t('Diaspora handle') . '</label>';
203         $s .= '<input id="diaspora-username" type="text" name="handle" value="' . $handle . '" />';
204         $s .= '</div><div class="clear"></div>';
205
206         $s .= '<div id="diaspora-password-wrapper">';
207         $s .= '<label id="diaspora-password-label" for="diaspora-password">' . L10n::t('Diaspora password') . '</label>';
208         $s .= '<input id="diaspora-password" type="password" name="password" value="' . $password . '" />';
209         $s .= '</div><div class="clear"></div>';
210
211         if ($aspects) {
212                 $single_aspect =  new stdClass();
213                 $single_aspect->id = 'all_aspects';
214                 $single_aspect->name = L10n::t('All aspects');
215                 $aspects[] = $single_aspect;
216
217                 $single_aspect =  new stdClass();
218                 $single_aspect->id = 'public';
219                 $single_aspect->name = L10n::t('Public');
220                 $aspects[] = $single_aspect;
221
222                 $s .= '<label id="diaspora-aspect-label" for="diaspora-aspect">' . L10n::t('Post to aspect:') . '</label>';
223                 $s .= '<select name="aspect" id="diaspora-aspect">';
224                 foreach($aspects as $single_aspect) {
225                         if ($single_aspect->id == $aspect)
226                                 $s .= "<option value='".$single_aspect->id."' selected>".$single_aspect->name."</option>";
227                         else
228                                 $s .= "<option value='".$single_aspect->id."'>".$single_aspect->name."</option>";
229                 }
230
231                 $s .= "</select>";
232                 $s .= '<div class="clear"></div>';
233         }
234
235         $s .= '<div id="diaspora-bydefault-wrapper">';
236         $s .= '<label id="diaspora-bydefault-label" for="diaspora-bydefault">' . L10n::t('Post to Diaspora by default') . '</label>';
237         $s .= '<input id="diaspora-bydefault" type="checkbox" name="diaspora_bydefault" value="1" ' . $def_checked . '/>';
238         $s .= '</div><div class="clear"></div>';
239
240         /* provide a submit button */
241
242         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="diaspora-submit" name="diaspora-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
243
244 }
245
246
247 function diaspora_settings_post(App $a, &$b)
248 {
249         if (!empty($_POST['diaspora-submit'])) {
250                 PConfig::set(local_user(),'diaspora', 'post'           , intval($_POST['diaspora']));
251                 PConfig::set(local_user(),'diaspora', 'post_by_default', intval($_POST['diaspora_bydefault']));
252                 PConfig::set(local_user(),'diaspora', 'handle'         , trim($_POST['handle']));
253                 PConfig::set(local_user(),'diaspora', 'password'       , trim($_POST['password']));
254                 PConfig::set(local_user(),'diaspora', 'aspect'         , trim($_POST['aspect']));
255         }
256 }
257
258 function diaspora_hook_fork(&$a, &$b)
259 {
260         if ($b['name'] != 'notifier_normal') {
261                 return;
262         }
263
264         $post = $b['data'];
265
266         if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
267                 !strstr($post['postopts'], 'diaspora') || ($post['parent'] != $post['id'])) {
268                 $b['execute'] = false;
269                 return;
270         }
271 }
272
273 function diaspora_post_local(App $a, array &$b)
274 {
275         if ($b['edit']) {
276                 return;
277         }
278
279         if (!local_user() || (local_user() != $b['uid'])) {
280                 return;
281         }
282
283         if ($b['private'] || $b['parent']) {
284                 return;
285         }
286
287         $diaspora_post   = intval(PConfig::get(local_user(),'diaspora','post'));
288
289         $diaspora_enable = (($diaspora_post && !empty($_REQUEST['diaspora_enable'])) ? intval($_REQUEST['diaspora_enable']) : 0);
290
291         if ($b['api_source'] && intval(PConfig::get(local_user(),'diaspora','post_by_default'))) {
292                 $diaspora_enable = 1;
293         }
294
295         if (!$diaspora_enable) {
296                 return;
297         }
298
299         if (strlen($b['postopts'])) {
300                 $b['postopts'] .= ',';
301         }
302
303         $b['postopts'] .= 'diaspora';
304 }
305
306 function diaspora_send(App $a, array &$b)
307 {
308         $hostname = $a->getHostName();
309
310         Logger::log('diaspora_send: invoked');
311
312         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
313                 return;
314         }
315
316         if (!strstr($b['postopts'],'diaspora')) {
317                 return;
318         }
319
320         if ($b['parent'] != $b['id']) {
321                 return;
322         }
323
324         // Dont't post if the post doesn't belong to us.
325         // This is a check for forum postings
326         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
327
328         if ($b['contact-id'] != $self['id']) {
329                 return;
330         }
331
332         Logger::log('diaspora_send: prepare posting', Logger::DEBUG);
333
334         $handle = PConfig::get($b['uid'],'diaspora','handle');
335         $password = PConfig::get($b['uid'],'diaspora','password');
336         $aspect = PConfig::get($b['uid'],'diaspora','aspect');
337
338         if ($handle && $password) {
339                 Logger::log('diaspora_send: all values seem to be okay', Logger::DEBUG);
340
341                 $tag_arr = [];
342                 $tags = '';
343                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
344
345                 if ($x) {
346                         foreach ($matches as $mtch) {
347                                 $tag_arr[] = $mtch[2];
348                         }
349                 }
350
351                 if (count($tag_arr)) {
352                         $tags = implode(',',$tag_arr);
353                 }
354
355                 $title = $b['title'];
356                 $body = $b['body'];
357                 // Insert a newline before and after a quote
358                 $body = str_ireplace("[quote", "\n\n[quote", $body);
359                 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
360
361                 // Removal of tags and mentions
362                 // #-tags
363                 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
364                 // @-mentions
365                 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
366
367                 // remove multiple newlines
368                 do {
369                         $oldbody = $body;
370                         $body = str_replace("\n\n\n", "\n\n", $body);
371                 } while ($oldbody != $body);
372
373                 // convert to markdown
374                 $body = BBCode::toMarkdown($body);
375
376                 // Adding the title
377                 if (strlen($title)) {
378                         $body = "## ".html_entity_decode($title)."\n\n".$body;
379                 }
380
381                 require_once "addon/diaspora/diasphp.php";
382
383                 try {
384                         Logger::log('diaspora_send: prepare', Logger::DEBUG);
385                         $conn = new Diaspora_Connection($handle, $password);
386                         Logger::log('diaspora_send: try to log in '.$handle, Logger::DEBUG);
387                         $conn->logIn();
388                         Logger::log('diaspora_send: try to send '.$body, Logger::DEBUG);
389
390                         $conn->provider = $hostname;
391                         $conn->postStatusMessage($body, $aspect);
392
393                         Logger::log('diaspora_send: success');
394                 } catch (Exception $e) {
395                         Logger::log("diaspora_send: Error submitting the post: " . $e->getMessage());
396
397                         Logger::log('diaspora_send: requeueing '.$b['uid'], Logger::DEBUG);
398
399                         $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
400                         if (count($r))
401                                 $a->contact = $r[0]["id"];
402
403                         $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $body]);
404
405                         Queue::add($a->contact, Protocol::DIASPORA2, $s);
406                         notice(L10n::t('Diaspora post failed. Queued for retry.').EOL);
407                 }
408         }
409 }