<?php
/**
* Name: pump.io Post Connector
- * Description: Post to pump.io
+ * Description: Bidirectional (posting, relaying and reading) connector for pump.io.
* Version: 0.2
* Author: Michael Vogel <http://pirati.ca/profile/heluecht>
*/
if ($application_name == "")
$application_name = $a->get_hostname();
+ $adminlist = explode(",", str_replace(" ", "", $a->config['admin_email']));
+
$params["type"] = "client_associate";
- $params["contacts"] = $a->config['admin_email'];
+ $params["contacts"] = $adminlist[0];
$params["application_type"] = "native";
$params["application_name"] = $application_name;
$params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
set_pconfig(local_user(),'pumpio','mirror',false);
set_pconfig(local_user(),'pumpio','post_by_default',false);
set_pconfig(local_user(),'pumpio','lastdate', 0);
+ set_pconfig(local_user(),'pumpio','last_id', '');
} else {
// filtering the username if it is filled wrong
$user = $_POST['pumpio_user'];
$title = trim($b['title']);
- //if ($title != '')
- // $title = "<h4>".$title."</h4>";
-
$content = bbcode($b['body'], false, false, 4);
// Enhance the way, videos are displayed
$username = $user.'@'.$host;
$url = 'https://'.$host.'/api/user/'.$user.'/feed';
- $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
+ if (pumpio_reachable($url))
+ $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
+ else
+ $success = false;
if($success) {
+
+ if ($user->generator->displayName)
+ set_pconfig($b["uid"], "pumpio", "application_name", $user->generator->displayName);
+
$post_id = $user->object->id;
logger('pumpio_send '.$username.': success '.$post_id);
if($post_id AND $iscomment) {
$url = 'https://'.$hostname.'/api/user/'.$username.'/feed';
- $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
+ if (pumpio_reachable($url))
+ $success = $client->CallAPI($url, 'POST', $params, array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
+ else
+ $success = false;
if($success)
logger('pumpio_action '.$username.' '.$action.': success '.$uri);
}
}
+function pumpio_sync(&$a) {
+ $r = q("SELECT * FROM `addon` WHERE `installed` = 1 AND `name` = 'pumpio'",
+ $plugin);
+
+ if (!count($r))
+ return;
-function pumpio_cron(&$a,$b) {
$last = get_config('pumpio','last_poll');
$poll_interval = intval(get_config('pumpio','poll_interval'));
set_config('pumpio','last_poll', time());
}
+function pumpio_cron(&$a,$b) {
+ //pumpio_sync($a);
+ proc_run("php","addon/pumpio/pumpio_sync.php");
+}
+
function pumpio_fetchtimeline(&$a, $uid) {
$ckey = get_pconfig($uid, 'pumpio', 'consumer_key');
$csecret = get_pconfig($uid, 'pumpio', 'consumer_secret');
$hostname = get_pconfig($uid, 'pumpio','host');
$username = get_pconfig($uid, "pumpio", "user");
- $application_name = get_config('pumpio', 'application_name');
-
+ // get the application name for the pump.io app
+ // 1st try personal config, then system config and fallback to the
+ // hostname of the node if neither one is set.
+ $application_name = get_pconfig( $uid, 'pumpio', 'application_name');
+ if ($application_name == "")
+ $application_name = get_config('pumpio', 'application_name');
if ($application_name == "")
$application_name = $a->get_hostname();
$username = $user.'@'.$host;
- $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
+ if (pumpio_reachable($url))
+ $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
+ else
+ $success = false;
if (!$success) {
logger('pumpio: error fetching posts for user '.$uid." ".$username." ".print_r($user, true));
if ($receiver->id == "http://activityschema.org/collection/public")
$public = true;
- if ($public AND !strstr($post->generator->displayName, $application_name)) {
+ if ($public AND !stristr($post->generator->displayName, $application_name)) {
require_once('include/html2bbcode.php');
$_SESSION["authenticated"] = true;
function pumpio_get_contact($uid, $contact) {
- $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
- dbesc(normalise_link($contact->url)));
-
- if (count($r) == 0)
- q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
- dbesc(normalise_link($contact->url)),
- dbesc($contact->displayName),
- dbesc($contact->preferredUsername),
- dbesc($contact->image->url));
- else
- q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
- dbesc($contact->displayName),
- dbesc($contact->preferredUsername),
- dbesc($contact->image->url),
+ if (function_exists("update_gcontact"))
+ update_gcontact(array("url" => $contact->url, "network" => NETWORK_PUMPIO, "generation" => 2,
+ "photo" => $contact->image->url, "name" => $contact->displayName, "hide" => true,
+ "nick" => $contact->preferredUsername, "location" => $contact->location->displayName,
+ "about" => $contact->summary, "addr" => str_replace("acct:", "", $contact->id)));
+ else {
+ // Old Code
+ $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
dbesc(normalise_link($contact->url)));
- if (DB_UPDATE_VERSION >= "1177")
- q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
- dbesc($contact->location->displayName),
- dbesc($contact->summary),
- dbesc(normalise_link($contact->url)));
+ if (count($r) == 0)
+ q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
+ dbesc(normalise_link($contact->url)),
+ dbesc($contact->displayName),
+ dbesc($contact->preferredUsername),
+ dbesc($contact->image->url));
+ else
+ q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
+ dbesc($contact->displayName),
+ dbesc($contact->preferredUsername),
+ dbesc($contact->image->url),
+ dbesc(normalise_link($contact->url)));
+
+ if (DB_UPDATE_VERSION >= "1177")
+ q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
+ dbesc($contact->location->displayName),
+ dbesc($contact->summary),
+ dbesc(normalise_link($contact->url)));
+ }
$r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `url` = '%s' LIMIT 1",
intval($uid), dbesc($contact->url));
$public = true;
$postarray = array();
+ $postarray['network'] = NETWORK_PUMPIO;
$postarray['gravity'] = 0;
$postarray['uid'] = $uid;
$postarray['wall'] = 0;
$postarray['body'] = "[share author='".$post->object->author->displayName.
"' profile='".$post->object->author->url.
"' avatar='".$post->object->author->image->url.
+ "' posted='".datetime_convert('UTC','UTC',$post->object->created).
"' link='".$post->links->self->href."']".$postarray['body']."[/share]";
} else {
// Let shares look like wall-to-wall posts
return false;
$top_item = item_store($postarray);
+ $postarray["id"] = $top_item;
if (($top_item == 0) AND ($post->verb == "update")) {
$r = q("UPDATE `item` SET `title` = '%s', `body` = '%s' , `changed` = '%s' WHERE `uri` = '%s' AND `uid` = %d",
if ($last_id != "")
$url .= '?since='.urlencode($last_id);
- $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
+ if (pumpio_reachable($url))
+ $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $user);
+ else
+ $success = false;
if ($user->items) {
$posts = array_reverse($user->items);
$url = 'https://'.$hostname.'/api/user/'.$username.'/following';
- $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
+ if (pumpio_reachable($url))
+ $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
+ else
+ $success = false;
if ($users->totalItems > count($users->items)) {
$url = 'https://'.$hostname.'/api/user/'.$username.'/following?count='.$users->totalItems;
- $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
+ if (pumpio_reachable($url))
+ $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $users);
+ else
+ $success = false;
}
foreach ($users->items AS $user)
$client->client_id = $consumer_key;
$client->client_secret = $consumer_secret;
- $success = $client->CallAPI($z['url'], 'POST', $z['post'], array('FailOnAccessError'=>true, 'RequestContentType'=>'application/json'), $user);
+ 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_fetchallcomments: fetching comment for user ".$uid." url ".$url);
- $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $item);
+ if (pumpio_reachable($url))
+ $success = $client->CallAPI($url, 'GET', array(), array('FailOnAccessError'=>true), $item);
+ else
+ $success = false;
if (!$success)
return;
}
}
+
+function pumpio_reachable($url) {
+ $data = z_fetch_url($url, false, $redirects, array('timeout'=>10));
+ return(intval($data['return_code']) != 0);
+}
+
/*
To-Do:
- edit own notes