+ $qi = q("SELECT * FROM `queue` WHERE `network` = '%s'",
+ dbesc(NETWORK_PUMPIO)
+ );
+ if(! count($qi))
+ return;
+
+ require_once('include/queue_fn.php');
+
+ foreach($qi as $x) {
+ if($x['network'] !== NETWORK_PUMPIO)
+ continue;
+
+ logger('pumpio_queue: run');
+
+ $r = q("SELECT `user`.* FROM `user` LEFT JOIN `contact` on `contact`.`uid` = `user`.`uid`
+ WHERE `contact`.`self` = 1 AND `contact`.`id` = %d LIMIT 1",
+ intval($x['cid'])
+ );
+ if(! count($r))
+ continue;
+
+ $userdata = $r[0];
+
+ //logger('pumpio_queue: fetching userdata '.print_r($userdata, true));
+
+ $oauth_token = get_pconfig($userdata['uid'], "pumpio", "oauth_token");
+ $oauth_token_secret = get_pconfig($userdata['uid'], "pumpio", "oauth_token_secret");
+ $consumer_key = get_pconfig($userdata['uid'], "pumpio","consumer_key");
+ $consumer_secret = get_pconfig($userdata['uid'], "pumpio","consumer_secret");
+
+ $host = get_pconfig($userdata['uid'], "pumpio", "host");
+ $user = get_pconfig($userdata['uid'], "pumpio", "user");
+
+ $success = false;
+
+ if ($oauth_token AND $oauth_token_secret AND
+ $consumer_key AND $consumer_secret) {
+ $username = $user.'@'.$host;
+
+ logger('pumpio_queue: able to post for user '.$username);
+
+ $z = unserialize($x['content']);
+
+ $client = new oauth_client_class;
+ $client->oauth_version = '1.0a';
+ $client->url_parameters = false;
+ $client->authorization_header = true;
+ $client->access_token = $oauth_token;
+ $client->access_token_secret = $oauth_token_secret;
+ $client->client_id = $consumer_key;
+ $client->client_secret = $consumer_secret;
+
+ if (pumpio_reachable($z['url']))
+ $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
+ else
+ $success = false;
+
+ if($success) {
+ $post_id = $user->object->id;
+ logger('pumpio_queue: send '.$username.': success '.$post_id);
+ if($post_id AND $iscomment) {
+ logger('pumpio_send '.$username.': Update extid '.$post_id." for post id ".$z['item']);
+ q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d",
+ dbesc($post_id),
+ intval($z['item'])
+ );
+ }
+ remove_queue_item($x['id']);
+ } else
+ logger('pumpio_queue: send '.$username.': '.$url.' general error: ' . print_r($user,true));
+ } else
+ logger("pumpio_queue: Error getting tokens for user ".$userdata['uid']);
+
+ if (!$success) {
+ logger('pumpio_queue: delayed');
+ update_queue_time($x['id']);
+ }
+ }
+}
+
+function pumpio_getreceiver(&$a, $b) {
+
+ $receiver = array();
+
+ if (!$b["private"]) {
+
+ if(! strstr($b['postopts'],'pumpio'))
+ return $receiver;
+
+ $public = get_pconfig($b['uid'], "pumpio", "public");
+
+ if ($public)
+ $receiver["to"][] = Array(
+ "objectType" => "collection",
+ "id" => "http://activityschema.org/collection/public");
+ } else {
+ $cids = explode("><", $b["allow_cid"]);
+ $gids = explode("><", $b["allow_gid"]);
+
+ foreach ($cids AS $cid) {
+ $cid = trim($cid, " <>");
+
+ $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",
+ intval($cid),
+ intval($b["uid"]),
+ dbesc(NETWORK_PUMPIO)
+ );
+
+ if (count($r)) {
+ $receiver["bcc"][] = Array(
+ "displayName" => $r[0]["name"],
+ "objectType" => "person",
+ "preferredUsername" => $r[0]["nick"],
+ "url" => $r[0]["url"]);
+ }
+ }
+ foreach ($gids AS $gid) {
+ $gid = trim($gid, " <>");
+
+ $r = q("SELECT `contact`.`name`, `contact`.`nick`, `contact`.`url`, `contact`.`network` ".
+ "FROM `group_member`, `contact` WHERE `group_member`.`gid` = %d AND `group_member`.`uid` = %d ".
+ "AND `contact`.`id` = `group_member`.`contact-id` AND `contact`.`network` = '%s'",
+ intval($gid),
+ intval($b["uid"]),
+ dbesc(NETWORK_PUMPIO)
+ );
+
+ foreach ($r AS $row)
+ $receiver["bcc"][] = Array(
+ "displayName" => $row["name"],
+ "objectType" => "person",
+ "preferredUsername" => $row["nick"],
+ "url" => $row["url"]);
+ }
+ }
+
+ if ($b["inform"] != "") {
+
+ $inform = explode(",", $b["inform"]);
+
+ foreach ($inform AS $cid) {
+ if (substr($cid, 0, 4) != "cid:")
+ continue;
+
+ $cid = str_replace("cid:", "", $cid);
+
+ $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",
+ intval($cid),
+ intval($b["uid"]),
+ dbesc(NETWORK_PUMPIO)
+ );
+
+ if (count($r)) {
+ $receiver["to"][] = Array(
+ "displayName" => $r[0]["name"],
+ "objectType" => "person",
+ "preferredUsername" => $r[0]["nick"],
+ "url" => $r[0]["url"]);
+ }
+ }
+ }
+
+ return $receiver;
+}