4 * Name: Diaspora Post Connector
5 * Description: Post to Diaspora
7 * Author: Michael Vogel <heluecht@pirati.ca>
10 function diaspora_install() {
11 register_hook('post_local', 'addon/diaspora/diaspora.php', 'diaspora_post_local');
12 register_hook('notifier_normal', 'addon/diaspora/diaspora.php', 'diaspora_send');
13 register_hook('jot_networks', 'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
14 register_hook('connector_settings', 'addon/diaspora/diaspora.php', 'diaspora_settings');
15 register_hook('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
16 register_hook('queue_predeliver', 'addon/diaspora/diaspora.php', 'diaspora_queue_hook');
18 function diaspora_uninstall() {
19 unregister_hook('post_local', 'addon/diaspora/diaspora.php', 'diaspora_post_local');
20 unregister_hook('notifier_normal', 'addon/diaspora/diaspora.php', 'diaspora_send');
21 unregister_hook('jot_networks', 'addon/diaspora/diaspora.php', 'diaspora_jot_nets');
22 unregister_hook('connector_settings', 'addon/diaspora/diaspora.php', 'diaspora_settings');
23 unregister_hook('connector_settings_post', 'addon/diaspora/diaspora.php', 'diaspora_settings_post');
24 unregister_hook('queue_predeliver', 'addon/diaspora/diaspora.php', 'diaspora_queue_hook');
28 function diaspora_jot_nets(&$a,&$b) {
32 $diaspora_post = get_pconfig(local_user(),'diaspora','post');
33 if(intval($diaspora_post) == 1) {
34 $diaspora_defpost = get_pconfig(local_user(),'diaspora','post_by_default');
35 $selected = ((intval($diaspora_defpost) == 1) ? ' checked="checked" ' : '');
36 $b .= '<div class="profile-jot-net"><input type="checkbox" name="diaspora_enable"' . $selected . ' value="1" /> '
37 . t('Post to Diaspora') . '</div>';
41 function diaspora_queue_hook(&$a,&$b) {
42 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
43 dbesc(NETWORK_DIASPORA2)
48 require_once('include/queue_fn.php');
51 if($x['network'] !== NETWORK_DIASPORA2)
54 logger('diaspora_queue: run');
56 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
57 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
65 $diaspora_username = get_pconfig($userdata['uid'],'diaspora','diaspora_username');
66 $diaspora_password = get_pconfig($userdata['uid'],'diaspora','diaspora_password');
67 $diaspora_url = get_pconfig($userdata['uid'],'diaspora','diaspora_url');
71 if($diaspora_url && $diaspora_username && $diaspora_password) {
72 require_once("addon/diaspora/diasphp.php");
74 logger('diaspora_queue: able to post for user '.$diaspora_username);
76 $z = unserialize($x['content']);
80 logger('diaspora_queue: post: '.$post, LOGGER_DATA);
83 logger('diaspora_queue: prepare', LOGGER_DEBUG);
84 $conn = new Diasphp($diaspora_url);
85 logger('diaspora_queue: try to log in '.$diaspora_username, LOGGER_DEBUG);
86 $conn->login($diaspora_username, $diaspora_password);
87 logger('diaspora_queue: try to send '.$body, LOGGER_DEBUG);
90 logger('diaspora_queue: send '.$userdata['uid'].' success', LOGGER_DEBUG);
94 remove_queue_item($x['id']);
95 } catch (Exception $e) {
96 logger("diaspora_queue: Send ".$userdata['uid']." failed: ".$e->getMessage(), LOGGER_DEBUG);
99 logger('diaspora_queue: send '.$userdata['uid'].' missing username or password', LOGGER_DEBUG);
102 logger('diaspora_queue: delayed');
103 update_queue_time($x['id']);
108 function diaspora_settings(&$a,&$s) {
113 /* Add our stylesheet to the page so we can make our settings look nice */
115 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/diaspora/diaspora.css' . '" media="all" />' . "\r\n";
117 /* Get the current state of our config variables */
119 $enabled = get_pconfig(local_user(),'diaspora','post');
121 $checked = (($enabled) ? ' checked="checked" ' : '');
123 $def_enabled = get_pconfig(local_user(),'diaspora','post_by_default');
125 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
127 $diaspora_username = get_pconfig(local_user(), 'diaspora', 'diaspora_username');
128 $diaspora_password = get_pconfig(local_user(), 'diaspora', 'diaspora_password');
129 $diaspora_url = get_pconfig(local_user(), 'diaspora', 'diaspora_url');
132 /* Add some HTML to the existing form */
134 $s .= '<span id="settings_diaspora_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
135 $s .= '<h3>' . t('Diaspora') . '</h3>';
137 $s .= '<div id="settings_diaspora_expanded" class="settings-block" style="display: none;">';
138 $s .= '<span class="fakelink" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
139 $s .= '<h3>' . t('Diaspora') . '</h3>';
142 $s .= '<div id="diaspora-enable-wrapper">';
143 $s .= '<label id="diaspora-enable-label" for="diaspora-checkbox">' . t('Enable Diaspora Post Plugin') . '</label>';
144 $s .= '<input id="diaspora-checkbox" type="checkbox" name="diaspora" value="1" ' . $checked . '/>';
145 $s .= '</div><div class="clear"></div>';
147 $s .= '<div id="diaspora-username-wrapper">';
148 $s .= '<label id="diaspora-username-label" for="diaspora-username">' . t('Diaspora username') . '</label>';
149 $s .= '<input id="diaspora-username" type="text" name="diaspora_username" value="' . $diaspora_username . '" />';
150 $s .= '</div><div class="clear"></div>';
152 $s .= '<div id="diaspora-password-wrapper">';
153 $s .= '<label id="diaspora-password-label" for="diaspora-password">' . t('Diaspora password') . '</label>';
154 $s .= '<input id="diaspora-password" type="password" name="diaspora_password" value="' . $diaspora_password . '" />';
155 $s .= '</div><div class="clear"></div>';
157 $s .= '<div id="diaspora-url-wrapper">';
158 $s .= '<label id="diaspora-url-label" for="diaspora-url">' . t('Diaspora site URL') . '</label>';
159 $s .= '<input id="diaspora-url" type="text" name="diaspora_url" value="' . $diaspora_url . '" />';
160 $s .= '</div><div class="clear"></div>';
162 $s .= '<div id="diaspora-bydefault-wrapper">';
163 $s .= '<label id="diaspora-bydefault-label" for="diaspora-bydefault">' . 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="' . t('Save Settings') . '" /></div></div>';
174 function diaspora_settings_post(&$a,&$b) {
176 if(x($_POST,'diaspora-submit')) {
178 set_pconfig(local_user(),'diaspora','post',intval($_POST['diaspora']));
179 set_pconfig(local_user(),'diaspora','post_by_default',intval($_POST['diaspora_bydefault']));
180 set_pconfig(local_user(),'diaspora','diaspora_username',trim($_POST['diaspora_username']));
181 set_pconfig(local_user(),'diaspora','diaspora_password',trim($_POST['diaspora_password']));
182 set_pconfig(local_user(),'diaspora','diaspora_url',trim($_POST['diaspora_url']));
188 function diaspora_post_local(&$a,&$b) {
193 if((! local_user()) || (local_user() != $b['uid']))
196 if($b['private'] || $b['parent'])
199 $diaspora_post = intval(get_pconfig(local_user(),'diaspora','post'));
201 $diaspora_enable = (($diaspora_post && x($_REQUEST,'diaspora_enable')) ? intval($_REQUEST['diaspora_enable']) : 0);
203 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'diaspora','post_by_default')))
204 $diaspora_enable = 1;
206 if(! $diaspora_enable)
209 if(strlen($b['postopts']))
210 $b['postopts'] .= ',';
211 $b['postopts'] .= 'diaspora';
217 function diaspora_send(&$a,&$b) {
219 logger('diaspora_send: invoked');
221 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
224 if(! strstr($b['postopts'],'diaspora'))
227 if($b['parent'] != $b['id'])
230 logger('diaspora_send: prepare posting', LOGGER_DEBUG);
232 $diaspora_username = get_pconfig($b['uid'],'diaspora','diaspora_username');
233 $diaspora_password = get_pconfig($b['uid'],'diaspora','diaspora_password');
234 $diaspora_url = get_pconfig($b['uid'],'diaspora','diaspora_url');
236 if($diaspora_url && $diaspora_username && $diaspora_password) {
238 logger('diaspora_send: all values seem to be okay', LOGGER_DEBUG);
240 require_once('include/bb2diaspora.php');
243 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
246 foreach($matches as $mtch) {
247 $tag_arr[] = $mtch[2];
251 $tags = implode(',',$tag_arr);
253 $title = $b['title'];
255 // Insert a newline before and after a quote
256 $body = str_ireplace("[quote", "\n\n[quote", $body);
257 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
259 // Removal of tags and mentions
261 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
263 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
265 // remove multiple newlines
268 $body = str_replace("\n\n\n", "\n\n", $body);
269 } while ($oldbody != $body);
271 // convert to markdown
272 $body = bb2diaspora($body, false, true);
276 $body = "## ".html_entity_decode($title)."\n\n".$body;
278 require_once("addon/diaspora/diasphp.php");
281 logger('diaspora_send: prepare', LOGGER_DEBUG);
282 $conn = new Diasphp($diaspora_url);
283 logger('diaspora_send: try to log in '.$diaspora_username, LOGGER_DEBUG);
284 $conn->login($diaspora_username, $diaspora_password);
285 logger('diaspora_send: try to send '.$body, LOGGER_DEBUG);
287 //throw new Exception('Test');
290 logger('diaspora_send: success');
291 } catch (Exception $e) {
292 logger("diaspora_send: Error submitting the post: " . $e->getMessage());
294 logger('diaspora_send: requeueing '.$b['uid'], LOGGER_DEBUG);
296 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
298 $a->contact = $r[0]["id"];
300 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $body));
301 require_once('include/queue_fn.php');
302 add_to_queue($a->contact,NETWORK_DIASPORA2,$s);
303 notice(t('Diaspora post failed. Queued for retry.').EOL);