Merge branch 'ostatus-crop' into 0.9.x

This commit is contained in:
Brion Vibber 2010-03-17 16:41:01 -07:00
commit 5d1295f233
6 changed files with 157 additions and 57 deletions

View File

@ -399,58 +399,70 @@ var SN = { // StatusNet
return; return;
} }
$.fn.jOverlay.options = { var attachment_more = notice.find('.attachment.more');
method : 'GET', if (attachment_more.length > 0) {
data : '', attachment_more.click(function() {
url : '', $.get($(this).attr('href')+'/ajax', null, function(data) {
color : '#000', notice.find('.entry-title .entry-content').html($(data).find('#attachment_view .entry-content').html());
opacity : '0.6', });
zIndex : 9999,
center : false,
imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
bgClickToClose : true,
success : function() {
$('#jOverlayContent').append('<button class="close">&#215;</button>');
$('#jOverlayContent button').click($.closeOverlay);
},
timeout : 0,
autoHide : true,
css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
};
notice.find('a.attachment').click(function() {
var attachId = ($(this).attr('id').substring('attachment'.length + 1));
if (attachId) {
$().jOverlay({url: $('address .url')[0].href+'attachment/' + attachId + '/ajax'});
return false; return false;
} });
}); }
else {
if ($('#shownotice').length == 0) { $.fn.jOverlay.options = {
var t; method : 'GET',
notice.find('a.thumbnail').hover( data : '',
function() { url : '',
var anchor = $(this); color : '#000',
$('a.thumbnail').children('img').hide(); opacity : '0.6',
anchor.closest(".entry-title").addClass('ov'); zIndex : 9999,
center : false,
if (anchor.children('img').length === 0) { imgLoading : $('address .url')[0].href+'theme/base/images/illustrations/illu_progress_loading-01.gif',
t = setTimeout(function() { bgClickToClose : true,
$.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) { success : function() {
anchor.append(data); $('#jOverlayContent').append('<button class="close">&#215;</button>');
}); $('#jOverlayContent button').click($.closeOverlay);
}, 500);
}
else {
anchor.children('img').show();
}
}, },
function() { timeout : 0,
clearTimeout(t); autoHide : true,
$('a.thumbnail').children('img').hide(); css : {'max-width':'542px', 'top':'5%', 'left':'32.5%'}
$(this).closest('.entry-title').removeClass('ov'); };
notice.find('a.attachment').click(function() {
var attachId = ($(this).attr('id').substring('attachment'.length + 1));
if (attachId) {
$().jOverlay({url: $('address .url')[0].href+'attachment/' + attachId + '/ajax'});
return false;
} }
); });
if ($('#shownotice').length == 0) {
var t;
notice.find('a.thumbnail').hover(
function() {
var anchor = $(this);
$('a.thumbnail').children('img').hide();
anchor.closest(".entry-title").addClass('ov');
if (anchor.children('img').length === 0) {
t = setTimeout(function() {
$.get($('address .url')[0].href+'attachment/' + (anchor.attr('id').substring('attachment'.length + 1)) + '/thumbnail', null, function(data) {
anchor.append(data);
});
}, 500);
}
else {
anchor.children('img').show();
}
},
function() {
clearTimeout(t);
$('a.thumbnail').children('img').hide();
$(this).closest('.entry-title').removeClass('ov');
}
);
}
} }
}, },

View File

@ -330,6 +330,13 @@ class Attachment extends AttachmentListItem
$this->out->element('param', array('name' => 'autoStart', 'value' => 1)); $this->out->element('param', array('name' => 'autoStart', 'value' => 1));
$this->out->elementEnd('object'); $this->out->elementEnd('object');
break; break;
case 'text/html':
if ($this->attachment->filename) {
// Locally-uploaded HTML. Scrub and display inline.
$this->showHtmlFile($this->attachment);
}
break;
} }
} }
} else { } else {
@ -356,5 +363,58 @@ class Attachment extends AttachmentListItem
} }
} }
} }
protected function showHtmlFile(File $attachment)
{
$body = $this->scrubHtmlFile($attachment);
if ($body) {
$this->out->raw($body);
}
}
/**
* @return mixed false on failure, HTML fragment string on success
*/
protected function scrubHtmlFile(File $attachment)
{
$path = File::path($attachment->filename);
if (!file_exists($path) || !is_readable($path)) {
common_log(LOG_ERR, "Missing local HTML attachment $path");
return false;
}
$raw = file_get_contents($path);
// Normalize...
$dom = new DOMDocument();
if(!$dom->loadHTML($raw)) {
common_log(LOG_ERR, "Bad HTML in local HTML attachment $path");
return false;
}
// Remove <script>s or htmlawed will dump their contents into output!
// Note: removing child nodes while iterating seems to mess things up,
// hence the double loop.
$scripts = array();
foreach ($dom->getElementsByTagName('script') as $script) {
$scripts[] = $script;
}
foreach ($scripts as $script) {
common_log(LOG_DEBUG, $script->textContent);
$script->parentNode->removeChild($script);
}
// Trim out everything outside the body...
$body = $dom->saveHTML();
$body = preg_replace('/^.*<body[^>]*>/is', '', $body);
$body = preg_replace('/<\/body[^>]*>.*$/is', '', $body);
require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
$config = array('safe' => 1,
'deny_attribute' => 'id,style,on*',
'comment' => 1); // remove comments
$scrubbed = htmLawed($body, $config);
return $scrubbed;
}
} }

