From ce0883330306ccb9bd50aef4d7963351284bd698 Mon Sep 17 00:00:00 2001 From: millette Date: Fri, 28 Nov 2008 16:01:14 -0500 Subject: [PATCH] shorten urls for posts > 140 chars only, from anywhere. Only show long urls in title attributes for links we shortened ourselves. darcs-hash:20081128210114-099f7-4e4cde0a983c2ac6d41efb59b46cb7dbf45dc7a6.gz --- actions/newmessage.php | 9 ++++----- actions/newnotice.php | 10 +++++----- actions/postnotice.php | 12 +++++------- actions/twitapidirect_messages.php | 4 ++-- actions/twitapistatuses.php | 6 +++--- classes/Message.php | 2 +- classes/Notice.php | 2 +- lib/util.php | 6 +++--- scripts/maildaemon.php | 5 ++++- scripts/xmppdaemon.php | 10 +++++++++- 10 files changed, 37 insertions(+), 29 deletions(-) diff --git a/actions/newmessage.php b/actions/newmessage.php index d4e289465a..67695210ec 100644 --- a/actions/newmessage.php +++ b/actions/newmessage.php @@ -51,13 +51,12 @@ class NewmessageAction extends Action { if (!$content) { $this->show_form(_('No content!')); return; -// } else if (mb_strlen($content) > 140) { } else { - $content = common_shorten_links($content); + $content_shortened = common_shorten_links($content); - if (mb_strlen($content) > 140) { - common_debug("Content = '$content'", __FILE__); - common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__); + if (mb_strlen($content_shortened) > 140) { + common_debug("Content = '$content_shortened'", __FILE__); + common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__); $this->show_form(_('That\'s too long. Max message size is 140 chars.')); return; } diff --git a/actions/newnotice.php b/actions/newnotice.php index a79d0a1a2f..932099c605 100644 --- a/actions/newnotice.php +++ b/actions/newnotice.php @@ -51,11 +51,11 @@ class NewnoticeAction extends Action { $this->show_form(_('No content!')); return; } else { - $content = common_shorten_links($content); + $content_shortened = common_shorten_links($content); - if (mb_strlen($content) > 140) { - common_debug("Content = '$content'", __FILE__); - common_debug("mb_strlen(\$content) = " . mb_strlen($content), __FILE__); + if (mb_strlen($content_shortened) > 140) { + common_debug("Content = '$content_shortened'", __FILE__); + common_debug("mb_strlen(\$content) = " . mb_strlen($content_shortened), __FILE__); $this->show_form(_('That\'s too long. Max notice size is 140 chars.')); return; } @@ -63,7 +63,7 @@ class NewnoticeAction extends Action { $inter = new CommandInterpreter(); - $cmd = $inter->handle_command($user, $content); + $cmd = $inter->handle_command($user, $content_shortened); if ($cmd) { $cmd->execute(new WebChannel()); diff --git a/actions/postnotice.php b/actions/postnotice.php index 243aa31633..243081f122 100644 --- a/actions/postnotice.php +++ b/actions/postnotice.php @@ -58,13 +58,11 @@ class PostnoticeAction extends Action { return false; } $content = $req->get_parameter('omb_notice_content'); -// if (!$content || strlen($content) > 140) { - $content = common_shorten_links($content); - if (mb_strlen($content) > 140) { - common_user_error(_('Invalid notice content'), 400); - return false; - } -// } + $content_shortened = common_shorten_links($content); + if (mb_strlen($content_shortened) > 140) { + common_user_error(_('Invalid notice content'), 400); + return false; + } $notice_uri = $req->get_parameter('omb_notice'); if (!Validate::uri($notice_uri) && !common_valid_tag($notice_uri)) { diff --git a/actions/twitapidirect_messages.php b/actions/twitapidirect_messages.php index a04ae5fa76..a14a9e567c 100644 --- a/actions/twitapidirect_messages.php +++ b/actions/twitapidirect_messages.php @@ -115,8 +115,8 @@ class Twitapidirect_messagesAction extends TwitterapiAction { $this->client_error(_('No message text!'), $code = 406, $apidata['content-type']); // } else if (mb_strlen($status) > 140) { } else { - $status = common_shorten_links($status); - if (mb_strlen($status) > 140) { + $content_shortened = common_shorten_links($content); + if (mb_strlen($content_shortened) > 140) { $this->client_error(_('That\'s too long. Max message size is 140 chars.'), $code = 406, $apidata['content-type']); return; diff --git a/actions/twitapistatuses.php b/actions/twitapistatuses.php index 6d6d5266ff..947d210326 100644 --- a/actions/twitapistatuses.php +++ b/actions/twitapistatuses.php @@ -255,9 +255,9 @@ class TwitapistatusesAction extends TwitterapiAction { // } else if (mb_strlen($status) > 140) { } else { - $status = common_shorten_links($status); + $status_shortened = common_shorten_links($status); - if (mb_strlen($status) > 140) { + if (mb_strlen($status_shortened) > 140) { // XXX: Twitter truncates anything over 140, flags the status // as "truncated." Sending this error may screw up some clients @@ -271,7 +271,7 @@ class TwitapistatusesAction extends TwitterapiAction { // Check for commands $inter = new CommandInterpreter(); - $cmd = $inter->handle_command($user, $status); + $cmd = $inter->handle_command($user, $status_shortened); if ($cmd) { diff --git a/classes/Message.php b/classes/Message.php index 8674f7f80c..ef4bd03161 100644 --- a/classes/Message.php +++ b/classes/Message.php @@ -41,7 +41,7 @@ class Message extends Memcached_DataObject $msg->from_profile = $from; $msg->to_profile = $to; - $msg->content = $content; + $msg->content = common_shorten_links($content); $msg->rendered = common_render_text($content); $msg->created = common_sql_now(); $msg->source = $source; diff --git a/classes/Notice.php b/classes/Notice.php index ceec7a2fa6..83862ae959 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -127,7 +127,7 @@ class Notice extends Memcached_DataObject $notice->reply_to = $reply_to; $notice->created = common_sql_now(); - $notice->content = $content; + $notice->content = common_shorten_links($content); $notice->rendered = common_render_content($notice->content, $notice); $notice->source = $source; $notice->uri = $uri; diff --git a/lib/util.php b/lib/util.php index b14d121e65..c3d11b074c 100644 --- a/lib/util.php +++ b/lib/util.php @@ -837,10 +837,10 @@ function common_longurl2($uri) { function common_shorten_links($text) { if (mb_strlen($text) <= 140) return $text; + static $cache = array(); + if (isset($cache[$text])) return $cache[$text]; // \s = not a horizontal whitespace character (since PHP 5.2.4) - // RYM this should prevent * preceded URLs from being processed but it its a char -// $r = preg_replace('@[^*](https?://[^)\]>\s]+)@e', "common_shorten_link('\\1')", $r); - return preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text); + return $cache[$text] = preg_replace('@https?://[^)\]>\s]+@e', "common_shorten_link('\\0')", $text); } function common_shorten_link($long_url) { diff --git a/scripts/maildaemon.php b/scripts/maildaemon.php index 64f913bf92..8b809f646e 100755 --- a/scripts/maildaemon.php +++ b/scripts/maildaemon.php @@ -120,6 +120,9 @@ class MailerDaemon { } function add_notice($user, $msg) { + // should test + // $msg_shortened = common_shorten_links($msg); + // if (mb_strlen($msg_shortened) > 140) ERROR and STOP $notice = Notice::saveNew($user->id, $msg, 'mail'); if (is_string($notice)) { $this->log(LOG_ERR, $notice); @@ -209,4 +212,4 @@ class MailerDaemon { } $md = new MailerDaemon(); -$md->handle_message('php://stdin'); \ No newline at end of file +$md->handle_message('php://stdin'); diff --git a/scripts/xmppdaemon.php b/scripts/xmppdaemon.php index 90506a0f42..e55b9e3e9d 100755 --- a/scripts/xmppdaemon.php +++ b/scripts/xmppdaemon.php @@ -236,7 +236,15 @@ class XMPPDaemon extends Daemon { } function add_notice(&$user, &$pl) { - $notice = Notice::saveNew($user->id, trim(mb_substr($pl['body'], 0, 140)), 'xmpp'); + $content_shortened = common_shorten_link($pl['body']); + if (mb_strlen($content_shortened) > 140) { + $content = trim(mb_substr($pl['body'], 0, 140)); + $content_shortened = common_shorten_link($content); + } + else { + $content = $pl['body']; + } + $notice = Notice::saveNew($user->id, $content, 'xmpp'); if (is_string($notice)) { $this->log(LOG_ERR, $notice); return; -- 2.39.5