Redid how URL shorteners work. This way is much more like how Evan wants events to work (and more like how the rest of SN works).

This commit is contained in:
Craig Andrews 2009-11-11 14:02:57 -05:00
parent 086759f32a
commit 014d6b1d19
11 changed files with 210 additions and 215 deletions

View File

@ -538,3 +538,17 @@ EndChangePassword: After changing a password
UserDeleteRelated: Specify additional tables to delete entries from when deleting users
- $user: User object
- &$related: array of DB_DataObject class names to delete entries on matching user_id.
GetUrlShorteners: Specify URL shorteners that are available for use
- &$shorteners: append your shortener to this array like so: $shorteners[shortenerName]=array('display'=>display, 'freeService'=>boolean)
StartShortenUrl: About to shorten a URL
- $url: url to be shortened
- $shortenerName: name of the requested shortener
- &$shortenedUrl: short version of the url
EndShortenUrl: After a URL has been shortened
- $url: url to be shortened
- $shortenerName: name of the requested shortener
- $shortenedUrl: short version of the url

View File

@ -97,20 +97,15 @@ 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(!empty($value['info']['freeService'])){
// I18N
$services[$name].=' (free service)';
}
Event::handle('GetUrlShorteners', array(&$shorteners));
foreach($shorteners as $name=>$value)
{
$services[$name]=$name;
if($value['freeService']){
$services[$name].=_(' (free service)');
}
}
asort($services);
$services['']='None';
$this->elementStart('ul', 'form_data');
$this->elementStart('li');

View File

@ -1,67 +0,0 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008, 2009, StatusNet, Inc.
*
* 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/>.
*/
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
abstract class ShortUrlApi
{
protected $service_url;
protected $long_limit = 27;
function __construct($service_url)
{
$this->service_url = $service_url;
}
function shorten($url)
{
if ($this->is_long($url)) return $this->shorten_imp($url);
return $url;
}
protected abstract function shorten_imp($url);
protected function is_long($url) {
return strlen($url) >= common_config('site', 'shorturllength');
}
protected function http_post($data)
{
$request = HTTPClient::start();
$response = $request->post($this->service_url, null, $data);
return $response->getBody();
}
protected function http_get($url)
{
$request = HTTPClient::start();
$response = $request->get($this->service_url . urlencode($url));
return $response->getBody();
}
protected function tidy($response) {
$response = str_replace('&nbsp;', ' ', $response);
$config = array('output-xhtml' => true);
$tidy = new tidy;
$tidy->parseString($response, $config, 'utf8');
$tidy->cleanRepair();
return (string)$tidy;
}
}

View File

@ -229,7 +229,6 @@ require_once INSTALLDIR.'/lib/util.php';
require_once INSTALLDIR.'/lib/action.php';
require_once INSTALLDIR.'/lib/mail.php';
require_once INSTALLDIR.'/lib/subs.php';
require_once INSTALLDIR.'/lib/Shorturl_api.php';
require_once INSTALLDIR.'/lib/clientexception.php';
require_once INSTALLDIR.'/lib/serverexception.php';

View File

@ -1423,25 +1423,18 @@ function common_shorten_url($long_url)
if (empty($user)) {
// common current user does not find a user when called from the XMPP daemon
// therefore we'll set one here fix, so that XMPP given URLs may be shortened
$svc = 'ur1.ca';
$shortenerName = 'ur1.ca';
} else {
$svc = $user->urlshorteningservice;
}
global $_shorteners;
if (!isset($_shorteners[$svc])) {
//the user selected service doesn't exist, so default to ur1.ca
$svc = 'ur1.ca';
}
if (!isset($_shorteners[$svc])) {
// no shortener plugins installed.
return $long_url;
$shortenerName = $user->urlshorteningservice;
}
$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;
if(Event::handle('StartShortenUrl', array($long_url,$shortenerName,&$shortenedUrl))){
//URL wasn't shortened, so return the long url
return $long_url;
}else{
//URL was shortened, so return the result
return $shortenedUrl;
}
}
function common_client_ip()

View File

@ -31,31 +31,24 @@ if (!defined('STATUSNET')) {
exit(1);
}
class BitlyUrlPlugin extends Plugin
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class BitlyUrlPlugin extends UrlShortenerPlugin
{
function __construct()
{
parent::__construct();
}
public $serviceUrl;
function onInitializePlugin(){
$this->registerUrlShortener(
'bit.ly',
array(),
array('BitlyUrl',array('http://bit.ly/api?method=shorten&long_url='))
);
}
}
class BitlyUrl extends ShortUrlApi
{
protected function shorten_imp($url) {
$response = $this->http_get($url);
if(!$response){
return $url;
}else{
return current(json_decode($response)->results)->hashUrl;
parent::onInitializePlugin();
if(!isset($this->serviceUrl)){
throw new Exception("must specify a serviceUrl");
}
}
protected function shorten($url) {
$response = $this->http_get($url);
if(!$response) return;
return current(json_decode($response)->results)->hashUrl;
}
}

View File

@ -31,37 +31,31 @@ if (!defined('STATUSNET')) {
exit(1);
}
require_once(INSTALLDIR.'/lib/Shorturl_api.php');
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class LilUrlPlugin extends Plugin
class LilUrlPlugin extends UrlShortenerPlugin
{
function __construct()
{
parent::__construct();
}
public $serviceUrl;
function onInitializePlugin(){
$this->registerUrlShortener(
'ur1.ca',
array('freeService'=>true),
array('LilUrl',array('http://ur1.ca/'))
);
parent::onInitializePlugin();
if(!isset($this->serviceUrl)){
throw new Exception("must specify a serviceUrl");
}
}
protected function shorten($url) {
$data = array('longurl' => $url);
$responseBody = $this->http_post($this->serviceUrl,$data);
if (!$responseBody) return;
$y = @simplexml_load_string($responseBody);
if (!isset($y->body)) return;
$x = $y->body->p[0]->a->attributes();
if (isset($x['href'])) {
return $x['href'];
}
}
}
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'])) {
common_log(LOG_INFO, __CLASS__ . ": shortened $url to $x[href]");
return $x['href'];
}
return $url;
}
}

