]> git.mxchange.org Git - friendica-addons.git/blob - diaspora/diaspora.php
Merge pull request #175 from annando/master
[friendica-addons.git] / diaspora / diaspora.php
1 <?php
2
3 /**
4  * Name: Diaspora Post Connector
5  * Description: Post to Diaspora
6  * Version: 0.1
7  * Author: Michael Vogel <heluecht@pirati.ca>
8  */
9
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');
17 }
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');
25 }
26
27
28 function diaspora_jot_nets(&$a,&$b) {
29     if(! local_user())
30         return;
31
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>';
38     }
39 }
40
41 function diaspora_queue_hook(&$a,&$b) {
42         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
43                 dbesc(NETWORK_DIASPORA2)
44         );
45         if(! count($qi))
46                 return;
47
48         require_once('include/queue_fn.php');
49
50         foreach($qi as $x) {
51                 if($x['network'] !== NETWORK_DIASPORA2)
52                         continue;
53
54                 logger('diaspora_queue: run');
55
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",
58                         intval($x['cid'])
59                 );
60                 if(! count($r))
61                         continue;
62
63                 $userdata = $r[0];
64
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');
68
69                 $success = false;
70
71                 if($diaspora_url && $diaspora_username && $diaspora_password) {
72                         require_once("addon/diaspora/diasphp.php");
73
74                         logger('diaspora_queue: able to post for user '.$diaspora_username);
75
76                         $z = unserialize($x['content']);
77
78                         $post = $z['post'];
79
80                         logger('diaspora_queue: post: '.$post, LOGGER_DATA);
81
82                         try {
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);
88                                 $conn->post($post);
89
90                                 logger('diaspora_queue: send '.$userdata['uid'].' success', LOGGER_DEBUG);
91
92                                 $success = true;
93
94                                 remove_queue_item($x['id']);
95                         } catch (Exception $e) {
96                                 logger("diaspora_queue: Send ".$userdata['uid']." failed: ".$e->getMessage(), LOGGER_DEBUG);
97                         }
98                 } else
99                         logger('diaspora_queue: send '.$userdata['uid'].' missing username or password', LOGGER_DEBUG);
100
101                 if (!$success) {
102                         logger('diaspora_queue: delayed');
103                         update_queue_time($x['id']);
104                 }
105         }
106 }
107
108 function diaspora_settings(&$a,&$s) {
109
110         if(! local_user())
111                 return;
112
113         /* Add our stylesheet to the page so we can make our settings look nice */
114
115         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/diaspora/diaspora.css' . '" media="all" />' . "\r\n";
116
117         /* Get the current state of our config variables */
118
119         $enabled = get_pconfig(local_user(),'diaspora','post');
120
121         $checked = (($enabled) ? ' checked="checked" ' : '');
122
123         $def_enabled = get_pconfig(local_user(),'diaspora','post_by_default');
124
125         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
126
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');
130
131         $status = "";
132
133         if ($diaspora_username AND $diaspora_password AND $diaspora_url) {
134                 try {
135                         require_once("addon/diaspora/diasphp.php");
136
137                         $conn = new Diasphp($diaspora_url);
138                         $conn->login($diaspora_username, $diaspora_password);
139                 } catch (Exception $e) {
140                         $status = t("Can't login to your Diaspora account. Please check username and password and ensure you used the complete address (including http...)");
141                 }
142         }
143
144         /* Add some HTML to the existing form */
145
146         $s .= '<span id="settings_diaspora_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
147         $s .= '<h3>' . t('Diaspora') . '</h3>';
148         $s .= '</span>';
149         $s .= '<div id="settings_diaspora_expanded" class="settings-block" style="display: none;">';
150         $s .= '<span class="fakelink" onclick="openClose(\'settings_diaspora_expanded\'); openClose(\'settings_diaspora_inflated\');">';
151         $s .= '<h3>' . t('Diaspora') . '</h3>';
152         $s .= '</span>';
153
154         if ($status) {
155                 $s .= '<div id="diaspora-status-wrapper"><strong>';
156                 $s .= $status;
157                 $s .= '</strong></div><div class="clear"></div>';
158         }
159
160         $s .= '<div id="diaspora-enable-wrapper">';
161         $s .= '<label id="diaspora-enable-label" for="diaspora-checkbox">' . t('Enable Diaspora Post Plugin') . '</label>';
162         $s .= '<input id="diaspora-checkbox" type="checkbox" name="diaspora" value="1" ' . $checked . '/>';
163         $s .= '</div><div class="clear"></div>';
164
165         $s .= '<div id="diaspora-username-wrapper">';
166         $s .= '<label id="diaspora-username-label" for="diaspora-username">' . t('Diaspora username') . '</label>';
167         $s .= '<input id="diaspora-username" type="text" name="diaspora_username" value="' . $diaspora_username . '" />';
168         $s .= '</div><div class="clear"></div>';
169
170         $s .= '<div id="diaspora-password-wrapper">';
171         $s .= '<label id="diaspora-password-label" for="diaspora-password">' . t('Diaspora password') . '</label>';
172         $s .= '<input id="diaspora-password" type="password" name="diaspora_password" value="' . $diaspora_password . '" />';
173         $s .= '</div><div class="clear"></div>';
174
175         $s .= '<div id="diaspora-url-wrapper">';
176         $s .= '<label id="diaspora-url-label" for="diaspora-url">' . t('Diaspora site URL') . '</label>';
177         $s .= '<input id="diaspora-url" type="text" name="diaspora_url" value="' . $diaspora_url . '" />';
178         $s .= '</div><div class="clear"></div>';
179
180         $s .= '<div id="diaspora-bydefault-wrapper">';
181         $s .= '<label id="diaspora-bydefault-label" for="diaspora-bydefault">' . t('Post to Diaspora by default') . '</label>';
182         $s .= '<input id="diaspora-bydefault" type="checkbox" name="diaspora_bydefault" value="1" ' . $def_checked . '/>';
183         $s .= '</div><div class="clear"></div>';
184
185         /* provide a submit button */
186
187         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="diaspora-submit" name="diaspora-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
188
189 }
190
191
192 function diaspora_settings_post(&$a,&$b) {
193
194         if(x($_POST,'diaspora-submit')) {
195
196                 set_pconfig(local_user(),'diaspora','post',intval($_POST['diaspora']));
197                 set_pconfig(local_user(),'diaspora','post_by_default',intval($_POST['diaspora_bydefault']));
198                 set_pconfig(local_user(),'diaspora','diaspora_username',trim($_POST['diaspora_username']));
199                 set_pconfig(local_user(),'diaspora','diaspora_password',trim($_POST['diaspora_password']));
200                 set_pconfig(local_user(),'diaspora','diaspora_url',trim($_POST['diaspora_url']));
201
202         }
203
204 }
205
206 function diaspora_post_local(&$a,&$b) {
207
208         if($b['edit'])
209                 return;
210
211         if((! local_user()) || (local_user() != $b['uid']))
212                 return;
213
214         if($b['private'] || $b['parent'])
215                 return;
216
217         $diaspora_post   = intval(get_pconfig(local_user(),'diaspora','post'));
218
219         $diaspora_enable = (($diaspora_post && x($_REQUEST,'diaspora_enable')) ? intval($_REQUEST['diaspora_enable']) : 0);
220
221         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'diaspora','post_by_default')))
222                 $diaspora_enable = 1;
223
224     if(! $diaspora_enable)
225        return;
226
227     if(strlen($b['postopts']))
228        $b['postopts'] .= ',';
229      $b['postopts'] .= 'diaspora';
230 }
231
232
233
234
235 function diaspora_send(&$a,&$b) {
236
237         logger('diaspora_send: invoked');
238
239         if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
240                 return;
241
242         if(! strstr($b['postopts'],'diaspora'))
243                 return;
244
245         if($b['parent'] != $b['id'])
246                 return;
247
248         logger('diaspora_send: prepare posting', LOGGER_DEBUG);
249
250         $diaspora_username = get_pconfig($b['uid'],'diaspora','diaspora_username');
251         $diaspora_password = get_pconfig($b['uid'],'diaspora','diaspora_password');
252         $diaspora_url = get_pconfig($b['uid'],'diaspora','diaspora_url');
253
254         if($diaspora_url && $diaspora_username && $diaspora_password) {
255
256                 logger('diaspora_send: all values seem to be okay', LOGGER_DEBUG);
257
258                 require_once('include/bb2diaspora.php');
259                 $tag_arr = array();
260                 $tags = '';
261                 $x = preg_match_all('/\#\[(.*?)\](.*?)\[/',$b['tag'],$matches,PREG_SET_ORDER);
262
263                 if($x) {
264                         foreach($matches as $mtch) {
265                                 $tag_arr[] = $mtch[2];
266                         }
267                 }
268                 if(count($tag_arr))
269                         $tags = implode(',',$tag_arr);
270
271                 $title = $b['title'];
272                 $body = $b['body'];
273                 // Insert a newline before and after a quote
274                 $body = str_ireplace("[quote", "\n\n[quote", $body);
275                 $body = str_ireplace("[/quote]", "[/quote]\n\n", $body);
276
277                 // Removal of tags and mentions
278                 // #-tags
279                 $body = preg_replace('/#\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '#$2', $body);
280                 // @-mentions
281                 $body = preg_replace('/@\[url\=(\w+.*?)\](\w+.*?)\[\/url\]/i', '@$2', $body);
282
283                 // remove multiple newlines
284                 do {
285                         $oldbody = $body;
286                         $body = str_replace("\n\n\n", "\n\n", $body);
287                 } while ($oldbody != $body);
288
289                 // convert to markdown
290                 $body = bb2diaspora($body, false, true);
291
292                 // Adding the title
293                 if(strlen($title))
294                         $body = "## ".html_entity_decode($title)."\n\n".$body;
295
296                 require_once("addon/diaspora/diasphp.php");
297
298                 try {
299                         logger('diaspora_send: prepare', LOGGER_DEBUG);
300                         $conn = new Diasphp($diaspora_url);
301                         logger('diaspora_send: try to log in '.$diaspora_username, LOGGER_DEBUG);
302                         $conn->login($diaspora_username, $diaspora_password);
303                         logger('diaspora_send: try to send '.$body, LOGGER_DEBUG);
304
305                         //throw new Exception('Test');
306                         $conn->post($body);
307
308                         logger('diaspora_send: success');
309                 } catch (Exception $e) {
310                         logger("diaspora_send: Error submitting the post: " . $e->getMessage());
311
312                         logger('diaspora_send: requeueing '.$b['uid'], LOGGER_DEBUG);
313
314                         $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
315                         if (count($r))
316                                 $a->contact = $r[0]["id"];
317
318                         $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $body));
319                         require_once('include/queue_fn.php');
320                         add_to_queue($a->contact,NETWORK_DIASPORA2,$s);
321                         notice(t('Diaspora post failed. Queued for retry.').EOL);
322                 }
323         }
324 }