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\L10n;
16 use Friendica\Core\Logger;
17 use Friendica\Database\DBA;
18 use Friendica\Core\Worker;
21 function diaspora_install()
23 Hook::register('hook_fork', 'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
24 Hook::register('post_local', 'addon/diaspora/diaspora.php', 'diaspora_post_local');
25 Hook::register('notifier_normal', 'addon/diaspora/diaspora.php', 'diaspora_send');
26 Hook::register('jot_networks', 'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
27 Hook::register('connector_settings', 'addon/diaspora/diaspora.php', 'diaspora_settings');
28 Hook::register('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
31 function diaspora_uninstall()
33 Hook::unregister('hook_fork', 'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
34 Hook::unregister('post_local', 'addon/diaspora/diaspora.php', 'diaspora_post_local');
35 Hook::unregister('notifier_normal', 'addon/diaspora/diaspora.php', 'diaspora_send');
36 Hook::unregister('jot_networks', 'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
37 Hook::unregister('connector_settings', 'addon/diaspora/diaspora.php', 'diaspora_settings');
38 Hook::unregister('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
41 function diaspora_jot_nets(App $a, array &$jotnets_fields)
47 if (DI::pConfig()->get(local_user(), 'diaspora', 'post')) {
52 DI::l10n()->t('Post to Diaspora'),
53 DI::pConfig()->get(local_user(), 'diaspora', 'post_by_default')
59 function diaspora_settings(App $a, &$s)
65 /* Add our stylesheet to the page so we can make our settings look nice */
67 DI::page()['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . DI::baseUrl()->get() . '/addon/diaspora/diaspora.css' . '" media="all" />' . "\r\n";
69 /* Get the current state of our config variables */
71 $enabled = DI::pConfig()->get(local_user(),'diaspora','post');
72 $checked = (($enabled) ? ' checked="checked" ' : '');
73 $css = (($enabled) ? '' : '-disabled');
75 $def_enabled = DI::pConfig()->get(local_user(),'diaspora','post_by_default');
77 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
79 $handle = DI::pConfig()->get(local_user(), 'diaspora', 'handle');
80 $password = DI::pConfig()->get(local_user(), 'diaspora', 'password');
81 $aspect = DI::pConfig()->get(local_user(),'diaspora','aspect');
85 $r = q("SELECT `addr` FROM `contact` WHERE `self` AND `uid` = %d", intval(local_user()));
87 if (DBA::isResult($r)) {
88 $status = DI::l10n()->t("Please remember: You can always be reached from Diaspora with your Friendica handle %s. ", $r[0]['addr']);
89 $status .= DI::l10n()->t('This connector is only meant if you still want to use your old Diaspora account for some time. ');
90 $status .= DI::l10n()->t('However, it is preferred that you tell your Diaspora contacts the new handle %s instead.', $r[0]['addr']);
95 if ($handle && $password) {
96 $conn = new Diaspora_Connection($handle, $password);
98 $aspects = $conn->getAspects();
101 $status = DI::l10n()->t("Can't login to your Diaspora account. Please check handle (in the format user@domain.tld) and password.");
105 /* Add some HTML to the existing form */
107 $s .= '<span id="settings_diaspora_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
108 $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. DI::l10n()->t('Diaspora Export').'</h3>';
110 $s .= '<div id="settings_diaspora_expanded" class="settings-block" style="display: none;">';
111 $s .= '<span class="fakelink" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
112 $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. DI::l10n()->t('Diaspora Export').'</h3>';
116 $s .= '<div id="diaspora-status-wrapper"><strong>';
118 $s .= '</strong></div><div class="clear"></div>';
121 $s .= '<div id="diaspora-enable-wrapper">';
122 $s .= '<label id="diaspora-enable-label" for="diaspora-checkbox">' . DI::l10n()->t('Enable Diaspora Post Addon') . '</label>';
123 $s .= '<input id="diaspora-checkbox" type="checkbox" name="diaspora" value="1" ' . $checked . '/>';
124 $s .= '</div><div class="clear"></div>';
126 $s .= '<div id="diaspora-username-wrapper">';
127 $s .= '<label id="diaspora-username-label" for="diaspora-username">' . DI::l10n()->t('Diaspora handle') . '</label>';
128 $s .= '<input id="diaspora-username" type="text" name="handle" value="' . $handle . '" />';
129 $s .= '</div><div class="clear"></div>';
131 $s .= '<div id="diaspora-password-wrapper">';
132 $s .= '<label id="diaspora-password-label" for="diaspora-password">' . DI::l10n()->t('Diaspora password') . '</label>';
133 $s .= '<input id="diaspora-password" type="password" name="password" value="' . $password . '" />';
134 $s .= '</div><div class="clear"></div>';
137 $single_aspect = new stdClass();
138 $single_aspect->id = 'all_aspects';
139 $single_aspect->name = DI::l10n()->t('All aspects');
140 $aspects[] = $single_aspect;
142 $single_aspect = new stdClass();
143 $single_aspect->id = 'public';
144 $single_aspect->name = DI::l10n()->t('Public');
145 $aspects[] = $single_aspect;
147 $s .= '<label id="diaspora-aspect-label" for="diaspora-aspect">' . DI::l10n()->t('Post to aspect:') . '</label>';
148 $s .= '<select name="aspect" id="diaspora-aspect">';
149 foreach($aspects as $single_aspect) {
150 if ($single_aspect->id == $aspect)
151 $s .= "<option value='".$single_aspect->id."' selected>".$single_aspect->name."</option>";
153 $s .= "<option value='".$single_aspect->id."'>".$single_aspect->name."</option>";
157 $s .= '<div class="clear"></div>';
160 $s .= '<div id="diaspora-bydefault-wrapper">';
161 $s .= '<label id="diaspora-bydefault-label" for="diaspora-bydefault">' . DI::l10n()->t('Post to Diaspora by default') . '</label>';
162 $s .= '<input id="diaspora-bydefault" type="checkbox" name="diaspora_bydefault" value="1" ' . $def_checked . '/>';
163 $s .= '</div><div class="clear"></div>';
165 /* provide a submit button */
167 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="diaspora-submit" name="diaspora-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div></div>';
172 function diaspora_settings_post(App $a, &$b)
174 if (!empty($_POST['diaspora-submit'])) {
175 DI::pConfig()->set(local_user(),'diaspora', 'post' , intval($_POST['diaspora']));
176 DI::pConfig()->set(local_user(),'diaspora', 'post_by_default', intval($_POST['diaspora_bydefault']));
177 DI::pConfig()->set(local_user(),'diaspora', 'handle' , trim($_POST['handle']));
178 DI::pConfig()->set(local_user(),'diaspora', 'password' , trim($_POST['password']));
179 DI::pConfig()->set(local_user(),'diaspora', 'aspect' , trim($_POST['aspect']));
183 function diaspora_hook_fork(&$a, &$b)
185 if ($b['name'] != 'notifier_normal') {
191 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
192 !strstr($post['postopts'], 'diaspora') || ($post['parent'] != $post['id'])) {
193 $b['execute'] = false;
198 function diaspora_post_local(App $a, array &$b)
204 if (!local_user() || (local_user() != $b['uid'])) {
208 if ($b['private'] || $b['parent']) {
212 $diaspora_post = intval(DI::pConfig()->get(local_user(),'diaspora','post'));
214 $diaspora_enable = (($diaspora_post && !empty($_REQUEST['diaspora_enable'])) ? intval($_REQUEST['diaspora_enable']) : 0);
216 if ($b['api_source'] && intval(DI::pConfig()->get(local_user(),'diaspora','post_by_default'))) {
217 $diaspora_enable = 1;
220 if (!$diaspora_enable) {
224 if (strlen($b['postopts'])) {
225 $b['postopts'] .= ',';
228 $b['postopts'] .= 'diaspora';
231 function diaspora_send(App $a, array &$b)
233 $hostname = DI::baseUrl()->getHostname();
235 Logger::log('diaspora_send: invoked');
237 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
241 if (!strstr($b['postopts'],'diaspora')) {
245 if ($b['parent'] != $b['id']) {
249 // Dont't post if the post doesn't belong to us.
250 // This is a check for forum postings
251 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
253 if ($b['contact-id'] != $self['id']) {
257 Logger::log('diaspora_send: prepare posting', Logger::DEBUG);
259 $handle = DI::pConfig()->get($b['uid'],'diaspora','handle');
260 $password = DI::pConfig()->get($b['uid'],'diaspora','password');
261 $aspect = DI::pConfig()->get($b['uid'],'diaspora','aspect');
263 if ($handle && $password) {
264 Logger::log('diaspora_send: all values seem to be okay', Logger::DEBUG);
268 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
271 foreach ($matches as $mtch) {
272 $tag_arr[] = $mtch[2];
276 if (count($tag_arr)) {
277 $tags = implode(',',$tag_arr);
280 $title = $b['title'];
282 // Insert a newline before and after a quote
283 $body = str_ireplace("[quote", "\n\n[quote", $body);
284 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
286 // Removal of tags and mentions
288 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
290 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
292 // remove multiple newlines
295 $body = str_replace("\n\n\n", "\n\n", $body);
296 } while ($oldbody != $body);
298 // convert to markdown
299 $body = BBCode::toMarkdown($body);
302 if (strlen($title)) {
303 $body = "## ".html_entity_decode($title)."\n\n".$body;
306 require_once "addon/diaspora/diasphp.php";
309 Logger::log('diaspora_send: prepare', Logger::DEBUG);
310 $conn = new Diaspora_Connection($handle, $password);
311 Logger::log('diaspora_send: try to log in '.$handle, Logger::DEBUG);
313 Logger::log('diaspora_send: try to send '.$body, Logger::DEBUG);
315 $conn->provider = $hostname;
316 $conn->postStatusMessage($body, $aspect);
318 Logger::log('diaspora_send: success');
319 } catch (Exception $e) {
320 Logger::log("diaspora_send: Error submitting the post: " . $e->getMessage());
322 Logger::log('diaspora_send: requeueing '.$b['uid'], Logger::DEBUG);