3 * Name: pump.io Post Connector
4 * Description: Bidirectional (posting, relaying and reading) connector for 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 require_once("mod/settings.php");
45 if (isset($a->argv[1]))
46 switch ($a->argv[1]) {
48 $o = pumpio_connect($a);
51 $o = print_r($a->argv, true);
55 $o = pumpio_connect($a);
60 function pumpio_registerclient(&$a, $host) {
62 $url = "https://".$host."/api/client/register";
66 $application_name = get_config('pumpio', 'application_name');
68 if ($application_name == "")
69 $application_name = $a->get_hostname();
71 $adminlist = explode(",", str_replace(" ", "", $a->config['admin_email']));
73 $params["type"] = "client_associate";
74 $params["contacts"] = $adminlist[0];
75 $params["application_type"] = "native";
76 $params["application_name"] = $application_name;
77 $params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
78 $params["redirect_uris"] = $a->get_baseurl()."/pumpio/connect";
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" ' : '');
206 $css = (($enabled) ? '' : '-disabled');
208 $def_enabled = get_pconfig(local_user(),'pumpio','post_by_default');
209 $def_checked = (($def_enabled) ? ' checked="checked" ' : '');
211 $public_enabled = get_pconfig(local_user(),'pumpio','public');
212 $public_checked = (($public_enabled) ? ' checked="checked" ' : '');
214 $mirror_enabled = get_pconfig(local_user(),'pumpio','mirror');
215 $mirror_checked = (($mirror_enabled) ? ' checked="checked" ' : '');
217 $servername = get_pconfig(local_user(), "pumpio", "host");
218 $username = get_pconfig(local_user(), "pumpio", "user");
220 /* Add some HTML to the existing form */
222 $s .= '<span id="settings_pumpio_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_pumpio_expanded\'); openClose(\'settings_pumpio_inflated\');">';
223 $s .= '<img class="connector'.$css.'" src="images/pumpio.png" /><h3 class="connector">'. t('Pump.io Import/Export/Mirror').'</h3>';
225 $s .= '<div id="settings_pumpio_expanded" class="settings-block" style="display: none;">';
226 $s .= '<span class="fakelink" onclick="openClose(\'settings_pumpio_expanded\'); openClose(\'settings_pumpio_inflated\');">';
227 $s .= '<img class="connector'.$css.'" src="images/pumpio.png" /><h3 class="connector">'. t('Pump.io Import/Export/Mirror').'</h3>';
230 $s .= '<div id="pumpio-username-wrapper">';
231 $s .= '<label id="pumpio-username-label" for="pumpio-username">'.t('pump.io username (without the servername)').'</label>';
232 $s .= '<input id="pumpio-username" type="text" name="pumpio_user" value="'.$username.'" />';
233 $s .= '</div><div class="clear"></div>';
235 $s .= '<div id="pumpio-servername-wrapper">';
236 $s .= '<label id="pumpio-servername-label" for="pumpio-servername">'.t('pump.io servername (without "http://" or "https://" )').'</label>';
237 $s .= '<input id="pumpio-servername" type="text" name="pumpio_host" value="'.$servername.'" />';
238 $s .= '</div><div class="clear"></div>';
240 if (($username != '') AND ($servername != '')) {
242 $oauth_token = get_pconfig(local_user(), "pumpio", "oauth_token");
243 $oauth_token_secret = get_pconfig(local_user(), "pumpio", "oauth_token_secret");
245 $s .= '<div id="pumpio-password-wrapper">';
246 if (($oauth_token == "") OR ($oauth_token_secret == "")) {
247 $s .= '<div id="pumpio-authenticate-wrapper">';
248 $s .= '<a href="'.$a->get_baseurl().'/pumpio/connect">'.t("Authenticate your pump.io connection").'</a>';
249 $s .= '</div><div class="clear"></div>';
251 $s .= '<div id="pumpio-import-wrapper">';
252 $s .= '<label id="pumpio-import-label" for="pumpio-import">' . t('Import the remote timeline') . '</label>';
253 $s .= '<input id="pumpio-import" type="checkbox" name="pumpio_import" value="1" ' . $import_checked . '/>';
254 $s .= '</div><div class="clear"></div>';
256 $s .= '<div id="pumpio-enable-wrapper">';
257 $s .= '<label id="pumpio-enable-label" for="pumpio-checkbox">' . t('Enable pump.io Post Plugin') . '</label>';
258 $s .= '<input id="pumpio-checkbox" type="checkbox" name="pumpio" value="1" ' . $checked . '/>';
259 $s .= '</div><div class="clear"></div>';
261 $s .= '<div id="pumpio-bydefault-wrapper">';
262 $s .= '<label id="pumpio-bydefault-label" for="pumpio-bydefault">' . t('Post to pump.io by default') . '</label>';
263 $s .= '<input id="pumpio-bydefault" type="checkbox" name="pumpio_bydefault" value="1" ' . $def_checked . '/>';
264 $s .= '</div><div class="clear"></div>';
266 $s .= '<div id="pumpio-public-wrapper">';
267 $s .= '<label id="pumpio-public-label" for="pumpio-public">' . t('Should posts be public?') . '</label>';
268 $s .= '<input id="pumpio-public" type="checkbox" name="pumpio_public" value="1" ' . $public_checked . '/>';
269 $s .= '</div><div class="clear"></div>';
271 $s .= '<div id="pumpio-mirror-wrapper">';
272 $s .= '<label id="pumpio-mirror-label" for="pumpio-mirror">' . t('Mirror all public posts') . '</label>';
273 $s .= '<input id="pumpio-mirror" type="checkbox" name="pumpio_mirror" value="1" ' . $mirror_checked . '/>';
274 $s .= '</div><div class="clear"></div>';
276 $s .= '<div id="pumpio-delete-wrapper">';
277 $s .= '<label id="pumpio-delete-label" for="pumpio-delete">' . t('Check to delete this preset') . '</label>';
278 $s .= '<input id="pumpio-delete" type="checkbox" name="pumpio_delete" value="1" />';
279 $s .= '</div><div class="clear"></div>';
282 $s .= '</div><div class="clear"></div>';
285 /* provide a submit button */
287 $s .= '<div class="settings-submit-wrapper" ><input type="submit" id="pumpio-submit" name="pumpio-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div></div>';
291 function pumpio_settings_post(&$a,&$b) {
293 if(x($_POST,'pumpio-submit')) {
294 if(x($_POST,'pumpio_delete')) {
295 set_pconfig(local_user(),'pumpio','consumer_key','');
296 set_pconfig(local_user(),'pumpio','consumer_secret','');
297 set_pconfig(local_user(),'pumpio','oauth_token','');
298 set_pconfig(local_user(),'pumpio','oauth_token_secret','');
299 set_pconfig(local_user(),'pumpio','post',false);
300 set_pconfig(local_user(),'pumpio','import',false);
301 set_pconfig(local_user(),'pumpio','host','');
302 set_pconfig(local_user(),'pumpio','user','');
303 set_pconfig(local_user(),'pumpio','public',false);
304 set_pconfig(local_user(),'pumpio','mirror',false);
305 set_pconfig(local_user(),'pumpio','post_by_default',false);
306 set_pconfig(local_user(),'pumpio','lastdate', 0);
307 set_pconfig(local_user(),'pumpio','last_id', '');
309 // filtering the username if it is filled wrong
310 $user = $_POST['pumpio_user'];
311 if (strstr($user, "@")) {
312 $pos = strpos($user, "@");
314 $user = substr($user, 0, $pos);
317 // Filtering the hostname if someone is entering it with "http"
318 $host = $_POST['pumpio_host'];
320 $host = str_replace(array("https://", "http://"), array("", ""), $host);
322 set_pconfig(local_user(),'pumpio','post',intval($_POST['pumpio']));
323 set_pconfig(local_user(),'pumpio','import',$_POST['pumpio_import']);
324 set_pconfig(local_user(),'pumpio','host',$host);
325 set_pconfig(local_user(),'pumpio','user',$user);
326 set_pconfig(local_user(),'pumpio','public',$_POST['pumpio_public']);
327 set_pconfig(local_user(),'pumpio','mirror',$_POST['pumpio_mirror']);
328 set_pconfig(local_user(),'pumpio','post_by_default',intval($_POST['pumpio_bydefault']));
330 if (!$_POST['pumpio_mirror'])
331 del_pconfig(local_user(),'pumpio','lastdate');
333 //header("Location: ".$a->get_baseurl()."/pumpio/connect");
338 function pumpio_post_local(&$a,&$b) {
340 if((! local_user()) || (local_user() != $b['uid']))
343 $pumpio_post = intval(get_pconfig(local_user(),'pumpio','post'));
345 $pumpio_enable = (($pumpio_post && x($_REQUEST,'pumpio_enable')) ? intval($_REQUEST['pumpio_enable']) : 0);
347 if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'pumpio','post_by_default')))
353 if(strlen($b['postopts']))
354 $b['postopts'] .= ',';
356 $b['postopts'] .= 'pumpio';
362 function pumpio_send(&$a,&$b) {
364 if (!get_pconfig($b["uid"],'pumpio','import')) {
365 if($b['deleted'] || $b['private'] || ($b['created'] !== $b['edited']))
369 logger("pumpio_send: parameter ".print_r($b, true), LOGGER_DATA);
371 if($b['parent'] != $b['id']) {
372 // Looking if its a reply to a pumpio post
373 $r = q("SELECT item.* FROM item, contact WHERE item.id = %d AND item.uid = %d AND contact.id = `contact-id` AND contact.network='%s'LIMIT 1",
374 intval($b["parent"]),
376 dbesc(NETWORK_PUMPIO));
379 logger("pumpio_send: no pumpio post ".$b["parent"]);
388 $receiver = pumpio_getreceiver($a, $b);
390 logger("pumpio_send: receiver ".print_r($receiver, true));
392 if (!count($receiver) AND ($b['private'] OR !strstr($b['postopts'],'pumpio')))
396 if($b['verb'] == ACTIVITY_LIKE) {
398 pumpio_action($a, $b["uid"], $b["thr-parent"], "unlike");
400 pumpio_action($a, $b["uid"], $b["thr-parent"], "like");
404 if($b['verb'] == ACTIVITY_DISLIKE)
407 if (($b['verb'] == ACTIVITY_POST) AND ($b['created'] !== $b['edited']) AND !$b['deleted'])
408 pumpio_action($a, $b["uid"], $b["uri"], "update", $b["body"]);
410 if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
411 pumpio_action($a, $b["uid"], $b["uri"], "delete");
413 if($b['deleted'] || ($b['created'] !== $b['edited']))
416 // if post comes from pump.io don't send it back
417 if($b['app'] == "pump.io")
421 // Support for native shares
422 // http://<hostname>/api/<type>/shares?id=<the-object-id>
424 $oauth_token = get_pconfig($b['uid'], "pumpio", "oauth_token");
425 $oauth_token_secret = get_pconfig($b['uid'], "pumpio", "oauth_token_secret");
426 $consumer_key = get_pconfig($b['uid'], "pumpio","consumer_key");
427 $consumer_secret = get_pconfig($b['uid'], "pumpio","consumer_secret");
429 $host = get_pconfig($b['uid'], "pumpio", "host");
430 $user = get_pconfig($b['uid'], "pumpio", "user");
431 $public = get_pconfig($b['uid'], "pumpio", "public");
433 if($oauth_token && $oauth_token_secret) {
435 require_once('include/bbcode.php');
437 $title = trim($b['title']);
439 $content = bbcode($b['body'], false, false, 4);
441 // Enhance the way, videos are displayed
442 $content = preg_replace('/<a href="(https?:\/\/www.youtube.com\/.*?)".*?>(.*?)<\/a>/ism',"\n[url]$1[/url]\n",$content);
443 $content = preg_replace('/<a href="(https?:\/\/youtu.be\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
444 $content = preg_replace('/<a href="(https?:\/\/vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
445 $content = preg_replace('/<a href="(https?:\/\/player.vimeo.com\/.*?)".*?>(.*?)<\/a>/ism',"\n$1\n",$content);
447 $URLSearchString = "^\[\]";
448 $content = preg_replace_callback("/\[url\]([$URLSearchString]*)\[\/url\]/ism",'tryoembed',$content);
452 $params["verb"] = "post";
455 $params["object"] = array(
456 'objectType' => "note",
457 'content' => $content);
460 $params["object"]["displayName"] = $title;
462 if (count($receiver["to"]))
463 $params["to"] = $receiver["to"];
465 if (count($receiver["bto"]))
466 $params["bto"] = $receiver["bto"];
468 if (count($receiver["cc"]))
469 $params["cc"] = $receiver["cc"];
471 if (count($receiver["bcc"]))
472 $params["bcc"] = $receiver["bcc"];
475 $inReplyTo = array("id" => $orig_post["uri"],
476 "objectType" => "note");
478 if (($orig_post["object-type"] != "") AND (strstr($orig_post["object-type"], NAMESPACE_ACTIVITY_SCHEMA)))
479 $inReplyTo["objectType"] = str_replace(NAMESPACE_ACTIVITY_SCHEMA, '', $orig_post["object-type"]);
481 $params["object"] = array(
482 'objectType' => "comment",
483 'content' => $content,
484 'inReplyTo' => $inReplyTo);
487 $params["object"]["displayName"] = $title;
490 $client = new oauth_client_class;
491 $client->oauth_version = '1.0a';
492 $client->url_parameters = false;
493 $client->authorization_header = true;
494 $client->access_token = $oauth_token;
495 $client->access_token_secret = $oauth_token_secret;
496 $client->client_id = $consumer_key;
497 $client->client_secret = $consumer_secret;
499 $username = $user.'@'.$host;
500 $url = 'https://'.$host.'/api/user/'.$user.'/feed';
502 if (pumpio_reachable($url))
503 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
509 if ($user->generator->displayName)
510 set_pconfig($b["uid"], "pumpio", "application_name", $user->generator->displayName);
512 $post_id = $user->object->id;
513 logger('pumpio_send '.$username.': success '.$post_id);
514 if($post_id AND $iscomment) {
515 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$b['id']);
516 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
522 logger('pumpio_send '.$username.': '.$url.' general error: ' . print_r($user,true));
524 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
526 $a->contact = $r[0]["id"];
528 $s = serialize(array('url' => $url, 'item' => $b['id'], 'post' => $params));
529 require_once('include/queue_fn.php');
530 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
531 notice(t('Pump.io post failed. Queued for retry.').EOL);
537 function pumpio_action(&$a, $uid, $uri, $action, $content = "") {
539 // Don't do likes and other stuff if you don't import the timeline
540 if (!get_pconfig($uid,'pumpio','import'))
543 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
544 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
545 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
546 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
547 $hostname = get_pconfig($uid, 'pumpio','host');
548 $username = get_pconfig($uid, "pumpio", "user");
550 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
560 if ($orig_post["extid"] AND !strstr($orig_post["extid"], "/proxy/"))
561 $uri = $orig_post["extid"];
563 $uri = $orig_post["uri"];
565 if (($orig_post["object-type"] != "") AND (strstr($orig_post["object-type"], NAMESPACE_ACTIVITY_SCHEMA)))
566 $objectType = str_replace(NAMESPACE_ACTIVITY_SCHEMA, '', $orig_post["object-type"]);
567 elseif (strstr($uri, "/api/comment/"))
568 $objectType = "comment";
569 elseif (strstr($uri, "/api/note/"))
570 $objectType = "note";
571 elseif (strstr($uri, "/api/image/"))
572 $objectType = "image";
574 $params["verb"] = $action;
575 $params["object"] = array('id' => $uri,
576 "objectType" => $objectType,
577 "content" => $content);
579 $client = new oauth_client_class;
580 $client->oauth_version = '1.0a';
581 $client->authorization_header = true;
582 $client->url_parameters = false;
584 $client->client_id = $ckey;
585 $client->client_secret = $csecret;
586 $client->access_token = $otoken;
587 $client->access_token_secret = $osecret;
589 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
591 if (pumpio_reachable($url))
592 $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
597 logger('pumpio_action '.$username.' '.$action.': success '.$uri);
599 logger('pumpio_action '.$username.' '.$action.': general error: '.$uri.' '.print_r($user,true));
601 $r = q("SELECT `id` FROM `contact` WHERE `uid` = %d AND `self`", $b['uid']);
603 $a->contact = $r[0]["id"];
605 $s = serialize(array('url' => $url, 'item' => $orig_post["id"], 'post' => $params));
606 require_once('include/queue_fn.php');
607 add_to_queue($a->contact,NETWORK_PUMPIO,$s);
608 notice(t('Pump.io like failed. Queued for retry.').EOL);
612 function pumpio_sync(&$a) {
613 $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = 'pumpio'",
619 $last = get_config('pumpio','last_poll');
621 $poll_interval = intval(get_config('pumpio','poll_interval'));
623 $poll_interval = PUMPIO_DEFAULT_POLL_INTERVAL;
626 $next = $last + ($poll_interval * 60);
628 logger('pumpio: poll intervall not reached');
632 logger('pumpio: cron_start');
634 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'mirror' AND `v` = '1' ORDER BY RAND() ");
637 logger('pumpio: mirroring user '.$rr['uid']);
638 pumpio_fetchtimeline($a, $rr['uid']);
642 $abandon_days = intval(get_config('system','account_abandon_days'));
643 if ($abandon_days < 1)
646 $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400);
648 $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'pumpio' AND `k` = 'import' AND `v` = '1' ORDER BY RAND() ");
651 if ($abandon_days != 0) {
652 $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
654 logger('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
659 logger('pumpio: importing timeline from user '.$rr['uid']);
660 pumpio_fetchinbox($a, $rr['uid']);
662 // check for new contacts once a day
663 $last_contact_check = get_pconfig($rr['uid'],'pumpio','contact_check');
664 if($last_contact_check)
665 $next_contact_check = $last_contact_check + 86400;
667 $next_contact_check = 0;
669 if($next_contact_check <= time()) {
670 pumpio_getallusers($a, $rr["uid"]);
671 set_pconfig($rr['uid'],'pumpio','contact_check',time());
676 logger('pumpio: cron_end');
678 set_config('pumpio','last_poll', time());
681 function pumpio_cron(&$a,$b) {
683 proc_run("php","addon/pumpio/pumpio_sync.php");
686 function pumpio_fetchtimeline(&$a, $uid) {
687 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
688 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
689 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
690 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
691 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
692 $hostname = get_pconfig($uid, 'pumpio','host');
693 $username = get_pconfig($uid, "pumpio", "user");
695 // get the application name for the pump.io app
696 // 1st try personal config, then system config and fallback to the
697 // hostname of the node if neither one is set.
698 $application_name = get_pconfig( $uid, 'pumpio', 'application_name');
699 if ($application_name == "")
700 $application_name = get_config('pumpio', 'application_name');
701 if ($application_name == "")
702 $application_name = $a->get_hostname();
704 $first_time = ($lastdate == "");
706 $client = new oauth_client_class;
707 $client->oauth_version = '1.0a';
708 $client->authorization_header = true;
709 $client->url_parameters = false;
711 $client->client_id = $ckey;
712 $client->client_secret = $csecret;
713 $client->access_token = $otoken;
714 $client->access_token_secret = $osecret;
716 $url = 'https://'.$hostname.'/api/user/'.$username.'/feed/major';
718 logger('pumpio: fetching for user '.$uid.' '.$url.' C:'.$client->client_id.' CS:'.$client->client_secret.' T:'.$client->access_token.' TS:'.$client->access_token_secret);
720 $username = $user.'@'.$host;
722 if (pumpio_reachable($url))
723 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
728 logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
732 $posts = array_reverse($user->items);
734 $initiallastdate = $lastdate;
738 foreach ($posts as $post) {
739 if ($post->published <= $initiallastdate)
742 if ($lastdate < $post->published)
743 $lastdate = $post->published;
748 $receiptians = array();
749 if (@is_array($post->cc))
750 $receiptians = array_merge($receiptians, $post->cc);
752 if (@is_array($post->to))
753 $receiptians = array_merge($receiptians, $post->to);
756 foreach ($receiptians AS $receiver)
757 if (is_string($receiver->objectType))
758 if ($receiver->id == "http://activityschema.org/collection/public")
761 if ($public AND !stristr($post->generator->displayName, $application_name)) {
762 require_once('include/html2bbcode.php');
764 $_SESSION["authenticated"] = true;
765 $_SESSION["uid"] = $uid;
768 $_REQUEST["type"] = "wall";
769 $_REQUEST["api_source"] = true;
770 $_REQUEST["profile_uid"] = $uid;
771 $_REQUEST["source"] = "pump.io";
773 if ($post->object->displayName != "")
774 $_REQUEST["title"] = html2bbcode($post->object->displayName);
776 $_REQUEST["title"] = "";
778 $_REQUEST["body"] = html2bbcode($post->object->content);
780 // To-Do: Picture has to be cached and stored locally
781 if ($post->object->fullImage->url != "") {
782 if ($post->object->fullImage->pump_io->proxyURL != "")
783 $_REQUEST["body"] = "[url=".$post->object->fullImage->pump_io->proxyURL."][img]".$post->object->image->pump_io->proxyURL."[/img][/url]\n".$_REQUEST["body"];
785 $_REQUEST["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$_REQUEST["body"];
788 logger('pumpio: posting for user '.$uid);
790 require_once('mod/item.php');
793 logger('pumpio: posting done - user '.$uid);
799 set_pconfig($uid,'pumpio','lastdate', $lastdate);
802 function pumpio_dounlike(&$a, $uid, $self, $post, $own_id) {
803 // Searching for the unliked post
804 // Two queries for speed issues
805 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
806 dbesc($post->object->id),
813 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
814 dbesc($post->object->id),
826 if(link_compare($post->actor->url, $own_id)) {
827 $contactid = $self[0]['id'];
829 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
830 dbesc($post->actor->url),
835 $contactid = $r[0]['id'];
838 $contactid = $orig_post['contact-id'];
841 $r = q("UPDATE `item` SET `deleted` = 1, `unseen` = 1, `changed` = '%s' WHERE `verb` = '%s' AND `uid` = %d AND `contact-id` = %d AND `thr-parent` = '%s'",
842 dbesc(datetime_convert()),
843 dbesc(ACTIVITY_LIKE),
846 dbesc($orig_post['uri'])
850 logger("pumpio_dounlike: unliked existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
852 logger("pumpio_dounlike: not found. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
855 function pumpio_dolike(&$a, $uid, $self, $post, $own_id, $threadcompletion = true) {
856 require_once('include/items.php');
858 // Searching for the liked post
859 // Two queries for speed issues
860 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
861 dbesc($post->object->id),
868 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
869 dbesc($post->object->id),
880 if ($threadcompletion)
881 pumpio_fetchallcomments($a, $uid, $post->object->id);
885 if(link_compare($post->actor->url, $own_id)) {
886 $contactid = $self[0]['id'];
887 $post->actor->displayName = $self[0]['name'];
888 $post->actor->url = $self[0]['url'];
889 $post->actor->image->url = $self[0]['photo'];
891 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
892 dbesc($post->actor->url),
897 $contactid = $r[0]['id'];
900 $contactid = $orig_post['contact-id'];
903 $r = q("SELECT parent FROM `item` WHERE `verb` = '%s' AND `uid` = %d AND `contact-id` = %d AND `thr-parent` = '%s' LIMIT 1",
904 dbesc(ACTIVITY_LIKE),
907 dbesc($orig_post['uri'])
911 logger("pumpio_dolike: found existing like. User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
916 $likedata['parent'] = $orig_post['id'];
917 $likedata['verb'] = ACTIVITY_LIKE;
918 $likedata['gravity'] = 3;
919 $likedata['uid'] = $uid;
920 $likedata['wall'] = 0;
921 $likedata['uri'] = item_new_uri($a->get_baseurl(), $uid);
922 $likedata['parent-uri'] = $orig_post["uri"];
923 $likedata['contact-id'] = $contactid;
924 $likedata['app'] = $post->generator->displayName;
925 $likedata['author-name'] = $post->actor->displayName;
926 $likedata['author-link'] = $post->actor->url;
927 $likedata['author-avatar'] = $post->actor->image->url;
929 $author = '[url=' . $likedata['author-link'] . ']' . $likedata['author-name'] . '[/url]';
930 $objauthor = '[url=' . $orig_post['author-link'] . ']' . $orig_post['author-name'] . '[/url]';
931 $post_type = t('status');
932 $plink = '[url=' . $orig_post['plink'] . ']' . $post_type . '[/url]';
933 $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
935 $likedata['body'] = sprintf( t('%1$s likes %2$s\'s %3$s'), $author, $objauthor, $plink);
937 $likedata['object'] = '<object><type>' . ACTIVITY_OBJ_NOTE . '</type><local>1</local>' .
938 '<id>' . $orig_post['uri'] . '</id><link>' . xmlify('<link rel="alternate" type="text/html" href="' . xmlify($orig_post['plink']) . '" />') . '</link><title>' . $orig_post['title'] . '</title><content>' . $orig_post['body'] . '</content></object>';
940 $ret = item_store($likedata);
942 logger("pumpio_dolike: ".$ret." User ".$own_id." ".$uid." Contact: ".$contactid." Url ".$orig_post['uri']);
945 function pumpio_get_contact($uid, $contact) {
947 $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
948 dbesc(normalise_link($contact->url)));
951 q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
952 dbesc(normalise_link($contact->url)),
953 dbesc($contact->displayName),
954 dbesc($contact->preferredUsername),
955 dbesc($contact->image->url));
957 q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
958 dbesc($contact->displayName),
959 dbesc($contact->preferredUsername),
960 dbesc($contact->image->url),
961 dbesc(normalise_link($contact->url)));
963 if (DB_UPDATE_VERSION >= "1177")
964 q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
965 dbesc($contact->location->displayName),
966 dbesc($contact->summary),
967 dbesc(normalise_link($contact->url)));
969 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
970 intval($uid), dbesc($contact->url));
973 // create contact record
974 q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
975 `name`, `nick`, `photo`, `network`, `rel`, `priority`,
976 `writable`, `blocked`, `readonly`, `pending` )
977 VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
979 dbesc(datetime_convert()),
980 dbesc($contact->url),
981 dbesc(normalise_link($contact->url)),
982 dbesc(str_replace("acct:", "", $contact->id)),
984 dbesc($contact->id), // What is it for?
985 dbesc('pump.io ' . $contact->id), // What is it for?
986 dbesc($contact->displayName),
987 dbesc($contact->preferredUsername),
988 dbesc($contact->image->url),
989 dbesc(NETWORK_PUMPIO),
990 intval(CONTACT_IS_FRIEND),
995 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d LIMIT 1",
996 dbesc($contact->url),
1003 $contact_id = $r[0]['id'];
1005 $g = q("select def_gid from user where uid = %d limit 1",
1009 if($g && intval($g[0]['def_gid'])) {
1010 require_once('include/group.php');
1011 group_add_member($uid,'',$contact_id,$g[0]['def_gid']);
1014 require_once("Photo.php");
1016 $photos = import_profile_photo($contact->image->url,$uid,$contact_id);
1018 q("UPDATE `contact` SET `photo` = '%s',
1023 `avatar-date` = '%s'
1029 dbesc(datetime_convert()),
1030 dbesc(datetime_convert()),
1031 dbesc(datetime_convert()),
1035 if (DB_UPDATE_VERSION >= "1177")
1036 q("UPDATE `contact` SET `location` = '%s',
1039 dbesc($contact->location->displayName),
1040 dbesc($contact->summary),
1044 // update profile photos once every two weeks as we have no notification of when they change.
1045 //$update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -14 days')) ? true : false);
1046 $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
1048 // check that we have all the photos, this has been known to fail on occasion
1050 if((! $r[0]['photo']) || (! $r[0]['thumb']) || (! $r[0]['micro']) || ($update_photo)) {
1051 require_once("Photo.php");
1053 $photos = import_profile_photo($contact->image->url, $uid, $r[0]['id']);
1055 q("UPDATE `contact` SET `photo` = '%s',
1060 `avatar-date` = '%s',
1068 dbesc(datetime_convert()),
1069 dbesc(datetime_convert()),
1070 dbesc(datetime_convert()),
1071 dbesc($contact->displayName),
1072 dbesc($contact->preferredUsername),
1076 if (DB_UPDATE_VERSION >= "1177")
1077 q("UPDATE `contact` SET `location` = '%s',
1080 dbesc($contact->location->displayName),
1081 dbesc($contact->summary),
1088 return($r[0]["id"]);
1091 function pumpio_dodelete(&$a, $uid, $self, $post, $own_id) {
1093 // Two queries for speed issues
1094 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1095 dbesc($post->object->id),
1100 return drop_item($r[0]["id"], $false);
1102 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1103 dbesc($post->object->id),
1108 return drop_item($r[0]["id"], $false);
1111 function pumpio_dopost(&$a, $client, $uid, $self, $post, $own_id, $threadcompletion = true) {
1112 require_once('include/items.php');
1113 require_once('include/html2bbcode.php');
1115 if (($post->verb == "like") OR ($post->verb == "favorite"))
1116 return pumpio_dolike($a, $uid, $self, $post, $own_id);
1118 if (($post->verb == "unlike") OR ($post->verb == "unfavorite"))
1119 return pumpio_dounlike($a, $uid, $self, $post, $own_id);
1121 if ($post->verb == "delete")
1122 return pumpio_dodelete($a, $uid, $self, $post, $own_id);
1124 if ($post->verb != "update") {
1125 // Two queries for speed issues
1126 $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1127 dbesc($post->object->id),
1134 $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1135 dbesc($post->object->id),
1143 // Only handle these three types
1144 if (!strstr("post|share|update", $post->verb))
1147 $receiptians = array();
1148 if (@is_array($post->cc))
1149 $receiptians = array_merge($receiptians, $post->cc);
1151 if (@is_array($post->to))
1152 $receiptians = array_merge($receiptians, $post->to);
1154 foreach ($receiptians AS $receiver)
1155 if (is_string($receiver->objectType))
1156 if ($receiver->id == "http://activityschema.org/collection/public")
1159 $postarray = array();
1160 $postarray['network'] = NETWORK_PUMPIO;
1161 $postarray['gravity'] = 0;
1162 $postarray['uid'] = $uid;
1163 $postarray['wall'] = 0;
1164 $postarray['uri'] = $post->object->id;
1165 $postarray['object-type'] = NAMESPACE_ACTIVITY_SCHEMA.strtolower($post->object->objectType);
1167 if ($post->object->objectType != "comment") {
1168 $contact_id = pumpio_get_contact($uid, $post->actor);
1171 $contact_id = $self[0]['id'];
1173 $postarray['parent-uri'] = $post->object->id;
1176 $postarray['private'] = 1;
1177 $postarray['allow_cid'] = '<' . $self[0]['id'] . '>';
1182 if(link_compare($post->actor->url, $own_id)) {
1183 $contact_id = $self[0]['id'];
1184 $post->actor->displayName = $self[0]['name'];
1185 $post->actor->url = $self[0]['url'];
1186 $post->actor->image->url = $self[0]['photo'];
1188 // Take an existing contact, the contact of the note or - as a fallback - the id of the user
1189 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1190 dbesc($post->actor->url),
1195 $contact_id = $r[0]['id'];
1197 $r = q("SELECT * FROM `contact` WHERE `url` = '%s' AND `uid` = %d AND `blocked` = 0 AND `readonly` = 0 LIMIT 1",
1198 dbesc($post->actor->url),
1203 $contact_id = $r[0]['id'];
1205 $contact_id = $self[0]['id'];
1209 $reply = new stdClass;
1210 $reply->verb = "note";
1211 $reply->cc = $post->cc;
1212 $reply->to = $post->to;
1213 $reply->object = new stdClass;
1214 $reply->object->objectType = $post->object->inReplyTo->objectType;
1215 $reply->object->content = $post->object->inReplyTo->content;
1216 $reply->object->id = $post->object->inReplyTo->id;
1217 $reply->actor = $post->object->inReplyTo->author;
1218 $reply->url = $post->object->inReplyTo->url;
1219 $reply->generator = new stdClass;
1220 $reply->generator->displayName = "pumpio";
1221 $reply->published = $post->object->inReplyTo->published;
1222 $reply->received = $post->object->inReplyTo->updated;
1223 $reply->url = $post->object->inReplyTo->url;
1224 pumpio_dopost($a, $client, $uid, $self, $reply, $own_id, false);
1226 $postarray['parent-uri'] = $post->object->inReplyTo->id;
1229 if ($post->object->pump_io->proxyURL)
1230 $postarray['extid'] = $post->object->pump_io->proxyURL;
1232 $postarray['contact-id'] = $contact_id;
1233 $postarray['verb'] = ACTIVITY_POST;
1234 $postarray['owner-name'] = $post->actor->displayName;
1235 $postarray['owner-link'] = $post->actor->url;
1236 $postarray['owner-avatar'] = $post->actor->image->url;
1237 $postarray['author-name'] = $post->actor->displayName;
1238 $postarray['author-link'] = $post->actor->url;
1239 $postarray['author-avatar'] = $post->actor->image->url;
1240 $postarray['plink'] = $post->object->url;
1241 $postarray['app'] = $post->generator->displayName;
1242 $postarray['body'] = html2bbcode($post->object->content);
1244 if ($post->object->fullImage->url != "")
1245 $postarray["body"] = "[url=".$post->object->fullImage->url."][img]".$post->object->image->url."[/img][/url]\n".$postarray["body"];
1247 if ($post->object->displayName != "")
1248 $postarray['title'] = $post->object->displayName;
1250 $postarray['created'] = datetime_convert('UTC','UTC',$post->published);
1251 $postarray['edited'] = datetime_convert('UTC','UTC',$post->received);
1253 if ($post->verb == "share") {
1254 if (!intval(get_config('system','wall-to-wall_share'))) {
1255 $postarray['body'] = "[share author='".$post->object->author->displayName.
1256 "' profile='".$post->object->author->url.
1257 "' avatar='".$post->object->author->image->url.
1258 "' posted='".datetime_convert('UTC','UTC',$post->object->created).
1259 "' link='".$post->links->self->href."']".$postarray['body']."[/share]";
1261 // Let shares look like wall-to-wall posts
1262 $postarray['author-name'] = $post->object->author->displayName;
1263 $postarray['author-link'] = $post->object->author->url;
1264 $postarray['author-avatar'] = $post->object->author->image->url;
1268 if (trim($postarray['body']) == "")
1271 $top_item = item_store($postarray);
1272 $postarray["id"] = $top_item;
1274 if (($top_item == 0) AND ($post->verb == "update")) {
1275 $r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
1276 dbesc($postarray["title"]),
1277 dbesc($postarray["body"]),
1278 dbesc($postarray["edited"]),
1279 dbesc($postarray["uri"]),
1284 if ($post->object->objectType == "comment") {
1286 if ($threadcompletion)
1287 pumpio_fetchallcomments($a, $uid, $postarray['parent-uri']);
1289 $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1",
1296 $importer_url = $a->get_baseurl() . '/profile/' . $user[0]['nickname'];
1298 if (link_compare($own_id, $postarray['author-link']))
1301 $myconv = q("SELECT `author-link`, `author-avatar`, `parent` FROM `item` WHERE `parent-uri` = '%s' AND `uid` = %d AND `parent` != 0 AND `deleted` = 0",
1302 dbesc($postarray['parent-uri']),
1306 if(count($myconv)) {
1308 foreach($myconv as $conv) {
1309 // now if we find a match, it means we're in this conversation
1311 if(!link_compare($conv['author-link'],$importer_url) AND !link_compare($conv['author-link'],$own_id))
1314 require_once('include/enotify.php');
1316 $conv_parent = $conv['parent'];
1319 'type' => NOTIFY_COMMENT,
1320 'notify_flags' => $user[0]['notify-flags'],
1321 'language' => $user[0]['language'],
1322 'to_name' => $user[0]['username'],
1323 'to_email' => $user[0]['email'],
1324 'uid' => $user[0]['uid'],
1325 'item' => $postarray,
1326 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($top_item)),
1327 'source_name' => $postarray['author-name'],
1328 'source_link' => $postarray['author-link'],
1329 'source_photo' => $postarray['author-avatar'],
1330 'verb' => ACTIVITY_POST,
1332 'parent' => $conv_parent,
1335 // only send one notification
1344 function pumpio_fetchinbox(&$a, $uid) {
1346 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1347 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1348 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1349 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1350 $lastdate = get_pconfig($uid, 'pumpio', 'lastdate');
1351 $hostname = get_pconfig($uid, 'pumpio','host');
1352 $username = get_pconfig($uid, "pumpio", "user");
1354 $own_id = "https://".$hostname."/".$username;
1356 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1359 $lastitems = q("SELECT uri FROM `item` WHERE `network` = '%s' AND `uid` = %d AND
1360 `extid` != '' AND `id` = `parent`
1361 ORDER BY `commented` DESC LIMIT 10",
1362 dbesc(NETWORK_PUMPIO),
1366 $client = new oauth_client_class;
1367 $client->oauth_version = '1.0a';
1368 $client->authorization_header = true;
1369 $client->url_parameters = false;
1371 $client->client_id = $ckey;
1372 $client->client_secret = $csecret;
1373 $client->access_token = $otoken;
1374 $client->access_token_secret = $osecret;
1376 $last_id = get_pconfig($uid,'pumpio','last_id');
1378 $url = 'https://'.$hostname.'/api/user/'.$username.'/inbox';
1381 $url .= '?since='.urlencode($last_id);
1383 if (pumpio_reachable($url))
1384 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
1389 $posts = array_reverse($user->items);
1392 foreach ($posts as $post) {
1393 $last_id = $post->id;
1394 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, true);
1398 foreach ($lastitems AS $item)
1399 pumpio_fetchallcomments($a, $uid, $item["uri"]);
1401 set_pconfig($uid,'pumpio','last_id', $last_id);
1404 function pumpio_getallusers(&$a, $uid) {
1405 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1406 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1407 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1408 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1409 $hostname = get_pconfig($uid, 'pumpio','host');
1410 $username = get_pconfig($uid, "pumpio", "user");
1412 $client = new oauth_client_class;
1413 $client->oauth_version = '1.0a';
1414 $client->authorization_header = true;
1415 $client->url_parameters = false;
1417 $client->client_id = $ckey;
1418 $client->client_secret = $csecret;
1419 $client->access_token = $otoken;
1420 $client->access_token_secret = $osecret;
1422 $url = 'https://'.$hostname.'/api/user/'.$username.'/following';
1424 if (pumpio_reachable($url))
1425 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1429 if ($users->totalItems > count($users->items)) {
1430 $url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
1432 if (pumpio_reachable($url))
1433 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
1438 foreach ($users->items AS $user)
1439 pumpio_get_contact($uid, $user);
1442 function pumpio_queue_hook(&$a,&$b) {
1444 $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
1445 dbesc(NETWORK_PUMPIO)
1450 require_once('include/queue_fn.php');
1452 foreach($qi as $x) {
1453 if($x['network'] !== NETWORK_PUMPIO)
1456 logger('pumpio_queue: run');
1458 $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
1459 WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
1467 //logger('pumpio_queue: fetching userdata '.print_r($userdata, true));
1469 $oauth_token = get_pconfig($userdata['uid'], "pumpio", "oauth_token");
1470 $oauth_token_secret = get_pconfig($userdata['uid'], "pumpio", "oauth_token_secret");
1471 $consumer_key = get_pconfig($userdata['uid'], "pumpio","consumer_key");
1472 $consumer_secret = get_pconfig($userdata['uid'], "pumpio","consumer_secret");
1474 $host = get_pconfig($userdata['uid'], "pumpio", "host");
1475 $user = get_pconfig($userdata['uid'], "pumpio", "user");
1479 if ($oauth_token AND $oauth_token_secret AND
1480 $consumer_key AND $consumer_secret) {
1481 $username = $user.'@'.$host;
1483 logger('pumpio_queue: able to post for user '.$username);
1485 $z = unserialize($x['content']);
1487 $client = new oauth_client_class;
1488 $client->oauth_version = '1.0a';
1489 $client->url_parameters = false;
1490 $client->authorization_header = true;
1491 $client->access_token = $oauth_token;
1492 $client->access_token_secret = $oauth_token_secret;
1493 $client->client_id = $consumer_key;
1494 $client->client_secret = $consumer_secret;
1496 if (pumpio_reachable($z['url']))
1497 $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
1502 $post_id = $user->object->id;
1503 logger('pumpio_queue: send '.$username.': success '.$post_id);
1504 if($post_id AND $iscomment) {
1505 logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$z['item']);
1506 q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
1511 remove_queue_item($x['id']);
1513 logger('pumpio_queue: send '.$username.': '.$url.' general error: ' . print_r($user,true));
1515 logger("pumpio_queue: Error getting tokens for user ".$userdata['uid']);
1518 logger('pumpio_queue: delayed');
1519 update_queue_time($x['id']);
1524 function pumpio_getreceiver(&$a, $b) {
1526 $receiver = array();
1528 if (!$b["private"]) {
1530 if(! strstr($b['postopts'],'pumpio'))
1533 $public = get_pconfig($b['uid'], "pumpio", "public");
1536 $receiver["to"][] = Array(
1537 "objectType" => "collection",
1538 "id" => "http://activityschema.org/collection/public");
1540 $cids = explode("><", $b["allow_cid"]);
1541 $gids = explode("><", $b["allow_gid"]);
1543 foreach ($cids AS $cid) {
1544 $cid = trim($cid, " <>");
1546 $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",
1549 dbesc(NETWORK_PUMPIO)
1553 $receiver["bcc"][] = Array(
1554 "displayName" => $r[0]["name"],
1555 "objectType" => "person",
1556 "preferredUsername" => $r[0]["nick"],
1557 "url" => $r[0]["url"]);
1560 foreach ($gids AS $gid) {
1561 $gid = trim($gid, " <>");
1563 $r = q("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network` ".
1564 "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d AND `group_member`.`uid` = %d ".
1565 "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
1568 dbesc(NETWORK_PUMPIO)
1571 foreach ($r AS $row)
1572 $receiver["bcc"][] = Array(
1573 "displayName" => $row["name"],
1574 "objectType" => "person",
1575 "preferredUsername" => $row["nick"],
1576 "url" => $row["url"]);
1580 if ($b["inform"] != "") {
1582 $inform = explode(",", $b["inform"]);
1584 foreach ($inform AS $cid) {
1585 if (substr($cid, 0, 4) != "cid:")
1588 $cid = str_replace("cid:", "", $cid);
1590 $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",
1593 dbesc(NETWORK_PUMPIO)
1597 $receiver["to"][] = Array(
1598 "displayName" => $r[0]["name"],
1599 "objectType" => "person",
1600 "preferredUsername" => $r[0]["nick"],
1601 "url" => $r[0]["url"]);
1609 function pumpio_fetchallcomments(&$a, $uid, $id) {
1610 $ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
1611 $csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
1612 $otoken = get_pconfig($uid, 'pumpio', 'oauth_token');
1613 $osecret = get_pconfig($uid, 'pumpio', 'oauth_token_secret');
1614 $hostname = get_pconfig($uid, 'pumpio','host');
1615 $username = get_pconfig($uid, "pumpio", "user");
1617 logger("pumpio_fetchallcomments: completing comment for user ".$uid." post id ".$id);
1619 $own_id = "https://".$hostname."/".$username;
1621 $self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1",
1624 // Fetching the original post
1625 $r = q("SELECT `extid` FROM `item` WHERE `uri` = '%s' AND `uid` = %d AND `extid` != '' LIMIT 1",
1633 $url = $r[0]["extid"];
1635 $client = new oauth_client_class;
1636 $client->oauth_version = '1.0a';
1637 $client->authorization_header = true;
1638 $client->url_parameters = false;
1640 $client->client_id = $ckey;
1641 $client->client_secret = $csecret;
1642 $client->access_token = $otoken;
1643 $client->access_token_secret = $osecret;
1645 logger("pumpio_fetchallcomments: fetching comment for user ".$uid." url ".$url);
1647 if (pumpio_reachable($url))
1648 $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $item);
1655 if ($item->likes->totalItems != 0) {
1656 foreach ($item->likes->items AS $post) {
1657 $like = new stdClass;
1658 $like->object = new stdClass;
1659 $like->object->id = $item->id;
1660 $like->actor = new stdClass;
1661 $like->actor->displayName = $item->displayName;
1662 $like->actor->preferredUsername = $item->preferredUsername;
1663 $like->actor->url = $item->url;
1664 $like->actor->image = $item->image;
1665 $like->generator = new stdClass;
1666 $like->generator->displayName = "pumpio";
1667 pumpio_dolike($a, $uid, $self, $post, $own_id, false);
1671 if ($item->replies->totalItems == 0)
1674 foreach ($item->replies->items AS $item) {
1675 if ($item->id == $id)
1678 // Checking if the comment already exists - Two queries for speed issues
1679 $r = q("SELECT extid FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
1687 $r = q("SELECT extid FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
1695 $post = new stdClass;
1696 $post->verb = "post";
1697 $post->actor = $item->author;
1698 $post->published = $item->published;
1699 $post->received = $item->updated;
1700 $post->generator = new stdClass;
1701 $post->generator->displayName = "pumpio";
1702 // To-Do: Check for public post
1704 unset($item->author);
1705 unset($item->published);
1706 unset($item->updated);
1708 $post->object = $item;
1710 logger("pumpio_fetchallcomments: posting comment ".$post->object->id." ".print_r($post, true));
1711 pumpio_dopost($a, $client, $uid, $self, $post, $own_id, false);
1716 function pumpio_reachable($url) {
1717 $data = z_fetch_url($url, false, $redirects, array('timeout'=>10));
1718 return(intval($data['return_code']) != 0);