PL translation diaspora THX waldis
[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\Hook;
15 use Friendica\Core\Logger;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\Session;
18 use Friendica\Database\DBA;
19 use Friendica\Core\Worker;
20 use Friendica\DI;
21
22 function diaspora_install()
23 {
24         Hook::register('hook_fork',               'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
25         Hook::register('post_local',              'addon/diaspora/diaspora.php', 'diaspora_post_local');
26         Hook::register('notifier_normal',         'addon/diaspora/diaspora.php', 'diaspora_send');
27         Hook::register('jot_networks',            'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
28         Hook::register('connector_settings',      'addon/diaspora/diaspora.php', 'diaspora_settings');
29         Hook::register('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
30 }
31
32 function diaspora_uninstall()
33 {
34         Hook::unregister('hook_fork',               'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
35         Hook::unregister('post_local',              'addon/diaspora/diaspora.php', 'diaspora_post_local');
36         Hook::unregister('notifier_normal',         'addon/diaspora/diaspora.php', 'diaspora_send');
37         Hook::unregister('jot_networks',            'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
38         Hook::unregister('connector_settings',      'addon/diaspora/diaspora.php', 'diaspora_settings');
39         Hook::unregister('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
40 }
41
42 function diaspora_jot_nets(App $a, array &$jotnets_fields)
43 {
44         if (!local_user()) {
45                 return;
46         }
47
48         if (DI::pConfig()->get(local_user(), 'diaspora', 'post')) {
49                 $jotnets_fields[] = [
50                         'type' => 'checkbox',
51                         'field' => [
52                                 'diaspora_enable',
53                                 DI::l10n()->t('Post to Diaspora'),
54                                 DI::pConfig()->get(local_user(), 'diaspora', 'post_by_default')
55                         ]
56                 ];
57         }
58 }
59
60 function diaspora_settings(App $a, &$s)
61 {
62         if (! local_user()) {
63                 return;
64         }
65
66         /* Get the current state of our config variables */
67
68         $enabled = DI::pConfig()->get(local_user(),'diaspora','post');
69         $def_enabled = DI::pConfig()->get(local_user(),'diaspora','post_by_default');
70
71         $handle = DI::pConfig()->get(local_user(), 'diaspora', 'handle');
72         $password = DI::pConfig()->get(local_user(), 'diaspora', 'password');
73         $aspect = DI::pConfig()->get(local_user(),'diaspora','aspect');
74
75         $info = '';
76         $error = '';
77         if (Session::get('my_address')) {
78                 $info = DI::l10n()->t('Please remember: You can always be reached from Diaspora with your Friendica handle <strong>%s</strong>. ', Session::get('my_address'));
79                 $info .= DI::l10n()->t('This connector is only meant if you still want to use your old Diaspora account for some time. ');
80                 $info .= DI::l10n()->t('However, it is preferred that you tell your Diaspora contacts the new handle <strong>%s</strong> instead.', Session::get('my_address'));
81         }
82
83         $aspect_select = '';
84         if ($handle && $password) {
85                 $conn = new Diaspora_Connection($handle, $password);
86                 $conn->logIn();
87                 $rawAspects = $conn->getAspects();
88                 if ($rawAspects) {
89                         $availableAspects = [
90                                 'all_aspects' => DI::l10n()->t('All aspects'),
91                                 'public' => DI::l10n()->t('Public'),
92                         ];
93                         foreach ($rawAspects as $rawAspect) {
94                                 $availableAspects[$rawAspect->id] = $rawAspect->name;
95                         }
96
97                         $aspect_select = ['aspect', DI::l10n()->t('Post to aspect:'), $aspect, '', $availableAspects];
98                         $info = DI::l10n()->t('Connected with your Diaspora account <strong>%s</strong>', $handle);
99                 } else {
100                         $info = '';
101                         $error = 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         DI::page()->registerStylesheet('addon/diaspora/diaspora.css');
106
107         $t = Renderer::getMarkupTemplate('settings.tpl', 'addon/diaspora/');
108         $s .= Renderer::replaceMacros($t, [
109                 '$header'           => DI::l10n()->t('Diaspora Export'),
110                 '$info_header'      => DI::l10n()->t('Information'),
111                 '$error_header'     => DI::l10n()->t('Error'),
112                 '$submit'           => DI::l10n()->t('Save Settings'),
113                 '$info'             => $info,
114                 '$error'            => $error,
115                 '$enabled'          => $enabled,
116                 '$enabled_checkbox' => ['enabled', DI::l10n()->t('Enable Diaspora Post Addon'), $enabled],
117                 '$handle'           => ['handle', DI::l10n()->t('Diaspora handle'), $handle, null, null, 'placeholder="user@domain.tld"'],
118                 '$password'         => ['password', DI::l10n()->t('Diaspora password'), '', DI::l10n()->t('Privacy notice: Your Diaspora password will be stored unencrypted to authenticate you with your Diaspora pod. This means your Friendica node administrator can have access to it.')],
119                 '$aspect_select'    => $aspect_select,
120                 '$post_by_default'  => ['post_by_default', DI::l10n()->t('Post to Diaspora by default'), $def_enabled],
121         ]);
122 }
123
124
125 function diaspora_settings_post(App $a, &$b)
126 {
127         if (!empty($_POST['diaspora-submit'])) {
128                 DI::pConfig()->set(local_user(),'diaspora', 'post'           , intval($_POST['enabled']));
129                 if (intval($_POST['enabled'])) {
130                         if (isset($_POST['handle'])) {
131                                 DI::pConfig()->set(local_user(),'diaspora', 'handle'         , trim($_POST['handle']));
132                                 DI::pConfig()->set(local_user(),'diaspora', 'password'       , trim($_POST['password']));
133                         }
134                         if (!empty($_POST['aspect'])) {
135                                 DI::pConfig()->set(local_user(),'diaspora', 'aspect'         , trim($_POST['aspect']));
136                                 DI::pConfig()->set(local_user(),'diaspora', 'post_by_default', intval($_POST['post_by_default']));
137                         }
138                         notice(DI::l10n()->t('Diaspora settings updated.'));
139                 } else {
140                         DI::pConfig()->delete(local_user(), 'diaspora', 'password');
141                         notice(DI::l10n()->t('Diaspora connector disabled.'));
142                 }
143         }
144 }
145
146 function diaspora_hook_fork(&$a, &$b)
147 {
148         if ($b['name'] != 'notifier_normal') {
149                 return;
150         }
151
152         $post = $b['data'];
153
154         if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
155                 !strstr($post['postopts'], 'diaspora') || ($post['parent'] != $post['id'])) {
156                 $b['execute'] = false;
157                 return;
158         }
159 }
160
161 function diaspora_post_local(App $a, array &$b)
162 {
163         if ($b['edit']) {
164                 return;
165         }
166
167         if (!local_user() || (local_user() != $b['uid'])) {
168                 return;
169         }
170
171         if ($b['private'] || $b['parent']) {
172                 return;
173         }
174
175         $diaspora_post   = intval(DI::pConfig()->get(local_user(),'diaspora','post'));
176
177         $diaspora_enable = (($diaspora_post && !empty($_REQUEST['diaspora_enable'])) ? intval($_REQUEST['diaspora_enable']) : 0);
178
179         if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'diaspora','post_by_default'))) {
180                 $diaspora_enable = 1;
181         }
182
183         if (!$diaspora_enable) {
184                 return;
185         }
186
187         if (strlen($b['postopts'])) {
188                 $b['postopts'] .= ',';
189         }
190
191         $b['postopts'] .= 'diaspora';
192 }
193
194 function diaspora_send(App $a, array &$b)
195 {
196         $hostname = DI::baseUrl()->getHostname();
197
198         Logger::log('diaspora_send: invoked');
199
200         if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
201                 return;
202         }
203
204         if (!strstr($b['postopts'],'diaspora')) {
205                 return;
206         }
207
208         if ($b['parent'] != $b['id']) {
209                 return;
210         }
211
212         // Dont't post if the post doesn't belong to us.
213         // This is a check for forum postings
214         $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
215
216         if ($b['contact-id'] != $self['id']) {
217                 return;
218         }
219
220         Logger::log('diaspora_send: prepare posting', Logger::DEBUG);
221
222         $handle = DI::pConfig()->get($b['uid'],'diaspora','handle');
223         $password = DI::pConfig()->get($b['uid'],'diaspora','password');
224         $aspect = DI::pConfig()->get($b['uid'],'diaspora','aspect');
225
226         if ($handle && $password) {
227                 Logger::log('diaspora_send: all values seem to be okay', Logger::DEBUG);
228
229                 $title = $b['title'];
230                 $body = $b['body'];
231                 // Insert a newline before and after a quote
232                 $body = str_ireplace("[quote", "\n\n[quote", $body);
233                 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
234
235                 // Removal of tags and mentions
236                 // #-tags
237                 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
238                 // @-mentions
239                 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
240
241                 // remove multiple newlines
242                 do {
243                         $oldbody = $body;
244                         $body = str_replace("\n\n\n", "\n\n", $body);
245                 } while ($oldbody != $body);
246
247                 // convert to markdown
248                 $body = BBCode::toMarkdown($body);
249
250                 // Adding the title
251                 if (strlen($title)) {
252                         $body = "## ".html_entity_decode($title)."\n\n".$body;
253                 }
254
255                 require_once "addon/diaspora/diasphp.php";
256
257                 try {
258                         Logger::log('diaspora_send: prepare', Logger::DEBUG);
259                         $conn = new Diaspora_Connection($handle, $password);
260                         Logger::log('diaspora_send: try to log in '.$handle, Logger::DEBUG);
261                         $conn->logIn();
262                         Logger::log('diaspora_send: try to send '.$body, Logger::DEBUG);
263
264                         $conn->provider = $hostname;
265                         $conn->postStatusMessage($body, $aspect);
266
267                         Logger::log('diaspora_send: success');
268                 } catch (Exception $e) {
269                         Logger::log("diaspora_send: Error submitting the post: " . $e->getMessage());
270
271                         Logger::log('diaspora_send: requeueing '.$b['uid'], Logger::DEBUG);
272
273                         Worker::defer();
274                 }
275         }
276 }