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\Core\Session;
18 use Friendica\Database\DBA;
19 use Friendica\Core\Worker;
21 use Friendica\Model\Post;
23 function diaspora_install()
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');
33 function diaspora_jot_nets(App $a, array &$jotnets_fields)
39 if (DI::pConfig()->get(local_user(), 'diaspora', 'post')) {
44 DI::l10n()->t('Post to Diaspora'),
45 DI::pConfig()->get(local_user(), 'diaspora', 'post_by_default')
51 function diaspora_settings(App $a, &$s)
57 /* Get the current state of our config variables */
59 $enabled = DI::pConfig()->get(local_user(),'diaspora','post');
60 $def_enabled = DI::pConfig()->get(local_user(),'diaspora','post_by_default');
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');
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'));
75 if ($handle && $password) {
76 $conn = new Diaspora_Connection($handle, $password);
78 $rawAspects = $conn->getAspects();
81 'all_aspects' => DI::l10n()->t('All aspects'),
82 'public' => DI::l10n()->t('Public'),
84 foreach ($rawAspects as $rawAspect) {
85 $availableAspects[$rawAspect->id] = $rawAspect->name;
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);
92 $error = DI::l10n()->t("Can't login to your Diaspora account. Please check handle (in the format user@domain.tld) and password.");
96 DI::page()->registerStylesheet('addon/diaspora/diaspora.css');
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'),
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],
116 function diaspora_settings_post(App $a, &$b)
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']));
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']));
130 DI::pConfig()->delete(local_user(), 'diaspora', 'password');
135 function diaspora_hook_fork(&$a, &$b)
137 if ($b['name'] != 'notifier_normal') {
143 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
144 !strstr($post['postopts'], 'diaspora') || ($post['parent'] != $post['id'])) {
145 $b['execute'] = false;
150 function diaspora_post_local(App $a, array &$b)
156 if (!local_user() || (local_user() != $b['uid'])) {
160 if ($b['private'] || $b['parent']) {
164 $diaspora_post = intval(DI::pConfig()->get(local_user(),'diaspora','post'));
166 $diaspora_enable = (($diaspora_post && !empty($_REQUEST['diaspora_enable'])) ? intval($_REQUEST['diaspora_enable']) : 0);
168 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'diaspora','post_by_default'))) {
169 $diaspora_enable = 1;
172 if (!$diaspora_enable) {
176 if (strlen($b['postopts'])) {
177 $b['postopts'] .= ',';
180 $b['postopts'] .= 'diaspora';
183 function diaspora_send(App $a, array &$b)
185 $hostname = DI::baseUrl()->getHostname();
187 Logger::notice('diaspora_send: invoked');
189 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
193 if (!strstr($b['postopts'],'diaspora')) {
197 if ($b['parent'] != $b['id']) {
201 $b['body'] = Post\Media::addAttachmentsToBody($b['uri-id'], $b['body']);
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]);
207 if ($b['contact-id'] != $self['id']) {
211 Logger::info('diaspora_send: prepare posting');
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');
217 if ($handle && $password) {
218 Logger::info('diaspora_send: all values seem to be okay');
220 $title = $b['title'];
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);
226 // Removal of tags and mentions
228 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
230 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
232 // remove multiple newlines
235 $body = str_replace("\n\n\n", "\n\n", $body);
236 } while ($oldbody != $body);
238 // convert to markdown
239 $body = BBCode::toMarkdown($body);
242 if (strlen($title)) {
243 $body = "## ".html_entity_decode($title)."\n\n".$body;
246 require_once "addon/diaspora/diasphp.php";
249 Logger::info('diaspora_send: prepare');
250 $conn = new Diaspora_Connection($handle, $password);
251 Logger::info('diaspora_send: try to log in '.$handle);
253 Logger::info('diaspora_send: try to send '.$body);
255 $conn->provider = $hostname;
256 $conn->postStatusMessage($body, $aspect);
258 Logger::notice('diaspora_send: success');
259 } catch (Exception $e) {
260 Logger::notice("diaspora_send: Error submitting the post: " . $e->getMessage());
262 Logger::info('diaspora_send: requeueing '.$b['uid']);