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