]> git.mxchange.org Git - friendica-addons.git/blob - pumpio/pumpio.php
9fb71c08e11d2f9b0707e5f82f613d6a5cb836b6
[friendica-addons.git] / pumpio / pumpio.php
1 <?php
2 /**
3  * Name: pump.io Post Connector
4  * Description: Post to pump.io
5  * Version: 0.1
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         if (isset($a->argv[1]))
43                 switch ($a->argv[1]) {
44                         case "connect":
45                                 $o = pumpio_connect($a);
46                                 break;
47                         default:
48                                 $o = print_r($a->argv, true);
49                                 break;
50                 }
51         else
52                 $o = pumpio_connect($a);
53
54         return $o;
55 }
56
57 function pumpio_registerclient($a, $host) {
58
59         $url = "https://".$host."/api/client/register";
60
61         $params = array();
62
63         $application_name  = get_config('pumpio', 'application_name');
64
65         if ($application_name == "")
66                 $application_name = $a->get_hostname();
67
68         $params["type"] = "client_associate";
69         $params["contacts"] = $a->config['admin_email'];
70         $params["application_type"] = "native";
71         $params["application_name"] = $application_name;
72         $params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
73         $params["redirect_uris"] = $a->get_baseurl()."/pumpio/connect";
74
75         $ch = curl_init($url);
76         curl_setopt($ch, CURLOPT_HEADER, false);
77         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
78         curl_setopt($ch, CURLOPT_POST,1);
79         curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
80         curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
81
82         $s = curl_exec($ch);
83         $curl_info = curl_getinfo($ch);
84
85         if ($curl_info["http_code"] == "200") {
86                 $values = json_decode($s);
87                 return($values);
88         }
89         return(false);
90 }
91
92 function pumpio_connect($a) {
93         // Start a session.  This is necessary to hold on to  a few keys the callback script will also need
94         session_start();
95
96         // Define the needed keys
97         $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
98         $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
99         $hostname = get_pconfig(local_user(), 'pumpio','host');
100
101         if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
102                 $clientdata = pumpio_registerclient($a, $hostname);
103                 set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
104                 set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
105
106                 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
107                 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
108         }
109
110         if (($consumer_key == "") OR ($consumer_secret == ""))
111                 return;
112
113         // The callback URL is the script that gets called after the user authenticates with pumpio
114         $callback_url = $a->get_baseurl()."/pumpio/connect";
115
116         // Let's begin.  First we need a Request Token.  The request token is required to send the user
117         // to pumpio's login page.
118
119         // Create a new instance of the TumblrOAuth library.  For this step, all we need to give the library is our
120         // Consumer Key and Consumer Secret
121         $client = new oauth_client_class;
122         $client->debug = 1;
123         $client->server = '';
124         $client->oauth_version = '1.0a';
125         $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
126         $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
127         $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
128         $client->url_parameters = false;
129         $client->authorization_header = true;
130         $client->redirect_uri = $callback_url;
131         $client->client_id = $consumer_key;
132         $client->client_secret = $consumer_secret;
133
134         if (($success = $client->Initialize())) {
135                 if (($success = $client->Process())) {
136                         if (strlen($client->access_token)) {
137                                 set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
138                                 set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
139                         }
140                 }
141                 $success = $client->Finalize($success);
142         }
143         if($client->exit)
144             $o = 'Could not connect to pumpio. Refresh the page or try again later.';
145
146         if($success) {
147                 $o .= t("You are now authenticated to pumpio.");
148                 $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.t("return to the connector page").'</a>';
149         }
150
151         return($o);
152 }
153
154 function pumpio_jot_nets(&$a,&$b) {
155     if(! local_user())
156         return;
157
158     $pumpio_post = get_pconfig(local_user(),'pumpio','post');
159     if(intval($pumpio_post) == 1) {
160         $pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
161         $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
162         $b .= '<div class="profile-jot-net"><input type="checkbox" name="pumpio_enable"' . $selected . ' value="1" /> '
163             . t('Post to pumpio') . '</div>';
164     }
165 }
166
167
168 function pumpio_settings(&$a,&$s) {
169
170     if(! local_user())
171         return;
172
173     /* Add our stylesheet to the page so we can make our settings look nice */
174
175     $a->page['htmlhead'] .= '<link rel="stylesheet"  type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
176
177     /* Get the current state of our config variables */
178
179     $import_enabled = get_pconfig(local_user(),'pumpio','import');
180     $import_checked = (($import_enabled) ? ' checked="checked" ' : '');
181
182     $enabled = get_pconfig(local_user(),'pumpio','post');
183     $checked = (($enabled) ? ' checked="checked" ' : '');
184
185     $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
186     $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
187
188     $public_enabled = get_pconfig(local_user(),'pumpio','public');
189     $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
190
191     $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
192     $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
193
194     $servername = get_pconfig(local_user(), "pumpio", "host");
195     $username = get_pconfig(local_user(), "pumpio", "user");
196
197     /* Add some HTML to the existing form */
198
199     $s .= '<div class="settings-block">';
200     $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
201
202     $s .= '<div id="pumpio-username-wrapper">';
203     $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username (without the servername)').'</label>';
204     $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
205     $s .= '</div><div class="clear"></div>';
206
207     $s .= '<div id="pumpio-servername-wrapper">';
208     $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername (without "http://" or "https://" )').'</label>';
209     $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
210     $s .= '</div><div class="clear"></div>';
211
212     if (($username != '') AND ($servername != '')) {
213
214         $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
215         $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
216
217         $s .= '<div id="pumpio-password-wrapper">';
218         if (($oauth_token == "") OR ($oauth_token_secret == "")) {
219                 $s .= '<div id="pumpio-authenticate-wrapper">';
220                 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Authenticate your pump.io connection").'</a>';
221                 $s .= '</div><div class="clear"></div>';
222
223                 //$s .= t("You are not authenticated to pumpio");
224         } else {
225                 $s .= '<div id="pumpio-import-wrapper">';
226                 $s .= '<label id="pumpio-import-label" for="pumpio-import">' . t('Import the remote timeline') . '</label>';
227                 $s .= '<input id="pumpio-import" type="checkbox" name="pumpio_import" value="1" ' . $import_checked . '/>';
228                 $s .= '</div><div class="clear"></div>';
229
230                 $s .= '<div id="pumpio-enable-wrapper">';
231                 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
232                 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
233                 $s .= '</div><div class="clear"></div>';
234
235                 $s .= '<div id="pumpio-bydefault-wrapper">';
236                 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
237                 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
238                 $s .= '</div><div class="clear"></div>';
239
240                 $s .= '<div id="pumpio-public-wrapper">';
241                 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
242                 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
243                 $s .= '</div><div class="clear"></div>';
244
245                 $s .= '<div id="pumpio-mirror-wrapper">';
246                 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
247                 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
248                 $s .= '</div><div class="clear"></div>';
249
250                 $s .= '<div id="pumpio-delete-wrapper">';
251                 $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . t('Check to delete this preset') . '</label>';
252                 $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
253                 $s .= '</div><div class="clear"></div>';
254
255                 //$s .= '<div id="pumpio-authenticate-wrapper">';
256                 //$s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Reauthenticate your pump.io connection").'</a>';
257                 //$s .= '</div><div class="clear"></div>';
258
259         }
260
261         $s .= '</div><div class="clear"></div>';
262     }
263
264     /* provide a submit button */
265
266     $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
267
268 }
269
270
271 function pumpio_settings_post(&$a,&$b) {
272
273         if(x($_POST,'pumpio-submit')) {
274                 if(x($_POST,'pumpio_delete')) {
275                         set_pconfig(local_user(),'pumpio','consumer_key','');
276                         set_pconfig(local_user(),'pumpio','consumer_secret','');
277                         set_pconfig(local_user(),'pumpio','host','');
278                         set_pconfig(local_user(),'pumpio','oauth_token','');
279                         set_pconfig(local_user(),'pumpio','oauth_token_secret','');
280                         set_pconfig(local_user(),'pumpio','post',false);
281                         set_pconfig(local_user(),'pumpio','post_by_default',false);
282                         set_pconfig(local_user(),'pumpio','user','');
283                 } else {
284                         // filtering the username if it is filled wrong
285                         $user = $_POST['pumpio_user'];
286                         if (strstr($user, "@")) {
287                                 $pos = strpos($user, "@");
288                                 if ($pos > 0)
289                                         $user = substr($user, 0, $pos);
290                         }
291
292                         // Filtering the hostname if someone is entering it with "http"
293                         $host = $_POST['pumpio_host'];
294                         $host = trim($host);
295                         $host = str_replace(array("https://", "http://"), array("", ""), $host);
296
297                         set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
298                         set_pconfig(local_user(),'pumpio','import',$_POST['pumpio_import']);
299                         set_pconfig(local_user(),'pumpio','host',$host);
300                         set_pconfig(local_user(),'pumpio','user',$user);
301                         set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
302                         set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
303                         set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
304                 }
305         }
306 }
307
308 function pumpio_post_local(&$a,&$b) {
309
310         // This can probably be changed to allow editing by pointing to a different API endpoint
311
312         if($b['edit'])
313                 return;
314
315         if((! local_user()) || (local_user() != $b['uid']))
316                 return;
317
318         if($b['private'] || $b['parent'])
319                 return;
320
321         $pumpio_post   = intval(get_pconfig(local_user(),'pumpio','post'));
322
323         $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
324
325         if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
326                 $pumpio_enable = 1;
327
328         if(! $pumpio_enable)
329                 return;
330
331         if(strlen($b['postopts']))
332                 $b['postopts'] .= ',';
333
334         $b['postopts'] .= 'pumpio';
335 }
336
337
338
339
340 function pumpio_send(&$a,&$b) {
341         if($b['deleted'] || ($b['created'] !== $b['edited']))
342                 return;
343
344         if($b['parent'] != $b['id']) {
345                 // Looking if its a reply to a pumpio post
346                 $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",
347                         intval($b["parent"]),
348                         intval($b["uid"]),
349                         dbesc(NETWORK_PUMPIO));
350
351                 if(!count($r))
352                         return;
353                 else {
354                         $iscomment = true;
355                         $orig_post = $r[0];
356                 }
357         } else {
358                 $iscomment = false;
359
360                 if(! strstr($b['postopts'],'pumpio'))
361                         return;
362
363                 if($b['private'])
364                         return;
365         }
366
367         // if post comes from pump.io don't send it back
368         if($b['app'] == "pump.io")
369                 return;
370
371         $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
372         $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
373         $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
374         $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
375
376         $host = get_pconfig($b['uid'], "pumpio", "host");
377         $user = get_pconfig($b['uid'], "pumpio", "user");
378         $public = get_pconfig($b['uid'], "pumpio", "public");
379
380         if($oauth_token && $oauth_token_secret) {
381
382                 require_once('include/bbcode.php');
383
384                 $title = trim($b['title']);
385
386                 if ($title != '')
387                         $title = "<h4>".$title."</h4>";
388
389                 $content = bbcode($b['body'], false, false);
390
391                 // Enhance the way, videos are displayed
392                 $content = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
393                 $content = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
394                 $content = preg_replace('/<a.*?href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
395                 $content = preg_replace('/<a.*?href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
396
397                 $URLSearchString = "^\[\]";
398                 $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
399
400                 $params = array();
401
402                 $params["verb"] = "post";
403
404                 if (!$iscomment) {
405                         $params["object"] = array(
406                                                 'objectType' => "note",
407                                                 'content' => $title.$content);
408
409                         if ($public)
410                                 $params["to"] = array(Array(
411                                                         "objectType" => "collection",
412                                                         "id" => "http://activityschema.org/collection/public"));
413                  } else {
414                         $inReplyTo = array("id" => $orig_post["uri"],
415                                         "objectType" => "note");
416
417                         $params["object"] = array(
418                                                 'objectType' => "comment",
419                                                 'content' => $title.$content,
420                                                 'inReplyTo' => $inReplyTo);
421                 }
422
423                 $client = new oauth_client_class;
424                 $client->oauth_version = '1.0a';
425                 $client->url_parameters = false;
426                 $client->authorization_header = true;
427                 $client->access_token = $oauth_token;
428                 $client->access_token_secret = $oauth_token_secret;
429                 $client->client_id = $consumer_key;
430                 $client->client_secret = $consumer_secret;
431
432                 $username = $user.'@'.$host;
433                 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
434
435                 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
436                 //$success = false;
437
438                 if($success) {
439                         $post_id = $user->object->id;
440                         logger('pumpio_send '.$username.': success '.$post_id);
441                         if($post_id AND $iscomment) {
442                                 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
443                                 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
444                                         dbesc($post_id),
445                                         intval($b['id'])
446                                 );
447                         }
448                 } else {
449                         logger('pumpio_send '.$username.': general error: ' . print_r($user,true));
450
451                         $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
452                         if (count($r))
453                                 $a->contact = $r[0]["id"];
454
455                         $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $params));
456                         require_once('include/queue_fn.php');
457                         add_to_queue($a->contact,NETWORK_PUMPIO,$s);
458                         notice(t('Pump.io post failed. Queued for retry.').EOL);
459                 }
460
461         }
462 }
463
464 function pumpio_cron($a,$b) {
465         $last = get_config('pumpio','last_poll');
466
467         $poll_interval = intval(get_config('pumpio','poll_interval'));
468         if(! $poll_interval)
469                 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
470
471         if($last) {
472                 $next = $last + ($poll_interval * 60);
473                 if($next > time()) {
474                         logger('pumpio: poll intervall not reached');
475                         return;
476                 }
477         }
478         logger('pumpio: cron_start');
479
480         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
481         if(count($r)) {
482                 foreach($r as $rr) {
483                         logger('pumpio: mirroring user '.$rr['uid']);
484                         pumpio_fetchtimeline($a, $rr['uid']);
485                 }
486         }
487
488         $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
489         if(count($r)) {
490                 foreach($r as $rr) {
491                         logger('pumpio: importing timeline from user '.$rr['uid']);
492                         pumpio_fetchinbox($a, $rr['uid']);
493                 }
494         }
495
496         // To-Do:
497         //pumpio_getallusers($a, $uid);
498
499         logger('pumpio: cron_end');
500
501         set_config('pumpio','last_poll', time());
502 }
503
504 function pumpio_fetchtimeline($a, $uid) {
505         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
506         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
507         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
508         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
509         $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
510         $hostname = get_pconfig($uid, 'pumpio','host');
511         $username = get_pconfig($uid, "pumpio", "user");
512
513         $application_name  = get_config('pumpio', 'application_name');
514
515         if ($application_name == "")
516                 $application_name = $a->get_hostname();
517
518         $first_time = ($lastdate == "");
519
520         $client = new oauth_client_class;
521         $client->oauth_version = '1.0a';
522         $client->authorization_header = true;
523         $client->url_parameters = false;
524
525         $client->client_id = $ckey;
526         $client->client_secret = $csecret;
527         $client->access_token = $otoken;
528         $client->access_token_secret = $osecret;
529
530         $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
531
532         logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
533
534         $username = $user.'@'.$host;
535
536         $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
537
538         if (!$success) {
539                 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
540                 return;
541         }
542
543         $posts = array_reverse($user->items);
544
545         $initiallastdate = $lastdate;
546         $lastdate = '';
547
548         if (count($posts)) {
549                 foreach ($posts as $post) {
550                         if ($post->generator->published <= $initiallastdate)
551                                 continue;
552
553                         if ($lastdate < $post->generator->published)
554                                 $lastdate = $post->generator->published;
555
556                         if ($first_time)
557                                 continue;
558
559                         $receiptians = array();
560                         if (@is_array($post->cc))
561                                 $receiptians = array_merge($receiptians, $post->cc);
562
563                         if (@is_array($post->to))
564                                 $receiptians = array_merge($receiptians, $post->to);
565
566                         $public = false;
567                         foreach ($receiptians AS $receiver)
568                                 if (is_string($receiver->objectType))
569                                         if ($receiver->id == "http://activityschema.org/collection/public")
570                                                 $public = true;
571
572                         if ($public AND !strstr($post->generator->displayName, $application_name)) {
573                                 require_once('include/html2bbcode.php');
574
575                                 $_SESSION["authenticated"] = true;
576                                 $_SESSION["uid"] = $uid;
577
578                                 unset($_REQUEST);
579                                 $_REQUEST["type"] = "wall";
580                                 $_REQUEST["api_source"] = true;
581                                 $_REQUEST["profile_uid"] = $uid;
582                                 $_REQUEST["source"] = "pump.io";
583
584                                 if ($post->object->displayName != "")
585                                         $_REQUEST["title"] = html2bbcode($post->object->displayName);
586                                 else
587                                         $_REQUEST["title"] = "";
588
589                                 $_REQUEST["body"] = html2bbcode($post->object->content);
590
591                                 if ($post->object->fullImage->url != "")
592                                         $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
593
594                                 logger('pumpio: posting for user '.$uid);
595
596                                 require_once('mod/item.php');
597                                 //print_r($_REQUEST);
598                                 item_post($a);
599                                 logger('pumpio: posting done - user '.$uid);
600                         }
601                 }
602         }
603
604         //$lastdate = '2013-05-16T20:22:12Z';
605
606         if ($lastdate != 0)
607                 set_pconfig($uid,'pumpio','lastdate', $lastdate);
608 }
609
610 function pumpio_dolike(&$a, $uid, $self, $post) {
611
612 /*
613     // If we posted the like locally, it will be found with our url, not the FB url.
614
615     $second_url = (($likes->id == $fb_id) ? $self[0]['url'] : 'http://facebook.com/profile.php?id=' . $likes->id);
616
617     $r = q("SELECT * FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `verb` = '%s'
618         AND ( `author-link` = '%s' OR `author-link` = '%s' ) LIMIT 1",
619         dbesc($orig_post['uri']),
620         intval($uid),
621         dbesc(ACTIVITY_LIKE),
622         dbesc('http://facebook.com/profile.php?id=' . $likes->id),
623         dbesc($second_url)
624     );
625
626     if(count($r))
627         return;
628 */
629
630         // To-Do
631         $own_id = "123455678";
632
633         // Two queries for speed issues
634         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
635                                 dbesc($post->object->id),
636                                 intval($uid)
637                 );
638
639         if (count($r))
640                 $orig_post = $r[0];
641         else {
642                 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
643                                         dbesc($post->object->id),
644                                         intval($uid)
645                         );
646
647                 if (!count($r))
648                         return;
649                 else
650                         $orig_post = $r[0];
651         }
652
653         $r = q("SELECT parent FROM `item` WHERE `verb` = '%s' AND `uid` = %d AND `author-link` = '%s' AND parent = %d LIMIT 1",
654                 dbesc(ACTIVITY_LIKE),
655                 intval($uid),
656                 dbesc($post->actor->url),
657                 intval($orig_post['id'])
658         );
659
660         if(count($r))
661                 return;
662
663         $likedata = array();
664         $likedata['parent'] = $orig_post['id'];
665         $likedata['verb'] = ACTIVITY_LIKE;
666         $likedata['gravity'] = 3;
667         $likedata['uid'] = $uid;
668         $likedata['wall'] = 0;
669         $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
670         $likedata['parent-uri'] = $orig_post["uri"];
671
672         if($likes->id == $own_id)
673                 $likedata['contact-id'] = $self[0]['id'];
674         else {
675                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
676                         dbesc($post->actor->url),
677                         intval($uid)
678                 );
679
680                 if(count($r))
681                         $likedata['contact-id'] = $r[0]['id'];
682         }
683         if(! x($likedata,'contact-id'))
684                 $likedata['contact-id'] = $orig_post['contact-id'];
685
686         $likedata['app'] = $post->generator->displayName;
687         $likedata['verb'] = ACTIVITY_LIKE;
688         $likedata['author-name'] = $post->actor->displayName;
689         $likedata['author-link'] = $post->actor->url;
690         $likedata['author-avatar'] = $post->actor->image->url;
691
692         $author  = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
693         $objauthor =  '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
694         $post_type = t('status');
695         $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
696         $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
697
698         $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
699
700         $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
701                 '<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>';
702
703         item_store($likedata);
704 }
705
706 function pumpio_get_contact($uid, $contact) {
707
708         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
709                 intval($uid), dbesc($contact->url));
710
711         if(!count($r)) {
712                 // create contact record
713                 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
714                                         `name`, `nick`, `photo`, `network`, `rel`, `priority`,
715                                         `writable`, `blocked`, `readonly`, `pending` )
716                                 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
717                         intval($uid),
718                         dbesc(datetime_convert()),
719                         dbesc($contact->url),
720                         dbesc(normalise_link($contact->url)),
721                         dbesc(str_replace("acct:", "", $contact->id)),
722                         dbesc(''),
723                         dbesc($contact->id), // To-Do?
724                         dbesc('pump.io ' . $contact->id), // To-Do?
725                         dbesc($contact->displayName),
726                         dbesc($contact->preferredUsername),
727                         dbesc($contact->image->url),
728                         dbesc(NETWORK_PUMPIO),
729                         intval(CONTACT_IS_FRIEND),
730                         intval(1),
731                         intval(1)
732                 );
733
734                 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
735                         dbesc($contact->url),
736                         intval($uid)
737                         );
738
739                 if(! count($r))
740                         return(false);
741
742                 $contact_id  = $r[0]['id'];
743
744                 $g = q("select def_gid from user where uid = %d limit 1",
745                         intval($uid)
746                 );
747
748                 if($g && intval($g[0]['def_gid'])) {
749                         require_once('include/group.php');
750                         group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
751                 }
752
753                 require_once("Photo.php");
754
755                 $photos = import_profile_photo($contact->image->url,$uid,$contact_id);
756
757                 q("UPDATE `contact` SET `photo` = '%s',
758                                         `thumb` = '%s',
759                                         `micro` = '%s',
760                                         `name-date` = '%s',
761                                         `uri-date` = '%s',
762                                         `avatar-date` = '%s'
763                                 WHERE `id` = %d LIMIT 1
764                         ",
765                 dbesc($photos[0]),
766                 dbesc($photos[1]),
767                 dbesc($photos[2]),
768                 dbesc(datetime_convert()),
769                 dbesc(datetime_convert()),
770                 dbesc(datetime_convert()),
771                 intval($contact_id)
772                 );
773         } else {
774                 // update profile photos once every two weeks as we have no notification of when they change.
775
776                 $update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
777
778                 // check that we have all the photos, this has been known to fail on occasion
779
780                 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
781                         require_once("Photo.php");
782
783                         $photos = import_profile_photo($contact->image->url, $uid, $r[0]['id']);
784
785                         q("UPDATE `contact` SET `photo` = '%s',
786                                         `thumb` = '%s',
787                                         `micro` = '%s',
788                                         `name-date` = '%s',
789                                         `uri-date` = '%s',
790                                         `avatar-date` = '%s'
791                                         WHERE `id` = %d LIMIT 1
792                                 ",
793                         dbesc($photos[0]),
794                         dbesc($photos[1]),
795                         dbesc($photos[2]),
796                         dbesc(datetime_convert()),
797                         dbesc(datetime_convert()),
798                         dbesc(datetime_convert()),
799                         intval($r[0]['id'])
800                         );
801                 }
802
803         }
804
805         return($r[0]["id"]);
806 }
807
808 function pumpio_dodelete(&$a, $client, $uid, $self, $post) {
809
810         // Two queries for speed issues
811         $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
812                                 dbesc($post->object->id),
813                                 intval($uid)
814                 );
815
816         if (count($r)) {
817                 drop_item($r[0]["id"], $false);
818                 return;
819         }
820
821         $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
822                                 dbesc($post->object->id),
823                                 intval($uid)
824                 );
825
826         if (count($r)) {
827                 drop_item($r[0]["id"], $false);
828                 return;
829         }
830 }
831
832 function pumpio_dopost(&$a, $client, $uid, $self, $post) {
833         require_once('include/items.php');
834
835         if ($post->verb == "like") {
836                 pumpio_dolike(&$a, $uid, $self, $post);
837                 return;
838         }
839
840         if ($post->verb == "delete") {
841                 pumpio_dodelete(&$a, $uid, $self, $post);
842                 return;
843         }
844
845         // Only handle these three types
846         if (!strstr("post|share|update", $post->verb))
847                 return;
848
849         if ($post->verb != "update") {
850                 // Two queries for speed issues
851                 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
852                                         dbesc($post->object->id),
853                                         intval($uid)
854                         );
855
856                 if (count($r))
857                         return;
858
859                 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
860                                         dbesc($post->object->id),
861                                         intval($uid)
862                         );
863
864                 if (count($r))
865                         return;
866         }
867
868         $receiptians = array();
869         if (@is_array($post->cc))
870                 $receiptians = array_merge($receiptians, $post->cc);
871
872         if (@is_array($post->to))
873                 $receiptians = array_merge($receiptians, $post->to);
874
875         foreach ($receiptians AS $receiver)
876                 if (is_string($receiver->objectType))
877                         if ($receiver->id == "http://activityschema.org/collection/public")
878                                 $public = true;
879
880         $contact_id = pumpio_get_contact($uid, $post->actor);
881
882         if (!$contact_id)
883                 $contact_id = $self[0]['id'];
884
885         $postarray = array();
886         $postarray['gravity'] = 0;
887         $postarray['uid'] = $uid;
888         $postarray['wall'] = 0;
889         $postarray['uri'] = $post->object->id;
890
891         if ($post->object->objectType != "comment") {
892                 $postarray['parent-uri'] = $post->object->id;
893         } else {
894                 //echo($post->object->inReplyTo->url."\n");
895                 //print_r($post->object->inReplyTo);
896                 //echo $post->object->inReplyTo->likes->url."\n";
897                 //$replies = $post->object->inReplyTo->replies->url;
898                 //$replies = $post->object->likes->pump_io->proxyURL;
899                 //$replies = $post->object->replies->pump_io->proxyURL;
900
901                 /*
902                 //$replies = $post->object->replies->pump_io->proxyURL;
903
904                 if ($replies != "") {
905                         $success = $client->CallAPI($replies, 'GET', array(), array('FailOnAccessError'=>true), $replydata);
906                         print_r($replydata);
907                 } else
908                         print_r($post);
909                 */
910
911                 $reply->verb = "note";
912                 $reply->cc = $post->cc;
913                 $reply->to = $post->to;
914                 $reply->object->objectType = $post->object->inReplyTo->objectType;
915                 $reply->object->content = $post->object->inReplyTo->content;
916                 $reply->object->id = $post->object->inReplyTo->id;
917                 $reply->actor = $post->object->inReplyTo->author;
918                 $reply->url = $post->object->inReplyTo->url;
919                 $reply->generator->displayName = "pumpio";
920                 $reply->published = $post->object->inReplyTo->published;
921                 $reply->received = $post->object->inReplyTo->updated;
922                 $reply->url = $post->object->inReplyTo->url;
923                 pumpio_dopost(&$a, $client, $uid, $self, $reply);
924
925                 $postarray['parent-uri'] = $post->object->inReplyTo->id;
926         }
927
928         $postarray['contact-id'] = $contact_id;
929         $postarray['verb'] = ACTIVITY_POST;
930         $postarray['owner-name'] = $post->actor->displayName;
931         $postarray['owner-link'] = $post->actor->url;
932         $postarray['owner-avatar'] = $post->actor->image->url;
933         $postarray['author-name'] = $post->actor->displayName;
934         $postarray['author-link'] = $post->actor->url;
935         $postarray['author-avatar'] = $post->actor->image->url;
936         $postarray['plink'] = $post->object->url;
937         $postarray['app'] = $post->generator->displayName;
938         $postarray['body'] = html2bbcode($post->object->content);
939
940         if ($post->object->fullImage->url != "")
941                 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
942
943         if ($post->object->displayName != "")
944                 $postarray['title'] = $post->object->displayName;
945
946         //$postarray['location'] = ""
947         $postarray['created'] = datetime_convert('UTC','UTC',$post->published);
948         $postarray['edited'] = datetime_convert('UTC','UTC',$post->received);
949         if (!$public) {
950                 $postarray['private'] = 1;
951                 $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
952         }
953
954         if ($post->verb == "share") {
955                 $postarray['body'] = "[share author='".$post->object->author->displayName.
956                                 "' profile='".$post->object->author->url.
957                                 "' avatar='".$post->object->author->image->url.
958                                 "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
959         }
960
961         if (trim($postarray['body']) == "")
962                 return;
963
964         $top_item = item_store($postarray);
965
966         if (($top_item == 0) AND ($post->verb == "update")) {
967                 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
968                         dbesc($postarray["title"]),
969                         dbesc($postarray["body"]),
970                         dbesc($postarray["edited"]),
971                         dbesc($postarray["uri"]),
972                         intval($uid)
973                         );
974         }
975
976         if ($post->object->objectType == "comment") {
977
978                 $hostname = get_pconfig($uid, 'pumpio','host');
979                 $username = get_pconfig($uid, "pumpio", "user");
980
981                 $foreign_url = "https://".$hostname."/".$username;
982
983                 $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
984                                 intval($uid)
985                         );
986
987                 if(!count($user))
988                         return;
989
990                 $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
991
992                 if (link_compare($foreign_url, $postarray['author-link']))
993                         return;
994
995                 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
996                                 dbesc($postarray['parent-uri']),
997                                 intval($uid)
998                                 );
999
1000                 if(count($myconv)) {
1001
1002                         foreach($myconv as $conv) {
1003                                 // now if we find a match, it means we're in this conversation
1004
1005                                 if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$foreign_url))
1006                                         continue;
1007
1008                                 require_once('include/enotify.php');
1009
1010                                 $conv_parent = $conv['parent'];
1011
1012                                 notification(array(
1013                                         'type'         => NOTIFY_COMMENT,
1014                                         'notify_flags' => $user[0]['notify-flags'],
1015                                         'language'     => $user[0]['language'],
1016                                         'to_name'      => $user[0]['username'],
1017                                         'to_email'     => $user[0]['email'],
1018                                         'uid'          => $user[0]['uid'],
1019                                         'item'         => $postarray,
1020                                         'link'             => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $top_item,
1021                                         'source_name'  => $postarray['author-name'],
1022                                         'source_link'  => $postarray['author-link'],
1023                                         'source_photo' => $postarray['author-avatar'],
1024                                         'verb'         => ACTIVITY_POST,
1025                                         'otype'        => 'item',
1026                                         'parent'       => $conv_parent,
1027                                         ));
1028
1029                                 // only send one notification
1030                                 break;
1031                         }
1032                 }
1033         }
1034 }
1035
1036 function pumpio_fetchinbox($a, $uid) {
1037
1038         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
1039         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1040         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
1041         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1042         $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
1043         $hostname = get_pconfig($uid, 'pumpio','host');
1044         $username = get_pconfig($uid, "pumpio", "user");
1045
1046         $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1047                 intval($uid));
1048
1049         $client = new oauth_client_class;
1050         $client->oauth_version = '1.0a';
1051         $client->authorization_header = true;
1052         $client->url_parameters = false;
1053
1054         $client->client_id = $ckey;
1055         $client->client_secret = $csecret;
1056         $client->access_token = $otoken;
1057         $client->access_token_secret = $osecret;
1058
1059         $last_id = get_pconfig($uid,'pumpio','last_id');
1060
1061         $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1062
1063         if ($last_id != "")
1064                 $url .= '?since='.urlencode($last_id);
1065
1066         $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
1067         $posts = array_reverse($user->items);
1068
1069         if (count($posts))
1070                 foreach ($posts as $post) {
1071                         $last_id = $post->id;
1072                         pumpio_dopost(&$a, $client, $uid, $self, $post);
1073                 }
1074
1075         set_pconfig($uid,'pumpio','last_id', $last_id);
1076 }
1077
1078 function pumpio_getallusers($a, $uid) {
1079         $ckey    = get_pconfig($uid, 'pumpio', 'consumer_key');
1080         $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1081         $otoken  = get_pconfig($uid, 'pumpio', 'oauth_token');
1082         $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1083         $hostname = get_pconfig($uid, 'pumpio','host');
1084         $username = get_pconfig($uid, "pumpio", "user");
1085
1086         $client = new oauth_client_class;
1087         $client->oauth_version = '1.0a';
1088         $client->authorization_header = true;
1089         $client->url_parameters = false;
1090
1091         $client->client_id = $ckey;
1092         $client->client_secret = $csecret;
1093         $client->access_token = $otoken;
1094         $client->access_token_secret = $osecret;
1095
1096         $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1097
1098         $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1099
1100         if ($users->totalItems > count($users->items)) {
1101                 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1102
1103                 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1104         }
1105
1106         foreach ($users->items AS $user)
1107                 echo pumpio_get_contact($uid, $user)."\n";
1108 }
1109
1110 function pumpio_queue_hook(&$a,&$b) {
1111
1112         $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
1113                 dbesc(NETWORK_PUMPIO)
1114         );
1115         if(! count($qi))
1116                 return;
1117
1118         require_once('include/queue_fn.php');
1119
1120         foreach($qi as $x) {
1121                 if($x['network'] !== NETWORK_PUMPIO)
1122                         continue;
1123
1124                 logger('pumpio_queue: run '.print_r($x, true));
1125
1126                 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid` 
1127                         WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
1128                         intval($x['cid'])
1129                 );
1130                 if(! count($r))
1131                         continue;
1132
1133                 $user = $r[0];
1134
1135                 $oauth_token = get_pconfig($user['uid'], "pumpio", "oauth_token");
1136                 $oauth_token_secret = get_pconfig($user['uid'], "pumpio", "oauth_token_secret");
1137                 $consumer_key = get_pconfig($user['uid'], "pumpio","consumer_key");
1138                 $consumer_secret = get_pconfig($user['uid'], "pumpio","consumer_secret");
1139
1140                 $host = get_pconfig($user['uid'], "pumpio", "host");
1141                 $user = get_pconfig($user['uid'], "pumpio", "user");
1142
1143                 $success = false;
1144
1145                 if ($oauth_token AND $oauth_token_secret AND
1146                         $consumer_key AND $consumer_secret) {
1147                         $username = $user.'@'.$host;
1148
1149                         logger('pumpio_queue: able to post for user '.$username);
1150
1151                         $z = unserialize($x['content']);
1152
1153                         $client = new oauth_client_class;
1154                         $client->oauth_version = '1.0a';
1155                         $client->url_parameters = false;
1156                         $client->authorization_header = true;
1157                         $client->access_token = $oauth_token;
1158                         $client->access_token_secret = $oauth_token_secret;
1159                         $client->client_id = $consumer_key;
1160                         $client->client_secret = $consumer_secret;
1161
1162                         $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
1163
1164                         if($success) {
1165                                 $post_id = $user->object->id;
1166                                 logger('pumpio_queue: send '.$username.': success '.$post_id);
1167                                 if($post_id AND $iscomment) {
1168                                         logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$z['item']);
1169                                         q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
1170                                                 dbesc($post_id),
1171                                                 intval($z['item'])
1172                                         );
1173                                 }
1174                                 remove_queue_item($x['id']);
1175                         } else
1176                                 logger('pumpio_queue: send '.$username.': general error: ' . print_r($user,true));
1177                 } else
1178                         logger("pumpio_queue: Error getting tokens for user ".$user['uid']);
1179
1180                 if (!$success) {
1181                         logger('pumpio_queue: delayed');
1182                         update_queue_time($x['id']);
1183                 }
1184         }
1185 }
1186
1187 /*
1188 To-Do:
1189  - doing likes
1190  - importing unlike
1191
1192 Work:
1193  - edit own posts
1194  - delete own posts
1195
1196 Problem:
1197  - Threads completion
1198  - refresh after post
1199
1200 */