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']));
320 header("Location: ".$a->get_baseurl()."/pumpio/connect");
325 function pumpio_post_local(&$a,&$b) {
327 if((! local_user()) || (local_user() != $b['uid']))
330 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
332 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
334 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
340 if(strlen($b['postopts']))
341 $b['postopts'] .= ',';
343 $b['postopts'] .= 'pumpio';
349 function pumpio_send(&$a,&$b) {
351 if (!get_pconfig($b["uid"],'pumpio','import')) {
352 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
356 logger("pumpio_send: parameter ".print_r($b, true), LOGGER_DATA);
358 if($b['parent'] != $b['id']) {
359 // Looking if its a reply to a pumpio post
360 $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",
361 intval($b["parent"]),
363 dbesc(NETWORK_PUMPIO));
366 logger("pumpio_send: no pumpio post ".$b["parent"]);
375 $receiver = pumpio_getreceiver($a, $b);
377 logger("pumpio_send: receiver ".print_r($receiver, true));
379 if (!count($receiver) AND ($b['private'] OR !strstr($b['postopts'],'pumpio')))
383 if($b['verb'] == ACTIVITY_LIKE) {
385 pumpio_action($a, $b["uid"], $b["thr-parent"], "unlike");
387 pumpio_action($a, $b["uid"], $b["thr-parent"], "like");
391 if($b['verb'] == ACTIVITY_DISLIKE)
394 if (($b['verb'] == ACTIVITY_POST) AND ($b['created'] !== $b['edited']) AND !$b['deleted'])
395 pumpio_action($a, $b["uid"], $b["uri"], "update", $b["body"]);
397 if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
398 pumpio_action($a, $b["uid"], $b["uri"], "delete");
400 if($b['deleted'] || ($b['created'] !== $b['edited']))
403 // if post comes from pump.io don't send it back
404 if($b['app'] == "pump.io")
408 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
409 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
410 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
411 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
413 $host = get_pconfig($b['uid'], "pumpio", "host");
414 $user = get_pconfig($b['uid'], "pumpio", "user");
415 $public = get_pconfig($b['uid'], "pumpio", "public");
417 if($oauth_token && $oauth_token_secret) {
419 require_once('include/bbcode.php');
421 $title = trim($b['title']);
424 $title = "<h4>".$title."</h4>";
426 $content = bbcode($b['body'], false, false);
428 // Enhance the way, videos are displayed
429 $content = preg_replace('/<a.*?href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
430 $content = preg_replace('/<a.*?href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
431 $content = preg_replace('/<a.*?href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
432 $content = preg_replace('/<a.*?href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
434 $URLSearchString = "^\[\]";
435 $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
439 $params["verb"] = "post";
442 $params["object"] = array(
443 'objectType' => "note",
444 'content' => $title.$content);
446 if (count($receiver["to"]))
447 $params["to"] = $receiver["to"];
449 if (count($receiver["bto"]))
450 $params["bto"] = $receiver["bto"];
452 if (count($receiver["cc"]))
453 $params["cc"] = $receiver["cc"];
455 if (count($receiver["bcc"]))
456 $params["bcc"] = $receiver["bcc"];
459 $inReplyTo = array("id" => $orig_post["uri"],
460 "objectType" => "note");
462 $params["object"] = array(
463 'objectType' => "comment",
464 'content' => $title.$content,
465 'inReplyTo' => $inReplyTo);
468 $client = new oauth_client_class;
469 $client->oauth_version = '1.0a';
470 $client->url_parameters = false;
471 $client->authorization_header = true;
472 $client->access_token = $oauth_token;
473 $client->access_token_secret = $oauth_token_secret;
474 $client->client_id = $consumer_key;
475 $client->client_secret = $consumer_secret;
477 $username = $user.'@'.$host;
478 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
480 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
483 $post_id = $user->object->id;
484 logger('pumpio_send '.$username.': success '.$post_id);
485 if($post_id AND $iscomment) {
486 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
487 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
493 logger('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user,true));
495 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
497 $a->contact = $r[0]["id"];
499 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $params));
500 require_once('include/queue_fn.php');
501 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
502 notice(t('Pump.io post failed. Queued for retry.').EOL);
508 function pumpio_action(&$a, $uid, $uri, $action, $content) {
510 // Don't do likes and other stuff if you don't import the timeline
511 if (!get_pconfig($uid,'pumpio','import'))
514 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
515 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
516 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
517 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
518 $hostname = get_pconfig($uid, 'pumpio','host');
519 $username = get_pconfig($uid, "pumpio", "user");
521 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
531 if ($orig_post["extid"] AND !strstr($orig_post["extid"], "/proxy/"))
532 $uri = $orig_post["extid"];
534 $uri = $orig_post["uri"];
536 if (strstr($uri, "/api/comment/"))
537 $objectType = "comment";
538 elseif (strstr($uri, "/api/note/"))
539 $objectType = "note";
540 elseif (strstr($uri, "/api/image/"))
541 $objectType = "image";
543 $params["verb"] = $action;
544 $params["object"] = array('id' => $uri,
545 "objectType" => $objectType,
546 "content" => $content);
548 $client = new oauth_client_class;
549 $client->oauth_version = '1.0a';
550 $client->authorization_header = true;
551 $client->url_parameters = false;
553 $client->client_id = $ckey;
554 $client->client_secret = $csecret;
555 $client->access_token = $otoken;
556 $client->access_token_secret = $osecret;
558 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
560 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
563 logger('pumpio_action '.$username.' '.$action.': success '.$uri);
565 logger('pumpio_action '.$username.' '.$action.': general error: '.$uri.' '.print_r($user,true));
567 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
569 $a->contact = $r[0]["id"];
571 $s = serialize(array('url' => $url, 'item' => $orig_post["id"], 'post' => $params));
572 require_once('include/queue_fn.php');
573 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
574 notice(t('Pump.io like failed. Queued for retry.').EOL);
579 function pumpio_cron(&$a,$b) {
580 $last = get_config('pumpio','last_poll');
582 $poll_interval = intval(get_config('pumpio','poll_interval'));
584 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
587 $next = $last + ($poll_interval * 60);
589 logger('pumpio: poll intervall not reached');
593 logger('pumpio: cron_start');
595 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
598 logger('pumpio: mirroring user '.$rr['uid']);
599 pumpio_fetchtimeline($a, $rr['uid']);
603 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
606 logger('pumpio: importing timeline from user '.$rr['uid']);
607 pumpio_fetchinbox($a, $rr['uid']);
609 // check for new contacts once a day
610 $last_contact_check = get_pconfig($rr['uid'],'pumpio','contact_check');
611 if($last_contact_check)
612 $next_contact_check = $last_contact_check + 86400;
614 $next_contact_check = 0;
616 if($next_contact_check <= time()) {
617 pumpio_getallusers($a, $rr["uid"]);
618 set_pconfig($rr['uid'],'pumpio','contact_check',time());
623 logger('pumpio: cron_end');
625 set_config('pumpio','last_poll', time());
628 function pumpio_fetchtimeline(&$a, $uid) {
629 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
630 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
631 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
632 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
633 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
634 $hostname = get_pconfig($uid, 'pumpio','host');
635 $username = get_pconfig($uid, "pumpio", "user");
637 $application_name = get_config('pumpio', 'application_name');
639 if ($application_name == "")
640 $application_name = $a->get_hostname();
642 $first_time = ($lastdate == "");
644 $client = new oauth_client_class;
645 $client->oauth_version = '1.0a';
646 $client->authorization_header = true;
647 $client->url_parameters = false;
649 $client->client_id = $ckey;
650 $client->client_secret = $csecret;
651 $client->access_token = $otoken;
652 $client->access_token_secret = $osecret;
654 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
656 logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
658 $username = $user.'@'.$host;
660 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
663 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
667 $posts = array_reverse($user->items);
669 $initiallastdate = $lastdate;
673 foreach ($posts as $post) {
674 if ($post->generator->published <= $initiallastdate)
677 if ($lastdate < $post->generator->published)
678 $lastdate = $post->generator->published;
683 $receiptians = array();
684 if (@is_array($post->cc))
685 $receiptians = array_merge($receiptians, $post->cc);
687 if (@is_array($post->to))
688 $receiptians = array_merge($receiptians, $post->to);
691 foreach ($receiptians AS $receiver)
692 if (is_string($receiver->objectType))
693 if ($receiver->id == "http://activityschema.org/collection/public")
696 if ($public AND !strstr($post->generator->displayName, $application_name)) {
697 require_once('include/html2bbcode.php');
699 $_SESSION["authenticated"] = true;
700 $_SESSION["uid"] = $uid;
703 $_REQUEST["type"] = "wall";
704 $_REQUEST["api_source"] = true;
705 $_REQUEST["profile_uid"] = $uid;
706 $_REQUEST["source"] = "pump.io";
708 if ($post->object->displayName != "")
709 $_REQUEST["title"] = html2bbcode($post->object->displayName);
711 $_REQUEST["title"] = "";
713 $_REQUEST["body"] = html2bbcode($post->object->content);
715 if ($post->object->fullImage->url != "")
716 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
718 logger('pumpio: posting for user '.$uid);
720 require_once('mod/item.php');
723 logger('pumpio: posting done - user '.$uid);
729 set_pconfig($uid,'pumpio','lastdate', $lastdate);
732 function pumpio_dounlike(&$a, $uid, $self, $post, $own_id) {
733 // Searching for the unliked post
734 // Two queries for speed issues
735 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
736 dbesc($post->object->id),
743 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
744 dbesc($post->object->id),
756 if(link_compare($post->actor->url, $own_id)) {
757 $contactid = $self[0]['id'];
759 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
760 dbesc($post->actor->url),
765 $contactid = $r[0]['id'];
768 $contactid = $orig_post['contact-id'];
771 $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'",
772 dbesc(datetime_convert()),
773 dbesc(ACTIVITY_LIKE),
776 dbesc($orig_post['uri'])
780 logger("pumpio_dounlike: unliked existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
782 logger("pumpio_dounlike: not found. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
785 function pumpio_dolike(&$a, $uid, $self, $post, $own_id) {
787 // Searching for the liked post
788 // Two queries for speed issues
789 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
790 dbesc($post->object->id),
797 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
798 dbesc($post->object->id),
810 if(link_compare($post->actor->url, $own_id)) {
811 $contactid = $self[0]['id'];
812 $post->actor->displayName = $self[0]['name'];
813 $post->actor->url = $self[0]['url'];
814 $post->actor->image->url = $self[0]['photo'];
816 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
817 dbesc($post->actor->url),
822 $contactid = $r[0]['id'];
825 $contactid = $orig_post['contact-id'];
828 $r = q("SELECT parent FROM `item` WHERE `verb` = '%s' AND `uid` = %d AND `contact-id` = %d AND `thr-parent` = '%s' LIMIT 1",
829 dbesc(ACTIVITY_LIKE),
832 dbesc($orig_post['uri'])
836 logger("pumpio_dolike: found existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
841 $likedata['parent'] = $orig_post['id'];
842 $likedata['verb'] = ACTIVITY_LIKE;
843 $likedata['gravity'] = 3;
844 $likedata['uid'] = $uid;
845 $likedata['wall'] = 0;
846 $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
847 $likedata['parent-uri'] = $orig_post["uri"];
848 $likedata['contact-id'] = $contactid;
849 $likedata['app'] = $post->generator->displayName;
850 $likedata['verb'] = ACTIVITY_LIKE;
851 $likedata['author-name'] = $post->actor->displayName;
852 $likedata['author-link'] = $post->actor->url;
853 $likedata['author-avatar'] = $post->actor->image->url;
855 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
856 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
857 $post_type = t('status');
858 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
859 $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
861 $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
863 $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
864 '<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>';
866 $ret = item_store($likedata);
868 logger("pumpio_dolike: ".$ret." User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
871 function pumpio_get_contact($uid, $contact) {
873 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
874 intval($uid), dbesc($contact->url));
877 // create contact record
878 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
879 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
880 `writable`, `blocked`, `readonly`, `pending` )
881 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
883 dbesc(datetime_convert()),
884 dbesc($contact->url),
885 dbesc(normalise_link($contact->url)),
886 dbesc(str_replace("acct:", "", $contact->id)),
888 dbesc($contact->id), // What is it for?
889 dbesc('pump.io ' . $contact->id), // What is it for?
890 dbesc($contact->displayName),
891 dbesc($contact->preferredUsername),
892 dbesc($contact->image->url),
893 dbesc(NETWORK_PUMPIO),
894 intval(CONTACT_IS_FRIEND),
899 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
900 dbesc($contact->url),
907 $contact_id = $r[0]['id'];
909 $g = q("select def_gid from user where uid = %d limit 1",
913 if($g && intval($g[0]['def_gid'])) {
914 require_once('include/group.php');
915 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
918 require_once("Photo.php");
920 $photos = import_profile_photo($contact->image->url,$uid,$contact_id);
922 q("UPDATE `contact` SET `photo` = '%s',
928 WHERE `id` = %d LIMIT 1
933 dbesc(datetime_convert()),
934 dbesc(datetime_convert()),
935 dbesc(datetime_convert()),
939 // update profile photos once every two weeks as we have no notification of when they change.
941 $update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
943 // check that we have all the photos, this has been known to fail on occasion
945 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
946 require_once("Photo.php");
948 $photos = import_profile_photo($contact->image->url, $uid, $r[0]['id']);
950 q("UPDATE `contact` SET `photo` = '%s',
956 WHERE `id` = %d LIMIT 1
961 dbesc(datetime_convert()),
962 dbesc(datetime_convert()),
963 dbesc(datetime_convert()),
973 function pumpio_dodelete(&$a, $uid, $self, $post, $own_id) {
975 // Two queries for speed issues
976 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
977 dbesc($post->object->id),
982 return drop_item($r[0]["id"], $false);
984 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
985 dbesc($post->object->id),
990 return drop_item($r[0]["id"], $false);
993 function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcompletion = false) {
994 require_once('include/items.php');
996 if (($post->verb == "like") OR ($post->verb == "favorite"))
997 return pumpio_dolike($a, $uid, $self, $post, $own_id);
999 if (($post->verb == "unlike") OR ($post->verb == "unfavorite"))
1000 return pumpio_dounlike($a, $uid, $self, $post, $own_id);
1002 if ($post->verb == "delete")
1003 return pumpio_dodelete($a, $uid, $self, $post, $own_id);
1005 if ($post->verb != "update") {
1006 // Two queries for speed issues
1007 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1008 dbesc($post->object->id),
1015 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1016 dbesc($post->object->id),
1024 // Only handle these three types
1025 if (!strstr("post|share|update", $post->verb))
1028 $receiptians = array();
1029 if (@is_array($post->cc))
1030 $receiptians = array_merge($receiptians, $post->cc);
1032 if (@is_array($post->to))
1033 $receiptians = array_merge($receiptians, $post->to);
1035 foreach ($receiptians AS $receiver)
1036 if (is_string($receiver->objectType))
1037 if ($receiver->id == "http://activityschema.org/collection/public")
1040 $postarray = array();
1041 $postarray['gravity'] = 0;
1042 $postarray['uid'] = $uid;
1043 $postarray['wall'] = 0;
1044 $postarray['uri'] = $post->object->id;
1046 if ($post->object->objectType != "comment") {
1047 $contact_id = pumpio_get_contact($uid, $post->actor);
1050 $contact_id = $self[0]['id'];
1052 $postarray['parent-uri'] = $post->object->id;
1056 if(link_compare($post->actor->url, $own_id)) {
1057 $contact_id = $self[0]['id'];
1058 $post->actor->displayName = $self[0]['name'];
1059 $post->actor->url = $self[0]['url'];
1060 $post->actor->image->url = $self[0]['photo'];
1062 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1063 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1064 dbesc($post->actor->url),
1069 $contact_id = $r[0]['id'];
1071 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1072 dbesc($post->actor->url),
1077 $contact_id = $r[0]['id'];
1079 $contact_id = $self[0]['id'];
1083 $reply->verb = "note";
1084 $reply->cc = $post->cc;
1085 $reply->to = $post->to;
1086 $reply->object->objectType = $post->object->inReplyTo->objectType;
1087 $reply->object->content = $post->object->inReplyTo->content;
1088 $reply->object->id = $post->object->inReplyTo->id;
1089 $reply->actor = $post->object->inReplyTo->author;
1090 $reply->url = $post->object->inReplyTo->url;
1091 $reply->generator->displayName = "pumpio";
1092 $reply->published = $post->object->inReplyTo->published;
1093 $reply->received = $post->object->inReplyTo->updated;
1094 $reply->url = $post->object->inReplyTo->url;
1095 pumpio_dopost($a, $client, $uid, $self, $reply, $own_id);
1097 $postarray['parent-uri'] = $post->object->inReplyTo->id;
1100 if ($post->object->pump_io->proxyURL)
1101 $postarray['extid'] = $post->object->pump_io->proxyURL;
1103 $postarray['contact-id'] = $contact_id;
1104 $postarray['verb'] = ACTIVITY_POST;
1105 $postarray['owner-name'] = $post->actor->displayName;
1106 $postarray['owner-link'] = $post->actor->url;
1107 $postarray['owner-avatar'] = $post->actor->image->url;
1108 $postarray['author-name'] = $post->actor->displayName;
1109 $postarray['author-link'] = $post->actor->url;
1110 $postarray['author-avatar'] = $post->actor->image->url;
1111 $postarray['plink'] = $post->object->url;
1112 $postarray['app'] = $post->generator->displayName;
1113 $postarray['body'] = html2bbcode($post->object->content);
1115 if ($post->object->fullImage->url != "")
1116 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
1118 if ($post->object->displayName != "")
1119 $postarray['title'] = $post->object->displayName;
1121 $postarray['created'] = datetime_convert('UTC','UTC',$post->published);
1122 $postarray['edited'] = datetime_convert('UTC','UTC',$post->received);
1124 $postarray['private'] = 1;
1125 $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
1128 if ($post->verb == "share") {
1129 $postarray['body'] = "[share author='".$post->object->author->displayName.
1130 "' profile='".$post->object->author->url.
1131 "' avatar='".$post->object->author->image->url.
1132 "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
1135 if (trim($postarray['body']) == "")
1138 $top_item = item_store($postarray);
1140 if (($top_item == 0) AND ($post->verb == "update")) {
1141 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
1142 dbesc($postarray["title"]),
1143 dbesc($postarray["body"]),
1144 dbesc($postarray["edited"]),
1145 dbesc($postarray["uri"]),
1150 if ($post->object->objectType == "comment") {
1152 if ($threadcompletion)
1153 pumpio_fetchallcomments($a, $uid, $postarray['parent-uri']);
1155 $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
1162 $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
1164 if (link_compare($own_id, $postarray['author-link']))
1167 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
1168 dbesc($postarray['parent-uri']),
1172 if(count($myconv)) {
1174 foreach($myconv as $conv) {
1175 // now if we find a match, it means we're in this conversation
1177 if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$own_id))
1180 require_once('include/enotify.php');
1182 $conv_parent = $conv['parent'];
1185 'type' => NOTIFY_COMMENT,
1186 'notify_flags' => $user[0]['notify-flags'],
1187 'language' => $user[0]['language'],
1188 'to_name' => $user[0]['username'],
1189 'to_email' => $user[0]['email'],
1190 'uid' => $user[0]['uid'],
1191 'item' => $postarray,
1192 'link' => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $top_item,
1193 'source_name' => $postarray['author-name'],
1194 'source_link' => $postarray['author-link'],
1195 'source_photo' => $postarray['author-avatar'],
1196 'verb' => ACTIVITY_POST,
1198 'parent' => $conv_parent,
1201 // only send one notification
1210 function pumpio_fetchinbox(&$a, $uid) {
1212 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1213 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1214 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1215 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1216 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
1217 $hostname = get_pconfig($uid, 'pumpio','host');
1218 $username = get_pconfig($uid, "pumpio", "user");
1220 $own_id = "https://".$hostname."/".$username;
1222 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1225 $client = new oauth_client_class;
1226 $client->oauth_version = '1.0a';
1227 $client->authorization_header = true;
1228 $client->url_parameters = false;
1230 $client->client_id = $ckey;
1231 $client->client_secret = $csecret;
1232 $client->access_token = $otoken;
1233 $client->access_token_secret = $osecret;
1235 $last_id = get_pconfig($uid,'pumpio','last_id');
1237 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1240 $url .= '?since='.urlencode($last_id);
1242 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
1245 $posts = array_reverse($user->items);
1248 foreach ($posts as $post) {
1249 $last_id = $post->id;
1250 pumpio_dopost($a, $client, $uid, $self, $post, $own_id);
1254 set_pconfig($uid,'pumpio','last_id', $last_id);
1257 function pumpio_getallusers(&$a, $uid) {
1258 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1259 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1260 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1261 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1262 $hostname = get_pconfig($uid, 'pumpio','host');
1263 $username = get_pconfig($uid, "pumpio", "user");
1265 $client = new oauth_client_class;
1266 $client->oauth_version = '1.0a';
1267 $client->authorization_header = true;
1268 $client->url_parameters = false;
1270 $client->client_id = $ckey;
1271 $client->client_secret = $csecret;
1272 $client->access_token = $otoken;
1273 $client->access_token_secret = $osecret;
1275 $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1277 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1279 if ($users->totalItems > count($users->items)) {
1280 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1282 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1285 foreach ($users->items AS $user)
1286 echo pumpio_get_contact($uid, $user)."\n";
1289 function pumpio_queue_hook(&$a,&$b) {
1291 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
1292 dbesc(NETWORK_PUMPIO)
1297 require_once('include/queue_fn.php');
1299 foreach($qi as $x) {
1300 if($x['network'] !== NETWORK_PUMPIO)
1303 logger('pumpio_queue: run');
1305 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
1306 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
1314 //logger('pumpio_queue: fetching userdata '.print_r($userdata, true));
1316 $oauth_token = get_pconfig($userdata['uid'], "pumpio", "oauth_token");
1317 $oauth_token_secret = get_pconfig($userdata['uid'], "pumpio", "oauth_token_secret");
1318 $consumer_key = get_pconfig($userdata['uid'], "pumpio","consumer_key");
1319 $consumer_secret = get_pconfig($userdata['uid'], "pumpio","consumer_secret");
1321 $host = get_pconfig($userdata['uid'], "pumpio", "host");
1322 $user = get_pconfig($userdata['uid'], "pumpio", "user");
1326 if ($oauth_token AND $oauth_token_secret AND
1327 $consumer_key AND $consumer_secret) {
1328 $username = $user.'@'.$host;
1330 logger('pumpio_queue: able to post for user '.$username);
1332 $z = unserialize($x['content']);
1334 $client = new oauth_client_class;
1335 $client->oauth_version = '1.0a';
1336 $client->url_parameters = false;
1337 $client->authorization_header = true;
1338 $client->access_token = $oauth_token;
1339 $client->access_token_secret = $oauth_token_secret;
1340 $client->client_id = $consumer_key;
1341 $client->client_secret = $consumer_secret;
1343 $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
1346 $post_id = $user->object->id;
1347 logger('pumpio_queue: send '.$username.': success '.$post_id);
1348 if($post_id AND $iscomment) {
1349 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$z['item']);
1350 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d LIMIT 1",
1355 remove_queue_item($x['id']);
1357 logger('pumpio_queue: send '.$username.': '.$url.' general error: ' . print_r($user,true));
1359 logger("pumpio_queue: Error getting tokens for user ".$userdata['uid']);
1362 logger('pumpio_queue: delayed');
1363 update_queue_time($x['id']);
1368 function pumpio_getreceiver(&$a, $b) {
1370 $receiver = array();
1372 if (!$b["private"]) {
1374 if(! strstr($b['postopts'],'pumpio'))
1377 $public = get_pconfig($b['uid'], "pumpio", "public");
1380 $receiver["to"][] = Array(
1381 "objectType" => "collection",
1382 "id" => "http://activityschema.org/collection/public");
1384 $cids = explode("><", $b["allow_cid"]);
1385 $gids = explode("><", $b["allow_gid"]);
1387 foreach ($cids AS $cid) {
1388 $cid = trim($cid, " <>");
1390 $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",
1393 dbesc(NETWORK_PUMPIO)
1397 $receiver["bcc"][] = Array(
1398 "displayName" => $r[0]["name"],
1399 "objectType" => "person",
1400 "preferredUsername" => $r[0]["nick"],
1401 "url" => $r[0]["url"]);
1404 foreach ($gids AS $gid) {
1405 $gid = trim($gid, " <>");
1407 $r = q("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network` ".
1408 "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d AND `group_member`.`uid` = %d ".
1409 "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
1412 dbesc(NETWORK_PUMPIO)
1415 foreach ($r AS $row)
1416 $receiver["bcc"][] = Array(
1417 "displayName" => $row["name"],
1418 "objectType" => "person",
1419 "preferredUsername" => $row["nick"],
1420 "url" => $row["url"]);
1424 if ($b["inform"] != "") {
1426 $inform = explode(",", $b["inform"]);
1428 foreach ($inform AS $cid) {
1429 if (substr($cid, 0, 4) != "cid:")
1432 $cid = str_replace("cid:", "", $cid);
1434 $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",
1437 dbesc(NETWORK_PUMPIO)
1441 $receiver["to"][] = Array(
1442 "displayName" => $r[0]["name"],
1443 "objectType" => "person",
1444 "preferredUsername" => $r[0]["nick"],
1445 "url" => $r[0]["url"]);
1453 function pumpio_fetchallcomments(&$a, $uid, $id) {
1454 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1455 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1456 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1457 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1458 $hostname = get_pconfig($uid, 'pumpio','host');
1459 $username = get_pconfig($uid, "pumpio", "user");
1461 $own_id = "https://".$hostname."/".$username;
1463 logger("pumpio_fetchallcomments: completing comment for user ".$uid." url ".$url);
1465 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1468 // Fetching the original post - Two queries for speed issues
1469 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1475 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1485 $url = $r[0]["extid"];
1489 $client = new oauth_client_class;
1490 $client->oauth_version = '1.0a';
1491 $client->authorization_header = true;
1492 $client->url_parameters = false;
1494 $client->client_id = $ckey;
1495 $client->client_secret = $csecret;
1496 $client->access_token = $otoken;
1497 $client->access_token_secret = $osecret;
1499 logger("pumpio_fetchallcomments: fetching comment for user ".$uid." url ".$url);
1501 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $item);
1506 if ($item->replies->totalItems == 0)
1509 foreach ($item->replies->items AS $item) {
1510 if ($item->id == $id)
1513 // Checking if the comment already exists - Two queries for speed issues
1514 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1522 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1530 $post->verb = "post";
1531 $post->actor = $item->author;
1532 $post->published = $item->published;
1533 $post->received = $item->updated;
1534 $post->generator->displayName = "pumpio";
1536 unset($item->author);
1537 unset($item->published);
1538 unset($item->updated);
1540 $post->object = $item;
1542 logger("pumpio_fetchallcomments: posting comment ".$post->object->id);
1543 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, false);
1549 - refresh after post doesn't always happen