From 658374a14c75c638fddbe41ea6114f94e68dbf33 Mon Sep 17 00:00:00 2001
From: sebastian <sebastian@cfc7b370-4200-0410-a6e3-cb6bdb053afe>
Date: Tue, 16 Feb 2010 17:14:16 +0000
Subject: [PATCH] #2362 twitter. url check, bitly

---
 .../modules/htmlUI/var/html/ui_browser.php    |  2 +-
 .../modules/htmlUI/var/ui_twitter.class.php   | 80 +++++++++++++++++--
 2 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/campcaster/src/modules/htmlUI/var/html/ui_browser.php b/campcaster/src/modules/htmlUI/var/html/ui_browser.php
index 207899273..8ef88c8fa 100644
--- a/campcaster/src/modules/htmlUI/var/html/ui_browser.php
+++ b/campcaster/src/modules/htmlUI/var/html/ui_browser.php
@@ -413,7 +413,7 @@ if ($uiBrowser->userid) {
 	        
 	    case "twitter.settings":
 	        $Smarty->assign('dynform', $uiBrowser->TWITTER->getSettingsForm());
-	        $Smarty->assign('twitter', array('samplefeed' => $uiBrowser->TWITTER->getFeed(true)));
+	        $Smarty->assign('twitter', array('samplefeed' => $uiBrowser->TWITTER->getFeed(true, true)));
 	        $Smarty->assign('act', $action);
 	        break;
     }
diff --git a/campcaster/src/modules/htmlUI/var/ui_twitter.class.php b/campcaster/src/modules/htmlUI/var/ui_twitter.class.php
index 29ef2a3f0..350a84c28 100644
--- a/campcaster/src/modules/htmlUI/var/ui_twitter.class.php
+++ b/campcaster/src/modules/htmlUI/var/ui_twitter.class.php
@@ -2,7 +2,10 @@
 class uiTwitter {
     private $Base;
     
-    private $sesttings = array();
+    private $settings = array(
+        'twitter-bitly-login' => 'campcaster',
+        'twitter-bitly-apikey'   => 'R_2f812152bfc21035468350273ec8ff43' 
+    );
     
     /**
      * Time in sec
@@ -121,6 +124,12 @@ class uiTwitter {
                 'label'     => 'URL (optional)',
                 'isPref'    => true
             ),
+            array(
+                'rule'      => 'regex',
+                'element'   => 'twitter-url',
+                'format'    => UI_REGEX_URL,
+                'rulemsg'   => 'The URL seems not to be valid. You need to use http(s):// prefix.'
+            ),
             array(
                 'element'   => 'twitter-offset',
                 'type'      => 'select',
@@ -156,6 +165,43 @@ class uiTwitter {
                 'element'   => 'twitter-config-fieldset-close',
                 'type'      => 'static',
                 'text'      => '</fieldset>'
+            ),
+                        array(
+                'element'   => 'twitter-shortener-fieldset-open',
+                'type'      => 'static',
+                'text'      => '<fieldset style="width: 300px;">'
+            ),
+            array(
+                'element'   => 'twitter-shortener-label',
+                'type'      => 'static',
+                'text'      => '<legend style="font-weight: bold;">URL shortener</legend>'
+            ),
+            array(
+                'element'   => 'twitter-shortener-provider',
+                'type'      => 'select',
+                'label'     => 'Provider',
+                'options'   => array(
+                    'bit.ly'        => 'bit.ly',
+                    'tinyurl.com'   => 'tinyurl.com',               
+                ),
+                'isPref'    => true
+            ),
+            array(
+                'element'   => 'twitter-bitly-login',
+                'type'      => 'text',
+                'label'     => 'bit.ly username',
+                'isPref'    => true
+            ),
+            array(
+                'element'   => 'twitter-bitly-apikey',
+                'type'      => 'text',
+                'label'     => 'bit.ly API key',
+                'isPref'    => true
+            ),
+            array(
+                'element'   => 'twitter-shortener-fieldset-close',
+                'type'      => 'static',
+                'text'      => '</fieldset>'
             ),
             array(
                 'element'   => 'Submit',
@@ -228,7 +274,7 @@ class uiTwitter {
         $this->Base->_retMsg('Twitter settings saved.');
     }
     
-    public function getFeed($p_useSampledata = false)
+    public function getFeed($p_useSampledata = false, $p_twitterfy = false)
     {        
         if ($p_useSampledata) {
             $whatsplaying = array(
@@ -259,7 +305,7 @@ class uiTwitter {
             $tweetsuffix = "";
         }
         if (!empty($this->settings['twitter-url'])) {
-            $tweetsuffix = $tweetsuffix . " " . self::GetTinyUrl($this->settings['twitter-url']);
+            $tweetsuffix = $tweetsuffix . " " . self::shortUrl($this->settings['twitter-url']);
         }
         // TWEET BODY
         $tweetbody = array();
@@ -276,15 +322,27 @@ class uiTwitter {
         }
         
         $tweet = $tweetprefix . $tweetbody . $tweetsuffix;
-                
+          
+        if ($p_twitterfy) {
+            $tweet = $this->twitterify($tweet); 
+        }      
         return $tweet;
 
     }
     
-    public function getTinyUrl($p_url)
+    public function shortUrl($p_url)
     {
-        $tiny = file_get_contents('http://tinyurl.com/api-create.php?url='.$p_url);
-        return $tiny;
+        switch ($this->settings['twitter-shortener-provider']) {
+            case 'tinyurl.com':
+                $short = file_get_contents('http://tinyurl.com/api-create.php?url='.$p_url);
+                break;
+                
+            case 'bit.ly':
+                $short = file_get_contents("http://api.bit.ly/shorten?version=2.0.1&longUrl={$p_url}&format=text&login={$this->settings['twitter-bitly-login']}&apiKey={$this->settings['twitter-bitly-apikey']}");
+                break;
+        }
+        
+        return $short;
     }
     
     public function getWhatsplaying($p_offset)
@@ -335,4 +393,12 @@ class uiTwitter {
         }
         return false;
     }
+    
+    public function twitterify($ret) {
+        $ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $ret);
+        $ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $ret);
+        $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
+        $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
+        return $ret;
+    }
 }
\ No newline at end of file