forked from Chaospott/site
added a few articles, added media
This commit is contained in:
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
class Dummy extends Hook
|
||||
{
|
||||
public function doHook(Post $post)
|
||||
{
|
||||
error_log('Hooked ' . $post->title);
|
||||
}
|
||||
}
|
70
_site/tmp/var/www/chaospott.de/example-hooks/post_fb.php
Normal file
70
_site/tmp/var/www/chaospott.de/example-hooks/post_fb.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
// Auto-posting requires Facebook's PHP API. It can be found on GitHub.
|
||||
// https://github.com/facebook/facebook-php-sdk
|
||||
|
||||
require_once(dirname(__FILE__) . '/facebook-php-sdk/src/facebook.php');
|
||||
|
||||
// Obtaining proper credentials will take a few steps, listed below.
|
||||
// More here: http://jeremygibbs.com/2012/02/11/how-to-autopost-facebook
|
||||
//
|
||||
// 1.) Create Facebook App
|
||||
// Go to Facebook app page: https://developers.facebook.com/apps
|
||||
// and click "Create New App" button in top right corner. Choose a
|
||||
// name (you can leave namespace empty) and continue. Next, you will
|
||||
// customize the app. Write down App ID and App Secret located in header.
|
||||
// In basic info section, enter email/domain from which you will be
|
||||
// posting. Finally, declare how the app integrates with Facebook. You
|
||||
// will likely want the website option. Enter your site's URL and save.
|
||||
//
|
||||
// 2.) Obtain Credentials
|
||||
// You now must authorize your app to post content on your behalf.
|
||||
// Normally, once an app is authorized, the user must be logged in to
|
||||
// verify permissions. You can avoid this by requesting a long-term
|
||||
// token. To do this, you will create a simple php webpage - grab the
|
||||
// example here: https://gist.github.com/2114528. Visit the webpage in
|
||||
// your app, which will forward you to Facebook. You will grant
|
||||
// permissions and be redirected back to the webpage. That page will
|
||||
// will display your offline access token. Save this token, and put
|
||||
// it with your App ID and App Secret. Now you are good to go.
|
||||
|
||||
class FacebookCredentials
|
||||
{
|
||||
public static $app_id = 'YOUR APP ID';
|
||||
public static $app_secret = 'YOUR APP SECRET';
|
||||
public static $access_token = 'YOUR ACCESS TOKEN';
|
||||
}
|
||||
|
||||
function construct_post_text(array $post)
|
||||
{
|
||||
$post_txt = $post['post-title'];
|
||||
$post_url = $post['post-absolute-permalink'];
|
||||
$post_txt .= ' - ' . $post_url;
|
||||
|
||||
if (isset($post['link'])) $post_txt = "\xE2\x86\x92 " . $post_txt;
|
||||
return $post_txt;
|
||||
}
|
||||
|
||||
function post_facebook_link_to_post(array $post)
|
||||
{
|
||||
$post_text = construct_post_text($post);
|
||||
|
||||
$facebook = new Facebook(array(
|
||||
'appId' => FacebookCredentials::$app_id,
|
||||
'secret' => FacebookCredentials::$app_secret,
|
||||
'cookie' => true));
|
||||
|
||||
$req = array(
|
||||
'access_token' => FacebookCredentials::$access_token,
|
||||
'message' => $post_text);
|
||||
|
||||
$res = $facebook->api('/me/feed', 'POST', $req);
|
||||
}
|
||||
|
||||
class fb extends Hook
|
||||
{
|
||||
public function doHook(Post $post)
|
||||
{
|
||||
post_facebook_link_to_post($post->array_for_template());
|
||||
}
|
||||
}
|
||||
?>
|
158
_site/tmp/var/www/chaospott.de/example-hooks/post_tumblr.php
Normal file
158
_site/tmp/var/www/chaospott.de/example-hooks/post_tumblr.php
Normal file
@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
// Paste in your values for these strings:
|
||||
class TumblrPostHookCredentials
|
||||
{
|
||||
public static $account_email = '';
|
||||
public static $account_password = '';
|
||||
|
||||
// The part that goes here: (blog name).tumblr.com
|
||||
public static $blog_name = '';
|
||||
}
|
||||
|
||||
// Test your credentials by executing this file alone from the command line:
|
||||
// php -f post_tumblr.php
|
||||
|
||||
// Edit this function to customize the generated Tumblr posts.
|
||||
// The $post array is just like the $content['post'] arrays in the templates.
|
||||
// This function MUST return an associative array of Tumblr post variables documented here:
|
||||
// http://www.tumblr.com/docs/en/api/v1#api_write
|
||||
//
|
||||
// Or return false to skip posting this to Tumblr.
|
||||
//
|
||||
function tumblr_post_for_post(array $post)
|
||||
{
|
||||
if (isset($post['link'])) {
|
||||
return false;
|
||||
} else {
|
||||
return array(
|
||||
'type' => 'link',
|
||||
'name' => $post['post-title'],
|
||||
'url' => $post['post-absolute-permalink'],
|
||||
'description' => isset($post['sharing-summary']) ? $post['sharing-summary'] : ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// ======== You probably don't want to edit anything below this line =========
|
||||
|
||||
function tumblr_http_build_query_raw(array $arr)
|
||||
{
|
||||
$out = array();
|
||||
foreach ($arr as $name => $val) $out[] = rawurlencode($name) . '=' . rawurlencode($val);
|
||||
return implode('&', $out);
|
||||
}
|
||||
|
||||
function post_tumblr_link_to_post(array $post_array_for_template)
|
||||
{
|
||||
$tumblr_post = tumblr_post_for_post($post_array_for_template);
|
||||
if (! $tumblr_post) {
|
||||
error_log("Not posting this to Tumblr");
|
||||
return;
|
||||
}
|
||||
|
||||
$request_data = tumblr_http_build_query_raw(
|
||||
array_merge(
|
||||
array(
|
||||
'email' => TumblrPostHookCredentials::$account_email,
|
||||
'password' => TumblrPostHookCredentials::$account_password,
|
||||
'group' => TumblrPostHookCredentials::$blog_name . '.tumblr.com',
|
||||
'generator' => 'Second Crack'
|
||||
),
|
||||
$tumblr_post
|
||||
)
|
||||
);
|
||||
|
||||
$c = curl_init('https://www.tumblr.com/api/write');
|
||||
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 5);
|
||||
curl_setopt($c, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($c, CURLOPT_FAILONERROR, 0);
|
||||
curl_setopt($c, CURLOPT_POST, true);
|
||||
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
|
||||
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
|
||||
$result = curl_exec($c);
|
||||
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
|
||||
curl_close($c);
|
||||
|
||||
// Check for success
|
||||
if ($status == 201) {
|
||||
error_log("Posted to Tumblr: http://" . TumblrPostHookCredentials::$blog_name . ".tumblr.com/post/$result");
|
||||
} else {
|
||||
error_log("Posting to Tumblr failed (HTTP $status): $response");
|
||||
}
|
||||
}
|
||||
|
||||
$command_line_test_mode = isset($_SERVER['argv'][0]) && substr($_SERVER['argv'][0], -15) == 'post_tumblr.php';
|
||||
if ($command_line_test_mode) {
|
||||
// test auth
|
||||
|
||||
$c = curl_init('https://www.tumblr.com/api/authenticate');
|
||||
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 5);
|
||||
curl_setopt($c, CURLOPT_TIMEOUT, 10);
|
||||
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($c, CURLOPT_FAILONERROR, 0);
|
||||
curl_setopt($c, CURLOPT_POST, true);
|
||||
curl_setopt($c, CURLOPT_POSTFIELDS,
|
||||
tumblr_http_build_query_raw(array(
|
||||
'email' => TumblrPostHookCredentials::$account_email,
|
||||
'password' => TumblrPostHookCredentials::$account_password,
|
||||
))
|
||||
);
|
||||
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
|
||||
$result = curl_exec($c);
|
||||
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
|
||||
curl_close($c);
|
||||
|
||||
// Check for success
|
||||
libxml_use_internal_errors(true);
|
||||
if ($status == 200 &&
|
||||
($dom = new DOMDocument()) &&
|
||||
$dom->loadXML($result) &&
|
||||
($tumblelogs = $dom->getElementsByTagName('tumblelog'))
|
||||
) {
|
||||
$permitted = false;
|
||||
$names = array();
|
||||
foreach ($tumblelogs as $tumblelog) {
|
||||
if (! ($name = $tumblelog->getAttribute('name')) ) continue;
|
||||
if ($name == TumblrPostHookCredentials::$blog_name) {
|
||||
$permitted = true;
|
||||
break;
|
||||
}
|
||||
$names[] = $name;
|
||||
}
|
||||
|
||||
if ($permitted) {
|
||||
echo "\nSuccessfully authenticated for " . TumblrPostHookCredentials::$blog_name . "\n";
|
||||
} else {
|
||||
echo "\nTumblrPostHookCredentials::\$blog_name is not set to one of your blog names.\nBlog names available on your account:\n\n\t" . implode("\n\t", $names) . "\n\n";
|
||||
exit(1);
|
||||
}
|
||||
} else if ($status == 403) {
|
||||
echo "\nInvalid Tumblr account email address or password.\n\n";
|
||||
exit(1);
|
||||
} else {
|
||||
echo "\nTumblr returned an error:\n$result\n\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'post') {
|
||||
post_tumblr_link_to_post(array(
|
||||
'post-title' => 'Test post title',
|
||||
'post-absolute-permalink' => 'http://www.tumblr.com/',
|
||||
'sharing-summary' => 'This is a post about nothing.',
|
||||
));
|
||||
echo "\n";
|
||||
} else {
|
||||
echo "Re-run with argument 'post' to create a test post on Tumblr.\n\n";
|
||||
}
|
||||
} else {
|
||||
class Tumblr extends Hook
|
||||
{
|
||||
public function doHook(Post $post)
|
||||
{
|
||||
post_tumblr_link_to_post($post->array_for_template());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
233
_site/tmp/var/www/chaospott.de/example-hooks/post_twitter.php
Normal file
233
_site/tmp/var/www/chaospott.de/example-hooks/post_twitter.php
Normal file
@ -0,0 +1,233 @@
|
||||
<?php
|
||||
|
||||
// Create an "app" from the Twitter account you want to post to:
|
||||
// https://dev.twitter.com/
|
||||
//
|
||||
// From your app's Details page, request an Access Token (bottom).
|
||||
// Reload periodically until you have it.
|
||||
// Paste in your values for these strings:
|
||||
|
||||
class TwitterPostHookCredentials
|
||||
{
|
||||
public static $consumer_key = '';
|
||||
public static $consumer_secret = '';
|
||||
public static $access_token = '';
|
||||
public static $access_token_secret = '';
|
||||
}
|
||||
|
||||
// Test your credentials by executing this file alone from the command line:
|
||||
// php -f post_twitter.php
|
||||
|
||||
// Edit this function to customize the formatting of the posted tweets.
|
||||
// The $post array is just like the $content['post'] arrays in the templates.
|
||||
// You can even change the posted URL by overwriting $url_to_use.
|
||||
//
|
||||
// The string returned by this function will have a space and URL appended.
|
||||
// $max_characters accounts for this, and is the most UTF-8 characters YOU can
|
||||
// return from this function.
|
||||
//
|
||||
// The twitter_summarize($text, $max_characters) function is available. It
|
||||
// will trim a string at the nearest word boundary and append an ellipsis to
|
||||
// fit within the number of characters if it's too long.
|
||||
//
|
||||
function tweet_text_before_url_for_post(array $post, $max_characters, &$url_to_use)
|
||||
{
|
||||
$title = $post['post-title'];
|
||||
if (isset($post['link'])) $title = "\xE2\x86\x92 " . $title; // right arrow
|
||||
return twitter_summarize($title, $max_characters);
|
||||
}
|
||||
|
||||
|
||||
// ======== You probably don't want to edit anything below this line =========
|
||||
|
||||
class ExtremelyBasicSelfContainedOAuthConsumer
|
||||
{
|
||||
public $consumer_key;
|
||||
public $consumer_secret;
|
||||
public $token = '';
|
||||
public $token_secret = '';
|
||||
|
||||
public $request_timeout_seconds = 10;
|
||||
|
||||
public static function hmac_sha1($data, $key)
|
||||
{
|
||||
if (strlen($key) > 64) $key = pack('H40', sha1($key));
|
||||
if (strlen($key) < 64) $key = str_pad($key, 64, chr(0));
|
||||
$ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
|
||||
$opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
|
||||
return sha1($opad . pack('H40', sha1($ipad . $data)), true);
|
||||
}
|
||||
|
||||
public function signature($request_method, $target_url, $post_params = array())
|
||||
{
|
||||
if ( ($qs = @parse_url($target_url, PHP_URL_QUERY)) ) {
|
||||
parse_str($qs, $get_params);
|
||||
$post_params = array_merge($get_params, $post_params);
|
||||
}
|
||||
if (isset($post_params['oauth_signature'])) unset($post_params['oauth_signature']);
|
||||
ksort($post_params);
|
||||
|
||||
if (false !== ($cutpos = strpos($target_url, '?')) ) $target_url = substr($target_url, 0, $cutpos);
|
||||
$parts = array($request_method, rawurlencode($target_url));
|
||||
$vars = array();
|
||||
foreach ($post_params as $name => $val) $vars[] = rawurlencode($name) . '=' . rawurlencode($val);
|
||||
$parts[] = rawurlencode(implode('&', $vars));
|
||||
$signature_base_string = implode('&', $parts);
|
||||
return base64_encode(self::hmac_sha1($signature_base_string, $this->consumer_secret . '&' . ($this->token_secret ? $this->token_secret : '')));
|
||||
}
|
||||
|
||||
public function request_parameters($request_method, $target_url, $post_params = array())
|
||||
{
|
||||
$params = array(
|
||||
'oauth_consumer_key' => $this->consumer_key,
|
||||
'oauth_signature_method' => 'HMAC-SHA1',
|
||||
'oauth_timestamp' => time(),
|
||||
'oauth_nonce' => sha1($this->consumer_secret . microtime(true) . $this->token_secret),
|
||||
'oauth_version' => '1.0'
|
||||
);
|
||||
if (strlen($this->token)) $params['oauth_token'] = $this->token;
|
||||
|
||||
$post_params = array_merge($post_params, $params);
|
||||
$sig = $this->signature($request_method, $target_url, $post_params);
|
||||
$params['oauth_signature'] = $sig;
|
||||
return $params;
|
||||
}
|
||||
|
||||
public function authorization_header_value($oauth_request_parameters)
|
||||
{
|
||||
$value = 'OAuth ';
|
||||
foreach ($oauth_request_parameters as $key => $val) {
|
||||
$value .= $key . '="' . rawurlencode($val) . '", ';
|
||||
}
|
||||
$value = rtrim($value, ', ');
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function http_build_query_raw(array $arr)
|
||||
{
|
||||
$out = array();
|
||||
foreach ($arr as $name => $val) $out[] = rawurlencode($name) . '=' . rawurlencode($val);
|
||||
return implode('&', $out);
|
||||
}
|
||||
|
||||
public function request($method, $endpoint, $params = array())
|
||||
{
|
||||
$oauth_parameters = $this->request_parameters($method, $endpoint, $params);
|
||||
$auth = $this->authorization_header_value($oauth_parameters);
|
||||
|
||||
$curl = curl_init($endpoint);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->request_timeout_seconds);
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, $this->request_timeout_seconds);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_FAILONERROR, 0);
|
||||
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: ' . $auth, 'Expect:'));
|
||||
if ($method == 'POST') {
|
||||
curl_setopt($curl, CURLOPT_POST, true);
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, self::http_build_query_raw($params));
|
||||
}
|
||||
|
||||
$data = curl_exec($curl);
|
||||
curl_close($curl);
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
function twitter_summarize($text, $length, $ellipsis = "\xE2\x80\xA6" /* UTF-8 … */)
|
||||
{
|
||||
if ($length == 0) return $text;
|
||||
if (mb_strlen($text, 'UTF-8') <= $length) return $text;
|
||||
|
||||
$word_delimiter = ' ';
|
||||
$word_delimiter_mblen = 1;
|
||||
|
||||
$length -= mb_strlen($ellipsis, 'UTF-8');
|
||||
$cut_str = mb_substr($text, 0, $length + $word_delimiter_mblen, 'UTF-8');
|
||||
if (mb_substr($cut_str, 0 - $word_delimiter_mblen) != $word_delimiter) {
|
||||
$cut_str = mb_substr($cut_str, 0, 0 - $word_delimiter_mblen);
|
||||
}
|
||||
|
||||
$split_pos = $cut_str ? mb_strrpos($cut_str, $word_delimiter, 'UTF-8') : false;
|
||||
if ($split_pos) {
|
||||
return mb_substr($text, 0, $split_pos, 'UTF-8') . $ellipsis;
|
||||
} else {
|
||||
return $cut_str . $ellipsis;
|
||||
}
|
||||
}
|
||||
|
||||
function tweet_link_to_post(array $post_array_for_template)
|
||||
{
|
||||
$oauth = new ExtremelyBasicSelfContainedOAuthConsumer();
|
||||
$oauth->consumer_key = TwitterPostHookCredentials::$consumer_key;
|
||||
$oauth->consumer_secret = TwitterPostHookCredentials::$consumer_secret;
|
||||
$oauth->token = TwitterPostHookCredentials::$access_token;
|
||||
$oauth->token_secret = TwitterPostHookCredentials::$access_token_secret;
|
||||
|
||||
$short_url_length = 24; // Big, safe default (at time of writing, Twitter's value is actually 20)
|
||||
try {
|
||||
$config = json_decode($oauth->request('GET', 'http://api.twitter.com/1/help/configuration.json'), true);
|
||||
if (isset($config['short_url_length'])) $short_url_length = intval($config['short_url_length']);
|
||||
if ($short_url_length < 16) $short_url_length = 24; // sanity check
|
||||
} catch (Exception $e) { }
|
||||
|
||||
$url_to_use = $post_array_for_template['post-absolute-permalink'];
|
||||
$tweet_text = tweet_text_before_url_for_post(
|
||||
$post_array_for_template,
|
||||
140 - ($short_url_length + 1 /* the space before the URL */),
|
||||
$url_to_use
|
||||
);
|
||||
|
||||
$tweet_text .= ' ' . $url_to_use;
|
||||
$response = $oauth->request(
|
||||
'POST', 'https://api.twitter.com/1/statuses/update.json',
|
||||
array('trim_user' => 'true', 'status' => $tweet_text)
|
||||
);
|
||||
|
||||
$post = @json_decode($response, true);
|
||||
if ($post && isset($post['id_str']) && isset($post['text'])) {
|
||||
error_log("Posted to Twitter: [{$post['text']}]");
|
||||
} else {
|
||||
error_log("Posting to Twitter failed: $response");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$command_line_test_mode = isset($_SERVER['argv'][0]) && substr($_SERVER['argv'][0], -16) == 'post_twitter.php';
|
||||
if ($command_line_test_mode) {
|
||||
$oauth = new ExtremelyBasicSelfContainedOAuthConsumer();
|
||||
$oauth->consumer_key = TwitterPostHookCredentials::$consumer_key;
|
||||
$oauth->consumer_secret = TwitterPostHookCredentials::$consumer_secret;
|
||||
$oauth->token = TwitterPostHookCredentials::$access_token;
|
||||
$oauth->token_secret = TwitterPostHookCredentials::$access_token_secret;
|
||||
|
||||
$response = $oauth->request('GET', 'https://api.twitter.com/1/account/verify_credentials.json');
|
||||
$user = @json_decode($response, true);
|
||||
|
||||
if ($user && isset($user['error'])) {
|
||||
echo "\nTwitter returned an error: {$user['error']}\n\n";
|
||||
exit(1);
|
||||
} else if ($user && isset($user['name']) && isset($user['screen_name'])) {
|
||||
echo "\nSuccessfully authenticated as @{$user['screen_name']} ({$user['name']})\n";
|
||||
} else {
|
||||
echo "\nGot unrecognized response from Twitter:\n$response\n\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (isset($_SERVER['argv'][1]) && $_SERVER['argv'][1] == 'post') {
|
||||
tweet_link_to_post(array(
|
||||
'post-title' => 'Test post title',
|
||||
'post-absolute-permalink' => 'http://twitter.com/',
|
||||
));
|
||||
echo "\n";
|
||||
} else {
|
||||
echo "Re-run with argument 'post' to create a test post on Twitter.\n\n";
|
||||
}
|
||||
} else {
|
||||
class Twitter extends Hook
|
||||
{
|
||||
public function doHook(Post $post)
|
||||
{
|
||||
tweet_link_to_post($post->array_for_template());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user