*/
require('addon/pumpio/oauth/http.php');
require('addon/pumpio/oauth/oauth_client.php');
define('PUMPIO_DEFAULT_POLL_INTERVAL', 5); // given in minutes
function pumpio_install() {
register_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
register_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
register_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
register_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
register_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
register_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
register_hook('queue_predeliver', 'addon/pumpio/pumpio.php', 'pumpio_queue_hook');
}
function pumpio_uninstall() {
unregister_hook('post_local', 'addon/pumpio/pumpio.php', 'pumpio_post_local');
unregister_hook('notifier_normal', 'addon/pumpio/pumpio.php', 'pumpio_send');
unregister_hook('jot_networks', 'addon/pumpio/pumpio.php', 'pumpio_jot_nets');
unregister_hook('connector_settings', 'addon/pumpio/pumpio.php', 'pumpio_settings');
unregister_hook('connector_settings_post', 'addon/pumpio/pumpio.php', 'pumpio_settings_post');
unregister_hook('cron', 'addon/pumpio/pumpio.php', 'pumpio_cron');
unregister_hook('queue_predeliver', 'addon/pumpio/pumpio.php', 'pumpio_queue_hook');
}
function pumpio_module() {}
function pumpio_content(&$a) {
if(! local_user()) {
notice( t('Permission denied.') . EOL);
return '';
}
require_once("mod/settings.php");
settings_init($a);
if (isset($a->argv[1]))
switch ($a->argv[1]) {
case "connect":
$o = pumpio_connect($a);
break;
default:
$o = print_r($a->argv, true);
break;
}
else
$o = pumpio_connect($a);
return $o;
}
function pumpio_registerclient(&$a, $host) {
$url = "https://".$host."/api/client/register";
$params = array();
$application_name = get_config('pumpio', 'application_name');
if ($application_name == "")
$application_name = $a->get_hostname();
$adminlist = explode(",", str_replace(" ", "", $a->config['admin_email']));
$params["type"] = "client_associate";
$params["contacts"] = $adminlist[0];
$params["application_type"] = "native";
$params["application_name"] = $application_name;
$params["logo_url"] = $a->get_baseurl()."/images/friendica-256.png";
$params["redirect_uris"] = $a->get_baseurl()."/pumpio/connect";
logger("pumpio_registerclient: ".$url." parameters ".print_r($params, true), LOGGER_DEBUG);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_USERAGENT, "Friendica");
$s = curl_exec($ch);
$curl_info = curl_getinfo($ch);
if ($curl_info["http_code"] == "200") {
$values = json_decode($s);
logger("pumpio_registerclient: success ".print_r($values, true), LOGGER_DEBUG);
return($values);
}
logger("pumpio_registerclient: failed: ".print_r($curl_info, true), LOGGER_DEBUG);
return(false);
}
function pumpio_connect(&$a) {
// Start a session. This is necessary to hold on to a few keys the callback script will also need
session_start();
// Define the needed keys
$consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
$consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
$hostname = get_pconfig(local_user(), 'pumpio','host');
if ((($consumer_key == "") OR ($consumer_secret == "")) AND ($hostname != "")) {
logger("pumpio_connect: register client");
$clientdata = pumpio_registerclient($a, $hostname);
set_pconfig(local_user(), 'pumpio','consumer_key', $clientdata->client_id);
set_pconfig(local_user(), 'pumpio','consumer_secret', $clientdata->client_secret);
$consumer_key = get_pconfig(local_user(), 'pumpio','consumer_key');
$consumer_secret = get_pconfig(local_user(), 'pumpio','consumer_secret');
logger("pumpio_connect: ckey: ".$consumer_key." csecrect: ".$consumer_secret, LOGGER_DEBUG);
}
if (($consumer_key == "") OR ($consumer_secret == "")) {
logger("pumpio_connect: ".sprintf("Unable to register the client at the pump.io server '%s'.", $hostname));
$o .= sprintf(t("Unable to register the client at the pump.io server '%s'."), $hostname);
return($o);
}
// The callback URL is the script that gets called after the user authenticates with pumpio
$callback_url = $a->get_baseurl()."/pumpio/connect";
// Let's begin. First we need a Request Token. The request token is required to send the user
// to pumpio's login page.
// Create a new instance of the TumblrOAuth library. For this step, all we need to give the library is our
// Consumer Key and Consumer Secret
$client = new oauth_client_class;
$client->debug = 1;
$client->server = '';
$client->oauth_version = '1.0a';
$client->request_token_url = 'https://'.$hostname.'/oauth/request_token';
$client->dialog_url = 'https://'.$hostname.'/oauth/authorize';
$client->access_token_url = 'https://'.$hostname.'/oauth/access_token';
$client->url_parameters = false;
$client->authorization_header = true;
$client->redirect_uri = $callback_url;
$client->client_id = $consumer_key;
$client->client_secret = $consumer_secret;
if (($success = $client->Initialize())) {
if (($success = $client->Process())) {
if (strlen($client->access_token)) {
logger("pumpio_connect: otoken: ".$client->access_token." osecrect: ".$client->access_token_secret, LOGGER_DEBUG);
set_pconfig(local_user(), "pumpio", "oauth_token", $client->access_token);
set_pconfig(local_user(), "pumpio", "oauth_token_secret", $client->access_token_secret);
}
}
$success = $client->Finalize($success);
}
if($client->exit)
$o = 'Could not connect to pumpio. Refresh the page or try again later.';
if($success) {
logger("pumpio_connect: authenticated");
$o .= t("You are now authenticated to pumpio.");
$o .= '
'.t("return to the connector page").'';
} else {
logger("pumpio_connect: could not connect");
$o = 'Could not connect to pumpio. Refresh the page or try again later.';
}
return($o);
}
function pumpio_jot_nets(&$a,&$b) {
if(! local_user())
return;
$pumpio_post = get_pconfig(local_user(),'pumpio','post');
if(intval($pumpio_post) == 1) {
$pumpio_defpost = get_pconfig(local_user(),'pumpio','post_by_default');
$selected = ((intval($pumpio_defpost) == 1) ? ' checked="checked" ' : '');
$b .= '