3 * Name: pump.io Post Connector
4 * Description: Post to pump.io
6 * Author: Michael Vogel <http://pirati.ca/profile/heluecht>
8 require('addon/pumpio/oauth/http.php');
9 require('addon/pumpio/oauth/oauth_client.php');
11 define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
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');
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');
33 function pumpio_module() {}
35 function pumpio_content(&$a) {
38 notice( t('Permission denied.') . EOL);
42 if (isset($a->argv[1]))
43 switch ($a->argv[1]) {
45 $o = pumpio_connect($a);
48 $o = print_r($a->argv, true);
52 $o = pumpio_connect($a);
57 function pumpio_registerclient($a, $host) {
59 $url = "https://".$host."/api/client/register";
63 $application_name = get_config('pumpio', 'application_name');
65 if ($application_name == "")
66 $application_name = $a->get_hostname();
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";
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");
83 $curl_info = curl_getinfo($ch);
85 if ($curl_info["http_code"] == "200") {
86 $values = json_decode($s);
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
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');
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);
106 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
107 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
110 if (($consumer_key == "") OR ($consumer_secret == ""))
113 // The callback URL is the script that gets called after the user authenticates with pumpio
114 $callback_url = $a->get_baseurl()."/pumpio/connect";
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.
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;
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;
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);
141 $success = $client->Finalize($success);
144 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
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>';
154 function pumpio_jot_nets(&$a,&$b) {
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>';
168 function pumpio_settings(&$a,&$s) {
173 /* Add our stylesheet to the page so we can make our settings look nice */
175 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
177 /* Get the current state of our config variables */
179 $import_enabled = get_pconfig(local_user(),'pumpio','import');
180 $import_checked = (($import_enabled) ? ' checked="checked" ' : '');
182 $enabled = get_pconfig(local_user(),'pumpio','post');
183 $checked = (($enabled) ? ' checked="checked" ' : '');
185 $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
186 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
188 $public_enabled = get_pconfig(local_user(),'pumpio','public');
189 $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
191 $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
192 $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
194 $servername = get_pconfig(local_user(), "pumpio", "host");
195 $username = get_pconfig(local_user(), "pumpio", "user");
197 /* Add some HTML to the existing form */
199 $s .= '<div class="settings-block">';
200 $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
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>';
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>';
212 if (($username != '') AND ($servername != '')) {
214 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
215 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
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>';
223 $s .= '<div id="pumpio-import-wrapper">';
224 $s .= '<label id="pumpio-import-label" for="pumpio-import">' . t('Import the remote timeline') . '</label>';
225 $s .= '<input id="pumpio-import" type="checkbox" name="pumpio_import" value="1" ' . $import_checked . '/>';
226 $s .= '</div><div class="clear"></div>';
228 $s .= '<div id="pumpio-enable-wrapper">';
229 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
230 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
231 $s .= '</div><div class="clear"></div>';
233 $s .= '<div id="pumpio-bydefault-wrapper">';
234 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
235 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
236 $s .= '</div><div class="clear"></div>';
238 $s .= '<div id="pumpio-public-wrapper">';
239 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
240 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
241 $s .= '</div><div class="clear"></div>';
243 $s .= '<div id="pumpio-mirror-wrapper">';
244 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
245 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
246 $s .= '</div><div class="clear"></div>';
248 $s .= '<div id="pumpio-delete-wrapper">';
249 $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . t('Check to delete this preset') . '</label>';
250 $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
251 $s .= '</div><div class="clear"></div>';
254 $s .= '</div><div class="clear"></div>';
257 /* provide a submit button */
259 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
264 function pumpio_settings_post(&$a,&$b) {
266 if(x($_POST,'pumpio-submit')) {
267 if(x($_POST,'pumpio_delete')) {
268 set_pconfig(local_user(),'pumpio','consumer_key','');
269 set_pconfig(local_user(),'pumpio','consumer_secret','');
270 set_pconfig(local_user(),'pumpio','host','');
271 set_pconfig(local_user(),'pumpio','oauth_token','');
272 set_pconfig(local_user(),'pumpio','oauth_token_secret','');
273 set_pconfig(local_user(),'pumpio','post',false);
274 set_pconfig(local_user(),'pumpio','post_by_default',false);
275 set_pconfig(local_user(),'pumpio','user','');
277 // filtering the username if it is filled wrong
278 $user = $_POST['pumpio_user'];
279 if (strstr($user, "@")) {
280 $pos = strpos($user, "@");
282 $user = substr($user, 0, $pos);
285 // Filtering the hostname if someone is entering it with "http"
286 $host = $_POST['pumpio_host'];
288 $host = str_replace(array("https://", "http://"), array("", ""), $host);
290 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
291 set_pconfig(local_user(),'pumpio','import',$_POST['pumpio_import']);
292 set_pconfig(local_user(),'pumpio','host',$host);
293 set_pconfig(local_user(),'pumpio','user',$user);
294 set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
295 set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
296 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
301 function pumpio_post_local(&$a,&$b) {
303 if((! local_user()) || (local_user() != $b['uid']))
306 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
308 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
310 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
316 if(strlen($b['postopts']))
317 $b['postopts'] .= ',';
319 $b['postopts'] .= 'pumpio';
325 function pumpio_send(&$a,&$b) {
327 if (!get_pconfig($b["uid"],'pumpio','import')) {
328 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
332 logger("pumpio_send: parameter ".print_r($b, true));
334 if($b['parent'] != $b['id']) {
335 // Looking if its a reply to a pumpio post
336 $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",
337 intval($b["parent"]),
339 dbesc(NETWORK_PUMPIO));
342 logger("pumpio_send: no pumpio post ".$b["parent"]);
351 $receiver = pumpio_getreceiver($a, $b);
353 logger("pumpio_send: receiver ".print_r($receiver, true));
355 if (!count($receiver) AND ($b['private'] OR !strstr($b['postopts'],'pumpio')))
359 if($b['verb'] == ACTIVITY_LIKE) {
361 pumpio_action($a, $b["uid"], $b["thr-parent"], "unlike");
363 pumpio_action($a, $b["uid"], $b["thr-parent"], "like");
367 if($b['verb'] == ACTIVITY_DISLIKE)
370 if (($b['verb'] == ACTIVITY_POST) AND ($b['created'] !== $b['edited']) AND !$b['deleted'])
371 pumpio_action($a, $b["uid"], $b["uri"], "update", $b["body"]);
373 if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
374 pumpio_action($a, $b["uid"], $b["uri"], "delete");
376 if($b['deleted'] || ($b['created'] !== $b['edited']))
379 // if post comes from pump.io don't send it back
380 if($b['app'] == "pump.io")
384 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
385 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
386 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
387 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
389 $host = get_pconfig($b['uid'], "pumpio", "host");
390 $user = get_pconfig($b['uid'], "pumpio", "user");
391 $public = get_pconfig($b['uid'], "pumpio", "public");
393 if($oauth_token && $oauth_token_secret) {
395 require_once('include/bbcode.php');
397 $title = trim($b['title']);
400 $title = "<h4>".$title."</h4>";
402 $content = bbcode($b['body'], false, false);
404 // Enhance the way, videos are displayed
405 $content = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
406 $content = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
407 $content = preg_replace('/<a.*?href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
408 $content = preg_replace('/<a.*?href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
410 $URLSearchString = "^\[\]";
411 $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
415 $params["verb"] = "post";
418 $params["object"] = array(
419 'objectType' => "note",
420 'content' => $title.$content);
422 if (count($receiver["to"]))
423 $params["to"] = $receiver["to"];
425 if (count($receiver["bto"]))
426 $params["bto"] = $receiver["bto"];
428 if (count($receiver["cc"]))
429 $params["cc"] = $receiver["cc"];
431 if (count($receiver["bcc"]))
432 $params["bcc"] = $receiver["bcc"];
435 $inReplyTo = array("id" => $orig_post["uri"],
436 "objectType" => "note");
438 $params["object"] = array(
439 'objectType' => "comment",
440 'content' => $title.$content,
441 'inReplyTo' => $inReplyTo);
444 $client = new oauth_client_class;
445 $client->oauth_version = '1.0a';
446 $client->url_parameters = false;
447 $client->authorization_header = true;
448 $client->access_token = $oauth_token;
449 $client->access_token_secret = $oauth_token_secret;
450 $client->client_id = $consumer_key;
451 $client->client_secret = $consumer_secret;
453 $username = $user.'@'.$host;
454 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
456 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
459 $post_id = $user->object->id;
460 logger('pumpio_send '.$username.': success '.$post_id);
461 if($post_id AND $iscomment) {
462 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
463 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
469 logger('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user,true));
471 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
473 $a->contact = $r[0]["id"];
475 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $params));
476 require_once('include/queue_fn.php');
477 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
478 notice(t('Pump.io post failed. Queued for retry.').EOL);
484 function pumpio_action($a, $uid, $uri, $action, $content) {
486 // Don't do likes and other stuff if you don't import the timeline
487 if (!get_pconfig($uid,'pumpio','import'))
490 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
491 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
492 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
493 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
494 $hostname = get_pconfig($uid, 'pumpio','host');
495 $username = get_pconfig($uid, "pumpio", "user");
497 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
507 if ($orig_post["extid"] AND !strstr($orig_post["extid"], "/proxy/"))
508 $uri = $orig_post["extid"];
510 $uri = $orig_post["uri"];
512 if (strstr($uri, "/api/comment/"))
513 $objectType = "comment";
514 elseif (strstr($uri, "/api/note/"))
515 $objectType = "note";
516 elseif (strstr($uri, "/api/image/"))
517 $objectType = "image";
519 $params["verb"] = $action;
520 $params["object"] = array('id' => $uri,
521 "objectType" => $objectType,
522 "content" => $content);
524 $client = new oauth_client_class;
525 $client->oauth_version = '1.0a';
526 $client->authorization_header = true;
527 $client->url_parameters = false;
529 $client->client_id = $ckey;
530 $client->client_secret = $csecret;
531 $client->access_token = $otoken;
532 $client->access_token_secret = $osecret;
534 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
536 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
539 logger('pumpio_action '.$username.' '.$action.': success '.$uri);
541 logger('pumpio_action '.$username.' '.$action.': general error: '.$uri.' '.print_r($user,true));
543 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
545 $a->contact = $r[0]["id"];
547 $s = serialize(array('url' => $url, 'item' => $orig_post["id"], 'post' => $params));
548 require_once('include/queue_fn.php');
549 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
550 notice(t('Pump.io like failed. Queued for retry.').EOL);
555 function pumpio_cron($a,$b) {
556 $last = get_config('pumpio','last_poll');
558 $poll_interval = intval(get_config('pumpio','poll_interval'));
560 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
563 $next = $last + ($poll_interval * 60);
565 logger('pumpio: poll intervall not reached');
569 logger('pumpio: cron_start');
571 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
574 logger('pumpio: mirroring user '.$rr['uid']);
575 pumpio_fetchtimeline($a, $rr['uid']);
579 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
582 logger('pumpio: importing timeline from user '.$rr['uid']);
583 pumpio_fetchinbox($a, $rr['uid']);
585 // check for new contacts once a day
586 $last_contact_check = get_pconfig($rr['uid'],'pumpio','contact_check');
587 if($last_contact_check)
588 $next_contact_check = $last_contact_check + 86400;
590 $next_contact_check = 0;
592 if($next_contact_check <= time()) {
593 pumpio_getallusers($a, $rr["uid"]);
594 set_pconfig($rr['uid'],'pumpio','contact_check',time());
599 logger('pumpio: cron_end');
601 set_config('pumpio','last_poll', time());
604 function pumpio_fetchtimeline($a, $uid) {
605 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
606 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
607 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
608 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
609 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
610 $hostname = get_pconfig($uid, 'pumpio','host');
611 $username = get_pconfig($uid, "pumpio", "user");
613 $application_name = get_config('pumpio', 'application_name');
615 if ($application_name == "")
616 $application_name = $a->get_hostname();
618 $first_time = ($lastdate == "");
620 $client = new oauth_client_class;
621 $client->oauth_version = '1.0a';
622 $client->authorization_header = true;
623 $client->url_parameters = false;
625 $client->client_id = $ckey;
626 $client->client_secret = $csecret;
627 $client->access_token = $otoken;
628 $client->access_token_secret = $osecret;
630 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
632 logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
634 $username = $user.'@'.$host;
636 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
639 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
643 $posts = array_reverse($user->items);
645 $initiallastdate = $lastdate;
649 foreach ($posts as $post) {
650 if ($post->generator->published <= $initiallastdate)
653 if ($lastdate < $post->generator->published)
654 $lastdate = $post->generator->published;
659 $receiptians = array();
660 if (@is_array($post->cc))
661 $receiptians = array_merge($receiptians, $post->cc);
663 if (@is_array($post->to))
664 $receiptians = array_merge($receiptians, $post->to);
667 foreach ($receiptians AS $receiver)
668 if (is_string($receiver->objectType))
669 if ($receiver->id == "http://activityschema.org/collection/public")
672 if ($public AND !strstr($post->generator->displayName, $application_name)) {
673 require_once('include/html2bbcode.php');
675 $_SESSION["authenticated"] = true;
676 $_SESSION["uid"] = $uid;
679 $_REQUEST["type"] = "wall";
680 $_REQUEST["api_source"] = true;
681 $_REQUEST["profile_uid"] = $uid;
682 $_REQUEST["source"] = "pump.io";
684 if ($post->object->displayName != "")
685 $_REQUEST["title"] = html2bbcode($post->object->displayName);
687 $_REQUEST["title"] = "";
689 $_REQUEST["body"] = html2bbcode($post->object->content);
691 if ($post->object->fullImage->url != "")
692 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
694 logger('pumpio: posting for user '.$uid);
696 require_once('mod/item.php');
699 logger('pumpio: posting done - user '.$uid);
705 set_pconfig($uid,'pumpio','lastdate', $lastdate);
708 function pumpio_dounlike(&$a, $uid, $self, $post, $own_id) {
709 // Searching for the unliked post
710 // Two queries for speed issues
711 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
712 dbesc($post->object->id),
719 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
720 dbesc($post->object->id),
732 if(link_compare($post->actor->url, $own_id)) {
733 $contactid = $self[0]['id'];
735 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
736 dbesc($post->actor->url),
741 $contactid = $r[0]['id'];
744 $contactid = $orig_post['contact-id'];
747 $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'",
748 dbesc(datetime_convert()),
749 dbesc(ACTIVITY_LIKE),
752 dbesc($orig_post['uri'])
756 logger("pumpio_dounlike: unliked existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
758 logger("pumpio_dounlike: not found. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
761 function pumpio_dolike(&$a, $uid, $self, $post, $own_id) {
763 // Searching for the liked post
764 // Two queries for speed issues
765 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
766 dbesc($post->object->id),
773 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
774 dbesc($post->object->id),
786 if(link_compare($post->actor->url, $own_id)) {
787 $contactid = $self[0]['id'];
788 $post->actor->displayName = $self[0]['name'];
789 $post->actor->url = $self[0]['url'];
790 $post->actor->image->url = $self[0]['photo'];
792 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
793 dbesc($post->actor->url),
798 $contactid = $r[0]['id'];
801 $contactid = $orig_post['contact-id'];
804 $r = q("SELECT parent FROM `item` WHERE `verb` = '%s' AND `uid` = %d AND `contact-id` = %d AND `thr-parent` = '%s' LIMIT 1",
805 dbesc(ACTIVITY_LIKE),
808 dbesc($orig_post['uri'])
812 logger("pumpio_dolike: found existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
817 $likedata['parent'] = $orig_post['id'];
818 $likedata['verb'] = ACTIVITY_LIKE;
819 $likedata['gravity'] = 3;
820 $likedata['uid'] = $uid;
821 $likedata['wall'] = 0;
822 $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
823 $likedata['parent-uri'] = $orig_post["uri"];
824 $likedata['contact-id'] = $contactid;
825 $likedata['app'] = $post->generator->displayName;
826 $likedata['verb'] = ACTIVITY_LIKE;
827 $likedata['author-name'] = $post->actor->displayName;
828 $likedata['author-link'] = $post->actor->url;
829 $likedata['author-avatar'] = $post->actor->image->url;
831 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
832 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
833 $post_type = t('status');
834 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
835 $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
837 $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
839 $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
840 '<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>';
842 $ret = item_store($likedata);
844 logger("pumpio_dolike: ".$ret." User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
847 function pumpio_get_contact($uid, $contact) {
849 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
850 intval($uid), dbesc($contact->url));
853 // create contact record
854 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
855 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
856 `writable`, `blocked`, `readonly`, `pending` )
857 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
859 dbesc(datetime_convert()),
860 dbesc($contact->url),
861 dbesc(normalise_link($contact->url)),
862 dbesc(str_replace("acct:", "", $contact->id)),
864 dbesc($contact->id), // What is it for?
865 dbesc('pump.io ' . $contact->id), // What is it for?
866 dbesc($contact->displayName),
867 dbesc($contact->preferredUsername),
868 dbesc($contact->image->url),
869 dbesc(NETWORK_PUMPIO),
870 intval(CONTACT_IS_FRIEND),
875 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
876 dbesc($contact->url),
883 $contact_id = $r[0]['id'];
885 $g = q("select def_gid from user where uid = %d limit 1",
889 if($g && intval($g[0]['def_gid'])) {
890 require_once('include/group.php');
891 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
894 require_once("Photo.php");
896 $photos = import_profile_photo($contact->image->url,$uid,$contact_id);
898 q("UPDATE `contact` SET `photo` = '%s',
904 WHERE `id` = %d LIMIT 1
909 dbesc(datetime_convert()),
910 dbesc(datetime_convert()),
911 dbesc(datetime_convert()),
915 // update profile photos once every two weeks as we have no notification of when they change.
917 $update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
919 // check that we have all the photos, this has been known to fail on occasion
921 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
922 require_once("Photo.php");
924 $photos = import_profile_photo($contact->image->url, $uid, $r[0]['id']);
926 q("UPDATE `contact` SET `photo` = '%s',
932 WHERE `id` = %d LIMIT 1
937 dbesc(datetime_convert()),
938 dbesc(datetime_convert()),
939 dbesc(datetime_convert()),
949 function pumpio_dodelete(&$a, $uid, $self, $post, $own_id) {
951 // Two queries for speed issues
952 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
953 dbesc($post->object->id),
958 return drop_item($r[0]["id"], $false);
960 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
961 dbesc($post->object->id),
966 return drop_item($r[0]["id"], $false);
969 function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id) {
970 require_once('include/items.php');
972 if (($post->verb == "like") OR ($post->verb == "favorite"))
973 return pumpio_dolike(&$a, $uid, $self, $post, $own_id);
975 if (($post->verb == "unlike") OR ($post->verb == "unfavorite"))
976 return pumpio_dounlike(&$a, $uid, $self, $post, $own_id);
978 if ($post->verb == "delete")
979 return pumpio_dodelete(&$a, $uid, $self, $post, $own_id);
981 if ($post->verb != "update") {
982 // Two queries for speed issues
983 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
984 dbesc($post->object->id),
991 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
992 dbesc($post->object->id),
1000 // Only handle these three types
1001 if (!strstr("post|share|update", $post->verb))
1004 $receiptians = array();
1005 if (@is_array($post->cc))
1006 $receiptians = array_merge($receiptians, $post->cc);
1008 if (@is_array($post->to))
1009 $receiptians = array_merge($receiptians, $post->to);
1011 foreach ($receiptians AS $receiver)
1012 if (is_string($receiver->objectType))
1013 if ($receiver->id == "http://activityschema.org/collection/public")
1016 $postarray = array();
1017 $postarray['gravity'] = 0;
1018 $postarray['uid'] = $uid;
1019 $postarray['wall'] = 0;
1020 $postarray['uri'] = $post->object->id;
1022 if ($post->object->objectType != "comment") {
1023 $contact_id = pumpio_get_contact($uid, $post->actor);
1026 $contact_id = $self[0]['id'];
1028 $postarray['parent-uri'] = $post->object->id;
1032 if(link_compare($post->actor->url, $own_id)) {
1033 $contact_id = $self[0]['id'];
1034 $post->actor->displayName = $self[0]['name'];
1035 $post->actor->url = $self[0]['url'];
1036 $post->actor->image->url = $self[0]['photo'];
1038 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1039 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1040 dbesc($post->actor->url),
1045 $contact_id = $r[0]['id'];
1047 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1048 dbesc($post->actor->url),
1053 $contact_id = $r[0]['id'];
1055 $contact_id = $self[0]['id'];
1059 $reply->verb = "note";
1060 $reply->cc = $post->cc;
1061 $reply->to = $post->to;
1062 $reply->object->objectType = $post->object->inReplyTo->objectType;
1063 $reply->object->content = $post->object->inReplyTo->content;
1064 $reply->object->id = $post->object->inReplyTo->id;
1065 $reply->actor = $post->object->inReplyTo->author;
1066 $reply->url = $post->object->inReplyTo->url;
1067 $reply->generator->displayName = "pumpio";
1068 $reply->published = $post->object->inReplyTo->published;
1069 $reply->received = $post->object->inReplyTo->updated;
1070 $reply->url = $post->object->inReplyTo->url;
1071 pumpio_dopost(&$a, $client, $uid, $self, $reply, $own_id);
1073 $postarray['parent-uri'] = $post->object->inReplyTo->id;
1076 if ($post->object->pump_io->proxyURL)
1077 $postarray['extid'] = $post->object->pump_io->proxyURL;
1079 $postarray['contact-id'] = $contact_id;
1080 $postarray['verb'] = ACTIVITY_POST;
1081 $postarray['owner-name'] = $post->actor->displayName;
1082 $postarray['owner-link'] = $post->actor->url;
1083 $postarray['owner-avatar'] = $post->actor->image->url;
1084 $postarray['author-name'] = $post->actor->displayName;
1085 $postarray['author-link'] = $post->actor->url;
1086 $postarray['author-avatar'] = $post->actor->image->url;
1087 $postarray['plink'] = $post->object->url;
1088 $postarray['app'] = $post->generator->displayName;
1089 $postarray['body'] = html2bbcode($post->object->content);
1091 if ($post->object->fullImage->url != "")
1092 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
1094 if ($post->object->displayName != "")
1095 $postarray['title'] = $post->object->displayName;
1097 $postarray['created'] = datetime_convert('UTC','UTC',$post->published);
1098 $postarray['edited'] = datetime_convert('UTC','UTC',$post->received);
1100 $postarray['private'] = 1;
1101 $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
1104 if ($post->verb == "share") {
1105 $postarray['body'] = "[share author='".$post->object->author->displayName.
1106 "' profile='".$post->object->author->url.
1107 "' avatar='".$post->object->author->image->url.
1108 "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
1111 if (trim($postarray['body']) == "")
1114 $top_item = item_store($postarray);
1116 if (($top_item == 0) AND ($post->verb == "update")) {
1117 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
1118 dbesc($postarray["title"]),
1119 dbesc($postarray["body"]),
1120 dbesc($postarray["edited"]),
1121 dbesc($postarray["uri"]),
1126 if ($post->object->objectType == "comment") {
1128 pumpio_fetchallcomments($a, $uid, $postarray['parent-uri']);
1130 $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
1137 $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
1139 if (link_compare($own_id, $postarray['author-link']))
1142 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
1143 dbesc($postarray['parent-uri']),
1147 if(count($myconv)) {
1149 foreach($myconv as $conv) {
1150 // now if we find a match, it means we're in this conversation
1152 if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$own_id))
1155 require_once('include/enotify.php');
1157 $conv_parent = $conv['parent'];
1160 'type' => NOTIFY_COMMENT,
1161 'notify_flags' => $user[0]['notify-flags'],
1162 'language' => $user[0]['language'],
1163 'to_name' => $user[0]['username'],
1164 'to_email' => $user[0]['email'],
1165 'uid' => $user[0]['uid'],
1166 'item' => $postarray,
1167 'link' => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $top_item,
1168 'source_name' => $postarray['author-name'],
1169 'source_link' => $postarray['author-link'],
1170 'source_photo' => $postarray['author-avatar'],
1171 'verb' => ACTIVITY_POST,
1173 'parent' => $conv_parent,
1176 // only send one notification
1185 function pumpio_fetchinbox($a, $uid) {
1187 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1188 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1189 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1190 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1191 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
1192 $hostname = get_pconfig($uid, 'pumpio','host');
1193 $username = get_pconfig($uid, "pumpio", "user");
1195 $own_id = "https://".$hostname."/".$username;
1197 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1200 $client = new oauth_client_class;
1201 $client->oauth_version = '1.0a';
1202 $client->authorization_header = true;
1203 $client->url_parameters = false;
1205 $client->client_id = $ckey;
1206 $client->client_secret = $csecret;
1207 $client->access_token = $otoken;
1208 $client->access_token_secret = $osecret;
1210 $last_id = get_pconfig($uid,'pumpio','last_id');
1212 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1215 $url .= '?since='.urlencode($last_id);
1217 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
1218 $posts = array_reverse($user->items);
1221 foreach ($posts as $post) {
1222 $last_id = $post->id;
1223 pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id);
1226 set_pconfig($uid,'pumpio','last_id', $last_id);
1229 function pumpio_getallusers($a, $uid) {
1230 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1231 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1232 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1233 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1234 $hostname = get_pconfig($uid, 'pumpio','host');
1235 $username = get_pconfig($uid, "pumpio", "user");
1237 $client = new oauth_client_class;
1238 $client->oauth_version = '1.0a';
1239 $client->authorization_header = true;
1240 $client->url_parameters = false;
1242 $client->client_id = $ckey;
1243 $client->client_secret = $csecret;
1244 $client->access_token = $otoken;
1245 $client->access_token_secret = $osecret;
1247 $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1249 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1251 if ($users->totalItems > count($users->items)) {
1252 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1254 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1257 foreach ($users->items AS $user)
1258 echo pumpio_get_contact($uid, $user)."\n";
1261 function pumpio_queue_hook(&$a,&$b) {
1263 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
1264 dbesc(NETWORK_PUMPIO)
1269 require_once('include/queue_fn.php');
1271 foreach($qi as $x) {
1272 if($x['network'] !== NETWORK_PUMPIO)
1275 logger('pumpio_queue: run');
1277 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
1278 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
1286 $oauth_token = get_pconfig($user['uid'], "pumpio", "oauth_token");
1287 $oauth_token_secret = get_pconfig($user['uid'], "pumpio", "oauth_token_secret");
1288 $consumer_key = get_pconfig($user['uid'], "pumpio","consumer_key");
1289 $consumer_secret = get_pconfig($user['uid'], "pumpio","consumer_secret");
1291 $host = get_pconfig($user['uid'], "pumpio", "host");
1292 $user = get_pconfig($user['uid'], "pumpio", "user");
1296 if ($oauth_token AND $oauth_token_secret AND
1297 $consumer_key AND $consumer_secret) {
1298 $username = $user.'@'.$host;
1300 logger('pumpio_queue: able to post for user '.$username);
1302 $z = unserialize($x['content']);
1304 $client = new oauth_client_class;
1305 $client->oauth_version = '1.0a';
1306 $client->url_parameters = false;
1307 $client->authorization_header = true;
1308 $client->access_token = $oauth_token;
1309 $client->access_token_secret = $oauth_token_secret;
1310 $client->client_id = $consumer_key;
1311 $client->client_secret = $consumer_secret;
1313 $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
1316 $post_id = $user->object->id;
1317 logger('pumpio_queue: send '.$username.': success '.$post_id);
1318 if($post_id AND $iscomment) {
1319 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$z['item']);
1320 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
1325 remove_queue_item($x['id']);
1327 logger('pumpio_queue: send '.$username.': '.$url.' general error: ' . print_r($user,true));
1329 logger("pumpio_queue: Error getting tokens for user ".$user['uid']);
1332 logger('pumpio_queue: delayed');
1333 update_queue_time($x['id']);
1338 function pumpio_getreceiver($a, $b) {
1340 $receiver = array();
1342 if (!$b["private"]) {
1344 if(! strstr($b['postopts'],'pumpio'))
1347 $public = get_pconfig($b['uid'], "pumpio", "public");
1350 $receiver["to"][] = Array(
1351 "objectType" => "collection",
1352 "id" => "http://activityschema.org/collection/public");
1354 $cids = explode("><", $b["allow_cid"]);
1355 $gids = explode("><", $b["allow_gid"]);
1357 foreach ($cids AS $cid) {
1358 $cid = trim($cid, " <>");
1360 $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",
1363 dbesc(NETWORK_PUMPIO)
1367 $receiver["bcc"][] = Array(
1368 "displayName" => $r[0]["name"],
1369 "objectType" => "person",
1370 "preferredUsername" => $r[0]["nick"],
1371 "url" => $r[0]["url"]);
1374 foreach ($gids AS $gid) {
1375 $gid = trim($gid, " <>");
1377 $r = q("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network` ".
1378 "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d AND `group_member`.`uid` = %d ".
1379 "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
1382 dbesc(NETWORK_PUMPIO)
1385 foreach ($r AS $row)
1386 $receiver["bcc"][] = Array(
1387 "displayName" => $row["name"],
1388 "objectType" => "person",
1389 "preferredUsername" => $row["nick"],
1390 "url" => $row["url"]);
1394 if ($b["inform"] != "") {
1396 $inform = explode(",", $b["inform"]);
1398 foreach ($inform AS $cid) {
1399 if (substr($cid, 0, 4) != "cid:")
1402 $cid = str_replace("cid:", "", $cid);
1404 $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",
1407 dbesc(NETWORK_PUMPIO)
1411 $receiver["to"][] = Array(
1412 "displayName" => $r[0]["name"],
1413 "objectType" => "person",
1414 "preferredUsername" => $r[0]["nick"],
1415 "url" => $r[0]["url"]);
1423 function pumpio_fetchallcomments($a, $uid, $id) {
1424 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1425 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1426 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1427 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1428 $hostname = get_pconfig($uid, 'pumpio','host');
1429 $username = get_pconfig($uid, "pumpio", "user");
1431 $own_id = "https://".$hostname."/".$username;
1433 logger("pumpio_fetchallcomments: completing comment for user ".$uid." url ".$url);
1435 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1438 // Fetching the original post - Two queries for speed issues
1439 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1445 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1455 $url = $r[0]["extid"];
1459 $client = new oauth_client_class;
1460 $client->oauth_version = '1.0a';
1461 $client->authorization_header = true;
1462 $client->url_parameters = false;
1464 $client->client_id = $ckey;
1465 $client->client_secret = $csecret;
1466 $client->access_token = $otoken;
1467 $client->access_token_secret = $osecret;
1469 logger("pumpio_fetchallcomments: fetching comment for user ".$uid." url ".$url);
1471 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $item);
1476 if ($item->replies->totalItems == 0)
1479 foreach ($item->replies->items AS $item) {
1480 if ($item->id == $id)
1483 // Checking if the comment already exists - Two queries for speed issues
1484 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1492 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1500 $post->verb = "post";
1501 $post->actor = $item->author;
1502 $post->published = $item->published;
1503 $post->received = $item->updated;
1504 $post->generator->displayName = "pumpio";
1506 unset($item->author);
1507 unset($item->published);
1508 unset($item->updated);
1510 $post->object = $item;
1512 logger("pumpio_fetchallcomments: posting comment ".$post->object->id);
1513 pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id);
1519 - refresh after post doesn't always happen