trac31 url_auto_shortening by sgmurphy

darcs-hash:20081106205926-099f7-6bcfd7969a159a12b1ba6a9ee254e44a07b94761.gz
This commit is contained in:
millette 2008-11-06 15:59:26 -05:00
parent 1e8d26baec
commit 15c1d4f5e4
12 changed files with 102 additions and 25 deletions

View File

@ -53,11 +53,16 @@ class NewmessageAction extends Action {
$this->show_form(_('No content!')); $this->show_form(_('No content!'));
return; return;
} else if (mb_strlen($content) > 140) { } else if (mb_strlen($content) > 140) {
$content = common_shorten_links($content);
if (mb_strlen($content) > 140) {
common_debug("Content = '$content'", __FILE__); common_debug("Content = '$content'", __FILE__);
common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__); common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
$this->show_form(_('That\'s too long. Max message size is 140 chars.')); $this->show_form(_('That\'s too long. Max message size is 140 chars.'));
return; return;
} }
}
$other = User::staticGet('id', $to); $other = User::staticGet('id', $to);

View File

@ -51,11 +51,16 @@ class NewnoticeAction extends Action {
$this->show_form(_('No content!')); $this->show_form(_('No content!'));
return; return;
} else if (mb_strlen($content) > 140) { } else if (mb_strlen($content) > 140) {
$content = common_shorten_links($content);
if (mb_strlen($content) > 140) {
common_debug("Content = '$content'", __FILE__); common_debug("Content = '$content'", __FILE__);
common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__); common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__);
$this->show_form(_('That\'s too long. Max notice size is 140 chars.')); $this->show_form(_('That\'s too long. Max notice size is 140 chars.'));
return; return;
} }
}
$inter = new CommandInterpreter(); $inter = new CommandInterpreter();

View File

@ -59,9 +59,12 @@ class PostnoticeAction extends Action {
} }
$content = $req->get_parameter('omb_notice_content'); $content = $req->get_parameter('omb_notice_content');
if (!$content || strlen($content) > 140) { if (!$content || strlen($content) > 140) {
$content = common_shorten_links($content);
if (mb_strlen($content) > 140) {
common_user_error(_('Invalid notice content'), 400); common_user_error(_('Invalid notice content'), 400);
return false; return false;
} }
}
$notice_uri = $req->get_parameter('omb_notice'); $notice_uri = $req->get_parameter('omb_notice');
if (!Validate::uri($notice_uri) && if (!Validate::uri($notice_uri) &&
!common_valid_tag($notice_uri)) { !common_valid_tag($notice_uri)) {

View File

@ -113,10 +113,13 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
if (!$content) { if (!$content) {
$this->client_error(_('No message text!'), $code = 406, $apidata['content-type']); $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']);
} else if (mb_strlen($status) > 140) { } else if (mb_strlen($status) > 140) {
$status = common_shorten_links($status);
if (mb_strlen($status) > 140) {
$this->client_error(_('That\'s too long. Max message size is 140 chars.'), $this->client_error(_('That\'s too long. Max message size is 140 chars.'),
$code = 406, $apidata['content-type']); $code = 406, $apidata['content-type']);
return; return;
} }
}
$other = $this->get_user($this->trimmed('user')); $other = $this->get_user($this->trimmed('user'));

View File

@ -254,12 +254,18 @@ class TwitapistatusesAction extends TwitterapiAction {
} else if (mb_strlen($status) > 140) { } else if (mb_strlen($status) > 140) {
$status = common_shorten_links($status);
if (mb_strlen($status) > 140) {
// XXX: Twitter truncates anything over 140, flags the status // XXX: Twitter truncates anything over 140, flags the status
// as "truncated." Sending this error may screw up some clients // as "truncated." Sending this error may screw up some clients
// that assume Twitter will truncate for them. Should we just // that assume Twitter will truncate for them. Should we just
// truncate too? -- Zach // truncate too? -- Zach
$this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']); $this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
return; return;
}
} }
// Check for commands // Check for commands

View File

@ -197,9 +197,14 @@ class MessageCommand extends Command {
$channel->error($this->user, _('No content!')); $channel->error($this->user, _('No content!'));
return; return;
} else if ($len > 140) { } else if ($len > 140) {
$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)); $channel->error($this->user, sprintf(_('Message too long - maximum is 140 characters, you sent %d'), $len));
return; return;
} else if (!$other) { }
}
if (!$other) {
$channel->error($this->user, _('No such user.')); $channel->error($this->user, _('No such user.'));
return; return;
} else if (!$this->user->mutuallySubscribed($other)) { } else if (!$this->user->mutuallySubscribed($other)) {

View File

@ -55,6 +55,7 @@ class User extends Memcached_DataObject
public $smsemail; // varchar(255) public $smsemail; // varchar(255)
public $uri; // varchar(255) unique_key public $uri; // varchar(255) unique_key
public $autosubscribe; // tinyint(1) public $autosubscribe; // tinyint(1)
public $urlshorteningservice; // varchar(50)
public $created; // datetime() not_null public $created; // datetime() not_null
public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP

View File

@ -295,6 +295,7 @@ smsreplies = 17
smsemail = 2 smsemail = 2
uri = 2 uri = 2
autosubscribe = 17 autosubscribe = 17
urlshorteningservice = 2
created = 142 created = 142
modified = 384 modified = 384

View File

@ -65,6 +65,7 @@ create table user (
smsemail varchar(255) comment 'built from sms and carrier', smsemail varchar(255) comment 'built from sms and carrier',
uri varchar(255) unique key comment 'universally unique identifier, usually a tag URI', 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', 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', created datetime not null comment 'date this record was created',
modified timestamp comment 'date this record was modified', modified timestamp comment 'date this record was modified',

View File

@ -97,7 +97,10 @@ class SettingsAction extends Action {
_('Updates by instant messenger (IM)')), _('Updates by instant messenger (IM)')),
'twittersettings' => 'twittersettings' =>
array(_('Twitter'), array(_('Twitter'),
_('Twitter integration options'))); _('Twitter integration options')),
'othersettings' =>
array(_('Other'),
_('Other options')));
$action = $this->trimmed('action'); $action = $this->trimmed('action');
common_element_start('ul', array('id' => 'nav_views')); common_element_start('ul', array('id' => 'nav_views'));

View File

@ -747,6 +747,50 @@ function common_render_uri_thingy($matches) {
return '<a href="' . $uri . '" class="extlink">' . $uri . '</a>' . $trailer; 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) { function common_xml_safe_str($str) {
$xmlStr = htmlentities(iconv('UTF-8', 'UTF-8//IGNORE', $str), ENT_NOQUOTES, 'UTF-8'); $xmlStr = htmlentities(iconv('UTF-8', 'UTF-8//IGNORE', $str), ENT_NOQUOTES, 'UTF-8');