View File

@ -542,14 +542,25 @@ class Ostatus_profile extends Memcached_DataObject
} }
$shortSummary = common_shorten_links($summary); $shortSummary = common_shorten_links($summary);
if (Notice::contentTooLong($shortSummary)) { if (Notice::contentTooLong($shortSummary)) {
$url = common_shorten_url(common_local_url('attachment', $url = common_shorten_url($sourceUrl);
array('attachment' => $attachment->id)));
$shortSummary = substr($shortSummary, $shortSummary = substr($shortSummary,
0, 0,
Notice::maxContent() - (mb_strlen($url) + 2)); Notice::maxContent() - (mb_strlen($url) + 2));
$shortSummary .= '… ' . $url; $shortSummary .= '…';
$content = $shortSummary; $content = $shortSummary . ' ' . $url;
$rendered = common_render_text($content);
// We mark up the attachment link specially for the HTML output
// so we can fold-out the full version inline.
$attachUrl = common_local_url('attachment',
array('attachment' => $attachment->id));
$rendered = common_render_text($shortSummary) .
' ' .
'<a href="' .
htmlspecialchars($attachUrl) .
'" class="attachment more">' .
// TRANS: expansion link for too-long remote messages
htmlspecialchars(_m('more')) .
'</a>';
} }
} }
@ -1526,10 +1537,18 @@ class Ostatus_profile extends Memcached_DataObject
throw new Exception("Couldn't find a valid profile for '$addr'"); throw new Exception("Couldn't find a valid profile for '$addr'");
} }
/**
* Store the full-length scrubbed HTML of a remote notice to an attachment
* file on our server. We'll link to this at the end of the cropped version.
*
* @param string $title plaintext for HTML page's title
* @param string $rendered HTML fragment for HTML page's body
* @return File
*/
function saveHTMLFile($title, $rendered) function saveHTMLFile($title, $rendered)
{ {
$final = sprintf("<!DOCTYPE html>\n<html><head><title>%s</title></head>". $final = sprintf("<!DOCTYPE html>\n<html><head><title>%s</title></head>".
'<body><div>%s</div></body></html>', '<body>%s</body></html>',
htmlspecialchars($title), htmlspecialchars($title),
$rendered); $rendered);

View File

@ -1284,9 +1284,16 @@ height:16px;
position:relative; position:relative;
padding-left:16px; padding-left:16px;
} }
#attachments .attachment { #attachments .attachment,
.notice .attachment.more {
padding-left:0; padding-left:0;
} }
.notice .attachment.more:before {
content:'( ';
}
.notice .attachment.more:after {
content:' )';
}
.notice .attachment img { .notice .attachment img {
position:absolute; position:absolute;
top:18px; top:18px;

View File

@ -410,7 +410,8 @@ background-position: 0 -1714px;
.notice .attachment { .notice .attachment {
background-position:0 -394px; background-position:0 -394px;
} }
#attachments .attachment { #attachments .attachment,
.notice .attachment.more {
background:none; background:none;
} }
.notice-options .notice_reply { .notice-options .notice_reply {

View File

@ -409,7 +409,8 @@ background-position: 0 -1714px;
.notice .attachment { .notice .attachment {
background-position:0 -394px; background-position:0 -394px;
} }
#attachments .attachment { #attachments .attachment,
.notice .attachment.more {
background:none; background:none;
} }
.notice-options .notice_reply { .notice-options .notice_reply {