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 (function_exists("apc_delete")) {
43 $toDelete = new APCIterator('user', APC_ITER_VALUE);
44 apc_delete($toDelete);
47 if (isset($a->argv[1]))
48 switch ($a->argv[1]) {
50 $o = pumpio_connect($a);
53 $o = print_r($a->argv, true);
57 $o = pumpio_connect($a);
62 function pumpio_registerclient(&$a, $host) {
64 $url = "https://".$host."/api/client/register";
68 $application_name = get_config('pumpio', 'application_name');
70 if ($application_name == "")
71 $application_name = $a->get_hostname();
73 $params["type"] = "client_associate";
74 $params["contacts"] = $a->config['admin_email'];
75 $params["application_type"] = "native";
76 $params["application_name"] = $application_name;
77 $params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
78 $params["redirect_uris"] = $a->get_baseurl()."/pumpio/connect";
80 logger("pumpio_registerclient: ".$url." parameters ".$print_r($params, true), LOGGER_DEBUG);
82 $ch = curl_init($url);
83 curl_setopt($ch, CURLOPT_HEADER, false);
84 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
85 curl_setopt($ch, CURLOPT_POST,1);
86 curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
87 curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
90 $curl_info = curl_getinfo($ch);
92 if ($curl_info["http_code"] == "200") {
93 $values = json_decode($s);
94 logger("pumpio_registerclient: success ".$print_r($values, true), LOGGER_DEBUG);
97 logger("pumpio_registerclient: failed: ".$print_r($curl_info, true), LOGGER_DEBUG);
102 function pumpio_connect(&$a) {
103 // Start a session. This is necessary to hold on to a few keys the callback script will also need
106 // Define the needed keys
107 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
108 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
109 $hostname = get_pconfig(local_user(), 'pumpio','host');
111 if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
112 logger("pumpio_connect: register client");
113 $clientdata = pumpio_registerclient($a, $hostname);
114 set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
115 set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
117 $consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
118 $consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
120 logger("pumpio_connect: ckey: ".$consumer_key." csecrect: ".$consumer_secret, LOGGER_DEBUG);
123 if (($consumer_key == "") OR ($consumer_secret == "")) {
124 logger("pumpio_connect: ".sprintf("Unable to register the client at the pump.io server '%s'.", $hostname));
126 $o .= sprintf(t("Unable to register the client at the pump.io server '%s'."), $hostname);
130 // The callback URL is the script that gets called after the user authenticates with pumpio
131 $callback_url = $a->get_baseurl()."/pumpio/connect";
133 // Let's begin. First we need a Request Token. The request token is required to send the user
134 // to pumpio's login page.
136 // Create a new instance of the TumblrOAuth library. For this step, all we need to give the library is our
137 // Consumer Key and Consumer Secret
138 $client = new oauth_client_class;
140 $client->server = '';
141 $client->oauth_version = '1.0a';
142 $client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
143 $client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
144 $client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
145 $client->url_parameters = false;
146 $client->authorization_header = true;
147 $client->redirect_uri = $callback_url;
148 $client->client_id = $consumer_key;
149 $client->client_secret = $consumer_secret;
151 if (($success = $client->Initialize())) {
152 if (($success = $client->Process())) {
153 if (strlen($client->access_token)) {
154 logger("pumpio_connect: otoken: ".$client->access_token." osecrect: ".$client->access_token_secret, LOGGER_DEBUG);
155 set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
156 set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
159 $success = $client->Finalize($success);
162 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
165 logger("pumpio_connect: authenticated");
166 $o .= t("You are now authenticated to pumpio.");
167 $o .= '<br /><a href="'.$a->get_baseurl().'/settings/connectors">'.t("return to the connector page").'</a>';
169 logger("pumpio_connect: could not connect");
170 $o = 'Could not connect to pumpio. Refresh the page or try again later.';
176 function pumpio_jot_nets(&$a,&$b) {
180 $pumpio_post = get_pconfig(local_user(),'pumpio','post');
181 if(intval($pumpio_post) == 1) {
182 $pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
183 $selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
184 $b .= '<div class="profile-jot-net"><input type="checkbox" name="pumpio_enable"' . $selected . ' value="1" /> '
185 . t('Post to pumpio') . '</div>';
190 function pumpio_settings(&$a,&$s) {
195 /* Add our stylesheet to the page so we can make our settings look nice */
197 $a->page['htmlhead'] .= '<link rel="stylesheet" type="text/css" href="' . $a->get_baseurl() . '/addon/pumpio/pumpio.css' . '" media="all" />' . "\r\n";
199 /* Get the current state of our config variables */
201 $import_enabled = get_pconfig(local_user(),'pumpio','import');
202 $import_checked = (($import_enabled) ? ' checked="checked" ' : '');
204 $enabled = get_pconfig(local_user(),'pumpio','post');
205 $checked = (($enabled) ? ' checked="checked" ' : '');
207 $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
208 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
210 $public_enabled = get_pconfig(local_user(),'pumpio','public');
211 $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
213 $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
214 $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
216 $servername = get_pconfig(local_user(), "pumpio", "host");
217 $username = get_pconfig(local_user(), "pumpio", "user");
219 /* Add some HTML to the existing form */
221 $s .= '<div class="settings-block">';
222 $s .= '<h3>' . t('Pump.io Post Settings') . '</h3>';
224 $s .= '<div id="pumpio-username-wrapper">';
225 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username (without the servername)').'</label>';
226 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
227 $s .= '</div><div class="clear"></div>';
229 $s .= '<div id="pumpio-servername-wrapper">';
230 $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername (without "http://" or "https://" )').'</label>';
231 $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
232 $s .= '</div><div class="clear"></div>';
234 if (($username != '') AND ($servername != '')) {
236 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
237 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
239 $s .= '<div id="pumpio-password-wrapper">';
240 if (($oauth_token == "") OR ($oauth_token_secret == "")) {
241 $s .= '<div id="pumpio-authenticate-wrapper">';
242 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Authenticate your pump.io connection").'</a>';
243 $s .= '</div><div class="clear"></div>';
245 $s .= '<div id="pumpio-import-wrapper">';
246 $s .= '<label id="pumpio-import-label" for="pumpio-import">' . t('Import the remote timeline') . '</label>';
247 $s .= '<input id="pumpio-import" type="checkbox" name="pumpio_import" value="1" ' . $import_checked . '/>';
248 $s .= '</div><div class="clear"></div>';
250 $s .= '<div id="pumpio-enable-wrapper">';
251 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
252 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
253 $s .= '</div><div class="clear"></div>';
255 $s .= '<div id="pumpio-bydefault-wrapper">';
256 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
257 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
258 $s .= '</div><div class="clear"></div>';
260 $s .= '<div id="pumpio-public-wrapper">';
261 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
262 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
263 $s .= '</div><div class="clear"></div>';
265 $s .= '<div id="pumpio-mirror-wrapper">';
266 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
267 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
268 $s .= '</div><div class="clear"></div>';
270 $s .= '<div id="pumpio-delete-wrapper">';
271 $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . t('Check to delete this preset') . '</label>';
272 $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
273 $s .= '</div><div class="clear"></div>';
276 $s .= '</div><div class="clear"></div>';
279 /* provide a submit button */
281 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Submit') . '" /></div></div>';
286 function pumpio_settings_post(&$a,&$b) {
288 if(x($_POST,'pumpio-submit')) {
289 if(x($_POST,'pumpio_delete')) {
290 set_pconfig(local_user(),'pumpio','consumer_key','');
291 set_pconfig(local_user(),'pumpio','consumer_secret','');
292 set_pconfig(local_user(),'pumpio','host','');
293 set_pconfig(local_user(),'pumpio','oauth_token','');
294 set_pconfig(local_user(),'pumpio','oauth_token_secret','');
295 set_pconfig(local_user(),'pumpio','post',false);
296 set_pconfig(local_user(),'pumpio','post_by_default',false);
297 set_pconfig(local_user(),'pumpio','user','');
299 // filtering the username if it is filled wrong
300 $user = $_POST['pumpio_user'];
301 if (strstr($user, "@")) {
302 $pos = strpos($user, "@");
304 $user = substr($user, 0, $pos);
307 // Filtering the hostname if someone is entering it with "http"
308 $host = $_POST['pumpio_host'];
310 $host = str_replace(array("https://", "http://"), array("", ""), $host);
312 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
313 set_pconfig(local_user(),'pumpio','import',$_POST['pumpio_import']);
314 set_pconfig(local_user(),'pumpio','host',$host);
315 set_pconfig(local_user(),'pumpio','user',$user);
316 set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
317 set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
318 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
323 function pumpio_post_local(&$a,&$b) {
325 if((! local_user()) || (local_user() != $b['uid']))
328 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
330 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
332 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
338 if(strlen($b['postopts']))
339 $b['postopts'] .= ',';
341 $b['postopts'] .= 'pumpio';
347 function pumpio_send(&$a,&$b) {
349 if (!get_pconfig($b["uid"],'pumpio','import')) {
350 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
354 logger("pumpio_send: parameter ".print_r($b, true));
356 if($b['parent'] != $b['id']) {
357 // Looking if its a reply to a pumpio post
358 $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",
359 intval($b["parent"]),
361 dbesc(NETWORK_PUMPIO));
364 logger("pumpio_send: no pumpio post ".$b["parent"]);
373 $receiver = pumpio_getreceiver($a, $b);
375 logger("pumpio_send: receiver ".print_r($receiver, true));
377 if (!count($receiver) AND ($b['private'] OR !strstr($b['postopts'],'pumpio')))
381 if($b['verb'] == ACTIVITY_LIKE) {
383 pumpio_action($a, $b["uid"], $b["thr-parent"], "unlike");
385 pumpio_action($a, $b["uid"], $b["thr-parent"], "like");
389 if($b['verb'] == ACTIVITY_DISLIKE)
392 if (($b['verb'] == ACTIVITY_POST) AND ($b['created'] !== $b['edited']) AND !$b['deleted'])
393 pumpio_action($a, $b["uid"], $b["uri"], "update", $b["body"]);
395 if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
396 pumpio_action($a, $b["uid"], $b["uri"], "delete");
398 if($b['deleted'] || ($b['created'] !== $b['edited']))
401 // if post comes from pump.io don't send it back
402 if($b['app'] == "pump.io")
406 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
407 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
408 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
409 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
411 $host = get_pconfig($b['uid'], "pumpio", "host");
412 $user = get_pconfig($b['uid'], "pumpio", "user");
413 $public = get_pconfig($b['uid'], "pumpio", "public");
415 if($oauth_token && $oauth_token_secret) {
417 require_once('include/bbcode.php');
419 $title = trim($b['title']);
422 $title = "<h4>".$title."</h4>";
424 $content = bbcode($b['body'], false, false);
426 // Enhance the way, videos are displayed
427 $content = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
428 $content = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
429 $content = preg_replace('/<a.*?href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
430 $content = preg_replace('/<a.*?href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
432 $URLSearchString = "^\[\]";
433 $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
437 $params["verb"] = "post";
440 $params["object"] = array(
441 'objectType' => "note",
442 'content' => $title.$content);
444 if (count($receiver["to"]))
445 $params["to"] = $receiver["to"];
447 if (count($receiver["bto"]))
448 $params["bto"] = $receiver["bto"];
450 if (count($receiver["cc"]))
451 $params["cc"] = $receiver["cc"];
453 if (count($receiver["bcc"]))
454 $params["bcc"] = $receiver["bcc"];
457 $inReplyTo = array("id" => $orig_post["uri"],
458 "objectType" => "note");
460 $params["object"] = array(
461 'objectType' => "comment",
462 'content' => $title.$content,
463 'inReplyTo' => $inReplyTo);
466 $client = new oauth_client_class;
467 $client->oauth_version = '1.0a';
468 $client->url_parameters = false;
469 $client->authorization_header = true;
470 $client->access_token = $oauth_token;
471 $client->access_token_secret = $oauth_token_secret;
472 $client->client_id = $consumer_key;
473 $client->client_secret = $consumer_secret;
475 $username = $user.'@'.$host;
476 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
478 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
481 $post_id = $user->object->id;
482 logger('pumpio_send '.$username.': success '.$post_id);
483 if($post_id AND $iscomment) {
484 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
485 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
491 logger('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user,true));
493 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
495 $a->contact = $r[0]["id"];
497 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $params));
498 require_once('include/queue_fn.php');
499 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
500 notice(t('Pump.io post failed. Queued for retry.').EOL);
506 function pumpio_action(&$a, $uid, $uri, $action, $content) {
508 // Don't do likes and other stuff if you don't import the timeline
509 if (!get_pconfig($uid,'pumpio','import'))
512 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
513 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
514 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
515 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
516 $hostname = get_pconfig($uid, 'pumpio','host');
517 $username = get_pconfig($uid, "pumpio", "user");
519 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
529 if ($orig_post["extid"] AND !strstr($orig_post["extid"], "/proxy/"))
530 $uri = $orig_post["extid"];
532 $uri = $orig_post["uri"];
534 if (strstr($uri, "/api/comment/"))
535 $objectType = "comment";
536 elseif (strstr($uri, "/api/note/"))
537 $objectType = "note";
538 elseif (strstr($uri, "/api/image/"))
539 $objectType = "image";
541 $params["verb"] = $action;
542 $params["object"] = array('id' => $uri,
543 "objectType" => $objectType,
544 "content" => $content);
546 $client = new oauth_client_class;
547 $client->oauth_version = '1.0a';
548 $client->authorization_header = true;
549 $client->url_parameters = false;
551 $client->client_id = $ckey;
552 $client->client_secret = $csecret;
553 $client->access_token = $otoken;
554 $client->access_token_secret = $osecret;
556 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
558 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
561 logger('pumpio_action '.$username.' '.$action.': success '.$uri);
563 logger('pumpio_action '.$username.' '.$action.': general error: '.$uri.' '.print_r($user,true));
565 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
567 $a->contact = $r[0]["id"];
569 $s = serialize(array('url' => $url, 'item' => $orig_post["id"], 'post' => $params));
570 require_once('include/queue_fn.php');
571 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
572 notice(t('Pump.io like failed. Queued for retry.').EOL);
577 function pumpio_cron(&$a,$b) {
578 $last = get_config('pumpio','last_poll');
580 $poll_interval = intval(get_config('pumpio','poll_interval'));
582 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
585 $next = $last + ($poll_interval * 60);
587 logger('pumpio: poll intervall not reached');
591 logger('pumpio: cron_start');
593 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
596 logger('pumpio: mirroring user '.$rr['uid']);
597 pumpio_fetchtimeline($a, $rr['uid']);
601 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
604 logger('pumpio: importing timeline from user '.$rr['uid']);
605 pumpio_fetchinbox($a, $rr['uid']);
607 // check for new contacts once a day
608 $last_contact_check = get_pconfig($rr['uid'],'pumpio','contact_check');
609 if($last_contact_check)
610 $next_contact_check = $last_contact_check + 86400;
612 $next_contact_check = 0;
614 if($next_contact_check <= time()) {
615 pumpio_getallusers($a, $rr["uid"]);
616 set_pconfig($rr['uid'],'pumpio','contact_check',time());
621 logger('pumpio: cron_end');
623 set_config('pumpio','last_poll', time());
626 function pumpio_fetchtimeline(&$a, $uid) {
627 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
628 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
629 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
630 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
631 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
632 $hostname = get_pconfig($uid, 'pumpio','host');
633 $username = get_pconfig($uid, "pumpio", "user");
635 $application_name = get_config('pumpio', 'application_name');
637 if ($application_name == "")
638 $application_name = $a->get_hostname();
640 $first_time = ($lastdate == "");
642 $client = new oauth_client_class;
643 $client->oauth_version = '1.0a';
644 $client->authorization_header = true;
645 $client->url_parameters = false;
647 $client->client_id = $ckey;
648 $client->client_secret = $csecret;
649 $client->access_token = $otoken;
650 $client->access_token_secret = $osecret;
652 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
654 logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
656 $username = $user.'@'.$host;
658 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
661 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
665 $posts = array_reverse($user->items);
667 $initiallastdate = $lastdate;
671 foreach ($posts as $post) {
672 if ($post->generator->published <= $initiallastdate)
675 if ($lastdate < $post->generator->published)
676 $lastdate = $post->generator->published;
681 $receiptians = array();
682 if (@is_array($post->cc))
683 $receiptians = array_merge($receiptians, $post->cc);
685 if (@is_array($post->to))
686 $receiptians = array_merge($receiptians, $post->to);
689 foreach ($receiptians AS $receiver)
690 if (is_string($receiver->objectType))
691 if ($receiver->id == "http://activityschema.org/collection/public")
694 if ($public AND !strstr($post->generator->displayName, $application_name)) {
695 require_once('include/html2bbcode.php');
697 $_SESSION["authenticated"] = true;
698 $_SESSION["uid"] = $uid;
701 $_REQUEST["type"] = "wall";
702 $_REQUEST["api_source"] = true;
703 $_REQUEST["profile_uid"] = $uid;
704 $_REQUEST["source"] = "pump.io";
706 if ($post->object->displayName != "")
707 $_REQUEST["title"] = html2bbcode($post->object->displayName);
709 $_REQUEST["title"] = "";
711 $_REQUEST["body"] = html2bbcode($post->object->content);
713 if ($post->object->fullImage->url != "")
714 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
716 logger('pumpio: posting for user '.$uid);
718 require_once('mod/item.php');
721 logger('pumpio: posting done - user '.$uid);
727 set_pconfig($uid,'pumpio','lastdate', $lastdate);
730 function pumpio_dounlike(&$a, $uid, $self, $post, $own_id) {
731 // Searching for the unliked post
732 // Two queries for speed issues
733 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
734 dbesc($post->object->id),
741 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
742 dbesc($post->object->id),
754 if(link_compare($post->actor->url, $own_id)) {
755 $contactid = $self[0]['id'];
757 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
758 dbesc($post->actor->url),
763 $contactid = $r[0]['id'];
766 $contactid = $orig_post['contact-id'];
769 $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'",
770 dbesc(datetime_convert()),
771 dbesc(ACTIVITY_LIKE),
774 dbesc($orig_post['uri'])
778 logger("pumpio_dounlike: unliked existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
780 logger("pumpio_dounlike: not found. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
783 function pumpio_dolike(&$a, $uid, $self, $post, $own_id) {
785 // Searching for the liked post
786 // Two queries for speed issues
787 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
788 dbesc($post->object->id),
795 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
796 dbesc($post->object->id),
808 if(link_compare($post->actor->url, $own_id)) {
809 $contactid = $self[0]['id'];
810 $post->actor->displayName = $self[0]['name'];
811 $post->actor->url = $self[0]['url'];
812 $post->actor->image->url = $self[0]['photo'];
814 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
815 dbesc($post->actor->url),
820 $contactid = $r[0]['id'];
823 $contactid = $orig_post['contact-id'];
826 $r = q("SELECT parent FROM `item` WHERE `verb` = '%s' AND `uid` = %d AND `contact-id` = %d AND `thr-parent` = '%s' LIMIT 1",
827 dbesc(ACTIVITY_LIKE),
830 dbesc($orig_post['uri'])
834 logger("pumpio_dolike: found existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
839 $likedata['parent'] = $orig_post['id'];
840 $likedata['verb'] = ACTIVITY_LIKE;
841 $likedata['gravity'] = 3;
842 $likedata['uid'] = $uid;
843 $likedata['wall'] = 0;
844 $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
845 $likedata['parent-uri'] = $orig_post["uri"];
846 $likedata['contact-id'] = $contactid;
847 $likedata['app'] = $post->generator->displayName;
848 $likedata['verb'] = ACTIVITY_LIKE;
849 $likedata['author-name'] = $post->actor->displayName;
850 $likedata['author-link'] = $post->actor->url;
851 $likedata['author-avatar'] = $post->actor->image->url;
853 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
854 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
855 $post_type = t('status');
856 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
857 $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
859 $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
861 $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
862 '<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>';
864 $ret = item_store($likedata);
866 logger("pumpio_dolike: ".$ret." User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
869 function pumpio_get_contact($uid, $contact) {
871 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
872 intval($uid), dbesc($contact->url));
875 // create contact record
876 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
877 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
878 `writable`, `blocked`, `readonly`, `pending` )
879 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
881 dbesc(datetime_convert()),
882 dbesc($contact->url),
883 dbesc(normalise_link($contact->url)),
884 dbesc(str_replace("acct:", "", $contact->id)),
886 dbesc($contact->id), // What is it for?
887 dbesc('pump.io ' . $contact->id), // What is it for?
888 dbesc($contact->displayName),
889 dbesc($contact->preferredUsername),
890 dbesc($contact->image->url),
891 dbesc(NETWORK_PUMPIO),
892 intval(CONTACT_IS_FRIEND),
897 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
898 dbesc($contact->url),
905 $contact_id = $r[0]['id'];
907 $g = q("select def_gid from user where uid = %d limit 1",
911 if($g && intval($g[0]['def_gid'])) {
912 require_once('include/group.php');
913 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
916 require_once("Photo.php");
918 $photos = import_profile_photo($contact->image->url,$uid,$contact_id);
920 q("UPDATE `contact` SET `photo` = '%s',
926 WHERE `id` = %d LIMIT 1
931 dbesc(datetime_convert()),
932 dbesc(datetime_convert()),
933 dbesc(datetime_convert()),
937 // update profile photos once every two weeks as we have no notification of when they change.
939 $update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
941 // check that we have all the photos, this has been known to fail on occasion
943 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
944 require_once("Photo.php");
946 $photos = import_profile_photo($contact->image->url, $uid, $r[0]['id']);
948 q("UPDATE `contact` SET `photo` = '%s',
954 WHERE `id` = %d LIMIT 1
959 dbesc(datetime_convert()),
960 dbesc(datetime_convert()),
961 dbesc(datetime_convert()),
971 function pumpio_dodelete(&$a, $uid, $self, $post, $own_id) {
973 // Two queries for speed issues
974 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
975 dbesc($post->object->id),
980 return drop_item($r[0]["id"], $false);
982 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
983 dbesc($post->object->id),
988 return drop_item($r[0]["id"], $false);
991 function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcompletion = false) {
992 require_once('include/items.php');
994 if (($post->verb == "like") OR ($post->verb == "favorite"))
995 return pumpio_dolike($a, $uid, $self, $post, $own_id);
997 if (($post->verb == "unlike") OR ($post->verb == "unfavorite"))
998 return pumpio_dounlike($a, $uid, $self, $post, $own_id);
1000 if ($post->verb == "delete")
1001 return pumpio_dodelete($a, $uid, $self, $post, $own_id);
1003 if ($post->verb != "update") {
1004 // Two queries for speed issues
1005 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1006 dbesc($post->object->id),
1013 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1014 dbesc($post->object->id),
1022 // Only handle these three types
1023 if (!strstr("post|share|update", $post->verb))
1026 $receiptians = array();
1027 if (@is_array($post->cc))
1028 $receiptians = array_merge($receiptians, $post->cc);
1030 if (@is_array($post->to))
1031 $receiptians = array_merge($receiptians, $post->to);
1033 foreach ($receiptians AS $receiver)
1034 if (is_string($receiver->objectType))
1035 if ($receiver->id == "http://activityschema.org/collection/public")
1038 $postarray = array();
1039 $postarray['gravity'] = 0;
1040 $postarray['uid'] = $uid;
1041 $postarray['wall'] = 0;
1042 $postarray['uri'] = $post->object->id;
1044 if ($post->object->objectType != "comment") {
1045 $contact_id = pumpio_get_contact($uid, $post->actor);
1048 $contact_id = $self[0]['id'];
1050 $postarray['parent-uri'] = $post->object->id;
1054 if(link_compare($post->actor->url, $own_id)) {
1055 $contact_id = $self[0]['id'];
1056 $post->actor->displayName = $self[0]['name'];
1057 $post->actor->url = $self[0]['url'];
1058 $post->actor->image->url = $self[0]['photo'];
1060 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1061 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1062 dbesc($post->actor->url),
1067 $contact_id = $r[0]['id'];
1069 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1070 dbesc($post->actor->url),
1075 $contact_id = $r[0]['id'];
1077 $contact_id = $self[0]['id'];
1081 $reply->verb = "note";
1082 $reply->cc = $post->cc;
1083 $reply->to = $post->to;
1084 $reply->object->objectType = $post->object->inReplyTo->objectType;
1085 $reply->object->content = $post->object->inReplyTo->content;
1086 $reply->object->id = $post->object->inReplyTo->id;
1087 $reply->actor = $post->object->inReplyTo->author;
1088 $reply->url = $post->object->inReplyTo->url;
1089 $reply->generator->displayName = "pumpio";
1090 $reply->published = $post->object->inReplyTo->published;
1091 $reply->received = $post->object->inReplyTo->updated;
1092 $reply->url = $post->object->inReplyTo->url;
1093 pumpio_dopost($a, $client, $uid, $self, $reply, $own_id);
1095 $postarray['parent-uri'] = $post->object->inReplyTo->id;
1098 if ($post->object->pump_io->proxyURL)
1099 $postarray['extid'] = $post->object->pump_io->proxyURL;
1101 $postarray['contact-id'] = $contact_id;
1102 $postarray['verb'] = ACTIVITY_POST;
1103 $postarray['owner-name'] = $post->actor->displayName;
1104 $postarray['owner-link'] = $post->actor->url;
1105 $postarray['owner-avatar'] = $post->actor->image->url;
1106 $postarray['author-name'] = $post->actor->displayName;
1107 $postarray['author-link'] = $post->actor->url;
1108 $postarray['author-avatar'] = $post->actor->image->url;
1109 $postarray['plink'] = $post->object->url;
1110 $postarray['app'] = $post->generator->displayName;
1111 $postarray['body'] = html2bbcode($post->object->content);
1113 if ($post->object->fullImage->url != "")
1114 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
1116 if ($post->object->displayName != "")
1117 $postarray['title'] = $post->object->displayName;
1119 $postarray['created'] = datetime_convert('UTC','UTC',$post->published);
1120 $postarray['edited'] = datetime_convert('UTC','UTC',$post->received);
1122 $postarray['private'] = 1;
1123 $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
1126 if ($post->verb == "share") {
1127 $postarray['body'] = "[share author='".$post->object->author->displayName.
1128 "' profile='".$post->object->author->url.
1129 "' avatar='".$post->object->author->image->url.
1130 "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
1133 if (trim($postarray['body']) == "")
1136 $top_item = item_store($postarray);
1138 if (($top_item == 0) AND ($post->verb == "update")) {
1139 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
1140 dbesc($postarray["title"]),
1141 dbesc($postarray["body"]),
1142 dbesc($postarray["edited"]),
1143 dbesc($postarray["uri"]),
1148 if ($post->object->objectType == "comment") {
1150 if ($threadcompletion)
1151 pumpio_fetchallcomments($a, $uid, $postarray['parent-uri']);
1153 $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
1160 $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
1162 if (link_compare($own_id, $postarray['author-link']))
1165 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
1166 dbesc($postarray['parent-uri']),
1170 if(count($myconv)) {
1172 foreach($myconv as $conv) {
1173 // now if we find a match, it means we're in this conversation
1175 if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$own_id))
1178 require_once('include/enotify.php');
1180 $conv_parent = $conv['parent'];
1183 'type' => NOTIFY_COMMENT,
1184 'notify_flags' => $user[0]['notify-flags'],
1185 'language' => $user[0]['language'],
1186 'to_name' => $user[0]['username'],
1187 'to_email' => $user[0]['email'],
1188 'uid' => $user[0]['uid'],
1189 'item' => $postarray,
1190 'link' => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $top_item,
1191 'source_name' => $postarray['author-name'],
1192 'source_link' => $postarray['author-link'],
1193 'source_photo' => $postarray['author-avatar'],
1194 'verb' => ACTIVITY_POST,
1196 'parent' => $conv_parent,
1199 // only send one notification
1208 function pumpio_fetchinbox(&$a, $uid) {
1210 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1211 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1212 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1213 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1214 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
1215 $hostname = get_pconfig($uid, 'pumpio','host');
1216 $username = get_pconfig($uid, "pumpio", "user");
1218 $own_id = "https://".$hostname."/".$username;
1220 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1223 $client = new oauth_client_class;
1224 $client->oauth_version = '1.0a';
1225 $client->authorization_header = true;
1226 $client->url_parameters = false;
1228 $client->client_id = $ckey;
1229 $client->client_secret = $csecret;
1230 $client->access_token = $otoken;
1231 $client->access_token_secret = $osecret;
1233 $last_id = get_pconfig($uid,'pumpio','last_id');
1235 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1238 $url .= '?since='.urlencode($last_id);
1240 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
1241 $posts = array_reverse($user->items);
1244 foreach ($posts as $post) {
1245 $last_id = $post->id;
1246 pumpio_dopost($a, $client, $uid, $self, $post, $own_id);
1249 set_pconfig($uid,'pumpio','last_id', $last_id);
1252 function pumpio_getallusers(&$a, $uid) {
1253 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1254 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1255 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1256 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1257 $hostname = get_pconfig($uid, 'pumpio','host');
1258 $username = get_pconfig($uid, "pumpio", "user");
1260 $client = new oauth_client_class;
1261 $client->oauth_version = '1.0a';
1262 $client->authorization_header = true;
1263 $client->url_parameters = false;
1265 $client->client_id = $ckey;
1266 $client->client_secret = $csecret;
1267 $client->access_token = $otoken;
1268 $client->access_token_secret = $osecret;
1270 $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1272 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1274 if ($users->totalItems > count($users->items)) {
1275 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1277 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1280 foreach ($users->items AS $user)
1281 echo pumpio_get_contact($uid, $user)."\n";
1284 function pumpio_queue_hook(&$a,&$b) {
1286 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
1287 dbesc(NETWORK_PUMPIO)
1292 require_once('include/queue_fn.php');
1294 foreach($qi as $x) {
1295 if($x['network'] !== NETWORK_PUMPIO)
1298 logger('pumpio_queue: run');
1300 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
1301 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
1309 //logger('pumpio_queue: fetching userdata '.print_r($userdata, true));
1311 $oauth_token = get_pconfig($userdata['uid'], "pumpio", "oauth_token");
1312 $oauth_token_secret = get_pconfig($userdata['uid'], "pumpio", "oauth_token_secret");
1313 $consumer_key = get_pconfig($userdata['uid'], "pumpio","consumer_key");
1314 $consumer_secret = get_pconfig($userdata['uid'], "pumpio","consumer_secret");
1316 $host = get_pconfig($userdata['uid'], "pumpio", "host");
1317 $user = get_pconfig($userdata['uid'], "pumpio", "user");
1321 if ($oauth_token AND $oauth_token_secret AND
1322 $consumer_key AND $consumer_secret) {
1323 $username = $user.'@'.$host;
1325 logger('pumpio_queue: able to post for user '.$username);
1327 $z = unserialize($x['content']);
1329 $client = new oauth_client_class;
1330 $client->oauth_version = '1.0a';
1331 $client->url_parameters = false;
1332 $client->authorization_header = true;
1333 $client->access_token = $oauth_token;
1334 $client->access_token_secret = $oauth_token_secret;
1335 $client->client_id = $consumer_key;
1336 $client->client_secret = $consumer_secret;
1338 $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
1341 $post_id = $user->object->id;
1342 logger('pumpio_queue: send '.$username.': success '.$post_id);
1343 if($post_id AND $iscomment) {
1344 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$z['item']);
1345 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
1350 remove_queue_item($x['id']);
1352 logger('pumpio_queue: send '.$username.': '.$url.' general error: ' . print_r($user,true));
1354 logger("pumpio_queue: Error getting tokens for user ".$userdata['uid']);
1357 logger('pumpio_queue: delayed');
1358 update_queue_time($x['id']);
1363 function pumpio_getreceiver(&$a, $b) {
1365 $receiver = array();
1367 if (!$b["private"]) {
1369 if(! strstr($b['postopts'],'pumpio'))
1372 $public = get_pconfig($b['uid'], "pumpio", "public");
1375 $receiver["to"][] = Array(
1376 "objectType" => "collection",
1377 "id" => "http://activityschema.org/collection/public");
1379 $cids = explode("><", $b["allow_cid"]);
1380 $gids = explode("><", $b["allow_gid"]);
1382 foreach ($cids AS $cid) {
1383 $cid = trim($cid, " <>");
1385 $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",
1388 dbesc(NETWORK_PUMPIO)
1392 $receiver["bcc"][] = Array(
1393 "displayName" => $r[0]["name"],
1394 "objectType" => "person",
1395 "preferredUsername" => $r[0]["nick"],
1396 "url" => $r[0]["url"]);
1399 foreach ($gids AS $gid) {
1400 $gid = trim($gid, " <>");
1402 $r = q("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network` ".
1403 "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d AND `group_member`.`uid` = %d ".
1404 "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
1407 dbesc(NETWORK_PUMPIO)
1410 foreach ($r AS $row)
1411 $receiver["bcc"][] = Array(
1412 "displayName" => $row["name"],
1413 "objectType" => "person",
1414 "preferredUsername" => $row["nick"],
1415 "url" => $row["url"]);
1419 if ($b["inform"] != "") {
1421 $inform = explode(",", $b["inform"]);
1423 foreach ($inform AS $cid) {
1424 if (substr($cid, 0, 4) != "cid:")
1427 $cid = str_replace("cid:", "", $cid);
1429 $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",
1432 dbesc(NETWORK_PUMPIO)
1436 $receiver["to"][] = Array(
1437 "displayName" => $r[0]["name"],
1438 "objectType" => "person",
1439 "preferredUsername" => $r[0]["nick"],
1440 "url" => $r[0]["url"]);
1448 function pumpio_fetchallcomments(&$a, $uid, $id) {
1449 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1450 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1451 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1452 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1453 $hostname = get_pconfig($uid, 'pumpio','host');
1454 $username = get_pconfig($uid, "pumpio", "user");
1456 $own_id = "https://".$hostname."/".$username;
1458 logger("pumpio_fetchallcomments: completing comment for user ".$uid." url ".$url);
1460 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1463 // Fetching the original post - Two queries for speed issues
1464 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1470 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1480 $url = $r[0]["extid"];
1484 $client = new oauth_client_class;
1485 $client->oauth_version = '1.0a';
1486 $client->authorization_header = true;
1487 $client->url_parameters = false;
1489 $client->client_id = $ckey;
1490 $client->client_secret = $csecret;
1491 $client->access_token = $otoken;
1492 $client->access_token_secret = $osecret;
1494 logger("pumpio_fetchallcomments: fetching comment for user ".$uid." url ".$url);
1496 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $item);
1501 if ($item->replies->totalItems == 0)
1504 foreach ($item->replies->items AS $item) {
1505 if ($item->id == $id)
1508 // Checking if the comment already exists - Two queries for speed issues
1509 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1517 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1525 $post->verb = "post";
1526 $post->actor = $item->author;
1527 $post->published = $item->published;
1528 $post->received = $item->updated;
1529 $post->generator->displayName = "pumpio";
1531 unset($item->author);
1532 unset($item->published);
1533 unset($item->updated);
1535 $post->object = $item;
1537 logger("pumpio_fetchallcomments: posting comment ".$post->object->id);
1538 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, false);
1544 - refresh after post doesn't always happen