additional_plugins/serendipity_plugin_twitter/classes/UrlShortener.php

304 lines
11 KiB
PHP

<?php
/*
* Created on 08.06.2009
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include dirname(__FILE__) . '/json.php4.include.php';
class UrlShortener {
// This login is a generic that fails in most cases because of exceeded ratio limits.
var $bitly_login = 'bitlyapidemo';
var $bitly_apikey = 'R_0da49e0a9118ff35f52f629d2d71bf07';
var $piratly_apikey = "0"; // This is the generic API token representing anonymous user
var $yourls_url = 'http://www.yourls.org';
var $yourls_apikey = 'signature';
function setBitlyLogin($login, $apikey) {
if (empty($login) || empty($apikey)) return;
$this->bitly_login = $login;
$this->bitly_apikey = $apikey;
}
function setYourlsLogin($yourl, $apikey) {
if (empty($yourl) || empty($apikey)) return;
$this->yourls_url = $yourl;
$this->yourls_apikey = $apikey;
}
function setPiratlyToken($apitoken) {
if (empty($apitoken)) return;
$this->piratly_apikey = $apitoken;
}
/**
* Fills up the shorturls hash with shorturls identified by service name.
*/
function put_shorturl($service, $url, &$shorturls) {
global $serendipity;
switch ($service) {
case 'linktrimmer':
if (class_exists('serendipity_event_linktrimmer')) {
$res = serendipity_db_query("SELECT value FROM `serendipity_config` WHERE name LIKE 'serendipity_event_linktrimmer:%/prefix';", true, 'assoc');
if (is_array($res) && !empty($res['value'])) {
$lt_prefix = "/" . trim($res['value']) . "/";
}
$res = serendipity_db_query("SELECT value FROM `serendipity_config` WHERE name LIKE 'serendipity_event_linktrimmer:%/domain';", true, 'assoc');
if (is_array($res) && !empty($res['value'])) {
$lt_domain = trim($res['value']);
}
if (empty($lt_domain)) $lt_domain = $serendipity['baseURL'];
if (empty($lt_prefix)) $lt_prefix = '/s/';
if ($lt_domain == $serendipity['baseURL']) {
$shorturls['linktrimmer'] = $lt_domain. $lt_prefix .trim(serendipity_event_linktrimmer::lookup($url));
} else {
$shorturls['linktrimmer'] = $lt_domain . trim(serendipity_event_linktrimmer::lookup($url));
}
}
break;
case 'raw':
$shorturls['raw'] = $url;
break;
case 'tinyurl':
UrlShortener::shorten_via_tinyurl( $url, $shorturls );
break;
case '7ax.de':
UrlShortener::shorten_via_7ax( $url, $shorturls );
break;
case 'isgd':
UrlShortener::shorten_via_isgd( $url, $shorturls );
break;
case 'bitly':
UrlShortener::shorten_via_bitly( $url, $shorturls );
break;
case 'yourls':
UrlShortener::shorten_via_yourls( $url, $shorturls );
break;
case 'jmp':
UrlShortener::shorten_via_jmp( $url, $shorturls );
break;
case 'delivr':
UrlShortener::shorten_via_delivr( $url, $shorturls );
break;
case 'twurl':
UrlShortener::shorten_via_twurl( $url, $shorturls );
break;
case 'piratly':
UrlShortener::shorten_via_piratly( $url, $shorturls );
break;
// old removed service
case 'snipr':
case 'tr.im':
case 'cli.gs':
UrlShortener::shorten_via_7ax( $url, $shorturls );
break;
}
}
/**
* Shorten an URL via a simple HTTP Get returning the short url only
* @param array shorturls List of processed short urls, where the new short url is added into
* @param string servicename short name of the service.
* @param string servicecall complete service URL to be called returning the short URL only
* @access private
*/
function shorten_via_simple( &$shorturls, $servicename, $servicecall ) {
// if we already evaluated the shorturl, stop here
if (!empty($shorturls[$servicename])) return;
if (function_exists('serendipity_request_url')) {
$short_url = serendipity_request_url($servicecall);
if (empty($short_url)) return false;
} else {
require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
serendipity_request_start();
$req = new HTTP_Request($servicecall, array('timeout' => 20, 'readTimeout' => array(5,0)));
$req->sendRequest();
$short_url = $req->getResponseBody();
serendipity_request_end();
}
if (!empty($short_url)) {
$short_url = trim($short_url);
if (strlen($short_url)<255) { // Should be an URL at least
$shorturls[$servicename] = trim($short_url);
}
}
}
// Working!
function shorten_via_tinyurl( $url, &$shorturls ) {
$url = urlencode($url);
UrlShortener::shorten_via_simple($shorturls, 'tinyurl', "http://tinyurl.com/api-create.php?url=$url");
}
function shorten_via_7ax( $url, &$shorturls ) {
$url = urlencode($url);
UrlShortener::shorten_via_simple($shorturls, '7ax.de', "http://7ax.de/api.php?url=$url");
}
function shorten_via_piratly( $url, &$shorturls ) {
$url = urlencode($url);
UrlShortener::shorten_via_simple($shorturls, 'piratly', "http://pirat.ly/shortener/api/createplain/{$this->piratly_apikey}/?$url");
}
// is.gd returns different short urls for same URL! How to handle this?!
// works *sometimes*
function shorten_via_isgd( $url, &$shorturls ) {
$url = urlencode($url);
UrlShortener::shorten_via_simple($shorturls, 'isgd', "http://is.gd/api.php?longurl=$url");
}
// twurl.nl returns different short urls for same URL! How to handle this?!
// works *sometimes*
// tinyburner.com
function shorten_via_twurl( $url, &$shorturls ) {
// if we already evaluated the shorturl, stop here
if (!empty($shorturls['twurl'])) return;
$req_url = "http://tweetburner.com/links";
if (function_exists('serendipity_request_url')) {
$short_url = serendipity_request_url($req_url, 'POST', null, array('link[url]' => $url));
if (empty($short_url)) return false;
} else {
require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
serendipity_request_start();
$req = new HTTP_Request($req_url, array('method' => HTTP_REQUEST_METHOD_POST, 'timeout' => 20, 'readTimeout' => array(5,0)));
$req->addPostData('link[url]',$url, true);
$req->sendRequest();
$short_url = trim($req->getResponseBody());
serendipity_request_end();
}
if ($short_url != '') {
$shorturls['twurl'] = $short_url;
}
}
function shorten_via_bitly( $url, &$shorturls ) {
// if we already evaluated the shorturl, stop here
if (!empty($shorturls['bitly'])) return;
$short_url = trim(UrlShortener::_make_bitly_api_url($url,'xml'));
if (!empty($short_url)) {
$shorturls['bitly'] = $short_url;
}
}
function shorten_via_yourls( $url, &$shorturls ) {
if (!empty($shorturls['yourls'])) return;
$url = urlencode($url);
UrlShortener::shorten_via_simple($shorturls, 'yourls', "http://{$this->yourls_url}/yourls-api.php?signature={$this->yourls_apikey}&action=shorturl&format=simple&url=$url");
}
function shorten_via_jmp( $url, &$shorturls ) {
// if we already evaluated the shorturl, stop here
if (!empty($shorturls['jmp'])) return;
$short_url = trim(UrlShortener::_make_bitly_api_url($url,'xml','j.mp'));
if (!empty($short_url)) {
$shorturls['jmp'] = $short_url;
}
}
/* bit.ly called via api */
function _make_bitly_api_url($url,$format = 'xml',$domain='bit.ly')
{
//create the API Call URL
$bitly = 'http://api.bit.ly/v3/shorten?longUrl='.urlencode($url).'&login='.$this->bitly_login.'&apiKey='.$this->bitly_apikey.'&format='.$format.'&domain='.$domain;
//get the url
if (function_exists('serendipity_request_url')) {
$response = serendipity_request_url($bitlyreq_url, 'POST');
if (empty($response)) return false;
} else {
require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
serendipity_request_start();
$req = new HTTP_Request($bitly, array('timeout' => 20, 'readTimeout' => array(5,0)));
$req->sendRequest();
$response = $req->getResponseBody();
serendipity_request_end();
if ($req->getResponseCode()!=200) {
return false;
}
if (strlen($response) < 1) {
return false;
}
}
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response);
$results = get_object_vars($json->results);
return $results[$url]->shortUrl;
}
else //xml
{
$vals = array();
$index = array();
$parser = xml_parser_create();
xml_parse_into_struct($parser, $response, $vals, $index);
xml_parser_free($parser);
return $short_url = $vals[$index['URL'][0]][value];
}
}
/*
* Doesn't work realy and is ultra slow..
*/
function shorten_via_delivr( $url, &$shorturls ) {
// if we already evaluated the shorturl, stop here
if (!empty($shorturls['delivr'])) return;
$login = 'public';
$apikey = '8bbce762-971e-45d6-a0e1-cbf5730252ea';
$url = urlencode($url);
$req_url = "http://api.delivr.com/shorten?username=$login&apiKey=$apikey&format=xml&url=" . $url;
if (function_exists('serendipity_request_url')) {
$xml = serendipity_request_url($req_url);
if (empty($xml)) return false;
} else {
require_once S9Y_PEAR_PATH . 'HTTP/Request.php';
serendipity_request_start();
$req = new HTTP_Request($req_url, array('timeout' => 20, 'readTimeout' => array(5,0)));
$req->sendRequest();
$xml = $req->getResponseBody();
serendipity_request_end();
}
if ($xml != '') {
$vals = array();
$index = array();
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml, $vals, $index);
xml_parser_free($parser);
$short_url = 'http://delivr.com/' . $vals[$index['DELIVRID'][0]][value];
$shorturls['delivr'] = trim($short_url);
}
}
// Fills in "special" services, if one service produces more than one result.
function fillup_extra_services( &$services ) {
if (array_search('bitly',$services)) {
$services[] = 'bitly_json';
}
}
}
?>