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