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