Unifying HTML stripping functions to common_strip_html

This commit is contained in:
Mikael Nordfeldth 2014-07-14 13:52:23 +02:00
parent e0a10f775f
commit ae62b91940
9 changed files with 23 additions and 27 deletions

View File

@ -384,7 +384,7 @@ class ApiTimelineUserAction extends ApiBareAuthAction
// Get (safe!) HTML and text versions of the content // Get (safe!) HTML and text versions of the content
$rendered = $this->purify($sourceContent); $rendered = $this->purify($sourceContent);
$content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8'); $content = common_strip_html($rendered);
$shortened = $this->auth_user->shortenLinks($content); $shortened = $this->auth_user->shortenLinks($content);

View File

@ -769,9 +769,10 @@ class Notice extends Managed_DataObject
$stored->verb = $act->verb; $stored->verb = $act->verb;
// Use the local user's shortening preferences, if applicable. // Use the local user's shortening preferences, if applicable.
$stored->content = $actor->isLocal() $stored->rendered = $actor->isLocal()
? $actor->shortenLinks($act->content) ? $actor->shortenLinks($act->content)
: $act->content; : $act->content;
$stored->content = common_strip_html($stored->rendered);
$autosource = common_config('public', 'autosource'); $autosource = common_config('public', 'autosource');

View File

@ -214,7 +214,7 @@ class ActivityImporter extends QueueHandler
// Get (safe!) HTML and text versions of the content // Get (safe!) HTML and text versions of the content
$rendered = $this->purify($sourceContent); $rendered = $this->purify($sourceContent);
$content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8'); $content = common_strip_html($rendered);
$shortened = $user->shortenLinks($content); $shortened = $user->shortenLinks($content);

View File

@ -200,7 +200,7 @@ class ActivityObject
$title = ActivityUtils::childHtmlContent($element, self::TITLE); $title = ActivityUtils::childHtmlContent($element, self::TITLE);
if (!empty($title)) { if (!empty($title)) {
$this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8'); $this->title = common_strip_html($title);
} }
// fall back to <atom:name> // fall back to <atom:name>
@ -251,10 +251,7 @@ class ActivityObject
$this->content = ActivityUtils::getContent($element); $this->content = ActivityUtils::getContent($element);
// We don't like HTML in our titles, although it's technically allowed // We don't like HTML in our titles, although it's technically allowed
$this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
$title = ActivityUtils::childHtmlContent($element, self::TITLE);
$this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
$this->source = $this->_getSource($element); $this->source = $this->_getSource($element);

View File

@ -2360,3 +2360,12 @@ function common_log_delta($comment=null)
common_debug(sprintf("%s: %d %d", $comment, $mtotal, round($ttotal * 1000000))); common_debug(sprintf("%s: %d %d", $comment, $mtotal, round($ttotal * 1000000)));
} }
function common_strip_html($html, $trim=true, $save_whitespace=false)
{
if (!$save_whitespace) {
$html = preg_replace('/\s+/', ' ', $html);
}
$text = html_entity_decode(strip_tags($html), ENT_QUOTES, 'UTF-8');
return $trim ? trim($text) : $text;
}

View File

@ -174,10 +174,10 @@ class Blog_entry extends Managed_DataObject
XMLStringer::estring('a', array('href' => $url, XMLStringer::estring('a', array('href' => $url,
'class' => 'blog-entry'), 'class' => 'blog-entry'),
_('More...')); _('More...'));
$text = html_entity_decode(strip_tags($be->summary), ENT_QUOTES, 'UTF-8'); $text = common_strip_html($be->summary);
} else { } else {
$options['rendered'] = $be->content; $options['rendered'] = $be->content;
$text = html_entity_decode(strip_tags($be->content), ENT_QUOTES, 'UTF-8'); $text = common_strip_html($be->content);
} }

View File

@ -341,7 +341,7 @@ class DomainStatusNetworkInstaller extends Installer
$breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/', $breakout = preg_replace('/<a[^>+]\bhref="(.*)"[^>]*>(.*)<\/a>/',
'\2 &lt;\1&gt;', '\2 &lt;\1&gt;',
$html); $html);
return html_entity_decode(strip_tags($breakout), ENT_QUOTES, 'UTF-8'); return common_strip_html($breakout);
} }
function databaseize($nickname) function databaseize($nickname)

View File

@ -607,7 +607,7 @@ class Ostatus_profile extends Managed_DataObject
// Get (safe!) HTML and text versions of the content // Get (safe!) HTML and text versions of the content
$rendered = $this->purify($sourceContent); $rendered = $this->purify($sourceContent);
$content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8'); $content = common_strip_html($rendered);
$shortened = common_shorten_links($content); $shortened = common_shorten_links($content);
@ -618,7 +618,7 @@ class Ostatus_profile extends Managed_DataObject
if (Notice::contentTooLong($shortened)) { if (Notice::contentTooLong($shortened)) {
$attachment = $this->saveHTMLFile($activity->title, $rendered); $attachment = $this->saveHTMLFile($activity->title, $rendered);
$summary = html_entity_decode(strip_tags($activity->summary), ENT_QUOTES, 'UTF-8'); $summary = common_strip_html($activity->summary);
if (empty($summary)) { if (empty($summary)) {
$summary = $content; $summary = $content;
} }
@ -774,7 +774,7 @@ class Ostatus_profile extends Managed_DataObject
// Get (safe!) HTML and text versions of the content // Get (safe!) HTML and text versions of the content
$rendered = $this->purify($sourceContent); $rendered = $this->purify($sourceContent);
$content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8'); $content = common_strip_html($rendered);
$shortened = common_shorten_links($content); $shortened = common_shorten_links($content);
@ -785,7 +785,7 @@ class Ostatus_profile extends Managed_DataObject
if (Notice::contentTooLong($shortened)) { if (Notice::contentTooLong($shortened)) {
$attachment = $this->saveHTMLFile($note->title, $rendered); $attachment = $this->saveHTMLFile($note->title, $rendered);
$summary = html_entity_decode(strip_tags($note->summary), ENT_QUOTES, 'UTF-8'); $summary = common_strip_html($note->summary);
if (empty($summary)) { if (empty($summary)) {
$summary = $content; $summary = $content;
} }

View File

@ -104,17 +104,6 @@ class TinyMCEPlugin extends Plugin
return htmLawed($raw, $config); return htmLawed($raw, $config);
} }
/**
* Strip HTML to plaintext string
*
* @param string $html HTML
* @return string plaintext, single line
*/
private function stripHtml($html)
{
return str_replace("\n", " ", html_entity_decode(strip_tags($html), ENT_QUOTES, 'UTF-8'));
}
/** /**
* Hook for new-notice form processing to take our HTML goodies; * Hook for new-notice form processing to take our HTML goodies;
* won't affect API posting etc. * won't affect API posting etc.
@ -130,7 +119,7 @@ class TinyMCEPlugin extends Plugin
if ($action->arg('richedit') && $this->isAllowedRichEdit()) { if ($action->arg('richedit') && $this->isAllowedRichEdit()) {
$html = $this->sanitizeHtml($content); $html = $this->sanitizeHtml($content);
$options['rendered'] = $html; $options['rendered'] = $html;
$content = $this->stripHtml($html); $content = common_strip_html($html);
} }
return true; return true;
} }