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';
12 use Friendica\Content\Text\BBCode;
13 use Friendica\Core\Addon;
14 use Friendica\Core\L10n;
15 use Friendica\Core\PConfig;
16 use Friendica\Database\DBM;
17 use Friendica\Model\Queue;
19 function diaspora_install() {
20 Addon::registerHook('post_local', 'addon/diaspora/diaspora.php', 'diaspora_post_local');
21 Addon::registerHook('notifier_normal', 'addon/diaspora/diaspora.php', 'diaspora_send');
22 Addon::registerHook('jot_networks', 'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
23 Addon::registerHook('connector_settings', 'addon/diaspora/diaspora.php', 'diaspora_settings');
24 Addon::registerHook('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
25 Addon::registerHook('queue_predeliver', 'addon/diaspora/diaspora.php', 'diaspora_queue_hook');
27 function diaspora_uninstall() {
28 Addon::unregisterHook('post_local', 'addon/diaspora/diaspora.php', 'diaspora_post_local');
29 Addon::unregisterHook('notifier_normal', 'addon/diaspora/diaspora.php', 'diaspora_send');
30 Addon::unregisterHook('jot_networks', 'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
31 Addon::unregisterHook('connector_settings', 'addon/diaspora/diaspora.php', 'diaspora_settings');
32 Addon::unregisterHook('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
33 Addon::unregisterHook('queue_predeliver', 'addon/diaspora/diaspora.php', 'diaspora_queue_hook');
37 function diaspora_jot_nets(&$a,&$b) {
41 $diaspora_post = PConfig::get(local_user(),'diaspora','post');
42 if(intval($diaspora_post) == 1) {
43 $diaspora_defpost = PConfig::get(local_user(),'diaspora','post_by_default');
44 $selected = ((intval($diaspora_defpost) == 1) ? ' checked="checked" ' : '');
45 $b .= '<div class="profile-jot-net"><input type="checkbox" name="diaspora_enable"' . $selected . ' value="1" /> '
46 . L10n::t('Post to Diaspora') . '</div>';
50 function diaspora_queue_hook(&$a,&$b) {
51 $hostname = $a->get_hostname();
53 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
54 dbesc(NETWORK_DIASPORA2)
60 if($x['network'] !== NETWORK_DIASPORA2)
63 logger('diaspora_queue: run');
65 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
66 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
74 $handle = PConfig::get($userdata['uid'],'diaspora','handle');
75 $password = PConfig::get($userdata['uid'],'diaspora','password');
76 $aspect = PConfig::get($userdata['uid'],'diaspora','aspect');
80 if ($handle && $password) {
81 logger('diaspora_queue: able to post for user '.$handle);
83 $z = unserialize($x['content']);
87 logger('diaspora_queue: post: '.$post, LOGGER_DATA);
90 logger('diaspora_queue: prepare', LOGGER_DEBUG);
91 $conn = new Diaspora_Connection($handle, $password);
92 logger('diaspora_queue: try to log in '.$handle, LOGGER_DEBUG);
94 logger('diaspora_queue: try to send '.$body, LOGGER_DEBUG);
95 $conn->provider = $hostname;
96 $conn->postStatusMessage($post, $aspect);
98 logger('diaspora_queue: send '.$userdata['uid'].' success', LOGGER_DEBUG);
102 Queue::removeItem($x['id']);
103 } catch (Exception $e) {
104 logger("diaspora_queue: Send ".$userdata['uid']." failed: ".$e->getMessage(), LOGGER_DEBUG);
107 logger('diaspora_queue: send '.$userdata['uid'].' missing username or password', LOGGER_DEBUG);
111 logger('diaspora_queue: delayed');
112 Queue::updateTime($x['id']);
117 function diaspora_settings(&$a,&$s) {
122 /* Add our stylesheet to the page so we can make our settings look nice */
124 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/diaspora/diaspora.css' . '" media="all" />' . "\r\n";
126 /* Get the current state of our config variables */
128 $enabled = PConfig::get(local_user(),'diaspora','post');
129 $checked = (($enabled) ? ' checked="checked" ' : '');
130 $css = (($enabled) ? '' : '-disabled');
132 $def_enabled = PConfig::get(local_user(),'diaspora','post_by_default');
134 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
136 $handle = PConfig::get(local_user(), 'diaspora', 'handle');
137 $password = PConfig::get(local_user(), 'diaspora', 'password');
138 $aspect = PConfig::get(local_user(),'diaspora','aspect');
142 $r = q("SELECT `addr` FROM `contact` WHERE `self` AND `uid` = %d", intval(local_user()));
143 if (DBM::is_result($r)) {
144 $status = L10n::t("Please remember: You can always be reached from Diaspora with your Friendica handle %s. ", $r[0]['addr']);
145 $status .= L10n::t('This connector is only meant if you still want to use your old Diaspora account for some time. ');
146 $status .= L10n::t('However, it is preferred that you tell your Diaspora contacts the new handle %s instead.', $r[0]['addr']);
151 if ($handle && $password) {
152 $conn = new Diaspora_Connection($handle, $password);
154 $aspects = $conn->getAspects();
156 $status = L10n::t("Can't login to your Diaspora account. Please check handle (in the format user@domain.tld) and password.");
160 /* Add some HTML to the existing form */
162 $s .= '<span id="settings_diaspora_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
163 $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. L10n::t('Diaspora Export').'</h3>';
165 $s .= '<div id="settings_diaspora_expanded" class="settings-block" style="display: none;">';
166 $s .= '<span class="fakelink" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
167 $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. L10n::t('Diaspora Export').'</h3>';
171 $s .= '<div id="diaspora-status-wrapper"><strong>';
173 $s .= '</strong></div><div class="clear"></div>';
176 $s .= '<div id="diaspora-enable-wrapper">';
177 $s .= '<label id="diaspora-enable-label" for="diaspora-checkbox">' . L10n::t('Enable Diaspora Post Addon') . '</label>';
178 $s .= '<input id="diaspora-checkbox" type="checkbox" name="diaspora" value="1" ' . $checked . '/>';
179 $s .= '</div><div class="clear"></div>';
181 $s .= '<div id="diaspora-username-wrapper">';
182 $s .= '<label id="diaspora-username-label" for="diaspora-username">' . L10n::t('Diaspora handle') . '</label>';
183 $s .= '<input id="diaspora-username" type="text" name="handle" value="' . $handle . '" />';
184 $s .= '</div><div class="clear"></div>';
186 $s .= '<div id="diaspora-password-wrapper">';
187 $s .= '<label id="diaspora-password-label" for="diaspora-password">' . L10n::t('Diaspora password') . '</label>';
188 $s .= '<input id="diaspora-password" type="password" name="password" value="' . $password . '" />';
189 $s .= '</div><div class="clear"></div>';
192 $single_aspect = new stdClass();
193 $single_aspect->id = 'all_aspects';
194 $single_aspect->name = L10n::t('All aspects');
195 $aspects[] = $single_aspect;
197 $single_aspect = new stdClass();
198 $single_aspect->id = 'public';
199 $single_aspect->name = L10n::t('Public');
200 $aspects[] = $single_aspect;
202 $s .= '<label id="diaspora-aspect-label" for="diaspora-aspect">' . L10n::t('Post to aspect:') . '</label>';
203 $s .= '<select name="aspect" id="diaspora-aspect">';
204 foreach($aspects as $single_aspect) {
205 if ($single_aspect->id == $aspect)
206 $s .= "<option value='".$single_aspect->id."' selected>".$single_aspect->name."</option>";
208 $s .= "<option value='".$single_aspect->id."'>".$single_aspect->name."</option>";
212 $s .= '<div class="clear"></div>';
215 $s .= '<div id="diaspora-bydefault-wrapper">';
216 $s .= '<label id="diaspora-bydefault-label" for="diaspora-bydefault">' . L10n::t('Post to Diaspora by default') . '</label>';
217 $s .= '<input id="diaspora-bydefault" type="checkbox" name="diaspora_bydefault" value="1" ' . $def_checked . '/>';
218 $s .= '</div><div class="clear"></div>';
220 /* provide a submit button */
222 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="diaspora-submit" name="diaspora-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div></div>';
227 function diaspora_settings_post(&$a,&$b) {
229 if(x($_POST,'diaspora-submit')) {
231 PConfig::set(local_user(),'diaspora','post',intval($_POST['diaspora']));
232 PConfig::set(local_user(),'diaspora','post_by_default',intval($_POST['diaspora_bydefault']));
233 PConfig::set(local_user(),'diaspora','handle',trim($_POST['handle']));
234 PConfig::set(local_user(),'diaspora','password',trim($_POST['password']));
235 PConfig::set(local_user(),'diaspora','aspect',trim($_POST['aspect']));
240 function diaspora_post_local(&$a,&$b) {
246 if (!local_user() || (local_user() != $b['uid'])) {
250 if ($b['private'] || $b['parent']) {
254 $diaspora_post = intval(PConfig::get(local_user(),'diaspora','post'));
256 $diaspora_enable = (($diaspora_post && x($_REQUEST,'diaspora_enable')) ? intval($_REQUEST['diaspora_enable']) : 0);
258 if ($b['api_source'] && intval(PConfig::get(local_user(),'diaspora','post_by_default'))) {
259 $diaspora_enable = 1;
262 if (!$diaspora_enable) {
266 if (strlen($b['postopts'])) {
267 $b['postopts'] .= ',';
270 $b['postopts'] .= 'diaspora';
276 function diaspora_send(&$a,&$b) {
277 $hostname = $a->get_hostname();
279 logger('diaspora_send: invoked');
281 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
285 if(! strstr($b['postopts'],'diaspora')) {
289 if($b['parent'] != $b['id']) {
293 // Dont't post if the post doesn't belong to us.
294 // This is a check for forum postings
295 $self = dba::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
296 if ($b['contact-id'] != $self['id']) {
300 logger('diaspora_send: prepare posting', LOGGER_DEBUG);
302 $handle = PConfig::get($b['uid'],'diaspora','handle');
303 $password = PConfig::get($b['uid'],'diaspora','password');
304 $aspect = PConfig::get($b['uid'],'diaspora','aspect');
306 if ($handle && $password) {
307 logger('diaspora_send: all values seem to be okay', LOGGER_DEBUG);
311 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
314 foreach($matches as $mtch) {
315 $tag_arr[] = $mtch[2];
319 $tags = implode(',',$tag_arr);
321 $title = $b['title'];
323 // Insert a newline before and after a quote
324 $body = str_ireplace("[quote", "\n\n[quote", $body);
325 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
327 // Removal of tags and mentions
329 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
331 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
333 // remove multiple newlines
336 $body = str_replace("\n\n\n", "\n\n", $body);
337 } while ($oldbody != $body);
339 // convert to markdown
340 $body = BBCode::toMarkdown($body);
344 $body = "## ".html_entity_decode($title)."\n\n".$body;
346 require_once("addon/diaspora/diasphp.php");
349 logger('diaspora_send: prepare', LOGGER_DEBUG);
350 $conn = new Diaspora_Connection($handle, $password);
351 logger('diaspora_send: try to log in '.$handle, LOGGER_DEBUG);
353 logger('diaspora_send: try to send '.$body, LOGGER_DEBUG);
355 $conn->provider = $hostname;
356 $conn->postStatusMessage($body, $aspect);
358 logger('diaspora_send: success');
359 } catch (Exception $e) {
360 logger("diaspora_send: Error submitting the post: " . $e->getMessage());
362 logger('diaspora_send: requeueing '.$b['uid'], LOGGER_DEBUG);
364 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
366 $a->contact = $r[0]["id"];
368 $s = serialize(['url' => $url, 'item' => $b['id'], 'post' => $body]);
370 Queue::add($a->contact, NETWORK_DIASPORA2, $s);
371 notice(L10n::t('Diaspora post failed. Queued for retry.').EOL);