summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCraig Andrews <candrews@integralblue.com>2009-11-11 14:02:57 -0500
committerCraig Andrews <candrews@integralblue.com>2009-11-11 14:04:44 -0500
commit014d6b1d19b6ae5de8d87f055397993f80579f74 (patch)
tree55faca777c7ef1f8576b21f98dc4e3f74e99ac4d
parent086759f32ab6d2c5aadecb57941e7e14015b8bd6 (diff)
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).
-rw-r--r--EVENTS.txt14
-rw-r--r--actions/othersettings.php17
-rw-r--r--lib/Shorturl_api.php67
-rw-r--r--lib/common.php1
-rw-r--r--lib/util.php25
-rw-r--r--plugins/BitlyUrl/BitlyUrlPlugin.php31
-rw-r--r--plugins/LilUrl/LilUrlPlugin.php38
-rw-r--r--plugins/PtitUrl/PtitUrlPlugin.php31
-rw-r--r--plugins/SimpleUrl/SimpleUrlPlugin.php41
-rw-r--r--plugins/TightUrl/TightUrlPlugin.php31
-rw-r--r--plugins/UrlShortener/UrlShortenerPlugin.php103
11 files changed, 197 insertions, 202 deletions
diff --git a/EVENTS.txt b/EVENTS.txt
index f75dcebca..3acff277b 100644
--- a/EVENTS.txt
+++ b/EVENTS.txt
@@ -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
+
diff --git a/actions/othersettings.php b/actions/othersettings.php
index d32a2d651..d52a634ac 100644
--- a/actions/othersettings.php
+++ b/actions/othersettings.php
@@ -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');
diff --git a/lib/Shorturl_api.php b/lib/Shorturl_api.php
deleted file mode 100644
index de4d55012..000000000
--- a/lib/Shorturl_api.php
+++ /dev/null
@@ -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;
- }
-}
-
diff --git a/lib/common.php b/lib/common.php
index 6aac46807..4958866d0 100644
--- a/lib/common.php
+++ b/lib/common.php
@@ -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';
diff --git a/lib/util.php b/lib/util.php
index 7aca4af8d..68f3520db 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -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;
+ $shortenerName = $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;
- }
-
- $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()
diff --git a/plugins/BitlyUrl/BitlyUrlPlugin.php b/plugins/BitlyUrl/BitlyUrlPlugin.php
index 478ef99d2..65d0f70e6 100644
--- a/plugins/BitlyUrl/BitlyUrlPlugin.php
+++ b/plugins/BitlyUrl/BitlyUrlPlugin.php
@@ -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='))
- );
+ parent::onInitializePlugin();
+ if(!isset($this->serviceUrl)){
+ throw new Exception("must specify a serviceUrl");
+ }
}
-}
-class BitlyUrl extends ShortUrlApi
-{
- protected function shorten_imp($url) {
+ protected function shorten($url) {
$response = $this->http_get($url);
- if(!$response){
- return $url;
- }else{
- return current(json_decode($response)->results)->hashUrl;
- }
+ if(!$response) return;
+ return current(json_decode($response)->results)->hashUrl;
}
}
+
diff --git a/plugins/LilUrl/LilUrlPlugin.php b/plugins/LilUrl/LilUrlPlugin.php
index 852253b02..e906751e8 100644
--- a/plugins/LilUrl/LilUrlPlugin.php
+++ b/plugins/LilUrl/LilUrlPlugin.php
@@ -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");
+ }
}
-}
-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;
+ 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'])) {
- common_log(LOG_INFO, __CLASS__ . ": shortened $url to $x[href]");
return $x['href'];
}
- return $url;
}
}
+
diff --git a/plugins/PtitUrl/PtitUrlPlugin.php b/plugins/PtitUrl/PtitUrlPlugin.php
index f00d3e2f2..ef453e96d 100644
--- a/plugins/PtitUrl/PtitUrlPlugin.php
+++ b/plugins/PtitUrl/PtitUrlPlugin.php
@@ -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");
+ }
}
-}
-class PtitUrl 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->center->table->tr->td->pre->a->attributes();
if (isset($xml['href'])) return $xml['href'];
- return $url;
}
}
+
diff --git a/plugins/SimpleUrl/SimpleUrlPlugin.php b/plugins/SimpleUrl/SimpleUrlPlugin.php
index d59d63e47..45b745b07 100644
--- a/plugins/SimpleUrl/SimpleUrlPlugin.php
+++ b/plugins/SimpleUrl/SimpleUrlPlugin.php
@@ -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");
+ }
}
-}
-class SimpleUrl extends ShortUrlApi
-{
- protected function shorten_imp($url) {
- return $this->http_get($url);
+ protected function shorten($url) {
+ return $this->http_get(sprintf($this->serviceUrl,urlencode($url)));
}
}
+
diff --git a/plugins/TightUrl/TightUrlPlugin.php b/plugins/TightUrl/TightUrlPlugin.php
index 48efb355f..56414c8c8 100644
--- a/plugins/TightUrl/TightUrlPlugin.php
+++ b/plugins/TightUrl/TightUrlPlugin.php
@@ -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;
}
}
diff --git a/plugins/UrlShortener/UrlShortenerPlugin.php b/plugins/UrlShortener/UrlShortenerPlugin.php
new file mode 100644
index 000000000..37206aa89
--- /dev/null
+++ b/plugins/UrlShortener/UrlShortenerPlugin.php
@@ -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;
+ }
+ }
+ }
+}