Pluginize the URL shorteners

This commit is contained in:
Craig Andrews 2009-09-03 14:58:50 -04:00
parent 5db43f4655
commit beae3db413
8 changed files with 305 additions and 125 deletions

View File

@ -91,19 +91,20 @@ class OthersettingsAction extends AccountSettingsAction
$this->elementStart('fieldset');
$this->hidden('token', common_session_token());
$services=array();
global $_shorteners;
if($_shorteners){
foreach($_shorteners as $name=>$value)
{
$services[$name]=$name;
if($value['info']['freeService']){
// I18N
$services = array(
'' => 'None',
'ur1.ca' => 'ur1.ca (free service)',
'2tu.us' => '2tu.us (free service)',
'ptiturl.com' => 'ptiturl.com',
'bit.ly' => 'bit.ly',
'tinyurl.com' => 'tinyurl.com',
'is.gd' => 'is.gd',
'snipr.com' => 'snipr.com',
'metamark.net' => 'metamark.net'
);
$services[$name].=' (free service)';
}
}
}
asort($services);
$services['']='None';
$this->elementStart('ul', 'form_data');
$this->elementStart('li');

View File

@ -19,7 +19,7 @@
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
class ShortUrlApi
abstract class ShortUrlApi
{
protected $service_url;
protected $long_limit = 27;
@ -35,11 +35,9 @@ class ShortUrlApi
return $url;
}
protected function shorten_imp($url) {
return "To Override";
}
protected abstract function shorten_imp($url);
private function is_long($url) {
protected function is_long($url) {
return strlen($url) >= common_config('site', 'shorturllength');
}
@ -71,61 +69,3 @@ class ShortUrlApi
}
}
class LilUrl extends ShortUrlApi
{
function __construct()
{
parent::__construct('http://ur1.ca/');
}
protected function shorten_imp($url) {
$data['longurl'] = $url;
$response = $this->http_post($data);
if (!$response) return $url;
$y = @simplexml_load_string($response);
if (!isset($y->body)) return $url;
$x = $y->body->p[0]->a->attributes();
if (isset($x['href'])) return $x['href'];
return $url;
}
}
class PtitUrl extends ShortUrlApi
{
function __construct()
{
parent::__construct('http://ptiturl.com/?creer=oui&action=Reduire&url=');
}
protected function shorten_imp($url) {
$response = $this->http_get($url);
if (!$response) return $url;
$response = $this->tidy($response);
$y = @simplexml_load_string($response);
if (!isset($y->body)) return $url;
$xml = $y->body->center->table->tr->td->pre->a->attributes();
if (isset($xml['href'])) return $xml['href'];
return $url;
}
}
class TightUrl extends ShortUrlApi
{
function __construct()
{
parent::__construct('http://2tu.us/?save=y&url=');
}
protected function shorten_imp($url) {
$response = $this->http_get($url);
if (!$response) return $url;
$response = $this->tidy($response);
$y = @simplexml_load_string($response);
if (!isset($y->body)) return $url;
$xml = $y->body->p[0]->code[0]->a->attributes();
if (isset($xml['href'])) return $xml['href'];
return $url;
}
}

View File

@ -76,4 +76,18 @@ class Plugin
{
return true;
}
/*
* the name of the shortener
* shortenerInfo associative array with additional information. One possible element is 'freeService' which can be true or false
* shortener array, first element is the name of the class, second element is an array to be passed as constructor parameters to the class
*/
function registerUrlShortener($name, $shortenerInfo, $shortener)
{
global $_shorteners;
if(!is_array($_shorteners)){
$_shorteners=array();
}
$_shorteners[$name]=array('info'=>$shortenerInfo, 'callInfo'=>$shortener);
}
}

View File

