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\Core\PConfig;
18 use Friendica\Core\Protocol;
19 use Friendica\Database\DBA;
20 use Friendica\Core\Worker;
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_uninstall()
35 Hook::unregister('hook_fork', 'addon/diaspora/diaspora.php', 'diaspora_hook_fork');
36 Hook::unregister('post_local', 'addon/diaspora/diaspora.php', 'diaspora_post_local');
37 Hook::unregister('notifier_normal', 'addon/diaspora/diaspora.php', 'diaspora_send');
38 Hook::unregister('jot_networks', 'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
39 Hook::unregister('connector_settings', 'addon/diaspora/diaspora.php', 'diaspora_settings');
40 Hook::unregister('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
43 function diaspora_jot_nets(App $a, array &$jotnets_fields)
49 if (PConfig::get(local_user(), 'diaspora', 'post')) {
54 L10n::t('Post to Diaspora'),
55 PConfig::get(local_user(), 'diaspora', 'post_by_default')
61 function diaspora_settings(App $a, &$s)
67 /* Add our stylesheet to the page so we can make our settings look nice */
69 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->getBaseURL() . '/addon/diaspora/diaspora.css' . '" media="all" />' . "\r\n";
71 /* Get the current state of our config variables */
73 $enabled = PConfig::get(local_user(),'diaspora','post');
74 $checked = (($enabled) ? ' checked="checked" ' : '');
75 $css = (($enabled) ? '' : '-disabled');
77 $def_enabled = PConfig::get(local_user(),'diaspora','post_by_default');
79 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
81 $handle = PConfig::get(local_user(), 'diaspora', 'handle');
82 $password = PConfig::get(local_user(), 'diaspora', 'password');
83 $aspect = PConfig::get(local_user(),'diaspora','aspect');
87 $r = q("SELECT `addr` FROM `contact` WHERE `self` AND `uid` = %d", intval(local_user()));
89 if (DBA::isResult($r)) {
90 $status = L10n::t("Please remember: You can always be reached from Diaspora with your Friendica handle %s. ", $r[0]['addr']);
91 $status .= L10n::t('This connector is only meant if you still want to use your old Diaspora account for some time. ');
92 $status .= L10n::t('However, it is preferred that you tell your Diaspora contacts the new handle %s instead.', $r[0]['addr']);
97 if ($handle && $password) {
98 $conn = new Diaspora_Connection($handle, $password);
100 $aspects = $conn->getAspects();
103 $status = L10n::t("Can't login to your Diaspora account. Please check handle (in the format user@domain.tld) and password.");
107 /* Add some HTML to the existing form */
109 $s .= '<span id="settings_diaspora_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
110 $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. L10n::t('Diaspora Export').'</h3>';
112 $s .= '<div id="settings_diaspora_expanded" class="settings-block" style="display: none;">';
113 $s .= '<span class="fakelink" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
114 $s .= '<img class="connector'.$css.'" src="images/diaspora-logo.png" /><h3 class="connector">'. L10n::t('Diaspora Export').'</h3>';
118 $s .= '<div id="diaspora-status-wrapper"><strong>';
120 $s .= '</strong></div><div class="clear"></div>';
123 $s .= '<div id="diaspora-enable-wrapper">';
124 $s .= '<label id="diaspora-enable-label" for="diaspora-checkbox">' . L10n::t('Enable Diaspora Post Addon') . '</label>';
125 $s .= '<input id="diaspora-checkbox" type="checkbox" name="diaspora" value="1" ' . $checked . '/>';
126 $s .= '</div><div class="clear"></div>';
128 $s .= '<div id="diaspora-username-wrapper">';
129 $s .= '<label id="diaspora-username-label" for="diaspora-username">' . L10n::t('Diaspora handle') . '</label>';
130 $s .= '<input id="diaspora-username" type="text" name="handle" value="' . $handle . '" />';
131 $s .= '</div><div class="clear"></div>';
133 $s .= '<div id="diaspora-password-wrapper">';
134 $s .= '<label id="diaspora-password-label" for="diaspora-password">' . L10n::t('Diaspora password') . '</label>';
135 $s .= '<input id="diaspora-password" type="password" name="password" value="' . $password . '" />';
136 $s .= '</div><div class="clear"></div>';
139 $single_aspect = new stdClass();
140 $single_aspect->id = 'all_aspects';
141 $single_aspect->name = L10n::t('All aspects');
142 $aspects[] = $single_aspect;
144 $single_aspect = new stdClass();
145 $single_aspect->id = 'public';
146 $single_aspect->name = L10n::t('Public');
147 $aspects[] = $single_aspect;
149 $s .= '<label id="diaspora-aspect-label" for="diaspora-aspect">' . L10n::t('Post to aspect:') . '</label>';
150 $s .= '<select name="aspect" id="diaspora-aspect">';
151 foreach($aspects as $single_aspect) {
152 if ($single_aspect->id == $aspect)
153 $s .= "<option value='".$single_aspect->id."' selected>".$single_aspect->name."</option>";
155 $s .= "<option value='".$single_aspect->id."'>".$single_aspect->name."</option>";
159 $s .= '<div class="clear"></div>';
162 $s .= '<div id="diaspora-bydefault-wrapper">';
163 $s .= '<label id="diaspora-bydefault-label" for="diaspora-bydefault">' . L10n::t('Post to Diaspora by default') . '</label>';
164 $s .= '<input id="diaspora-bydefault" type="checkbox" name="diaspora_bydefault" value="1" ' . $def_checked . '/>';
165 $s .= '</div><div class="clear"></div>';
167 /* provide a submit button */
169 $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>';
174 function diaspora_settings_post(App $a, &$b)
176 if (!empty($_POST['diaspora-submit'])) {
177 PConfig::set(local_user(),'diaspora', 'post' , intval($_POST['diaspora']));
178 PConfig::set(local_user(),'diaspora', 'post_by_default', intval($_POST['diaspora_bydefault']));
179 PConfig::set(local_user(),'diaspora', 'handle' , trim($_POST['handle']));
180 PConfig::set(local_user(),'diaspora', 'password' , trim($_POST['password']));
181 PConfig::set(local_user(),'diaspora', 'aspect' , trim($_POST['aspect']));
185 function diaspora_hook_fork(&$a, &$b)
187 if ($b['name'] != 'notifier_normal') {
193 if ($post['deleted'] || $post['private'] || ($post['created'] !== $post['edited']) ||
194 !strstr($post['postopts'], 'diaspora') || ($post['parent'] != $post['id'])) {
195 $b['execute'] = false;
200 function diaspora_post_local(App $a, array &$b)
206 if (!local_user() || (local_user() != $b['uid'])) {
210 if ($b['private'] || $b['parent']) {
214 $diaspora_post = intval(PConfig::get(local_user(),'diaspora','post'));
216 $diaspora_enable = (($diaspora_post && !empty($_REQUEST['diaspora_enable'])) ? intval($_REQUEST['diaspora_enable']) : 0);
218 if ($b['api_source'] && intval(PConfig::get(local_user(),'diaspora','post_by_default'))) {
219 $diaspora_enable = 1;
222 if (!$diaspora_enable) {
226 if (strlen($b['postopts'])) {
227 $b['postopts'] .= ',';
230 $b['postopts'] .= 'diaspora';
233 function diaspora_send(App $a, array &$b)
235 $hostname = DI::baseUrl()->getHostname();
237 Logger::log('diaspora_send: invoked');
239 if ($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited'])) {
243 if (!strstr($b['postopts'],'diaspora')) {
247 if ($b['parent'] != $b['id']) {
251 // Dont't post if the post doesn't belong to us.
252 // This is a check for forum postings
253 $self = DBA::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
255 if ($b['contact-id'] != $self['id']) {
259 Logger::log('diaspora_send: prepare posting', Logger::DEBUG);
261 $handle = PConfig::get($b['uid'],'diaspora','handle');
262 $password = PConfig::get($b['uid'],'diaspora','password');
263 $aspect = PConfig::get($b['uid'],'diaspora','aspect');
265 if ($handle && $password) {
266 Logger::log('diaspora_send: all values seem to be okay', Logger::DEBUG);
270 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
273 foreach ($matches as $mtch) {
274 $tag_arr[] = $mtch[2];
278 if (count($tag_arr)) {
279 $tags = implode(',',$tag_arr);
282 $title = $b['title'];
284 // Insert a newline before and after a quote
285 $body = str_ireplace("[quote", "\n\n[quote", $body);
286 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
288 // Removal of tags and mentions
290 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
292 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
294 // remove multiple newlines
297 $body = str_replace("\n\n\n", "\n\n", $body);
298 } while ($oldbody != $body);
300 // convert to markdown
301 $body = BBCode::toMarkdown($body);
304 if (strlen($title)) {
305 $body = "## ".html_entity_decode($title)."\n\n".$body;
308 require_once "addon/diaspora/diasphp.php";
311 Logger::log('diaspora_send: prepare', Logger::DEBUG);
312 $conn = new Diaspora_Connection($handle, $password);
313 Logger::log('diaspora_send: try to log in '.$handle, Logger::DEBUG);
315 Logger::log('diaspora_send: try to send '.$body, Logger::DEBUG);
317 $conn->provider = $hostname;
318 $conn->postStatusMessage($body, $aspect);
320 Logger::log('diaspora_send: success');
321 } catch (Exception $e) {
322 Logger::log("diaspora_send: Error submitting the post: " . $e->getMessage());
324 Logger::log('diaspora_send: requeueing '.$b['uid'], Logger::DEBUG);