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