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