]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/BitlyUrl/BitlyUrlPlugin.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / BitlyUrl / BitlyUrlPlugin.php
index c9005e52eca6688ffcd05839e81fd7eab412f226..532e66fbc73d2ec7bb06ddce6d6d3e2e672d053d 100644 (file)
@@ -33,12 +33,12 @@ if (!defined('STATUSNET')) {
     exit(1);
 }
 
-require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
-
 class BitlyUrlPlugin extends UrlShortenerPlugin
 {
     public $shortenerName = 'bit.ly';
     public $serviceUrl = 'http://bit.ly/api?method=shorten&version=2.0.1&longUrl=%s';
+    public $login; // To set a site-default when admins or users don't override it.
+    public $apiKey;
 
     function onInitializePlugin(){
         parent::onInitializePlugin();
@@ -47,6 +47,21 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
         }
     }
 
+    /**
+     * Add bit.ly to the list of available URL shorteners if it's configured,
+     * otherwise leave it out.
+     *
+     * @param array $shorteners
+     * @return boolean hook return value
+     */
+    function onGetUrlShorteners(&$shorteners)
+    {
+        if ($this->getLogin() && $this->getApiKey()) {
+            return parent::onGetUrlShorteners($shorteners);
+        }
+        return true;
+    }
+
     /**
      * Short a URL
      * @param url
@@ -68,7 +83,11 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
      */
     protected function getLogin()
     {
-        return common_config('bitly', 'default_login');
+        $login = common_config('bitly', 'default_login');
+        if (!$login) {
+            $login = $this->login;
+        }
+        return $login;
     }
 
     /**
@@ -78,7 +97,11 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
      */
     protected function getApiKey()
     {
-        return common_config('bitly', 'default_apikey');
+        $key = common_config('bitly', 'default_apikey');
+        if (!$key) {
+            $key = $this->apiKey;
+        }
+        return $key;
     }
 
     /**
@@ -93,7 +116,7 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
         $params = http_build_query(array(
             'login' => $this->getLogin(),
             'apiKey' => $this->getApiKey()), '', '&');
-        $serviceUrl = sprintf($this->serviceUrl, $url) . '&' . $params;
+        $serviceUrl = sprintf($this->serviceUrl, urlencode($url)) . '&' . $params;
 
         $request = HTTPClient::start();
         return $request->get($serviceUrl);
@@ -120,6 +143,10 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
             common_log(LOG_INFO, $body);
             $json = json_decode($body, true);
             if ($json['statusCode'] == 'OK') {
+                if (!isset($json['results'][$url])) {
+                    common_log(LOG_ERR, "bit.ly returned OK response, but didn't find expected URL $url in $body");
+                    return false;
+                }
                 $data = $json['results'][$url];
                 if (isset($data['shortUrl'])) {
                     return true;
@@ -213,4 +240,20 @@ class BitlyUrlPlugin extends UrlShortenerPlugin
             return true;
         }
     }
+
+    /**
+     * Internal hook point to check the default global credentials so
+     * the admin form knows if we have a fallback or not.
+     *
+     * @param string $login
+     * @param string $apiKey
+     * @return boolean hook return value
+     */
+    function onBitlyDefaultCredentials(&$login, &$apiKey)
+    {
+        $login = $this->login;
+        $apiKey = $this->apiKey;
+        return false;
+    }
+
 }