]> git.mxchange.org Git - friendica-addons.git/blob - pumpio/pumpio.php
Pumpio: Posted videos look better now
[friendica-addons.git] / pumpio / pumpio.php
1 <?php
2 /**
3  * Name: pump.io Post Connector
4  * Description: Bidirectional (posting, relaying and reading) connector for pump.io.
5  * Version: 0.2
6  * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
7  */
8 require('addon/pumpio/oauth/http.php');
9 require('addon/pumpio/oauth/oauth_client.php');
10 require_once('include/enotify.php');
11
12 define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
13
14 function pumpio_install() {
15         register_hook('post_local',           'addon/pumpio/pumpio.php', 'pumpio_post_local');
16         register_hook('notifier_normal',      'addon/pumpio/pumpio.php', 'pumpio_send');
17         register_hook('jot_networks',         'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
18         register_hook('connector_settings',      'addon/pumpio/pumpio.php', 'pumpio_settings');
19         register_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
20         register_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
21         register_hook('queue_predeliver', 'addon/pumpio/pumpio.php', 'pumpio_queue_hook');
22         register_hook('check_item_notification','addon/pumpio/pumpio.php', 'pumpio_check_item_notification');
23 }
24
25 function pumpio_uninstall() {
26         unregister_hook('post_local',       'addon/pumpio/pumpio.php', 'pumpio_post_local');
27         unregister_hook('notifier_normal',  'addon/pumpio/pumpio.php', 'pumpio_send');
28         unregister_hook('jot_networks',     'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
29         unregister_hook('connector_settings',      'addon/pumpio/pumpio.php', 'pumpio_settings');
30         unregister_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
31         unregister_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
32         unregister_hook('queue_predeliver', 'addon/pumpio/pumpio.php', 'pumpio_queue_hook');
33         unregister_hook('check_item_notification','addon/pumpio/pumpio.php', 'pumpio_check_item_notification');
34 }
35
36 function pumpio_module() {}
37
38 function pumpio_content(&$a) {
39
40         if(! local_user()) {
41                 notice( t('Permission denied.') . EOL);
42                 return '';
43         }
44
45         require_once("mod/settings.php");
46         settings_init($a);
47
48         if (isset($a->argv[1]))
49                 switch ($a->argv[1]) {
50                         case "connect":
51                                 $o = pumpio_connect($a);
52                                 break;
53                         default:
54                                 $o = print_r($a->argv, true);
55                                 break;
56                 }
57         else
58                 $o = pumpio_connect($a);
59
60         return $o;
61 }
62
63 function pumpio_check_item_notification($a, &$notification_data) {
64         $hostname = get_pconfig($notification_data["uid"], 'pumpio','host');
65         $username = get_pconfig($notification_data["uid"], "pumpio", "user");
66
67         $notification_data["profiles"][] = "https://".$hostname."/".$username;
68 }
69
70
71 function pumpio_registerclient(&$a, $host) {
72
73         $url = "https://".$host."/api/client/register";
74
75         $params = array();
76
77         $application_name  = get_config('pumpio', 'application_name');
78
79         if ($application_name == "")
80                 $application_name = $a->get_hostname();
81
82         $adminlist = explode(",", str_replace(" ", "", $a->config['admin_email']));
83
84         $params["type"] = "client_associate";
85         $params["contacts"] = $adminlist[0];
86         $params["application_type"] = "native";
87         $params["application_name"] = $application_name;
88         $params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
89         $params["redirect_uris"] = $a->get_baseurl()."/pumpio/connect";
90
91         logger("pumpio_registerclient: ".$url." parameters ".print_r($params, true), LOGGER_DEBUG);
92
93         $ch = curl_init($url);
94         curl_setopt($ch, CURLOPT_HEADER, false);
95         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
96         curl_setopt($ch, CURLOPT_POST,1);
97         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
98         curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
99
100         $s = curl_exec($ch);
101         $curl_info = curl_getinfo($ch);
102
103         if ($curl_info["http_code"] == "200") {
104                 $values = json_decode($s);
105                 logger("pumpio_registerclient: success ".print_r($values, true), LOGGER_DEBUG);
106                 return($values);
107         }
108         logger("pumpio_registerclient: failed: ".print_r($curl_info, true), LOGGER_DEBUG);
109         return(false);
110
111 }
112
113 function pumpio_connect(&$a) {
114         // Start a session.  This is necessary to hold on to  a few keys the callback script will also need
115         session_start();
116
117         // Define the needed keys
118         $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
119         $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
120         $hostname = get_pconfig(local_user(), 'pumpio','host');
121
122         if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
123                 logger("pumpio_connect: register client");
124                 $clientdata = pumpio_registerclient($a, $hostname);
125                 set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
126                 set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
127
128                 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
129                 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
130
131                 logger("pumpio_connect: ckey: ".$consumer_key." csecrect: ".$consumer_secret, LOGGER_DEBUG);
132         }
133
134         if (($consumer_key == "") OR ($consumer_secret == "")) {
135                 logger("pumpio_connect: ".sprintf("Unable to register the client at the pump.io server '%s'.", $hostname));
136
137                 $o .= sprintf(t("Unable to register the client at the pump.io server '%s'."), $hostname);
138                 return($o);
139         }
140
141         // The callback URL is the script that gets called after the user authenticates with pumpio
142         $callback_url = $a->get_baseurl()."/pumpio/connect";
143
144         // Let's begin.  First we need a Request Token.  The request token is required to send the user
145         // to pumpio's login page.
146
147         // Create a new instance of the TumblrOAuth library.  For this step, all we need to give the library is our
148         // Consumer Key and Consumer Secret
149         $client = new oauth_client_class;
150         $client->debug = 1;
151         $client->server = '';
152         $client->oauth_version = '1.0a';
153         $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
154         $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
155         $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
156         $client->url_parameters = false;
157         $client->authorization_header = true;
158         $client->redirect_uri = $callback_url;
159         $client->client_id = $consumer_key;
160         $client->client_secret = $consumer_secret;
161
162         if (($success = $client->Initialize())) {
163                 if (($success = $client->Process())) {
164                         if (strlen($client->access_token)) {
165                                 logger("pumpio_connect: otoken: ".$client->access_token." osecrect: ".$client->access_token_secret, LOGGER_DEBUG);
166                                 set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
167                                 set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
168                         }
169                 }
170                 $success = $client->Finalize($success);
171         }
172         if($client->exit)
173                 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
174
175         if($success) {
176                 logger("pumpio_connect: authenticated");
177                 $o .= t("You are now authenticated to pumpio.");
178                 $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.t("return to the connector page").'</a>';
179         } else {
180                 logger("pumpio_connect: could not connect");
181                 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
182         }
183
184         return($o);
185 }
186
187 function pumpio_jot_nets(&$a,&$b) {
188         if(! local_user())
189                 return;
190
191         $pumpio_post = get_pconfig(local_user(),'pumpio','post');
192         if(intval($pumpio_post) == 1) {
193                 $pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
194                 $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
195                 $b .= '<div class="profile-jot-net"><input type="checkbox" name="pumpio_enable"' . $selected . ' value="1" /> '
196                         . t('Post to pumpio') . '</div>';
197         }
198 }
199
200
201 function pumpio_settings(&$a,&$s) {
202
203         if(! local_user())
204                 return;
205
206         /* Add our stylesheet to the page so we can make our settings look nice */
207
208         $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
209
210         /* Get the current state of our config variables */
211
212         $import_enabled = get_pconfig(local_user(),'pumpio','import');
213         $import_checked = (($import_enabled) ? ' checked="checked" ' : '');
214
215         $enabled = get_pconfig(local_user(),'pumpio','post');
216         $checked = (($enabled) ? ' checked="checked" ' : '');
217         $css = (($enabled) ? '' : '-disabled');
218
219         $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
220         $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
221
222         $public_enabled = get_pconfig(local_user(),'pumpio','public');
223         $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
224
225         $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
226         $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
227
228         $servername = get_pconfig(local_user(), "pumpio", "host");
229         $username = get_pconfig(local_user(), "pumpio", "user");
230
231         /* Add some HTML to the existing form */
232
233         $s .= '<span id="settings_pumpio_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_pumpio_expanded\'); openClose(\'settings_pumpio_inflated\');">';
234         $s .= '<img class="connector'.$css.'" src="images/pumpio.png" /><h3 class="connector">'. t('Pump.io Import/Export/Mirror').'</h3>';
235         $s .= '</span>';
236         $s .= '<div id="settings_pumpio_expanded" class="settings-block" style="display: none;">';
237         $s .= '<span class="fakelink" onclick="openClose(\'settings_pumpio_expanded\'); openClose(\'settings_pumpio_inflated\');">';
238         $s .= '<img class="connector'.$css.'" src="images/pumpio.png" /><h3 class="connector">'. t('Pump.io Import/Export/Mirror').'</h3>';
239         $s .= '</span>';
240
241         $s .= '<div id="pumpio-username-wrapper">';
242         $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username (without the servername)').'</label>';
243         $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
244         $s .= '</div><div class="clear"></div>';
245
246         $s .= '<div id="pumpio-servername-wrapper">';
247         $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername (without "http://" or "https://" )').'</label>';
248         $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
249         $s .= '</div><div class="clear"></div>';
250
251         if (($username != '') AND ($servername != '')) {
252
253                 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
254                 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
255
256                 $s .= '<div id="pumpio-password-wrapper">';
257                 if (($oauth_token == "") OR ($oauth_token_secret == "")) {
258                         $s .= '<div id="pumpio-authenticate-wrapper">';
259                         $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Authenticate your pump.io connection").'</a>';
260                         $s .= '</div><div class="clear"></div>';
261                 } else {
262                         $s .= '<div id="pumpio-import-wrapper">';
263                         $s .= '<label id="pumpio-import-label" for="pumpio-import">' . t('Import the remote timeline') . '</label>';
264                         $s .= '<input id="pumpio-import" type="checkbox" name="pumpio_import" value="1" ' . $import_checked . '/>';
265                         $s .= '</div><div class="clear"></div>';
266
267                         $s .= '<div id="pumpio-enable-wrapper">';
268                         $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
269                         $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
270                         $s .= '</div><div class="clear"></div>';
271
272                         $s .= '<div id="pumpio-bydefault-wrapper">';
273                         $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
274                         $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
275                         $s .= '</div><div class="clear"></div>';
276
277                         $s .= '<div id="pumpio-public-wrapper">';
278                         $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
279                         $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
280                         $s .= '</div><div class="clear"></div>';
281
282                         $s .= '<div id="pumpio-mirror-wrapper">';
283                         $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
284                         $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
285                         $s .= '</div><div class="clear"></div>';
286
287                         $s .= '<div id="pumpio-delete-wrapper">';
288                         $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . t('Check to delete this preset') . '</label>';
289                         $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
290                         $s .= '</div><div class="clear"></div>';
291                 }
292
293                 $s .= '</div><div class="clear"></div>';
294         }
295
296         /* provide a submit button */
297
298         $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
299 }
300
301
302 function pumpio_settings_post(&$a,&$b) {
303
304         if(x($_POST,'pumpio-submit')) {
305                 if(x($_POST,'pumpio_delete')) {
306                         set_pconfig(local_user(),'pumpio','consumer_key','');
307                         set_pconfig(local_user(),'pumpio','consumer_secret','');
308                         set_pconfig(local_user(),'pumpio','oauth_token','');
309                         set_pconfig(local_user(),'pumpio','oauth_token_secret','');
310                         set_pconfig(local_user(),'pumpio','post',false);
311                         set_pconfig(local_user(),'pumpio','import',false);
312                         set_pconfig(local_user(),'pumpio','host','');
313                         set_pconfig(local_user(),'pumpio','user','');
314                         set_pconfig(local_user(),'pumpio','public',false);
315                         set_pconfig(local_user(),'pumpio','mirror',false);
316                         set_pconfig(local_user(),'pumpio','post_by_default',false);
317                         set_pconfig(local_user(),'pumpio','lastdate', 0);
318                         set_pconfig(local_user(),'pumpio','last_id', '');
319                 } else {
320                         // filtering the username if it is filled wrong
321                         $user = $_POST['pumpio_user'];
322                         if (strstr($user, "@")) {
323                                 $pos = strpos($user, "@");
324                                 if ($pos > 0)
325                                         $user = substr($user, 0, $pos);
326                         }
327
328                         // Filtering the hostname if someone is entering it with "http"
329                         $host = $_POST['pumpio_host'];
330                         $host = trim($host);
331                         $host = str_replace(array("https://", "http://"), array("", ""), $host);
332
333                         set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
334                         set_pconfig(local_user(),'pumpio','import',$_POST['pumpio_import']);
335                         set_pconfig(local_user(),'pumpio','host',$host);
336                         set_pconfig(local_user(),'pumpio','user',$user);
337                         set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
338                         set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
339                         set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
340
341                         if (!$_POST['pumpio_mirror'])
342                                 del_pconfig(local_user(),'pumpio','lastdate');
343
344                         //header("Location: ".$a->get_baseurl()."/pumpio/connect");
345                 }
346         }
347 }
348
349 function pumpio_post_local(&$a,&$b) {
350
351         if((! local_user()) || (local_user() != $b['uid']))
352                 return;
353
354         $pumpio_post   = intval(get_pconfig(local_user(),'pumpio','post'));
355
356         $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
357
358         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
359                 $pumpio_enable = 1;
360
361         if(! $pumpio_enable)
362                 return;
363
364         if(strlen($b['postopts']))
365                 $b['postopts'] .= ',';
366
367         $b['postopts'] .= 'pumpio';
368 }
369
370
371
372
373 function pumpio_send(&$a,&$b) {
374
375         if (!get_pconfig($b["uid"],'pumpio','import')) {
376                 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
377                         return;
378         }
379
380         logger("pumpio_send: parameter ".print_r($b, true), LOGGER_DATA);
381
382         if($b['parent'] != $b['id']) {
383                 // Looking if its a reply to a pumpio post
384                 $r = q("SELECT item.* FROM item, contact WHERE item.id = %d AND item.uid = %d AND contact.id = `contact-id` AND contact.network='%s'LIMIT 1",
385                         intval($b["parent"]),
386                         intval($b["uid"]),
387                         dbesc(NETWORK_PUMPIO));
388
389                 if(!count($r)) {
390                         logger("pumpio_send: no pumpio post ".$b["parent"]);
391                         return;
392                 } else {
393                         $iscomment = true;
394                         $orig_post = $r[0];
395                 }
396         } else {
397                 $iscomment = false;
398
399                 $receiver = pumpio_getreceiver($a, $b);
400
401                 logger("pumpio_send: receiver ".print_r($receiver, true));
402
403                 if (!count($receiver) AND ($b['private'] OR !strstr($b['postopts'],'pumpio')))
404                         return;
405         }
406
407         if($b['verb'] == ACTIVITY_LIKE) {
408                 if ($b['deleted'])
409                         pumpio_action($a, $b["uid"], $b["thr-parent"], "unlike");
410                 else
411                         pumpio_action($a, $b["uid"], $b["thr-parent"], "like");
412                 return;
413         }
414
415         if($b['verb'] == ACTIVITY_DISLIKE)
416                 return;
417
418         if (($b['verb'] == ACTIVITY_POST) AND ($b['created'] !== $b['edited']) AND !$b['deleted'])
419                         pumpio_action($a, $b["uid"], $b["uri"], "update", $b["body"]);
420
421         if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
422                         pumpio_action($a, $b["uid"], $b["uri"], "delete");
423
424         if($b['deleted'] || ($b['created'] !== $b['edited']))
425                 return;
426
427         // if post comes from pump.io don't send it back
428         if($b['app'] == "pump.io")
429                 return;
430
431         // To-Do;
432         // Support for native shares
433         // http://<hostname>/api/<type>/shares?id=<the-object-id>
434
435         $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
436         $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
437         $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
438         $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
439
440         $host = get_pconfig($b['uid'], "pumpio", "host");
441         $user = get_pconfig($b['uid'], "pumpio", "user");
442         $public = get_pconfig($b['uid'], "pumpio", "public");
443
444         if($oauth_token && $oauth_token_secret) {
445
446                 require_once('include/bbcode.php');
447
448                 $title = trim($b['title']);
449
450                 $content = bbcode($b['body'], false, false, 4);
451
452                 $params = array();
453
454                 $params["verb"] = "post";
455
456                 if (!$iscomment) {
457                         $params["object"] = array(
458                                                 'objectType' => "note",
459                                                 'content' => $content);
460
461                         if ($title != "")
462                                 $params["object"]["displayName"] = $title;
463
464                         if (count($receiver["to"]))
465                                 $params["to"] = $receiver["to"];
466
467                         if (count($receiver["bto"]))
468                                 $params["bto"] = $receiver["bto"];
469
470                         if (count($receiver["cc"]))
471                                 $params["cc"] = $receiver["cc"];
472
473                         if (count($receiver["bcc"]))
474                                 $params["bcc"] = $receiver["bcc"];
475
476                  } else {
477                         $inReplyTo = array("id" => $orig_post["uri"],
478                                         "objectType" => "note");
479
480                         if (($orig_post["object-type"] != "") AND (strstr($orig_post["object-type"], NAMESPACE_ACTIVITY_SCHEMA)))
481                                 $inReplyTo["objectType"] = str_replace(NAMESPACE_ACTIVITY_SCHEMA, '', $orig_post["object-type"]);
482
483                         $params["object"] = array(
484                                                 'objectType' => "comment",
485                                                 'content' => $content,
486                                                 'inReplyTo' => $inReplyTo);
487
488                         if ($title != "")
489                                 $params["object"]["displayName"] = $title;
490                 }
491
492                 $client = new oauth_client_class;
493                 $client->oauth_version = '1.0a';
494                 $client->url_parameters = false;
495                 $client->authorization_header = true;
496                 $client->access_token = $oauth_token;
497                 $client->access_token_secret = $oauth_token_secret;
498                 $client->client_id = $consumer_key;
499                 $client->client_secret = $consumer_secret;
500
501                 $username = $user.'@'.$host;
502                 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
503
504                 if (pumpio_reachable($url))
505                         $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
506                 else
507                         $success = false;
508
509                 if($success) {
510
511                         if ($user->generator->displayName)
512                                 set_pconfig($b["uid"], "pumpio", "application_name", $user->generator->displayName);
513
514                         $post_id = $user->object->id;
515                         logger('pumpio_send '.$username.': success '.$post_id);
516                         if($post_id AND $iscomment) {
517                                 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
518                                 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
519                                         dbesc($post_id),
520                                         intval($b['id'])
521                                 );
522                         }
523                 } else {
524                         logger('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user,true));
525
526                         $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
527                         if (count($r))
528                                 $a->contact = $r[0]["id"];
529
530                         $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $params));
531                         require_once('include/queue_fn.php');
532                         add_to_queue($a->contact,NETWORK_PUMPIO,$s);
533                         notice(t('Pump.io post failed. Queued for retry.').EOL);
534                 }
535
536         }
537 }
538
539 function pumpio_action(&$a, $uid, $uri, $action, $content = "") {
540
541         // Don't do likes and other stuff if you don't import the timeline
542         if (!get_pconfig($uid,'pumpio','import'))
543                 return;
544
545         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
546         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
547         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
548         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
549         $hostname = get_pconfig($uid, 'pumpio','host');
550         $username = get_pconfig($uid, "pumpio", "user");
551
552         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
553                                 dbesc($uri),
554                                 intval($uid)
555         );
556
557         if (!count($r))
558                 return;
559
560         $orig_post = $r[0];
561
562         if ($orig_post["extid"] AND !strstr($orig_post["extid"], "/proxy/"))
563                 $uri = $orig_post["extid"];
564         else
565                 $uri = $orig_post["uri"];
566
567         if (($orig_post["object-type"] != "") AND (strstr($orig_post["object-type"], NAMESPACE_ACTIVITY_SCHEMA)))
568                 $objectType = str_replace(NAMESPACE_ACTIVITY_SCHEMA, '', $orig_post["object-type"]);
569         elseif (strstr($uri, "/api/comment/"))
570                 $objectType = "comment";
571         elseif (strstr($uri, "/api/note/"))
572                 $objectType = "note";
573         elseif (strstr($uri, "/api/image/"))
574                 $objectType = "image";
575
576         $params["verb"] = $action;
577         $params["object"] = array('id' => $uri,
578                                 "objectType" => $objectType,
579                                 "content" => $content);
580
581         $client = new oauth_client_class;
582         $client->oauth_version = '1.0a';
583         $client->authorization_header = true;
584         $client->url_parameters = false;
585
586         $client->client_id = $ckey;
587         $client->client_secret = $csecret;
588         $client->access_token = $otoken;
589         $client->access_token_secret = $osecret;
590
591         $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
592
593         if (pumpio_reachable($url))
594                 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
595         else
596                 $success = false;
597
598         if($success)
599                 logger('pumpio_action '.$username.' '.$action.': success '.$uri);
600         else {
601                 logger('pumpio_action '.$username.' '.$action.': general error: '.$uri.' '.print_r($user,true));
602
603                 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
604                 if (count($r))
605                         $a->contact = $r[0]["id"];
606
607                 $s = serialize(array('url' => $url, 'item' => $orig_post["id"], 'post' => $params));
608                 require_once('include/queue_fn.php');
609                 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
610                 notice(t('Pump.io like failed. Queued for retry.').EOL);
611         }
612 }
613
614 function pumpio_sync(&$a) {
615         $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = 'pumpio'",
616                 $plugin);
617
618         if (!count($r))
619                 return;
620
621         $last = get_config('pumpio','last_poll');
622
623         $poll_interval = intval(get_config('pumpio','poll_interval'));
624         if(! $poll_interval)
625                 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
626
627         if($last) {
628                 $next = $last + ($poll_interval * 60);
629                 if($next > time()) {
630                         logger('pumpio: poll intervall not reached');
631                         return;
632                 }
633         }
634         logger('pumpio: cron_start');
635
636         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
637         if(count($r)) {
638                 foreach($r as $rr) {
639                         logger('pumpio: mirroring user '.$rr['uid']);
640                         pumpio_fetchtimeline($a, $rr['uid']);
641                 }
642         }
643
644         $abandon_days = intval(get_config('system','account_abandon_days'));
645         if ($abandon_days < 1)
646                 $abandon_days = 0;
647
648         $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400);
649
650         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
651         if(count($r)) {
652                 foreach($r as $rr) {
653                         if ($abandon_days != 0) {
654                                 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
655                                 if (!count($user)) {
656                                         logger('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
657                                         continue;
658                                 }
659                         }
660
661                         logger('pumpio: importing timeline from user '.$rr['uid']);
662                         pumpio_fetchinbox($a, $rr['uid']);
663
664                         // check for new contacts once a day
665                         $last_contact_check = get_pconfig($rr['uid'],'pumpio','contact_check');
666                         if($last_contact_check)
667                                 $next_contact_check = $last_contact_check + 86400;
668                         else
669                                 $next_contact_check = 0;
670
671                         if($next_contact_check <= time()) {
672                                 pumpio_getallusers($a, $rr["uid"]);
673                                 set_pconfig($rr['uid'],'pumpio','contact_check',time());
674                         }
675                 }
676         }
677
678         logger('pumpio: cron_end');
679
680         set_config('pumpio','last_poll', time());
681 }
682
683 function pumpio_cron(&$a,$b) {
684         //pumpio_sync($a);
685         proc_run("php","addon/pumpio/pumpio_sync.php");
686 }
687
688 function pumpio_fetchtimeline(&$a, $uid) {
689         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
690         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
691         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
692         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
693         $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
694         $hostname = get_pconfig($uid, 'pumpio','host');
695         $username = get_pconfig($uid, "pumpio", "user");
696
697         //  get the application name for the pump.io app
698         //  1st try personal config, then system config and fallback to the
699         //  hostname of the node if neither one is set.
700         $application_name  = get_pconfig( $uid, 'pumpio', 'application_name');
701         if ($application_name == "")
702                 $application_name  = get_config('pumpio', 'application_name');
703         if ($application_name == "")
704                 $application_name = $a->get_hostname();
705
706         $first_time = ($lastdate == "");
707
708         $client = new oauth_client_class;
709         $client->oauth_version = '1.0a';
710         $client->authorization_header = true;
711         $client->url_parameters = false;
712
713         $client->client_id = $ckey;
714         $client->client_secret = $csecret;
715         $client->access_token = $otoken;
716         $client->access_token_secret = $osecret;
717
718         $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
719
720         logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
721
722         $username = $user.'@'.$host;
723
724         if (pumpio_reachable($url))
725                 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
726         else
727                 $success = false;
728
729         if (!$success) {
730                 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
731                 return;
732         }
733
734         $posts = array_reverse($user->items);
735
736         $initiallastdate = $lastdate;
737         $lastdate = '';
738
739         if (count($posts)) {
740                 foreach ($posts as $post) {
741                         if ($post->published <= $initiallastdate)
742                                 continue;
743
744                         if ($lastdate < $post->published)
745                                 $lastdate = $post->published;
746
747                         if ($first_time)
748                                 continue;
749
750                         $receiptians = array();
751                         if (@is_array($post->cc))
752                                 $receiptians = array_merge($receiptians, $post->cc);
753
754                         if (@is_array($post->to))
755                                 $receiptians = array_merge($receiptians, $post->to);
756
757                         $public = false;
758                         foreach ($receiptians AS $receiver)
759                                 if (is_string($receiver->objectType))
760                                         if ($receiver->id == "http://activityschema.org/collection/public")
761                                                 $public = true;
762
763                         if ($public AND !stristr($post->generator->displayName, $application_name)) {
764                                 require_once('include/html2bbcode.php');
765
766                                 $_SESSION["authenticated"] = true;
767                                 $_SESSION["uid"] = $uid;
768
769                                 unset($_REQUEST);
770                                 $_REQUEST["type"] = "wall";
771                                 $_REQUEST["api_source"] = true;
772                                 $_REQUEST["profile_uid"] = $uid;
773                                 $_REQUEST["source"] = "pump.io";
774
775                                 if ($post->object->displayName != "")
776                                         $_REQUEST["title"] = html2bbcode($post->object->displayName);
777                                 else
778                                         $_REQUEST["title"] = "";
779
780                                 $_REQUEST["body"] = html2bbcode($post->object->content);
781
782                                 // To-Do: Picture has to be cached and stored locally
783                                 if ($post->object->fullImage->url != "") {
784                                         if ($post->object->fullImage->pump_io->proxyURL != "")
785                                                 $_REQUEST["body"] = "[url=".$post->object->fullImage->pump_io->proxyURL."][img]".$post->object->image->pump_io->proxyURL."[/img][/url]\n".$_REQUEST["body"];
786                                         else
787                                                 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
788                                 }
789
790                                 logger('pumpio: posting for user '.$uid);
791
792                                 require_once('mod/item.php');
793
794                                 item_post($a);
795                                 logger('pumpio: posting done - user '.$uid);
796                         }
797                 }
798         }
799
800         if ($lastdate != 0)
801                 set_pconfig($uid,'pumpio','lastdate', $lastdate);
802 }
803
804 function pumpio_dounlike(&$a, $uid, $self, $post, $own_id) {
805         // Searching for the unliked post
806         // Two queries for speed issues
807         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
808                                 dbesc($post->object->id),
809                                 intval($uid)
810                 );
811
812         if (count($r))
813                 $orig_post = $r[0];
814         else {
815                 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
816                                         dbesc($post->object->id),
817                                         intval($uid)
818                         );
819
820                 if (!count($r))
821                         return;
822                 else
823                         $orig_post = $r[0];
824         }
825
826         $contactid = 0;
827
828         if(link_compare($post->actor->url, $own_id)) {
829                 $contactid = $self[0]['id'];
830         } else {
831                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
832                         dbesc($post->actor->url),
833                         intval($uid)
834                 );
835
836                 if(count($r))
837                         $contactid = $r[0]['id'];
838
839                 if($contactid == 0)
840                         $contactid = $orig_post['contact-id'];
841         }
842
843         $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `verb` = '%s' AND `uid` = %d AND `contact-id` = %d AND `thr-parent` = '%s'",
844                 dbesc(datetime_convert()),
845                 dbesc(ACTIVITY_LIKE),
846                 intval($uid),
847                 intval($contactid),
848                 dbesc($orig_post['uri'])
849         );
850
851         if(count($r))
852                 logger("pumpio_dounlike: unliked existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
853         else
854                 logger("pumpio_dounlike: not found. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
855 }
856
857 function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = true) {
858         require_once('include/items.php');
859
860         // Searching for the liked post
861         // Two queries for speed issues
862         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `network` = '%s' LIMIT 1",
863                                 dbesc($post->object->id),
864                                 intval($uid),
865                                 dbesc(NETWORK_PUMPIO)
866                 );
867
868         if (count($r))
869                 $orig_post = $r[0];
870         else {
871                 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d AND `network` = '%s' LIMIT 1",
872                                         dbesc($post->object->id),
873                                         intval($uid),
874                                         dbesc(NETWORK_PUMPIO)
875                         );
876
877                 if (!count($r))
878                         return;
879                 else
880                         $orig_post = $r[0];
881         }
882
883         // thread completion
884         if ($threadcompletion)
885                 pumpio_fetchallcomments($a, $uid, $post->object->id);
886
887         $contactid = 0;
888
889         if(link_compare($post->actor->url, $own_id)) {
890                 $contactid = $self[0]['id'];
891                 $post->actor->displayName = $self[0]['name'];
892                 $post->actor->url = $self[0]['url'];
893                 $post->actor->image->url = $self[0]['photo'];
894         } else {
895                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
896                         dbesc($post->actor->url),
897                         intval($uid)
898                 );
899
900                 if(count($r))
901                         $contactid = $r[0]['id'];
902
903                 if($contactid == 0)
904                         $contactid = $orig_post['contact-id'];
905         }
906
907         $r = q("SELECT parent FROM `item` WHERE `verb` = '%s' AND `uid` = %d AND `contact-id` = %d AND `thr-parent` = '%s' LIMIT 1",
908                 dbesc(ACTIVITY_LIKE),
909                 intval($uid),
910                 intval($contactid),
911                 dbesc($orig_post['uri'])
912         );
913
914         if(count($r)) {
915                 logger("pumpio_dolike: found existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
916                 return;
917         }
918
919         $likedata = array();
920         $likedata['parent'] = $orig_post['id'];
921         $likedata['verb'] = ACTIVITY_LIKE;
922         $likedata['gravity'] = 3;
923         $likedata['uid'] = $uid;
924         $likedata['wall'] = 0;
925         $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
926         $likedata['parent-uri'] = $orig_post["uri"];
927         $likedata['contact-id'] = $contactid;
928         $likedata['app'] = $post->generator->displayName;
929         $likedata['author-name'] = $post->actor->displayName;
930         $likedata['author-link'] = $post->actor->url;
931         $likedata['author-avatar'] = $post->actor->image->url;
932
933         $author  = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
934         $objauthor =  '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
935         $post_type = t('status');
936         $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
937         $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
938
939         $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
940
941         $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
942                 '<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . xmlify($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';
943
944         $ret = item_store($likedata);
945
946         logger("pumpio_dolike: ".$ret." User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
947 }
948
949 function pumpio_get_contact($uid, $contact) {
950
951         if (function_exists("update_gcontact"))
952                 update_gcontact(array("url" => $contact->url, "network" => NETWORK_PUMPIO, "generation" => 2,
953                                 "photo" => $contact->image->url, "name" => $contact->displayName,  "hide" => true,
954                                 "nick" => $contact->preferredUsername, "location" => $contact->location->displayName,
955                                 "about" => $contact->summary, "addr" => str_replace("acct:", "", $contact->id)));
956         else {
957                 // Old Code
958                 $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
959                         dbesc(normalise_link($contact->url)));
960
961                 if (count($r) == 0)
962                         q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
963                                 dbesc(normalise_link($contact->url)),
964                                 dbesc($contact->displayName),
965                                 dbesc($contact->preferredUsername),
966                                 dbesc($contact->image->url));
967                 else
968                         q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
969                                 dbesc($contact->displayName),
970                                 dbesc($contact->preferredUsername),
971                                 dbesc($contact->image->url),
972                                 dbesc(normalise_link($contact->url)));
973
974                 if (DB_UPDATE_VERSION >= "1177")
975                         q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
976                                 dbesc($contact->location->displayName),
977                                 dbesc($contact->summary),
978                                 dbesc(normalise_link($contact->url)));
979         }
980
981         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
982                 intval($uid), dbesc($contact->url));
983
984         if(!count($r)) {
985                 // create contact record
986                 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
987                                         `name`, `nick`, `photo`, `network`, `rel`, `priority`,
988                                         `writable`, `blocked`, `readonly`, `pending` )
989                                 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
990                         intval($uid),
991                         dbesc(datetime_convert()),
992                         dbesc($contact->url),
993                         dbesc(normalise_link($contact->url)),
994                         dbesc(str_replace("acct:", "", $contact->id)),
995                         dbesc(''),
996                         dbesc($contact->id), // What is it for?
997                         dbesc('pump.io ' . $contact->id), // What is it for?
998                         dbesc($contact->displayName),
999                         dbesc($contact->preferredUsername),
1000                         dbesc($contact->image->url),
1001                         dbesc(NETWORK_PUMPIO),
1002                         intval(CONTACT_IS_FRIEND),
1003                         intval(1),
1004                         intval(1)
1005                 );
1006
1007                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
1008                         dbesc($contact->url),
1009                         intval($uid)
1010                         );
1011
1012                 if(! count($r))
1013                         return(false);
1014
1015                 $contact_id  = $r[0]['id'];
1016
1017                 $g = q("select def_gid from user where uid = %d limit 1",
1018                         intval($uid)
1019                 );
1020
1021                 if($g && intval($g[0]['def_gid'])) {
1022                         require_once('include/group.php');
1023                         group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
1024                 }
1025
1026                 require_once("Photo.php");
1027
1028                 $photos = import_profile_photo($contact->image->url,$uid,$contact_id);
1029
1030                 q("UPDATE `contact` SET `photo` = '%s',
1031                                         `thumb` = '%s',
1032                                         `micro` = '%s',
1033                                         `name-date` = '%s',
1034                                         `uri-date` = '%s',
1035                                         `avatar-date` = '%s'
1036                                 WHERE `id` = %d
1037                         ",
1038                 dbesc($photos[0]),
1039                 dbesc($photos[1]),
1040                 dbesc($photos[2]),
1041                 dbesc(datetime_convert()),
1042                 dbesc(datetime_convert()),
1043                 dbesc(datetime_convert()),
1044                 intval($contact_id)
1045                 );
1046
1047                 if (DB_UPDATE_VERSION >= "1177")
1048                         q("UPDATE `contact` SET `location` = '%s',
1049                                                 `about` = '%s'
1050                                         WHERE `id` = %d",
1051                                 dbesc($contact->location->displayName),
1052                                 dbesc($contact->summary),
1053                                 intval($contact_id)
1054                         );
1055         } else {
1056                 // update profile photos once every two weeks as we have no notification of when they change.
1057                 //$update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
1058                 $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
1059
1060                 // check that we have all the photos, this has been known to fail on occasion
1061
1062                 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
1063                         require_once("Photo.php");
1064
1065                         $photos = import_profile_photo($contact->image->url, $uid, $r[0]['id']);
1066
1067                         q("UPDATE `contact` SET `photo` = '%s',
1068                                         `thumb` = '%s',
1069                                         `micro` = '%s',
1070                                         `name-date` = '%s',
1071                                         `uri-date` = '%s',
1072                                         `avatar-date` = '%s',
1073                                         `name` = '%s',
1074                                         `nick` = '%s'
1075                                         WHERE `id` = %d
1076                                 ",
1077                         dbesc($photos[0]),
1078                         dbesc($photos[1]),
1079                         dbesc($photos[2]),
1080                         dbesc(datetime_convert()),
1081                         dbesc(datetime_convert()),
1082                         dbesc(datetime_convert()),
1083                         dbesc($contact->displayName),
1084                         dbesc($contact->preferredUsername),
1085                         intval($r[0]['id'])
1086                         );
1087
1088                         if (DB_UPDATE_VERSION >= "1177")
1089                                 q("UPDATE `contact` SET `location` = '%s',
1090                                                         `about` = '%s'
1091                                                 WHERE `id` = %d",
1092                                         dbesc($contact->location->displayName),
1093                                         dbesc($contact->summary),
1094                                         intval($r[0]['id'])
1095                                 );
1096                 }
1097
1098         }
1099
1100         return($r[0]["id"]);
1101 }
1102
1103 function pumpio_dodelete(&$a, $uid, $self, $post, $own_id) {
1104
1105         // Two queries for speed issues
1106         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1107                                 dbesc($post->object->id),
1108                                 intval($uid)
1109                 );
1110
1111         if (count($r))
1112                 return drop_item($r[0]["id"], $false);
1113
1114         $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1115                                 dbesc($post->object->id),
1116                                 intval($uid)
1117                 );
1118
1119         if (count($r))
1120                 return drop_item($r[0]["id"], $false);
1121 }
1122
1123 function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcompletion = true) {
1124         require_once('include/items.php');
1125         require_once('include/html2bbcode.php');
1126
1127         if (($post->verb == "like") OR ($post->verb == "favorite"))
1128                 return pumpio_dolike($a, $uid, $self, $post, $own_id);
1129
1130         if (($post->verb == "unlike") OR ($post->verb == "unfavorite"))
1131                 return pumpio_dounlike($a, $uid, $self, $post, $own_id);
1132
1133         if ($post->verb == "delete")
1134                 return pumpio_dodelete($a, $uid, $self, $post, $own_id);
1135
1136         if ($post->verb != "update") {
1137                 // Two queries for speed issues
1138                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1139                                         dbesc($post->object->id),
1140                                         intval($uid)
1141                         );
1142
1143                 if (count($r))
1144                         return false;
1145
1146                 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1147                                         dbesc($post->object->id),
1148                                         intval($uid)
1149                         );
1150
1151                 if (count($r))
1152                         return false;
1153         }
1154
1155         // Only handle these three types
1156         if (!strstr("post|share|update", $post->verb))
1157                 return false;
1158
1159         $receiptians = array();
1160         if (@is_array($post->cc))
1161                 $receiptians = array_merge($receiptians, $post->cc);
1162
1163         if (@is_array($post->to))
1164                 $receiptians = array_merge($receiptians, $post->to);
1165
1166         foreach ($receiptians AS $receiver)
1167                 if (is_string($receiver->objectType))
1168                         if ($receiver->id == "http://activityschema.org/collection/public")
1169                                 $public = true;
1170
1171         $postarray = array();
1172         $postarray['network'] = NETWORK_PUMPIO;
1173         $postarray['gravity'] = 0;
1174         $postarray['uid'] = $uid;
1175         $postarray['wall'] = 0;
1176         $postarray['uri'] = $post->object->id;
1177         $postarray['object-type'] = NAMESPACE_ACTIVITY_SCHEMA.strtolower($post->object->objectType);
1178
1179         if ($post->object->objectType != "comment") {
1180                 $contact_id = pumpio_get_contact($uid, $post->actor);
1181
1182                 if (!$contact_id)
1183                         $contact_id = $self[0]['id'];
1184
1185                 $postarray['parent-uri'] = $post->object->id;
1186
1187                 if (!$public) {
1188                         $postarray['private'] = 1;
1189                         $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
1190                 }
1191         } else {
1192                 $contact_id = 0;
1193
1194                 if(link_compare($post->actor->url, $own_id)) {
1195                         $contact_id = $self[0]['id'];
1196                         $post->actor->displayName = $self[0]['name'];
1197                         $post->actor->url = $self[0]['url'];
1198                         $post->actor->image->url = $self[0]['photo'];
1199                 } else {
1200                         // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1201                         $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1202                                 dbesc($post->actor->url),
1203                                 intval($uid)
1204                         );
1205
1206                         if(count($r))
1207                                 $contact_id = $r[0]['id'];
1208                         else {
1209                                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1210                                         dbesc($post->actor->url),
1211                                         intval($uid)
1212                                 );
1213
1214                                 if(count($r))
1215                                         $contact_id = $r[0]['id'];
1216                                 else
1217                                         $contact_id = $self[0]['id'];
1218                         }
1219                 }
1220
1221                 $reply = new stdClass;
1222                 $reply->verb = "note";
1223                 $reply->cc = $post->cc;
1224                 $reply->to = $post->to;
1225                 $reply->object = new stdClass;
1226                 $reply->object->objectType = $post->object->inReplyTo->objectType;
1227                 $reply->object->content = $post->object->inReplyTo->content;
1228                 $reply->object->id = $post->object->inReplyTo->id;
1229                 $reply->actor = $post->object->inReplyTo->author;
1230                 $reply->url = $post->object->inReplyTo->url;
1231                 $reply->generator = new stdClass;
1232                 $reply->generator->displayName = "pumpio";
1233                 $reply->published = $post->object->inReplyTo->published;
1234                 $reply->received = $post->object->inReplyTo->updated;
1235                 $reply->url = $post->object->inReplyTo->url;
1236                 pumpio_dopost($a, $client, $uid, $self, $reply, $own_id, false);
1237
1238                 $postarray['parent-uri'] = $post->object->inReplyTo->id;
1239         }
1240
1241         if ($post->object->pump_io->proxyURL)
1242                 $postarray['extid'] = $post->object->pump_io->proxyURL;
1243
1244         $postarray['contact-id'] = $contact_id;
1245         $postarray['verb'] = ACTIVITY_POST;
1246         $postarray['owner-name'] = $post->actor->displayName;
1247         $postarray['owner-link'] = $post->actor->url;
1248         $postarray['owner-avatar'] = $post->actor->image->url;
1249         $postarray['author-name'] = $post->actor->displayName;
1250         $postarray['author-link'] = $post->actor->url;
1251         $postarray['author-avatar'] = $post->actor->image->url;
1252         $postarray['plink'] = $post->object->url;
1253         $postarray['app'] = $post->generator->displayName;
1254         $postarray['body'] = html2bbcode($post->object->content);
1255         $postarray['object'] = json_encode($post);
1256
1257         if ($post->object->fullImage->url != "")
1258                 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
1259
1260         if ($post->object->displayName != "")
1261                 $postarray['title'] = $post->object->displayName;
1262
1263         $postarray['created'] = datetime_convert('UTC','UTC',$post->published);
1264         $postarray['edited'] = datetime_convert('UTC','UTC',$post->received);
1265
1266         if ($post->verb == "share") {
1267                 if (!intval(get_config('system','wall-to-wall_share'))) {
1268                         $postarray['body'] = "[share author='".$post->object->author->displayName.
1269                                         "' profile='".$post->object->author->url.
1270                                         "' avatar='".$post->object->author->image->url.
1271                                         "' posted='".datetime_convert('UTC','UTC',$post->object->created).
1272                                         "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
1273                 } else {
1274                         // Let shares look like wall-to-wall posts
1275                         $postarray['author-name'] = $post->object->author->displayName;
1276                         $postarray['author-link'] = $post->object->author->url;
1277                         $postarray['author-avatar'] = $post->object->author->image->url;
1278                 }
1279         }
1280
1281         if (trim($postarray['body']) == "")
1282                 return false;
1283
1284         $top_item = item_store($postarray);
1285         $postarray["id"] = $top_item;
1286
1287         if (($top_item == 0) AND ($post->verb == "update")) {
1288                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
1289                         dbesc($postarray["title"]),
1290                         dbesc($postarray["body"]),
1291                         dbesc($postarray["edited"]),
1292                         dbesc($postarray["uri"]),
1293                         intval($uid)
1294                         );
1295         }
1296
1297         if ($post->object->objectType == "comment") {
1298
1299                 if ($threadcompletion)
1300                         pumpio_fetchallcomments($a, $uid, $postarray['parent-uri']);
1301
1302                 $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
1303                                 intval($uid)
1304                         );
1305
1306                 if(!count($user))
1307                         return $top_item;
1308
1309                 $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
1310
1311                 if (link_compare($own_id, $postarray['author-link']))
1312                         return $top_item;
1313
1314                 if (!function_exists("check_item_notification")) {
1315                         $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
1316                                         dbesc($postarray['parent-uri']),
1317                                         intval($uid)
1318                                         );
1319
1320                         if(count($myconv)) {
1321
1322                                 foreach($myconv as $conv) {
1323                                         // now if we find a match, it means we're in this conversation
1324
1325                                         if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$own_id))
1326                                                 continue;
1327
1328                                         require_once('include/enotify.php');
1329
1330                                         $conv_parent = $conv['parent'];
1331
1332                                         notification(array(
1333                                                 'type'         => NOTIFY_COMMENT,
1334                                                 'notify_flags' => $user[0]['notify-flags'],
1335                                                 'language'     => $user[0]['language'],
1336                                                 'to_name'      => $user[0]['username'],
1337                                                 'to_email'     => $user[0]['email'],
1338                                                 'uid'          => $user[0]['uid'],
1339                                                 'item'         => $postarray,
1340                                                 'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($top_item)),
1341                                                 'source_name'  => $postarray['author-name'],
1342                                                 'source_link'  => $postarray['author-link'],
1343                                                 'source_photo' => $postarray['author-avatar'],
1344                                                 'verb'         => ACTIVITY_POST,
1345                                                 'otype'        => 'item',
1346                                                 'parent'       => $conv_parent,
1347                                                 ));
1348
1349                                         // only send one notification
1350                                         break;
1351                                 }
1352                         }
1353                 }
1354         }
1355
1356         return $top_item;
1357 }
1358
1359 function pumpio_fetchinbox(&$a, $uid) {
1360
1361         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
1362         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1363         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
1364         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1365         $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
1366         $hostname = get_pconfig($uid, 'pumpio','host');
1367         $username = get_pconfig($uid, "pumpio", "user");
1368
1369         $own_id = "https://".$hostname."/".$username;
1370
1371         $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1372                 intval($uid));
1373
1374         $lastitems = q("SELECT uri FROM `item` WHERE `network` = '%s' AND `uid` = %d AND
1375                         `extid` != '' AND `id` = `parent`
1376                         ORDER BY `commented` DESC LIMIT 10",
1377                                 dbesc(NETWORK_PUMPIO),
1378                                 intval($uid)
1379                         );
1380
1381         $client = new oauth_client_class;
1382         $client->oauth_version = '1.0a';
1383         $client->authorization_header = true;
1384         $client->url_parameters = false;
1385
1386         $client->client_id = $ckey;
1387         $client->client_secret = $csecret;
1388         $client->access_token = $otoken;
1389         $client->access_token_secret = $osecret;
1390
1391         $last_id = get_pconfig($uid,'pumpio','last_id');
1392
1393         $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1394
1395         if ($last_id != "")
1396                 $url .= '?since='.urlencode($last_id);
1397
1398         if (pumpio_reachable($url))
1399                 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
1400         else
1401                 $success = false;
1402
1403         if ($user->items) {
1404             $posts = array_reverse($user->items);
1405
1406             if (count($posts))
1407                     foreach ($posts as $post) {
1408                             $last_id = $post->id;
1409                             pumpio_dopost($a, $client, $uid, $self, $post, $own_id, true);
1410                     }
1411         }
1412
1413         foreach ($lastitems AS $item)
1414                 pumpio_fetchallcomments($a, $uid, $item["uri"]);
1415
1416         set_pconfig($uid,'pumpio','last_id', $last_id);
1417 }
1418
1419 function pumpio_getallusers(&$a, $uid) {
1420         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
1421         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1422         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
1423         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1424         $hostname = get_pconfig($uid, 'pumpio','host');
1425         $username = get_pconfig($uid, "pumpio", "user");
1426
1427         $client = new oauth_client_class;
1428         $client->oauth_version = '1.0a';
1429         $client->authorization_header = true;
1430         $client->url_parameters = false;
1431
1432         $client->client_id = $ckey;
1433         $client->client_secret = $csecret;
1434         $client->access_token = $otoken;
1435         $client->access_token_secret = $osecret;
1436
1437         $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1438
1439         if (pumpio_reachable($url))
1440                 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1441         else
1442                 $success = false;
1443
1444         if ($users->totalItems > count($users->items)) {
1445                 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1446
1447                 if (pumpio_reachable($url))
1448                         $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1449                 else
1450                         $success = false;
1451         }
1452
1453         foreach ($users->items AS $user)
1454                 pumpio_get_contact($uid, $user);
1455 }
1456
1457 function pumpio_queue_hook(&$a,&$b) {
1458
1459         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
1460                 dbesc(NETWORK_PUMPIO)
1461         );
1462         if(! count($qi))
1463                 return;
1464
1465         require_once('include/queue_fn.php');
1466
1467         foreach($qi as $x) {
1468                 if($x['network'] !== NETWORK_PUMPIO)
1469                         continue;
1470
1471                 logger('pumpio_queue: run');
1472
1473                 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid` 
1474                         WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
1475                         intval($x['cid'])
1476                 );
1477                 if(! count($r))
1478                         continue;
1479
1480                 $userdata = $r[0];
1481
1482                 //logger('pumpio_queue: fetching userdata '.print_r($userdata, true));
1483
1484                 $oauth_token = get_pconfig($userdata['uid'], "pumpio", "oauth_token");
1485                 $oauth_token_secret = get_pconfig($userdata['uid'], "pumpio", "oauth_token_secret");
1486                 $consumer_key = get_pconfig($userdata['uid'], "pumpio","consumer_key");
1487                 $consumer_secret = get_pconfig($userdata['uid'], "pumpio","consumer_secret");
1488
1489                 $host = get_pconfig($userdata['uid'], "pumpio", "host");
1490                 $user = get_pconfig($userdata['uid'], "pumpio", "user");
1491
1492                 $success = false;
1493
1494                 if ($oauth_token AND $oauth_token_secret AND
1495                         $consumer_key AND $consumer_secret) {
1496                         $username = $user.'@'.$host;
1497
1498                         logger('pumpio_queue: able to post for user '.$username);
1499
1500                         $z = unserialize($x['content']);
1501
1502                         $client = new oauth_client_class;
1503                         $client->oauth_version = '1.0a';
1504                         $client->url_parameters = false;
1505                         $client->authorization_header = true;
1506                         $client->access_token = $oauth_token;
1507                         $client->access_token_secret = $oauth_token_secret;
1508                         $client->client_id = $consumer_key;
1509                         $client->client_secret = $consumer_secret;
1510
1511                         if (pumpio_reachable($z['url']))
1512                                 $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
1513                         else
1514                                 $success = false;
1515
1516                         if($success) {
1517                                 $post_id = $user->object->id;
1518                                 logger('pumpio_queue: send '.$username.': success '.$post_id);
1519                                 if($post_id AND $iscomment) {
1520                                         logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$z['item']);
1521                                         q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
1522                                                 dbesc($post_id),
1523                                                 intval($z['item'])
1524                                         );
1525                                 }
1526                                 remove_queue_item($x['id']);
1527                         } else
1528                                 logger('pumpio_queue: send '.$username.': '.$url.' general error: ' . print_r($user,true));
1529                 } else
1530                         logger("pumpio_queue: Error getting tokens for user ".$userdata['uid']);
1531
1532                 if (!$success) {
1533                         logger('pumpio_queue: delayed');
1534                         update_queue_time($x['id']);
1535                 }
1536         }
1537 }
1538
1539 function pumpio_getreceiver(&$a, $b) {
1540
1541         $receiver = array();
1542
1543         if (!$b["private"]) {
1544
1545                 if(! strstr($b['postopts'],'pumpio'))
1546                         return $receiver;
1547
1548                 $public = get_pconfig($b['uid'], "pumpio", "public");
1549
1550                 if ($public)
1551                         $receiver["to"][] = Array(
1552                                                 "objectType" => "collection",
1553                                                 "id" => "http://activityschema.org/collection/public");
1554         } else {
1555                 $cids = explode("><", $b["allow_cid"]);
1556                 $gids = explode("><", $b["allow_gid"]);
1557
1558                 foreach ($cids AS $cid) {
1559                         $cid = trim($cid, " <>");
1560
1561                         $r = q("SELECT `name`, `nick`, `url` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `network` = '%s' AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1562                                 intval($cid),
1563                                 intval($b["uid"]),
1564                                 dbesc(NETWORK_PUMPIO)
1565                                 );
1566
1567                         if (count($r)) {
1568                                 $receiver["bcc"][] = Array(
1569                                                         "displayName" => $r[0]["name"],
1570                                                         "objectType" => "person",
1571                                                         "preferredUsername" => $r[0]["nick"],
1572                                                         "url" => $r[0]["url"]);
1573                         }
1574                 }
1575                 foreach ($gids AS $gid) {
1576                         $gid = trim($gid, " <>");
1577
1578                         $r = q("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network` ".
1579                                 "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d AND `group_member`.`uid` = %d ".
1580                                 "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
1581                                         intval($gid),
1582                                         intval($b["uid"]),
1583                                         dbesc(NETWORK_PUMPIO)
1584                                 );
1585
1586                         foreach ($r AS $row)
1587                                 $receiver["bcc"][] = Array(
1588                                                         "displayName" => $row["name"],
1589                                                         "objectType" => "person",
1590                                                         "preferredUsername" => $row["nick"],
1591                                                         "url" => $row["url"]);
1592                 }
1593         }
1594
1595         if ($b["inform"] != "") {
1596
1597                 $inform = explode(",", $b["inform"]);
1598
1599                 foreach ($inform AS $cid) {
1600                         if (substr($cid, 0, 4) != "cid:")
1601                                 continue;
1602
1603                         $cid = str_replace("cid:", "", $cid);
1604
1605                         $r = q("SELECT `name`, `nick`, `url` FROM `contact` WHERE `id` = %d AND `uid` = %d AND `network` = '%s' AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1606                                 intval($cid),
1607                                 intval($b["uid"]),
1608                                 dbesc(NETWORK_PUMPIO)
1609                                 );
1610
1611                         if (count($r)) {
1612                                         $receiver["to"][] = Array(
1613                                                                 "displayName" => $r[0]["name"],
1614                                                                 "objectType" => "person",
1615                                                                 "preferredUsername" => $r[0]["nick"],
1616                                                                 "url" => $r[0]["url"]);
1617                         }
1618                 }
1619         }
1620
1621         return $receiver;
1622 }
1623
1624 function pumpio_fetchallcomments(&$a, $uid, $id) {
1625         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
1626         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1627         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
1628         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1629         $hostname = get_pconfig($uid, 'pumpio','host');
1630         $username = get_pconfig($uid, "pumpio", "user");
1631
1632         logger("pumpio_fetchallcomments: completing comment for user ".$uid." post id ".$id);
1633
1634         $own_id = "https://".$hostname."/".$username;
1635
1636         $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1637                 intval($uid));
1638
1639         // Fetching the original post
1640         $r = q("SELECT `extid` FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `extid` != '' LIMIT 1",
1641                         dbesc($id),
1642                         intval($uid)
1643                 );
1644
1645         if (!count($r))
1646                 return false;
1647
1648         $url = $r[0]["extid"];
1649
1650         $client = new oauth_client_class;
1651         $client->oauth_version = '1.0a';
1652         $client->authorization_header = true;
1653         $client->url_parameters = false;
1654
1655         $client->client_id = $ckey;
1656         $client->client_secret = $csecret;
1657         $client->access_token = $otoken;
1658         $client->access_token_secret = $osecret;
1659
1660         logger("pumpio_fetchallcomments: fetching comment for user ".$uid." url ".$url);
1661
1662         if (pumpio_reachable($url))
1663                 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $item);
1664         else
1665                 $success = false;
1666
1667         if (!$success)
1668                 return;
1669
1670         if ($item->likes->totalItems != 0) {
1671                 foreach ($item->likes->items AS $post) {
1672                         $like = new stdClass;
1673                         $like->object = new stdClass;
1674                         $like->object->id = $item->id;
1675                         $like->actor = new stdClass;
1676                         $like->actor->displayName = $item->displayName;
1677                         $like->actor->preferredUsername = $item->preferredUsername;
1678                         $like->actor->url = $item->url;
1679                         $like->actor->image = $item->image;
1680                         $like->generator = new stdClass;
1681                         $like->generator->displayName = "pumpio";
1682                         pumpio_dolike($a, $uid, $self, $post, $own_id, false);
1683                 }
1684         }
1685
1686         if ($item->replies->totalItems == 0)
1687                 return;
1688
1689         foreach ($item->replies->items AS $item) {
1690                 if ($item->id == $id)
1691                         continue;
1692
1693                 // Checking if the comment already exists - Two queries for speed issues
1694                 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1695                                 dbesc($item->id),
1696                                 intval($uid)
1697                         );
1698
1699                 if (count($r))
1700                         continue;
1701
1702                 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1703                                 dbesc($item->id),
1704                                 intval($uid)
1705                         );
1706
1707                 if (count($r))
1708                         continue;
1709
1710                 $post = new stdClass;
1711                 $post->verb = "post";
1712                 $post->actor = $item->author;
1713                 $post->published = $item->published;
1714                 $post->received = $item->updated;
1715                 $post->generator = new stdClass;
1716                 $post->generator->displayName = "pumpio";
1717                 // To-Do: Check for public post
1718
1719                 unset($item->author);
1720                 unset($item->published);
1721                 unset($item->updated);
1722
1723                 $post->object = $item;
1724
1725                 logger("pumpio_fetchallcomments: posting comment ".$post->object->id." ".print_r($post, true));
1726                 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, false);
1727         }
1728 }
1729
1730
1731 function pumpio_reachable($url) {
1732         $data = z_fetch_url($url, false, $redirects, array('timeout'=>10));
1733         return(intval($data['return_code']) != 0);
1734 }
1735
1736 /*
1737 To-Do:
1738  - edit own notes
1739  - delete own notes
1740 */