]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
trac31 url_auto_shortening by sgmurphy
authormillette <millette@controlyourself.ca>
Thu, 6 Nov 2008 20:59:26 +0000 (15:59 -0500)
committermillette <millette@controlyourself.ca>
Thu, 6 Nov 2008 20:59:26 +0000 (15:59 -0500)
darcs-hash:20081106205926-099f7-6bcfd7969a159a12b1ba6a9ee254e44a07b94761.gz

12 files changed:
actions/newmessage.php
actions/newnotice.php
actions/postnotice.php
actions/profilesettings.php
actions/twitapidirect_messages.php
actions/twitapistatuses.php
classes/Command.php
classes/User.php
classes/laconica.ini
db/laconica.sql
lib/settingsaction.php
lib/util.php

index 7dce97d20be7dec6c8aa0006a885df607f59fdf5..2455f42e947959fb40f5cd07ba4bf4d78b3aa5c0 100644 (file)
@@ -53,10 +53,15 @@ class NewmessageAction extends Action {
                        $this->show_form(_('No content!'));
                        return;
                } else if (mb_strlen($content) > 140) {
-                       common_debug("Content = '$content'", __FILE__);
-                       common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
-                       $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
-                       return;
+               
+                       $content = common_shorten_links($content);
+
+                       if (mb_strlen($content) > 140) {
+                               common_debug("Content = '$content'", __FILE__);
+                               common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
+                               $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
+                               return;
+                       }
                }
 
                $other = User::staticGet('id', $to);
index 37cca982d6d882f2e2c950a88f1333efce9a15ad..8ea47b235ba8d956bffedd3f2945811098764dfa 100644 (file)
@@ -51,10 +51,15 @@ class NewnoticeAction extends Action {
                        $this->show_form(_('No content!'));
                        return;
                } else if (mb_strlen($content) > 140) {
-                       common_debug("Content = '$content'", __FILE__);
-                       common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
-                       $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
-                       return;
+                       
+                       $content = common_shorten_links($content);
+                       
+                       if (mb_strlen($content) > 140) {
+                               common_debug("Content = '$content'", __FILE__);
+                               common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
+                               $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
+                               return;
+                       }
                }
 
                $inter = new CommandInterpreter();
index be486a1e9df39491e1b79aa59e298859ea8eb5fb..b4a272e6182c8e2b2874eb60dc8dba13fabe335d 100644 (file)
@@ -59,8 +59,11 @@ class PostnoticeAction extends Action {
                }
                $content = $req->get_parameter('omb_notice_content');
                if (!$content || strlen($content) > 140) {
-                       common_user_error(_('Invalid notice content'), 400);
-                       return false;
+                       $content = common_shorten_links($content);
+                       if (mb_strlen($content) > 140) {
+                               common_user_error(_('Invalid notice content'), 400);
+                               return false;
+                       }
                }
                $notice_uri = $req->get_parameter('omb_notice');
                if (!Validate::uri($notice_uri) &&
index e5698011ac0b7769ab3bea9fe122153e30871271..2ae736087635b16628f740a2fa3f4835f79143d3 100644 (file)
@@ -224,8 +224,8 @@ class ProfilesettingsAction extends SettingsAction {
                } else if ($this->nickname_exists($nickname)) {
                        $this->show_form(_('Nickname already in use. Try another one.'));
                        return;
-                } else if (!is_null($language) && strlen($language) > 50) {
-                        $this->show_form(_('Language is too long (max 50 chars).'));
+        } else if (!is_null($language) && strlen($language) > 50) {
+               $this->show_form(_('Language is too long (max 50 chars).'));
                }
 
                $user = common_current_user();
index a0a09a4108dc02d06efc30f0008035dd4bbc5be3..3037890ad22440a585a4643ef07def3c69449b3b 100644 (file)
@@ -113,9 +113,12 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
                if (!$content) {
                        $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
                } else if (mb_strlen($status) > 140) {
-                       $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
-                               $code = 406, $apidata['content-type']);
-                       return;
+                       $status = common_shorten_links($status);
+                       if (mb_strlen($status) > 140) {
+                               $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
+                                       $code = 406, $apidata['content-type']);
+                               return;
+                       }
                }
 
                $other = $this->get_user($this->trimmed('user'));
