Commit ad5a59a1 authored by lucha's avatar lucha

removed flattr plugin

parent df01da35
/*
Document : flattr
Created on : Dec 27, 2011, 2:28:15 AM
Author : Michael
*/
#icon-options-general.icon32.flattr {
background-image: url('img/flattr_button.png');
background-position: center center;
background-repeat: no-repeat;
}
th {
text-align: right;
vertical-align: top;
}
td {
vertical-align: top;
}
td input {
margin-right: 8px;
}
.flattr-wrap table.form-table li iframe,
.flattr-wrap table.form-table li img {
vertical-align: middle;
}
.flattr-wrap span.inactive {
color: gray;
}
.flattr-wrap span.active:before {
content : " ";
}
.flattr-wrap div#dialog {
display: none;
}
.flattr-server-check {
font-weight: bold;
}
.flattr-server-check.failed {
color: red;
}
.flattr-server-check.passed {
color: green;
}
.flattr-server-check.warn {
color: yellow;
}
#dialog ol li {
list-style-type :decimal-leading-zero;
}
\ No newline at end of file
This diff is collapsed.
<?php
require_once dirname(__FILE__) . '/lib/httpconnection.php';
require_once dirname(__FILE__) . '/lib/httpresponse.php';
require_once dirname(__FILE__) . '/lib/oauth2client.php';
\ No newline at end of file
# Sample code to use the Flattr v2 web API
to get up and running:
the oauth2client uses httpconnection and httpresponse which wrapps the curl php bindings.
if you are using a server with apt you can install it by
apt-get install php5-curl
## public api call
$client = new OAuth2Client(array('base_url' => 'https://api.flattr.com/rest/v2'));
$thing = $client->getParsed('/things/423405/');
var_dump($thing); // array
## minimal sample code
create an API key at https://flattr.com/apps
make sure you add a correct callback\_url it should be something like http://url-to-your-vhost/callback.php
Copy /config.template.php to /config.php and enter your api credentials.
Point a apache/lighttpd/nginx vhost to the minimal/ directory and restart.
Open http://url-to-your-vhost
## connect with flattr sample
sample app that implements a connect with flattr (based on sessions, no database, using coltrane framework)
see connect\_with\_flattr/
----
Documentation at [http://developers.flattr.net/](http://developers.flattr.net/)
Questions: [StackOverflow](http://stackoverflow.com/questions/tagged/flattr)
Feedback: [twitter](https://twitter.com/#!/flattr)
<?php
if ( isset($_GET['page'], $_GET['code']) && !isset($_GET['flattrJAX']) ) {
if ($_GET['page'] == "flattr/flattr.php") {
$flattr_domain = 'flattr.com';
$key = get_option('flattrss_api_key');
$sec = get_option('flattrss_api_secret');
$callback = urlencode(home_url()."/wp-admin/admin.php?page=flattr/flattr.php");
include_once 'flattr_client.php';
$basic_options = array(
'client_id' => $key,
'client_secret' => $sec,
'base_url' => 'https://api.' . $flattr_domain . '/rest/v2',
'site_url' => 'https://' . $flattr_domain,
'authorize_url' => 'https://' . $flattr_domain . '/oauth/authorize',
'access_token_url' => 'https://' . $flattr_domain . '/oauth/token',
'redirect_uri' => $callback,
'scopes' => 'thing+flattr',
'token_param_name' => 'Bearer',
'response_type' => 'code',
'grant_type' => 'authorization_code',
'refresh_token' => null,
'access_token' => null,
'code' => null,
'developer_mode' => false
);
$client = new OAuth2Client($basic_options);
try {
$access_token = $client->fetchAccessToken($_GET['code']);
$client = new OAuth2Client(array_merge($basic_options, array(
'access_token' => $access_token
)));
try {
$user = $client->getParsed('/user');
if (!isset($user['error'])) {
update_user_meta( get_current_user_id(), "user_flattrss_api_oauth_token", $access_token );
if (current_user_can('activate_plugins')) {
update_option('flattr_access_token', $access_token);
}
}
if (!current_user_can('activate_plugins')) {
header("Status: 307");
header("Location: ". home_url()."/wp-admin/users.php?page=flattr/flattr.php?user");
flush();
exit (0);
}
} catch (Exception $e) {}
} catch (Exception $e) {}
}
}
\ No newline at end of file
This diff is collapsed.
<?php
class HttpConnection {
// @param string $base url to the service, i.e http://example.com:80
protected $baseUrl;
// @param array $defaultOptions variable to hold default cURL options.
protected $defaultOptions;
/**
* constructor with some defaults
* @param string $baseUrl
*/
public function __construct($baseUrl = '', $params = array())
{
$this->baseUrl = $baseUrl;
$params = array_merge(array('headers' => array(),'basic_auth' => array()),$params);
if ( ! in_array('Expect:', $params['headers']))
{
$params['headers'][] = 'Expect:'; // lighty needs this
}
$this->defaultOptions = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $params['headers'],
CURLOPT_HEADER => 0,
CURLOPT_USERAGENT => 'curl',
);
if (!empty($params['verify_peer']) && $params['verify_peer'] === false) {
$this->verifyPeer($params['verify_peer']);
}
if ( !empty($params['basic_auth'])) {
$this->setBasicAuth($params['basic_auth']['user'],$params['basic_auth']['password']);
}
}
public function setBasicAuth($user,$password)
{
$this->defaultOptions[CURLOPT_USERPWD] = $user.':'.$password;
}
public function verifyPeer( $verify = true )
{
$this->defaultOptions[CURLOPT_SSL_VERIFYPEER] = $verify;
$this->defaultOptions[CURLOPT_SSL_VERIFYHOST] = $verify;
}
/**
* @param string $resourceUrl
* @param array $headers (optional)
* @return boolean
*/
public function get($resourceUrl, $opts = array())
{
$opts = $this->parseOptions($opts);
if (!empty($opts['params']))
{
$resourceUrl .= (strpos($responseUrl,'?') === false) ? '?' .$opts['params'] : '&'.$opts['params'];
}
$response = new HttpResponse();
$ch = curl_init();
$h = $this->mergeHeaders($opts['headers']);
$merged_curlOptions = array(
CURLOPT_URL => $this->baseUrl.$resourceUrl,
CURLOPT_HTTPHEADER => $h,
CURLOPT_HEADERFUNCTION => array(&$response,'readHeader')
);
// can't use array_merge or the $array + $array since headers is nested array
$curlOptions = $this->defaultOptions;
foreach($merged_curlOptions as $k => $v) {
$curlOptions[$k] = $v;
}
curl_setopt_array($ch,$curlOptions);
$response->body = curl_exec($ch);
$response->errors = curl_error($ch);
$response->info = curl_getinfo($ch);
curl_close($ch);
return $response;
}
/**
* @param string $resourceUrl
* @param mixed $post_body array or string.
* @param array $headers (optional)
* @return boolean
*/
public function post($resourceUrl, $opts= array())
{
$opts = $this->parseOptions($opts);
$response = new HttpResponse();
$ch = curl_init();
$h = $this->mergeHeaders($opts['headers']);
$merged_curlOptions = array(
CURLOPT_URL => $this->baseUrl.$resourceUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $opts['params'],
CURLOPT_HTTPHEADER => $h,
CURLOPT_HEADERFUNCTION => array(&$response,'readHeader')
);
// can't use array_merge or the $array + $array since headers is nested array
$curlOptions = $this->defaultOptions;
foreach($merged_curlOptions as $k => $v) {
$curlOptions[$k] = $v;
}
curl_setopt_array($ch,$curlOptions);
$response->body = curl_exec($ch);
$response->errors = curl_error($ch);
$response->info = curl_getinfo($ch);
curl_close($ch);
return $response;
}
/**
* merge headers with the defaults from curl
* @param array $headers
* @return array $currentHeaders
*/
protected function mergeHeaders( $headers = array() )
{
$currentHeaders = $this->defaultOptions[CURLOPT_HTTPHEADER];
foreach ($headers as $k => $h) {
$header = $k . ': ' . $h;
if ($this->shouldAddRequestHeader($header, $currentHeaders)) {
$currentHeaders[] = $header;
}
}
return $currentHeaders;
}
public function shouldAddRequestHeader($header, &$currentHeaders)
{
if (!in_array($header, $currentHeaders)) {
return true;
} else {
return false;
}
}
protected function parseOptions($options) {
$options = array_merge(array('params' => '', 'headers' => array()), $options);
if ( is_array($options['params']) )
{
$options['params'] = http_build_query($options['params']);
}
return $options;
}
}
<?php
class HttpResponse
{
public $body, $responseCode = null;
public $errors = array();
public $info = array();
public $responseHeaders = array();
public function __construct()
{
}
public function readHeader($ch, $header)
{
list($key, $value) = self::parseHeader($header);
if(empty($value)) {
return strlen($header);
}
if ($key == 0 && strpos($value,'HTTP/1.1 ') === 0) {
$key = 'Status';
$value = str_replace('HTTP/1.1 ','',$value);
$this->responseCode = (int)substr($value,0,3); // cache the response code
}
$this->responseHeaders[$key] = $value;
// !important, return bytes read, curl will fail otherwise
return strlen($header);
}
public static function parseHeader( $header = '' )
{
$exploded = explode(':', $header);
$r = (count($exploded) > 1) ? array() : array(0);
foreach($exploded as $e) {
$r[] = trim($e);
}
return $r;
}
}
<?php
class OAuth2Client
{
protected $settings = array();
protected $http = null;
/**
* creates the oauth2client object with some defaults
*
* @param array $settings overrides default settings.
* @return OAuth2Client
*/
public function __construct( $settings = array() )
{
$this->settings = array_merge(array(
'client_id' => null,
'client_secret' => null,
'base_url' => null,
'site_url' => null,
'authorize_url' => null,
'access_token_url' => null,
'scopes' => null,
'token_param_name' => 'Bearer',
'redirect_uri' => null,
'response_type' => 'code',
'grant_type' => 'authorization_code',
'access_token' => null,
'refresh_token' => null,
'developer_mode' => false,
'format' => 'application/json',
), $settings);
$this->http = new HttpConnection();
if ($this->settings['developer_mode']) {
$this->http->verifyPeer( false );
}
}
/**
* assemble the authorize_url link
*
* @return string authorize url endpoint
*/
public function authorizeUrl()
{
return $this->settings['authorize_url'].'?'.
'client_id='.$this->settings['client_id'].'&'.
'response_type='.$this->settings['response_type'].'&'.
'redirect_uri='.$this->settings['redirect_uri'].'&'.
'scope='.$this->settings['scopes'];
}
/**
* exchange $code for an access_token
*
* @param string $code
* @throws exception
* @return mixed string or false
*/
public function fetchAccessToken($code, $authInParams = false)
{
$params = array(
'code' => $code,
'grant_type' => $this->settings['grant_type'],
);
// @todo if more params is needed, make a loop...
if (!empty($this->settings['redirect_uri'])) {
$params['redirect_uri'] = $this->settings['redirect_uri'];
}
$headers = $this->headers();
if ( $authInParams) {
$headers = $this->headers(false);
$params['client_id'] = $this->settings['client_id'];
$params['client_secret'] = $this->settings['client_secret'];
}
$this->logRequest('POST '.$this->settings['access_token_url'],array(
'params' => $params,
'headers' => $headers,
));
$response = $this->http->post( $this->settings['access_token_url'],
array(
'params' => $params,
'headers' => $headers,
)
);
$this->logResponse($response);
if (!empty($response->body)) {
$prms = self::parseResponse($response);
if(!empty($prms["error"])) {
throw new Exception("Authentication error: " . $prms["error"]);
}
if ( strpos($response->info['http_code'], '20') !== 0 )
{
throw new Exception('failed to create access_token');
}
if ( !empty($prms['token_type']) ) {
$this->settings['token_type'] = $prms['token_type'];
}
$this->settings['access_token'] = $prms['access_token'];
return $prms['access_token'];
} else {
throw new Exception('No body received from POST access_token');
}
return false;
}
/**
* perform a GET api call
*
* @param string $path
* @return HttpResponse
*/
public function get($path)
{
$this->logRequest(
'GET '.$this->uriFor($path),
array(
'headers' => $this->headers(),
)
);
$response = $this->http->get(
$this->uriFor($path),
array(
'headers' => $this->headers(),
)
);
$this->logResponse($response);
return $response;
}
/**
* perform a GET api call, wrapped in OAuth2Client::parseResponse
*
* @param string $path
* @return mixed array or string
*/
public function getParsed($path)
{
$response = $this->get($path);
if (!empty($response->body) ) {
return self::parseResponse($response);
} else {
return array(
'error' => 'did not get a body from '.$uri
);
}
}
/**
* perform a POST api call