@ -1373,57 +1373,15 @@ function common_shorten_url($long_url)
} else {
$svc = $user->urlshorteningservice;
}
$curlh = curl_init();
curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait
curl_setopt($curlh, CURLOPT_USERAGENT, 'StatusNet');
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
switch($svc) {
case 'ur1.ca':
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url_service = new LilUrl;
$short_url = $short_url_service->shorten($long_url);
break;
case '2tu.us':
$short_url_service = new TightUrl;
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url = $short_url_service->shorten($long_url);
break;
case 'ptiturl.com':
require_once INSTALLDIR.'/lib/Shorturl_api.php';
$short_url_service = new PtitUrl;
$short_url = $short_url_service->shorten($long_url);
break;
case 'bit.ly':
curl_setopt($curlh, CURLOPT_URL, 'http://bit.ly/api?method=shorten&long_url='.urlencode($long_url));
$short_url = current(json_decode(curl_exec($curlh))->results)->hashUrl;
break;
case 'is.gd':
curl_setopt($curlh, CURLOPT_URL, 'http://is.gd/api.php?longurl='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'snipr.com':
curl_setopt($curlh, CURLOPT_URL, 'http://snipr.com/site/snip?r=simple&link='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'metamark.net':
curl_setopt($curlh, CURLOPT_URL, 'http://metamark.net/api/rest/simple?long_url='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
case 'tinyurl.com':
curl_setopt($curlh, CURLOPT_URL, 'http://tinyurl.com/api-create.php?url='.urlencode($long_url));
$short_url = curl_exec($curlh);
break;
default:
$short_url = false;
global $_shorteners;
if(! $_shorteners[$svc]){
//the user selected service doesn't exist, so default to ur1.ca
$svc = 'ur1.ca';
}
curl_close($curlh);
$reflectionObj = new ReflectionClass($_shorteners[$svc]['callInfo'][0]);
$short_url_service = $reflectionObj->newInstanceArgs($_shorteners[$svc]['callInfo'][1]);
$short_url = $short_url_service->shorten($long_url);
return $short_url;
}

View File

@ -0,0 +1,64 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Plugin to push RSS/Atom updates to a PubSubHubBub hub
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @copyright 2009 Craig Andrews http://candrews.integralblue.com
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
require_once(INSTALLDIR.'/lib/Shorturl_api.php');
class LilUrlPlugin extends Plugin
{
function __construct()
{
parent::__construct();
}
function onInitializePlugin(){
$this->registerUrlShortener(
'ur1.ca',
array('freeService'=>true),
array('LilUrl',array('http://ur1.ca/'))
);
}
}
class LilUrl extends ShortUrlApi
{
protected function shorten_imp($url) {
$data['longurl'] = $url;
$response = $this->http_post($data);
if (!$response) return $url;
$y = @simplexml_load_string($response);
if (!isset($y->body)) return $url;
$x = $y->body->p[0]->a->attributes();
if (isset($x['href'])) return $x['href'];
return $url;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Plugin to push RSS/Atom updates to a PubSubHubBub hub
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @copyright 2009 Craig Andrews http://candrews.integralblue.com
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
class PtitUrlPlugin extends Plugin
{
function __construct()
{
parent::__construct();
}
function onInitializePlugin(){
$this->registerUrlShortener(
'ptiturl.com',
array(),
array('PtitUrl',array('http://ptiturl.com/?creer=oui&action=Reduire&url='))
);
}
}
class PtitUrl extends ShortUrlApi
{
protected function shorten_imp($url) {
$response = $this->http_get($url);
if (!$response) return $url;
$response = $this->tidy($response);
$y = @simplexml_load_string($response);
if (!isset($y->body)) return $url;
$xml = $y->body->center->table->tr->td->pre->a->attributes();
if (isset($xml['href'])) return $xml['href'];
return $url;
}
}

View File

@ -0,0 +1,79 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Plugin to push RSS/Atom updates to a PubSubHubBub hub
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @copyright 2009 Craig Andrews http://candrews.integralblue.com
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
class SimpleUrlPlugin extends Plugin
{
function __construct()
{
parent::__construct();
}
function onInitializePlugin(){
$this->registerUrlShortener(
'is.gd',
array(),
array('SimpleUrl',array('http://is.gd/api.php?longurl='))
);
$this->registerUrlShortener(
'snipr.com',
array(),
array('SimpleUrl',array('http://snipr.com/site/snip?r=simple&link='))
);
$this->registerUrlShortener(
'metamark.net',
array(),
array('SimpleUrl',array('http://metamark.net/api/rest/simple?long_url='))
);
$this->registerUrlShortener(
'tinyurl.com',
array(),
array('SimpleUrl',array('http://tinyurl.com/api-create.php?url='))
);
}
}
class SimpleUrl extends ShortUrlApi
{
protected function shorten_imp($url) {
$curlh = curl_init();
curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait
curl_setopt($curlh, CURLOPT_USERAGENT, 'StatusNet');
curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlh, CURLOPT_URL, $this->service_url.urlencode($url));
$short_url = curl_exec($curlh);
curl_close($curlh);
return $short_url;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Plugin to push RSS/Atom updates to a PubSubHubBub hub
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @copyright 2009 Craig Andrews http://candrews.integralblue.com
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
class TightUrlPlugin extends Plugin
{
function __construct()
{
parent::__construct();
}
function onInitializePlugin(){
$this->registerUrlShortener(
'2tu.us',
array('freeService'=>true),
array('TightUrl',array('http://2tu.us/?save=y&url='))
);
}
}
class TightUrl extends ShortUrlApi
{
protected function shorten_imp($url) {
$response = $this->http_get($url);
if (!$response) return $url;
$response = $this->tidy($response);
$y = @simplexml_load_string($response);
if (!isset($y->body)) return $url;
$xml = $y->body->p[0]->code[0]->a->attributes();
if (isset($xml['href'])) return $xml['href'];
return $url;
}
}