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