]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Redid how URL shorteners work. This way is much more like how Evan wants events to...
authorCraig Andrews <candrews@integralblue.com>
Wed, 11 Nov 2009 19:02:57 +0000 (14:02 -0500)
committerCraig Andrews <candrews@integralblue.com>
Wed, 11 Nov 2009 19:04:44 +0000 (14:04 -0500)
EVENTS.txt
actions/othersettings.php
lib/Shorturl_api.php [deleted file]
lib/common.php
lib/util.php
plugins/BitlyUrl/BitlyUrlPlugin.php
plugins/LilUrl/LilUrlPlugin.php
plugins/PtitUrl/PtitUrlPlugin.php
plugins/SimpleUrl/SimpleUrlPlugin.php
plugins/TightUrl/TightUrlPlugin.php
plugins/UrlShortener/UrlShortenerPlugin.php [new file with mode: 0644]

index f75dcebca6df1f3636628a02e25f304f0fca7dea..3acff277ba1f04c8c8f63d99dffbce5f5eafe266 100644 (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
+
index d32a2d651cb390b2d248064d43543f0a233dbb9f..d52a634aca37b606a14f5f95b83d37e93457e3fb 100644 (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');
diff --git a/lib/Shorturl_api.php b/lib/Shorturl_api.php
deleted file mode 100644 (file)
index de4d550..0000000
+++ /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;
-    }
-}
-
index 6aac468075f301dd3fce0702b80b3b70b5553d49..4958866d030f5c2093f5c01a4894de11f3461386 100644 (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';
index 7aca4af8d622910ca9bd90d97c39e630de5fb725..68f3520db5c366086ce2c3e0af4c9a2501d47b63 100644 (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;
+        $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()
index 478ef99d289fa4f9865aa30b2ec3098586bb03dd..65d0f70e653a2ed4e82f3c2d223ca8242dd70f7c 100644 (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='))
-        );
+        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;
     }
 }
 
+
index 852253b02326c91ea2e228b5c40c12a36498836a..e906751e8424410b15fbbb3061e0d0897cee9fcc 100644 (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");
+        }
     }
-}
 
-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;
     }
 }
+
index f00d3e2f218944a21b554d248e50583f58c05022..ef453e96d84e388903efa480b7eb923e030777f3 100644 (file)
 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;
     }
 }
+
index d59d63e47c9f99b60279b2b6ef8cf3e388518458..45b745b07d0000a50860ef5042c084d5c280f2e5 100644 (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");
+        }
     }
-}
 
-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)));
     }
 }
+
index 48efb355f03dd619668f7be9c493230ff109986a..56414c8c8d121fe45ea23668e79a0bdf750c8df7 100644 (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;
     }
 }
diff --git a/plugins/UrlShortener/UrlShortenerPlugin.php b/plugins/UrlShortener/UrlShortenerPlugin.php
new file mode 100644 (file)
index 0000000..37206aa
--- /dev/null
@@ -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;
+            }
+        }
+    }
+}