]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Pluginize the URL shorteners
authorCraig Andrews <candrews@integralblue.com>
Thu, 3 Sep 2009 18:58:50 +0000 (14:58 -0400)
committerCraig Andrews <candrews@integralblue.com>
Thu, 3 Sep 2009 18:58:50 +0000 (14:58 -0400)
actions/othersettings.php
lib/Shorturl_api.php
lib/plugin.php
lib/util.php
plugins/LilUrl/LilUrlPlugin.php [new file with mode: 0644]
plugins/PtitUrl/PtitUrlPlugin.php [new file with mode: 0644]
plugins/SimpleUrl/SimpleUrlPlugin.php [new file with mode: 0644]
plugins/TightUrl/TightUrlPlugin.php [new file with mode: 0644]

index 8b674161a89bb1a28408e4e91b158083a5492167..4ccef84125275bf6ff288cc8f061d7a661b7209d 100644 (file)
@@ -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');
index 6402dbc099615918d7ee777330b61abfe3c8873b..18ae7719b25c9df14146079ece46afade3fd362f 100644 (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;
-    }
-}
-
index 87d7be5a7597296bb2c2d777358f7365455beb5d..59bf3ba9d695914c6544dd76dade659e97bd2dad 100644 (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);
+    }
 }
index 9cf46251501a1805c4efeaaf299d8a29d4e8bce2..0a25907c479097226c7bba2f556c06e065c39e03 100644 (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;
 }
diff --git a/plugins/LilUrl/LilUrlPlugin.php b/plugins/LilUrl/LilUrlPlugin.php
new file mode 100644 (file)
index 0000000..7665b6c
--- /dev/null
@@ -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;
+    }
+}
diff --git a/plugins/PtitUrl/PtitUrlPlugin.php b/plugins/PtitUrl/PtitUrlPlugin.php
new file mode 100644 (file)
index 0000000..f00d3e2
--- /dev/null
@@ -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;
+    }
+}
diff --git a/plugins/SimpleUrl/SimpleUrlPlugin.php b/plugins/SimpleUrl/SimpleUrlPlugin.php
new file mode 100644 (file)
index 0000000..82d7720
--- /dev/null
@@ -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;
+    }
+}
diff --git a/plugins/TightUrl/TightUrlPlugin.php b/plugins/TightUrl/TightUrlPlugin.php
new file mode 100644 (file)
index 0000000..48efb35
--- /dev/null
@@ -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;
+    }
+}