4 * Name: Diaspora Post Connector
5 * Description: Post to Diaspora
7 * Author: Michael Vogel <heluecht@pirati.ca>
10 require_once 'addon/diaspora/Diaspora_Connection.php';
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;
20 use Friendica\Model\Post;
22 function diaspora_install()
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');
32 function diaspora_jot_nets(array &$jotnets_fields)
34 if (!DI::userSession()->getLocalUserId()) {
38 if (DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'diaspora', 'post')) {
43 DI::l10n()->t('Post to Diaspora'),
44 DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'diaspora', 'post_by_default')
50 function diaspora_settings(array &$data)
52 if (!DI::userSession()->getLocalUserId()) {
56 $enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'diaspora', 'post', false);
57 $def_enabled = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'diaspora', 'post_by_default');
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');
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'));
72 if ($handle && $password) {
73 $conn = new Diaspora_Connection($handle, $password);
75 $rawAspects = $conn->getAspects();
78 'all_aspects' => DI::l10n()->t('All aspects'),
79 'public' => DI::l10n()->t('Public'),
81 foreach ($rawAspects as $rawAspect) {
82 $availableAspects[$rawAspect->id] = $rawAspect->name;
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);
89 $error = DI::l10n()->t("Can't login to your Diaspora account. Please check handle (in the format user@domain.tld) and password.");
93 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/diaspora/');
94 $html = Renderer::replaceMacros($t, [
96 'info_header' => DI::l10n()->t('Information'),
97 'error_header' => DI::l10n()->t('Error'),
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],
111 'connector' => 'diaspora',
112 'title' => DI::l10n()->t('Diaspora Export'),
113 'image' => 'images/diaspora-logo.png',
114 'enabled' => $enabled,
120 function diaspora_settings_post(array &$b)
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']));
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']));
134 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'diaspora', 'password');
139 function diaspora_hook_fork(array &$b)
141 if ($b['name'] != 'notifier_normal') {
147 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
148 !strstr($post['postopts'] ?? '', 'diaspora') || ($post['parent'] != $post['id'])) {
149 $b['execute'] = false;
154 function diaspora_post_local(array &$b)
160 if (!DI::userSession()->getLocalUserId() || (DI::userSession()->getLocalUserId() != $b['uid'])) {
164 if ($b['private'] || $b['parent']) {
168 $diaspora_post = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(),'diaspora','post'));
170 $diaspora_enable = (($diaspora_post && !empty($_REQUEST['diaspora_enable'])) ? intval($_REQUEST['diaspora_enable']) : 0);
172 if ($b['api_source'] && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(),'diaspora','post_by_default'))) {
173 $diaspora_enable = 1;
176 if (!$diaspora_enable) {
180 if (strlen($b['postopts'])) {
181 $b['postopts'] .= ',';
184 $b['postopts'] .= 'diaspora';
187 function diaspora_send(array &$b)
189 $hostname = DI::baseUrl()->getHost();
191 Logger::notice('diaspora_send: invoked');
193 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
197 if (!strstr($b['postopts'],'diaspora')) {
201 if ($b['parent'] != $b['id']) {
205 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], DI::contentItem()->addSharedPost($b));
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]);
211 if ($b['contact-id'] != $self['id']) {
215 Logger::info('diaspora_send: prepare posting');
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');
221 if ($handle && $password) {
222 Logger::info('diaspora_send: all values seem to be okay');
224 $title = $b['title'];
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);
230 // Removal of tags and mentions
232 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
234 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
236 // remove multiple newlines
239 $body = str_replace("\n\n\n", "\n\n", $body);
240 } while ($oldbody != $body);
242 // convert to markdown
243 $body = BBCode::toMarkdown($body);
246 if (strlen($title)) {
247 $body = "## ".html_entity_decode($title)."\n\n".$body;
250 require_once "addon/diaspora/diasphp.php";
253 Logger::info('diaspora_send: prepare');
254 $conn = new Diaspora_Connection($handle, $password);
255 Logger::info('diaspora_send: try to log in '.$handle);
257 Logger::info('diaspora_send: try to send '.$body);
259 $conn->provider = $hostname;
260 $conn->postStatusMessage($body, $aspect);
262 Logger::notice('diaspora_send: success');
263 } catch (Exception $e) {
264 Logger::notice("diaspora_send: Error submitting the post: " . $e->getMessage());
266 Logger::info('diaspora_send: requeueing '.$b['uid']);