View File

@ -30,33 +30,28 @@
if (!defined('STATUSNET')) {
exit(1);
}
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class PtitUrlPlugin extends Plugin
class PtitUrlPlugin extends UrlShortenerPlugin
{
function __construct()
{
parent::__construct();
}
public $serviceUrl;
function onInitializePlugin(){
$this->registerUrlShortener(
'ptiturl.com',
array(),
array('PtitUrl',array('http://ptiturl.com/?creer=oui&action=Reduire&url='))
);
parent::onInitializePlugin();
if(!isset($this->serviceUrl)){
throw new Exception("must specify a serviceUrl");
}
}
protected function shorten($url)
{
$response = $this->http_get(sprintf($this->serviceUrl,urlencode($url)));
if (!$response) return;
$response = $this->tidy($response);
$y = @simplexml_load_string($response);
if (!isset($y->body)) return;
$xml = $y->body->center->table->tr->td->pre->a->attributes();
if (isset($xml['href'])) return $xml['href'];
}
}
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

@ -31,40 +31,21 @@ if (!defined('STATUSNET')) {
exit(1);
}
class SimpleUrlPlugin extends Plugin
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class SimpleUrlPlugin extends UrlShortenerPlugin
{
function __construct()
{
parent::__construct();
}
public $serviceUrl;
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='))
);
parent::onInitializePlugin();
if(!isset($this->serviceUrl)){
throw new Exception("must specify a serviceUrl");
}
}
protected function shorten($url) {
return $this->http_get(sprintf($this->serviceUrl,urlencode($url)));
}
}
class SimpleUrl extends ShortUrlApi
{
protected function shorten_imp($url) {
return $this->http_get($url);
}
}

View File

@ -31,32 +31,27 @@ if (!defined('STATUSNET')) {
exit(1);
}
class TightUrlPlugin extends Plugin
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class TightUrlPlugin extends UrlShortenerPlugin
{
function __construct()
{
parent::__construct();
}
public $serviceUrl;
function onInitializePlugin(){
$this->registerUrlShortener(
'2tu.us',
array('freeService'=>true),
array('TightUrl',array('http://2tu.us/?save=y&url='))
);
parent::onInitializePlugin();
if(!isset($this->serviceUrl)){
throw new Exception("must specify a serviceUrl");
}
}
}
class TightUrl extends ShortUrlApi
{
protected function shorten_imp($url) {
$response = $this->http_get($url);
if (!$response) return $url;
protected function shorten($url)
{
$response = $this->http_get(sprintf($this->serviceUrl,urlencode($url)));
if (!$response) return;
$response = $this->tidy($response);
$y = @simplexml_load_string($response);
if (!isset($y->body)) return $url;
if (!isset($y->body)) return;
$xml = $y->body->p[0]->code[0]->a->attributes();
if (isset($xml['href'])) return $xml['href'];
return $url;
}
}

View File

@ -0,0 +1,103 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Superclass for plugins that do URL shortening
*
* 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>
* @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') && !defined('LACONICA')) {
exit(1);
}
/**
* Superclass for plugins that do URL shortening
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <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/
*/
abstract class UrlShortenerPlugin extends Plugin
{
public $shortenerName;
public $freeService=false;
//------------Url Shortener plugin should implement some (or all) of these methods------------\\
/**
* Short a URL
* @param url
* @return string shortened version of the url, or null if URL shortening failed
*/
protected abstract function shorten($url);
//------------These methods may help you implement your plugin------------\\
protected function http_get($url)
{
$request = HTTPClient::start();
$response = $request->get($url);
return $response->getBody();
}
protected function http_post($url,$data)
{
$request = HTTPClient::start();
$response = $request->post($url, null, $data);
return $response->getBody();
}
protected function tidy($response) {
$response = str_replace('&nbsp;', ' ', $response);
$config = array('output-xhtml' => true);
$tidy = new tidy;
$tidy->parseString($response, $config, 'utf8');
$tidy->cleanRepair();
return (string)$tidy;
}
//------------Below are the methods that connect StatusNet to the implementing Url Shortener plugin------------\\
function onInitializePlugin(){
if(!isset($this->shortenerName)){
throw new Exception("must specify a shortenerName");
}
}
function onGetUrlShorteners(&$shorteners)
{
$shorteners[$this->shortenerName]=array('freeService'=>$this->freeService);
}
function onStartShortenUrl($url,$shortenerName,&$shortenedUrl)
{
if($shortenerName == $this->shortenerName && strlen($url) >= common_config('site', 'shorturllength')){
$result = $this->shorten($url);
if(isset($result) && $result != null && $result !== false){
$shortenedUrl=$result;
common_log(LOG_INFO, __CLASS__ . ": $this->shortenerName shortened $url to $shortenedUrl");
return false;
}
}
}
}