From beae3db41375879e725af053edf8041bbd76ac8c Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Thu, 3 Sep 2009 14:58:50 -0400 Subject: [PATCH] Pluginize the URL shorteners --- actions/othersettings.php | 27 ++++----- lib/Shorturl_api.php | 66 +--------------------- lib/plugin.php | 14 +++++ lib/util.php | 56 +++---------------- plugins/LilUrl/LilUrlPlugin.php | 64 ++++++++++++++++++++++ plugins/PtitUrl/PtitUrlPlugin.php | 62 +++++++++++++++++++++ plugins/SimpleUrl/SimpleUrlPlugin.php | 79 +++++++++++++++++++++++++++ plugins/TightUrl/TightUrlPlugin.php | 62 +++++++++++++++++++++ 8 files changed, 305 insertions(+), 125 deletions(-) create mode 100644 plugins/LilUrl/LilUrlPlugin.php create mode 100644 plugins/PtitUrl/PtitUrlPlugin.php create mode 100644 plugins/SimpleUrl/SimpleUrlPlugin.php create mode 100644 plugins/TightUrl/TightUrlPlugin.php diff --git a/actions/othersettings.php b/actions/othersettings.php index 8b674161a8..4ccef84125 100644 --- a/actions/othersettings.php +++ b/actions/othersettings.php @@ -91,19 +91,20 @@ class OthersettingsAction extends AccountSettingsAction $this->elementStart('fieldset'); $this->hidden('token', common_session_token()); - // 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=array(); + global $_shorteners; + if($_shorteners){ + foreach($_shorteners as $name=>$value) + { + $services[$name]=$name; + if($value['info']['freeService']){ + // I18N + $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 index 6402dbc099..18ae7719b2 100644 --- a/lib/Shorturl_api.php +++ b/lib/Shorturl_api.php @@ -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; - } -} - diff --git a/lib/plugin.php b/lib/plugin.php index 87d7be5a75..59bf3ba9d6 100644 --- a/lib/plugin.php +++ b/lib/plugin.php @@ -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); + } } diff --git a/lib/util.php b/lib/util.php index 9cf4625150..0a25907c47 100644 --- a/lib/util.php +++ b/lib/util.php @@ -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; } diff --git a/plugins/LilUrl/LilUrlPlugin.php b/plugins/LilUrl/LilUrlPlugin.php new file mode 100644 index 0000000000..7665b6c1e4 --- /dev/null +++ b/plugins/LilUrl/LilUrlPlugin.php @@ -0,0 +1,64 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Craig Andrews + * @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; + } +} diff --git a/plugins/PtitUrl/PtitUrlPlugin.php b/plugins/PtitUrl/PtitUrlPlugin.php new file mode 100644 index 0000000000..f00d3e2f21 --- /dev/null +++ b/plugins/PtitUrl/PtitUrlPlugin.php @@ -0,0 +1,62 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Craig Andrews + * @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; + } +} diff --git a/plugins/SimpleUrl/SimpleUrlPlugin.php b/plugins/SimpleUrl/SimpleUrlPlugin.php new file mode 100644 index 0000000000..82d7720487 --- /dev/null +++ b/plugins/SimpleUrl/SimpleUrlPlugin.php @@ -0,0 +1,79 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Craig Andrews + * @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; + } +} diff --git a/plugins/TightUrl/TightUrlPlugin.php b/plugins/TightUrl/TightUrlPlugin.php new file mode 100644 index 0000000000..48efb355f0 --- /dev/null +++ b/plugins/TightUrl/TightUrlPlugin.php @@ -0,0 +1,62 @@ +. + * + * @category Plugin + * @package StatusNet + * @author Craig Andrews + * @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; + } +}