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