index 539a0b1b3b408f2cdfddbae9942216eee1719a19..ca79feb4c6ed7b9fd43e2237f64fe64b7dfaef3a 100644 (file)
@@ -253,13 +253,19 @@ class TwitapistatusesAction extends TwitterapiAction {
                        return;
 
                } else if (mb_strlen($status) > 140) {
+                       
+                       $status = common_shorten_links($status);
 
-                       // XXX: Twitter truncates anything over 140, flags the status
-                   // as "truncated."  Sending this error may screw up some clients
-                   // that assume Twitter will truncate for them.  Should we just
-                   // truncate too? -- Zach
-                       $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
-                       return;
+                       if (mb_strlen($status) > 140) {
+
+                               // XXX: Twitter truncates anything over 140, flags the status
+                           // as "truncated."  Sending this error may screw up some clients
+                           // that assume Twitter will truncate for them.  Should we just
+                           // truncate too? -- Zach
+                               $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
+                               return;
+                               
+                       }
                }
 
                // Check for commands
index 0795f86afbc12d6f16f75abddd1c609a8b4d8885..c2409d140a2ac152c86ca780ffe36d288833bf42 100644 (file)
@@ -197,9 +197,14 @@ class MessageCommand extends Command {
                        $channel->error($this->user, _('No content!'));
                        return;
                } else if ($len > 140) {
-                       $channel->error($this->user, sprintf(_('Message too long - maximum is 140 characters, you sent %d'), $len));
-                       return;
-               } else if (!$other) {
+                       $content = common_shorten_links($content);
+                       if (mb_strlen($content) > 140) {
+                               $channel->error($this->user, sprintf(_('Message too long - maximum is 140 characters, you sent %d'), $len));
+                               return;
+                       }
+               }
+               
+               if (!$other) {
                        $channel->error($this->user, _('No such user.'));
                        return;
                } else if (!$this->user->mutuallySubscribed($other)) {
index 916524b8e8ac90ceef33992780a70e46e261a67f..e3fa93395e322f7ad7f68ff0fa1a6f43f4f02c41 100644 (file)
@@ -55,6 +55,7 @@ class User extends Memcached_DataObject
     public $smsemail;                        // varchar(255)  
     public $uri;                             // varchar(255)  unique_key
     public $autosubscribe;                   // tinyint(1)  
+       public $urlshorteningservice;                    // varchar(50)
     public $created;                         // datetime()   not_null
     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
 
index 480b872d1138a33a0ec81399d467b1e7ac55ff26..aa0225739e6632e34f0b679eeb1a956c3ad36161 100644 (file)
@@ -295,6 +295,7 @@ smsreplies = 17
 smsemail = 2
 uri = 2
 autosubscribe = 17
+urlshorteningservice = 2
 created = 142
 modified = 384
 
index 872481ace4486af886a9a18061f76440b8155504..d9a6dbf86a5750755f5db38a2489ae5972e1799b 100644 (file)
@@ -65,6 +65,7 @@ create table user (
     smsemail varchar(255) comment 'built from sms and carrier',
     uri varchar(255) unique key comment 'universally unique identifier, usually a tag URI',
     autosubscribe tinyint default 0 comment 'automatically subscribe to users who subscribe to us',
+       urlshorteningservice varchar(50) default 'metamark.net' comment 'service to use for auto-shortening URLs',
     created datetime not null comment 'date this record was created',
     modified timestamp comment 'date this record was modified',
 
index 705ccf3eb673856a5db249e46835b9e3628bda47..9e783431f23586073429c867546e3cddbc7b3a89 100644 (file)
@@ -97,7 +97,10 @@ class SettingsAction extends Action {
                                          _('Updates by instant messenger (IM)')),
                                'twittersettings' =>
                                array(_('Twitter'),
-                                         _('Twitter integration options')));
+                                         _('Twitter integration options')),
+                               'othersettings' =>
+                               array(_('Other'),
+                                         _('Other options')));
                
         $action = $this->trimmed('action');
         common_element_start('ul', array('id' => 'nav_views'));
index a5eeab0566f319ac6066535b70df44fc65fa8661..edddcd2a70547ee1523cc5eaa2e56b595904c28e 100644 (file)
@@ -747,6 +747,50 @@ function common_render_uri_thingy($matches) {
        return '<a href="' . $uri . '" class="extlink">' . $uri . '</a>' . $trailer;
 }
 
+function common_shorten_links($text) {
+       $r = htmlspecialchars($text);
+       $r = preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $r);
+       return $r;
+}
+
+function common_shorten_link($long_url) {
+       
+       $user = common_current_user();
+       
+       $curlh = curl_init();
+       curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait
+       curl_setopt($curlh, CURLOPT_USERAGENT, 'Laconica');
+       curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true);
+       
+       switch($user->urlshorteningservice) {
+               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;
+       }
+       
+       curl_close($curlh);
+       
+       if ($short_url) {
+               return $short_url;
+       }
+       return $long_url;
+}
+
 function common_xml_safe_str($str) {
        $xmlStr = htmlentities(iconv('UTF-8', 'UTF-8//IGNORE', $str), ENT_NOQUOTES, 'UTF-8');